1'use strict';
2const common = require('../common');
3
4// This tests the creation of a single executable application.
5
6const fixtures = require('../common/fixtures');
7const tmpdir = require('../common/tmpdir');
8const { copyFileSync, readFileSync, writeFileSync } = require('fs');
9const { execFileSync } = require('child_process');
10const { join } = require('path');
11const { strictEqual } = require('assert');
12
13if (!process.config.variables.single_executable_application)
14  common.skip('Single Executable Application support has been disabled.');
15
16if (!['darwin', 'win32', 'linux'].includes(process.platform))
17  common.skip(`Unsupported platform ${process.platform}.`);
18
19if (process.platform === 'linux' && process.config.variables.asan) {
20  // Source of the memory leak - https://github.com/nodejs/node/blob/da0bc6db98cef98686122ea1e2cd2dbd2f52d123/src/node_sea.cc#L94.
21  common.skip('Running the resultant binary fails because of a memory leak ASAN error.');
22}
23
24if (process.platform === 'linux' && process.config.variables.is_debug === 1)
25  common.skip('Running the resultant binary fails with `Couldn\'t read target executable"`.');
26
27if (process.config.variables.node_shared)
28  common.skip('Running the resultant binary fails with ' +
29    '`/home/iojs/node-tmp/.tmp.2366/sea: error while loading shared libraries: ' +
30    'libnode.so.112: cannot open shared object file: No such file or directory`.');
31
32if (process.config.variables.icu_gyp_path === 'tools/icu/icu-system.gyp')
33  common.skip('Running the resultant binary fails with ' +
34    '`/home/iojs/node-tmp/.tmp.2379/sea: error while loading shared libraries: ' +
35    'libicui18n.so.71: cannot open shared object file: No such file or directory`.');
36
37if (!process.config.variables.node_use_openssl || process.config.variables.node_shared_openssl)
38  common.skip('Running the resultant binary fails with `Node.js is not compiled with OpenSSL crypto support`.');
39
40if (process.config.variables.want_separate_host_toolset !== 0)
41  common.skip('Running the resultant binary fails with `Segmentation fault (core dumped)`.');
42
43if (process.platform === 'linux') {
44  const osReleaseText = readFileSync('/etc/os-release', { encoding: 'utf-8' });
45  const isAlpine = /^NAME="Alpine Linux"/m.test(osReleaseText);
46  if (isAlpine) common.skip('Alpine Linux is not supported.');
47
48  if (process.arch === 's390x') {
49    common.skip('On s390x, postject fails with `memory access out of bounds`.');
50  }
51
52  if (process.arch === 'ppc64') {
53    common.skip('On ppc64, this test times out.');
54  }
55}
56
57const inputFile = fixtures.path('sea.js');
58const requirableFile = join(tmpdir.path, 'requirable.js');
59const outputFile = join(tmpdir.path, process.platform === 'win32' ? 'sea.exe' : 'sea');
60
61tmpdir.refresh();
62
63writeFileSync(requirableFile, `
64module.exports = {
65  hello: 'world',
66};
67`);
68
69copyFileSync(process.execPath, outputFile);
70const postjectFile = fixtures.path('postject-copy', 'node_modules', 'postject', 'dist', 'cli.js');
71execFileSync(process.execPath, [
72  postjectFile,
73  outputFile,
74  'NODE_JS_CODE',
75  inputFile,
76  '--sentinel-fuse', 'NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
77  ...process.platform === 'darwin' ? [ '--macho-segment-name', 'NODE_JS' ] : [],
78]);
79
80if (process.platform === 'darwin') {
81  execFileSync('codesign', [ '--sign', '-', outputFile ]);
82  execFileSync('codesign', [ '--verify', outputFile ]);
83} else if (process.platform === 'win32') {
84  let signtoolFound = false;
85  try {
86    execFileSync('where', [ 'signtool' ]);
87    signtoolFound = true;
88  } catch (err) {
89    console.log(err.message);
90  }
91  if (signtoolFound) {
92    let certificatesFound = false;
93    try {
94      execFileSync('signtool', [ 'sign', '/fd', 'SHA256', outputFile ]);
95      certificatesFound = true;
96    } catch (err) {
97      if (!/SignTool Error: No certificates were found that met all the given criteria/.test(err)) {
98        throw err;
99      }
100    }
101    if (certificatesFound) {
102      execFileSync('signtool', 'verify', '/pa', 'SHA256', outputFile);
103    }
104  }
105}
106
107const singleExecutableApplicationOutput = execFileSync(
108  outputFile,
109  [ '-a', '--b=c', 'd' ],
110  { env: { COMMON_DIRECTORY: join(__dirname, '..', 'common') } });
111strictEqual(singleExecutableApplicationOutput.toString(), 'Hello, world! �\n');
112