1'use strict'; 2 3const common = require('../common'); 4 5if (!common.enoughTestMem) 6 common.skip('intensive toString tests due to memory confinements'); 7 8const assert = require('assert'); 9const fs = require('fs'); 10const path = require('path'); 11const cp = require('child_process'); 12const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH; 13if (common.isAIX && (Number(cp.execSync('ulimit -f')) * 512) < kStringMaxLength) 14 common.skip('intensive toString tests due to file size confinements'); 15 16const tmpdir = require('../common/tmpdir'); 17tmpdir.refresh(); 18 19const file = path.join(tmpdir.path, 'toobig.txt'); 20const stream = fs.createWriteStream(file, { 21 flags: 'a' 22}); 23 24stream.on('error', (err) => { throw err; }); 25 26const size = kStringMaxLength / 200; 27const a = Buffer.alloc(size, 'a'); 28 29for (let i = 0; i < 201; i++) { 30 stream.write(a); 31} 32 33stream.end(); 34stream.on('finish', common.mustCall(function() { 35 fs.readFile(file, 'utf8', common.mustCall(function(err, buf) { 36 assert.ok(err instanceof Error); 37 if (err.message !== 'Array buffer allocation failed') { 38 const stringLengthHex = kStringMaxLength.toString(16); 39 common.expectsError({ 40 message: 'Cannot create a string longer than ' + 41 `0x${stringLengthHex} characters`, 42 code: 'ERR_STRING_TOO_LONG', 43 name: 'Error' 44 })(err); 45 } 46 assert.strictEqual(buf, undefined); 47 })); 48})); 49 50function destroy() { 51 try { 52 fs.unlinkSync(file); 53 } catch { 54 // it may not exist 55 } 56} 57 58process.on('exit', destroy); 59 60process.on('SIGINT', function() { 61 destroy(); 62 process.exit(); 63}); 64 65// To make sure we don't leave a very large file 66// on test machines in the event this test fails. 67process.on('uncaughtException', function(err) { 68 destroy(); 69 throw err; 70}); 71