1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5
6const { readFileSync } = require('fs');
7const { execFileSync } = require('child_process');
8
9function skipIfSingleExecutableIsNotSupported() {
10  if (!process.config.variables.single_executable_application)
11    common.skip('Single Executable Application support has been disabled.');
12
13  if (!['darwin', 'win32', 'linux'].includes(process.platform))
14    common.skip(`Unsupported platform ${process.platform}.`);
15
16  if (process.platform === 'linux' && process.config.variables.is_debug === 1)
17    common.skip('Running the resultant binary fails with `Couldn\'t read target executable"`.');
18
19  if (process.config.variables.node_shared)
20    common.skip('Running the resultant binary fails with ' +
21      '`/home/iojs/node-tmp/.tmp.2366/sea: error while loading shared libraries: ' +
22      'libnode.so.112: cannot open shared object file: No such file or directory`.');
23
24  if (process.config.variables.icu_gyp_path === 'tools/icu/icu-system.gyp')
25    common.skip('Running the resultant binary fails with ' +
26      '`/home/iojs/node-tmp/.tmp.2379/sea: error while loading shared libraries: ' +
27      'libicui18n.so.71: cannot open shared object file: No such file or directory`.');
28
29  if (!process.config.variables.node_use_openssl || process.config.variables.node_shared_openssl)
30    common.skip('Running the resultant binary fails with `Node.js is not compiled with OpenSSL crypto support`.');
31
32  if (process.config.variables.want_separate_host_toolset !== 0)
33    common.skip('Running the resultant binary fails with `Segmentation fault (core dumped)`.');
34
35  if (process.platform === 'linux') {
36    const osReleaseText = readFileSync('/etc/os-release', { encoding: 'utf-8' });
37    const isAlpine = /^NAME="Alpine Linux"/m.test(osReleaseText);
38    if (isAlpine) common.skip('Alpine Linux is not supported.');
39
40    if (process.arch === 's390x') {
41      common.skip('On s390x, postject fails with `memory access out of bounds`.');
42    }
43  }
44}
45
46function injectAndCodeSign(targetExecutable, resource) {
47  const postjectFile = fixtures.path('postject-copy', 'node_modules', 'postject', 'dist', 'cli.js');
48  execFileSync(process.execPath, [
49    postjectFile,
50    targetExecutable,
51    'NODE_SEA_BLOB',
52    resource,
53    '--sentinel-fuse', 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
54    ...process.platform === 'darwin' ? [ '--macho-segment-name', 'NODE_SEA' ] : [],
55  ]);
56
57  if (process.platform === 'darwin') {
58    execFileSync('codesign', [ '--sign', '-', targetExecutable ]);
59    execFileSync('codesign', [ '--verify', targetExecutable ]);
60  } else if (process.platform === 'win32') {
61    let signtoolFound = false;
62    try {
63      execFileSync('where', [ 'signtool' ]);
64      signtoolFound = true;
65    } catch (err) {
66      console.log(err.message);
67    }
68    if (signtoolFound) {
69      let certificatesFound = false;
70      try {
71        execFileSync('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ]);
72        certificatesFound = true;
73      } catch (err) {
74        if (!/SignTool Error: No certificates were found that met all the given criteria/.test(err)) {
75          throw err;
76        }
77      }
78      if (certificatesFound) {
79        execFileSync('signtool', 'verify', '/pa', 'SHA256', targetExecutable);
80      }
81    }
82  }
83}
84
85module.exports = {
86  skipIfSingleExecutableIsNotSupported,
87  injectAndCodeSign,
88};
89