From fb14fb157c0835d61cbc7f8425d89b941bd0ab16 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 25 Jun 2017 10:20:26 +0200 Subject: Implement object IDs --- objects/id_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 objects/id_test.go (limited to 'objects/id_test.go') diff --git a/objects/id_test.go b/objects/id_test.go new file mode 100644 index 0000000..361798d --- /dev/null +++ b/objects/id_test.go @@ -0,0 +1,77 @@ +package objects + +import ( + "testing" +) + +func TestParseValidId(t *testing.T) { + have, err := ParseObjectId("sha3-256:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + want := ObjectId{ + Algo: OIdAlgoSHA3_256, + Sum: []byte{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + }, + } + + if !have.Equals(want) { + t.Errorf("unexpected result, want: '%s', have: '%s'", want, have) + } +} + +func TestParseMalformedIds(t *testing.T) { + malformed := []string{ + "", // Empty not permitted + "sha3-256", // Missing : + "sha3-256:", // Missing hex sum + ":abcdef", // Missing algo + "foobar:abcdef", // Basic format ok, but unknown algo + "sha3-256:foobar", // Not hexadecimal + "sha3-256:abcdef", // sum length mismatch + } + + for _, s := range malformed { + oid, err := ParseObjectId(s) + if err == nil { + t.Errorf("'%s' resulted in valid id (%s), expected error", s, oid) + } + } +} + +func TestParseSerialize(t *testing.T) { + want := ObjectId{ + Algo: OIdAlgoSHA3_256, + Sum: []byte{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + }, + } + + have, err := ParseObjectId(want.String()) + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + if !have.Equals(want) { + t.Errorf("unexpected result, want: '%s', have: '%s'", want, have) + } +} + +func TestSerializeParse(t *testing.T) { + want := "sha3-256:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" + + oid, err := ParseObjectId(want) + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + have := oid.String() + + if have != want { + t.Errorf("unexpected result, want: '%s', have: '%s'", want, have) + } +} -- cgit v1.2.3-54-g00ecf