diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index ab40331225a3..c726a1b8b0ef 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1262,51 +1262,28 @@ describe('BrowserWindow module', function () { }) describe('mixed sandbox option', function () { - // TOOD (juturu): This test needs to be fixed - let sandboxServer = null - const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-mixed-sandbox' : '/tmp/electron-app-mixed-sandbox' + this.timeout(120000) - beforeEach(function (done) { - fs.unlink(socketPath, () => { - sandboxServer = net.createServer() - sandboxServer.listen(socketPath) - done() - }) - }) + let appProcess - afterEach(function (done) { - sandboxServer.close(() => { - if (process.platform === 'win32') { - done() - } else { - fs.unlink(socketPath, () => { - done() - }) - } - }) + afterEach(function () { + if (appProcess != null) { + appProcess.kill() + } }) it('adds --enable-sandbox to render processes created with sandbox: true', (done) => { - this.timeout(120000) - - let state = 'none' - sandboxServer.once('error', (error) => { - done(error) - }) - sandboxServer.on('connection', (client) => { - client.once('data', function (data) { - if (String(data) === 'false' && state === 'none') { - state = 'first-launch' - } else if (String(data) === 'true' && state === 'first-launch') { - done() - } else { - done(`Unexpected state: ${data}`) - } - }) - }) - const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') - ChildProcess.spawn(remote.process.execPath, [appPath]) + ©appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'], {stdio: ['ignore', 'ipc', 'ignore']}) + appProcess.once('message', (argv) => { + assert.equal(argv.sandbox.includes('--enable-sandbox'), true) + assert.equal(argv.sandbox.includes('--no-sandbox'), false) + + assert.equal(argv.noSandbox.includes('--enable-sandbox'), false) + assert.equal(argv.noSandbox.includes('--no-sandbox'), true) + + done() + }) }) }) diff --git a/spec/fixtures/api/mixed-sandbox-app/electron-app-mixed-sandbox-preload.js b/spec/fixtures/api/mixed-sandbox-app/electron-app-mixed-sandbox-preload.js index 3efe83276cb3..abe0eeea87ed 100644 --- a/spec/fixtures/api/mixed-sandbox-app/electron-app-mixed-sandbox-preload.js +++ b/spec/fixtures/api/mixed-sandbox-app/electron-app-mixed-sandbox-preload.js @@ -1,8 +1 @@ -const { ipcRenderer } = require('electron') -if (process) { - process.once('loaded', function () { - ipcRenderer.send('processArgs', process.argv) - }) -} else { - ipcRenderer.send('processArgs', 'unable to get process args') -} +require('electron').ipcRenderer.send('argv', process.argv) diff --git a/spec/fixtures/api/mixed-sandbox-app/main.js b/spec/fixtures/api/mixed-sandbox-app/main.js index fe8c2e45f3e5..7823208b72e2 100644 --- a/spec/fixtures/api/mixed-sandbox-app/main.js +++ b/spec/fixtures/api/mixed-sandbox-app/main.js @@ -1,39 +1,46 @@ -const { app, BrowserWindow, ipcMain } = require('electron') -const net = require('net') +const {app, BrowserWindow, ipcMain} = require('electron') const path = require('path') -const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-mixed-sandbox' : '/tmp/electron-app-mixed-sandbox' - process.on('uncaughtException', () => { app.exit(1) }) -app.once('ready', () => { - let lastArg = process.argv[process.argv.length - 1] - const client = net.connect(socketPath) - client.once('connect', () => { - if (lastArg === '--enable-mixed-sandbox') { - ipcMain.on('processArgs', (event, args) => { - client.end(String(args.indexOf('--no-sandbox') >= 0)) - }) - let window = new BrowserWindow({ - show: false, - webPreferences: { - preload: path.join(app.getAppPath(), 'electron-app-mixed-sandbox-preload.js'), - sandbox: false - } - }) +let sandboxWindow +let noSandboxWindow - window.loadURL('data:,window') - } else { - client.end(String(false)) +app.once('ready', () => { + sandboxWindow = new BrowserWindow({ + show: false, + webPreferences: { + preload: path.join(app.getAppPath(), 'electron-app-mixed-sandbox-preload.js'), + sandbox: true } }) - client.once('end', () => { - app.exit(0) - }) + sandboxWindow.loadURL('about:blank') - if (lastArg !== '--enable-mixed-sandbox') { - app.relaunch({ args: process.argv.slice(1).concat('--enable-mixed-sandbox') }) + noSandboxWindow = new BrowserWindow({ + show: false, + webPreferences: { + preload: path.join(app.getAppPath(), 'electron-app-mixed-sandbox-preload.js'), + sandbox: false + } + }) + noSandboxWindow.loadURL('about:blank') + + const argv = { + sandbox: null, + noSandbox: null } + + ipcMain.on('argv', (event, value) => { + if (event.sender === sandboxWindow.webContents) { + argv.sandbox = value + } else if (event.sender === noSandboxWindow.webContents) { + argv.noSandbox = value + } + + if (argv.sandbox != null && argv.noSandbox != null) { + process.send(argv) + } + }) })