From 4bd19e7b76b03632645dd93a209746408f64e355 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Fri, 8 Dec 2023 15:17:40 -0800 Subject: [PATCH 1/2] 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 --- tests/trustless_gateway_car_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/trustless_gateway_car_test.go b/tests/trustless_gateway_car_test.go index 5f0aa7e3a..6fd56ea58 100644 --- a/tests/trustless_gateway_car_test.go +++ b/tests/trustless_gateway_car_test.go @@ -634,7 +634,7 @@ func TestTrustlessCarEntityBytes(t *testing.T) { HasBlocks( flattenStrings(t, subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"), - subdirWithMixedBlockFiles.MustGetDescendantsCids("subdir", "multiblock.txt")[:5])..., + subdirWithMixedBlockFiles.MustGetDescendantsCids("subdir", "multiblock.txt")[:len(subdirWithMixedBlockFiles.MustGetDescendantsCids("subdir", "multiblock.txt"))-1])..., ). Exactly(). InThatOrder(), From 7ed58d142a6a8295777f5a6a5bd4f8e68c179355 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 24 Jan 2024 01:25:14 +0100 Subject: [PATCH 2/2] refactor: explain what we test and why --- tests/trustless_gateway_car_test.go | 49 ++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/tests/trustless_gateway_car_test.go b/tests/trustless_gateway_car_test.go index 6fd56ea58..9f1831476 100644 --- a/tests/trustless_gateway_car_test.go +++ b/tests/trustless_gateway_car_test.go @@ -617,15 +617,45 @@ func TestTrustlessCarEntityBytes(t *testing.T) { ), }, { - Name: "GET CAR with entity-bytes requesting a range from the end of a file", + Name: "GET CAR with entity-bytes requesting a negative range bigger than the length of a file", Hint: ` - The response MUST contain only the minimal set of blocks necessary for fulfilling the range request + When range starts on negative index that makes it bigger than the file + the request is truncated and starts at the beginning of a file. + `, + Request: Request(). + Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")). + Query("format", "car"). + Query("dag-scope", "entity"). + Query("entity-bytes", "-9999:*"), // multiblock.txt size is 1026 (4*256+2) + Response: Expect(). + Status(200). + Body( + IsCar(). + IgnoreRoots(). + HasBlocks( // expect entire file + flattenStrings(t, + subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"), // dag-pb root of the file DAG + "bafkreie5noke3mb7hqxukzcy73nl23k6lxszxi5w3dtmuwz62wnvkpsscm", // 256 chunk + "bafkreih4ephajybraj6wnxsbwjwa77fukurtpl7oj7t7pfq545duhot7cq", // 256 + "bafkreigu7buvm3cfunb35766dn7tmqyh2um62zcio63en2btvxuybgcpue", // 256 + "bafkreicll3huefkc3qnrzeony7zcfo7cr3nbx64hnxrqzsixpceg332fhe", // 256 + "bafkreifst3pqztuvj57lycamoi7z34b4emf7gawxs74nwrc2c7jncmpaqm", // 2 + )...). + Exactly(). + InThatOrder(), + ), + }, + { + Name: "GET CAR with entity-bytes requesting a range from the end of a file that is bigger than a file itself", + Hint: ` + The response MUST contain only the minimal set of blocks necessary for fulfilling the range request, + everything before file start is ignored and the explicit end of the range is respected. `, Request: Request(). Path("/ipfs/{{cid}}", subdirWithMixedBlockFiles.MustGetCidWithCodec(0x70, "subdir", "multiblock.txt")). Query("format", "car"). Query("dag-scope", "entity"). - Query("entity-bytes", "-999999:-3"), + Query("entity-bytes", "-9999:-3"), // multiblock.txt size is 1026 (4*256+2) Response: Expect(). Status(200). Body( @@ -633,15 +663,19 @@ func TestTrustlessCarEntityBytes(t *testing.T) { IgnoreRoots(). HasBlocks( flattenStrings(t, - subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"), - subdirWithMixedBlockFiles.MustGetDescendantsCids("subdir", "multiblock.txt")[:len(subdirWithMixedBlockFiles.MustGetDescendantsCids("subdir", "multiblock.txt"))-1])..., - ). + subdirWithMixedBlockFiles.MustGetCid("subdir", "multiblock.txt"), // dag-pb root of the file DAG + "bafkreie5noke3mb7hqxukzcy73nl23k6lxszxi5w3dtmuwz62wnvkpsscm", // 256 chunk + "bafkreih4ephajybraj6wnxsbwjwa77fukurtpl7oj7t7pfq545duhot7cq", // 256 + "bafkreigu7buvm3cfunb35766dn7tmqyh2um62zcio63en2btvxuybgcpue", // 256 + "bafkreicll3huefkc3qnrzeony7zcfo7cr3nbx64hnxrqzsixpceg332fhe", // 256 + // skip "bafkreifst3pqztuvj57lycamoi7z34b4emf7gawxs74nwrc2c7jncmpaqm", // 2 + )...). Exactly(). InThatOrder(), ), }, { - Name: "GET CAR with entity-bytes requesting the first byte of a file", + Name: "GET CAR with entity-bytes requesting only the blocks for the first byte of a file", Hint: ` The response MUST contain only the first block of the file. `, @@ -835,6 +869,7 @@ func TestTrustlessCarOrderAndDuplicates(t *testing.T) { RunWithSpecs(t, tests, specs.TrustlessGatewayCAROptional) } +// TODO: this feels like it could be an internal detail of HasBlocks func flattenStrings(t *testing.T, values ...interface{}) []string { var res []string for _, v := range values {