-
-
Notifications
You must be signed in to change notification settings - Fork 38.4k
test: cover immutable ArrayBuffer known issues #66350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
panva
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
panva:immutable-arraybuffer-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| prefix js-native-api | ||
|
|
||
| [true] | ||
| # napi_detach_arraybuffer() aborts when given an immutable ArrayBuffer. | ||
| # Keep this test here so native CI builds its typed-array addon. The | ||
| # known_issues suite also runs in JavaScript-only CI without built addons. | ||
| test_typedarray/test-immutable-arraybuffer: FAIL | ||
28 changes: 28 additions & 0 deletions
28
test/js-native-api/test_typedarray/test-immutable-arraybuffer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Flags: --js-immutable-arraybuffer | ||
| 'use strict'; | ||
|
|
||
| const common = require('../../common'); | ||
| const assert = require('node:assert'); | ||
|
|
||
| if (process.argv[2] === 'child') { | ||
| const binding = require(`./build/${common.buildType}/test_typedarray`); | ||
| const mutable = Uint8Array.from([65, 66, 67, 68]); | ||
| binding.Detach(mutable); | ||
| assert.strictEqual(binding.IsDetached(mutable.buffer), true); | ||
|
|
||
| const buffer = Uint8Array.from([65, 66, 67, 68]).buffer.transferToImmutable(); | ||
| const view = new Uint8Array(buffer); | ||
|
|
||
| // napi_detach_arraybuffer() must return an error or propagate an exception | ||
| // when detaching an immutable ArrayBuffer, without aborting the process. | ||
| assert.throws(() => binding.Detach(view), Error); | ||
| assert.strictEqual(binding.IsDetached(buffer), false); | ||
| assert.strictEqual(buffer.immutable, true); | ||
| assert.deepStrictEqual([...view], [65, 66, 67, 68]); | ||
| } else { | ||
| const { spawnSyncAndExitWithoutError } = require('../../common/child_process'); | ||
| const [command, options] = common.escapePOSIXShell`"${process.execPath}" --js-immutable-arraybuffer "${__filename}" child`; | ||
| // Avoid core files while this known issue still aborts the child. | ||
| const childCommand = common.isWindows ? command : `ulimit -c 0 && exec ${command}`; | ||
| spawnSyncAndExitWithoutError(childCommand, { ...options, shell: true }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Flags: --js-immutable-arraybuffer | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { test } = require('node:test'); | ||
|
|
||
| // Native Buffer mutators must reject immutable backing storage without | ||
| // modifying its bytes. | ||
| function checkImmutable(mutate) { | ||
| const expected = Array.from({ length: 64 }, (_, i) => i); | ||
| const ab = Uint8Array.from(expected).buffer.transferToImmutable(); | ||
| const buffer = Buffer.from(ab); | ||
|
|
||
| // The error behavior for immutable backing storage is not defined yet. | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| assert.throws(() => mutate(buffer)); | ||
|
|
||
| assert.deepStrictEqual(Array.from(new Uint8Array(ab)), expected); | ||
| } | ||
|
|
||
| const encodings = [ | ||
| ['utf8', 'hello'], | ||
| ['utf-8', 'hello'], | ||
| ['ascii', 'hello'], | ||
| ['latin1', 'hello'], | ||
| ['binary', 'hello'], | ||
| ['hex', '68656c6c6f'], | ||
| ['base64', 'aGVsbG8='], | ||
| ['base64url', 'aGVsbG8'], | ||
| ['ucs2', 'hello'], | ||
| ['ucs-2', 'hello'], | ||
| ['utf16le', 'hello'], | ||
| ['utf-16le', 'hello'], | ||
| ]; | ||
|
|
||
| for (const [encoding, value] of encodings) { | ||
| test(`Buffer.write with ${encoding} preserves immutable bytes`, () => { | ||
| checkImmutable((buffer) => buffer.write(value, encoding)); | ||
| }); | ||
| } | ||
|
|
||
| const writers = [ | ||
| ['utf8Write', 'hello'], | ||
| ['asciiWrite', 'hello'], | ||
| ['latin1Write', 'hello'], | ||
| ['hexWrite', '68656c6c6f'], | ||
| ['base64Write', 'aGVsbG8='], | ||
| ['base64urlWrite', 'aGVsbG8'], | ||
| ['ucs2Write', 'hello'], | ||
| ]; | ||
|
|
||
| for (const [method, value] of writers) { | ||
| test(`Buffer.${method} preserves immutable bytes`, () => { | ||
| checkImmutable((buffer) => buffer[method](value, 0, buffer.length)); | ||
| }); | ||
| } | ||
|
|
||
| const fills = [ | ||
| ['string', 'AB'], | ||
| ['non-ASCII character', 'é'], | ||
| ['Buffer', Buffer.from([0xfe, 0xff])], | ||
| ['coerced value', true], | ||
| ]; | ||
|
|
||
| for (const [name, value] of fills) { | ||
| test(`Buffer.fill with ${name} preserves immutable bytes`, () => { | ||
| checkImmutable((buffer) => buffer.fill(value)); | ||
| }); | ||
| } | ||
|
|
||
| for (const method of ['swap16', 'swap32', 'swap64']) { | ||
| test(`Buffer.${method} preserves immutable bytes`, () => { | ||
| // A 64-byte buffer reaches the native path for all three swap methods. | ||
| checkImmutable((buffer) => buffer[method]()); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Flags: --js-immutable-arraybuffer --experimental-stream-iter | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { test } = require('node:test'); | ||
| const { Readable } = require('node:stream'); | ||
| const iter = require('node:stream/iter'); | ||
| const { serialize, deserialize } = require('node:v8'); | ||
|
|
||
| // V8 currently checks the source of TypedArray.prototype.set() for write | ||
| // access. Copying immutable bytes into a mutable destination must be allowed. | ||
| function immutable(bytes = [65, 66, 67, 68]) { | ||
| return new Uint8Array(Uint8Array.from(bytes).buffer.transferToImmutable()); | ||
| } | ||
|
|
||
| for (const [name, copy] of [ | ||
| ['Buffer.from', (view) => Buffer.from(view)], | ||
| ['Buffer.copyBytesFrom', (view) => Buffer.copyBytesFrom(view)], | ||
| ['Buffer.concat', (view) => Buffer.concat([view])], | ||
| ['Buffer.concat with length', (view) => Buffer.concat([view], view.length)], | ||
| ]) { | ||
| test(name, () => { | ||
| const source = immutable(); | ||
| assert.deepStrictEqual([...copy(source)], [...source]); | ||
| }); | ||
| } | ||
|
|
||
| test('TextDecoder single-byte non-ASCII input', () => { | ||
| assert.strictEqual(new TextDecoder('windows-1252').decode(immutable([0xe9])), '\u00e9'); | ||
| }); | ||
|
|
||
| test('TextDecoder split UTF-8 sequence', () => { | ||
| const decoder = new TextDecoder(); | ||
| assert.strictEqual(decoder.decode(immutable([0xc3]), { stream: true }), ''); | ||
| assert.strictEqual(decoder.decode(immutable([0xa9])), '\u00e9'); | ||
| }); | ||
|
|
||
| test('TextDecoderStream split UTF-8 sequence', async () => { | ||
| const source = new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(immutable([0xc3])); | ||
| controller.enqueue(immutable([0xa9])); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
| let result = ''; | ||
| for await (const chunk of source.pipeThrough(new TextDecoderStream())) { | ||
| result += chunk; | ||
| } | ||
| assert.strictEqual(result, '\u00e9'); | ||
| }); | ||
|
|
||
| test('Readable.read spanning chunks', (t) => { | ||
| const stream = new Readable({ read() {} }); | ||
| t.after(() => stream.destroy()); | ||
| stream.push(Buffer.from(immutable().buffer)); | ||
| stream.push(Buffer.from(immutable().buffer)); | ||
| stream.push(null); | ||
| assert.deepStrictEqual([...stream.read(6)], [65, 66, 67, 68, 65, 66]); | ||
| }); | ||
|
|
||
| for (const name of ['bytes', 'bytesSync', 'arrayBuffer', 'arrayBufferSync', 'text', 'textSync']) { | ||
| test(`stream/iter.${name}`, async () => { | ||
| // A partial view cannot use the full-buffer fast path. | ||
| const result = await iter[name](immutable().subarray(1)); | ||
| if (typeof result === 'string') { | ||
| assert.strictEqual(result, 'BCD'); | ||
| } else { | ||
| assert.deepStrictEqual([...new Uint8Array(result)], [66, 67, 68]); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| for (const [name, consume] of [ | ||
| ['Response', (view) => new Response(view).text()], | ||
| ['Request', (view) => new Request('http://localhost/', { | ||
| method: 'POST', body: view, | ||
| }).text()], | ||
| ]) { | ||
| test(`${name} body`, async () => { | ||
| assert.strictEqual(await consume(immutable()), 'ABCD'); | ||
| }); | ||
| } | ||
|
|
||
| test('Response consumes immutable stream chunks', async () => { | ||
| const stream = new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(immutable()); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
| assert.strictEqual(await new Response(stream).text(), 'ABCD'); | ||
| }); | ||
|
|
||
| test('v8.deserialize copies an unaligned payload from immutable storage', () => { | ||
| const wire = serialize(new Uint32Array([1, 2])); | ||
| const source = wire.buffer.sliceToImmutable(wire.byteOffset, wire.byteOffset + wire.length); | ||
| assert.deepStrictEqual([...deserialize(new Uint8Array(source))], [1, 2]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Flags: --js-immutable-arraybuffer | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
|
|
||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
|
|
||
| const assert = require('assert'); | ||
| const crypto = require('crypto'); | ||
| const { test } = require('node:test'); | ||
|
|
||
| const cases = [ | ||
| ['randomFillSync with an ArrayBuffer', (buffer) => { | ||
| crypto.randomFillSync(buffer); | ||
| }], | ||
| ['randomFill with a DataView', (buffer) => { | ||
| return new Promise((resolve, reject) => { | ||
| crypto.randomFill(new DataView(buffer), (err) => { | ||
| if (err) { | ||
| reject(err); | ||
| return; | ||
| } | ||
| resolve(); | ||
| }); | ||
| }); | ||
| }], | ||
| ['getRandomValues with a Uint8Array', (buffer) => { | ||
| crypto.getRandomValues(new Uint8Array(buffer)); | ||
| }], | ||
| ['global crypto.getRandomValues with a Buffer', (buffer) => { | ||
| globalThis.crypto.getRandomValues(Buffer.from(buffer)); | ||
| }], | ||
| ]; | ||
|
|
||
| for (const [name, fill] of cases) { | ||
| test(name, async () => { | ||
| const buffer = new ArrayBuffer(32).transferToImmutable(); | ||
| const bytes = new Uint8Array(buffer); | ||
| const original = Array.from(bytes); | ||
|
|
||
| // The error behavior for immutable backing storage is not defined yet. | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| await assert.rejects(async () => fill(buffer)); | ||
|
|
||
| assert.strictEqual(buffer.immutable, true); | ||
| assert.deepStrictEqual(Array.from(bytes), original); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Flags: --js-immutable-arraybuffer | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { test } = require('node:test'); | ||
|
|
||
| for (const [name, input] of [['short', 'hello'], ['long', 'hello'.repeat(16)]]) { | ||
| test(`TextEncoder.encodeInto with ${name} input preserves immutable bytes`, () => { | ||
| const expected = Array(128).fill(0xaa); | ||
| const ab = Uint8Array.from(expected).buffer.transferToImmutable(); | ||
| const destination = new Uint8Array(ab); | ||
|
|
||
| // The short and long inputs exercise different native encoding paths. | ||
| // The error behavior for immutable backing storage is not defined yet. | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| assert.throws(() => { | ||
| new TextEncoder().encodeInto(input, destination); | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(Array.from(new Uint8Array(ab)), expected); | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.