2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as childProcess from 'node:child_process';
|
2024-06-19 14:10:16 +00:00
|
|
|
import * as fs from 'node:fs';
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as path from 'node:path';
|
|
|
|
import * as util from 'node:util';
|
2023-01-25 21:01:25 +00:00
|
|
|
import { getRemoteContext, ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
|
2024-05-13 17:48:26 +00:00
|
|
|
import { copyMacOSFixtureApp, getCodesignIdentity, shouldRunCodesignTests, signApp, spawn } from './lib/codesign-helpers';
|
2023-02-16 14:41:41 +00:00
|
|
|
import { webContents } from 'electron/main';
|
2023-06-15 14:42:27 +00:00
|
|
|
import { EventEmitter } from 'node:stream';
|
|
|
|
import { once } from 'node:events';
|
2024-05-13 17:48:26 +00:00
|
|
|
import { withTempDirectory } from './lib/fs-helpers';
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2021-03-22 20:11:03 +00:00
|
|
|
const mainFixturesPath = path.resolve(__dirname, 'fixtures');
|
2019-09-20 14:41:40 +00:00
|
|
|
|
|
|
|
describe('node feature', () => {
|
2022-08-16 19:23:13 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures');
|
2022-08-05 00:20:17 +00:00
|
|
|
|
2019-09-20 14:41:40 +00:00
|
|
|
describe('child_process', () => {
|
|
|
|
describe('child_process.fork', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
it('Works in browser process', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const child = childProcess.fork(path.join(fixtures, 'module', 'ping.js'));
|
2023-02-23 23:53:53 +00:00
|
|
|
const message = once(child, 'message');
|
2020-03-20 20:28:31 +00:00
|
|
|
child.send('message');
|
2020-06-30 22:10:36 +00:00
|
|
|
const [msg] = await message;
|
|
|
|
expect(msg).to.equal('message');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2024-01-26 08:29:04 +00:00
|
|
|
|
|
|
|
it('Has its module searth paths restricted', async () => {
|
|
|
|
const child = childProcess.fork(path.join(fixtures, 'module', 'module-paths.js'));
|
|
|
|
const [msg] = await once(child, 'message');
|
|
|
|
expect(msg.length).to.equal(2);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2023-06-08 20:40:08 +00:00
|
|
|
describe('child_process in renderer', () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
useRemoteContext();
|
|
|
|
|
|
|
|
describe('child_process.fork', () => {
|
|
|
|
itremote('works in current process', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'ping.js'));
|
2022-08-05 00:20:17 +00:00
|
|
|
const message = new Promise<any>(resolve => child.once('message', resolve));
|
|
|
|
child.send('message');
|
|
|
|
const msg = await message;
|
|
|
|
expect(msg).to.equal('message');
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('preserves args', async (fixtures: string) => {
|
|
|
|
const args = ['--expose_gc', '-test', '1'];
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'process_args.js'), args);
|
2022-08-05 00:20:17 +00:00
|
|
|
const message = new Promise<any>(resolve => child.once('message', resolve));
|
|
|
|
child.send('message');
|
|
|
|
const msg = await message;
|
|
|
|
expect(args).to.deep.equal(msg.slice(2));
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('works in forked process', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'fork_ping.js'));
|
2022-08-05 00:20:17 +00:00
|
|
|
const message = new Promise<any>(resolve => child.once('message', resolve));
|
|
|
|
child.send('message');
|
|
|
|
const msg = await message;
|
|
|
|
expect(msg).to.equal('message');
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('works in forked process when options.env is specified', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'fork_ping.js'), [], {
|
2022-08-05 00:20:17 +00:00
|
|
|
path: process.env.PATH
|
|
|
|
});
|
|
|
|
const message = new Promise<any>(resolve => child.once('message', resolve));
|
|
|
|
child.send('message');
|
|
|
|
const msg = await message;
|
|
|
|
expect(msg).to.equal('message');
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('has String::localeCompare working in script', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'locale-compare.js'));
|
2022-08-05 00:20:17 +00:00
|
|
|
const message = new Promise<any>(resolve => child.once('message', resolve));
|
|
|
|
child.send('message');
|
|
|
|
const msg = await message;
|
|
|
|
expect(msg).to.deep.equal([0, -1, 1]);
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('has setImmediate working in script', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'set-immediate.js'));
|
2022-08-05 00:20:17 +00:00
|
|
|
const message = new Promise<any>(resolve => child.once('message', resolve));
|
|
|
|
child.send('message');
|
|
|
|
const msg = await message;
|
|
|
|
expect(msg).to.equal('ok');
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('pipes stdio', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').fork(require('node:path').join(fixtures, 'module', 'process-stdout.js'), { silent: true });
|
2022-08-05 00:20:17 +00:00
|
|
|
let data = '';
|
|
|
|
child.stdout.on('data', (chunk: any) => {
|
|
|
|
data += String(chunk);
|
|
|
|
});
|
|
|
|
const code = await new Promise<any>(resolve => child.once('close', resolve));
|
|
|
|
expect(code).to.equal(0);
|
|
|
|
expect(data).to.equal('pipes stdio');
|
|
|
|
}, [fixtures]);
|
|
|
|
|
|
|
|
itremote('works when sending a message to a process forked with the --eval argument', async () => {
|
|
|
|
const source = "process.on('message', (message) => { process.send(message) })";
|
2023-06-15 14:42:27 +00:00
|
|
|
const forked = require('node:child_process').fork('--eval', [source]);
|
2022-08-05 00:20:17 +00:00
|
|
|
const message = new Promise(resolve => forked.once('message', resolve));
|
|
|
|
forked.send('hello');
|
|
|
|
const msg = await message;
|
|
|
|
expect(msg).to.equal('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has the electron version in process.versions', async () => {
|
|
|
|
const source = 'process.send(process.versions)';
|
2023-06-15 14:42:27 +00:00
|
|
|
const forked = require('node:child_process').fork('--eval', [source]);
|
2023-02-23 23:53:53 +00:00
|
|
|
const [message] = await once(forked, 'message');
|
2022-08-05 00:20:17 +00:00
|
|
|
expect(message)
|
|
|
|
.to.have.own.property('electron')
|
|
|
|
.that.is.a('string')
|
|
|
|
.and.matches(/^\d+\.\d+\.\d+(\S*)?$/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('child_process.spawn', () => {
|
|
|
|
itremote('supports spawning Electron as a node process via the ELECTRON_RUN_AS_NODE env var', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const child = require('node:child_process').spawn(process.execPath, [require('node:path').join(fixtures, 'module', 'run-as-node.js')], {
|
2022-08-05 00:20:17 +00:00
|
|
|
env: {
|
|
|
|
ELECTRON_RUN_AS_NODE: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
child.stdout.on('data', (data: any) => {
|
|
|
|
output += data;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await new Promise(resolve => child.stdout.once('close', resolve));
|
|
|
|
expect(JSON.parse(output)).to.deep.equal({
|
|
|
|
stdoutType: 'pipe',
|
|
|
|
processType: 'undefined',
|
|
|
|
window: 'undefined'
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
child.kill();
|
|
|
|
}
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('child_process.exec', () => {
|
|
|
|
ifit(process.platform === 'linux')('allows executing a setuid binary from non-sandboxed renderer', async () => {
|
|
|
|
// Chrome uses prctl(2) to set the NO_NEW_PRIVILEGES flag on Linux (see
|
|
|
|
// https://github.com/torvalds/linux/blob/40fde647cc/Documentation/userspace-api/no_new_privs.rst).
|
|
|
|
// We disable this for unsandboxed processes, which the renderer tests
|
|
|
|
// are running in. If this test fails with an error like 'effective uid
|
|
|
|
// is not 0', then it's likely that our patch to prevent the flag from
|
|
|
|
// being set has become ineffective.
|
|
|
|
const w = await getRemoteContext();
|
|
|
|
const stdout = await w.webContents.executeJavaScript('require(\'child_process\').execSync(\'sudo --help\')');
|
|
|
|
expect(stdout).to.not.be.empty();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-06-20 15:01:50 +00:00
|
|
|
describe('fetch', () => {
|
|
|
|
itremote('works correctly when nodeIntegration is enabled in the renderer', async (fixtures: string) => {
|
|
|
|
const file = require('node:path').join(fixtures, 'hello.txt');
|
|
|
|
expect(() => {
|
|
|
|
fetch('file://' + file);
|
|
|
|
}).to.not.throw();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('username', 'Groucho');
|
|
|
|
}).not.to.throw();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
const request = new Request('https://example.com', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ foo: 'bar' })
|
|
|
|
});
|
|
|
|
expect(request.method).to.equal('POST');
|
|
|
|
}).not.to.throw();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
const response = new Response('Hello, world!');
|
|
|
|
expect(response.status).to.equal(200);
|
|
|
|
}).not.to.throw();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.append('Content-Type', 'text/xml');
|
|
|
|
}).not.to.throw();
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
2021-04-09 07:09:17 +00:00
|
|
|
it('does not hang when using the fs module in the renderer process', async () => {
|
2021-03-22 20:11:03 +00:00
|
|
|
const appPath = path.join(mainFixturesPath, 'apps', 'libuv-hang', 'main.js');
|
|
|
|
const appProcess = childProcess.spawn(process.execPath, [appPath], {
|
|
|
|
cwd: path.join(mainFixturesPath, 'apps', 'libuv-hang'),
|
|
|
|
stdio: 'inherit'
|
|
|
|
});
|
2023-02-23 23:53:53 +00:00
|
|
|
const [code] = await once(appProcess, 'close');
|
2021-03-22 20:11:03 +00:00
|
|
|
expect(code).to.equal(0);
|
|
|
|
});
|
|
|
|
|
2019-09-20 14:41:40 +00:00
|
|
|
describe('contexts', () => {
|
|
|
|
describe('setTimeout called under Chromium event loop in browser process', () => {
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Can be scheduled in time', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
setTimeout(done, 0);
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Can be promisified', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
util.promisify(setTimeout)(0).then(done);
|
|
|
|
});
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
|
|
|
describe('setInterval called under Chromium event loop in browser process', () => {
|
|
|
|
it('can be scheduled in time', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let interval: any = null;
|
|
|
|
let clearing = false;
|
2019-09-20 14:41:40 +00:00
|
|
|
const clear = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (interval === null || clearing) return;
|
2019-09-20 14:41:40 +00:00
|
|
|
|
|
|
|
// interval might trigger while clearing (remote is slow sometimes)
|
2020-03-20 20:28:31 +00:00
|
|
|
clearing = true;
|
|
|
|
clearInterval(interval);
|
|
|
|
clearing = false;
|
|
|
|
interval = null;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
interval = setInterval(clear, 10);
|
|
|
|
});
|
|
|
|
});
|
2022-08-05 00:20:17 +00:00
|
|
|
|
|
|
|
const suspendListeners = (emitter: EventEmitter, eventName: string, callback: (...args: any[]) => void) => {
|
|
|
|
const listeners = emitter.listeners(eventName) as ((...args: any[]) => void)[];
|
|
|
|
emitter.removeAllListeners(eventName);
|
|
|
|
emitter.once(eventName, (...args) => {
|
|
|
|
emitter.removeAllListeners(eventName);
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const listener of listeners) {
|
2022-08-05 00:20:17 +00:00
|
|
|
emitter.on(eventName, listener);
|
2023-08-31 14:36:43 +00:00
|
|
|
}
|
2022-08-05 00:20:17 +00:00
|
|
|
|
|
|
|
callback(...args);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
describe('error thrown in main process node context', () => {
|
|
|
|
it('gets emitted as a process uncaughtException event', async () => {
|
|
|
|
fs.readFile(__filename, () => {
|
|
|
|
throw new Error('hello');
|
|
|
|
});
|
|
|
|
const result = await new Promise(resolve => suspendListeners(process, 'uncaughtException', (error) => {
|
|
|
|
resolve(error.message);
|
|
|
|
}));
|
|
|
|
expect(result).to.equal('hello');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('promise rejection in main process node context', () => {
|
|
|
|
it('gets emitted as a process unhandledRejection event', async () => {
|
|
|
|
fs.readFile(__filename, () => {
|
|
|
|
Promise.reject(new Error('hello'));
|
|
|
|
});
|
|
|
|
const result = await new Promise(resolve => suspendListeners(process, 'unhandledRejection', (error) => {
|
|
|
|
resolve(error.message);
|
|
|
|
}));
|
|
|
|
expect(result).to.equal('hello');
|
|
|
|
});
|
2023-03-06 10:04:43 +00:00
|
|
|
|
|
|
|
it('does not log the warning more than once when the rejection is unhandled', async () => {
|
|
|
|
const appPath = path.join(mainFixturesPath, 'api', 'unhandled-rejection.js');
|
|
|
|
const appProcess = childProcess.spawn(process.execPath, [appPath]);
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
const out = (data: string) => {
|
|
|
|
output += data;
|
|
|
|
if (/UnhandledPromiseRejectionWarning/.test(data)) {
|
|
|
|
appProcess.kill();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
appProcess.stdout!.on('data', out);
|
|
|
|
appProcess.stderr!.on('data', out);
|
|
|
|
|
|
|
|
await once(appProcess, 'exit');
|
|
|
|
expect(/UnhandledPromiseRejectionWarning/.test(output)).to.equal(true);
|
|
|
|
const matches = output.match(/Error: oops/gm);
|
|
|
|
expect(matches).to.have.lengthOf(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not log the warning more than once when the rejection is handled', async () => {
|
|
|
|
const appPath = path.join(mainFixturesPath, 'api', 'unhandled-rejection-handled.js');
|
|
|
|
const appProcess = childProcess.spawn(process.execPath, [appPath]);
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
const out = (data: string) => { output += data; };
|
|
|
|
appProcess.stdout!.on('data', out);
|
|
|
|
appProcess.stderr!.on('data', out);
|
|
|
|
|
|
|
|
const [code] = await once(appProcess, 'exit');
|
|
|
|
expect(code).to.equal(0);
|
|
|
|
expect(/UnhandledPromiseRejectionWarning/.test(output)).to.equal(false);
|
|
|
|
const matches = output.match(/Error: oops/gm);
|
|
|
|
expect(matches).to.have.lengthOf(1);
|
|
|
|
});
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('contexts in renderer', () => {
|
|
|
|
useRemoteContext();
|
|
|
|
|
|
|
|
describe('setTimeout in fs callback', () => {
|
|
|
|
itremote('does not crash', async (filename: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
await new Promise(resolve => require('node:fs').readFile(filename, () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
setTimeout(resolve, 0);
|
|
|
|
}));
|
|
|
|
}, [__filename]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('error thrown in renderer process node context', () => {
|
|
|
|
itremote('gets emitted as a process uncaughtException event', async (filename: string) => {
|
|
|
|
const error = new Error('boo!');
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:fs').readFile(filename, () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
process.once('uncaughtException', (thrown) => {
|
|
|
|
try {
|
|
|
|
expect(thrown).to.equal(error);
|
|
|
|
resolve();
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, [__filename]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('URL handling in the renderer process', () => {
|
|
|
|
itremote('can successfully handle WHATWG URLs constructed by Blink', (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const url = new URL('file://' + require('node:path').resolve(fixtures, 'pages', 'base-page.html'));
|
2022-08-05 00:20:17 +00:00
|
|
|
expect(() => {
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:fs').createReadStream(url);
|
2022-08-05 00:20:17 +00:00
|
|
|
}).to.not.throw();
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setTimeout called under blink env in renderer process', () => {
|
|
|
|
itremote('can be scheduled in time', async () => {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('works from the timers module', async () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
await new Promise(resolve => require('node:timers').setTimeout(resolve, 10));
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setInterval called under blink env in renderer process', () => {
|
|
|
|
itremote('can be scheduled in time', async () => {
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
const id = setInterval(() => {
|
|
|
|
clearInterval(id);
|
|
|
|
resolve();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('can be scheduled in time from timers module', async () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const { setInterval, clearInterval } = require('node:timers');
|
2022-08-05 00:20:17 +00:00
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
const id = setInterval(() => {
|
|
|
|
clearInterval(id);
|
|
|
|
resolve();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('message loop in renderer', () => {
|
|
|
|
useRemoteContext();
|
|
|
|
|
|
|
|
describe('process.nextTick', () => {
|
|
|
|
itremote('emits the callback', () => new Promise(resolve => process.nextTick(resolve)));
|
|
|
|
|
|
|
|
itremote('works in nested calls', () =>
|
|
|
|
new Promise(resolve => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
process.nextTick(() => process.nextTick(resolve));
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setImmediate', () => {
|
|
|
|
itremote('emits the callback', () => new Promise(resolve => setImmediate(resolve)));
|
|
|
|
|
|
|
|
itremote('works in nested calls', () => new Promise(resolve => {
|
|
|
|
setImmediate(() => {
|
|
|
|
setImmediate(() => setImmediate(resolve));
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-08 20:40:08 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('net.connect', () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
itremote('emit error when connect to a socket path without listeners', async (fixtures: string) => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const socketPath = require('node:path').join(require('node:os').tmpdir(), 'electron-test.sock');
|
|
|
|
const script = require('node:path').join(fixtures, 'module', 'create_socket.js');
|
|
|
|
const child = require('node:child_process').fork(script, [socketPath]);
|
2022-08-05 00:20:17 +00:00
|
|
|
const code = await new Promise(resolve => child.once('exit', resolve));
|
|
|
|
expect(code).to.equal(0);
|
2023-06-15 14:42:27 +00:00
|
|
|
const client = require('node:net').connect(socketPath);
|
2022-08-05 00:20:17 +00:00
|
|
|
const error = await new Promise<any>(resolve => client.once('error', resolve));
|
|
|
|
expect(error.code).to.equal('ECONNREFUSED');
|
|
|
|
}, [fixtures]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Buffer', () => {
|
|
|
|
useRemoteContext();
|
|
|
|
|
2022-10-12 00:01:57 +00:00
|
|
|
itremote('can be created from Blink external string', () => {
|
2022-08-05 00:20:17 +00:00
|
|
|
const p = document.createElement('p');
|
|
|
|
p.innerText = '闲云潭影日悠悠,物换星移几度秋';
|
|
|
|
const b = Buffer.from(p.innerText);
|
|
|
|
expect(b.toString()).to.equal('闲云潭影日悠悠,物换星移几度秋');
|
|
|
|
expect(Buffer.byteLength(p.innerText)).to.equal(45);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('correctly parses external one-byte UTF8 string', () => {
|
|
|
|
const p = document.createElement('p');
|
|
|
|
p.innerText = 'Jøhänñéß';
|
|
|
|
const b = Buffer.from(p.innerText);
|
|
|
|
expect(b.toString()).to.equal('Jøhänñéß');
|
|
|
|
expect(Buffer.byteLength(p.innerText)).to.equal(13);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('does not crash when creating large Buffers', () => {
|
|
|
|
let buffer = Buffer.from(new Array(4096).join(' '));
|
|
|
|
expect(buffer.length).to.equal(4095);
|
|
|
|
buffer = Buffer.from(new Array(4097).join(' '));
|
|
|
|
expect(buffer.length).to.equal(4096);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('does not crash for crypto operations', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const crypto = require('node:crypto');
|
2022-08-05 00:20:17 +00:00
|
|
|
const data = 'lG9E+/g4JmRmedDAnihtBD4Dfaha/GFOjd+xUOQI05UtfVX3DjUXvrS98p7kZQwY3LNhdiFo7MY5rGft8yBuDhKuNNag9vRx/44IuClDhdQ=';
|
|
|
|
const key = 'q90K9yBqhWZnAMCMTOJfPQ==';
|
|
|
|
const cipherText = '{"error_code":114,"error_message":"Tham số không hợp lệ","data":null}';
|
|
|
|
for (let i = 0; i < 10000; ++i) {
|
|
|
|
const iv = Buffer.from('0'.repeat(32), 'hex');
|
|
|
|
const input = Buffer.from(data, 'base64');
|
|
|
|
const decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'base64'), iv);
|
|
|
|
const result = Buffer.concat([decipher.update(input), decipher.final()]).toString('utf8');
|
|
|
|
expect(cipherText).to.equal(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('does not crash when using crypto.diffieHellman() constructors', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const crypto = require('node:crypto');
|
2022-08-05 00:20:17 +00:00
|
|
|
|
|
|
|
crypto.createDiffieHellman('abc');
|
|
|
|
crypto.createDiffieHellman('abc', 2);
|
|
|
|
|
|
|
|
// Needed to test specific DiffieHellman ctors.
|
|
|
|
|
|
|
|
crypto.createDiffieHellman('abc', Buffer.from([2]));
|
|
|
|
crypto.createDiffieHellman('abc', '123');
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('does not crash when calling crypto.createPrivateKey() with an unsupported algorithm', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const crypto = require('node:crypto');
|
2022-08-05 00:20:17 +00:00
|
|
|
|
|
|
|
const ed448 = {
|
|
|
|
crv: 'Ed448',
|
|
|
|
x: 'KYWcaDwgH77xdAwcbzOgvCVcGMy9I6prRQBhQTTdKXUcr-VquTz7Fd5adJO0wT2VHysF3bk3kBoA',
|
|
|
|
d: 'UhC3-vN5vp_g9PnTknXZgfXUez7Xvw-OfuJ0pYkuwzpYkcTvacqoFkV_O05WMHpyXkzH9q2wzx5n',
|
|
|
|
kty: 'OKP'
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
crypto.createPrivateKey({ key: ed448, format: 'jwk' });
|
|
|
|
}).to.throw(/Invalid JWK data/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('process.stdout', () => {
|
|
|
|
useRemoteContext();
|
|
|
|
|
|
|
|
itremote('does not throw an exception when accessed', () => {
|
|
|
|
expect(() => process.stdout).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('does not throw an exception when calling write()', () => {
|
|
|
|
expect(() => {
|
|
|
|
process.stdout.write('test');
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: figure out why process.stdout.isTTY is true on Darwin but not Linux/Win.
|
|
|
|
ifdescribe(process.platform !== 'darwin')('isTTY', () => {
|
|
|
|
itremote('should be undefined in the renderer process', function () {
|
|
|
|
expect(process.stdout.isTTY).to.be.undefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('process.stdin', () => {
|
|
|
|
useRemoteContext();
|
|
|
|
|
|
|
|
itremote('does not throw an exception when accessed', () => {
|
|
|
|
expect(() => process.stdin).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('returns null when read from', () => {
|
|
|
|
expect(process.stdin.read()).to.be.null();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('process.version', () => {
|
|
|
|
itremote('should not have -pre', () => {
|
|
|
|
expect(process.version.endsWith('-pre')).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('vm.runInNewContext', () => {
|
|
|
|
itremote('should not crash', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:vm').runInNewContext('');
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('crypto', () => {
|
|
|
|
useRemoteContext();
|
|
|
|
itremote('should list the ripemd160 hash in getHashes', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
expect(require('node:crypto').getHashes()).to.include('ripemd160');
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should be able to create a ripemd160 hash and use it', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const hash = require('node:crypto').createHash('ripemd160');
|
2022-08-05 00:20:17 +00:00
|
|
|
hash.update('electron-ripemd160');
|
|
|
|
expect(hash.digest('hex')).to.equal('fa7fec13c624009ab126ebb99eda6525583395fe');
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should list aes-{128,256}-cfb in getCiphers', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
expect(require('node:crypto').getCiphers()).to.include.members(['aes-128-cfb', 'aes-256-cfb']);
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should be able to create an aes-128-cfb cipher', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:crypto').createCipheriv('aes-128-cfb', '0123456789abcdef', '0123456789abcdef');
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should be able to create an aes-256-cfb cipher', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:crypto').createCipheriv('aes-256-cfb', '0123456789abcdef0123456789abcdef', '0123456789abcdef');
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should be able to create a bf-{cbc,cfb,ecb} ciphers', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:crypto').createCipheriv('bf-cbc', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
|
|
|
|
require('node:crypto').createCipheriv('bf-cfb', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
|
|
|
|
require('node:crypto').createCipheriv('bf-ecb', Buffer.from('0123456789abcdef'), Buffer.from('01234567'));
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should list des-ede-cbc in getCiphers', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
expect(require('node:crypto').getCiphers()).to.include('des-ede-cbc');
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should be able to create an des-ede-cbc cipher', () => {
|
|
|
|
const key = Buffer.from('0123456789abcdeff1e0d3c2b5a49786', 'hex');
|
|
|
|
const iv = Buffer.from('fedcba9876543210', 'hex');
|
2023-06-15 14:42:27 +00:00
|
|
|
require('node:crypto').createCipheriv('des-ede-cbc', key, iv);
|
2022-08-05 00:20:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should not crash when getting an ECDH key', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const ecdh = require('node:crypto').createECDH('prime256v1');
|
2022-08-05 00:20:17 +00:00
|
|
|
expect(ecdh.generateKeys()).to.be.an.instanceof(Buffer);
|
|
|
|
expect(ecdh.getPrivateKey()).to.be.an.instanceof(Buffer);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should not crash when generating DH keys or fetching DH fields', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const dh = require('node:crypto').createDiffieHellman('modp15');
|
2022-08-05 00:20:17 +00:00
|
|
|
expect(dh.generateKeys()).to.be.an.instanceof(Buffer);
|
|
|
|
expect(dh.getPublicKey()).to.be.an.instanceof(Buffer);
|
|
|
|
expect(dh.getPrivateKey()).to.be.an.instanceof(Buffer);
|
|
|
|
expect(dh.getPrime()).to.be.an.instanceof(Buffer);
|
|
|
|
expect(dh.getGenerator()).to.be.an.instanceof(Buffer);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('should not crash when creating an ECDH cipher', () => {
|
2023-06-15 14:42:27 +00:00
|
|
|
const crypto = require('node:crypto');
|
2022-08-05 00:20:17 +00:00
|
|
|
const dh = crypto.createECDH('prime256v1');
|
|
|
|
dh.generateKeys();
|
|
|
|
dh.setPrivateKey(dh.getPrivateKey());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('includes the electron version in process.versions', () => {
|
|
|
|
expect(process.versions)
|
|
|
|
.to.have.own.property('electron')
|
|
|
|
.that.is.a('string')
|
|
|
|
.and.matches(/^\d+\.\d+\.\d+(\S*)?$/);
|
|
|
|
});
|
|
|
|
|
|
|
|
itremote('includes the chrome version in process.versions', () => {
|
|
|
|
expect(process.versions)
|
|
|
|
.to.have.own.property('chrome')
|
|
|
|
.that.is.a('string')
|
|
|
|
.and.matches(/^\d+\.\d+\.\d+\.\d+$/);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2019-11-02 01:06:15 +00:00
|
|
|
describe('NODE_OPTIONS', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let child: childProcess.ChildProcessWithoutNullStreams;
|
|
|
|
let exitPromise: Promise<any[]>;
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Fails for options disallowed by Node.js itself', (done) => {
|
|
|
|
after(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [code, signal] = await exitPromise;
|
|
|
|
expect(signal).to.equal(null);
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
// Exit code 9 indicates cli flag parsing failure
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(code).to.equal(9);
|
|
|
|
child.kill();
|
|
|
|
});
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2022-06-27 08:29:18 +00:00
|
|
|
const env = { ...process.env, NODE_OPTIONS: '--v8-options' };
|
2020-03-20 20:28:31 +00:00
|
|
|
child = childProcess.spawn(process.execPath, { env });
|
2023-02-23 23:53:53 +00:00
|
|
|
exitPromise = once(child, 'exit');
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
|
|
|
let success = false;
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.removeListener('data', listener);
|
|
|
|
child.stdout.removeListener('data', listener);
|
|
|
|
};
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
const listener = (data: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
output += data;
|
2019-11-02 01:06:15 +00:00
|
|
|
if (/electron: --v8-options is not allowed in NODE_OPTIONS/m.test(output)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
success = true;
|
|
|
|
cleanup();
|
|
|
|
done();
|
2019-11-02 01:06:15 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
2020-02-07 02:59:38 +00:00
|
|
|
child.on('exit', () => {
|
|
|
|
if (!success) {
|
2020-03-20 20:28:31 +00:00
|
|
|
cleanup();
|
|
|
|
done(new Error(`Unexpected output: ${output.toString()}`));
|
2020-02-07 02:59:38 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Disallows crypto-related options', (done) => {
|
|
|
|
after(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.kill();
|
|
|
|
});
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2022-08-05 00:20:17 +00:00
|
|
|
const appPath = path.join(fixtures, 'module', 'noop.js');
|
2022-06-27 08:29:18 +00:00
|
|
|
const env = { ...process.env, NODE_OPTIONS: '--use-openssl-ca' };
|
2022-08-05 00:20:17 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--enable-logging', appPath], { env });
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.removeListener('data', listener);
|
|
|
|
child.stdout.removeListener('data', listener);
|
|
|
|
};
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
const listener = (data: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
output += data;
|
2019-11-02 01:06:15 +00:00
|
|
|
if (/The NODE_OPTION --use-openssl-ca is not supported in Electron/m.test(output)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
cleanup();
|
|
|
|
done();
|
2019-11-02 01:06:15 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
|
|
|
});
|
2021-06-03 05:46:44 +00:00
|
|
|
|
|
|
|
it('does allow --require in non-packaged apps', async () => {
|
|
|
|
const appPath = path.join(fixtures, 'module', 'noop.js');
|
2022-06-27 08:29:18 +00:00
|
|
|
const env = {
|
|
|
|
...process.env,
|
2021-06-03 05:46:44 +00:00
|
|
|
NODE_OPTIONS: `--require=${path.join(fixtures, 'module', 'fail.js')}`
|
2022-06-27 08:29:18 +00:00
|
|
|
};
|
2021-06-03 05:46:44 +00:00
|
|
|
// App should exit with code 1.
|
|
|
|
const child = childProcess.spawn(process.execPath, [appPath], { env });
|
2023-02-23 23:53:53 +00:00
|
|
|
const [code] = await once(child, 'exit');
|
2021-06-03 05:46:44 +00:00
|
|
|
expect(code).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow --require in packaged apps', async () => {
|
|
|
|
const appPath = path.join(fixtures, 'module', 'noop.js');
|
2022-06-27 08:29:18 +00:00
|
|
|
const env = {
|
|
|
|
...process.env,
|
2021-06-03 05:46:44 +00:00
|
|
|
ELECTRON_FORCE_IS_PACKAGED: 'true',
|
|
|
|
NODE_OPTIONS: `--require=${path.join(fixtures, 'module', 'fail.js')}`
|
2022-06-27 08:29:18 +00:00
|
|
|
};
|
2021-06-03 05:46:44 +00:00
|
|
|
// App should exit with code 0.
|
|
|
|
const child = childProcess.spawn(process.execPath, [appPath], { env });
|
2023-02-23 23:53:53 +00:00
|
|
|
const [code] = await once(child, 'exit');
|
2021-06-03 05:46:44 +00:00
|
|
|
expect(code).to.equal(0);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2023-12-06 02:23:54 +00:00
|
|
|
ifdescribe(shouldRunCodesignTests)('NODE_OPTIONS in signed app', function () {
|
|
|
|
let identity = '';
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
const result = getCodesignIdentity();
|
|
|
|
if (result === null) {
|
|
|
|
this.skip();
|
|
|
|
} else {
|
|
|
|
identity = result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const script = path.join(fixtures, 'api', 'fork-with-node-options.js');
|
2024-01-04 07:34:08 +00:00
|
|
|
const nodeOptionsWarning = 'Node.js environment variables are disabled because this process is invoked by other apps';
|
2023-12-06 02:23:54 +00:00
|
|
|
|
|
|
|
it('is disabled when invoked by other apps in ELECTRON_RUN_AS_NODE mode', async () => {
|
|
|
|
await withTempDirectory(async (dir) => {
|
2024-05-13 17:48:26 +00:00
|
|
|
const appPath = await copyMacOSFixtureApp(dir);
|
2023-12-06 02:23:54 +00:00
|
|
|
await signApp(appPath, identity);
|
|
|
|
// Invoke Electron by using the system node binary as middle layer, so
|
|
|
|
// the check of NODE_OPTIONS will think the process is started by other
|
|
|
|
// apps.
|
|
|
|
const { code, out } = await spawn('node', [script, path.join(appPath, 'Contents/MacOS/Electron')]);
|
|
|
|
expect(code).to.equal(0);
|
|
|
|
expect(out).to.include(nodeOptionsWarning);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is disabled when invoked by alien binary in app bundle in ELECTRON_RUN_AS_NODE mode', async function () {
|
|
|
|
await withTempDirectory(async (dir) => {
|
2024-05-13 17:48:26 +00:00
|
|
|
const appPath = await copyMacOSFixtureApp(dir);
|
2023-12-06 02:23:54 +00:00
|
|
|
await signApp(appPath, identity);
|
|
|
|
// Find system node and copy it to app bundle.
|
|
|
|
const nodePath = process.env.PATH?.split(path.delimiter).find(dir => fs.existsSync(path.join(dir, 'node')));
|
|
|
|
if (!nodePath) {
|
|
|
|
this.skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const alienBinary = path.join(appPath, 'Contents/MacOS/node');
|
2024-06-19 14:10:16 +00:00
|
|
|
await fs.promises.cp(path.join(nodePath, 'node'), alienBinary, { recursive: true });
|
2023-12-06 02:23:54 +00:00
|
|
|
// Try to execute electron app from the alien node in app bundle.
|
|
|
|
const { code, out } = await spawn(alienBinary, [script, path.join(appPath, 'Contents/MacOS/Electron')]);
|
|
|
|
expect(code).to.equal(0);
|
|
|
|
expect(out).to.include(nodeOptionsWarning);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is respected when invoked from self', async () => {
|
|
|
|
await withTempDirectory(async (dir) => {
|
2024-05-13 17:48:26 +00:00
|
|
|
const appPath = await copyMacOSFixtureApp(dir, null);
|
2023-12-06 02:23:54 +00:00
|
|
|
await signApp(appPath, identity);
|
|
|
|
const appExePath = path.join(appPath, 'Contents/MacOS/Electron');
|
|
|
|
const { code, out } = await spawn(appExePath, [script, appExePath]);
|
|
|
|
expect(code).to.equal(1);
|
|
|
|
expect(out).to.not.include(nodeOptionsWarning);
|
|
|
|
expect(out).to.include('NODE_OPTIONS passed to child');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-08 20:40:08 +00:00
|
|
|
describe('Node.js cli flags', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let child: childProcess.ChildProcessWithoutNullStreams;
|
|
|
|
let exitPromise: Promise<any[]>;
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2021-03-16 21:02:47 +00:00
|
|
|
it('Prohibits crypto-related flags in ELECTRON_RUN_AS_NODE mode', (done) => {
|
2020-02-07 02:59:38 +00:00
|
|
|
after(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [code, signal] = await exitPromise;
|
|
|
|
expect(signal).to.equal(null);
|
|
|
|
expect(code).to.equal(9);
|
|
|
|
child.kill();
|
|
|
|
});
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
child = childProcess.spawn(process.execPath, ['--force-fips'], {
|
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2023-02-23 23:53:53 +00:00
|
|
|
exitPromise = once(child, 'exit');
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.removeListener('data', listener);
|
|
|
|
child.stdout.removeListener('data', listener);
|
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
const listener = (data: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
output += data;
|
2020-02-07 02:59:38 +00:00
|
|
|
if (/.*The Node.js cli flag --force-fips is not supported in Electron/m.test(output)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
cleanup();
|
|
|
|
done();
|
2020-02-07 02:59:38 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
|
|
|
});
|
|
|
|
});
|
2019-11-02 01:06:15 +00:00
|
|
|
|
2020-10-05 17:10:38 +00:00
|
|
|
describe('process.stdout', () => {
|
|
|
|
it('is a real Node stream', () => {
|
|
|
|
expect((process.stdout as any)._type).to.not.be.undefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-07 19:19:39 +00:00
|
|
|
describe('fs.readFile', () => {
|
|
|
|
it('can accept a FileHandle as the Path argument', async () => {
|
|
|
|
const filePathForHandle = path.resolve(mainFixturesPath, 'dogs-running.txt');
|
|
|
|
const fileHandle = await fs.promises.open(filePathForHandle, 'r');
|
|
|
|
|
|
|
|
const file = await fs.promises.readFile(fileHandle, { encoding: 'utf8' });
|
|
|
|
expect(file).to.not.be.empty();
|
|
|
|
await fileHandle.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-08 20:40:08 +00:00
|
|
|
describe('inspector', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let child: childProcess.ChildProcessWithoutNullStreams;
|
2022-01-31 11:37:40 +00:00
|
|
|
let exitPromise: Promise<any[]> | null;
|
2019-09-20 14:41:40 +00:00
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
if (child && exitPromise) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [code, signal] = await exitPromise;
|
|
|
|
expect(signal).to.equal(null);
|
|
|
|
expect(code).to.equal(0);
|
2019-09-20 14:41:40 +00:00
|
|
|
} else if (child) {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.kill();
|
2019-09-20 14:41:40 +00:00
|
|
|
}
|
2020-05-19 02:05:49 +00:00
|
|
|
child = null as any;
|
|
|
|
exitPromise = null as any;
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
it('Supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--inspect-brk', path.join(fixtures, 'module', 'run-as-node.js')], {
|
2020-02-07 02:59:38 +00:00
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.removeListener('data', listener);
|
|
|
|
child.stdout.removeListener('data', listener);
|
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
|
|
|
const listener = (data: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
output += data;
|
2020-02-07 02:59:38 +00:00
|
|
|
if (/Debugger listening on ws:/m.test(output)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
cleanup();
|
|
|
|
done();
|
2019-09-20 14:41:40 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('Supports starting the v8 inspector with --inspect and a provided port', async () => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--inspect=17364', path.join(fixtures, 'module', 'run-as-node.js')], {
|
2020-02-07 02:59:38 +00:00
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2023-02-23 23:53:53 +00:00
|
|
|
exitPromise = once(child, 'exit');
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
|
|
|
const listener = (data: Buffer) => { output += data; };
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.removeListener('data', listener);
|
|
|
|
child.stdout.removeListener('data', listener);
|
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(child, 'exit');
|
2020-06-30 22:10:36 +00:00
|
|
|
cleanup();
|
|
|
|
if (/^Debugger listening on ws:/m.test(output)) {
|
|
|
|
expect(output.trim()).to.contain(':17364', 'should be listening on port 17364');
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unexpected output: ${output.toString()}`);
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('Does not start the v8 inspector when --inspect is after a -- argument', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'noop.js'), '--', '--inspect']);
|
2023-02-23 23:53:53 +00:00
|
|
|
exitPromise = once(child, 'exit');
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
|
|
|
const listener = (data: Buffer) => { output += data; };
|
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
2023-02-23 23:53:53 +00:00
|
|
|
await once(child, 'exit');
|
2020-06-30 22:10:36 +00:00
|
|
|
if (output.trim().startsWith('Debugger listening on ws://')) {
|
|
|
|
throw new Error('Inspector was started when it should not have been');
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2021-03-16 21:02:47 +00:00
|
|
|
// IPC Electron child process not supported on Windows.
|
|
|
|
ifit(process.platform !== 'win32')('does not crash when quitting with the inspector connected', function (done) {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'delay-exit'), '--inspect=0'], {
|
|
|
|
stdio: ['ipc']
|
2020-03-20 20:28:31 +00:00
|
|
|
}) as childProcess.ChildProcessWithoutNullStreams;
|
2023-02-23 23:53:53 +00:00
|
|
|
exitPromise = once(child, 'exit');
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-02-07 02:59:38 +00:00
|
|
|
const cleanup = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.removeListener('data', listener);
|
|
|
|
child.stdout.removeListener('data', listener);
|
|
|
|
};
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let output = '';
|
2020-05-19 02:05:49 +00:00
|
|
|
const success = false;
|
2020-02-07 02:59:38 +00:00
|
|
|
function listener (data: Buffer) {
|
2020-03-20 20:28:31 +00:00
|
|
|
output += data;
|
2020-05-26 01:20:16 +00:00
|
|
|
console.log(data.toString()); // NOTE: temporary debug logging to try to catch flake.
|
2020-05-19 02:05:49 +00:00
|
|
|
const match = /^Debugger listening on (ws:\/\/.+:\d+\/.+)\n/m.exec(output.trim());
|
|
|
|
if (match) {
|
|
|
|
cleanup();
|
2020-05-26 01:20:16 +00:00
|
|
|
// NOTE: temporary debug logging to try to catch flake.
|
|
|
|
child.stderr.on('data', (m) => console.log(m.toString()));
|
|
|
|
child.stdout.on('data', (m) => console.log(m.toString()));
|
2023-02-16 14:41:41 +00:00
|
|
|
const w = (webContents as typeof ElectronInternal.WebContents).create();
|
2020-05-19 02:05:49 +00:00
|
|
|
w.loadURL('about:blank')
|
|
|
|
.then(() => w.executeJavaScript(`new Promise(resolve => {
|
|
|
|
const connection = new WebSocket(${JSON.stringify(match[1])})
|
|
|
|
connection.onopen = () => {
|
2020-05-26 14:47:49 +00:00
|
|
|
connection.onclose = () => resolve()
|
2020-05-19 02:05:49 +00:00
|
|
|
connection.close()
|
|
|
|
}
|
|
|
|
})`))
|
|
|
|
.then(() => {
|
2022-12-14 21:07:38 +00:00
|
|
|
w.destroy();
|
2020-05-19 02:05:49 +00:00
|
|
|
child.send('plz-quit');
|
|
|
|
done();
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-07 02:59:38 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
child.stderr.on('data', listener);
|
|
|
|
child.stdout.on('data', listener);
|
2020-02-07 02:59:38 +00:00
|
|
|
child.on('exit', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!success) cleanup();
|
|
|
|
});
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('Supports js binding', async () => {
|
2019-09-20 14:41:40 +00:00
|
|
|
child = childProcess.spawn(process.execPath, ['--inspect', path.join(fixtures, 'module', 'inspector-binding.js')], {
|
2020-02-07 02:59:38 +00:00
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' },
|
2019-09-20 14:41:40 +00:00
|
|
|
stdio: ['ipc']
|
2020-03-20 20:28:31 +00:00
|
|
|
}) as childProcess.ChildProcessWithoutNullStreams;
|
2023-02-23 23:53:53 +00:00
|
|
|
exitPromise = once(child, 'exit');
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const [{ cmd, debuggerEnabled, success }] = await once(child, 'message');
|
2020-06-30 22:10:36 +00:00
|
|
|
expect(cmd).to.equal('assert');
|
|
|
|
expect(debuggerEnabled).to.be.true();
|
|
|
|
expect(success).to.be.true();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-20 14:41:40 +00:00
|
|
|
|
2021-04-09 07:09:17 +00:00
|
|
|
it('Can find a module using a package.json main field', () => {
|
2021-09-09 21:49:01 +00:00
|
|
|
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')], { stdio: 'inherit' });
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(result.status).to.equal(0);
|
|
|
|
});
|
2020-04-21 19:18:22 +00:00
|
|
|
|
2023-06-08 20:40:08 +00:00
|
|
|
it('handles Promise timeouts correctly', async () => {
|
2020-04-21 19:18:22 +00:00
|
|
|
const scriptPath = path.join(fixtures, 'module', 'node-promise-timer.js');
|
|
|
|
const child = childProcess.spawn(process.execPath, [scriptPath], {
|
|
|
|
env: { ELECTRON_RUN_AS_NODE: 'true' }
|
|
|
|
});
|
2023-02-23 23:53:53 +00:00
|
|
|
const [code, signal] = await once(child, 'exit');
|
2020-06-30 22:10:36 +00:00
|
|
|
expect(code).to.equal(0);
|
|
|
|
expect(signal).to.equal(null);
|
|
|
|
child.kill();
|
2020-04-21 19:18:22 +00:00
|
|
|
});
|
2020-06-17 17:08:10 +00:00
|
|
|
|
|
|
|
it('performs microtask checkpoint correctly', (done) => {
|
2023-12-05 03:56:51 +00:00
|
|
|
let timer : NodeJS.Timeout;
|
|
|
|
const listener = () => {
|
|
|
|
done(new Error('catch block is delayed to next tick'));
|
|
|
|
};
|
|
|
|
|
2020-06-17 17:08:10 +00:00
|
|
|
const f3 = async () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-12-05 03:56:51 +00:00
|
|
|
timer = setTimeout(listener);
|
2020-06-17 17:08:10 +00:00
|
|
|
reject(new Error('oops'));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2023-12-05 03:56:51 +00:00
|
|
|
f3().catch(() => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
done();
|
|
|
|
});
|
2020-06-17 17:08:10 +00:00
|
|
|
});
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|