Skip to content

Commit

Permalink
Add tests for zstd content-encoding (web-platform-tests#44393)
Browse files Browse the repository at this point in the history
* add zstd tests and separate into folders

* add bad input tests and make window size test tentative

* add https to bad input test
  • Loading branch information
nidhijaju authored and mbrodesser-Igalia committed Feb 19, 2024
1 parent b09034c commit 5e7517e
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions fetch/content-encoding/zstd/bad-zstd-body.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// META: global=window,worker

promise_test((test) => {
return fetch("resources/bad-zstd-body.py").then(res => {
assert_equals(res.status, 200);
});
}, "Fetching a resource with bad zstd content should still resolve");

[
"arrayBuffer",
"blob",
"formData",
"json",
"text"
].forEach(method => {
promise_test(t => {
return fetch("resources/bad-zstd-body.py").then(res => {
assert_equals(res.status, 200);
return promise_rejects_js(t, TypeError, res[method]());
});
}, "Consuming the body of a resource with bad zstd content with " + method + "() should reject");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// META: global=window,worker
// See https://github.com/facebook/zstd/issues/2713 for discussion about
// standardizing window size limits.

promise_test(async t => {
const response = await fetch('resources/big.window.zst');
assert_true(response.ok);
await promise_rejects_js(t, TypeError, response.text());
}, 'Consuming the body of a resource with too large of a zstd window size should reject');
55 changes: 55 additions & 0 deletions fetch/content-encoding/zstd/big-zstd-body.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// META: global=window,worker

const EXPECTED_SIZE = 27000000;
const EXPECTED_SHA256 = [
74, 100, 37, 243, 147, 61, 116, 60, 241, 221, 126,
18, 24, 71, 204, 28, 50, 62, 201, 130, 152, 225,
217, 183, 10, 201, 143, 214, 102, 155, 212, 248,
];

promise_test(async () => {
const response = await fetch('resources/big.text.zst');
assert_true(response.ok);
const arrayBuffer = await response.arrayBuffer();
assert_equals(arrayBuffer.byteLength, EXPECTED_SIZE,
'uncompressed size should match');
const sha256 = await crypto.subtle.digest('SHA-256', arrayBuffer);
assert_array_equals(new Uint8Array(sha256), EXPECTED_SHA256,
'digest should match');
}, 'large zstd data should be decompressed successfully');

promise_test(async () => {
const response = await fetch('resources/big.text.zst');
assert_true(response.ok);
const reader = response.body.getReader({mode: 'byob'});
let offset = 0;
// Pre-allocate space for the output. The response body will be read
// chunk-by-chunk into this array.
let ab = new ArrayBuffer(EXPECTED_SIZE);
while (offset < EXPECTED_SIZE) {
// To stress the data pipe, we want to use a different size read each
// time. Unfortunately, JavaScript doesn't have a seeded random number
// generator, so this creates the possibility of making this test flaky if
// it doesn't work for some edge cases.
let size = Math.floor(Math.random() * 65535 + 1);
if (size + offset > EXPECTED_SIZE) {
size = EXPECTED_SIZE - offset;
}
const u8 = new Uint8Array(ab, offset, size);
const { value, done } = await reader.read(u8);
ab = value.buffer;
// Check that we got our original array back.
assert_equals(ab.byteLength, EXPECTED_SIZE,
'backing array should be the same size');
assert_equals(offset, value.byteOffset, 'offset should match');
assert_less_than_equal(value.byteLength, size,
'we should not have got more than we asked for');
offset = value.byteOffset + value.byteLength;
if (done) break;
}
assert_equals(offset, EXPECTED_SIZE,
'we should have read the whole thing');
const sha256 = await crypto.subtle.digest('SHA-256', new Uint8Array(ab));
assert_array_equals(new Uint8Array(sha256), EXPECTED_SHA256,
'digest should match');
}, 'large zstd data should be decompressed successfully with byte stream');
3 changes: 3 additions & 0 deletions fetch/content-encoding/zstd/resources/bad-zstd-body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main(request, response):
headers = [(b"Content-Encoding", b"zstd")]
return headers, b"not actually zstd"
Binary file not shown.
3 changes: 3 additions & 0 deletions fetch/content-encoding/zstd/resources/big.text.zst.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Content-type: text/plain
Content-Encoding: zstd
Cache-Control: no-store
Binary file added fetch/content-encoding/zstd/resources/big.window.zst
Binary file not shown.
2 changes: 2 additions & 0 deletions fetch/content-encoding/zstd/resources/big.window.zst.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Content-type: text/plain
Content-Encoding: zstd
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Content-type: application/octet-stream
Content-Encoding: zstd
Binary file added fetch/content-encoding/zstd/resources/foo.text.zst
Binary file not shown.
2 changes: 2 additions & 0 deletions fetch/content-encoding/zstd/resources/foo.text.zst.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Content-type: text/plain
Content-Encoding: zstd
15 changes: 15 additions & 0 deletions fetch/content-encoding/zstd/zstd-body.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// META: global=window,worker

const expectedDecompressedSize = 10500;
[
"text",
"octetstream"
].forEach(contentType => {
promise_test(async t => {
let response = await fetch(`resources/foo.${contentType}.zst`);
assert_true(response.ok);
let arrayBuffer = await response.arrayBuffer()
let u8 = new Uint8Array(arrayBuffer);
assert_equals(u8.length, expectedDecompressedSize);
}, `fetched zstd data with content type ${contentType} should be decompressed.`);
});

0 comments on commit 5e7517e

Please sign in to comment.