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 { getSystemErrorName } = require('util');
91cb0ef41Sopenharmony_ciconst { execSync } = require('child_process');
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  assert.throws(() => {
211cb0ef41Sopenharmony_ci    execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 });
221cb0ef41Sopenharmony_ci  }, (e) => {
231cb0ef41Sopenharmony_ci    assert.ok(e, 'maxBuffer should error');
241cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'ENOBUFS');
251cb0ef41Sopenharmony_ci    assert.strictEqual(getSystemErrorName(e.errno), 'ENOBUFS');
261cb0ef41Sopenharmony_ci    // We can have buffers larger than maxBuffer because underneath we alloc 64k
271cb0ef41Sopenharmony_ci    // that matches our read sizes.
281cb0ef41Sopenharmony_ci    assert.deepStrictEqual(e.stdout, msgOutBuf);
291cb0ef41Sopenharmony_ci    return true;
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci// Verify that a maxBuffer size of Infinity works.
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  const ret = execSync(
361cb0ef41Sopenharmony_ci    `"${process.execPath}" ${args.join(' ')}`,
371cb0ef41Sopenharmony_ci    { maxBuffer: Infinity }
381cb0ef41Sopenharmony_ci  );
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  assert.deepStrictEqual(ret, msgOutBuf);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// Default maxBuffer size is 1024 * 1024.
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  assert.throws(() => {
461cb0ef41Sopenharmony_ci    execSync(
471cb0ef41Sopenharmony_ci      `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`
481cb0ef41Sopenharmony_ci    );
491cb0ef41Sopenharmony_ci  }, (e) => {
501cb0ef41Sopenharmony_ci    assert.ok(e, 'maxBuffer should error');
511cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'ENOBUFS');
521cb0ef41Sopenharmony_ci    assert.strictEqual(getSystemErrorName(e.errno), 'ENOBUFS');
531cb0ef41Sopenharmony_ci    return true;
541cb0ef41Sopenharmony_ci  });
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci// Default maxBuffer size is 1024 * 1024.
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  const ret = execSync(
601cb0ef41Sopenharmony_ci    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`
611cb0ef41Sopenharmony_ci  );
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
641cb0ef41Sopenharmony_ci    ret.toString().trim(),
651cb0ef41Sopenharmony_ci    'a'.repeat(1024 * 1024 - 1)
661cb0ef41Sopenharmony_ci  );
671cb0ef41Sopenharmony_ci}
68