AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-endpoint.sgl

1(import (sigil test)
2 (sigil process)
3 (sigil socket)
4 (sigil crypto)
5 (sigil quic bytes)
6 (sigil quic error)
7 (sigil quic frame)
8 (sigil quic packet)
9 (sigil quic connection)
10 (sigil quic stream)
11 (sigil quic endpoint))
13(define endpoint-test-original #u8(1 2 3 4 5 6 7 8))
14(define endpoint-test-client-id #u8(11 12 13 14 15 16 17 18))
16(define (call-with-endpoint-test-pair procedure window pin?)
17 (let* ((key (ecdsa-p256-generate-keypair))
18 (connection (make-quic-client (list #u8(113 49)) "example.test"
19 (stream-transport-parameters 'client receive-window: window)
20 (lambda (certificates name)
21 (and (equal? certificates (list #u8(48 1 0))) (cdr key)))
22 destination-id: endpoint-test-original local-id: endpoint-test-client-id))
23 (stream (make-quic-stream connection))
24 (client (open-quic-endpoint))
25 (server (open-quic-endpoint))
26 (pair (vector connection #f stream #f client server 0)))
27 (dynamic-wind
28 (lambda () #t)
29 (lambda ()
30 (quic-endpoint-accept! server
31 (lambda (original source)
32 (let* ((connection (make-quic-server (list #u8(113 49))
33 (stream-transport-parameters 'server receive-window: window)
34 (list #u8(48 1 0)) (car key) original source))
35 (stream (make-quic-stream connection)))
36 (vector-set! pair 1 connection)
37 (vector-set! pair 3 stream)
38 (vector-set! pair 6 (+ (vector-ref pair 6) 1))
39 connection))
40 peer-port: (and pin? (quic-endpoint-port client)))
41 (quic-endpoint-attach! client connection (quic-endpoint-port server))
42 (procedure pair))
43 (lambda ()
44 (quic-endpoint-close! client)
45 (quic-endpoint-close! server)))))
47(define (endpoint-test-await! pair predicate)
48 (let loop ((count 0))
49 (unless (predicate)
50 (when (= count 5000) (error "endpoint test: bounded pump exhausted"))
51 (quic-endpoint-step! (vector-ref pair 4))
52 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
53 (loop (+ count 1)))))
55(define (endpoint-test-ready! pair)
56 (endpoint-test-await! pair
57 (lambda ()
58 (and (quic-confirmed? (vector-ref pair 0)) (vector-ref pair 1)
59 (quic-confirmed? (vector-ref pair 1))))))
61(define (endpoint-test-pattern size)
62 (let ((bytes (make-bytevector size 0)))
63 (do ((i 0 (+ i 1))) ((= i size)) (bytevector-u8-set! bytes i (modulo i 256)))
64 bytes))
66(define (endpoint-test-initial (keys: (body #u8(1)) (destination endpoint-test-original)))
67 (let* ((keys (initial-keys destination 'client))
68 (header (encode-packet-header 'initial destination endpoint-test-client-id #u8() 0 4 17
69 length-width: 2))
70 (payload (bytes-append body
71 (make-bytevector (- 1200 (bytevector-length header) 16 (bytevector-length body)) 0))))
72 (dynamic-wind
73 (lambda () #t)
74 (lambda ()
75 (seal-quic-packet keys 'initial destination endpoint-test-client-id 0 payload
76 length-width: 2))
77 (lambda () (discard-packet-keys! keys)))))
79(define (endpoint-test-rebind! endpoint)
80 (let ((replacement (open-quic-endpoint port: (quic-endpoint-port endpoint))))
81 (dynamic-wind
82 (lambda () #t)
83 (lambda () (assert-equal (quic-endpoint-port endpoint) (quic-endpoint-port replacement)))
84 (lambda () (quic-endpoint-close! replacement)))))
86(test-group "QUIC private UDP endpoint"
87 (test "endpoint-control: real UDP handshake confirms both roles"
88 (call-with-endpoint-test-pair
89 (lambda (pair)
90 (endpoint-test-ready! pair)
91 (assert-equal (not (equal? (getenv "QUIC_ENDPOINT_RED") "1")) (quic-complete? (vector-ref pair 0)))
92 (assert-true (quic-complete? (vector-ref pair 1)))
93 (assert-equal 1 (vector-ref pair 6))
94 (assert-false (quic-endpoint-failure (vector-ref pair 4)))
95 (assert-false (quic-endpoint-failure (vector-ref pair 5)))) 1024 #f))
96 (test "endpoint-control: binary request and response survive UDP and flow control"
97 (call-with-endpoint-test-pair
98 (lambda (pair)
99 (endpoint-test-ready! pair)
100 (let ((request (endpoint-test-pattern 256)) (response (endpoint-test-pattern 65536))
101 (request-received #u8()) (response-received #u8()) (sent 0))
102 (assert-equal 256 (quic-stream-write! (vector-ref pair 2) request fin?: #t))
103 (endpoint-test-await! pair
104 (lambda ()
105 (set! request-received (bytes-append request-received (quic-stream-read! (vector-ref pair 3))))
106 (quic-stream-eof? (vector-ref pair 3))))
107 (assert-equal request request-received)
108 (endpoint-test-await! pair
109 (lambda ()
110 (unless (quic-stream-send-closed? (vector-ref pair 3))
111 (set! sent (+ sent (quic-stream-write! (vector-ref pair 3)
112 (bytevector-copy response sent) fin?: #t))))
113 (set! response-received (bytes-append response-received (quic-stream-read! (vector-ref pair 2))))
114 (quic-stream-eof? (vector-ref pair 2))))
115 (assert-equal 65536 sent)
116 (assert-equal (if (equal? (getenv "QUIC_ENDPOINT_RED") "1") (flip-byte response 0) response)
117 response-received))) 1024 #f))
118 (test "local ephemeral port and idempotent socket cleanup"
119 (let ((endpoint (open-quic-endpoint)))
120 (assert-true (<= 1 (quic-endpoint-port endpoint) 65535))
121 (quic-endpoint-close! endpoint)
122 (quic-endpoint-close! endpoint)
123 (assert-true (quic-endpoint-closed? endpoint))
124 (assert-false (quic-endpoint-step! endpoint))))
125 (test "invalid port and unbounded poll waits are rejected"
126 (assert-error (open-quic-endpoint port: -1))
127 (assert-error (open-quic-endpoint port: 65536))
128 (let ((endpoint (open-quic-endpoint)))
129 (dynamic-wind
130 (lambda () #t)
131 (lambda ()
132 (assert-error (quic-endpoint-step! endpoint wait-ms: 51))
133 (assert-error (quic-endpoint-step! endpoint wait-ms: -1))
134 (assert-false (quic-endpoint-closed? endpoint)))
135 (lambda () (quic-endpoint-close! endpoint)))))
136 (test "valid Initial from a different source port cannot claim a pinned endpoint"
137 (call-with-endpoint-test-pair
138 (lambda (pair)
139 (let ((attacker (udp-socket)))
140 (dynamic-wind
141 (lambda () #t)
142 (lambda ()
143 (assert-true (udp-bind attacker 0 "127.0.0.1"))
144 (assert-equal 1200 (udp-send attacker (endpoint-test-initial) "127.0.0.1"
145 (quic-endpoint-port (vector-ref pair 5))))
146 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
147 (assert-false (quic-endpoint-connection (vector-ref pair 5)))
148 (assert-equal 0 (vector-ref pair 6)))
149 (lambda () (socket-close attacker))))
150 (endpoint-test-ready! pair)
151 (assert-equal 1 (vector-ref pair 6))) 1024 #t))
152 (test "malformed and undersized datagrams cannot invoke server factory"
153 (call-with-endpoint-test-pair
154 (lambda (pair)
155 (let ((sender (udp-socket)))
156 (dynamic-wind
157 (lambda () #t)
158 (lambda ()
159 (assert-true (udp-bind sender 0 "127.0.0.1"))
160 (for-each
161 (lambda (bytes) (udp-send sender bytes "127.0.0.1" (quic-endpoint-port (vector-ref pair 5))))
162 (list #u8() #u8(1) (make-bytevector 1200 0)))
163 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
164 (assert-false (quic-endpoint-connection (vector-ref pair 5)))
165 (assert-equal 0 (vector-ref pair 6)))
166 (lambda () (socket-close sender))))
167 (endpoint-test-ready! pair)) 1024 #f))
168 (test "explicit endpoint close retires both connection and stream state"
169 (call-with-endpoint-test-pair
170 (lambda (pair)
171 (endpoint-test-ready! pair)
172 (quic-endpoint-close! (vector-ref pair 4))
173 (assert-true (quic-closed? (vector-ref pair 0)))
174 (assert-error (quic-stream-read! (vector-ref pair 2)))
175 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
176 (assert-true (quic-endpoint-closed? (vector-ref pair 5)))
177 (assert-true (quic-closed? (vector-ref pair 1)))) 1024 #f))
178 (test "handshake deadline closes the dedicated UDP socket"
179 (let* ((connection (make-quic-client (list #u8(113 49)) "example.test" '()
180 (lambda (certificates name) #f) handshake-timeout: 1))
181 (endpoint (open-quic-endpoint)) (blackhole (open-quic-endpoint)))
182 (dynamic-wind
183 (lambda () #t)
184 (lambda ()
185 (quic-endpoint-attach! endpoint connection (quic-endpoint-port blackhole))
186 (quic-endpoint-step! endpoint wait-ms: 2)
187 (assert-true (quic-endpoint-closed? endpoint))
188 (assert-true (quic-closed? connection))
189 (assert-equal 1 (quic-error-code (quic-failure connection))))
190 (lambda () (quic-endpoint-close! endpoint) (quic-endpoint-close! blackhole)))))
191 (test "factory exception closes its endpoint and preserves failure metadata"
192 (let* ((connection (make-quic-client (list #u8(113 49)) "example.test" '()
193 (lambda (certificates name) #f)))
194 (client (open-quic-endpoint)) (server (open-quic-endpoint)))
195 (dynamic-wind
196 (lambda () #t)
197 (lambda ()
198 (quic-endpoint-accept! server (lambda (original source) (error "intentional server factory failure")))
199 (quic-endpoint-attach! client connection (quic-endpoint-port server))
200 (quic-endpoint-step! client)
201 (assert-error (quic-endpoint-step! server wait-ms: 1))
202 (assert-true (quic-endpoint-closed? server))
203 (assert-true (error-object? (quic-endpoint-failure server))))
204 (lambda () (quic-endpoint-close! client) (quic-endpoint-close! server)))))
205 (test "endpoint refuses a second connection attachment"
206 (call-with-endpoint-test-pair
207 (lambda (pair)
208 (assert-error (quic-endpoint-attach! (vector-ref pair 4) (vector-ref pair 0)
209 (quic-endpoint-port (vector-ref pair 5))))) 1024 #f))
210 (test "ql3-endpoint: explicit socket close retires the retained connection cache"
211 (call-with-endpoint-test-pair
212 (lambda (pair)
213 (endpoint-test-ready! pair)
214 (let ((connection (vector-ref pair 0)) (endpoint (vector-ref pair 4)) (hooks 0))
215 (quic-on-close! connection (lambda () (set! hooks (+ hooks 1))))
216 (quic-endpoint-close! endpoint)
217 (assert-true (quic-endpoint-closed? endpoint))
218 (assert-true (quic-closed? connection))
219 (assert-false (quic-closing? connection))
220 (assert-equal 1 hooks)
221 (assert-true (quic-application-error? (quic-failure connection)))
222 (quic-endpoint-close! endpoint)
223 (assert-equal 1 hooks)
224 (endpoint-test-rebind! endpoint))) 1024 #f))
225 (test "ql3-endpoint: automatic socket retirement preserves failure and ends closing"
226 (call-with-endpoint-test-pair
227 (lambda (pair)
228 (endpoint-test-ready! pair)
229 (let ((connection (vector-ref pair 1)) (endpoint (vector-ref pair 5)) (hooks 0))
230 (quic-on-close! connection (lambda () (set! hooks (+ hooks 1))))
231 (quic-transport-close! connection code: 10 frame-type: 6 reason: "endpoint test close")
232 (let ((failure (quic-failure connection)))
233 (assert-true (quic-closing? connection))
234 (assert-false (quic-endpoint-step! endpoint))
235 (assert-true (quic-endpoint-closed? endpoint))
236 (assert-false (quic-closing? connection))
237 (assert-true (eq? failure (quic-failure connection)))
238 (assert-equal 1 hooks)
239 (endpoint-test-rebind! endpoint)))) 1024 #f))
240 (test "ql3-endpoint: failed admission discards its retained cache before slot reuse"
241 (call-with-endpoint-test-pair
242 (lambda (pair)
243 (let ((sender (udp-socket)) (endpoint (vector-ref pair 5)))
244 (dynamic-wind
245 (lambda () #t)
246 (lambda ()
247 (assert-true (udp-bind sender 0 "127.0.0.1"))
248 ;; Distinct DCID/key from the later live client's Initial PN 0.
249 (let ((packet (endpoint-test-initial destination: #u8(51 52 53 54 55 56 57 58) body:
250 (encode-frames
251 (list (make-quic-frame 6
252 (list (cons 'offset 0) (cons 'data #u8(1 0 0 0)))))
253 'initial 'client))))
254 (assert-equal 1200 (udp-send sender packet "127.0.0.1" (quic-endpoint-port endpoint))))
255 (quic-endpoint-step! endpoint wait-ms: 1)
256 (let ((failed (vector-ref pair 1)))
257 (assert-equal 1 (vector-ref pair 6))
258 (assert-true (quic-closed? failed))
259 (assert-false (quic-closing? failed))
260 (assert-false (quic-endpoint-closed? endpoint))
261 (assert-false (quic-endpoint-connection endpoint))
262 (endpoint-test-ready! pair)
263 (assert-equal 2 (vector-ref pair 6))
264 (assert-false (eq? failed (vector-ref pair 1)))
265 (assert-false (quic-closing? failed))))
266 (lambda () (socket-close sender))))) 1024 #f))
267 (test "ql3-endpoint: unattached endpoint close releases its port and remains idempotent"
268 (let ((endpoint (open-quic-endpoint)))
269 (dynamic-wind
270 (lambda () #t)
271 (lambda ()
272 (quic-endpoint-close! endpoint)
273 (quic-endpoint-close! endpoint)
274 (assert-true (quic-endpoint-closed? endpoint))
275 (assert-false (quic-endpoint-step! endpoint))
276 (assert-false (quic-endpoint-connection endpoint))
277 (endpoint-test-rebind! endpoint))
278 (lambda () (quic-endpoint-close! endpoint)))))
279 (test "ql3-endpoint: live admission retains its connection until explicit retirement"
280 (call-with-endpoint-test-pair
281 (lambda (pair)
282 (endpoint-test-ready! pair)
283 (assert-true (eq? (vector-ref pair 1) (quic-endpoint-connection (vector-ref pair 5))))
284 (assert-false (quic-closed? (vector-ref pair 0)))
285 (assert-false (quic-closed? (vector-ref pair 1)))
286 (assert-false (quic-endpoint-closed? (vector-ref pair 4)))
287 (assert-false (quic-endpoint-closed? (vector-ref pair 5)))) 1024 #f)))