| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | cd "$(dirname "$0")/.." |
| 4 | [[ $(id -u) != 0 ]] || { echo 'Run as an ordinary user in a disposable Ubuntu container.' >&2; exit 2; } |
| 5 | source internal/cage/tool-versions.env |
| 6 | source internal/cage/guest-tools.sh |
| 7 | source internal/cage/ubuntu-browser.sh |
| 8 | cage_is_ubuntu || { echo 'This test requires Ubuntu.' >&2; exit 2; } |
| 9 | sudo apt-get update |
| 10 | cage_install_ubuntu_browser |
| 11 | cage_install_ubuntu_novnc |
| 12 | test -r /usr/local/share/cage-novnc/core/rfb.js |
| 13 | sudo python3 internal/cage/novnc-patch.py /usr/local/share/cage-novnc |
| 14 | chromium --version |
| 15 | |
| 16 | stage=$(mktemp -d) |
| 17 | trap 'rm -rf "$stage"' EXIT |
| 18 | CAGE_BROWSER_TEST_PROFILE="$stage/profile" CAGE_BROWSER_TEST_EXTENSION="$PWD/internal/cage/split-extension" CAGE_BROWSER_TEST_VERSION="$CAGE_UBUNTU_BROWSER_VERSION" \ |
| 19 | xvfb-run -a node --input-type=module <<'JS' |
| 20 | import assert from 'node:assert/strict'; |
| 21 | import { spawn } from 'node:child_process'; |
| 22 | import { createHash } from 'node:crypto'; |
| 23 | const extension = process.env.CAGE_BROWSER_TEST_EXTENSION; |
| 24 | const id = createHash('sha256').update(extension).digest('hex').slice(0, 32) |
| 25 | .replace(/./g, c => String.fromCharCode(97 + parseInt(c, 16))); |
| 26 | const browser = spawn('/usr/bin/chromium', [ |
| 27 | `--user-data-dir=${process.env.CAGE_BROWSER_TEST_PROFILE}`, `--load-extension=${extension}`, |
| 28 | '--remote-debugging-pipe', '--no-first-run', '--no-default-browser-check', 'about:blank', |
| 29 | ], { stdio: ['ignore', 'ignore', 'inherit', 'pipe', 'pipe'] }); |
| 30 | let sequence = 0, buffer = Buffer.alloc(0), pageLoad; |
| 31 | const pending = new Map(); |
| 32 | browser.stdio[4].on('data', chunk => { |
| 33 | buffer = Buffer.concat([buffer, chunk]); |
| 34 | let end; |
| 35 | while ((end = buffer.indexOf(0)) >= 0) { |
| 36 | const message = JSON.parse(buffer.subarray(0, end).toString()); |
| 37 | buffer = buffer.subarray(end + 1); |
| 38 | if (pageLoad && message.sessionId === pageLoad.sessionId) { |
| 39 | if (message.method === 'Page.frameNavigated' && message.params.frame.url === pageLoad.url) pageLoad.navigated = true; |
| 40 | if (message.method === 'Page.loadEventFired' && pageLoad.navigated) pageLoad.resolve(); |
| 41 | } |
| 42 | const job = pending.get(message.id); |
| 43 | if (job) { pending.delete(message.id); message.error ? job.reject(message.error) : job.resolve(message.result); } |
| 44 | } |
| 45 | }); |
| 46 | function cdp(method, params = {}, sessionId) { |
| 47 | const id = ++sequence; |
| 48 | return new Promise((resolve, reject) => { |
| 49 | pending.set(id, { resolve, reject }); |
| 50 | browser.stdio[3].write(JSON.stringify({ id, method, params, sessionId }) + '\0'); |
| 51 | }); |
| 52 | } |
| 53 | const timeout = setTimeout(() => { browser.kill('SIGKILL'); console.error('Browser pipe test timed out'); process.exitCode = 1; }, 30000); |
| 54 | try { |
| 55 | const version = await cdp('Browser.getVersion'); |
| 56 | assert.ok(version.product.endsWith('/' + process.env.CAGE_BROWSER_TEST_VERSION)); |
| 57 | const { targetId } = await cdp('Target.createTarget', { url: 'about:blank' }); |
| 58 | const { sessionId } = await cdp('Target.attachToTarget', { targetId, flatten: true }); |
| 59 | await cdp('Page.enable', {}, sessionId); |
| 60 | const url = `chrome-extension://${id}/control.html`; |
| 61 | const loaded = new Promise(resolve => { pageLoad = { sessionId, url, resolve, navigated: false }; }); |
| 62 | const navigation = await cdp('Page.navigate', { url }, sessionId); |
| 63 | assert.equal(navigation.errorText, undefined); |
| 64 | await loaded; |
| 65 | const result = await cdp('Runtime.evaluate', { |
| 66 | expression: `({ protocol: location.protocol, extension: globalThis.chrome?.runtime?.id, title: document.title })`, |
| 67 | returnByValue: true, |
| 68 | }, sessionId); |
| 69 | assert.equal(result.result.value.protocol, 'chrome-extension:'); |
| 70 | assert.equal(result.result.value.extension, id); |
| 71 | console.log(JSON.stringify({ browser: version.product, extension: true, cdpPipe: true, sandboxEnabled: true, noVNC: '1.6.0' })); |
| 72 | await cdp('Browser.close'); |
| 73 | } finally { |
| 74 | clearTimeout(timeout); |
| 75 | browser.kill(); |
| 76 | } |
| 77 | JS |