From 04dc1f19b547384fac8d96e5b517f53a47490bec Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Mon, 3 May 2021 17:03:59 +0800 Subject: [PATCH] test: improve fs coverage --- test/parallel/test-fs-read.js | 11 +++++++++-- test/parallel/test-fs-readfile.js | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-fs-read.js b/test/parallel/test-fs-read.js index 531eba00d2847d5..c5e18234b053a68 100644 --- a/test/parallel/test-fs-read.js +++ b/test/parallel/test-fs-read.js @@ -78,13 +78,20 @@ assert.throws( } ); -['buffer', 'offset', 'length'].forEach((option) => +['buffer', 'offset', 'length'].forEach((option) => { assert.throws( () => fs.read(fd, { [option]: null }), `not throws when options.${option} is null` - )); + ); + assert.throws( + () => fs.readSync(fd, { + [option]: null + }), + `not throws when options.${option} is null` + ); +}); assert.throws( () => fs.read(null, Buffer.alloc(1), 0, 1, 0), diff --git a/test/parallel/test-fs-readfile.js b/test/parallel/test-fs-readfile.js index a081eec099890d2..712bc059f9e7c00 100644 --- a/test/parallel/test-fs-readfile.js +++ b/test/parallel/test-fs-readfile.js @@ -52,6 +52,23 @@ for (const e of fileInfo) { assert.deepStrictEqual(buf, e.contents); })); } +// Test readFile size too large +{ + const kIoMaxLength = 2 ** 31 - 1; + + const file = path.join(tmpdir.path, `${prefix}-too-large.txt`); + fs.writeFileSync(file, Buffer.from('0')); + fs.truncateSync(file, kIoMaxLength + 1); + + fs.readFile(file, common.mustCall((err) => { + assert.strictEqual(err.name, 'RangeError'); + assert.strictEqual(err.code, 'ERR_FS_FILE_TOO_LARGE'); + })); + assert.throws(() => { + fs.readFileSync(file); + }, { code: 'ERR_FS_FILE_TOO_LARGE', name: 'RangeError' }); +} + { // Test cancellation, before const signal = AbortSignal.abort();