aboutsummaryrefslogtreecommitdiff
path: root/objects/object_file_test.go
blob: f0d45c32c09b58f13fd43d4a74fc730aaec4b039 (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
88
package objects

import (
	"bytes"
	"testing"
)

var (
	testFileObj = File{
		FileFragment{Blob: genId(0x11), Size: 10},
		FileFragment{Blob: genId(0x22), Size: 20},
		FileFragment{Blob: genId(0x33), Size: 30},
		FileFragment{Blob: genId(0x44), Size: 40},
		FileFragment{Blob: genId(0x55), Size: 50},
	}

	testFileSerialization = []byte("" +
		"blob=sha3-256:1111111111111111111111111111111111111111111111111111111111111111&size=10\n" +
		"blob=sha3-256:2222222222222222222222222222222222222222222222222222222222222222&size=20\n" +
		"blob=sha3-256:3333333333333333333333333333333333333333333333333333333333333333&size=30\n" +
		"blob=sha3-256:4444444444444444444444444444444444444444444444444444444444444444&size=40\n" +
		"blob=sha3-256:5555555555555555555555555555555555555555555555555555555555555555&size=50\n")
)

func TestSerializeFile(t *testing.T) {
	have := testFileObj.Payload()

	if !bytes.Equal(have, testFileSerialization) {
		t.Errorf("Unexpected serialization result: %s", have)
	}
}

func TestSerializeEmptyFile(t *testing.T) {
	f := File{}

	have := f.Payload()
	want := []byte{}

	if !bytes.Equal(have, want) {
		t.Errorf("Unexpected serialization result: %s", have)
	}
}

func TestUnserializeFile(t *testing.T) {
	have := File{}
	err := have.FromPayload(testFileSerialization)

	if err != nil {
		t.Fatalf("Unexpected error: %s", err)
	}

	if !have.Equals(testFileObj) {
		t.Errorf("Unexpeced unserialization result: %v", have)
	}
}

func TestUnserializeEmptyFile(t *testing.T) {
	have := File{}
	err := have.FromPayload([]byte{})

	if err != nil {
		t.Fatalf("Unexpected error: %s", err)
	}

	if len(have) != 0 {
		t.Errorf("Unexpeced unserialization result: %v", have)
	}
}

func TestUnserializeFileFailure(t *testing.T) {
	subtests := []struct{ name, payload string }{
		{"missing blob", "size=100\n"},
		{"empty blob", "blob=&size=100"},
		{"invalid blob", "blob=foobar&size=100"}, // Variations of invalid IDs are tested elsewhere
		{"missing size", "blob=sha3-256:0000000000000000000000000000000000000000000000000000000000000000\n"},
		{"empty size", "blob=sha3-256:0000000000000000000000000000000000000000000000000000000000000000&size=\n"},
		{"invalid size", "blob=sha3-256:0000000000000000000000000000000000000000000000000000000000000000&size=foobar\n"},
		{"no props", "foobar\n"},
	}

	for _, subtest := range subtests {
		have := File{}
		err := have.FromPayload([]byte(subtest.payload))
		if err == nil {
			t.Errorf("Unexpected unserialization success: %v", have)
		}
	}
}