11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciif (!common.enoughTestMem) 61cb0ef41Sopenharmony_ci common.skip('intensive toString tests due to memory confinements'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst fs = require('fs'); 101cb0ef41Sopenharmony_ciconst path = require('path'); 111cb0ef41Sopenharmony_ciconst cp = require('child_process'); 121cb0ef41Sopenharmony_ciconst kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH; 131cb0ef41Sopenharmony_ciif (common.isAIX && (Number(cp.execSync('ulimit -f')) * 512) < kStringMaxLength) 141cb0ef41Sopenharmony_ci common.skip('intensive toString tests due to file size confinements'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 171cb0ef41Sopenharmony_citmpdir.refresh(); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciif (!tmpdir.hasEnoughSpace(kStringMaxLength)) { 201cb0ef41Sopenharmony_ci common.skip(`Not enough space in ${tmpdir.path}`); 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciconst file = path.join(tmpdir.path, 'toobig.txt'); 241cb0ef41Sopenharmony_ciconst stream = fs.createWriteStream(file, { 251cb0ef41Sopenharmony_ci flags: 'a', 261cb0ef41Sopenharmony_ci}); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cistream.on('error', (err) => { throw err; }); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst size = kStringMaxLength / 200; 311cb0ef41Sopenharmony_ciconst a = Buffer.alloc(size, 'a'); 321cb0ef41Sopenharmony_cilet expectedSize = 0; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_cifor (let i = 0; i < 201; i++) { 351cb0ef41Sopenharmony_ci stream.write(a, (err) => { assert.ifError(err); }); 361cb0ef41Sopenharmony_ci expectedSize += a.length; 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cistream.end(); 401cb0ef41Sopenharmony_cistream.on('finish', common.mustCall(function() { 411cb0ef41Sopenharmony_ci assert.strictEqual(stream.bytesWritten, expectedSize, 421cb0ef41Sopenharmony_ci `${stream.bytesWritten} bytes written (expected ${expectedSize} bytes).`); 431cb0ef41Sopenharmony_ci fs.readFile(file, 'utf8', common.mustCall(function(err, buf) { 441cb0ef41Sopenharmony_ci assert.ok(err instanceof Error); 451cb0ef41Sopenharmony_ci if (err.message !== 'Array buffer allocation failed') { 461cb0ef41Sopenharmony_ci const stringLengthHex = kStringMaxLength.toString(16); 471cb0ef41Sopenharmony_ci common.expectsError({ 481cb0ef41Sopenharmony_ci message: 'Cannot create a string longer than ' + 491cb0ef41Sopenharmony_ci `0x${stringLengthHex} characters`, 501cb0ef41Sopenharmony_ci code: 'ERR_STRING_TOO_LONG', 511cb0ef41Sopenharmony_ci name: 'Error', 521cb0ef41Sopenharmony_ci })(err); 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci assert.strictEqual(buf, undefined); 551cb0ef41Sopenharmony_ci })); 561cb0ef41Sopenharmony_ci})); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cifunction destroy() { 591cb0ef41Sopenharmony_ci try { 601cb0ef41Sopenharmony_ci fs.unlinkSync(file); 611cb0ef41Sopenharmony_ci } catch { 621cb0ef41Sopenharmony_ci // it may not exist 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciprocess.on('exit', destroy); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciprocess.on('SIGINT', function() { 691cb0ef41Sopenharmony_ci destroy(); 701cb0ef41Sopenharmony_ci process.exit(); 711cb0ef41Sopenharmony_ci}); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci// To make sure we don't leave a very large file 741cb0ef41Sopenharmony_ci// on test machines in the event this test fails. 751cb0ef41Sopenharmony_ciprocess.on('uncaughtException', function(err) { 761cb0ef41Sopenharmony_ci destroy(); 771cb0ef41Sopenharmony_ci throw err; 781cb0ef41Sopenharmony_ci}); 79