summaryrefslogtreecommitdiff
path: root/test/web/catalog.py
blob: 87773d887ee3a02b02986c1b1ac27bab8dc61e45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/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 shutil
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'
            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
            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
            # 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:
            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()