summaryrefslogtreecommitdiff
path: root/test/web
diff options
context:
space:
mode:
Diffstat (limited to 'test/web')
-rw-r--r--test/web/catalog.py217
1 files changed, 217 insertions, 0 deletions
diff --git a/test/web/catalog.py b/test/web/catalog.py
new file mode 100644
index 0000000..e1b153e
--- /dev/null
+++ b/test/web/catalog.py
@@ -0,0 +1,217 @@
+#!/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('<H', len(b)) + b
+
+
+class Nine:
+ def __init__(self, path):
+ self.s = socket.socket(socket.AF_UNIX)
+ self.s.settimeout(5)
+ self.s.connect(path)
+ self.rpc(100, struct.pack('<I', 65536) + string('9P2000'), 65535)
+ self.rpc(104, struct.pack('<II', 1, 0xffffffff) + string('user') + string(''))
+
+ def recv(self, n):
+ b = b''
+ while len(b) < n:
+ chunk = self.s.recv(n-len(b))
+ if not chunk:
+ raise AssertionError('9P disconnected')
+ b += chunk
+ return b
+
+ def rpc(self, t, body, tag=1):
+ self.s.sendall(struct.pack('<IBH', len(body)+7, t, tag) + body)
+ size, rt, rtag = struct.unpack('<IBH', self.recv(7))
+ result = self.recv(size-7)
+ assert rt != 107, result
+ assert rt == t+1 and rtag == tag, (rt, rtag)
+ return result
+
+ def walk(self, path):
+ names = path.split('/')
+ r = self.rpc(110, struct.pack('<IIH', 1, 2, len(names)) + b''.join(map(string, names)))
+ assert struct.unpack('<H', r[:2])[0] == len(names), path
+
+ def read(self, path):
+ self.walk(path)
+ self.rpc(112, struct.pack('<IB', 2, 0))
+ r = self.rpc(116, struct.pack('<IQI', 2, 0, 32000))
+ self.rpc(120, struct.pack('<I', 2))
+ return r[4:]
+
+ def write(self, path, text):
+ self.walk(path)
+ self.rpc(112, struct.pack('<IB', 2, 0x11))
+ b = text.encode()
+ self.rpc(118, struct.pack('<IQI', 2, 0, len(b)) + b)
+ self.rpc(120, struct.pack('<I', 2))
+
+ def list(self, path):
+ self.walk(path)
+ self.rpc(112, struct.pack('<IB', 2, 0))
+ offset, names = 0, []
+ while True:
+ data = self.rpc(116, struct.pack('<IQI', 2, offset, 160))[4:]
+ if not data: break
+ offset += len(data)
+ while data:
+ size = struct.unpack('<H', data[:2])[0] + 2
+ nlen = struct.unpack('<H', data[41:43])[0]
+ names.append(data[43:43+nlen].decode())
+ data = data[size:]
+ self.rpc(120, struct.pack('<I', 2))
+ return names
+
+
+def main():
+ binary = str(Path(sys.argv[1] if len(sys.argv) > 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('<h1>catalog fixture</h1>')
+ (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'<h1>catalog fixture</h1>'
+ 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'<h1>')
+ 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('<IB', 2, 0))
+ first = events.rpc(116, struct.pack('<IQI', 2, 0, 2048))
+ assert json.loads(first[4:])['revision'] > 0
+ pending = struct.pack('<IQI', 2, len(first)-4, 2048)
+ events.s.sendall(struct.pack('<IBH', len(pending)+7, 116, 2) + pending)
+ assert http(site_port)[0] == 200
+ (root/'slow').write_text('yes')
+ nine.write('sites/fixture/ctl', 'build')
+ size, typ, tag = struct.unpack('<IBH', events.recv(7))
+ assert (typ, tag) == (117, 2)
+ assert json.loads(events.recv(size-7)[4:])['revision'] > 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()