#!/usr/bin/env python3 """Loopback-only updater fixture. Never used by the shipping app.""" from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path import hashlib import json ROOT = Path(__file__).resolve().parents[1] COMMIT = 'a' * 40 class Handler(BaseHTTPRequestHandler): def log_message(self, *args): pass def do_GET(self): path = self.path files = {'Luma.apk': ROOT/'output/Luma.apk', 'wrong.apk': ROOT/'build/update-tests/wrong-signer.apk', 'other.apk': ROOT/'output/Luma-wake-tests.apk'} if path == '/api/v1/repos/Elijah/Luma/branches/main': payload = json.dumps({'commit': {'id': COMMIT}}).encode() elif path.endswith('.json'): feed = json.loads((ROOT/'output/update.json').read_text()) mode = path.rsplit('/', 1)[-1] if mode == 'badhash.json': feed['sha256'] = '0' * 64 elif mode == 'wrongkey.json': feed.update(apk='wrong.apk', size=files['wrong.apk'].stat().st_size, sha256=hashlib.sha256(files['wrong.apk'].read_bytes()).hexdigest()) elif mode == 'wrongpackage.json': feed.update(apk='other.apk', size=files['other.apk'].stat().st_size, sha256=hashlib.sha256(files['other.apk'].read_bytes()).hexdigest()) elif mode == 'wrongcode.json': feed['versionCode'] += 1 elif mode == 'oversized.json': feed['size'] = 33 * 1024 * 1024 elif mode == 'current.json': feed['versionCode'] = 3 payload = json.dumps(feed).encode() elif path.rsplit('/', 1)[-1] in files: payload = files[path.rsplit('/', 1)[-1]].read_bytes() else: self.send_error(404); return self.send_response(200) self.send_header('Content-Length', str(len(payload))) self.send_header('Content-Type', 'application/json' if path.endswith('.json') else 'application/octet-stream') self.end_headers() self.wfile.write(payload) if __name__ == '__main__': print('Updater fixture: 127.0.0.1:8766', flush=True) ThreadingHTTPServer(('127.0.0.1', 8766), Handler).serve_forever()