1// Flags: --expose-gc 2'use strict'; 3 4const common = require('../../common'); 5const skipMessage = 'intensive toString tests due to memory confinements'; 6if (!common.enoughTestMem) 7 common.skip(skipMessage); 8 9const binding = require(`./build/${common.buildType}/binding`); 10const assert = require('assert'); 11 12// v8 fails silently if string length > v8::String::kMaxLength 13// v8::String::kMaxLength defined in v8.h 14const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH; 15 16let buf; 17try { 18 buf = Buffer.allocUnsafe(kStringMaxLength + 1); 19} catch (e) { 20 // If the exception is not due to memory confinement then rethrow it. 21 if (e.message !== 'Array buffer allocation failed') throw (e); 22 common.skip(skipMessage); 23} 24 25// Ensure we have enough memory available for future allocations to succeed. 26if (!binding.ensureAllocation(2 * kStringMaxLength)) 27 common.skip(skipMessage); 28 29const stringLengthHex = kStringMaxLength.toString(16); 30assert.throws(() => { 31 buf.toString('latin1'); 32}, { 33 message: `Cannot create a string longer than 0x${stringLengthHex} ` + 34 'characters', 35 code: 'ERR_STRING_TOO_LONG', 36 name: 'Error', 37}); 38 39// FIXME: Free the memory early to avoid OOM. 40// REF: https://github.com/nodejs/reliability/issues/12#issuecomment-412619655 41global.gc(); 42let maxString = buf.toString('latin1', 1); 43assert.strictEqual(maxString.length, kStringMaxLength); 44maxString = undefined; 45global.gc(); 46 47maxString = buf.toString('latin1', 0, kStringMaxLength); 48assert.strictEqual(maxString.length, kStringMaxLength); 49