Skip to content

Commit 39a3c23

Browse files
committed
feat: check for last element, or no roots
1 parent ba816e4 commit 39a3c23

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

tests/t0118_gateway_car_test.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func TestGatewayCarPathing(t *testing.T) {
2929
Status(200).
3030
Body(
3131
IsCar().
32-
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid()).
32+
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid("subdir", "ascii.txt")).
33+
MightHaveNoRoots().
3334
HasBlocks(
3435
subdirTwoSingleBlockFilesFixture.MustGetCid(),
3536
subdirTwoSingleBlockFilesFixture.MustGetCid("subdir"),
@@ -53,7 +54,8 @@ func TestGatewayCarPathing(t *testing.T) {
5354
Status(200).
5455
Body(
5556
IsCar().
56-
HasRoot(singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
57+
HasRoot(singleLayerHamtMultiBlockFilesFixture.MustGetCid("685.txt")).
58+
MightHaveNoRoots().
5759
HasBlocks(flattenStrings(t,
5860
singleLayerHamtMultiBlockFilesFixture.MustGetCid(),
5961
singleLayerHamtMultiBlockFilesFixture.MustGetCIDsInHAMTTraversal(nil, "685.txt"),
@@ -78,7 +80,8 @@ func TestGatewayCarPathing(t *testing.T) {
7880
Status(200).
7981
Body(
8082
IsCar().
81-
HasRoot(dirWithDagCborWithLinksFixture.MustGetCid("document")).
83+
HasRoot(dirWithDagCborWithLinksFixture.MustGetCid("document", "files", "single")).
84+
MightHaveNoRoots().
8285
HasBlocks(flattenStrings(t,
8386
dirWithDagCborWithLinksFixture.MustGetCid("document"),
8487
dirWithDagCborWithLinksFixture.MustGetCid("document", "files", "single"),
@@ -112,7 +115,8 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
112115
Status(200).
113116
Body(
114117
IsCar().
115-
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid()).
118+
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid("subdir")).
119+
MightHaveNoRoots().
116120
HasBlocks(
117121
subdirTwoSingleBlockFilesFixture.MustGetCid(),
118122
subdirTwoSingleBlockFilesFixture.MustGetCid("subdir"),
@@ -135,7 +139,8 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
135139
Status(200).
136140
Body(
137141
IsCar().
138-
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid()).
142+
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid("subdir", "ascii.txt")).
143+
MightHaveNoRoots().
139144
HasBlocks(
140145
subdirTwoSingleBlockFilesFixture.MustGetCid(),
141146
subdirTwoSingleBlockFilesFixture.MustGetCid("subdir"),
@@ -161,7 +166,8 @@ func TestGatewayCarDagScopeBlock(t *testing.T) {
161166
Status(200).
162167
Body(
163168
IsCar().
164-
HasRoot(singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
169+
HasRoot(singleLayerHamtMultiBlockFilesFixture.MustGetCid("1.txt")).
170+
MightHaveNoRoots().
165171
HasBlocks(flattenStrings(t,
166172
singleLayerHamtMultiBlockFilesFixture.MustGetCid(),
167173
singleLayerHamtMultiBlockFilesFixture.MustGetCIDsInHAMTTraversal(nil, "1.txt"),
@@ -199,6 +205,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
199205
Body(
200206
IsCar().
201207
HasRoot(subdirTwoSingleBlockFilesFixture.MustGetCid()).
208+
MightHaveNoRoots().
202209
HasBlocks(
203210
subdirTwoSingleBlockFilesFixture.MustGetCid(),
204211
).
@@ -221,6 +228,7 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
221228
Body(
222229
IsCar().
223230
HasRoot(singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
231+
MightHaveNoRoots().
224232
HasBlocks(
225233
flattenStrings(t,
226234
singleLayerHamtMultiBlockFilesFixture.MustGetCid(),
@@ -244,7 +252,8 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
244252
Status(200).
245253
Body(
246254
IsCar().
247-
HasRoot(subdirWithMixedBlockFiles.MustGetCid()).
255+
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "ascii.txt")).
256+
MightHaveNoRoots().
248257
HasBlocks(
249258
flattenStrings(t,
250259
subdirWithMixedBlockFiles.MustGetCid(),
@@ -270,7 +279,8 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
270279
Status(200).
271280
Body(
272281
IsCar().
273-
HasRoot(subdirWithMixedBlockFiles.MustGetCid()).
282+
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
283+
MightHaveNoRoots().
274284
HasBlocks(
275285
flattenStrings(t,
276286
subdirWithMixedBlockFiles.MustGetCid(),
@@ -297,7 +307,8 @@ func TestGatewayCarDagScopeEntity(t *testing.T) {
297307
Status(200).
298308
Body(
299309
IsCar().
300-
HasRoot(dirWithDagCborWithLinksFixture.MustGetCid()).
310+
HasRoot(dirWithDagCborWithLinksFixture.MustGetCid("document")).
311+
MightHaveNoRoots().
301312
HasBlocks(flattenStrings(t,
302313
dirWithDagCborWithLinksFixture.MustGetCid(),
303314
dirWithDagCborWithLinksFixture.MustGetCid("document"),
@@ -330,7 +341,8 @@ func TestGatewayCarDagScopeAll(t *testing.T) {
330341
Status(200).
331342
Body(
332343
IsCar().
333-
HasRoot(subdirWithMixedBlockFiles.MustGetCid()).
344+
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir")).
345+
MightHaveNoRoots().
334346
HasBlocks(
335347
flattenStrings(
336348
t,
@@ -357,7 +369,8 @@ func TestGatewayCarDagScopeAll(t *testing.T) {
357369
Status(200).
358370
Body(
359371
IsCar().
360-
HasRoot(subdirWithMixedBlockFiles.MustGetCid()).
372+
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
373+
MightHaveNoRoots().
361374
HasBlocks(
362375
flattenStrings(t,
363376
subdirWithMixedBlockFiles.MustGetCid(),
@@ -396,7 +409,8 @@ func TestGatewayCarEntityBytes(t *testing.T) {
396409
Status(200).
397410
Body(
398411
IsCar().
399-
HasRoot(subdirWithMixedBlockFiles.MustGetCid()).
412+
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
413+
MightHaveNoRoots().
400414
HasBlocks(flattenStrings(t,
401415
subdirWithMixedBlockFiles.MustGetCid(),
402416
subdirWithMixedBlockFiles.MustGetCid("subdir"),
@@ -424,6 +438,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
424438
Body(
425439
IsCar().
426440
HasRoot(singleLayerHamtMultiBlockFilesFixture.MustGetCid()).
441+
MightHaveNoRoots().
427442
HasBlocks(
428443
flattenStrings(t,
429444
singleLayerHamtMultiBlockFilesFixture.MustGetCid(),
@@ -448,6 +463,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
448463
Body(
449464
IsCar().
450465
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
466+
MightHaveNoRoots().
451467
HasBlocks(
452468
flattenStrings(t,
453469
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),
@@ -472,6 +488,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
472488
Body(
473489
IsCar().
474490
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
491+
MightHaveNoRoots().
475492
HasBlocks(
476493
flattenStrings(t,
477494
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),
@@ -496,6 +513,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
496513
Body(
497514
IsCar().
498515
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
516+
MightHaveNoRoots().
499517
HasBlocks(
500518
flattenStrings(t,
501519
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),
@@ -520,6 +538,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
520538
Body(
521539
IsCar().
522540
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
541+
MightHaveNoRoots().
523542
HasBlocks(
524543
flattenStrings(t,
525544
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),
@@ -544,6 +563,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
544563
Body(
545564
IsCar().
546565
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
566+
MightHaveNoRoots().
547567
HasBlocks(
548568
flattenStrings(t,
549569
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),
@@ -568,6 +588,7 @@ func TestGatewayCarEntityBytes(t *testing.T) {
568588
Body(
569589
IsCar().
570590
HasRoot(subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt")).
591+
MightHaveNoRoots().
571592
HasBlocks(
572593
flattenStrings(t,
573594
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),

tooling/car/unixfs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ func (d *UnixfsDag) getNode(names ...string) (format.Node, error) {
9292
// Maybe it's an IPLD Link!
9393
lnk, _, err := node.ResolveLink(names[i:])
9494
if err != nil {
95-
return nil, fmt.Errorf("node is neither a unixfs directory, or includes an ipld link: %w", err)
95+
return nil, fmt.Errorf("node is neither a unixfs directory, nor includes an ipld link: %w", err)
9696
}
9797
n, err := lnk.GetNode(context.Background(), d.dsvc)
9898
if err != nil {
9999
return nil, fmt.Errorf("found link node could not be fetched: %w", err)
100100
}
101-
d.node = n
102-
return d.node, nil
101+
return n, nil
103102
} else if err != nil {
104103
return nil, err
105104
}

tooling/check/car.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
)
88

99
type CheckIsCarFile struct {
10-
blockCIDs []cid.Cid
11-
rootCIDs []cid.Cid
12-
isExact bool
13-
isOrdered bool
10+
blockCIDs []cid.Cid
11+
rootCIDs []cid.Cid
12+
mightHaveNoRoots bool
13+
isExact bool
14+
isOrdered bool
1415
}
1516

1617
var _ Check[[]byte] = (*CheckIsCarFile)(nil)
@@ -56,6 +57,11 @@ func (c CheckIsCarFile) HasRoots(cidStrs ...string) *CheckIsCarFile {
5657
return &c
5758
}
5859

60+
func (c CheckIsCarFile) MightHaveNoRoots() *CheckIsCarFile {
61+
c.mightHaveNoRoots = true
62+
return &c
63+
}
64+
5965
func (c CheckIsCarFile) Exactly() *CheckIsCarFile {
6066
c.isExact = true
6167
return &c
@@ -104,10 +110,11 @@ func (c *CheckIsCarFile) Check(carContent []byte) CheckOutput {
104110
}
105111
}
106112

107-
output = cmp(gotRoots, c.rootCIDs)
108-
109-
if !output.Success {
110-
return output
113+
if !(c.mightHaveNoRoots && len(gotRoots) == 0) {
114+
output = cmp(gotRoots, c.rootCIDs)
115+
if !output.Success {
116+
return output
117+
}
111118
}
112119
}
113120

0 commit comments

Comments
 (0)