Skip to content

Commit

Permalink
Bug 1652418 [wpt PR 24575] - [nativeio] NativeIO: Add setLength, a=te…
Browse files Browse the repository at this point in the history
…stonly

Automatic update from web-platform-tests
[nativeio] NativeIO: Add setLength

The MacOS sandbox does not allow calling base::File::SetLength() from
the renderer directly, see crbug.com/1084565. The call to setLength is
therefore routed through the browser process, which likely decreases
performance and increases complexity. Another option would be to create
a SeatbeltExtensionToken that allows to pierce the sandbox on MacOS for
a single file. As this would likely lead to more complex code, we
decided that going through the browser process is the better option for
now.

Bug: 914488
Change-Id: Idc58d804e19751a2e37cfeb5c645f778edf4639c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2257855
Commit-Queue: Richard Stotz <rstz@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799005}

--

wpt-commits: c9e08236b532473b460dca5daa717dcb8824d7dc
wpt-pr: 24575
  • Loading branch information
rstz authored and moz-wptsync-bot committed Aug 25, 2020
1 parent 9874603 commit c7490ac
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,18 @@ promise_test(async testCase => {
await promise_rejects_dom(testCase, 'InvalidStateError', file.flush());
assert_equals(await closePromise, undefined);
}, 'NativeIOFile.flush fails immediately after calling NativeIOFile.close');

promise_test(async testCase => {
const file = await createFile(testCase, 'file_name');
assert_equals(await file.close(), undefined);

await promise_rejects_dom(testCase, 'InvalidStateError', file.setLength(5));
}, 'NativeIOFile.setLength fails after NativeIOFile.close settles');

promise_test(async testCase => {
const file = await createFile(testCase, 'file_name');
const closePromise = file.close();

await promise_rejects_dom(testCase, 'InvalidStateError', file.setLength(5));
assert_equals(await closePromise, undefined);
}, 'NativeIOFile.setLength fails immediately after calling NativeIOFile.close');
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ test(testCase => {

assert_throws_dom('InvalidStateError', () => file.flush());
}, 'NativeIOFileSync.flush fails after NativeIOFileSync.close');

test(testCase => {
const file = createFileSync(testCase, 'file_name');
assert_equals(undefined, file.close());

assert_throws_dom('InvalidStateError', () => file.setLength(4));
}, 'NativeIOFileSync.setLength fails after NativeIOFileSync.close');
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// META: title=NativeIO API: Concurrent io while setLength is resolving.
// META: global=window,worker
// META: script=operation_helpers.js
// META: script=../resources/support.js

'use strict';

// See documentation in operation_helpers.js

for (let op of kOperations) {
promise_test(async testCase => {
const file = await createFile(testCase, 'setlength_file');

const res = op.prepare();

const setLengthPromise = file.setLength(5);
op.assertRejection(testCase, file, res);

await setLengthPromise;

const readSharedArrayBuffer = new SharedArrayBuffer(5);
const readBytes = new Uint8Array(readSharedArrayBuffer);
assert_equals(await file.read(readBytes, 0), 5,
`NativeIOFile.read() should not fail after a rejected ` +
`${op.name}() during setLength().`);
assert_array_equals(readBytes, [64, 65, 66, 67, 0],
`Rejecting ${op.name}() during setLength()` +
` should not change the file.`);
op.assertUnchanged(res);
}, `${op.name}() rejects while setLength() is resolving.`);
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const kOperations = [];
},
};
kOperations.push(kOpRead);

