#!/usr/bin/env python3
"""Exercise real browser storage with synthetic data across independent processes.
Linux: xvfb-run -a python3 scripts/test-browser-storage.py --helper /path/to/zeron-webkit
Native shell (Linux/macOS): python3 scripts/test-browser-storage.py --fixture /path/to/browser-fixture
"""
import argparse
import http.server
import json
import os
from pathlib import Path
import queue
import struct
import sys
import subprocess
import tempfile
import threading
import time
from urllib.parse import urlparse
PAGE = r"""
Storage fixture"""
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
mode = urlparse(self.path).path
self.send_response(200)
self.send_header('Cache-Control', 'no-store')
if mode == '/cookies':
body = json.dumps(self.headers.get('Cookie', '')).encode()
self.send_header('Content-Type', 'application/json')
else:
body = PAGE.encode()
self.send_header('Content-Type', 'text/html')
if mode in ('/write', '/delete'):
age = 3600 if mode == '/write' else 0
self.send_header('Set-Cookie', f'httpOnlyToken=fixture; Path=/; HttpOnly; SameSite=Lax; Max-Age={age}')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *_):
pass
class Helper:
def __init__(self, binary, profile, expect_error=False):
self.process = subprocess.Popen([binary, '--profile', str(profile)], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
self.events = queue.Queue()
self.thread = threading.Thread(target=self.receive, daemon=True)
self.thread.start()
kind, _, data = self.events.get(timeout=15)
if expect_error:
assert kind == b'E', (kind, data)
assert self.process.wait(timeout=5) != 0
self.process.stdin.close()
self.process.stdout.close()
return
if kind != b'R':
self.process.kill()
self.process.wait()
raise AssertionError(('helper startup failed', kind, data))
def receive(self):
def read(length):
data = bytearray()
while len(data) < length:
part = self.process.stdout.read(length - len(data))
if not part:
raise EOFError()
data.extend(part)
return bytes(data)
try:
while True:
kind, page, length = struct.unpack('