AtlatestRepositorysigil-quic

sigil-quic / tree / scriptsretry-cleanup-tests.sgl

1(import (sigil test)
2 (sigil io)
3 (sigil crypto)
4 (sigil quic bytes)
5 (sigil quic packet)
6 (sigil quic connection)
7 (sigil quic stream)
8 (sigil quic endpoint)
9 (sigil quic retry token)
10 (sigil quic tests retry spy)
11 (sigil quic tests retry fixture))
13;; Explicit remote-only observations of actual owned buffers. Predicates run
14;; before teardown; negative variants' stranded references are wiped afterward.
15(define (rc-wipe! bytes)
16 (do ((index 0 (+ index 1))) ((= index (bytevector-length bytes)))
17 (bytevector-u8-set! bytes index 0)))
19(define (rc-zero? bytes)
20 (let loop ((index 0))
21 (or (= index (bytevector-length bytes))
22 (and (= 0 (bytevector-u8-ref bytes index)) (loop (+ index 1))))))
24(define (rc-with-spy procedure)
25 (dynamic-wind
26 retry-spy-reset!
27 procedure
28 (lambda ()
29 (retry-spy-fail! #f)
30 (retry-spy-send-mode! #f)
31 (for-each
32 (lambda (entry)
33 (let ((manager (vector-ref entry 2)))
34 (when manager
35 (guard (exn (else #f)) (close-retry-token-manager! manager)))))
36 (retry-spy-owned))
37 (for-each
38 (lambda (candidate)
39 (when (quic-connection? candidate)
40 (guard (exn (else #f)) (quic-abort! candidate))))
41 (retry-spy-candidates))
42 (for-each (lambda (entry) (rc-wipe! (vector-ref entry 0))) (retry-spy-owned))
43 (retry-spy-reset!))))
45(define (rc-record)
46 (let ((entries (retry-spy-owned)))
47 (unless (= 1 (length entries)) (error "Retry lifetime fixture: expected one owned key"))
48 (car entries)))
50(define (rc-captured? entry)
51 (and (= 32 (bytevector-length (vector-ref entry 0))) (vector-ref entry 1)))
53(define (rc-live? entry)
54 (and (rc-captured? entry) (retry-token-manager? (vector-ref entry 2))
55 (not (rc-zero? (vector-ref entry 0)))))
57(define (rc-erased? entry)
58 (and (rc-captured? entry) (rc-zero? (vector-ref entry 0))))
60(define (rc-failure? stage procedure)
61 (let ((failure (guard (exn (else exn)) (procedure) #f)))
62 (and (error-object? failure)
63 (equal? "QL12 injected Retry failure" (error-object-message failure))
64 (equal? (list stage) (error-object-irritants failure)))))
66(define (rc-message? message procedure)
67 (let ((failure (guard (exn (else exn)) (procedure) #f)))
68 (and (error-object? failure) (equal? message (error-object-message failure)))))
70(define (rc-rebind? endpoint)
71 (let ((replacement (open-quic-endpoint port: (quic-endpoint-port endpoint))))
72 (quic-endpoint-close! replacement)
73 #t))
75(define (rc-closed? endpoint entry)
76 (and (quic-endpoint-closed? endpoint) (rc-erased? entry) (rc-rebind? endpoint)))
78(define (rc-candidate-closed? pair)
79 (let ((candidate (vector-ref pair 5)) (stream (vector-ref pair 6)))
80 (and candidate stream (quic-closed? candidate)
81 (memq candidate (retry-spy-candidates))
82 (guard (exn (else #t)) (quic-stream-read! stream) #f))))
84(define (rc-step! pair)
85 (quic-endpoint-step! (vector-ref pair 1) wait-ms: 1))
87(define (rc-positive procedure)
88 (assert-true (guard (exn (else #f)) (rc-with-spy procedure))))
90;; The operation is armed only after fixture setup, while its key is live.
91(define (rc-challenge-failure? stage)
92 (er-with-pair
93 (lambda (pair)
94 (let ((entry (rc-record)))
95 (and (rc-live? entry)
96 (begin
97 (retry-spy-fail! stage)
98 (and (rc-failure? stage (lambda () (er-challenge! pair)))
99 (= 0 (vector-ref pair 7))
100 (rc-closed? (vector-ref pair 1) entry))))))))
102(test-group "Private Retry key lifetime and ordinary unwind failures"
103 (test "ql12-retry-lifetime: unconfirmed endpoint close erases owned key and releases socket"
104 (rc-positive
105 (lambda ()
106 (er-with-pair
107 (lambda (pair)
108 (let ((entry (rc-record)) (endpoint (vector-ref pair 1)))
109 (and (rc-live? entry)
110 (begin
111 (quic-endpoint-close! endpoint)
112 (quic-endpoint-close! endpoint)
113 (and (= 0 (vector-ref pair 7)) (rc-closed? endpoint entry))))))))))
114 (test "ql12-retry-lifetime: manager close erases owned copy and preserves caller key"
115 (rc-positive
116 (lambda ()
117 (let ((borrowed (make-bytevector 32 90)))
118 (dynamic-wind
119 (lambda () #t)
120 (lambda ()
121 (let* ((manager (make-retry-token-manager key: borrowed)) (entry (rc-record)))
122 (and (rc-live? entry) (eq? manager (vector-ref entry 2))
123 (not (eq? borrowed (vector-ref entry 0)))
124 (begin
125 (close-retry-token-manager! manager)
126 (close-retry-token-manager! manager)
127 (and (rc-erased? entry) (bytes=? borrowed (make-bytevector 32 90)))))))
128 (lambda () (rc-wipe! borrowed)))))))
129 (test "ql12-retry-lifetime: next constructor operation failure wipes untransferred key"
130 (rc-positive
131 (lambda ()
132 (retry-spy-fail! 'manager-next)
133 (and (rc-failure? 'manager-next (lambda () (make-retry-token-manager)))
134 (let ((entry (rc-record)))
135 (and (not (vector-ref entry 2)) (rc-erased? entry)))))))
136 (test "ql12-retry-lifetime: setup failure after endpoint takes manager releases key and socket"
137 (rc-positive
138 (lambda ()
139 (let ((endpoint (open-quic-endpoint)))
140 (dynamic-wind
141 (lambda () #t)
142 (lambda ()
143 (retry-spy-fail! 'accept-after-manager)
144 (and (rc-failure? 'accept-after-manager
145 (lambda () (quic-endpoint-accept-retry! endpoint (lambda args #f))))
146 (rc-closed? endpoint (rc-record))))
147 (lambda () (quic-endpoint-close! endpoint)))))))
148 (test "ql12-retry-lifetime: confirmation retires key before explicit endpoint close"
149 (rc-positive
150 (lambda ()
151 (er-with-pair
152 (lambda (pair)
153 (let ((entry (rc-record)))
154 (and (rc-live? entry)
155 (begin
156 (er-ready! pair)
157 (and (not (quic-endpoint-closed? (vector-ref pair 1)))
158 (quic-confirmed? (vector-ref pair 0))
159 (quic-confirmed? (vector-ref pair 5)) (rc-erased? entry))))))))))
160 (test "ql12-retry-lifetime: tentative TLS failure retains key until replacement confirms"
161 (rc-positive
162 (lambda ()
163 (er-with-pair
164 (lambda (pair)
165 (let* ((entry (rc-record)) (challenge (er-challenge! pair))
166 (header (decode-packet-header challenge 0 0)) (packet (er-retried! pair challenge)))
167 (er-send! pair (er-forged header))
168 (rc-step! pair)
169 (and (= 1 (vector-ref pair 7)) (rc-candidate-closed? pair)
170 (not (quic-endpoint-connection (vector-ref pair 1))) (rc-live? entry)
171 (begin
172 (let drain ((count 0))
173 (when (and (< count 64) (er-read pair)) (drain (+ count 1))))
174 (er-send! pair packet)
175 (er-ready! pair)
176 (and (= 2 (vector-ref pair 7)) (rc-erased? entry))))))))))
177 (test "ql12-retry-lifetime: invalid token and Initial tag retain key until valid confirmation"
178 (rc-positive
179 (lambda ()
180 (er-with-pair
181 (lambda (pair)
182 (let* ((entry (rc-record)) (challenge (er-challenge! pair))
183 (header (decode-packet-header challenge 0 0)) (packet (er-retried! pair challenge))
184 (token (quic-header-token header)))
185 (and (er-drop? pair (er-forged header token: (flip-byte token (- (bytevector-length token) 1))))
186 (rc-live? entry)
187 (er-drop? pair (flip-byte packet (- (bytevector-length packet) 1)))
188 (rc-live? entry)
189 (begin
190 (er-send! pair packet)
191 (er-ready! pair)
192 (and (= 1 (vector-ref pair 7)) (rc-erased? entry))))))))))
193 (test "ql12-retry-lifetime: Retry ID random failure retires key and socket"
194 (rc-positive (lambda () (rc-challenge-failure? 'retry-id))))
195 (test "ql12-retry-lifetime: token nonce random failure retires key and socket"
196 (rc-positive (lambda () (rc-challenge-failure? 'token-nonce))))
197 (test "ql12-retry-lifetime: token mint MAC failure retires key and socket"
198 (rc-positive (lambda () (rc-challenge-failure? 'token-mac))))
199 (test "ql12-retry-lifetime: token validation MAC failure retires key before factory"
200 (rc-positive
201 (lambda ()
202 (er-with-pair
203 (lambda (pair)
204 (let ((entry (rc-record)) (packet (er-retried! pair (er-challenge! pair))))
205 (and (rc-live? entry)
206 (begin
207 (retry-spy-fail! 'token-mac)
208 (er-send! pair packet)
209 (and (rc-failure? 'token-mac (lambda () (rc-step! pair)))
210 (= 0 (vector-ref pair 7))
211 (rc-closed? (vector-ref pair 1) entry))))))))))
212 (test "ql12-retry-lifetime: Retry encoding failure retires key and socket"
213 (rc-positive (lambda () (rc-challenge-failure? 'retry-packet))))
214 (test "ql12-retry-lifetime: blocked send retains key and duplicate challenge later confirms"
215 (rc-positive
216 (lambda ()
217 (er-with-pair
218 (lambda (pair)
219 (let ((entry (rc-record))
220 (packet (car (quic-take-datagrams! (vector-ref pair 0) (er-now pair)))))
221 (retry-spy-send-mode! 'blocked)
222 (er-send! pair packet)
223 (rc-step! pair)
224 (and (er-empty? pair) (not (er-read pair)) (rc-live? entry)
225 (begin
226 (retry-spy-send-mode! #f)
227 (er-send! pair packet)
228 (rc-step! pair)
229 (let ((challenge (er-read pair)))
230 (and challenge (valid-retry-packet? er-original challenge)
231 (begin
232 (er-send! pair (er-retried! pair challenge))
233 (er-ready! pair)
234 (and (= 1 (vector-ref pair 7)) (rc-erased? entry)))))))))))))
235 (test "ql12-retry-lifetime: partial Retry send raises and retires key and socket"
236 (rc-positive
237 (lambda ()
238 (er-with-pair
239 (lambda (pair)
240 (let ((entry (rc-record)))
241 (and (rc-live? entry)
242 (begin
243 (retry-spy-send-mode! 'partial)
244 (and (rc-message? "QUIC endpoint: partial Retry datagram send"
245 (lambda () (er-challenge! pair)))
246 (= 0 (vector-ref pair 7))
247 (rc-closed? (vector-ref pair 1) entry))))))))))
248 (test "ql12-retry-lifetime: returned candidate failure aborts connection and retires key"
249 (rc-positive
250 (lambda ()
251 (er-with-pair
252 (lambda (pair)
253 (let ((entry (rc-record)) (packet (er-retried! pair (er-challenge! pair))))
254 (and (rc-live? entry)
255 (begin
256 (retry-spy-fail! 'candidate-after)
257 (er-send! pair packet)
258 (and (rc-failure? 'candidate-after (lambda () (rc-step! pair)))
259 (= 1 (vector-ref pair 7)) (rc-candidate-closed? pair)
260 (not (quic-endpoint-connection (vector-ref pair 1)))
261 (rc-closed? (vector-ref pair 1) entry))))))))))
262 (test "ql12-retry-lifetime: post-wipe close failure still releases socket and tentative connection"
263 (rc-positive
264 (lambda ()
265 (er-with-pair
266 (lambda (pair)
267 (let ((entry (rc-record)) (packet (er-retried! pair (er-challenge! pair)))
268 (endpoint (vector-ref pair 1)))
269 (er-send! pair packet)
270 (rc-step! pair)
271 (and (= 1 (vector-ref pair 7)) (vector-ref pair 5)
272 (eq? (vector-ref pair 5) (quic-endpoint-connection endpoint))
273 (not (quic-confirmed? (vector-ref pair 5))) (rc-live? entry)
274 (begin
275 (retry-spy-fail! 'close-after)
276 (and (rc-failure? 'close-after (lambda () (quic-endpoint-close! endpoint)))
277 (rc-candidate-closed? pair) (rc-closed? endpoint entry)))))))))))