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 + 1); 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); 29assert.throws(() => { 30 buf.toString('hex'); 31}, { 32 message: `Cannot create a string longer than 0x${stringLengthHex} ` + 33 'characters', 34 code: 'ERR_STRING_TOO_LONG', 35 name: 'Error' 36}); 37