AtlatestRepositorysigil-doc

sigil-doc / tree / testd4a-trusted-presentation.sgl

1(import (sigil core) (sigil fs) (sigil string)
2 (sigil doc) (sigil doc examples) (sigil doc verify)
3 (sigil doc presentation) (sigil doc publication) (sigil doc render html))
4
5(load "test/fixtures/d3-examples.sgd")
6(define source-document (eval 'document))
7(define examples (document-examples source-document))
8(define spec (car examples))
9(define cache-dir (make-temp-directory))
10(define effective-source (example-source spec))
11(define sigil-bin
12 (or (getenv "SIGIL_D1_COMPILER")
13 "../task-sigil-language-readers/build/dev/bin/sigil"))
14(define result
15 (verify-example source-document examples spec "."
16 sigil-bin cache-dir #t))
17(define presentation (make-verified-presentation spec result effective-source))
18(unless (verified-presentation-matches-spec? presentation spec)
19 (error "D4A genuine presentation did not match its exact specification"))
20(define document
21 (make-document 'chapter "Trusted result" '("Practical Sigil") (list spec)
22 (document-location source-document)))
23(define publication
24 (build-publication-index (list (make-publication-document "trusted" document))))
25(define context
26 (make-html-render-context publication "trusted"
27 (lambda (target) (string-append "#" (publication-target-id target)))
28 (list presentation) 2))
29(define html (document->html-fragment document context))
31(unless (and (string-contains? html "class=\"verification-status\">Verified")
32 (string-contains? html "<h2>Example: stdout</h2>")
33 (string-contains? html "class=\"example-output\">hello\n</pre>")
34 (string-contains? html "(display &quot;hello\\n&quot;)"))
35 (error "D4A trusted presentation did not render verified observation"))
37;; The verifier result is no longer the mutable vector used by the original D4
38;; bridge. Mutating defensive observations, the caller's source, or a
39;; presentation accessor cannot change the trusted presentation.
40(when (vector? result)
41 (error "D4A verifier exposed a mutable result vector"))
42(define result-source-copy (verify-result-effective-source result))
43(define result-stdout-copy (verify-result-stdout result))
44(define presentation-source-copy (verified-presentation-source presentation))
45(define presentation-stdout-copy (verified-presentation-stdout presentation))
46(string-set! result-source-copy 0 #\X)
47(string-set! result-stdout-copy 0 #\X)
48(string-set! presentation-source-copy 0 #\X)
49(string-set! presentation-stdout-copy 0 #\X)
50(string-set! effective-source 0 #\X)
51(define html-after-observation-mutation (document->html-fragment document context))
52(unless (and (string-contains? html-after-observation-mutation "class=\"example-output\">hello\n</pre>")
53 (string-contains? html-after-observation-mutation "(display &quot;hello\\n&quot;)"))
54 (error "D4A trusted presentation changed after caller-owned observation mutation"))
56;; A genuine result is bound to the exact effective source verified by the run.
57(guard (exn
58 (else
59 (unless (string=? (exception-message exn)
60 "make-verified-presentation: mismatched verified result")
61 (raise exn))))
62 (make-verified-presentation spec result "(display \"forged\\n\")")
63 (error "D4A trusted presentation accepted an unrelated effective source"))
65;; A genuine result may not be attached to a same-id spec with another
66;; expectation kind. Presentation data is accepted only through this bridge.
67(define wrong-spec
68 (make-example "stdout" 'inline "(+ 1 1)" "sigil" #f 'bytecode
69 'value "2" "" 5 'source+expected (example-location spec)))
70(when (verified-presentation-matches-spec? presentation wrong-spec)
71 (error "D4A presentation predicate accepted a same-id changed-source/kind spec"))
72(guard (exn
73 (else
74 (unless (string=? (exception-message exn)
75 "make-verified-presentation: mismatched verified result")
76 (raise exn))))
77 (make-verified-presentation wrong-spec result "(+ 1 1)")
78 (error "D4A trusted presentation accepted a mismatched result"))
80;; Matching id/kind is insufficient: the complete author specification is
81;; bound into the genuine result as well.
82(define changed-observation-spec
83 (make-example "stdout" 'inline "(display \"hello\\n\")" "sigil" #f 'bytecode
84 'stdout "forged\n" "" 30 'source+expected (example-location spec)))
85(when (verified-presentation-matches-spec? presentation changed-observation-spec)
86 (error "D4A presentation predicate accepted a same-id changed observation spec"))
87(guard (exn
88 (else
89 (unless (string=? (exception-message exn)
90 "make-verified-presentation: mismatched verified result")
91 (raise exn))))
92 (make-verified-presentation changed-observation-spec result (example-source spec))
93 (error "D4A trusted presentation accepted changed expected observation"))
95;; The authenticated cache record may reproduce a trusted result, but it must
96;; preserve the same source/specification binding and immutable observations.
97(define cached-result
98 (verify-example source-document examples spec "."
99 sigil-bin cache-dir #f))
100(define cached-presentation
101 (make-verified-presentation spec cached-result (example-source spec)))
102(unless (and (verify-result-cached? cached-result)
103 (verified-presentation-cached? cached-presentation)
104 (string=? (verified-presentation-stdout cached-presentation) "hello\n"))
105 (error "D4A genuine cached result did not preserve its trusted binding"))
107(delete-directory cache-dir)
108(display "D4A_TRUSTED_PRESENTATION_GREEN\n")