10 func getStackTrace() string {
11 buf := make([]byte, 1000)
12 bytes_written := runtime.Stack(buf, false)
13 return "Stack Trace:\n" + string(buf[:bytes_written])
16 func expectEqual(t *testing.T, actual interface{}, expected interface{}) {
17 if actual != expected {
18 t.Fatalf("Expected %v but received %v instead. %s",
25 func expectStringSlicesEqual(t *testing.T, actual []string, expected []string) {
26 if len(actual) != len(expected) {
27 t.Fatalf("Expected %v (length %d), but received %v (length %d) instead. %s", expected, len(expected), actual, len(actual), getStackTrace())
29 for i := range actual {
30 if actual[i] != expected[i] {
31 t.Fatalf("Expected %v but received %v instead (first disagreement at position %d). %s", expected, actual, i, getStackTrace())
36 func expectValidDigestString(t *testing.T, s string) {
37 bd, err := FromString(s)
39 t.Fatalf("Expected %s to produce a valid BlockDigest but instead got error: %v", s, err)
42 expected := strings.ToLower(s)
44 if expected != bd.String() {
45 t.Fatalf("Expected %s to be returned by FromString(%s).String() but instead we received %s", expected, s, bd.String())
49 func expectInvalidDigestString(t *testing.T, s string) {
50 _, err := FromString(s)
52 t.Fatalf("Expected %s to be an invalid BlockDigest, but did not receive an error", s)
56 func expectBlockLocator(t *testing.T, actual BlockLocator, expected BlockLocator) {
57 expectEqual(t, actual.Digest, expected.Digest)
58 expectEqual(t, actual.Size, expected.Size)
59 expectStringSlicesEqual(t, actual.Hints, expected.Hints)
62 func expectLocatorPatternMatch(t *testing.T, s string) {
63 if !LocatorPattern.MatchString(s) {
64 t.Fatalf("Expected \"%s\" to match locator pattern but it did not.",
69 func expectLocatorPatternFail(t *testing.T, s string) {
70 if LocatorPattern.MatchString(s) {
71 t.Fatalf("Expected \"%s\" to fail locator pattern but it passed.",
76 func TestValidDigestStrings(t *testing.T) {
77 expectValidDigestString(t, "01234567890123456789abcdefabcdef")
78 expectValidDigestString(t, "01234567890123456789ABCDEFABCDEF")
79 expectValidDigestString(t, "01234567890123456789AbCdEfaBcDeF")
82 func TestInvalidDigestStrings(t *testing.T) {
83 expectInvalidDigestString(t, "01234567890123456789abcdefabcdeg")
84 expectInvalidDigestString(t, "01234567890123456789abcdefabcde")
85 expectInvalidDigestString(t, "01234567890123456789abcdefabcdefa")
86 expectInvalidDigestString(t, "g1234567890123456789abcdefabcdef")
89 func TestBlockDigestWorksAsMapKey(t *testing.T) {
90 m := make(map[BlockDigest]int)
91 bd := AssertFromString("01234567890123456789abcdefabcdef")
95 func TestBlockDigestGetsPrettyPrintedByPrintf(t *testing.T) {
96 input := "01234567890123456789abcdefabcdef"
97 prettyPrinted := fmt.Sprintf("%v", AssertFromString(input))
98 if prettyPrinted != input {
99 t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as "+
100 "\"%s\", but instead it was printed as %s",
101 input, input, prettyPrinted)
105 func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) {
106 input := "01234567890123456789abcdefabcdef"
109 // Fun trivia fact: If this field was called "digest" instead of
110 // "Digest", then it would not be exported and String() would
111 // never get called on it and our output would look very
116 AssertFromString(input),
119 prettyPrinted := fmt.Sprintf("%+v", nested)
120 expected := fmt.Sprintf("{Digest:%s value:%d}", input, value)
121 if prettyPrinted != expected {
122 t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as "+
123 "\"%s\", but instead it was printed as %s",
124 input, expected, prettyPrinted)
128 func TestLocatorPatternBasic(t *testing.T) {
129 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345")
130 expectLocatorPatternMatch(t, "A2345678901234abcdefababdeffdfdf+12345")
131 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A1")
132 expectLocatorPatternMatch(t,
133 "12345678901234567890123456789012+12345+A1+B123wxyz@_-")
134 expectLocatorPatternMatch(t,
135 "12345678901234567890123456789012+12345+A1+B123wxyz@_-+C@")
137 expectLocatorPatternFail(t, "12345678901234567890123456789012")
138 expectLocatorPatternFail(t, "12345678901234567890123456789012+")
139 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+")
140 expectLocatorPatternFail(t, "1234567890123456789012345678901+12345")
141 expectLocatorPatternFail(t, "123456789012345678901234567890123+12345")
142 expectLocatorPatternFail(t, "g2345678901234abcdefababdeffdfdf+12345")
143 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345 ")
144 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1")
145 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1A")
146 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A")
147 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+a1")
148 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+")
149 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+B")
150 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A+B2")
153 func TestParseBlockLocatorSimple(t *testing.T) {
154 b, err := ParseBlockLocator("365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf")
156 t.Fatalf("Unexpected error parsing block locator: %v", err)
158 expectBlockLocator(t, b, BlockLocator{Digest: AssertFromString("365f83f5f808896ec834c8b595288735"),
160 Hints: []string{"K@qr1hi",
161 "Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}})