11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test checks that the maxBuffer option for child_process.execFileSync()
51cb0ef41Sopenharmony_ci// works as expected.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { getSystemErrorName } = require('util');
91cb0ef41Sopenharmony_ciconst { execFileSync } = 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    execFileSync(process.execPath, args, { 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 = execFileSync(process.execPath, args, { maxBuffer: Infinity });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  assert.deepStrictEqual(ret, msgOutBuf);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// Default maxBuffer size is 1024 * 1024.
411cb0ef41Sopenharmony_ci{
421cb0ef41Sopenharmony_ci  assert.throws(() => {
431cb0ef41Sopenharmony_ci    execFileSync(
441cb0ef41Sopenharmony_ci      process.execPath,
451cb0ef41Sopenharmony_ci      ['-e', "console.log('a'.repeat(1024 * 1024))"]
461cb0ef41Sopenharmony_ci    );
471cb0ef41Sopenharmony_ci  }, (e) => {
481cb0ef41Sopenharmony_ci    assert.ok(e, 'maxBuffer should error');
491cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'ENOBUFS');
501cb0ef41Sopenharmony_ci    assert.strictEqual(getSystemErrorName(e.errno), 'ENOBUFS');
511cb0ef41Sopenharmony_ci    return true;
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci}
54