diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-12 17:15:03 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:16 -0300 |
| commit | 7a07e24916cd5bc762996d81aa1faf550177cd6f (patch) | |
| tree | 4530a8562fb3f068cc4340f1fab1f6c599721735 /test/web | |
| parent | f9ca3fb5ba767300bec3f1fbb13fc5e6552a90e3 (diff) | |
| download | cloud9-7a07e24916cd5bc762996d81aa1faf550177cd6f.tar.gz cloud9-7a07e24916cd5bc762996d81aa1faf550177cd6f.zip | |
9web: serve prefixed sites through the composed 9P namespace
Post only the pure site/control/discovery tree, with a separate host-only adapter and no duplicate supervision. Serve websites, metadata and legacy controls through the ordinary 9P mux; retain whole-root filesystem browsing and compatibility paths. Pin public file descriptors across publication.
Add opt-in build-time HTML/CSS prefix packaging with separate atomic preview generations and unchanged JavaScript/default output. Optional compatibility ports use ?u16. Deploy all five workstation sites on [::]:8090, with /mnt/9p/9web controls. Document Plan 9 references, companion build commits, shared-origin trust and live validation.
Validated catalogue, host/FUSE, HTTP FS, mux, prefix and Chromium suites; original E2E 32 scenarios; all five live sites, blog navigation, IPv4/IPv6 and real cafe build through 9P.
Diffstat (limited to 'test/web')
| -rw-r--r-- | test/web/catalog.py | 18 | ||||
| -rw-r--r-- | test/web/catalog_ui.mjs | 29 | ||||
| -rw-r--r-- | test/web/host_fs.py | 8 | ||||
| -rw-r--r-- | test/web/prefix.py | 73 |
4 files changed, 122 insertions, 6 deletions
diff --git a/test/web/catalog.py b/test/web/catalog.py index e1b153e..87773d8 100644 --- a/test/web/catalog.py +++ b/test/web/catalog.py @@ -6,6 +6,7 @@ import json import os from pathlib import Path import socket +import shutil import struct import subprocess import sys @@ -145,8 +146,14 @@ def main(): assert http(site_port, '/nested/')[2] == b'nested page' assert http(site_port, '/', 'HEAD')[2] == b'' assert http(site_port, '/style.css', headers={'Range':'bytes=0-3'})[2] == b'body' + assert http(dashboard, '/sites/fixture/')[2] == b'<h1>catalog fixture</h1>' + assert http(dashboard, '/sites/fixture/pretty')[2] == b'pretty page' + assert http(dashboard, '/sites/fixture/nested?x=1')[1]['location'] == '/sites/fixture/nested/?x=1' + assert http(dashboard, '/sites/fixture/style.css', headers={'Range':'bytes=-2'})[0] == 206 + assert http(dashboard, '/sites/fixture/style.css', headers={'Range':'bytes=999999-'})[0] == 416 for path in ['/../secret', '/%2e%2e/secret', '/escape', '/escape-dir/secret', '/.git/config', '/%2fetc/passwd']: assert http(site_port, path)[0] in (400,403,404), path + assert http(dashboard, '/sites/fixture' + path)[0] in (400,403,404), path assert http(dashboard, headers={'Host':'attacker.invalid'})[0] == 403 ctl = '/_cloud9/sites/fixture/ctl' assert http(dashboard, ctl, 'POST', 'build', {'Origin':'https://attacker.invalid'})[0] == 403 @@ -199,6 +206,17 @@ def main(): nine.write('sites/fixture/ctl', 'build') eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'failed') assert http(site_port)[0] == 200 + # A published generation replacement must not splice an open fid. + saved = (public/'style.css').read_bytes() + nine.walk('sites/fixture/public/style.css') + nine.rpc(112, struct.pack('<IB', 2, 0)) + assert nine.rpc(116, struct.pack('<IQI', 2, 0, 2))[4:] == saved[:2] + public.rename(root/'previous-public') + shutil.copytree(root/'previous-public', public, symlinks=True) + (public/'style.css').write_bytes(b'new generation') + assert nine.rpc(116, struct.pack('<IQI', 2, 2, 30000))[4:] == saved[2:] + nine.rpc(120, struct.pack('<I', 2)) + assert http(dashboard, '/sites/fixture/style.css')[2] == b'new generation' nine.s.close() print('catalog: HTTP serving, paths, MIME, HEAD/range, controls, watch, failure, cancellation, 9P events and passive discovery passed') except BaseException: diff --git a/test/web/catalog_ui.mjs b/test/web/catalog_ui.mjs index eed6c0a..9f4aca5 100644 --- a/test/web/catalog_ui.mjs +++ b/test/web/catalog_ui.mjs @@ -48,12 +48,12 @@ try { const root = path.join(work, 'public'); await fs.mkdir(root); await fs.writeFile(path.join(root, 'index.html'), '<h1>Fixture</h1>'); await fs.writeFile(path.join(work, 'large.txt'), 'x'.repeat(180000)); - const dashboard = await port(), sitePort = await port(), debugPort = await port(); + const dashboard = await port(), debugPort = await port(); const runtime = path.join(work, 'runtime'); await fs.mkdir(path.join(runtime, '9p'), { recursive: true }); start(path.resolve('zig-out/bin/9proc-demo'), ['--unix', path.join(runtime, '9p', 'fixture')]); await wait(() => fs.stat(path.join(runtime, '9p', 'fixture')), '9P fixture posted'); - await fs.writeFile(path.join(work, 'config.json'), JSON.stringify({ filesystem: '/', sites: [{ name: 'fixture', title: 'Fixture notes', cwd: work, root, port: sitePort, build: ['sh', '-c', 'echo integrated-build'], inputs: ['public/index.html'] }] })); + await fs.writeFile(path.join(work, 'config.json'), JSON.stringify({ filesystem: '/', sites: [{ name: 'fixture', title: 'Fixture notes', cwd: work, root, build: ['sh', '-c', 'echo integrated-build'], inputs: ['public/index.html'] }] })); const startWeb = () => start(path.resolve('zig-out/bin/9ns'), ['--mntgen', '--', binary, '--listen', `127.0.0.1:${dashboard}`, '--catalog', path.join(work, 'config.json'), '--catalog-socket', path.join(work, 'catalog.sock'), '--state-dir', path.join(work, 'state')], { ...process.env, XDG_RUNTIME_DIR: runtime }); const web = startWeb(); const url = `http://127.0.0.1:${dashboard}`; @@ -106,7 +106,7 @@ try { await fs.writeFile('.zig-cache/catalog-ui/files.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64')); await view('sites'); await readySites(); assert.equal(await evaluate(`document.title`), 'sites — 9web'); - assert.equal(await evaluate(`document.querySelector('#sites-entries .site-name').href`), `http://127.0.0.1:${sitePort}/`); + assert.equal(await evaluate(`document.querySelector('#sites-entries .site-name').href`), `${url}/sites/fixture/`); assert.equal(await evaluate(`document.querySelectorAll('link[rel=stylesheet]').length`), 1); assert.equal(await evaluate(`document.querySelector('link[rel=stylesheet]').getAttribute('href')`), '/_cloud9/style.css'); // Build button writes through the same /fs tree as the normal file browser. @@ -161,16 +161,35 @@ try { web.kill('SIGTERM'); await once(web, 'exit'); await fs.unlink(path.join(work, 'catalog.sock')); startWeb(); - await wait(async () => (await fetch(url)).ok && (await fetch(`http://127.0.0.1:${sitePort}`)).ok, 'immediate restart with recent HTTP traffic'); + await wait(async () => (await fetch(url)).ok && (await fetch(`${url}/sites/fixture/`)).ok, 'immediate restart with recent HTTP traffic'); // Opt-in, read-only smoke navigation against an installed workstation. if (process.env.NINE_WEB_LIVE) { - for (const pathname of ['/mnt/9p', '/mnt/9p/agents', '/mnt/9p/agents/codex/history', '/mnt/9p/zmx/daybreak/command', '/mnt/9p/zmx/daybreak/cwd', '/usr/bin']) { + for (const pathname of ['/mnt/9p', '/mnt/9p/9web', '/mnt/9p/9web/sites/entomology', '/mnt/9p/agents', '/mnt/9p/agents/codex/history', '/mnt/9p/zmx/daybreak/command', '/mnt/9p/zmx/daybreak/cwd', '/usr/bin']) { await cdp('Page.navigate', { url: process.env.NINE_WEB_LIVE + pathname }); await wait(() => evaluate(`document.getElementById('path')?.value === ${JSON.stringify(pathname)} && (document.getElementById('status').textContent === 'File loaded.' || document.getElementById('status').textContent.includes('entries'))`), 'live navigation: ' + pathname, 30000).catch(async error => { throw new Error(error.message + ': ' + await evaluate(`document.getElementById('status')?.textContent`)); }); console.log('live browser navigation passed:', pathname); } + for (const site of ['entomology', 'cafe', 'standards', 'pardes', 'cafe-hub']) { + const target = `${process.env.NINE_WEB_LIVE}/sites/${site}/`; + await cdp('Page.navigate', { url: target }); + await wait(() => evaluate(`location.href === ${JSON.stringify(target)} && document.readyState === 'complete' && document.body.textContent.length > 50`), 'live website: ' + site, 60000); + const images = await evaluate(`Array.from(document.images).filter(i => new URL(i.src).origin === location.origin && i.getAttribute('loading') !== 'lazy').map(i => ({src:i.src, ok:i.complete && i.naturalWidth > 0}))`); + assert.deepEqual(images.filter(i => !i.ok), [], `${site}: failed same-origin images`); + const unprefixed = await evaluate(`Array.from(document.querySelectorAll('a[href],img[src],script[src],link[href]')).map(e => e.getAttribute('href') || e.getAttribute('src')).filter(u => u?.startsWith('/') && !u.startsWith('//') && !u.startsWith('/sites/${site}/'))`); + assert.deepEqual(unprefixed, [], `${site}: unresolved absolute URLs`); + console.log('live prefixed website passed:', site, `(${images.length} local images)`); + if (site === 'cafe') { + await cdp('Page.navigate', { url: target + 'blog/' }); + await wait(() => evaluate(`location.pathname === '/sites/cafe/blog/' && document.readyState === 'complete' && document.querySelectorAll('a[href^="/sites/cafe/blog/"]').length > 0`), 'cafe blog navigation', 30000); + const article = await evaluate(`Array.from(document.querySelectorAll('a[href^="/sites/cafe/blog/"]')).map(a=>a.href).find(h=>new URL(h).pathname !== '/sites/cafe/blog/')`); + assert.ok(article); + await cdp('Page.navigate', { url: article }); + await wait(() => evaluate(`location.href === ${JSON.stringify(article)} && document.readyState === 'complete' && document.body.textContent.length > 100`), 'cafe pretty article', 30000); + console.log('live cafe blog and pretty article passed:', article); + } + } } console.log('catalog_ui: shared shell/style, tab history, file/log navigation, build control, focus, dirty edits and mobile layout passed'); } catch (e) { diff --git a/test/web/host_fs.py b/test/web/host_fs.py index 7a55eec..ea39cc9 100644 --- a/test/web/host_fs.py +++ b/test/web/host_fs.py @@ -137,7 +137,13 @@ def main(): eventually(lambda: b'host-build' in http(p, '/fs/_9web/sites/fixture/log')[2]) assert http(config['sites'][0]['port'])[2] == b'site still works' assert http(p, '/_cloud9/catalog.json')[0] == 200 - assert json.loads(http(p, '/_cloud9/catalog.json')[2])['namespace'] == '/_9web' + assert json.loads(http(p, '/_cloud9/catalog.json')[2])['namespace'] == '/mnt/9p/9web' + assert http(p, '/fs/mnt/9p/9web/sites/fixture/public/index.html')[2] == b'site still works' + assert http(p, '/sites/fixture/')[2] == b'site still works' + pure = Nine(str(registry/'9web')) + assert pure.list('sites') == ['fixture'] + assert pure.read('sites/fixture/public/index.html') == b'site still works' + pure.s.close() assert http(p, target+'/renamed', 'DELETE')[0] == 204 assert http(p, target, 'DELETE')[0] == 204 nine.s.close() diff --git a/test/web/prefix.py b/test/web/prefix.py new file mode 100644 index 0000000..68fe974 --- /dev/null +++ b/test/web/prefix.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +import hashlib +import importlib.util +from pathlib import Path +import tempfile + +spec = importlib.util.spec_from_file_location('prefix', Path(__file__).resolve().parents[2] / 'web/prefix.py') +p = importlib.util.module_from_spec(spec) +spec.loader.exec_module(p) +prefix = '/sites/fixture' +original = '''<!DOCTYPE html><html><head><style> +/* url(/comment) */ +a[href="/blog/"]{background:url('/assets/a.png');content:"/ordinary"} +@font-face{src:url(/fonts/a.woff2)} +@import "/main.css"; +.svg{background:url("data:image/svg+xml,%3Csvg viewBox='0 0 10 10'%3E")} +</style></head><body> +<a href="/blog/post?q=1&x=2#part">go</a><img src="/img.png" srcset="/one.png 1x, /two.png 2x"> +<a href="//elsewhere/foo">external</a><a href="https://other/x">external</a> +<a href="#part">fragment</a><img src="data:image/png;base64,abcd"> +<a href="../page.html">relative</a><div style="background:url(/img.png)"></div> +<code>href="/example"</code><!-- <a href="/comment"> --> +<script>const s = '/assets/unmodified.js'; fetch('/api/pokes');</script> +<script type="application/json">{"url":"/unmodified"}</script> +</body></html>''' +rewritten = p.html(original, prefix) +assert 'srcset=" /sites/fixture/a.png 1x, /sites/fixture/b.png 2x"' in p.html('<img srcset=" /a.png 1x, /b.png 2x">', prefix) +assert 'url("/sites/fixture/a.png")' in p.html('<div style="background:url("/a.png")"></div>', prefix) +assert p.srcset('data:image/svg+xml,/untouched 1x, /a.png 2x', prefix) == 'data:image/svg+xml,/untouched 1x, /sites/fixture/a.png 2x' +for piece in ['href="/sites/fixture/blog/post?q=1&x=2#part"', + 'srcset="/sites/fixture/one.png 1x, /sites/fixture/two.png 2x"', + 'url(/sites/fixture/fonts/a.woff2)', 'a[href="/sites/fixture/blog/"]', + '@import "/sites/fixture/main.css"']: + assert piece in rewritten, piece +for piece in ['/* url(/comment) */', 'content:"/ordinary"', '<code>href="/example"</code>', + "fetch('/api/pokes')", "const s = '/assets/unmodified.js'", '{"url":"/unmodified"}', + '<!-- <a href="/comment"> -->', 'href="../page.html"', 'href="#part"', + 'href="//elsewhere/foo"', 'href="https://other/x"', 'src="data:image/png;base64,abcd"', + 'url("data:image/svg+xml,%3Csvg viewBox=\'0 0 10 10\'%3E")']: + assert piece in rewritten, piece +for invalid in ['', '/', '//host', '/a/../b', '/a?b', '/a#b', 'http://a']: + try: + p.check_prefix(invalid) + raise AssertionError(invalid) + except ValueError: + pass +with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + source, out = root/'normal', root/'preview' + source.mkdir() + (source/'index.html').write_text(original) + (source/'app.js').write_text("fetch('/api/pokes')") + def hashes(): + return {str(f.relative_to(source)): hashlib.sha256(f.read_bytes()).hexdigest() for f in source.rglob('*') if f.is_file()} + before = hashes() + p.publish(source, out, prefix) + assert hashes() == before + assert out.is_symlink() and (out/'index.html').read_text() == rewritten + assert (out/'app.js').read_bytes() == (source/'app.js').read_bytes() + previous = out.resolve() + (source/'app.js').unlink() + p.publish(source, out, prefix) + assert out.resolve() != previous and previous.exists() + assert not (out/'app.js').exists(), 'stale files must not leak into next generation' + (source/'escape').symlink_to('/etc/hostname') + current = out.resolve() + try: + p.publish(source, out, prefix) + raise AssertionError('symlink accepted') + except ValueError: + pass + assert out.resolve() == current, 'failed generation changed published output' +print('prefix: scoped HTML/CSS, unchanged JS/text/source, atomic publication and failure preservation passed') |
