11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This tests the creation of a single executable application.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ciconst { copyFileSync, readFileSync, writeFileSync } = require('fs');
91cb0ef41Sopenharmony_ciconst { execFileSync } = require('child_process');
101cb0ef41Sopenharmony_ciconst { join } = require('path');
111cb0ef41Sopenharmony_ciconst { strictEqual } = require('assert');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciif (!process.config.variables.single_executable_application)
141cb0ef41Sopenharmony_ci  common.skip('Single Executable Application support has been disabled.');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciif (!['darwin', 'win32', 'linux'].includes(process.platform))
171cb0ef41Sopenharmony_ci  common.skip(`Unsupported platform ${process.platform}.`);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciif (process.platform === 'linux' && process.config.variables.asan) {
201cb0ef41Sopenharmony_ci  // Source of the memory leak - https://github.com/nodejs/node/blob/da0bc6db98cef98686122ea1e2cd2dbd2f52d123/src/node_sea.cc#L94.
211cb0ef41Sopenharmony_ci  common.skip('Running the resultant binary fails because of a memory leak ASAN error.');
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciif (process.platform === 'linux' && process.config.variables.is_debug === 1)
251cb0ef41Sopenharmony_ci  common.skip('Running the resultant binary fails with `Couldn\'t read target executable"`.');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciif (process.config.variables.node_shared)
281cb0ef41Sopenharmony_ci  common.skip('Running the resultant binary fails with ' +
291cb0ef41Sopenharmony_ci    '`/home/iojs/node-tmp/.tmp.2366/sea: error while loading shared libraries: ' +
301cb0ef41Sopenharmony_ci    'libnode.so.112: cannot open shared object file: No such file or directory`.');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciif (process.config.variables.icu_gyp_path === 'tools/icu/icu-system.gyp')
331cb0ef41Sopenharmony_ci  common.skip('Running the resultant binary fails with ' +
341cb0ef41Sopenharmony_ci    '`/home/iojs/node-tmp/.tmp.2379/sea: error while loading shared libraries: ' +
351cb0ef41Sopenharmony_ci    'libicui18n.so.71: cannot open shared object file: No such file or directory`.');
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciif (!process.config.variables.node_use_openssl || process.config.variables.node_shared_openssl)
381cb0ef41Sopenharmony_ci  common.skip('Running the resultant binary fails with `Node.js is not compiled with OpenSSL crypto support`.');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciif (process.config.variables.want_separate_host_toolset !== 0)
411cb0ef41Sopenharmony_ci  common.skip('Running the resultant binary fails with `Segmentation fault (core dumped)`.');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciif (process.platform === 'linux') {
441cb0ef41Sopenharmony_ci  const osReleaseText = readFileSync('/etc/os-release', { encoding: 'utf-8' });
451cb0ef41Sopenharmony_ci  const isAlpine = /^NAME="Alpine Linux"/m.test(osReleaseText);
461cb0ef41Sopenharmony_ci  if (isAlpine) common.skip('Alpine Linux is not supported.');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  if (process.arch === 's390x') {
491cb0ef41Sopenharmony_ci    common.skip('On s390x, postject fails with `memory access out of bounds`.');
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  if (process.arch === 'ppc64') {
531cb0ef41Sopenharmony_ci    common.skip('On ppc64, this test times out.');
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciconst inputFile = fixtures.path('sea.js');
581cb0ef41Sopenharmony_ciconst requirableFile = join(tmpdir.path, 'requirable.js');
591cb0ef41Sopenharmony_ciconst outputFile = join(tmpdir.path, process.platform === 'win32' ? 'sea.exe' : 'sea');
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_citmpdir.refresh();
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciwriteFileSync(requirableFile, `
641cb0ef41Sopenharmony_cimodule.exports = {
651cb0ef41Sopenharmony_ci  hello: 'world',
661cb0ef41Sopenharmony_ci};
671cb0ef41Sopenharmony_ci`);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cicopyFileSync(process.execPath, outputFile);
701cb0ef41Sopenharmony_ciconst postjectFile = fixtures.path('postject-copy', 'node_modules', 'postject', 'dist', 'cli.js');
711cb0ef41Sopenharmony_ciexecFileSync(process.execPath, [
721cb0ef41Sopenharmony_ci  postjectFile,
731cb0ef41Sopenharmony_ci  outputFile,
741cb0ef41Sopenharmony_ci  'NODE_JS_CODE',
751cb0ef41Sopenharmony_ci  inputFile,
761cb0ef41Sopenharmony_ci  '--sentinel-fuse', 'NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
771cb0ef41Sopenharmony_ci  ...process.platform === 'darwin' ? [ '--macho-segment-name', 'NODE_JS' ] : [],
781cb0ef41Sopenharmony_ci]);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciif (process.platform === 'darwin') {
811cb0ef41Sopenharmony_ci  execFileSync('codesign', [ '--sign', '-', outputFile ]);
821cb0ef41Sopenharmony_ci  execFileSync('codesign', [ '--verify', outputFile ]);
831cb0ef41Sopenharmony_ci} else if (process.platform === 'win32') {
841cb0ef41Sopenharmony_ci  let signtoolFound = false;
851cb0ef41Sopenharmony_ci  try {
861cb0ef41Sopenharmony_ci    execFileSync('where', [ 'signtool' ]);
871cb0ef41Sopenharmony_ci    signtoolFound = true;
881cb0ef41Sopenharmony_ci  } catch (err) {
891cb0ef41Sopenharmony_ci    console.log(err.message);
901cb0ef41Sopenharmony_ci  }
911cb0ef41Sopenharmony_ci  if (signtoolFound) {
921cb0ef41Sopenharmony_ci    let certificatesFound = false;
931cb0ef41Sopenharmony_ci    try {
941cb0ef41Sopenharmony_ci      execFileSync('signtool', [ 'sign', '/fd', 'SHA256', outputFile ]);
951cb0ef41Sopenharmony_ci      certificatesFound = true;
961cb0ef41Sopenharmony_ci    } catch (err) {
971cb0ef41Sopenharmony_ci      if (!/SignTool Error: No certificates were found that met all the given criteria/.test(err)) {
981cb0ef41Sopenharmony_ci        throw err;
991cb0ef41Sopenharmony_ci      }
1001cb0ef41Sopenharmony_ci    }
1011cb0ef41Sopenharmony_ci    if (certificatesFound) {
1021cb0ef41Sopenharmony_ci      execFileSync('signtool', 'verify', '/pa', 'SHA256', outputFile);
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci  }
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciconst singleExecutableApplicationOutput = execFileSync(
1081cb0ef41Sopenharmony_ci  outputFile,
1091cb0ef41Sopenharmony_ci  [ '-a', '--b=c', 'd' ],
1101cb0ef41Sopenharmony_ci  { env: { COMMON_DIRECTORY: join(__dirname, '..', 'common') } });
1111cb0ef41Sopenharmony_cistrictEqual(singleExecutableApplicationOutput.toString(), 'Hello, world! �\n');
112