|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +// This test ensures that filehandle.write accepts "named parameters" object |
| 6 | +// and doesn't interpret objects as strings |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | +const fsPromises = require('fs').promises; |
| 10 | +const path = require('path'); |
| 11 | +const tmpdir = require('../common/tmpdir'); |
| 12 | + |
| 13 | +tmpdir.refresh(); |
| 14 | + |
| 15 | +const dest = path.resolve(tmpdir.path, 'tmp.txt'); |
| 16 | +const buffer = Buffer.from('zyx'); |
| 17 | + |
| 18 | +async function testInvalid(dest, expectedCode, params) { |
| 19 | + let fh; |
| 20 | + try { |
| 21 | + fh = await fsPromises.open(dest, 'w+'); |
| 22 | + await assert.rejects( |
| 23 | + async () => fh.write(params), |
| 24 | + { code: expectedCode }); |
| 25 | + } finally { |
| 26 | + await fh?.close(); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async function testValid(dest, params) { |
| 31 | + let fh; |
| 32 | + try { |
| 33 | + fh = await fsPromises.open(dest, 'w+'); |
| 34 | + const writeResult = await fh.write(params); |
| 35 | + const writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer); |
| 36 | + const readResult = await fh.read(params); |
| 37 | + const readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer); |
| 38 | + |
| 39 | + assert.ok(writeResult.bytesWritten >= readResult.bytesRead); |
| 40 | + if (params.length !== undefined && params.length !== null) { |
| 41 | + assert.strictEqual(writeResult.bytesWritten, params.length); |
| 42 | + } |
| 43 | + if (params.offset === undefined || params.offset === 0) { |
| 44 | + assert.deepStrictEqual(writeBufCopy, readBufCopy); |
| 45 | + } |
| 46 | + assert.deepStrictEqual(writeResult.buffer, readResult.buffer); |
| 47 | + } finally { |
| 48 | + await fh?.close(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +(async () => { |
| 53 | + // Test if first argument is not wrongly interpreted as ArrayBufferView|string |
| 54 | + for (const badParams of [ |
| 55 | + undefined, null, true, 42, 42n, Symbol('42'), NaN, [], |
| 56 | + {}, |
| 57 | + { buffer: 'amNotParam' }, |
| 58 | + { string: 'amNotParam' }, |
| 59 | + { buffer: new Uint8Array(1).buffer }, |
| 60 | + new Date(), |
| 61 | + new String('notPrimitive'), |
| 62 | + { toString() { return 'amObject'; } }, |
| 63 | + { [Symbol.toPrimitive]: (hint) => 'amObject' }, |
| 64 | + ]) { |
| 65 | + await testInvalid(dest, 'ERR_INVALID_ARG_TYPE', badParams); |
| 66 | + } |
| 67 | + |
| 68 | + // Various invalid params |
| 69 | + await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, length: 5 }); |
| 70 | + await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, offset: 5 }); |
| 71 | + await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, length: 1, offset: 3 }); |
| 72 | + await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, length: -1 }); |
| 73 | + await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, offset: -1 }); |
| 74 | + |
| 75 | + // Test compatibility with filehandle.read counterpart with reused params |
| 76 | + for (const params of [ |
| 77 | + { buffer }, |
| 78 | + { buffer, length: 1 }, |
| 79 | + { buffer, position: 5 }, |
| 80 | + { buffer, length: 1, position: 5 }, |
| 81 | + { buffer, length: 1, position: -1, offset: 2 }, |
| 82 | + { buffer, length: null }, |
| 83 | + { buffer, offset: 1 }, |
| 84 | + ]) { |
| 85 | + await testValid(dest, params); |
| 86 | + } |
| 87 | +})().then(common.mustCall()); |
0 commit comments