test: tsify sub-frames spec (#19965)

This commit is contained in:
Jeremy Apthorp 2019-08-28 13:55:01 -07:00 committed by GitHub
parent 99de0975c3
commit f212ed85dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 45 deletions

View file

@ -1,18 +1,17 @@
const { expect } = require('chai') import { expect } from 'chai'
const { remote } = require('electron') import * as path from 'path'
const path = require('path') import * as http from 'http'
const http = require('http') import { emittedNTimes, emittedOnce } from './events-helpers'
import { closeWindow } from './window-helpers'
const { emittedNTimes, emittedOnce } = require('./events-helpers') import { app, BrowserWindow, ipcMain } from 'electron'
const { closeWindow } = require('./window-helpers') import { AddressInfo } from 'net'
import { ifdescribe } from './spec-helpers';
const { app, BrowserWindow, ipcMain } = remote
describe('renderer nodeIntegrationInSubFrames', () => { describe('renderer nodeIntegrationInSubFrames', () => {
const generateTests = (description, webPreferences) => { const generateTests = (description: string, webPreferences: any) => {
describe(description, () => { describe(description, () => {
const fixtureSuffix = webPreferences.webviewTag ? '-webview' : '' const fixtureSuffix = webPreferences.webviewTag ? '-webview' : ''
let w let w: BrowserWindow
beforeEach(async () => { beforeEach(async () => {
await closeWindow(w) await closeWindow(w)
@ -24,10 +23,9 @@ describe('renderer nodeIntegrationInSubFrames', () => {
}) })
}) })
afterEach(() => { afterEach(async () => {
return closeWindow(w).then(() => { await closeWindow(w)
w = null w = null as unknown as BrowserWindow
})
}) })
it('should load preload scripts in top level iframes', async () => { it('should load preload scripts in top level iframes', async () => {
@ -103,8 +101,8 @@ describe('renderer nodeIntegrationInSubFrames', () => {
}) })
} }
const generateConfigs = (webPreferences, ...permutations) => { const generateConfigs = (webPreferences: any, ...permutations: {name: string, webPreferences: any}[]) => {
const configs = [{ webPreferences, names: [] }] const configs = [{ webPreferences, names: [] as string[] }]
for (let i = 0; i < permutations.length; i++) { for (let i = 0; i < permutations.length; i++) {
const length = configs.length const length = configs.length
for (let j = 0; j < length; j++) { for (let j = 0; j < length; j++) {
@ -117,7 +115,7 @@ describe('renderer nodeIntegrationInSubFrames', () => {
} }
} }
return configs.map(config => { return configs.map((config: any) => {
if (config.names.length > 0) { if (config.names.length > 0) {
config.title = `with ${config.names.join(', ')} on` config.title = `with ${config.names.join(', ')} on`
} else { } else {
@ -125,7 +123,7 @@ describe('renderer nodeIntegrationInSubFrames', () => {
} }
delete config.names delete config.names
return config return config as {title: string, webPreferences: any}
}) })
} }
@ -151,7 +149,7 @@ describe('renderer nodeIntegrationInSubFrames', () => {
}) })
describe('internal <iframe> inside of <webview>', () => { describe('internal <iframe> inside of <webview>', () => {
let w let w: BrowserWindow
beforeEach(async () => { beforeEach(async () => {
await closeWindow(w) await closeWindow(w)
@ -167,10 +165,9 @@ describe('renderer nodeIntegrationInSubFrames', () => {
}) })
}) })
afterEach(() => { afterEach(async () => {
return closeWindow(w).then(() => { await closeWindow(w)
w = null w = null as unknown as BrowserWindow
})
}) })
it('should not load preload scripts', async () => { it('should not load preload scripts', async () => {
@ -184,40 +181,36 @@ describe('renderer nodeIntegrationInSubFrames', () => {
}) })
}) })
describe('cross-site frame sandboxing', () => { // app.getAppMetrics() does not return sandbox information on Linux.
let server = null ifdescribe(process.platform !== 'linux')('cross-site frame sandboxing', () => {
let server: http.Server
beforeEach(function () { let crossSiteUrl: string
if (process.platform === 'linux') { let serverUrl: string
this.skip()
}
})
before(function (done) { before(function (done) {
server = http.createServer((req, res) => { server = http.createServer((req, res) => {
res.end(`<iframe name="frame" src="${server.cross_site_url}" />`) res.end(`<iframe name="frame" src="${crossSiteUrl}" />`)
}) })
server.listen(0, '127.0.0.1', () => { server.listen(0, '127.0.0.1', () => {
server.url = `http://127.0.0.1:${server.address().port}/` serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`
server.cross_site_url = `http://localhost:${server.address().port}/` crossSiteUrl = `http://localhost:${(server.address() as AddressInfo).port}/`
done() done()
}) })
}) })
after(() => { after(() => {
server.close() server.close()
server = null server = null as unknown as http.Server
}) })
let w let w: BrowserWindow
afterEach(() => { afterEach(async () => {
return closeWindow(w).then(() => { await closeWindow(w)
w = null w = null as unknown as BrowserWindow
})
}) })
const generateSpecs = (description, webPreferences) => { const generateSpecs = (description: string, webPreferences: any) => {
describe(description, () => { describe(description, () => {
it('iframe process is sandboxed if possible', async () => { it('iframe process is sandboxed if possible', async () => {
w = new BrowserWindow({ w = new BrowserWindow({
@ -225,13 +218,13 @@ describe('cross-site frame sandboxing', () => {
webPreferences webPreferences
}) })
await w.loadURL(server.url) await w.loadURL(serverUrl)
const pidMain = w.webContents.getOSProcessId() const pidMain = w.webContents.getOSProcessId()
const pidFrame = w.webContents._getOSProcessIdForFrame('frame', server.cross_site_url) const pidFrame = (w.webContents as any)._getOSProcessIdForFrame('frame', crossSiteUrl)
const metrics = app.getAppMetrics() const metrics = app.getAppMetrics()
const isProcessSandboxed = function (pid) { const isProcessSandboxed = function (pid: number) {
const entry = metrics.filter(metric => metric.pid === pid)[0] const entry = metrics.filter(metric => metric.pid === pid)[0]
return entry && entry.sandboxed return entry && entry.sandboxed
} }