diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d5dc79976..97dffd87f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -947,9 +947,15 @@ jobs: CI_SOURCE_HOME: ${{ runner.temp }}/elastos-source-home CI_MODEL_REPAIR_JOURNEY: 'true' run: scripts/ci-installed-journeys.sh model-repair + - name: installed two-Home Community and direct messages + timeout-minutes: 10 + env: + CI_SOURCE_HOME: ${{ runner.temp }}/elastos-source-home + run: scripts/ci-installed-journeys.sh two-home-messages - name: summarize installed model journey if: always() env: + CI_TWO_HOME_MESSAGES: 'true' CI_SOURCE_HOME: ${{ runner.temp }}/elastos-source-home CI_CARRIER_GET: ${{ matrix.os == 'ubuntu-24.04' }} CI_FINITE_MEMORY: ${{ matrix.os == 'ubuntu-24.04' }} @@ -1193,9 +1199,15 @@ jobs: env: CI_SOURCE_HOME: ${{ runner.temp }}/elastos-mac-test-home run: scripts/ci-installed-journeys.sh home-repeat + - name: installed two-Home Community and direct messages + timeout-minutes: 10 + env: + CI_SOURCE_HOME: ${{ runner.temp }}/elastos-mac-test-home + run: scripts/ci-installed-journeys.sh two-home-messages - name: summarize installed model journey if: always() env: + CI_TWO_HOME_MESSAGES: 'true' CI_SOURCE_HOME: ${{ runner.temp }}/elastos-mac-test-home run: scripts/ci-installed-journeys.sh summary - name: upload installed model journey diff --git a/scripts/ci-installed-journeys.py b/scripts/ci-installed-journeys.py index 4149a70af..f85b89342 100644 --- a/scripts/ci-installed-journeys.py +++ b/scripts/ci-installed-journeys.py @@ -2,6 +2,7 @@ """Run journeys against this job's installed Home; save only public proof.""" import argparse import base64 +from contextlib import contextmanager import hashlib import json import os @@ -1695,9 +1696,9 @@ def ignore(directory, names): needed = sum(path.stat().st_size for name in immutable for path in (data / name).rglob("*") if path.is_file() and not path.is_symlink() and not any(parent in excluded for parent in (path, *path.parents))) require_disk_space(data, needed) - destination.mkdir(parents=True, exist_ok=False) + destination.mkdir(mode=0o700, parents=True, exist_ok=False) target = destination / data.relative_to(home) - target.mkdir(parents=True) + target.mkdir(mode=0o700, parents=True) for name in immutable: if (data / name).exists(): shutil.copytree(data / name, target / name, symlinks=True, ignore=ignore) @@ -1712,6 +1713,310 @@ def ignore(directory, names): return target +def installed_message_identity(data): + """Use the same source/installed byte boundary as the other installed journeys.""" + receipt = json.loads((data / "receipts/source-home-installation.json").read_text()) + candidate = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip() + tree = subprocess.check_output(["git", "rev-parse", "HEAD^{tree}"], text=True).strip() + runtime = digest(data / "bin/elastos") + assert receipt["source"]["commit"] == candidate and receipt["source"]["tree"] == tree + assert receipt["source"]["clean"] is True and receipt["runtime"]["parity"] is True + assert receipt["runtime"]["built_sha256"] == receipt["runtime"]["installed_sha256"] == "sha256:" + runtime + assert receipt["components_sha256"] == "sha256:" + digest(data / "components.json") + capsules = data / "receipts/source-home-capsules.json" + assert receipt["source_home_capsule_metadata_receipt_sha256"] == "sha256:" + digest(capsules) + manifest = json.loads((data / "components.json").read_text()) + for name in ("home", "home-gui", "people", "inbox", "chat-room"): + entry = manifest["capsules"][name] + installed = data / "capsules" / name + assert "sha256:" + digest(installed / entry["entrypoint"]) == entry["entrypoint_sha256"] + for asset in entry["browser_assets"]: + assert "sha256:" + digest(installed / asset["path"]) == asset["sha256"] + return {"candidate": candidate, "source_tree": tree, "installed_runtime_sha256": runtime, + "source_components_sha256": digest(data / "components.json"), + "installation_receipt_sha256": digest(data / "receipts/source-home-installation.json")} + + +def remove_message_fixture(root): + """Remove only this fresh, process-free fixture; never follow a root/parent link.""" + root = Path(os.path.abspath(root)) + for path in (root, *root.parents): + assert stat.S_ISDIR(path.lstat().st_mode) and not path.is_symlink() + assert not any(row["command"].startswith(str(root) + "/") for row in process_rows().values()) + shutil.rmtree(root) + + +# The 10-minute CI step reserves 360 s for work, 150 s for process cleanup, +# and 30 s for disk/removal/receipt operations: 540 s, with 60 s runner margin. +class MessageDeadline(BaseException): + """A phase deadline survives recoverable OS/network error handlers.""" + + +@contextmanager +def message_timeout(seconds): + def expired(_signum, _frame): + raise MessageDeadline("two-Home phase deadline") + started = time.monotonic() + previous = signal.signal(signal.SIGALRM, expired) + timer = signal.setitimer(signal.ITIMER_REAL, 0) + signal.setitimer(signal.ITIMER_REAL, min(seconds, timer[0]) if timer[0] else seconds) + try: + yield + finally: + signal.setitimer(signal.ITIMER_REAL, 0) + signal.signal(signal.SIGALRM, previous) + if timer[0]: + signal.setitimer(signal.ITIMER_REAL, max(.001, timer[0] - (time.monotonic() - started)), timer[1]) + + +def remember_message_processes(child, known, marker=None): + rows = {pid: row for pid, row in process_rows().items() + if not row["command"].endswith(" ")} + roots = {pid for pid, old in known.items() if pid in rows + and all(rows[pid][key] == old[key] for key in ("start", "command"))} + if not known and child.pid in rows and child.poll() is None: + roots.add(child.pid) + # Chromium has its own process group. Its unique fixture argument also + # identifies it if Node exits before the next descendant census. + if marker: + roots.update(pid for pid, row in rows.items() if re.search(r"(?= next_census: + remember_message_processes(browser, browser_processes, "--two-home-fixture=" + str(control)) + next_census = time.monotonic() + 1 + request = control / "request.json" + if request.exists(): + value = private_json(request) + if value["id"] > last: + assert value["id"] == last + 1 and value["action"] in {"stop_b", "start_b"} + stop("b") if value["action"] == "stop_b" else launch("b") + last = value["id"] + private_json(control / "response.json", {"id": last, "status": "passed"}) + time.sleep(.1) + observed = json.loads((evidence / "two-home-ui.json").read_text()) + record.update(observed) + if browser.returncode and (control / "error.private.txt").exists(): + record["browser_failure"] = redact((control / "error.private.txt").read_text(), [str(root)])[:1200] + assert browser.returncode == 0, "installed two-Home browser journey failed" + assert all(record["results"].get(name) == "passed" for name in ( + "community_post", "direct_received", "sender_delivered", "offline_pending", "restart_delivery")) + assert all(record["negative_controls"].get(name, {}).get("assertion_rejected") is True + for name in ("sender_delivered",)) + except BaseException as error: + record["failure"] = observer_failure("two_home_messages", error, [str(root), str(home)]) + raise + finally: + record["process_cleanup"] = cleanup + finish_message_fixture(root, data, evidence, record, children, fixtures, stop, browser, browser_processes, started) + + def repeat(home, data, evidence, count): if count != 3: raise RuntimeError("Mac timing series requires three fresh journeys") @@ -1747,7 +2052,16 @@ def repeat(home, data, evidence, count): parser.add_argument("--carrier-get", action="store_true") parser.add_argument("--finite-memory", type=int, choices=[1, 4]) parser.add_argument("--model-repair", action="store_true", help="real Stop, Get different model and interrupted Get/Home restart/Retry") + parser.add_argument("--two-home-messages", action="store_true") + parser.add_argument("--network-arrangement", choices=MESSAGE_NETWORKS.keys(), default="same-host") + parser.add_argument("--two-home-root", type=Path, help="fresh disposable fixture path") args = parser.parse_args() + if args.two_home_messages: + if args.repeat or args.home_only or args.carrier_get or args.finite_memory or args.model_repair: + parser.error("two-Home messages is a separate installed journey") + two_home_messages(args.home.resolve(), args.data.resolve(), args.evidence.resolve(), + args.network_arrangement, args.two_home_root) + raise SystemExit(0) if args.model_repair and (sys.platform != "linux" or args.home_only or args.repeat or args.finite_memory): parser.error("--model-repair proves one production Linux Home with two signed models") if args.home_only and args.repeat: diff --git a/scripts/ci-installed-journeys.sh b/scripts/ci-installed-journeys.sh index a0027969e..252ed9982 100755 --- a/scripts/ci-installed-journeys.sh +++ b/scripts/ci-installed-journeys.sh @@ -39,6 +39,9 @@ PY model-repair) python3 scripts/ci-installed-journeys.py "$CI_HOME" "$DATA" "$EVIDENCE/model-repair" --model-repair ;; + two-home-messages) + python3 scripts/ci-installed-journeys.py "$CI_HOME" "$DATA" "$EVIDENCE/two-home-messages" --two-home-messages + ;; home) python3 scripts/ci-installed-journeys.py "$CI_HOME" "$DATA" "$EVIDENCE/engine-absent-home" --home-only if [[ "${CI_FINITE_MEMORY:-false}" == true ]]; then @@ -83,6 +86,9 @@ except (OSError, ValueError, AssertionError, KeyError, RuntimeError): carrier = os.environ.get("CI_CARRIER_GET") == "true" finite = os.environ.get("CI_FINITE_MEMORY") == "true" repair = os.environ.get("CI_MODEL_REPAIR_JOURNEY") == "true" +messages_path = root / "two-home-messages/installed-journeys.json" +messages = json.loads(messages_path.read_text()) if messages_path.exists() else {} +messages_required = os.environ.get("CI_TWO_HOME_MESSAGES") == "true" results = {name: "failed or not run" for name in ( "home_screenshots", "model_package_admission", *(["carrier_get"] if carrier else []), "installed_runtime_reply", "model_timing_observer", "process_cleanup", "disk_reserve")} @@ -127,6 +133,14 @@ results["engine_absent_home"] = ("passed" if current(absence) and absence.get("e ("engine_absent_home", "engine_absent_refusal", "home_screenshots", "process_cleanup", "disk_reserve")) else "failed or not run") elapsed += absence.get("elapsed_seconds", 0) + absence.get("fixture_preparation_seconds", 0) +if messages_required: + try: + message_current = journey["two_home_receipt_valid"](messages, journey["installed_message_identity"](data)) + except (OSError, ValueError, KeyError, AssertionError): + message_current = False + for name in ("community_post", "direct_received", "sender_delivered", "offline_pending", "restart_delivery"): + results["two_home_" + name] = "passed" if message_current else "failed or not run" +elapsed += messages.get("elapsed_seconds", 0) repair_path = root / "model-repair/installed-journeys.json" repair_record = json.loads(repair_path.read_text()) if repair and repair_path.exists() else {} if repair: @@ -145,6 +159,7 @@ if repair: "fixture_policy": "fresh Home state and engine process per run; verified source engine prerequisite; same host with potentially warm OS file cache" + ("; reply package only on a separate holder Home, fetched over Carrier" if carrier else ""), "engine_absent_home": absence, + **({"two_home_messages": messages} if messages_required else {}), **({"finite_memory_refusal": memory} if finite else {}), **({"model_repair": repair_record} if repair else {}), }, indent=2) + "\n") @@ -176,5 +191,5 @@ if spread["status"] == "incomplete" or any(result != "passed" for result in resu raise SystemExit("installed journey acceptance is incomplete") PY ;; - *) echo "Usage: $0 prepare|home|home-repeat|model-repair|summary" >&2; exit 2 ;; + *) echo "Usage: $0 prepare|home|home-repeat|model-repair|two-home-messages|summary" >&2; exit 2 ;; esac diff --git a/scripts/ci-installed-two-home-journey.mjs b/scripts/ci-installed-two-home-journey.mjs new file mode 100644 index 000000000..402b8e1b4 --- /dev/null +++ b/scripts/ci-installed-two-home-journey.mjs @@ -0,0 +1,232 @@ +import assert from 'node:assert/strict'; +import {createRequire} from 'node:module'; +import {writeFileSync, readFileSync, renameSync} from 'node:fs'; +import {fileURLToPath} from 'node:url'; +import {join} from 'node:path'; +export function assertCommunityMessage(poll, id, text) { + const message = poll.objects.find(row => row.message_id === id); + assert(message && message.body === text && !message.from_current_session, 'Community exact text from the other Home'); + return message; +} +export function assertReceivedDirect(messages, id, text) { + const message = messages.find(row => row.message_id === id); + assert(message && message.direction === 'incoming' && message.text === text && message.delivery_state === 'received', 'recipient exact direct message'); + return message; +} +export function assertSenderDelivery(message, detail) { + assert(message?.direction === 'outgoing' && message.delivery_state === 'receipt_settled', 'sender receipt settled'); + const time = new Date(message.created_at * 1000).toISOString().slice(11, 16); + assert.equal(detail, time, 'sender shows the message time, with neither Sending nor Expired'); +} +export function assertMessageOrigins(bases, browserHost) { + assert(browserHost && bases.every(base => new URL(base).hostname === browserHost), 'Home origins match the network arrangement browser host'); +} +async function run() { + const require = createRequire(import.meta.url); + const {chromium} = require(process.env.BROWSER_OPERATOR_PLAYWRIGHT_CORE || '../elastos/tools/browser-playwright-engine/node_modules/playwright'); + const [baseA, baseB, evidence, control, browserHost] = process.argv.slice(2); + const bases = [baseA, baseB]; + assertMessageOrigins(bases, browserHost); + const result = {stage:'sign_in', results:{}, negative_controls:{}, observations:[], browser_cross_home_http_requests:0}; + let sequence = 0; + async function runtime(action) { + const id = ++sequence; + writeFileSync(join(control, 'request.json.next'), JSON.stringify({id, action}), {flag:'wx', mode:0o600}); + renameSync(join(control, 'request.json.next'), join(control, 'request.json')); + await until('owned Runtime action', async () => { + let reply; + try {reply = JSON.parse(readFileSync(join(control, 'response.json'), 'utf8'));} + catch (error) {if (error.code !== 'ENOENT') throw error;} + return reply?.id === id && reply.status === 'passed'; + }, 120000); + } + const sleep = ms => new Promise(resolve=>setTimeout(resolve,ms)); + async function until(label, fn, ms=150000) { + const end=Date.now()+ms; + while(Date.now() { + const url = new URL(route.request().url()); + if (['http:', 'https:'].includes(url.protocol) && url.origin !== base) { + result.browser_cross_home_http_requests++; + return route.abort(); + } + return route.continue(); + }); + const page=await context.newPage(); + const cdp=await context.newCDPSession(page); + await cdp.send('WebAuthn.enable'); + await cdp.send('WebAuthn.addVirtualAuthenticator',{options:{protocol:'ctap2',transport:'internal',hasResidentKey:true,hasUserVerification:true,isUserVerified:true,automaticPresenceSimulation:true}}); + result.stage='home_frontdoor_'+i; + await page.goto(base+'/home/',{waitUntil:'domcontentloaded'}); + result.stage='home_name_'+i; + await page.locator('#home-unlock-name').fill('Two Home '+(i?'B':'A'),{timeout:60000}); + result.stage='passkey_registration_'+i; + const registered=page.waitForResponse(r=>r.url().endsWith('/api/auth/passkey/register/complete')); + await page.locator('#home-unlock-primary').click(); + const registration = await registered; + result.registration_http_status = registration.status(); + if (!registration.ok()) throw new Error('passkey registration HTTP '+registration.status()+' '+await registration.text()); + result.stage='home_ready_'+i; + await page.waitForFunction(()=>document.body.dataset.homeStatus==='ready'&&document.body.dataset.homeAuthority==='signed',null,{timeout:60000}); + async function app(target,query={}) { + const launch=await page.evaluate(async ({target,query})=>{ + const {fetchJson}=await import('/home/shell-core.js?v=home-wallpaper-20261030b'); + return fetchJson('/api/apps/home/launch',{method:'POST',body:JSON.stringify({target,query:{...query,home_origin:location.origin}})}); + },{target,query}); + const tab=await context.newPage(); + await tab.goto(new URL(launch.route,base).href,{waitUntil:'domcontentloaded'}); + return tab; + } + result.stage='people_launch_'+i; + const people=await app('people'); + homes.push({context,page,people,app,base}); + } + async function api(page,path,body) { + return page.evaluate(async ({path,body})=>{ + const token=new URLSearchParams(location.hash.slice(1)).get('home_token'); + const r=await fetch(path,{method:body===undefined?'GET':'POST',headers:{'content-type':'application/json','x-elastos-home-token':token},body:body===undefined?undefined:JSON.stringify(body),signal:AbortSignal.timeout(30000)}); + if(!r.ok) throw new Error('product API '+path+' HTTP '+r.status); + return r.json(); + },{path,body}); + } + const [a,b]=homes; + const communityA=await a.app('chat-room'), communityB=await b.app('chat-room'); + result.stage='community_post'; + await communityA.locator('#message-input:enabled').waitFor({timeout:90000}); + await api(communityB, '/api/apps/chat-room/session/start', {}); + await communityB.locator('#message-input:enabled').waitFor({timeout:60000}); + const postText='Two Home Community '+crypto.randomUUID(); + const posted=await api(communityA, '/api/apps/chat-room/objects/send', {request_id:'two-home:'+crypto.randomUUID(),body:postText}); + let communityPoll; + await until('Community exact text at B', async()=>{ + communityPoll=await api(communityB, '/api/apps/chat-room/poll', {since:0,mark_read:false}); + return communityPoll.objects.some(row=>row.message_id===posted.message_id && row.body===postText); + }); + assertCommunityMessage(communityPoll, posted.message_id, postText); + await communityB.locator('#message-list .message-body').filter({hasText:postText}).waitFor({state:'visible',timeout:60000}); + assert.equal(await communityB.locator('#message-list .message-body').filter({hasText:postText}).innerText(),postText); + result.results.community_post='passed'; + result.community_text=postText; + await communityB.screenshot({path:join(evidence,'community-b-desktop.png'),fullPage:true}); + const profile=await api(b.people,'/api/apps/people/summary'); + assert(profile.profile_id,'B profile available'); + result.stage='contact_request'; + const request=await api(a.people,'/api/apps/people/contacts/request',{profile_id:profile.profile_id}); + result.observations.push({step:'contact_request',status:request.status}); + const inbox=await b.app('inbox'); + function action(value) { + if(!value||typeof value!=='object') return null; + if(typeof value.action_id==='string'&&value.action_id.startsWith('contact-accept-request:')) return value.action_id; + return Object.values(value).map(action).find(Boolean); + } + result.stage='contact_accept'; + const accept=await until('contact Inbox arrival',async()=>action(await api(inbox,'/api/apps/inbox/summary'))); + await api(inbox,'/api/apps/inbox/actions',{action_id:accept}); + result.observations.push({step:'contact_accept',status:'accepted'}); + const ac=await a.app('chat-room'), bc=await b.app('chat-room'); + result.stage='direct_conversation'; + const conversation=await until('A contact conversation',async()=> (await api(ac,'/api/apps/chat-room/direct/conversations')).conversations.find(c=>!c.removed)); + await until('B contact conversation',async()=> (await api(bc,'/api/apps/chat-room/direct/conversations')).conversations.find(c=>c.conversation_id===conversation.conversation_id)); + const text='Two Home direct '+crypto.randomUUID(); + result.stage='direct_send'; + const sent=await api(ac,'/api/apps/chat-room/direct/messages/send',{request_id:'two-home:'+crypto.randomUUID(),conversation_id:conversation.conversation_id,text}); + result.observations.push({step:'direct_send',status:sent.status,text}); + const path='/api/apps/chat-room/direct/conversations/'+encodeURIComponent(conversation.conversation_id)+'/messages'; + result.stage='direct_receive'; + let receivedMessages; + const received=await until('B direct exact text',async()=> { + receivedMessages=(await api(bc,path)).messages; + return receivedMessages.find(m=>m.text===text&&m.direction==='incoming'&&m.delivery_state==='received'); + }); + assertReceivedDirect(receivedMessages, received.message_id, text); + result.results.direct_received='passed'; + const senderMessage=async expected=> (await api(ac,path)).messages.find(m=>m.text===expected&&m.direction==='outgoing'); + const settled=await until('A settled receipt',async()=> { + const message=await senderMessage(text); + return message?.delivery_state==='receipt_settled' && message; + }); + const direct=await a.app('chat-room',{conversation_id:conversation.conversation_id}); + async function detail(expected) { + const row=direct.locator('#message-list li.message.self-message').filter({has:direct.locator('.message-body').filter({hasText:expected})}); + await row.waitFor({state:'visible',timeout:60000}); + assert.equal(await row.locator('.message-body').innerText(),expected); + return row.locator('.message-meta > span').last().innerText(); + } + const senderDetail=await until('A delivered time',async()=>{ + const value=await detail(text); + return /^\d{2}:\d{2}$/.test(value)&&value; + }); + assertSenderDelivery(settled,senderDetail); + result.results.sender_delivered='passed'; + result.direct={text,recipient_state:received.delivery_state,sender_state:settled.delivery_state,sender_detail:senderDetail}; + result.stage='offline_pending'; + // Close B's projections before stopping its owned Runtime; retain only its passkey context. + for(const page of b.context.pages()) if(page!==b.page) await page.close(); + await runtime('stop_b'); + const offlineText='Two Home recovery '+crypto.randomUUID(); + const pendingSend=await api(ac,'/api/apps/chat-room/direct/messages/send',{request_id:'two-home:'+crypto.randomUUID(),conversation_id:conversation.conversation_id,text:offlineText}); + assert.equal(pendingSend.status,'pending'); + let pending; + await until('A Sending line',async()=>{ + pending=await senderMessage(offlineText); + return pending?.delivery_state==='pending' && await detail(offlineText)==='Sending'; + }); + const pendingObservations=[]; + for(let sample=0;sample<3;sample++) { + pending=await senderMessage(offlineText); + const value=await detail(offlineText); + assert.equal(pending.delivery_state,'pending'); + assert.equal(value,'Sending'); + assert.throws(()=>assertSenderDelivery(pending,value),{name:'AssertionError'}); + pendingObservations.push({api_state:pending.delivery_state,ui_detail:value}); + await sleep(500); + } + result.negative_controls.sender_delivered={assertion_rejected:true,broken_input:'B stopped before send',...pendingObservations.at(-1),samples:pendingObservations.length,observations:pendingObservations}; + result.results.offline_pending='passed'; + result.stage='restart_delivery'; + await runtime('start_b'); + await b.page.reload({waitUntil:'domcontentloaded'}); + await b.page.waitForFunction(()=>document.body.dataset.homeStatus==='ready'||document.querySelector('#home-unlock')?.hidden===false,null,{timeout:60000}); + if(await b.page.locator('#home-unlock-primary').isVisible()) await b.page.locator('#home-unlock-primary').click(); + await b.page.waitForFunction(()=>document.body.dataset.homeStatus==='ready'&&document.body.dataset.homeAuthority==='signed',null,{timeout:60000}); + const restarted=await b.app('chat-room',{conversation_id:conversation.conversation_id}); + const recovered=await until('B receives after restart',async()=> (await api(restarted,path)).messages.find(m=>m.text===offlineText&&m.direction==='incoming'&&m.delivery_state==='received')); + const settledAfterRestart=await until('A receipt settles after restart',async()=>{ + const message=await senderMessage(offlineText); + return message?.delivery_state==='receipt_settled' && message; + }); + const recoveredDetail=await until('A time replaces Sending',async()=> { + const value=await detail(offlineText); + return /^\d{2}:\d{2}$/.test(value) && value; + }); + assertReceivedDirect([recovered],recovered.message_id,offlineText); + assertSenderDelivery(settledAfterRestart,recoveredDetail); + result.recovery={text:offlineText,recipient_state:recovered.delivery_state,sender_state:settledAfterRestart.delivery_state,sender_detail:recoveredDetail}; + result.results.restart_delivery='passed'; + await direct.screenshot({path:join(evidence,'direct-a-desktop.png'),fullPage:true}); + await direct.setViewportSize({width:390,height:844}); + await direct.screenshot({path:join(evidence,'direct-a-phone.png'),fullPage:true}); + assert.equal(result.browser_cross_home_http_requests,0,'browser calls only its own Home; peer delivery belongs to Carrier'); + result.status='passed'; + } catch(error) { + result.status='failed'; + result.failure=['AssertionError','TimeoutError'].includes(error.name)?error.name:'journey_failed'; + writeFileSync(join(control,'error.private.txt'),String(error.stack),{mode:0o600}); + process.exitCode=1; + } finally { + if(browser) await browser.close(); + writeFileSync(join(evidence,'two-home-ui.json'),JSON.stringify(result,null,2)+'\n',{mode:0o600}); + console.log(JSON.stringify({stage:result.stage,status:result.status})); + } + +} +if (process.argv[1] === fileURLToPath(import.meta.url)) await run(); diff --git a/scripts/ci-installed-two-home-test.py b/scripts/ci-installed-two-home-test.py new file mode 100644 index 000000000..218a5e48c --- /dev/null +++ b/scripts/ci-installed-two-home-test.py @@ -0,0 +1,268 @@ +#!/usr/bin/env python3 +"""Receipt guards for the installed two-Home journey; no Runtime or browser mocks.""" +import copy +import json +from pathlib import Path +import runpy +import os +import stat +import subprocess +import sys +import time +import signal +import tempfile +import unittest +from unittest import mock + +ROOT = Path(__file__).resolve().parent.parent +JOURNEY = runpy.run_path(str(ROOT / 'scripts/ci-installed-journeys.py')) + + +class TwoHomeReceiptTests(unittest.TestCase): + def test_fresh_home_and_data_are_private_with_the_operator_umask(self): + fresh = JOURNEY['fresh_fixture'] + with tempfile.TemporaryDirectory() as temporary: + home = Path(temporary).resolve()/'source' + data = home/'.local/share/elastos' + data.mkdir(parents=True) + (data/'components.json').write_text('{}') + prior = os.umask(0o022) + try: + with mock.patch.dict(fresh.__globals__, {'engine_paths':lambda _: (None,None,None,data/'missing-engine')}): + destination = Path(temporary).resolve()/'fresh' + copied = fresh(home,data,destination,include_engine=False) + self.assertEqual(stat.S_IMODE(destination.stat().st_mode),0o700) + self.assertEqual(stat.S_IMODE(copied.stat().st_mode),0o700) + finally: + os.umask(prior) + + def test_source_home_jobs_run_and_require_the_message_receipt(self): + source = (ROOT/'.github/workflows/ci.yml').read_text() + linux = source.split('\n source-home-linux:\n',1)[1].split('\n source-home-',1)[0] + self.assertLess(linux.index('run: scripts/ci-installed-journeys.sh model-repair'), + linux.index('run: scripts/ci-installed-journeys.sh two-home-messages')) + for job in ('source-home-linux', 'source-home-macos'): + block = source.split('\n '+job+':\n', 1)[1].split('\n source-home-', 1)[0] + self.assertIn('run: scripts/ci-installed-journeys.sh two-home-messages', block) + summary = block.split('name: summarize installed model journey', 1)[1] + self.assertIn("CI_TWO_HOME_MESSAGES: 'true'", summary) + self.assertNotIn('continue-on-error:', block.split('name: installed two-Home Community',1)[1].split(' - name:',1)[0]) + + def identity(self): + return {'candidate':'c'*40, 'source_tree':'d'*40, 'installed_runtime_sha256':'a'*64, + 'source_components_sha256':'b'*64, 'installation_receipt_sha256':'e'*64} + + def receipt(self): + identity = self.identity() + return {**identity, 'status':'passed', 'fixture_removed':True, 'network_arrangement':'same-host', + 'homes':[{'home':name, 'installed_runtime_sha256':identity['installed_runtime_sha256'], + 'carrier_bind_address':f'127.0.0.1:{1025+index}'} + for index,name in enumerate(('a','b','community'))], + 'process_cleanup':[{'status':'passed', 'after':{'runtime':0}} for _ in range(5)], + 'browser_cross_home_http_requests':0, + 'direct_endpoint_observations':[{'home':name,'ticket_addresses':[{'Ip':f'127.0.0.1:{1025+index}'}]} + for index,name in ((2,'community'),(2,'community'),(0,'a'),(1,'b'),(1,'b'))], + 'results':{name:'passed' for name in ('community_post','direct_received','sender_delivered', + 'offline_pending','restart_delivery')}, + 'negative_controls':{name:{'assertion_rejected':True, 'api_state':'pending', 'ui_detail':'Sending'} + for name in ('sender_delivered',)}} + + def test_acceptance_refuses_stale_identity_unchecked_failure_controls_or_surviving_processes(self): + valid = self.receipt() + check = JOURNEY['two_home_receipt_valid'] + self.assertTrue(check(valid, self.identity())) + for key in self.identity(): + changed = copy.deepcopy(valid) + changed[key] = 'stale' + self.assertFalse(check(changed, self.identity()), key) + for key, value in [('status','failed'), ('failure',{'phase':'late failure'}), ('browser_failure','failed'), + ('fixture_removed',False), ('homes',[]), ('process_cleanup',[]), + ('browser_cross_home_http_requests',1), ('direct_endpoint_observations',[])]: + changed = copy.deepcopy(valid) + changed[key] = value + self.assertFalse(check(changed, self.identity()), key) + for field in ('results','negative_controls'): + for name in valid[field]: + changed = copy.deepcopy(valid) + del changed[field][name] + self.assertFalse(check(changed, self.identity()), name) + changed = copy.deepcopy(valid) + changed['process_cleanup'][0]['after']['runtime'] = 1 + self.assertFalse(check(changed, self.identity())) + changed = copy.deepcopy(valid) + changed['negative_controls']['sender_delivered']['api_state'] = 'receipt_settled' + self.assertFalse(check(changed, self.identity())) + changed = copy.deepcopy(valid) + changed['homes'][1]['home'] = 'a' + self.assertFalse(check(changed, self.identity())) + + def test_network_arrangement_and_each_observed_ticket_must_match_configuration(self): + check = JOURNEY['two_home_receipt_valid'] + valid = self.receipt() + changed = copy.deepcopy(valid) + changed['network_arrangement'] = 'two-nat' + self.assertFalse(check(changed, self.identity())) + self.assertFalse(check(valid, self.identity(), 'two-nat')) + for index in range(5): + for address in ({'Relay':'https://relay.invalid'}, {'Ip':'192.0.2.1:1234'}, {'Ip':'127.0.0.1:1234'}): + changed = copy.deepcopy(valid) + changed['direct_endpoint_observations'][index]['ticket_addresses'] = [address] + self.assertFalse(check(changed, self.identity()), (index, address)) + changed = copy.deepcopy(valid) + changed['homes'][0]['carrier_bind_address'] = '192.0.2.1:1234' + self.assertFalse(check(changed, self.identity())) + + def test_fixture_removal_refuses_nested_home_process(self): + remove = JOURNEY['remove_message_fixture'] + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary).resolve()/'fixture' + root.mkdir() + row = {'command':str(root/'b/data/bin/elastos')+' gateway'} + with mock.patch.dict(remove.__globals__, {'process_rows':lambda: {123:row}}): + with self.assertRaises(AssertionError): + remove(root) + self.assertTrue(root.exists()) + with mock.patch.dict(remove.__globals__, {'process_rows':lambda: {}}): + remove(root) + self.assertFalse(root.exists()) + + def test_failed_stop_and_disk_check_finish_other_stops_remove_keys_and_write_failure_receipt(self): + finish = JOURNEY['finish_message_fixture'] + for disk_fails in (False, True): + with self.subTest(disk_fails=disk_fails), tempfile.TemporaryDirectory() as temporary: + root = Path(temporary).resolve()/'fixture' + root.mkdir() + (root/'authority.key').write_text('disposable test key') + evidence = Path(temporary).resolve()/'evidence' + evidence.mkdir() + stopped = [] + children = {'a':mock.Mock(pid=101), 'b':mock.Mock(pid=102), 'community':mock.Mock(pid=103)} + def stop(name): + stopped.append(name) + if name == 'a': + raise RuntimeError('injected stop failure') + del children[name] + def disk(_): + if disk_fails: + raise RuntimeError('injected disk failure') + return {'free':123} + fallback = mock.Mock() + with mock.patch.dict(finish.__globals__, {'process_rows':lambda: {}, + 'stop_message_processes':fallback, 'require_disk_space':disk}): + with self.assertRaisesRegex(RuntimeError, 'cleanup failed'): + finish(root, root, evidence, {'status':'passed'}, children, + {name:(root,root) for name in children}, stop, None, {}, time.monotonic()) + self.assertEqual(stopped, ['a','b','community']) + fallback.assert_called_once() + self.assertFalse(root.exists()) + receipt = json.loads((evidence/'installed-journeys.json').read_text()) + self.assertEqual(receipt['status'], 'failed') + self.assertTrue(receipt['fixture_removed']) + self.assertEqual(len(receipt['cleanup_failures']), 2 if disk_fails else 1) + self.assertNotIn('disposable test key', json.dumps(receipt)) + + def test_work_deadline_interrupts_a_hung_phase_and_restores_the_timer(self): + before = signal.getsignal(signal.SIGALRM) + with self.assertRaises(JOURNEY['MessageDeadline']): + with JOURNEY['message_timeout'](.05): + time.sleep(1) + self.assertEqual(signal.getitimer(signal.ITIMER_REAL), (0,0)) + self.assertEqual(signal.getsignal(signal.SIGALRM), before) + + def test_deadline_cannot_be_swallowed_by_an_oserror_handler(self): + swallowed = [] + with self.assertRaises(BaseException) as raised: + with JOURNEY['message_timeout'](.05): + try: + time.sleep(.2) + except OSError: + swallowed.append(True) + self.assertFalse(swallowed) + self.assertNotIsInstance(raised.exception, OSError) + + def test_signal_census_cost_does_not_grow_with_process_count(self): + stop = JOURNEY['stop_message_processes'] + counts = [] + for count in (2,20): + rows = {pid:{'ppid':0,'start':'fixture birth','command':'fixture child'} + for pid in range(100,100+count)} + census = mock.Mock(return_value=rows) + with mock.patch.dict(stop.__globals__, {'process_rows':census}), mock.patch.object(os,'kill'): + with self.assertRaises(AssertionError): + stop(mock.Mock(pid=100), rows.copy(), grace=0, killed=0) + counts.append(census.call_count) + self.assertEqual(counts[0], counts[1]) + self.assertLessEqual(counts[0], 4) + + def test_sigkill_fallback_stops_a_detached_browser_after_node_exits(self): + stop = JOURNEY['stop_message_processes'] + with tempfile.TemporaryDirectory() as temporary: + pidfile = Path(temporary).resolve()/'browser.pid' + marker = '--two-home-fixture='+str(Path(temporary).resolve()) + termfile = Path(temporary).resolve()/'term-observed' + detached_code = ('import os,signal,sys,time; from pathlib import Path; ' + 'signal.signal(signal.SIGTERM,lambda *_: Path(sys.argv[3]).write_text("TERM")); ' + 'Path(sys.argv[2]).write_text(str(os.getpid())); time.sleep(60)') + parent_code = ('import subprocess,sys,time; from pathlib import Path\n' + 'subprocess.Popen([sys.executable,"-c",sys.argv[2],sys.argv[3],sys.argv[1],sys.argv[4]],start_new_session=True)\n' + 'while not Path(sys.argv[1]).exists(): time.sleep(.01)') + parent = subprocess.Popen([sys.executable,'-c',parent_code,str(pidfile),detached_code,marker,str(termfile)]) + detached = None + try: + parent.wait(timeout=5) + detached = int(pidfile.read_text()) + # Node has already exited; the fixture marker identifies Chromium. + with JOURNEY['message_timeout'](10): + stop(parent, {}, marker, grace=.2, killed=2) + self.assertTrue(termfile.exists(), 'detached browser received TERM and required KILL') + rows = JOURNEY['process_rows']() + self.assertTrue(detached not in rows or rows[detached]['command'].endswith(' ')) + finally: + if detached: + try: + os.kill(detached, signal.SIGKILL) + except ProcessLookupError: + pass + if parent.poll() is None: + parent.kill() + parent.wait() + + def test_source_identity_rejects_changed_runtime_and_browser_asset_before_launch(self): + identify = JOURNEY['installed_message_identity'] + with tempfile.TemporaryDirectory() as temporary: + data = Path(temporary).resolve() + (data/'bin').mkdir() + (data/'receipts').mkdir() + runtime = data/'bin/elastos' + runtime.write_bytes(b'fixture runtime') + manifest = {'capsules':{}} + for name in ('home','home-gui','people','inbox','chat-room'): + directory = data/'capsules'/name + directory.mkdir(parents=True) + (directory/'index.html').write_text('fixture page') + manifest['capsules'][name] = {'entrypoint':'index.html', + 'entrypoint_sha256':'sha256:'+JOURNEY['digest'](directory/'index.html'), + 'browser_assets':[{'path':'index.html','sha256':'sha256:'+JOURNEY['digest'](directory/'index.html')}]} + (data/'components.json').write_text(json.dumps(manifest)) + (data/'receipts/source-home-capsules.json').write_text('{}') + sha = 'sha256:'+JOURNEY['digest'](runtime) + receipt = {'source':{'commit':'c'*40,'tree':'d'*40,'clean':True}, + 'runtime':{'built_sha256':sha,'installed_sha256':sha,'parity':True}, + 'components_sha256':'sha256:'+JOURNEY['digest'](data/'components.json'), + 'source_home_capsule_metadata_receipt_sha256':'sha256:'+JOURNEY['digest'](data/'receipts/source-home-capsules.json')} + (data/'receipts/source-home-installation.json').write_text(json.dumps(receipt)) + def observe(): + with mock.patch.object(identify.__globals__['subprocess'], 'check_output', side_effect=['c'*40,'d'*40]): + return identify(data) + self.assertEqual(observe()['candidate'], 'c'*40) + (data/'capsules/chat-room/index.html').write_text('stale UI') + with self.assertRaises(AssertionError): + observe() + (data/'capsules/chat-room/index.html').write_text('fixture page') + runtime.write_bytes(b'stale binary') + with self.assertRaises(AssertionError): + observe() + + +if __name__ == '__main__': + unittest.main() diff --git a/scripts/ci-installed-two-home.test.mjs b/scripts/ci-installed-two-home.test.mjs new file mode 100644 index 000000000..53f2429e4 --- /dev/null +++ b/scripts/ci-installed-two-home.test.mjs @@ -0,0 +1,37 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import {assertCommunityMessage, assertReceivedDirect, assertSenderDelivery, assertMessageOrigins} from './ci-installed-two-home-journey.mjs'; + +test('Community proof rejects wrong text, wrong identity and the receiving Home own post', () => { + const message = {message_id:'post:one', body:'fresh post', from_current_session:false}; + assertCommunityMessage({objects:[message]}, 'post:one', 'fresh post'); + for (const broken of [{body:'other text'}, {message_id:'post:two'}, {from_current_session:true}]) { + assert.throws(() => assertCommunityMessage({objects:[{...message,...broken}]}, 'post:one', 'fresh post'), assert.AssertionError); + } +}); + +test('Direct receive proof requires the exact incoming message and received state', () => { + const message = {message_id:'dm:one', text:'fresh direct', direction:'incoming', delivery_state:'received'}; + assertReceivedDirect([message], 'dm:one', 'fresh direct'); + for (const broken of [{text:'other text'}, {message_id:'dm:two'}, {direction:'outgoing'}, {delivery_state:'pending'}]) { + assert.throws(() => assertReceivedDirect([{...message,...broken}], 'dm:one', 'fresh direct'), assert.AssertionError); + } +}); + +test('Sender delivered requires both the settled API state and the exact message time', () => { + const message = {direction:'outgoing', delivery_state:'receipt_settled', created_at:42}; + assertSenderDelivery(message, '00:00'); + for (const detail of ['Sending', 'Expired', '', 'Delivered', '01:00']) { + assert.throws(() => assertSenderDelivery(message, detail), assert.AssertionError); + } + for (const state of ['pending','expired','received']) { + assert.throws(() => assertSenderDelivery({...message,delivery_state:state}, '00:00'), assert.AssertionError); + } +}); + +test('Home origins use the arrangement browser host and reject a mismatch', () => { + assertMessageOrigins(['http://localhost/','http://localhost/'], 'localhost'); + assertMessageOrigins(['http://127.0.0.1/','http://127.0.0.1/'], '127.0.0.1'); + assert.throws(()=>assertMessageOrigins(['http://localhost/','http://127.0.0.1/'], 'localhost'), assert.AssertionError); + assert.throws(()=>assertMessageOrigins(['http://localhost/'], undefined), assert.AssertionError); +}); diff --git a/scripts/test-source-gate.sh b/scripts/test-source-gate.sh index 86aedf932..ecfaddd9e 100755 --- a/scripts/test-source-gate.sh +++ b/scripts/test-source-gate.sh @@ -40,8 +40,9 @@ product_ui() { } release() { - node --test scripts/ci-model-package.test.mjs scripts/ci-installed-home-failure.test.mjs + node --test scripts/ci-model-package.test.mjs scripts/ci-installed-home-failure.test.mjs scripts/ci-installed-two-home.test.mjs python3 scripts/ci-release-policy-test.py + python3 scripts/ci-installed-two-home-test.py python3 scripts/ci-download-cache-test.py python3 scripts/components-release-integrity-check.py --self-test python3 scripts/publish-platform-artifacts-test.py