-
Notifications
You must be signed in to change notification settings - Fork 44
/
car_test.go
247 lines (229 loc) · 6.52 KB
/
car_test.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package car_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/internal/carv1"
"github.com/stretchr/testify/assert"
)
func TestCarV2PragmaLength(t *testing.T) {
tests := []struct {
name string
want interface{}
got interface{}
}{
{
"ActualSizeShouldBe11",
11,
len(carv2.Pragma),
},
{
"ShouldStartWithVarint(10)",
carv2.Pragma[0],
10,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert.EqualValues(t, tt.want, tt.got, "CarV2Pragma got = %v, want %v", tt.got, tt.want)
})
}
}
func TestCarV2PragmaIsValidCarV1Header(t *testing.T) {
v1h, err := carv1.ReadHeader(bytes.NewReader(carv2.Pragma), carv1.DefaultMaxAllowedHeaderSize)
assert.NoError(t, err, "cannot decode pragma as CBOR with CARv1 header structure")
assert.Equal(t, &carv1.CarHeader{
Roots: nil,
Version: 2,
}, v1h, "CARv2 pragma must be a valid CARv1 header")
}
func TestHeader_WriteTo(t *testing.T) {
tests := []struct {
name string
target carv2.Header
wantWrite []byte
wantErr bool
}{
{
"HeaderWithEmptyCharacteristicsIsWrittenAsExpected",
carv2.Header{
Characteristics: carv2.Characteristics{},
DataOffset: 99,
},
[]byte{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x63, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
},
false,
},
{
"NonEmptyHeaderIsWrittenAsExpected",
carv2.Header{
Characteristics: carv2.Characteristics{
Hi: 1001, Lo: 1002,
},
DataOffset: 99,
DataSize: 100,
IndexOffset: 101,
},
[]byte{
0xe9, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xea, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x63, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x64, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x65, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
written, err := tt.target.WriteTo(buf)
if (err != nil) != tt.wantErr {
t.Errorf("WriteTo() error = %v, wantErr %v", err, tt.wantErr)
return
}
gotWrite := buf.Bytes()
assert.Equal(t, tt.wantWrite, gotWrite, "Header.WriteTo() gotWrite = %v, wantWrite %v", gotWrite, tt.wantWrite)
assert.EqualValues(t, carv2.HeaderSize, uint64(len(gotWrite)), "WriteTo() CARv2 header length must always be %v bytes long", carv2.HeaderSize)
assert.EqualValues(t, carv2.HeaderSize, uint64(written), "WriteTo() CARv2 header byte count must always be %v bytes long", carv2.HeaderSize)
})
}
}
func TestHeader_ReadFrom(t *testing.T) {
tests := []struct {
name string
target []byte
wantHeader carv2.Header
wantErr bool
}{
{
"HeaderWithEmptyCharacteristicsIsWrittenAsExpected",
[]byte{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x63, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x64, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
},
carv2.Header{
Characteristics: carv2.Characteristics{},
DataOffset: 99,
DataSize: 100,
},
false,
},
{
"NonEmptyHeaderIsWrittenAsExpected",
[]byte{
0xe9, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xea, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x63, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x64, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x65, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
},
carv2.Header{
Characteristics: carv2.Characteristics{
Hi: 1001, Lo: 1002,
},
DataOffset: 99,
DataSize: 100,
IndexOffset: 101,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHeader := carv2.Header{}
gotRead, err := gotHeader.ReadFrom(bytes.NewReader(tt.target))
assert.NoError(t, err)
assert.Equal(t, int64(carv2.HeaderSize), gotRead)
assert.Equal(t, tt.wantHeader, gotHeader)
})
}
}
func TestHeader_WithPadding(t *testing.T) {
tests := []struct {
name string
subject carv2.Header
wantCarV1Offset uint64
wantIndexOffset uint64
}{
{
"WhenNoPaddingOffsetsAreSumOfSizes",
carv2.NewHeader(123),
carv2.PragmaSize + carv2.HeaderSize,
carv2.PragmaSize + carv2.HeaderSize + 123,
},
{
"WhenOnlyPaddingCarV1BothOffsetsShift",
carv2.NewHeader(123).WithDataPadding(3),
carv2.PragmaSize + carv2.HeaderSize + 3,
carv2.PragmaSize + carv2.HeaderSize + 3 + 123,
},
{
"WhenPaddingBothCarV1AndIndexBothOffsetsShiftWithAdditionalIndexShift",
carv2.NewHeader(123).WithDataPadding(3).WithIndexPadding(7),
carv2.PragmaSize + carv2.HeaderSize + 3,
carv2.PragmaSize + carv2.HeaderSize + 3 + 123 + 7,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualValues(t, tt.wantCarV1Offset, tt.subject.DataOffset)
assert.EqualValues(t, tt.wantIndexOffset, tt.subject.IndexOffset)
})
}
}
func TestNewHeaderHasExpectedValues(t *testing.T) {
wantCarV1Len := uint64(1413)
want := carv2.Header{
Characteristics: carv2.Characteristics{},
DataOffset: carv2.PragmaSize + carv2.HeaderSize,
DataSize: wantCarV1Len,
IndexOffset: carv2.PragmaSize + carv2.HeaderSize + wantCarV1Len,
}
got := carv2.NewHeader(wantCarV1Len)
assert.Equal(t, want, got, "NewHeader got = %v, want = %v", got, want)
}
func TestCharacteristics_StoreIdentityCIDs(t *testing.T) {
subject := carv2.Characteristics{}
require.False(t, subject.IsFullyIndexed())
subject.SetFullyIndexed(true)
require.True(t, subject.IsFullyIndexed())
var buf bytes.Buffer
written, err := subject.WriteTo(&buf)
require.NoError(t, err)
require.Equal(t, int64(16), written)
require.Equal(t, []byte{
0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
}, buf.Bytes())
var decodedSubject carv2.Characteristics
read, err := decodedSubject.ReadFrom(&buf)
require.NoError(t, err)
require.Equal(t, int64(16), read)
require.True(t, decodedSubject.IsFullyIndexed())
buf.Reset()
subject.SetFullyIndexed(false)
require.False(t, subject.IsFullyIndexed())
written, err = subject.WriteTo(&buf)
require.NoError(t, err)
require.Equal(t, int64(16), written)
require.Equal(t, []byte{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
}, buf.Bytes())
var decodedSubjectAgain carv2.Characteristics
read, err = decodedSubjectAgain.ReadFrom(&buf)
require.NoError(t, err)
require.Equal(t, int64(16), read)
require.False(t, decodedSubjectAgain.IsFullyIndexed())
}