| ... | ... | @@ -30,9 +30,13 @@ class Host(socketserver.StreamRequestHandler): |
| 30 | 30 | owner = None |
| 31 | 31 | files = {} |
| 32 | 32 | power_actions = [] |
| 33 | writes = [] |
| 33 | 34 | |
| 34 | 35 | def handle(self): |
| 35 | | request = json.loads(self.rfile.readline()) |
| 36 | line = self.rfile.readline() |
| 37 | if not line: |
| 38 | return |
| 39 | request = json.loads(line) |
| 36 | 40 | try: |
| 37 | 41 | reply = {"value": self.answer(request["operation"], request.get("payload") or {})} |
| 38 | 42 | except KeyError as missing: |
| ... | ... | @@ -63,13 +67,16 @@ class Host(socketserver.StreamRequestHandler): |
| 63 | 67 | if operation == "stat": |
| 64 | 68 | return {"directory": True} if path.endswith("/") else {"directory": False, "size": len(files[path])} |
| 65 | 69 | if operation == "write": |
| 66 | | files[path] = files.get(path, b"")[:request["offset"]] + base64.b64decode(request["data"]) |
| 70 | data = base64.b64decode(request["data"]) |
| 71 | self.writes.append((path, len(data))) |
| 72 | files[path] = files.get(path, b"")[:request["offset"]] + data |
| 67 | 73 | elif operation == "commit": |
| 68 | 74 | files[request["destination"]] = files.pop(path) |
| 69 | 75 | elif operation == "read": |
| 70 | 76 | data = files[path][request["offset"]:request["offset"] + request["length"]] |
| 71 | 77 | return {"data": base64.b64encode(data).decode()} |
| 72 | 78 | elif operation == "remove": |
| 79 | time.sleep(.05) |
| 73 | 80 | files.pop(path, None) |
| 74 | 81 | return {} |
| 75 | 82 | |
| ... | ... | @@ -84,8 +91,8 @@ class Client(unittest.TestCase): |
| 84 | 91 | with socket.socket() as available: |
| 85 | 92 | available.bind(("127.0.0.1", 0)) |
| 86 | 93 | port = available.getsockname()[1] |
| 87 | | host = socketserver.ThreadingUnixStreamServer(str(data / "host.sock"), Host) |
| 88 | | threading.Thread(target=host.serve_forever, daemon=True).start() |
| 94 | cls.host = socketserver.ThreadingUnixStreamServer(str(data / "host.sock"), Host) |
| 95 | threading.Thread(target=cls.host.serve_forever, daemon=True).start() |
| 89 | 96 | environment = {**os.environ, "SNOWGLOBE_DOMAIN": "paperclover.net", "SNOWGLOBE_DATA_DIR": str(data), |
| 90 | 97 | "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + HOST, "SNOWGLOBE_AUTH_RP_ID": "auth.paperclover.net", |
| 91 | 98 | "SNOWGLOBE_FILE_ORIGIN": "https://file.paperclover.net", "SNOWGLOBE_AUTH_REQUIRED": "1", |
| ... | ... | @@ -96,7 +103,8 @@ class Client(unittest.TestCase): |
| 96 | 103 | with sqlite3.connect(data / "accounts.sqlite") as db: |
| 97 | 104 | Host.owner = db.execute("SELECT id FROM users WHERE username='preview'").fetchone()[0] |
| 98 | 105 | cls.log = data / "server.log" |
| 99 | | cls.server = subprocess.Popen([BINARY], env=environment, stdout=cls.log.open("wb"), stderr=subprocess.STDOUT) |
| 106 | with cls.log.open("wb") as log: |
| 107 | cls.server = subprocess.Popen([BINARY], env=environment, stdout=log, stderr=subprocess.STDOUT) |
| 100 | 108 | deadline = time.monotonic() + 30 |
| 101 | 109 | while True: |
| 102 | 110 | try: |
| ... | ... | @@ -130,6 +138,8 @@ class Client(unittest.TestCase): |
| 130 | 138 | def tearDownClass(cls): |
| 131 | 139 | cls.server.terminate() |
| 132 | 140 | cls.server.wait(timeout=10) |
| 141 | cls.host.shutdown() |
| 142 | cls.host.server_close() |
| 133 | 143 | cls.temporary.cleanup() |
| 134 | 144 | |
| 135 | 145 | @classmethod |
| ... | ... | @@ -141,6 +151,7 @@ class Client(unittest.TestCase): |
| 141 | 151 | connection.request(method, path, json.dumps(body) if body is not None else None, headers) |
| 142 | 152 | response = connection.getresponse() |
| 143 | 153 | content = response.read() |
| 154 | connection.close() |
| 144 | 155 | for cookie in response.headers.get_all("set-cookie") or []: |
| 145 | 156 | key, value = cookie.split(";", 1)[0].split("=", 1) |
| 146 | 157 | cls.cookies[key] = value |
| ... | ... | @@ -181,6 +192,33 @@ class Client(unittest.TestCase): |
| 181 | 192 | vm.download("test-vm", "/tmp/payload.bin", source.with_suffix(".back")) |
| 182 | 193 | self.assertEqual(source.with_suffix(".back").read_bytes(), source.read_bytes()) |
| 183 | 194 | |
| 195 | data = source.read_bytes() |
| 196 | for declared, content, expected in [(len(data), data, 200), (0, b"", 200), |
| 197 | (len(data) - 1, data, 413), (len(data) + 1, data, 400)]: |
| 198 | destination = f"/tmp/fragmented-{declared}" |
| 199 | if expected != 200: |
| 200 | Host.files[destination] = b"retained" |
| 201 | ticket = vm.call("computer_upload", vm="test-vm", path=destination, size=declared, overwrite=True) |
| 202 | def fragments(): |
| 203 | for offset in range(0, len(content), 997): |
| 204 | yield content[offset:offset + 997] |
| 205 | time.sleep(.001) |
| 206 | connection = http.client.HTTPConnection("127.0.0.1", self.port, timeout=30) |
| 207 | connection.request("PUT", urlsplit(ticket["url"]).path, fragments(), self.headers, encode_chunked=True) |
| 208 | response = connection.getresponse() |
| 209 | body = response.read() |
| 210 | connection.close() |
| 211 | self.assertEqual(response.status, expected, body) |
| 212 | if expected == 200: |
| 213 | self.assertEqual(Host.files[destination], content) |
| 214 | sizes = [size for path, size in Host.writes if path.startswith(destination + ".snowglobe-")] |
| 215 | self.assertEqual(len(sizes), max(1, (len(content) + 65535) // 65536)) |
| 216 | self.assertEqual(sum(sizes), len(content)) |
| 217 | else: |
| 218 | self.assertEqual(Host.files[destination], b"retained") |
| 219 | self.assertFalse(any(path.startswith(destination + ".snowglobe-") for path in Host.files), |
| 220 | "upload returned before removing its temporary file") |
| 221 | |
| 184 | 222 | self.assertIn("png", vm.call("computer_shot", vm="test-vm")) |
| 185 | 223 | |
| 186 | 224 | state = json.loads(computer.STATE.read_text()) |