closes #7253
[arvados.git] / sdk / go / blockdigest / blockdigest_test.go
1 package blockdigest
2
3 import (
4         "fmt"
5         "runtime"
6         "strings"
7         "testing"
8 )
9
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])
14 }
15
16 func expectEqual(t *testing.T, actual interface{}, expected interface{}) {
17         if actual != expected {
18                 t.Fatalf("Expected %v but received %v instead. %s",
19                         expected,
20                         actual,
21                         getStackTrace())
22         }
23 }
24
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())
28         }
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())
32                 }
33         }
34 }
35
36 func expectValidDigestString(t *testing.T, s string) {
37         bd, err := FromString(s)
38         if err != nil {
39                 t.Fatalf("Expected %s to produce a valid BlockDigest but instead got error: %v", s, err)
40         }
41
42         expected := strings.ToLower(s)
43
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())
46         }
47 }
48
49 func expectInvalidDigestString(t *testing.T, s string) {
50         _, err := FromString(s)
51         if err == nil {
52                 t.Fatalf("Expected %s to be an invalid BlockDigest, but did not receive an error", s)
53         }
54 }
55
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)
60 }
61
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.",
65                         s)
66         }
67 }
68
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.",
72                         s)
73         }
74 }
75
76 func TestValidDigestStrings(t *testing.T) {
77         expectValidDigestString(t, "01234567890123456789abcdefabcdef")
78         expectValidDigestString(t, "01234567890123456789ABCDEFABCDEF")
79         expectValidDigestString(t, "01234567890123456789AbCdEfaBcDeF")
80 }
81
82 func TestInvalidDigestStrings(t *testing.T) {
83         expectInvalidDigestString(t, "01234567890123456789abcdefabcdeg")
84         expectInvalidDigestString(t, "01234567890123456789abcdefabcde")
85         expectInvalidDigestString(t, "01234567890123456789abcdefabcdefa")
86         expectInvalidDigestString(t, "g1234567890123456789abcdefabcdef")
87 }
88
89 func TestBlockDigestWorksAsMapKey(t *testing.T) {
90         m := make(map[BlockDigest]int)
91         bd := AssertFromString("01234567890123456789abcdefabcdef")
92         m[bd] = 5
93 }
94
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)
102         }
103 }
104
105 func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) {
106         input := "01234567890123456789abcdefabcdef"
107         value := 42
108         nested := struct {
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
112                 // different.
113                 Digest BlockDigest
114                 value  int
115         }{
116                 AssertFromString(input),
117                 value,
118         }
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)
125         }
126 }
127
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@")
136
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")
151 }
152
153 func TestParseBlockLocatorSimple(t *testing.T) {
154         b, err := ParseBlockLocator("365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf")
155         if err != nil {
156                 t.Fatalf("Unexpected error parsing block locator: %v", err)
157         }
158         expectBlockLocator(t, b, BlockLocator{Digest: AssertFromString("365f83f5f808896ec834c8b595288735"),
159                 Size: 2310,
160                 Hints: []string{"K@qr1hi",
161                         "Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}})
162 }