aboutsummaryrefslogtreecommitdiff
path: root/objects/id_test.go
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2017-06-25 10:20:26 +0200
committerLaria Carolin Chabowski <laria@laria.me>2017-06-25 17:44:49 +0200
commitfb14fb157c0835d61cbc7f8425d89b941bd0ab16 (patch)
treeeb8a1d6e00cd4024f2f088326853be2174e7869d /objects/id_test.go
parent82dd9f540be12c4c93679d2a60c4d1950e57eec8 (diff)
downloadpetrific-fb14fb157c0835d61cbc7f8425d89b941bd0ab16.tar.gz
petrific-fb14fb157c0835d61cbc7f8425d89b941bd0ab16.tar.bz2
petrific-fb14fb157c0835d61cbc7f8425d89b941bd0ab16.zip
Implement object IDs
Diffstat (limited to 'objects/id_test.go')
-rw-r--r--objects/id_test.go77
1 files changed, 77 insertions, 0 deletions
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)
+ }
+}