aboutsummaryrefslogtreecommitdiff
path: root/objects/id_test.go
blob: 5ca4da9c7e7f2634a3170f34f2bcb04433ca6b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package objects

import (
	"testing"
)

func genId(b byte) (oid ObjectId) {
	oid.Algo = OIdAlgoSHA3_256
	oid.Sum = make([]byte, OIdAlgoSHA3_256.sumLength())
	for i := 0; i < OIdAlgoSHA3_256.sumLength(); i++ {
		oid.Sum[i] = b
	}

	return
}

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)
	}
}