const kOpWrite = {
name: 'write',
prepare: () => {
Expand All @@ -54,6 +55,7 @@ const kOperations = [];
assertUnchanged: () => {},
};
kOperations.push(kOpWrite);

const kOpGetLength = {
name: 'getLength',
prepare: () => {},
Expand All @@ -75,4 +77,15 @@ const kOperations = [];
assertUnchanged: () => {},
};
kOperations.push(kOpFlush);

const kOpSetLength = {
name: 'setLength',
prepare: () => {},
assertRejection: async (testCase, file, readBytes) => {
return promise_rejects_dom(testCase, 'InvalidStateError',
file.setLength(2));
},
assertUnchanged: () => {},
};
kOperations.push(kOpSetLength);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// META: title=NativeIO API: Assigned length is observed back.
// META: global=window,worker
// META: script=resources/support.js

'use strict';

promise_test(async testCase => {
const file = await nativeIO.open('test_file');
testCase.add_cleanup(async () => {
await file.close();
await nativeIO.delete('test_file');
});

const writeSharedArrayBuffer = new SharedArrayBuffer(4);
const writtenBytes = new Uint8Array(writeSharedArrayBuffer);
writtenBytes.set([97, 98, 99, 100]);
await file.write(writtenBytes, 0);

await file.setLength(3);
const readBytes = await readIoFile(file);

const remainingBytes = Uint8Array.from([97, 98, 99]);

assert_array_equals(
readBytes, remainingBytes,
'NativeIOFile.setLength() should remove bytes from the end of ' +
'a file when decreasing its length');
}, 'NativeIOFile.setLength shrinks a file, NativeIOFile.getLength reports ' +
'new length');

promise_test(async testCase => {
const file = await nativeIO.open('test_file');
testCase.add_cleanup(async () => {
await file.close();
await nativeIO.delete('test_file');
});

const writeSharedArrayBuffer = new SharedArrayBuffer(4);
const writtenBytes = new Uint8Array(writeSharedArrayBuffer);
writtenBytes.set([97, 98, 99, 100]);
await file.write(writtenBytes, 0);

await file.setLength(5);
const readBytes = await readIoFile(file);

const expectedBytes = Uint8Array.from([97, 98, 99, 100, 0]);

assert_array_equals(
readBytes, expectedBytes,
'NativeIOFile.setLength() should append zeros when increasing' +
' the file size');
}, 'NativeIOFile.setLength appends zeros to a file, NativeIOFile.getLength ' +
'reports new length');
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// META: title=Synchronous NativeIO API: Assigned length is observed back.
// META: global=dedicatedworker
// META: script=resources/support.js

'use strict';

test(testCase => {
const file = nativeIO.openSync('test_file');
testCase.add_cleanup(() => {
file.close();
nativeIO.deleteSync('test_file');
});

const writtenBytes = Uint8Array.from([97, 98, 99, 100]);
file.write(writtenBytes, 0);

file.setLength(3);
const readBytes = readIoFileSync(file);

const remainingBytes = Uint8Array.from([97, 98, 99]);
assert_array_equals(
readBytes, remainingBytes,
'NativeIOFileSync.setLength() should remove bytes from the end of ' +
'a file when decreasing its length.');
}, 'NativeIOFileSync.setLength shrinks a file and' +
' NativeIOFileSync.getLength reports its new length.');

test(testCase => {
const file = nativeIO.openSync('test_file');
testCase.add_cleanup(() => {
file.close();
nativeIO.deleteSync('test_file');
});

const writtenBytes = Uint8Array.from([97, 98, 99, 100]);
file.write(writtenBytes, 0);

file.setLength(5);
const readBytes = readIoFileSync(file);

const expectedBytes = Uint8Array.from([97, 98, 99, 100, 0]);

assert_array_equals(
readBytes, expectedBytes,
'NativeIOFileSync.setLength() should append zeros when increasing' +
' the file size.');
}, 'NativeIOFileSync.setLength appends zeros to a file and ' +
'NativeIOFileSync.getLength reports its new length.');
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// META: title=NativeIO API: Out-of-bounds errors for setLength.
// META: global=window,worker
// META: script=resources/support.js

'use strict';

promise_test(async testCase => {
const file = await createFile(testCase, "file_length_zero");
await file.setLength(0);
const lengthDecreased = await file.getLength();
assert_equals(lengthDecreased, 0,
"NativeIOFile.setLength() should set the file length to 0.");
}, 'NativeIOFile.setLength does not throw an error when descreasing the ' +
'file length to 0.');

promise_test(async testCase => {
const file = await createFile(testCase, "file_length_negative");
await promise_rejects_js(testCase, TypeError,
file.setLength(-1));
}, 'NativeIOFile.setLength() throws when setting negative lengths.');
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// META: title=Synchronous NativeIO API: Out-of-bounds errors for setLength.
// META: global=dedicatedworker
// META: script=resources/support.js

'use strict';

test(testCase => {
const file = createFileSync(testCase, "file_length_zero");
file.setLength(0);
const lengthDecreased = file.getLength();
assert_equals(lengthDecreased, 0,
"NativeIOFileSync.setLength() should set the file length to 0.");
}, 'NativeIOFileSync.setLength() does not throw an error when descreasing ' +
'the file length to 0.');

test(testCase => {
const file = createFileSync(testCase, "file_length_negative");

// Without this assertion, the test passes even if setLength is not defined.
assert_implements(file.setLength,
"NativeIOFileSync.setLength is not implemented.");

assert_throws_js(TypeError, () => file.setLength(-1));
}, 'NativeIOFileSync.setLength() throws when setting negative lengths.');

0 comments on commit c7490ac

Please sign in to comment.