#!/usr/bin/env python3 """Black-box catalogue checks: real HTTP, 9P, child processes and files.""" from http.client import HTTPConnection, HTTPException import importlib.util import json import os from pathlib import Path import socket import struct import subprocess import sys import tempfile import time def port(): with socket.socket() as s: s.bind(('127.0.0.1', 0)) return s.getsockname()[1] def eventually(f, timeout=15): end = time.monotonic() + timeout while time.monotonic() < end: try: value = f() if value: return value except (OSError, HTTPException): pass time.sleep(.1) raise AssertionError('timed out: ' + repr(f)) def http(p, path='/', method='GET', body=None, headers=None): c = HTTPConnection('127.0.0.1', p, timeout=5) try: c.request(method, path, body, headers or {}) r = c.getresponse() return r.status, dict(r.getheaders()), r.read() finally: c.close() def string(s): b = s.encode() return struct.pack(' 1 else 'zig-out/bin/9web').resolve()) spec = importlib.util.spec_from_file_location('discover', Path(__file__).resolve().parents[2] / 'web/discover.py') discover = importlib.util.module_from_spec(spec) spec.loader.exec_module(discover) assert discover.recognize(['python3', '-m', 'http.server', '8000']) assert discover.recognize(['node', '/project/app.js', 'vite']) is None assert discover.recognize(['miniserve', '--tls-cert', 'cert'])[1] == 'https' with tempfile.TemporaryDirectory(prefix='9web-catalog-') as tmp: root = Path(tmp) public = root/'public' public.mkdir() (public/'index.html').write_text('

catalog fixture

') (public/'style.css').write_text('body{color:red}') (public/'pretty.html').write_text('pretty page') (public/'nested').mkdir() (public/'nested/index.html').write_text('nested page') (root/'secret').write_text('must not serve') (public/'escape').symlink_to(root/'secret') (public/'escape-dir').symlink_to(root, target_is_directory=True) (root/'source.txt').write_text('source') dashboard, site_port, python_port = port(), port(), port() cfg = dict(sites=[dict(name='fixture', title='Fixture', cwd=tmp, root=str(public), port=site_port, build=['sh','-c','echo build-output; if test -f slow; then sleep 30 & echo $! > descendant; wait; fi; sleep 0.2; cp source.txt public/result.txt'], inputs=['source.txt'])]) (root/'catalog.json').write_text(json.dumps(cfg)) log = open(root/'server.log', 'wb') proc = subprocess.Popen([binary, '--listen', f'0.0.0.0:{dashboard}', '--catalog', str(root/'catalog.json'), '--catalog-socket', str(root/'catalog.sock'), '--state-dir', str(root/'state')], stdout=log, stderr=log) py = subprocess.Popen([sys.executable, '-m', 'http.server', str(python_port), '--bind', '127.0.0.1', '--directory', str(public)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) try: eventually(lambda: http(dashboard)[0] == 200) assert http(site_port)[2] == b'

catalog fixture

' assert http(site_port, '/style.css')[1]['content-type'] == 'text/css' assert http(site_port, '/pretty')[2] == b'pretty page' assert http(site_port, '/nested')[0] == 301 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' 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, headers={'Host':'attacker.invalid'})[0] == 403 ctl = '/_cloud9/sites/fixture/ctl' assert http(dashboard, ctl, 'POST', 'build', {'Origin':'https://attacker.invalid'})[0] == 403 assert http(dashboard, ctl, 'POST', 'bogus')[0] == 400 nine = Nine(str(root/'catalog.sock')) assert nine.list('sites') == ['fixture'] assert nine.list('sites/fixture') == ['ctl', 'events', 'log', 'public', 'status'] assert nine.list('sites/fixture/public') == ['index.html', 'nested', 'pretty.html', 'style.css'] assert nine.read('sites/fixture/public/index.html').startswith(b'

') assert b'build' in nine.read('sites/fixture/ctl') nine.write('sites/fixture/ctl', 'build\n') eventually(lambda: (public/'result.txt').exists()) eventually(lambda: json.loads(nine.read('sites/fixture/status'))['generation'] == 1) assert b'build-output' in nine.read('sites/fixture/log') assert http(dashboard, ctl, 'POST', 'watch on')[0] == 202 eventually(lambda: json.loads(nine.read('sites/fixture/status'))['generation'] >= 2) (root/'source.txt').write_text('changed source') eventually(lambda: (public/'result.txt').read_text() == 'changed source') assert http(dashboard, ctl, 'POST', 'watch off')[0] == 202 def discovered(): data = json.loads(http(dashboard, '/_cloud9/catalog.json')[2]) return any(x['port'] == python_port and x['service'] == 'python http.server' for x in data['listeners']) eventually(discovered) # A parked event reader must not stall HTTP or other 9P clients. events = Nine(str(root/'catalog.sock')) events.walk('sites/fixture/events') events.rpc(112, struct.pack(' 0 pending = struct.pack(' json.loads(first[4:])['revision'] eventually(lambda: (root/'descendant').exists()) nine.write('sites/fixture/ctl', 'cancel') eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'canceled') descendant = int((root/'descendant').read_text()) def stopped(): try: return Path(f'/proc/{descendant}/stat').read_text().split(') ')[1].startswith('Z') except FileNotFoundError: return True eventually(stopped) events.s.close() (root/'slow').unlink() # A failed compile leaves readable existing output and reports failure. (root/'source.txt').unlink() nine.write('sites/fixture/ctl', 'build') eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'failed') assert http(site_port)[0] == 200 nine.s.close() print('catalog: HTTP serving, paths, MIME, HEAD/range, controls, watch, failure, cancellation, 9P events and passive discovery passed') except BaseException: log.flush() print((root/'server.log').read_text(), file=sys.stderr) raise finally: proc.terminate(); py.terminate() for p in (proc, py): try: p.wait(timeout=5) except subprocess.TimeoutExpired: p.kill(); p.wait() log.close() if __name__ == '__main__': main()