From 0b7e7458c94db2f0d7a4a8a35e61db2d8e990d6a Mon Sep 17 00:00:00 2001 From: Hari Krishna Reddy Juturu Date: Fri, 16 Jun 2017 15:34:11 -0700 Subject: [PATCH] WIP: Adding UT --- spec/api-browser-window-spec.js | 52 +++++++++++++++++++ .../electron-app-mixed-sandbox-preload.js | 3 ++ spec/fixtures/api/mixed-sandbox-app/main.js | 52 +++++++++++++++++++ .../api/mixed-sandbox-app/package.json | 5 ++ 4 files changed, 112 insertions(+) create mode 100644 spec/fixtures/api/mixed-sandbox-app/electron-app-mixed-sandbox-preload.js create mode 100644 spec/fixtures/api/mixed-sandbox-app/main.js create mode 100644 spec/fixtures/api/mixed-sandbox-app/package.json diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 5c084cb178fd..322f25ca32e0 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1,11 +1,13 @@ 'use strict' const assert = require('assert') +const ChildProcess = require('child_process') const fs = require('fs') const path = require('path') const os = require('os') const qs = require('querystring') const http = require('http') +const net = require('net') const {closeWindow} = require('./window-helpers') const {ipcRenderer, remote, screen} = require('electron') @@ -1259,6 +1261,56 @@ 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' + + beforeEach(function (done) { + fs.unlink(socketPath, () => { + sandboxServer = net.createServer() + sandboxServer.listen(socketPath) + done() + }) + }) + + afterEach(function (done) { + sandboxServer.close(() => { + if (process.platform === 'win32') { + done() + } else { + fs.unlink(socketPath, () => { + done() + }) + } + }) + }) + + it('enable-mixed-sandbox', (done) => { + this.timeout(120000) + + let state = 'none' + sandboxServer.once('error', (error) => { + done(error) + }) + sandboxServer.on('connection', (client) => { + client.once('data', function (data) { + console.log('jhreddy -' + data) + if (String(data) === 'false' && state === 'none') { + state = 'first-launch' + } else if (String(data) === 'true' && state === 'first-launch') { + done() + } else { + done(`Unexpected state: ${state}`) + } + }) + }) + + const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') + ChildProcess.spawn(remote.process.execPath, [appPath]) + }) + }) + describe('nativeWindowOpen option', () => { beforeEach(() => { w.destroy() 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 new file mode 100644 index 000000000000..5a781c9b70e4 --- /dev/null +++ b/spec/fixtures/api/mixed-sandbox-app/electron-app-mixed-sandbox-preload.js @@ -0,0 +1,3 @@ +process.once('loaded', function () { + window.argv = process.argv +}) \ No newline at end of file diff --git a/spec/fixtures/api/mixed-sandbox-app/main.js b/spec/fixtures/api/mixed-sandbox-app/main.js new file mode 100644 index 000000000000..179c0813f327 --- /dev/null +++ b/spec/fixtures/api/mixed-sandbox-app/main.js @@ -0,0 +1,52 @@ +const { app, BrowserWindow, dialog } = require('electron') +const net = require('net') +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') { + dialog.showErrorBox('connected', app.getAppPath()) + let window = new BrowserWindow({ + show: true, + webPreferences: { + preload: path.join(app.getAppPath(), 'electron-app-mixed-sandbox-preload.js'), + sandbox: false + } + }) + + window.loadURL('data:,window') + let argv = 'test' + window.webContents.once('did-finish-load', () => { + dialog.showErrorBox('finished-load', 'finished') + window.webContents.executeJavaScript('window.argv', false, (result) => { + dialog.showErrorBox('hello', result) + client.end(String(result)) + }) + }) + // window.webContents.openDevTools() + // window.webContents.executeJavaScript('window.argv', true).then((result) => { + // dialog.showErrorBox('hello', result) + // client.end(String(argv)) + // }) + + } else { + client.end(String(false)) + } + }) + client.once('end', () => { + app.exit(0) + }) + + if (lastArg !== '--enable-mixed-sandbox') { + app.relaunch({ args: process.argv.slice(1).concat('--enable-mixed-sandbox') }) + } +}) diff --git a/spec/fixtures/api/mixed-sandbox-app/package.json b/spec/fixtures/api/mixed-sandbox-app/package.json new file mode 100644 index 000000000000..c5b70811e60c --- /dev/null +++ b/spec/fixtures/api/mixed-sandbox-app/package.json @@ -0,0 +1,5 @@ +{ + "name": "electron-app-mixed-sandbox", + "main": "main.js" +} +