screentinker/server/test/device-owner-qr.test.js
screentinker 12c0004245
test(ci): OS-assigned ephemeral ports for subprocess suites — kill the port-collision flake (#176)
The subprocess-booting test suites hand-picked fixed ports in a cramped ~3955-4021 range, and
156-schedule-read-path deviated to a RANDOM port (3900 + rand%90) that overlapped those fixed
ports. Under CI load two servers could race on the same port, surfacing as flaky "no such table:
devices" / "FOREIGN KEY constraint failed" (a server answering a request against a half-migrated
or wrong DB). It's environmental — the suites pass locally and in isolation.

Fix: a shared test/helpers/free-port.js (bind :0 on loopback, read the OS-assigned port, release)
called in before() so every suite gets a guaranteed-unique ephemeral port — concurrent suites can
no longer collide, and no one has to hand-assign ports.

- Codemod converted 30 suites: const PORT = <fixed|random> -> let PORT (+ BASE) assigned via
  `PORT = await freePort()` at the top of before().
- 3 hand-fixed (different structure): 148-eviction-storm (lowercase `base`), boot-health (no
  before() — allocates PORT + a throwaway SEED_PORT inside the test, replacing the hardcoded
  3894), totp-keyrotation (no before() — allocates at the test start before bootServer()).

No fixed 39xx/40xx ports remain. Full server suite 435/435; the 4 hand-touched suites pass in
isolation. Pure test-infra change — no app code touched.
2026-07-13 09:51:40 -05:00

52 lines
3.1 KiB
JavaScript

// #161: the device-owner QR provisioning endpoint returns the AOSP provisioning payload (DPC
// component + APK download URL + signing-cert checksum), a rendered QR, and the ADB one-liner; and
// it is auth-gated. Boots a real server (same harness style as the other socket tests).
const path = require('node:path'); const os = require('node:os'); const crypto = require('node:crypto');
const fs = require('node:fs');
const { test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const { spawn } = require('node:child_process');
const sleep = ms => new Promise(r => setTimeout(r, ms));
const { freePort } = require('./helpers/free-port');
let PORT, BASE;
const DATA_DIR = path.join(os.tmpdir(), 'st-doqr-' + crypto.randomBytes(4).toString('hex'));
let proc, JWT;
before(async () => {
PORT = await freePort();
BASE = `http://127.0.0.1:${PORT}`;
const logFd = fs.openSync(path.join(os.tmpdir(), 'st-doqr.log'), 'w');
proc = spawn('node', ['server.js'], { cwd: path.join(__dirname, '..'), env: { ...process.env, DATA_DIR, SELF_HOSTED: 'true', PORT: String(PORT), NODE_ENV: 'test' }, stdio: ['ignore', logFd, logFd] });
let up = false; for (let i = 0; i < 80; i++) { try { if ((await fetch(BASE + '/api/status')).ok) { up = true; break; } } catch {} await sleep(250); }
if (!up) throw new Error('boot fail');
JWT = (await (await fetch(BASE + '/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: 'op@t.local', password: 'test12345', name: 'Op' }) })).json()).token;
});
after(() => { try { proc.kill('SIGKILL'); } catch {} });
test('device-owner-qr requires auth', async () => {
const r = await fetch(BASE + '/api/provision/device-owner-qr');
assert.equal(r.status, 401, 'unauthenticated request is rejected');
});
test('device-owner-qr returns the provisioning payload + QR + adb one-liner', async () => {
const r = await fetch(BASE + '/api/provision/device-owner-qr', { headers: { Authorization: 'Bearer ' + JWT } });
assert.equal(r.status, 200);
const b = await r.json();
assert.equal(b.component, 'com.remotedisplay.player/.admin.STDeviceAdminReceiver');
assert.match(b.adb_command, /^adb shell dpm set-device-owner com\.remotedisplay\.player\/\.admin\.STDeviceAdminReceiver$/);
assert.match(b.apk_url, /\/download\/apk$/, 'APK url points at the download route');
assert.ok(b.signature_checksum && b.signature_checksum.length > 20, 'a signing-cert checksum is present');
// URL-safe base64, no padding.
assert.doesNotMatch(b.signature_checksum, /[+/=]/, 'checksum is URL-safe base64 without padding');
const p = b.payload;
assert.equal(p['android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME'], b.component);
assert.equal(p['android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION'], b.apk_url);
assert.equal(p['android.app.extra.PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM'], b.signature_checksum);
assert.equal(p['android.app.extra.PROVISIONING_SKIP_ENCRYPTION'], true);
assert.match(b.qr_data_url, /^data:image\/png;base64,/, 'QR rendered as a PNG data-url');
});