2016-03-25 23:03:49 +03:00
|
|
|
const assert = require('assert')
|
2016-06-29 19:37:10 +03:00
|
|
|
const ChildProcess = require('child_process')
|
2016-03-25 23:03:49 +03:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const os = require('os')
|
2017-01-25 19:55:38 +03:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2016-06-20 05:16:17 +03:00
|
|
|
const isCI = remote.getGlobal('isCi')
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('node feature', () => {
|
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
|
|
|
|
|
|
|
describe('child_process', () => {
|
|
|
|
describe('child_process.fork', () => {
|
|
|
|
it('works in current process', (done) => {
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'ping.js'))
|
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(msg, 'message')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('preserves args', (done) => {
|
|
|
|
const args = ['--expose_gc', '-test', '1']
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'process_args.js'), args)
|
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.deepEqual(args, msg.slice(2))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('works in forked process', (done) => {
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'))
|
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(msg, 'message')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('works in forked process when options.env is specifed', (done) => {
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'), [], {
|
2016-01-12 05:40:23 +03:00
|
|
|
path: process.env['PATH']
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
2017-10-27 23:45:58 +03:00
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(msg, 'message')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('works in browser process', (done) => {
|
|
|
|
const fork = remote.require('child_process').fork
|
|
|
|
const child = fork(path.join(fixtures, 'module', 'ping.js'))
|
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(msg, 'message')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('has String::localeCompare working in script', (done) => {
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'locale-compare.js'))
|
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.deepEqual(msg, [0, -1, 1])
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('has setImmediate working in script', (done) => {
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'set-immediate.js'))
|
|
|
|
child.on('message', (msg) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(msg, 'ok')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
2016-05-31 04:26:19 +03:00
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('pipes stdio', (done) => {
|
|
|
|
const child = ChildProcess.fork(path.join(fixtures, 'module', 'process-stdout.js'), {silent: true})
|
2016-05-31 04:26:19 +03:00
|
|
|
let data = ''
|
|
|
|
child.stdout.on('data', (chunk) => {
|
|
|
|
data += String(chunk)
|
|
|
|
})
|
2017-04-20 21:40:04 +03:00
|
|
|
child.on('close', (code) => {
|
2016-05-31 04:26:19 +03:00
|
|
|
assert.equal(code, 0)
|
|
|
|
assert.equal(data, 'pipes stdio')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-04-06 19:47:33 +03:00
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('works when sending a message to a process forked with the --eval argument', (done) => {
|
2017-04-06 19:52:52 +03:00
|
|
|
const source = "process.on('message', (message) => { process.send(message) })"
|
2017-04-06 19:47:33 +03:00
|
|
|
const forked = ChildProcess.fork('--eval', [source])
|
2017-04-06 19:52:52 +03:00
|
|
|
forked.once('message', (message) => {
|
2017-04-06 19:47:33 +03:00
|
|
|
assert.equal(message, 'hello')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
forked.send('hello')
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
2016-10-12 23:01:43 +03:00
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('child_process.spawn', () => {
|
2017-06-13 00:04:57 +03:00
|
|
|
let child
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
afterEach(() => {
|
|
|
|
if (child != null) child.kill()
|
2017-06-13 00:04:57 +03:00
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('supports spawning Electron as a node process via the ELECTRON_RUN_AS_NODE env var', (done) => {
|
2017-06-13 00:04:57 +03:00
|
|
|
child = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
|
2016-10-12 23:01:43 +03:00
|
|
|
env: {
|
|
|
|
ELECTRON_RUN_AS_NODE: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
let output = ''
|
2017-10-27 23:45:58 +03:00
|
|
|
child.stdout.on('data', (data) => {
|
2016-10-12 23:01:43 +03:00
|
|
|
output += data
|
|
|
|
})
|
2017-10-27 23:45:58 +03:00
|
|
|
child.stdout.on('close', () => {
|
2016-10-12 23:01:43 +03:00
|
|
|
assert.deepEqual(JSON.parse(output), {
|
|
|
|
processLog: process.platform === 'win32' ? 'function' : 'undefined',
|
|
|
|
processType: 'undefined',
|
|
|
|
window: 'undefined'
|
|
|
|
})
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('contexts', () => {
|
|
|
|
describe('setTimeout in fs callback', () => {
|
|
|
|
it('does not crash', (done) => {
|
|
|
|
fs.readFile(__filename, () => {
|
2016-03-25 23:03:49 +03:00
|
|
|
setTimeout(done, 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('error thrown in renderer process node context', () => {
|
|
|
|
it('gets emitted as a process uncaughtException event', (done) => {
|
2017-01-25 19:55:38 +03:00
|
|
|
const error = new Error('boo!')
|
|
|
|
const listeners = process.listeners('uncaughtException')
|
2016-03-25 23:03:49 +03:00
|
|
|
process.removeAllListeners('uncaughtException')
|
2017-01-25 19:55:38 +03:00
|
|
|
process.on('uncaughtException', (thrown) => {
|
|
|
|
assert.strictEqual(thrown, error)
|
2016-03-25 23:03:49 +03:00
|
|
|
process.removeAllListeners('uncaughtException')
|
2017-01-25 19:55:38 +03:00
|
|
|
listeners.forEach((listener) => {
|
|
|
|
process.on('uncaughtException', listener)
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
done()
|
|
|
|
})
|
2017-01-25 19:55:38 +03:00
|
|
|
fs.readFile(__filename, () => {
|
2016-03-25 23:03:49 +03:00
|
|
|
throw error
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('error thrown in main process node context', () => {
|
|
|
|
it('gets emitted as a process uncaughtException event', () => {
|
2017-01-25 19:55:38 +03:00
|
|
|
const error = ipcRenderer.sendSync('handle-uncaught-exception', 'hello')
|
|
|
|
assert.equal(error, 'hello')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('promise rejection in main process node context', () => {
|
|
|
|
it('gets emitted as a process unhandledRejection event', () => {
|
2017-01-25 20:04:25 +03:00
|
|
|
const error = ipcRenderer.sendSync('handle-unhandled-rejection', 'hello')
|
|
|
|
assert.equal(error, 'hello')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('setTimeout called under Chromium event loop in browser process', () => {
|
|
|
|
it('can be scheduled in time', (done) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
remote.getGlobal('setTimeout')(done, 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('setInterval called under Chromium event loop in browser process', () => {
|
|
|
|
it('can be scheduled in time', (done) => {
|
2018-03-07 08:40:27 +03:00
|
|
|
let interval = null
|
2018-03-14 08:51:47 +03:00
|
|
|
let clearing = false
|
2018-03-07 08:40:27 +03:00
|
|
|
const clear = () => {
|
2018-03-14 08:51:47 +03:00
|
|
|
if (interval === null || clearing) {
|
2018-03-07 08:40:27 +03:00
|
|
|
return
|
|
|
|
}
|
2018-03-14 08:51:47 +03:00
|
|
|
// interval might trigger while clearing (remote is slow sometimes)
|
|
|
|
clearing = true
|
2016-03-25 23:03:49 +03:00
|
|
|
remote.getGlobal('clearInterval')(interval)
|
2018-03-14 08:51:47 +03:00
|
|
|
clearing = false
|
2018-03-07 08:40:27 +03:00
|
|
|
interval = null
|
2016-03-25 23:03:49 +03:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
interval = remote.getGlobal('setInterval')(clear, 10)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-06 17:25:48 +03:00
|
|
|
describe('inspector', () => {
|
|
|
|
let child
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (child != null) child.kill()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
|
|
|
|
child = ChildProcess.spawn(process.execPath, ['--inspect-brk', path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
|
|
|
|
env: {
|
|
|
|
ELECTRON_RUN_AS_NODE: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
let output = ''
|
|
|
|
child.stderr.on('data', (data) => {
|
|
|
|
output += data
|
|
|
|
if (output.trim().startsWith('Debugger listening on ws://')) done()
|
|
|
|
})
|
|
|
|
|
|
|
|
child.stdout.on('data', (data) => {
|
|
|
|
done(new Error(`Unexpected output: ${data.toString()}`))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports js binding', (done) => {
|
|
|
|
child = ChildProcess.spawn(process.execPath, ['--inspect', path.join(__dirname, 'fixtures', 'module', 'inspector-binding.js')], {
|
|
|
|
env: {
|
|
|
|
ELECTRON_RUN_AS_NODE: true
|
|
|
|
},
|
|
|
|
stdio: ['ipc']
|
|
|
|
})
|
|
|
|
|
|
|
|
child.on('message', ({cmd, debuggerEnabled, secondSessionOpened, success}) => {
|
|
|
|
if (cmd === 'assert') {
|
|
|
|
assert.equal(debuggerEnabled, true)
|
|
|
|
assert.equal(secondSessionOpened, false)
|
|
|
|
assert.equal(success, true)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('message loop', () => {
|
|
|
|
describe('process.nextTick', () => {
|
|
|
|
it('emits the callback', (done) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
process.nextTick(done)
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('works in nested calls', (done) => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
process.nextTick(() => {
|
2016-03-25 23:03:49 +03:00
|
|
|
process.nextTick(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('setImmediate', () => {
|
|
|
|
it('emits the callback', (done) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
setImmediate(done)
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('works in nested calls', (done) => {
|
|
|
|
setImmediate(() => {
|
|
|
|
setImmediate(() => {
|
2016-03-25 23:03:49 +03:00
|
|
|
setImmediate(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('net.connect', () => {
|
2017-11-16 00:05:46 +03:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('emit error when connect to a socket path without listeners', (done) => {
|
|
|
|
const socketPath = path.join(os.tmpdir(), 'atom-shell-test.sock')
|
|
|
|
const script = path.join(fixtures, 'module', 'create_socket.js')
|
|
|
|
const child = ChildProcess.fork(script, [socketPath])
|
|
|
|
child.on('exit', (code) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(code, 0)
|
2017-10-27 23:45:58 +03:00
|
|
|
const client = require('net').connect(socketPath)
|
|
|
|
client.on('error', (error) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(error.code, 'ECONNREFUSED')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('Buffer', () => {
|
|
|
|
it('can be created from WebKit external string', () => {
|
|
|
|
const p = document.createElement('p')
|
2016-03-25 23:03:49 +03:00
|
|
|
p.innerText = '闲云潭影日悠悠,物换星移几度秋'
|
2017-11-24 01:22:31 +03:00
|
|
|
const b = Buffer.from(p.innerText)
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(b.toString(), '闲云潭影日悠悠,物换星移几度秋')
|
|
|
|
assert.equal(Buffer.byteLength(p.innerText), 45)
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('correctly parses external one-byte UTF8 string', () => {
|
|
|
|
const p = document.createElement('p')
|
2016-03-25 23:03:49 +03:00
|
|
|
p.innerText = 'Jøhänñéß'
|
2017-11-24 01:22:31 +03:00
|
|
|
const b = Buffer.from(p.innerText)
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(b.toString(), 'Jøhänñéß')
|
|
|
|
assert.equal(Buffer.byteLength(p.innerText), 13)
|
|
|
|
})
|
2016-04-05 11:08:27 +03:00
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('does not crash when creating large Buffers', () => {
|
2017-11-24 01:22:31 +03:00
|
|
|
let buffer = Buffer.from(new Array(4096).join(' '))
|
2016-06-29 19:44:38 +03:00
|
|
|
assert.equal(buffer.length, 4095)
|
2017-11-24 01:22:31 +03:00
|
|
|
buffer = Buffer.from(new Array(4097).join(' '))
|
2016-06-29 19:44:38 +03:00
|
|
|
assert.equal(buffer.length, 4096)
|
2016-04-05 11:08:27 +03:00
|
|
|
})
|
2017-12-27 14:14:41 +03:00
|
|
|
|
|
|
|
it('does not crash for crypto operations', () => {
|
|
|
|
const crypto = require('crypto')
|
|
|
|
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) {
|
|
|
|
let iv = Buffer.from('0'.repeat(32), 'hex')
|
|
|
|
let input = Buffer.from(data, 'base64')
|
|
|
|
let decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'base64'), iv)
|
|
|
|
let result = Buffer.concat([decipher.update(input), decipher.final()])
|
|
|
|
assert.equal(cipherText, result)
|
|
|
|
}
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('process.stdout', () => {
|
|
|
|
it('does not throw an exception when accessed', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2017-11-24 01:22:43 +03:00
|
|
|
// eslint-disable-next-line
|
2016-09-08 00:40:18 +03:00
|
|
|
process.stdout
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('does not throw an exception when calling write()', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-09-08 00:40:18 +03:00
|
|
|
process.stdout.write('test')
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
2017-11-16 00:05:46 +03:00
|
|
|
it('should have isTTY defined on Mac and Linux', function () {
|
|
|
|
if (isCI || process.platform === 'win32') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test.
|
|
|
|
// this.skip()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(typeof process.stdout.isTTY, 'boolean')
|
|
|
|
})
|
2016-06-20 05:16:17 +03:00
|
|
|
|
2017-11-16 00:05:46 +03:00
|
|
|
it('should have isTTY undefined on Windows', function () {
|
|
|
|
if (isCI || process.platform !== 'win32') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test.
|
|
|
|
// this.skip()
|
|
|
|
return
|
2016-09-08 23:12:00 +03:00
|
|
|
}
|
2017-11-16 00:05:46 +03:00
|
|
|
|
|
|
|
assert.equal(process.stdout.isTTY, undefined)
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('process.stdin', () => {
|
|
|
|
it('does not throw an exception when accessed', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2017-11-24 01:22:43 +03:00
|
|
|
process.stdin // eslint-disable-line
|
2016-09-08 00:40:18 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
it('returns null when read from', () => {
|
2016-09-08 00:51:05 +03:00
|
|
|
assert.equal(process.stdin.read(), null)
|
2016-09-08 00:40:18 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('process.version', () => {
|
|
|
|
it('should not have -pre', () => {
|
2016-04-05 13:53:07 +03:00
|
|
|
assert(!process.version.endsWith('-pre'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 23:45:58 +03:00
|
|
|
describe('vm.createContext', () => {
|
|
|
|
it('should not crash', () => {
|
2016-03-25 23:03:49 +03:00
|
|
|
require('vm').runInNewContext('')
|
|
|
|
})
|
|
|
|
})
|
2017-01-12 04:51:46 +03:00
|
|
|
|
|
|
|
it('includes the electron version in process.versions', () => {
|
2017-11-13 22:22:34 +03:00
|
|
|
assert(/^\d+\.\d+\.\d+(\S*)?$/.test(process.versions.electron))
|
2017-01-12 04:51:46 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('includes the chrome version in process.versions', () => {
|
|
|
|
assert(/^\d+\.\d+\.\d+\.\d+$/.test(process.versions.chrome))
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|