diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-04-02 10:39:26 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-04-07 17:15:13 +0800 |
commit | 63eb267c34198c4bece62cb6432bb2bceb399de6 (patch) | |
tree | 7a3188e205b7205a7cfb2854bd289f5d7fec35d1 /test | |
parent | 289d152ce0222d3be32bf20ef7dc7d8d5e40f218 (diff) | |
download | android-node-v8-63eb267c34198c4bece62cb6432bb2bceb399de6.tar.gz android-node-v8-63eb267c34198c4bece62cb6432bb2bceb399de6.tar.bz2 android-node-v8-63eb267c34198c4bece62cb6432bb2bceb399de6.zip |
src: migrate string_bytes.cc to throw errors with code
PR-URL: https://github.com/nodejs/node/pull/19739
Fixes: https://github.com/nodejs/node/issues/3175
Fixes: https://github.com/nodejs/node/issues/9489
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test')
7 files changed, 65 insertions, 19 deletions
diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js index 43b8c63f1e..96a3273254 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js @@ -6,7 +6,6 @@ if (!common.enoughTestMem) common.skip(skipMessage); const binding = require(`./build/${common.buildType}/binding`); -const assert = require('assert'); // v8 fails silently if string length > v8::String::kMaxLength // v8::String::kMaxLength defined in v8.h @@ -25,6 +24,11 @@ try { if (!binding.ensureAllocation(2 * kStringMaxLength)) common.skip(skipMessage); -assert.throws(function() { +const stringLengthHex = kStringMaxLength.toString(16); +common.expectsError(function() { buf.toString('ascii'); -}, /"toString\(\)" failed/); +}, { + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error +}); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js index a94a57e180..90e13ce788 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js @@ -6,7 +6,6 @@ if (!common.enoughTestMem) common.skip(skipMessage); const binding = require(`./build/${common.buildType}/binding`); -const assert = require('assert'); // v8 fails silently if string length > v8::String::kMaxLength // v8::String::kMaxLength defined in v8.h @@ -25,6 +24,11 @@ try { if (!binding.ensureAllocation(2 * kStringMaxLength)) common.skip(skipMessage); -assert.throws(function() { +const stringLengthHex = kStringMaxLength.toString(16); +common.expectsError(function() { buf.toString('base64'); -}, /"toString\(\)" failed/); +}, { + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error +}); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js index 996c01752d..0ffd1eb416 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js @@ -25,9 +25,14 @@ try { if (!binding.ensureAllocation(2 * kStringMaxLength)) common.skip(skipMessage); -assert.throws(function() { +const stringLengthHex = kStringMaxLength.toString(16); +common.expectsError(function() { buf.toString('latin1'); -}, /"toString\(\)" failed/); +}, { + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error +}); let maxString = buf.toString('latin1', 1); assert.strictEqual(maxString.length, kStringMaxLength); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js index 17d9ae5d68..bc64ef396d 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js @@ -6,7 +6,6 @@ if (!common.enoughTestMem) common.skip(skipMessage); const binding = require(`./build/${common.buildType}/binding`); -const assert = require('assert'); // v8 fails silently if string length > v8::String::kMaxLength // v8::String::kMaxLength defined in v8.h @@ -25,6 +24,11 @@ try { if (!binding.ensureAllocation(2 * kStringMaxLength)) common.skip(skipMessage); -assert.throws(function() { +const stringLengthHex = kStringMaxLength.toString(16); +common.expectsError(function() { buf.toString('hex'); -}, /"toString\(\)" failed/); +}, { + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error +}); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js index d3368ca7b2..f6c9f21e4b 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js @@ -25,10 +25,27 @@ try { if (!binding.ensureAllocation(2 * kStringMaxLength)) common.skip(skipMessage); +const stringLengthHex = kStringMaxLength.toString(16); + assert.throws(function() { buf.toString(); -}, /"toString\(\)" failed|Array buffer allocation failed/); +}, function(e) { + if (e.message !== 'Array buffer allocation failed') { + common.expectsError({ + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error + })(e); + return true; + } else { + return true; + } +}); -assert.throws(function() { +common.expectsError(function() { buf.toString('utf8'); -}, /"toString\(\)" failed/); +}, { + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error +}); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js index 319a8a9355..e4b66d8f30 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js @@ -6,7 +6,6 @@ if (!common.enoughTestMem) common.skip(skipMessage); const binding = require(`./build/${common.buildType}/binding`); -const assert = require('assert'); // v8 fails silently if string length > v8::String::kMaxLength // v8::String::kMaxLength defined in v8.h @@ -25,6 +24,12 @@ try { if (!binding.ensureAllocation(2 * kStringMaxLength)) common.skip(skipMessage); -assert.throws(function() { +const stringLengthHex = kStringMaxLength.toString(16); + +common.expectsError(function() { buf.toString('utf16le'); -}, /"toString\(\)" failed/); +}, { + message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error +}); diff --git a/test/sequential/test-fs-readfile-tostring-fail.js b/test/sequential/test-fs-readfile-tostring-fail.js index 81c7a941eb..f8b0c666a0 100644 --- a/test/sequential/test-fs-readfile-tostring-fail.js +++ b/test/sequential/test-fs-readfile-tostring-fail.js @@ -32,8 +32,15 @@ stream.end(); stream.on('finish', common.mustCall(function() { fs.readFile(file, 'utf8', common.mustCall(function(err, buf) { assert.ok(err instanceof Error); - assert.ok(/^(Array buffer allocation failed|"toString\(\)" failed)$/ - .test(err.message)); + if (err.message !== 'Array buffer allocation failed') { + const stringLengthHex = kStringMaxLength.toString(16); + common.expectsError({ + message: 'Cannot create a string larger than ' + + `0x${stringLengthHex} bytes`, + code: 'ERR_STRING_TOO_LARGE', + type: Error + })(err); + } assert.strictEqual(buf, undefined); })); })); |