11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test checks that the maxBuffer option for child_process.spawnSync()
51cb0ef41Sopenharmony_ci// works as expected.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst spawnSync = require('child_process').spawnSync;
91cb0ef41Sopenharmony_ciconst { getSystemErrorName } = require('util');
101cb0ef41Sopenharmony_ciconst msgOut = 'this is stdout';
111cb0ef41Sopenharmony_ciconst msgOutBuf = Buffer.from(`${msgOut}\n`);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst args = [
141cb0ef41Sopenharmony_ci  '-e',
151cb0ef41Sopenharmony_ci  `console.log("${msgOut}");`,
161cb0ef41Sopenharmony_ci];
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Verify that an error is returned if maxBuffer is surpassed.
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  const ret = spawnSync(process.execPath, args, { maxBuffer: 1 });
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  assert.ok(ret.error, 'maxBuffer should error');
231cb0ef41Sopenharmony_ci  assert.strictEqual(ret.error.code, 'ENOBUFS');
241cb0ef41Sopenharmony_ci  assert.strictEqual(getSystemErrorName(ret.error.errno), 'ENOBUFS');
251cb0ef41Sopenharmony_ci  // We can have buffers larger than maxBuffer because underneath we alloc 64k
261cb0ef41Sopenharmony_ci  // that matches our read sizes.
271cb0ef41Sopenharmony_ci  assert.deepStrictEqual(ret.stdout, msgOutBuf);
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// Verify that a maxBuffer size of Infinity works.
311cb0ef41Sopenharmony_ci{
321cb0ef41Sopenharmony_ci  const ret = spawnSync(process.execPath, args, { maxBuffer: Infinity });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  assert.ifError(ret.error);
351cb0ef41Sopenharmony_ci  assert.deepStrictEqual(ret.stdout, msgOutBuf);
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci// Default maxBuffer size is 1024 * 1024.
391cb0ef41Sopenharmony_ci{
401cb0ef41Sopenharmony_ci  const args = ['-e', "console.log('a'.repeat(1024 * 1024))"];
411cb0ef41Sopenharmony_ci  const ret = spawnSync(process.execPath, args);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  assert.ok(ret.error, 'maxBuffer should error');
441cb0ef41Sopenharmony_ci  assert.strictEqual(ret.error.code, 'ENOBUFS');
451cb0ef41Sopenharmony_ci  assert.strictEqual(getSystemErrorName(ret.error.errno), 'ENOBUFS');
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// Default maxBuffer size is 1024 * 1024.
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const args = ['-e', "console.log('a'.repeat(1024 * 1024 - 1))"];
511cb0ef41Sopenharmony_ci  const ret = spawnSync(process.execPath, args);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  assert.ifError(ret.error);
541cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
551cb0ef41Sopenharmony_ci    ret.stdout.toString().trim(),
561cb0ef41Sopenharmony_ci    'a'.repeat(1024 * 1024 - 1)
571cb0ef41Sopenharmony_ci  );
581cb0ef41Sopenharmony_ci}
59