1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 func getStackTrace() string {
15 buf := make([]byte, 1000)
16 bytes_written := runtime.Stack(buf, false)
17 return "Stack Trace:\n" + string(buf[:bytes_written])
20 func expectEqual(t *testing.T, actual interface{}, expected interface{}) {
21 if actual != expected {
22 t.Fatalf("Expected %v but received %v instead. %s",
29 func expectStringSlicesEqual(t *testing.T, actual []string, expected []string) {
30 if len(actual) != len(expected) {
31 t.Fatalf("Expected %v (length %d), but received %v (length %d) instead. %s", expected, len(expected), actual, len(actual), getStackTrace())
33 for i := range actual {
34 if actual[i] != expected[i] {
35 t.Fatalf("Expected %v but received %v instead (first disagreement at position %d). %s", expected, actual, i, getStackTrace())
40 func expectValidDigestString(t *testing.T, s string) {
41 bd, err := FromString(s)
43 t.Fatalf("Expected %s to produce a valid BlockDigest but instead got error: %v", s, err)
46 expected := strings.ToLower(s)
48 if expected != bd.String() {
49 t.Fatalf("Expected %s to be returned by FromString(%s).String() but instead we received %s", expected, s, bd.String())
53 func expectInvalidDigestString(t *testing.T, s string) {
54 _, err := FromString(s)
56 t.Fatalf("Expected %s to be an invalid BlockDigest, but did not receive an error", s)
60 func expectBlockLocator(t *testing.T, actual BlockLocator, expected BlockLocator) {
61 expectEqual(t, actual.Digest, expected.Digest)
62 expectEqual(t, actual.Size, expected.Size)
63 expectStringSlicesEqual(t, actual.Hints, expected.Hints)
66 func expectLocatorPatternMatch(t *testing.T, s string) {
67 if !LocatorPattern.MatchString(s) {
68 t.Fatalf("Expected \"%s\" to match locator pattern but it did not.",
73 func expectLocatorPatternFail(t *testing.T, s string) {
74 if LocatorPattern.MatchString(s) {
75 t.Fatalf("Expected \"%s\" to fail locator pattern but it passed.",
80 func TestValidDigestStrings(t *testing.T) {
81 expectValidDigestString(t, "01234567890123456789abcdefabcdef")
82 expectValidDigestString(t, "01234567890123456789ABCDEFABCDEF")
83 expectValidDigestString(t, "01234567890123456789AbCdEfaBcDeF")
86 func TestInvalidDigestStrings(t *testing.T) {
87 expectInvalidDigestString(t, "01234567890123456789abcdefabcdeg")
88 expectInvalidDigestString(t, "01234567890123456789abcdefabcde")
89 expectInvalidDigestString(t, "01234567890123456789abcdefabcdefa")
90 expectInvalidDigestString(t, "g1234567890123456789abcdefabcdef")
93 func TestBlockDigestWorksAsMapKey(t *testing.T) {
94 m := make(map[BlockDigest]int)
95 bd, err := FromString("01234567890123456789abcdefabcdef")
97 t.Fatalf("Unexpected error during FromString for block: %v", err)
102 func TestBlockDigestGetsPrettyPrintedByPrintf(t *testing.T) {
103 input := "01234567890123456789abcdefabcdef"
104 fromString, err := FromString(input)
106 t.Fatalf("Unexpected error during FromString: %v", err)
108 prettyPrinted := fmt.Sprintf("%v", fromString)
109 if prettyPrinted != input {
110 t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as "+
111 "\"%s\", but instead it was printed as %s",
112 input, input, prettyPrinted)
116 func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) {
117 input, err := FromString("01234567890123456789abcdefabcdef")
119 t.Fatalf("Unexpected error during FromString for block: %v", err)
123 // Fun trivia fact: If this field was called "digest" instead of
124 // "Digest", then it would not be exported and String() would
125 // never get called on it and our output would look very
133 prettyPrinted := fmt.Sprintf("%+v", nested)
134 expected := fmt.Sprintf("{Digest:%s value:%d}", input, value)
135 if prettyPrinted != expected {
136 t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as "+
137 "\"%s\", but instead it was printed as %s",
138 input, expected, prettyPrinted)
142 func TestLocatorPatternBasic(t *testing.T) {
143 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345")
144 expectLocatorPatternMatch(t, "A2345678901234abcdefababdeffdfdf+12345")
145 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A1")
146 expectLocatorPatternMatch(t,
147 "12345678901234567890123456789012+12345+A1+B123wxyz@_-")
148 expectLocatorPatternMatch(t,
149 "12345678901234567890123456789012+12345+A1+B123wxyz@_-+C@")
150 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A")
151 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A1+B")
152 expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A+B2")
154 expectLocatorPatternFail(t, "12345678901234567890123456789012")
155 expectLocatorPatternFail(t, "12345678901234567890123456789012+")
156 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+")
157 expectLocatorPatternFail(t, "1234567890123456789012345678901+12345")
158 expectLocatorPatternFail(t, "123456789012345678901234567890123+12345")
159 expectLocatorPatternFail(t, "g2345678901234abcdefababdeffdfdf+12345")
160 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345 ")
161 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1")
162 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1A")
163 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+a1")
164 expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+")
168 func TestParseBlockLocatorSimple(t *testing.T) {
169 b, err := ParseBlockLocator("365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf")
171 t.Fatalf("Unexpected error parsing block locator: %v", err)
173 d, err := FromString("365f83f5f808896ec834c8b595288735")
175 t.Fatalf("Unexpected error during FromString for block: %v", err)
177 expectBlockLocator(t, b, BlockLocator{Digest: d,
179 Hints: []string{"K@qr1hi",
180 "Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}})