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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package objects
import (
"bufio"
"bytes"
"fmt"
"testing"
)
func TestUnserializeSuccess(t *testing.T) {
r := bufio.NewReader(bytes.NewBuffer([]byte("blob 16\n0123456789abcdef")))
o, err := Unserialize(r)
if err != nil {
t.Fatalf("Unserialize failed: %s", err)
}
if o.Type != OTBlob {
t.Errorf("expected type %s, got %s", OTBlob, o.Type)
}
if !bytes.Equal([]byte("0123456789abcdef"), o.Payload) {
t.Errorf("Unexpected payload: (size %d) %v", len(o.Payload), o.Payload)
}
}
func TestUnserializeSuccess0Payload(t *testing.T) {
r := bufio.NewReader(bytes.NewBuffer([]byte("blob 0\n")))
o, err := Unserialize(r)
if err != nil {
t.Fatalf("Unserialize failed: %s", err)
}
if o.Type != OTBlob {
t.Errorf("expected type %s, got %s", OTBlob, o.Type)
}
if !bytes.Equal([]byte{}, o.Payload) {
t.Errorf("Unexpected payload: (size %d) %v", len(o.Payload), o.Payload)
}
}
func unserializeMustFail(b []byte, t *testing.T) {
r := bufio.NewReader(bytes.NewBuffer(b))
o, err := Unserialize(r)
if err == nil {
t.Fatalf("Expected an error, but object was successfully read: %#v", o)
}
_, ok := err.(UnserializeError)
if !ok {
t.Fatalf("Unknown error, expected an UnserializeError: %#v", err)
}
}
func TestUnserializeInvalidEmpty(t *testing.T) {
unserializeMustFail([]byte{}, t)
}
func TestUnserializeIncompleteHeader(t *testing.T) {
unserializeMustFail([]byte("foo\nbar"), t)
}
func TestUnserializeInvalidNumber(t *testing.T) {
unserializeMustFail([]byte("blob abc\nbar"), t)
}
func TestUnserializePayloadTooSmall(t *testing.T) {
unserializeMustFail([]byte("blob 10\nbar"), t)
}
func TestSerialize(t *testing.T) {
o := RawObject{
Type: OTBlob,
Payload: []byte("foo bar\nbaz"),
}
buf := new(bytes.Buffer)
if err := o.Serialize(buf); err != nil {
t.Fatalf("Serialization failed:%s", err)
}
b := buf.Bytes()
if !bytes.Equal(b, []byte("blob 11\nfoo bar\nbaz")) {
t.Errorf("Unexpected serialization result: %v", b)
}
}
func TestSerializeAndId(t *testing.T) {
o := RawObject{
Type: OTBlob,
Payload: []byte("foo bar\nbaz"),
}
buf := new(bytes.Buffer)
id, err := o.SerializeAndId(buf, OIdAlgoDefault)
if err != nil {
t.Fatalf("Serialization failed:%s", err)
}
if !id.VerifyObject(o) {
t.Errorf("Verification failed")
}
}
func unserializeObj(t *testing.T, ot ObjectType, b []byte) Object {
ro, err := Unserialize(bytes.NewReader(b))
if err != nil {
t.Fatalf("Failed unserializing: %s", err)
}
o, err := ro.Object()
if err != nil {
t.Fatalf("Failed generating Object from RawObject: %s", err)
}
if o.Type() != ot {
t.Fatalf("Unexpected object type, have %s, want %s", o.Type(), ot)
}
return o
}
func TestUnserializeBlobObj(t *testing.T) {
blob := unserializeObj(t, OTBlob, []byte(""+
"blob 6\n"+
"foobar")).(*Blob)
if string(*blob) != "foobar" {
t.Errorf("Unexpected blob content: %v", blob)
}
}
func TestUnserializeFileObj(t *testing.T) {
file := unserializeObj(t, OTFile, append(
[]byte(fmt.Sprintf("file %d\n", len(testFileSerialization))),
testFileSerialization...,
)).(*File)
if !file.Equals(testFileObj) {
t.Errorf("Unexpected file object: %v", *file)
}
}
func TestUnserializeTreeObj(t *testing.T) {
tree := unserializeObj(t, OTTree, append(
[]byte(fmt.Sprintf("tree %d\n", len(testTreeSerialization))),
testTreeSerialization...,
)).(Tree)
if !tree.Equals(testTreeObj) {
t.Errorf("Unexpected tree object: %v", tree)
}
}
func TestUnserializeSnapshotObj(t *testing.T) {
snapshot := unserializeObj(t, OTSnapshot, append(
[]byte(fmt.Sprintf("snapshot %d\n", len(testSnapshotSerialization))),
testSnapshotSerialization...,
)).(*Snapshot)
if !snapshot.Equals(testSnapshotObj) {
t.Errorf("Unexpected snapshot object: %v", *snapshot)
}
}
|