summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js')
-rw-r--r--testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js245
1 files changed, 245 insertions, 0 deletions
diff --git a/testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js b/testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js
new file mode 100644
index 0000000000..cdefc78b6c
--- /dev/null
+++ b/testing/web-platform/tests/fs/FileSystemSyncAccessHandle-read-write.https.tentative.worker.js
@@ -0,0 +1,245 @@
+importScripts("/resources/testharness.js");
+importScripts('resources/sync-access-handle-test.js');
+
+'use strict';
+
+sync_access_handle_test((t, handle) => {
+ const readBuffer = new Uint8Array(24);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(0, readBytes, 'Check that no bytes were read');
+}, 'Test reading an empty file through a sync access handle.');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const decoder = new TextDecoder();
+
+ const text = 'Hello Storage Foundation';
+ const writeBuffer = new TextEncoder().encode(text);
+ const writtenBytes = handle.write(writeBuffer, {at: 0});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+ let readBuffer = new Uint8Array(writtenBytes);
+ let readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(writtenBytes, readBytes, 'Check that all bytes were read');
+ assert_equals(
+ text, decoder.decode(readBuffer),
+ 'Check that the written bytes and the read bytes match');
+
+ // Test a read of less bytes than available.
+ const expected = 'Storage';
+ readBuffer = new Uint8Array(expected.length);
+ readBytes = handle.read(readBuffer, {at: text.indexOf(expected)});
+ assert_equals(readBuffer.length, readBytes, 'Check that all bytes were read');
+ const actual = decoder.decode(readBuffer);
+ assert_equals(
+ expected, actual,
+ 'Partial read returned unexpected contents');
+}, 'Test writing and reading through a sync access handle.');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const encoder = new TextEncoder();
+ const decoder = new TextDecoder();
+
+ for (text of ['Hello', 'Longer Text']) {
+ const writeBuffer = encoder.encode(text);
+ const writtenBytes = handle.write(writeBuffer, {at: 0});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+ const readBuffer = new Uint8Array(writtenBytes);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(writtenBytes, readBytes, 'Check that all bytes were read');
+ assert_equals(
+ text, decoder.decode(readBuffer),
+ 'Check that the written bytes and the read bytes match');
+ }
+}, 'Test second write that is bigger than the first write');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const encoder = new TextEncoder();
+ const decoder = new TextDecoder();
+
+ for (tuple
+ of [{input: 'Hello World', expected: 'Hello World'},
+ {input: 'foobar', expected: 'foobarWorld'}]) {
+ const text = tuple.input;
+ const expected = tuple.expected;
+ const writeBuffer = encoder.encode(text);
+ const writtenBytes = handle.write(writeBuffer, {at: 0});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+ const readBuffer = new Uint8Array(expected.length);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(expected.length, readBytes, 'Check that all bytes were read');
+ assert_equals(
+ expected, decoder.decode(readBuffer),
+ 'Check that the written bytes and the read bytes match');
+ }
+}, 'Test second write that is smaller than the first write');
+
+sync_access_handle_test((t, handle) => {
+ const expected = 17;
+ const writeBuffer = new Uint8Array(1);
+ writeBuffer[0] = expected;
+ const offset = 5;
+ const writtenBytes = handle.write(writeBuffer, {at: offset});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+ const fileLength = writeBuffer.byteLength + offset;
+ const readBuffer = new Uint8Array(fileLength);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(fileLength, readBytes, 'Check that all bytes were read');
+ for (let i = 0; i < offset; ++i) {
+ assert_equals(
+ readBuffer[i], 0,
+ `Gaps in the file should be filled with 0, but got ${readBuffer[i]}.`);
+ }
+
+ assert_equals(
+ readBuffer[offset], expected,
+ 'Gaps in the file should be filled with 0.');
+}, 'Test initial write with an offset');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const encoder = new TextEncoder();
+ const decoder = new TextDecoder();
+
+ for (tuple
+ of [{input: 'Hello World', expected: 'Hello World', offset: 0},
+ {input: 'foobar', expected: 'Hello foobar', offset: 6}]) {
+ const text = tuple.input;
+ const expected = tuple.expected;
+ const offset = tuple.offset;
+ const writeBuffer = encoder.encode(text);
+ const writtenBytes = handle.write(writeBuffer, {at: offset});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+ const readBuffer = new Uint8Array(expected.length);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(expected.length, readBytes, 'Check that all bytes were read');
+ const actual = decoder.decode(readBuffer);
+ assert_equals(
+ expected, actual,
+ 'Check content read from the handle');
+ }
+}, 'Test overwriting the file at an offset');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const decoder = new TextDecoder();
+
+ const text = 'Hello Storage Foundation';
+ const writeBuffer = new TextEncoder().encode(text);
+ const writtenBytes = handle.write(writeBuffer, {at: 0});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+ const bufferLength = text.length;
+ for (tuple
+ of [{offset: 0, expected: text},
+ {offset: 6, expected: text.substring(6)}]) {
+ const offset = tuple.offset;
+ const expected = tuple.expected;
+
+ const readBuffer = new Uint8Array(bufferLength);
+ const readBytes = handle.read(readBuffer, {at: offset});
+ assert_equals(expected.length, readBytes, 'Check that all bytes were read');
+ const actual = decoder.decode(readBuffer);
+ assert_true(
+ actual.startsWith(expected),
+ `Expected to read ${expected} but the actual value was ${actual}.`);
+ }
+
+ const readBuffer = new Uint8Array(bufferLength);
+ // Offset is greater than the file length.
+ const readBytes = handle.read(readBuffer, {at: bufferLength + 1});
+ assert_equals(0, readBytes, 'Check that no bytes were read');
+ for (let i = 0; i < readBuffer.byteLength; ++i) {
+ assert_equals(0, readBuffer[i], 'Check that the read buffer is unchanged.');
+ }
+}, 'Test read at an offset');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const expected = 'Hello Storage Foundation';
+ const writeBuffer = new TextEncoder().encode(expected);
+ const writtenBytes = handle.write(writeBuffer, {at: 0});
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+
+ const bufferLength = expected.length;
+ const readBuffer = new Uint8Array(expected.length);
+ // No options parameter provided, should read at offset 0.
+ const readBytes = handle.read(readBuffer);
+ assert_equals(expected.length, readBytes, 'Check that all bytes were read');
+ const actual = new TextDecoder().decode(readBuffer);
+ assert_equals(
+ expected, actual,
+ `Expected to read ${expected} but the actual value was ${actual}.`);
+}, 'Test read with default options');
+
+sync_access_handle_test((t, handle) => {
+ if (!('TextEncoder' in self)) {
+ return;
+ }
+
+ const expected = 'Hello Storage Foundation';
+ const writeBuffer = new TextEncoder().encode(expected);
+ // No options parameter provided, should write at offset 0.
+ const writtenBytes = handle.write(writeBuffer);
+ assert_equals(
+ writeBuffer.byteLength, writtenBytes,
+ 'Check that all bytes were written.');
+
+ const bufferLength = expected.length;
+ const readBuffer = new Uint8Array(expected.length);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(expected.length, readBytes, 'Check that all bytes were read');
+ const actual = new TextDecoder().decode(readBuffer);
+ assert_equals(
+ expected, actual,
+ `Expected to read ${expected} but the actual value was ${actual}.`);
+}, 'Test write with default options');
+
+sync_access_handle_test((t, handle) => {
+ const readBuffer = new Uint8Array(24);
+ assert_throws_js(TypeError, () => handle.read(readBuffer, {at: -1}));
+}, 'Test reading at a negative offset fails.');
+
+sync_access_handle_test((t, handle) => {
+ const text = 'foobar';
+ const writeBuffer = new TextEncoder().encode(text);
+ assert_throws_js(TypeError, () => handle.write(writeBuffer, {at: -1}));
+
+ const readBuffer = new Uint8Array(24);
+ const readBytes = handle.read(readBuffer, {at: 0});
+ assert_equals(0, readBytes, 'Check that no bytes were written');
+}, 'Test writing at a negative offset fails.');
+
+done();