Skip to content

Commit dc25ca6

Browse files
hannahhowardlidel
andauthored
fix(tests): properly test entity-bytes with negative indexing (#190)
* fix(tests): properly test negative indexing for the entity-bytes negative indexing test, fix the test so that it actually tests that the last block in the file is not read --------- Co-authored-by: Marcin Rataj <[email protected]>
1 parent 94f2a92 commit dc25ca6

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

tests/trustless_gateway_car_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,31 +617,65 @@ func TestTrustlessCarEntityBytes(t *testing.T) {
617617
),
618618
},
619619
{
620-
Name: "GET CAR with entity-bytes requesting a range from the end of a file",
620+
Name: "GET CAR with entity-bytes requesting a negative range bigger than the length of a file",
621621
Hint: `
622-
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request
622+
When range starts on negative index that makes it bigger than the file
623+
the request is truncated and starts at the beginning of a file.
624+
`,
625+
Request: Request().
626+
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
627+
Query("format", "car").
628+
Query("dag-scope", "entity").
629+
Query("entity-bytes", "-9999:*"), // multiblock.txt size is 1026 (4*256+2)
630+
Response: Expect().
631+
Status(200).
632+
Body(
633+
IsCar().
634+
IgnoreRoots().
635+
HasBlocks( // expect entire file
636+
flattenStrings(t,
637+
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"), // dag-pb root of the file DAG
638+
"bafkreie5noke3mb7hqxukzcy73nl23k6lxszxi5w3dtmuwz62wnvkpsscm", // 256 chunk
639+
"bafkreih4ephajybraj6wnxsbwjwa77fukurtpl7oj7t7pfq545duhot7cq", // 256
640+
"bafkreigu7buvm3cfunb35766dn7tmqyh2um62zcio63en2btvxuybgcpue", // 256
641+
"bafkreicll3huefkc3qnrzeony7zcfo7cr3nbx64hnxrqzsixpceg332fhe", // 256
642+
"bafkreifst3pqztuvj57lycamoi7z34b4emf7gawxs74nwrc2c7jncmpaqm", // 2
643+
)...).
644+
Exactly().
645+
InThatOrder(),
646+
),
647+
},
648+
{
649+
Name: "GET CAR with entity-bytes requesting a range from the end of a file that is bigger than a file itself",
650+
Hint: `
651+
The response MUST contain only the minimal set of blocks necessary for fulfilling the range request,
652+
everything before file start is ignored and the explicit end of the range is respected.
623653
`,
624654
Request: Request().
625655
Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")).
626656
Query("format", "car").
627657
Query("dag-scope", "entity").
628-
Query("entity-bytes", "-999999:-3"),
658+
Query("entity-bytes", "-9999:-3"), // multiblock.txt size is 1026 (4*256+2)
629659
Response: Expect().
630660
Status(200).
631661
Body(
632662
IsCar().
633663
IgnoreRoots().
634664
HasBlocks(
635665
flattenStrings(t,
636-
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"),
637-
subdirWithMixedBlockFiles.MustGetDescendantsCids("subdir", "multiblock.txt")[:5])...,
638-
).
666+
subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"), // dag-pb root of the file DAG
667+
"bafkreie5noke3mb7hqxukzcy73nl23k6lxszxi5w3dtmuwz62wnvkpsscm", // 256 chunk
668+
"bafkreih4ephajybraj6wnxsbwjwa77fukurtpl7oj7t7pfq545duhot7cq", // 256
669+
"bafkreigu7buvm3cfunb35766dn7tmqyh2um62zcio63en2btvxuybgcpue", // 256
670+
"bafkreicll3huefkc3qnrzeony7zcfo7cr3nbx64hnxrqzsixpceg332fhe", // 256
671+
// skip "bafkreifst3pqztuvj57lycamoi7z34b4emf7gawxs74nwrc2c7jncmpaqm", // 2
672+
)...).
639673
Exactly().
640674
InThatOrder(),
641675
),
642676
},
643677
{
644-
Name: "GET CAR with entity-bytes requesting the first byte of a file",
678+
Name: "GET CAR with entity-bytes requesting only the blocks for the first byte of a file",
645679
Hint: `
646680
The response MUST contain only the first block of the file.
647681
`,
@@ -835,6 +869,7 @@ func TestTrustlessCarOrderAndDuplicates(t *testing.T) {
835869
RunWithSpecs(t, tests, specs.TrustlessGatewayCAROptional)
836870
}
837871

872+
// TODO: this feels like it could be an internal detail of HasBlocks
838873
func flattenStrings(t *testing.T, values ...interface{}) []string {
839874
var res []string
840875
for _, v := range values {

0 commit comments

Comments
 (0)