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 19if (!tmpdir.hasEnoughSpace(kStringMaxLength)) { 20 common.skip(`Not enough space in ${tmpdir.path}`); 21} 22 23const file = path.join(tmpdir.path, 'toobig.txt'); 24const stream = fs.createWriteStream(file, { 25 flags: 'a', 26}); 27 28stream.on('error', (err) => { throw err; }); 29 30const size = kStringMaxLength / 200; 31const a = Buffer.alloc(size, 'a'); 32let expectedSize = 0; 33 34for (let i = 0; i < 201; i++) { 35 stream.write(a, (err) => { assert.ifError(err); }); 36 expectedSize += a.length; 37} 38 39stream.end(); 40stream.on('finish', common.mustCall(function() { 41 assert.strictEqual(stream.bytesWritten, expectedSize, 42 `${stream.bytesWritten} bytes written (expected ${expectedSize} bytes).`); 43 fs.readFile(file, 'utf8', common.mustCall(function(err, buf) { 44 assert.ok(err instanceof Error); 45 if (err.message !== 'Array buffer allocation failed') { 46 const stringLengthHex = kStringMaxLength.toString(16); 47 common.expectsError({ 48 message: 'Cannot create a string longer than ' + 49 `0x${stringLengthHex} characters`, 50 code: 'ERR_STRING_TOO_LONG', 51 name: 'Error', 52 })(err); 53 } 54 assert.strictEqual(buf, undefined); 55 })); 56})); 57 58function destroy() { 59 try { 60 fs.unlinkSync(file); 61 } catch { 62 // it may not exist 63 } 64} 65 66process.on('exit', destroy); 67 68process.on('SIGINT', function() { 69 destroy(); 70 process.exit(); 71}); 72 73// To make sure we don't leave a very large file 74// on test machines in the event this test fails. 75process.on('uncaughtException', function(err) { 76 destroy(); 77 throw err; 78}); 79