11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cifunction runChecks(err, stdio, streamName, expected) {
71cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`);
81cb0ef41Sopenharmony_ci  assert(err instanceof RangeError);
91cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
101cb0ef41Sopenharmony_ci  assert.deepStrictEqual(stdio[streamName], expected);
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// default value
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  const cmd =
161cb0ef41Sopenharmony_ci    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  cp.exec(cmd, common.mustCall((err) => {
191cb0ef41Sopenharmony_ci    assert(err instanceof RangeError);
201cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'stdout maxBuffer length exceeded');
211cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
221cb0ef41Sopenharmony_ci  }));
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// default value
261cb0ef41Sopenharmony_ci{
271cb0ef41Sopenharmony_ci  const cmd =
281cb0ef41Sopenharmony_ci    `${process.execPath} -e "console.log('a'.repeat(1024 * 1024 - 1))"`;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  cp.exec(cmd, common.mustSucceed((stdout, stderr) => {
311cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
321cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
331cb0ef41Sopenharmony_ci  }));
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" -e "console.log('hello world');"`;
381cb0ef41Sopenharmony_ci  const options = { maxBuffer: Infinity };
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => {
411cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), 'hello world');
421cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci{
471cb0ef41Sopenharmony_ci  const cmd = 'echo hello world';
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  cp.exec(
501cb0ef41Sopenharmony_ci    cmd,
511cb0ef41Sopenharmony_ci    { maxBuffer: 5 },
521cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
531cb0ef41Sopenharmony_ci      runChecks(err, { stdout, stderr }, 'stdout', 'hello');
541cb0ef41Sopenharmony_ci    })
551cb0ef41Sopenharmony_ci  );
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci// default value
591cb0ef41Sopenharmony_ci{
601cb0ef41Sopenharmony_ci  const cmd =
611cb0ef41Sopenharmony_ci    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`;
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  cp.exec(
641cb0ef41Sopenharmony_ci    cmd,
651cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
661cb0ef41Sopenharmony_ci      runChecks(
671cb0ef41Sopenharmony_ci        err,
681cb0ef41Sopenharmony_ci        { stdout, stderr },
691cb0ef41Sopenharmony_ci        'stdout',
701cb0ef41Sopenharmony_ci        'a'.repeat(1024 * 1024)
711cb0ef41Sopenharmony_ci      );
721cb0ef41Sopenharmony_ci    })
731cb0ef41Sopenharmony_ci  );
741cb0ef41Sopenharmony_ci}
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci// default value
771cb0ef41Sopenharmony_ci{
781cb0ef41Sopenharmony_ci  const cmd =
791cb0ef41Sopenharmony_ci    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  cp.exec(cmd, common.mustSucceed((stdout, stderr) => {
821cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
831cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
841cb0ef41Sopenharmony_ci  }));
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciconst unicode = '中文测试'; // length = 4, byte length = 12
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  cp.exec(
931cb0ef41Sopenharmony_ci    cmd,
941cb0ef41Sopenharmony_ci    { maxBuffer: 10 },
951cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
961cb0ef41Sopenharmony_ci      runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n');
971cb0ef41Sopenharmony_ci    })
981cb0ef41Sopenharmony_ci  );
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci{
1021cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  cp.exec(
1051cb0ef41Sopenharmony_ci    cmd,
1061cb0ef41Sopenharmony_ci    { maxBuffer: 3 },
1071cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
1081cb0ef41Sopenharmony_ci      runChecks(err, { stdout, stderr }, 'stderr', '中文测');
1091cb0ef41Sopenharmony_ci    })
1101cb0ef41Sopenharmony_ci  );
1111cb0ef41Sopenharmony_ci}
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci{
1141cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci  const child = cp.exec(
1171cb0ef41Sopenharmony_ci    cmd,
1181cb0ef41Sopenharmony_ci    { encoding: null, maxBuffer: 10 },
1191cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
1201cb0ef41Sopenharmony_ci      runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n');
1211cb0ef41Sopenharmony_ci    })
1221cb0ef41Sopenharmony_ci  );
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  child.stdout.setEncoding('utf-8');
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci{
1281cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  const child = cp.exec(
1311cb0ef41Sopenharmony_ci    cmd,
1321cb0ef41Sopenharmony_ci    { encoding: null, maxBuffer: 3 },
1331cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
1341cb0ef41Sopenharmony_ci      runChecks(err, { stdout, stderr }, 'stderr', '中文测');
1351cb0ef41Sopenharmony_ci    })
1361cb0ef41Sopenharmony_ci  );
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  child.stderr.setEncoding('utf-8');
1391cb0ef41Sopenharmony_ci}
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci{
1421cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  cp.exec(
1451cb0ef41Sopenharmony_ci    cmd,
1461cb0ef41Sopenharmony_ci    { encoding: null, maxBuffer: 5 },
1471cb0ef41Sopenharmony_ci    common.mustCall((err, stdout, stderr) => {
1481cb0ef41Sopenharmony_ci      const buf = Buffer.from(unicode).slice(0, 5);
1491cb0ef41Sopenharmony_ci      runChecks(err, { stdout, stderr }, 'stderr', buf);
1501cb0ef41Sopenharmony_ci    })
1511cb0ef41Sopenharmony_ci  );
1521cb0ef41Sopenharmony_ci}
153