1'use strict'; 2 3const common = require('../../common'); 4const skipMessage = 'intensive toString tests due to memory confinements'; 5if (!common.enoughTestMem) 6 common.skip(skipMessage); 7 8const assert = require('assert'); 9const binding = require(`./build/${common.buildType}/binding`); 10 11// v8 fails silently if string length > v8::String::kMaxLength 12// v8::String::kMaxLength defined in v8.h 13const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH; 14 15let buf; 16try { 17 buf = Buffer.allocUnsafe(kStringMaxLength * 2 + 2); 18} catch (e) { 19 // If the exception is not due to memory confinement then rethrow it. 20 if (e.message !== 'Array buffer allocation failed') throw (e); 21 common.skip(skipMessage); 22} 23 24// Ensure we have enough memory available for future allocations to succeed. 25if (!binding.ensureAllocation(2 * kStringMaxLength)) 26 common.skip(skipMessage); 27 28const stringLengthHex = kStringMaxLength.toString(16); 29 30assert.throws(() => { 31 buf.toString('utf16le'); 32}, { 33 message: `Cannot create a string longer than 0x${stringLengthHex} ` + 34 'characters', 35 code: 'ERR_STRING_TOO_LONG', 36 name: 'Error', 37}); 38