X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/47d1bdc0960af5bfc8f2793c352f60483539c389..c63ff55687f32dfdff01b9827b411b3757d48ee7:/sdk/go/blockdigest/blockdigest_test.go diff --git a/sdk/go/blockdigest/blockdigest_test.go b/sdk/go/blockdigest/blockdigest_test.go index 068a1385ae..017aaa4710 100644 --- a/sdk/go/blockdigest/blockdigest_test.go +++ b/sdk/go/blockdigest/blockdigest_test.go @@ -2,10 +2,37 @@ package blockdigest import ( "fmt" + "runtime" "strings" "testing" ) +func getStackTrace() string { + buf := make([]byte, 1000) + bytes_written := runtime.Stack(buf, false) + return "Stack Trace:\n" + string(buf[:bytes_written]) +} + +func expectEqual(t *testing.T, actual interface{}, expected interface{}) { + if actual != expected { + t.Fatalf("Expected %v but received %v instead. %s", + expected, + actual, + getStackTrace()) + } +} + +func expectStringSlicesEqual(t *testing.T, actual []string, expected []string) { + if len(actual) != len(expected) { + t.Fatalf("Expected %v (length %d), but received %v (length %d) instead. %s", expected, len(expected), actual, len(actual), getStackTrace()) + } + for i := range actual { + if actual[i] != expected[i] { + t.Fatalf("Expected %v but received %v instead (first disagreement at position %d). %s", expected, actual, i, getStackTrace()) + } + } +} + func expectValidDigestString(t *testing.T, s string) { bd, err := FromString(s) if err != nil { @@ -13,7 +40,7 @@ func expectValidDigestString(t *testing.T, s string) { } expected := strings.ToLower(s) - + if expected != bd.String() { t.Fatalf("Expected %s to be returned by FromString(%s).String() but instead we received %s", expected, s, bd.String()) } @@ -26,6 +53,26 @@ func expectInvalidDigestString(t *testing.T, s string) { } } +func expectBlockLocator(t *testing.T, actual BlockLocator, expected BlockLocator) { + expectEqual(t, actual.Digest, expected.Digest) + expectEqual(t, actual.Size, expected.Size) + expectStringSlicesEqual(t, actual.Hints, expected.Hints) +} + +func expectLocatorPatternMatch(t *testing.T, s string) { + if !LocatorPattern.MatchString(s) { + t.Fatalf("Expected \"%s\" to match locator pattern but it did not.", + s) + } +} + +func expectLocatorPatternFail(t *testing.T, s string) { + if LocatorPattern.MatchString(s) { + t.Fatalf("Expected \"%s\" to fail locator pattern but it passed.", + s) + } +} + func TestValidDigestStrings(t *testing.T) { expectValidDigestString(t, "01234567890123456789abcdefabcdef") expectValidDigestString(t, "01234567890123456789ABCDEFABCDEF") @@ -49,7 +96,7 @@ func TestBlockDigestGetsPrettyPrintedByPrintf(t *testing.T) { input := "01234567890123456789abcdefabcdef" prettyPrinted := fmt.Sprintf("%v", AssertFromString(input)) if prettyPrinted != input { - t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as " + + t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as "+ "\"%s\", but instead it was printed as %s", input, input, prettyPrinted) } @@ -58,13 +105,13 @@ func TestBlockDigestGetsPrettyPrintedByPrintf(t *testing.T) { func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) { input := "01234567890123456789abcdefabcdef" value := 42 - nested := struct{ + nested := struct { // Fun trivia fact: If this field was called "digest" instead of // "Digest", then it would not be exported and String() would // never get called on it and our output would look very // different. Digest BlockDigest - value int + value int }{ AssertFromString(input), value, @@ -72,8 +119,44 @@ func TestBlockDigestGetsPrettyPrintedByPrintfInNestedStructs(t *testing.T) { prettyPrinted := fmt.Sprintf("%+v", nested) expected := fmt.Sprintf("{Digest:%s value:%d}", input, value) if prettyPrinted != expected { - t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as " + + t.Fatalf("Expected blockDigest produced from \"%s\" to be printed as "+ "\"%s\", but instead it was printed as %s", input, expected, prettyPrinted) } } + +func TestLocatorPatternBasic(t *testing.T) { + expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345") + expectLocatorPatternMatch(t, "A2345678901234abcdefababdeffdfdf+12345") + expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A1") + expectLocatorPatternMatch(t, + "12345678901234567890123456789012+12345+A1+B123wxyz@_-") + expectLocatorPatternMatch(t, + "12345678901234567890123456789012+12345+A1+B123wxyz@_-+C@") + + expectLocatorPatternFail(t, "12345678901234567890123456789012") + expectLocatorPatternFail(t, "12345678901234567890123456789012+") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+") + expectLocatorPatternFail(t, "1234567890123456789012345678901+12345") + expectLocatorPatternFail(t, "123456789012345678901234567890123+12345") + expectLocatorPatternFail(t, "g2345678901234abcdefababdeffdfdf+12345") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345 ") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1A") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+a1") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+B") + expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A+B2") +} + +func TestParseBlockLocatorSimple(t *testing.T) { + b, err := ParseBlockLocator("365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf") + if err != nil { + t.Fatalf("Unexpected error parsing block locator: %v", err) + } + expectBlockLocator(t, b, BlockLocator{Digest: AssertFromString("365f83f5f808896ec834c8b595288735"), + Size: 2310, + Hints: []string{"K@qr1hi", + "Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}}) +}