| 1 | import assert from "node:assert/strict"; |
| 2 | import { execFile } from "node:child_process"; |
| 3 | import { once } from "node:events"; |
| 4 | import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 5 | import http from "node:http"; |
| 6 | import net from "node:net"; |
| 7 | import { tmpdir } from "node:os"; |
| 8 | import path from "node:path"; |
| 9 | import { randomUUID } from "node:crypto"; |
| 10 | import { PassThrough } from "node:stream"; |
| 11 | import { createInterface } from "node:readline"; |
| 12 | import { promisify } from "node:util"; |
| 13 | import { fileURLToPath, pathToFileURL } from "node:url"; |
| 14 | import { setTimeout as delay } from "node:timers/promises"; |
| 15 | import test from "node:test"; |
| 16 | |
| 17 | test("packaged Mac phone controller uses isolated profile and public-web policy", { skip: !process.env.CAGE_TEST_CHROMIUM }, async t => { |
| 18 | const temporary = await mkdtemp(path.join(tmpdir(), "cage-mac-phone-controller-")); |
| 19 | t.after(() => rm(temporary, { recursive: true, force: true })); |
| 20 | const bundle = path.join(temporary, "controller"); |
| 21 | const script = new URL("../package-macos-browser.sh", import.meta.url); |
| 22 | await promisify(execFile)("bash", [fileURLToPath(script), bundle]); |
| 23 | const { runMacOSPhoneBrowser } = await import(pathToFileURL(path.join(bundle, "macos-phone-browser.mjs"))); |
| 24 | const site = http.createServer((_, response) => { |
| 25 | response.setHeader("Content-Type", "text/html"); |
| 26 | response.end('<meta name="viewport" content="width=device-width,initial-scale=1"><title>native phone fixture</title><input aria-label="message">'); |
| 27 | }); |
| 28 | site.listen(0, "127.0.0.1"); |
| 29 | await once(site, "listening"); |
| 30 | t.after(() => site.close()); |
| 31 | const input = new PassThrough(); |
| 32 | const output = new PassThrough(); |
| 33 | const replies = createInterface({ input: output })[Symbol.asyncIterator](); |
| 34 | const controller = runMacOSPhoneBrowser(input, output, { |
| 35 | testing: true, binary: process.env.CAGE_TEST_CHROMIUM, directory: path.join(temporary, "phone-profile"), |
| 36 | connect: async (name, port) => { |
| 37 | assert.equal(name, "phone.example"); |
| 38 | assert.equal(port, site.address().port); |
| 39 | const socket = net.connect({ host: "127.0.0.1", port }); |
| 40 | await once(socket, "connect"); |
| 41 | return socket; |
| 42 | }, |
| 43 | }); |
| 44 | t.after(async () => { input.end(); await controller; output.end(); }); |
| 45 | const id = randomUUID(); |
| 46 | const request = async (operation, values = {}) => { |
| 47 | const reply = replies.next(); |
| 48 | input.write(JSON.stringify({ method: "POST", path: "/phone-browser/" + operation, input: { id, ...values } }) + "\n"); |
| 49 | return JSON.parse((await reply).value); |
| 50 | }; |
| 51 | const profile = "a".repeat(64); |
| 52 | const rejected = await request("open", { profile, url: "http://127.0.0.1/private", width: 390, height: 640 }); |
| 53 | assert.equal(rejected.status, 403); |
| 54 | assert.equal((await request("open", { profile, url: `http://phone.example:${site.address().port}/`, width: 390, height: 640 })).id, id); |
| 55 | let frame; |
| 56 | for (let attempt = 0; attempt < 90; attempt++) { |
| 57 | frame = await request("frame"); |
| 58 | if (frame.title === "native phone fixture" && frame.image) break; |
| 59 | await delay(frame.status === 425 ? 1000 : 50); |
| 60 | } |
| 61 | assert.equal(frame.title, "native phone fixture"); |
| 62 | assert.equal(frame.width, 390); |
| 63 | assert.equal(frame.height, 640); |
| 64 | assert.ok(frame.document && frame.image); |
| 65 | assert.equal((await request("action", { type: "navigate", document: frame.document, url: "file:///etc/passwd" })).status, 403); |
| 66 | assert.equal((await request("close")).id, id); |
| 67 | assert.equal((await request("frame")).status, 410); |
| 68 | }); |
| 69 | |
| 70 | test("a cold phone launch is shared across polls and stopped on close, expiry and transport EOF", { skip: process.platform === "win32" }, async t => { |
| 71 | const directory = await mkdtemp(path.join(tmpdir(), "cage-phone-start-")); |
| 72 | t.after(() => rm(directory, { recursive: true, force: true })); |
| 73 | const bundle = path.join(directory, "controller"); |
| 74 | await promisify(execFile)("bash", [fileURLToPath(new URL("../package-macos-browser.sh", import.meta.url)), bundle]); |
| 75 | const { runMacOSPhoneBrowser } = await import(pathToFileURL(path.join(bundle, "macos-phone-browser.mjs"))); |
| 76 | const marker = path.join(directory, "starts"); |
| 77 | const binary = path.join(directory, "slow-browser"); |
| 78 | await writeFile(binary, `#!/usr/bin/env node\nrequire("node:fs").appendFileSync(${JSON.stringify(marker)}, process.pid + "\\n"); setInterval(() => {}, 1000);\n`); |
| 79 | await chmod(binary, 0o700); |
| 80 | const input = new PassThrough(), output = new PassThrough(); |
| 81 | const replies = createInterface({ input: output })[Symbol.asyncIterator](); |
| 82 | const controller = runMacOSPhoneBrowser(input, output, { testing: true, binary, directory }); |
| 83 | t.after(async () => { input.end(); await controller; output.end(); }); |
| 84 | async function request(id, operation) { |
| 85 | const reply = replies.next(); |
| 86 | const fields = operation === "open" ? { width: 390, height: 640, profile: "a".repeat(64) } : {}; |
| 87 | input.write(JSON.stringify({ method: "POST", path: "/phone-browser/" + operation, input: { id, ...fields } }) + "\n"); |
| 88 | return JSON.parse((await reply).value); |
| 89 | } |
| 90 | async function starts(count) { |
| 91 | let list = []; |
| 92 | for (let i = 0; i < 60; i++) { |
| 93 | try { list = (await readFile(marker, "utf8")).trim().split("\n").map(Number); } |
| 94 | catch (error) { if (error.code !== "ENOENT") throw error; } |
| 95 | if (list.length >= count) break; |
| 96 | await delay(50); |
| 97 | } |
| 98 | assert.equal(list.length, count); |
| 99 | return list; |
| 100 | } |
| 101 | const dead = pid => assert.throws(() => process.kill(pid, 0), { code: "ESRCH" }); |
| 102 | const first = randomUUID(); |
| 103 | assert.equal((await request(first, "open")).id, first); |
| 104 | const polls = await Promise.all([request(first, "frame"), request(first, "frame"), request(first, "open")]); |
| 105 | assert.deepEqual(polls.map(reply => reply.status ?? reply.id), [425, 425, first]); |
| 106 | const [firstPid] = await starts(1); |
| 107 | assert.equal((await request(first, "close")).id, first); |
| 108 | dead(firstPid); |
| 109 | |
| 110 | const realTimeout = globalThis.setTimeout; |
| 111 | const shortDeadline = t.mock.method(globalThis, "setTimeout", (fn, ms, ...args) => realTimeout(fn, ms === 75000 ? 600 : ms, ...args)); |
| 112 | const second = randomUUID(); |
| 113 | assert.equal((await request(second, "open")).id, second); |
| 114 | let final; |
| 115 | for (let i = 0; i < 50; i++) { |
| 116 | final = await request(second, "frame"); |
| 117 | if (final.status !== 425) break; |
| 118 | await delay(100); |
| 119 | } |
| 120 | assert.equal(final.status, 503); |
| 121 | dead((await starts(2))[1]); |
| 122 | shortDeadline.mock.restore(); |
| 123 | |
| 124 | assert.equal((await request(first, "open")).id, first); |
| 125 | const launched = await starts(3); |
| 126 | input.end(); |
| 127 | await controller; |
| 128 | dead(launched[2]); |
| 129 | }); |