AtlatestRepositorysigil-ui

sigil-ui / tree / scripts / macostest-packet.sgl

1;;; Linux-only controlled tool model. Never a real Mac behavior receipt.
2(import (sigil shell prelude))
3(requires (tools sigil chmod cp sha256sum))
4(define packet (realpath (arg 1)))
5(define work (arg 2))
6(define cli (or (getenv "SIGIL") (tool-path 'sigil)))
7(unless (and (starts-with? work "/") (not (file-exists? work)))
8 (error "Expected a fresh absolute model directory"))
9(define bin (path work "bin"))
10(define logs (path work "logs"))
11(ensure-directory bin)
12(ensure-directory logs)
13(define (shim name body)
14 (define file (path bin name))
15 (write-text file (str "#!" cli "\n(import (sigil shell prelude))\n" body "\n"))
16 ($ chmod 700 ,file))
17(shim "uname" "(println \"~a\" (if (equal? (arg 1) \"-s\") \"Darwin\" \"arm64\"))")
18(shim "sw_vers" "(println \"15.6.1\")")
19(shim "shasum" "($ sha256sum ,@(cddr (script-args)))")
20(shim "open" "(error \"Linux model must never launch an application\")")
21(shim "xattr" "(error \"Read-only plan must never modify attributes\")")
22(define environment (dict "PATH" (str bin ":" (getenv "PATH"))))
23(define pin (car (regex-split "\\s+" (out sha256sum ,(path packet "SHA256SUMS")))))
24(define destination (path work "run"))
25(define (check name directory expected success? message (opts: (execute? #f)))
26 (define result
27 ($? ,cli ,(path directory "qualify.sgl") ,destination ,expected
28 ,(if execute? "--execute" "--plan") env: ,environment err: 'merge timeout: 90))
29 (write-text (path logs (str name ".log"))
30 (str "Linux model only: controlled uname/sw_vers; no Mac accessed.\n"
31 (stdout-of result) "\n" (format-outcome result) "\n"))
32 (unless (and (eq? success? (ok? result)) (contains? (stdout-of result) message)
33 (eq? execute? (file-exists? destination)))
34 (error "Packet model failed" name (stdout-of result)))
35 (println "PASS: Linux packet model ~a" name))
36(check "valid" packet pin #t "33 exact hashes")
37(check "external-pin" packet (make-string 64 #\0) #f "independently supplied SHA-256")
38(define altered (path work "altered"))
39($ cp -R ,packet ,altered)
40($ chmod "644" ,(path altered "ui-drawing-aarch64"))
41($ sha256sum --quiet -c SHA256SUMS cwd: ,altered)
42(check "non-executable-fixture" altered pin #f "Fixture is not executable")
43($ chmod "755" ,(path altered "ui-drawing-aarch64"))
44(write-text (path altered "font/fixtures/JetBrainsMono-Regular.ttf") "tampered")
45(check "payload" altered pin #f "input hash mismatch")
46($ cp ,(path packet "font/fixtures/JetBrainsMono-Regular.ttf")
47 ,(path altered "font/fixtures/JetBrainsMono-Regular.ttf"))
48(define rows (split (read-text (path altered "SHA256SUMS") trim: #t) "\n"))
49(write-text (path altered "SHA256SUMS")
50 (str (string-join (cons (car rows) (cons (car rows) (cddr rows))) "\n") "\n"))
51(define changed-pin (car (regex-split "\\s+" (out sha256sum ,(path altered "SHA256SUMS")))))
52(check "duplicate-row" altered changed-pin #f "duplicate")
53;; Tamper only with the new scratch executable after the real copy. The
54;; pinned manifest must stop this before even the controlled open is called.
55(shim "cp"
56 (format "($ ~s ,@(script-args))\n(define target (car (reverse (script-args))))\n(when (ends-with? target \"/runtime-probe\") (write-text target \"changed scratch executable\"))"
57 (tool-path 'cp)))
58(shim "xattr"
59 (format "(unless (starts-with? (car (reverse (script-args))) ~s) (error \"Attribute target escaped owned model scratch\"))"
60 (str destination "/")))
61(check "copied-executable" packet pin #f "Copied executable differs from the pinned manifest" #t)
63;; Replace the six Mach-O images only in a separate, explicitly modeled packet.
64;; These controlled executables test direct-launch status/evidence handling.
65(define modeled (path work "modeled-packet"))
66($ cp -R ,packet ,modeled)
67(each name '("host-aarch64" "appkit-conformance" "webkit-aarch64" "initialization-aarch64"
68 "font-aarch64" "ui-drawing-aarch64")
69 (define executable (path modeled name))
70 (define count (if (member name '("font-aarch64" "ui-drawing-aarch64")) 2 1))
71 (write-text executable
72 (str "#!" cli "\n(import (sigil shell prelude))\n"
73 "(define fixture-name " (format "~s" name) ")\n"
74 "(unless (= " (str count) " (length (script-args))) (exit 2))\n"
75 "(define mode (getenv \"SIGIL_PACKET_MODEL_MODE\"))\n"
76 "(when (equal? mode \"pre-start\") (display \"argument admission failed\\n\") (exit 2))\n"
77 "(define run (arg 1))\n(define pid (process-id))\n"
78 "(write-text (path run \"runtime.log\") (format \"START pid=~a\\n\" pid))\n"
79 "(unless (equal? mode \"missing-result\")\n"
80 " (define hash (car (regex-split \"\\\\s+\" (out sha256sum ,(script-file)))))\n"
81 " (write-text (path run \"runtime.result\")\n"
82 " (format \"status=~a pid=~a sha256=~a\\n\"\n"
83 " (if (or (equal? mode \"status-mismatch\")\n"
84 " (and (equal? mode \"font-only-failure\") (equal? fixture-name \"font-aarch64\"))) 1 0)\n"
85 " (if (equal? mode \"pid-mismatch\") (+ pid 1) pid)\n"
86 " (if (equal? mode \"hash-mismatch\") (make-string 64 #\\0) hash))))\n"
87 "(when (equal? mode \"process-status\") (exit 2))\n"
88 "(when (and (equal? mode \"font-only-failure\") (equal? fixture-name \"font-aarch64\")) (exit 1))\n"))
89 ($ chmod 700 ,executable))
90(define modeled-names
91 (map (lambda (row) (cadr (regex-split "\\s+" row)))
92 (split (read-text (path modeled "SHA256SUMS") trim: #t) "\n")))
93(write-text (path modeled "SHA256SUMS")
94 (str (out sha256sum ,@modeled-names cwd: ,modeled) "\n"))
95(define modeled-pin
96 (car (regex-split "\\s+" (out sha256sum ,(path modeled "SHA256SUMS")))))
97(shim "cp" (format "($ ~s ,@(script-args))" (tool-path 'cp)))
98(shim "xattr"
99 (format "(unless (starts-with? (car (reverse (script-args))) ~s) (error \"Attribute target escaped model scratch\"))"
100 (str work "/")))
101(each control '(("direct-success" "pass" #t "PASS: AppKit, CoreText and native UI drawing")
102 ("pre-start-status-2" "pre-start" #f "no START line (exit status 2;")
103 ("pid-mismatch" "pid-mismatch" #f "exit/PID/hash evidence did not match")
104 ("hash-mismatch" "hash-mismatch" #f "exit/PID/hash evidence did not match")
105 ("status-mismatch" "status-mismatch" #f "exit/PID/hash evidence did not match")
106 ("process-status" "process-status" #f "exit/PID/hash evidence did not match")
107 ("missing-result" "missing-result" #f "exit/PID/hash evidence did not match")
108 ("font-failure-still-draws" "font-only-failure" #f "all fixtures attempted"))
109 (set! destination (path work (car control)))
110 (set! environment (dict-set environment "SIGIL_PACKET_MODEL_MODE" (cadr control)))
111 (check (car control) modeled modeled-pin (caddr control) (cadddr control) #t)
112 (unless (file? (path destination "ui-drawing-aarch64/launch.result"))
113 (error "Failed fixture prevented the remaining drawing launch" (car control)))
114 (when (equal? (car control) "font-failure-still-draws")
115 (unless (contains? (read-text (path destination "qualification.result"))
116 "font-aarch64 FAIL\nui-drawing-aarch64 PASS")
117 (error "Font failure did not retain an independent drawing PASS"))))
118(println "PASS: 14 Linux packet controls; controlled executables never prove Mac behavior")