AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-connection.sgl

1(import (sigil test)
2 (sigil process)
3 (sigil crypto)
4 (sigil quic bytes)
5 (sigil quic error)
6 (sigil quic frame)
7 (sigil quic packet)
8 (sigil quic connection)
9 (sigil quic tls))
11;; Explicit synthetic pin authentication tests packet/TLS integration only.
12(define pump-original #u8(10 11 12 13 14 15 16 17))
13(define pump-client-id #u8(20 21 22 23 24 25 26 27))
14(define pump-server-id #u8(30 31 32 33 34 35 36 37))
16(define (pump-pair defer? accept?)
17 (let* ((key (ecdsa-p256-generate-keypair))
18 (client
19 (make-quic-client (list #u8(113 49)) "example.test" '()
20 (lambda (certificates name)
21 (and accept? (equal? name "example.test")
22 (equal? certificates (list #u8(48 1 0))) (cdr key)))
23 local-id: pump-client-id destination-id: pump-original defer-key-share?: defer?))
24 (server
25 (make-quic-server (list #u8(113 49)) '() (list #u8(48 1 0)) (car key)
26 pump-original pump-client-id local-id: pump-server-id)))
27 (vector client server)))
29(define (pump-deliver! pair side now reverse? duplicate?)
30 (let* ((packets (quic-take-datagrams! (vector-ref pair side) now))
31 (ordered (if reverse? (reverse packets) packets))
32 (target (vector-ref pair (- 1 side))))
33 (for-each
34 (lambda (packet)
35 (quic-receive! target packet now)
36 (when duplicate? (quic-receive! target packet now))) ordered)
37 packets))
39(define (pump-run! pair now reverse? duplicate?)
40 (do ((i 0 (+ i 1))) ((= i 8))
41 (pump-deliver! pair 0 now reverse? duplicate?)
42 (pump-deliver! pair 1 now reverse? duplicate?)))
44(define (pump-start! pair)
45 (quic-start! (vector-ref pair 0) 0))
47(define (pump-assert-complete pair)
48 (for-each
49 (lambda (connection)
50 (assert-false (quic-failure connection))
51 (assert-false (quic-closed? connection))
52 (assert-true (quic-complete? connection))
53 (assert-true (quic-confirmed? connection))) (vector->list pair)))
55(define (pump-control actual)
56 (assert-equal (not (equal? (getenv "QUIC_CONNECTION_RED") "1")) actual))
58(test-group "QUIC protected connection pump"
59 (test "connection-control: client completes a protected handshake"
60 (let ((pair (pump-pair #f #t)))
61 (pump-start! pair)
62 (pump-run! pair 0 #f #f)
63 (pump-control (quic-complete? (vector-ref pair 0)))))
64 (test "connection-control: server confirms a protected handshake"
65 (let ((pair (pump-pair #f #t)))
66 (pump-start! pair)
67 (pump-run! pair 0 #f #f)
68 (pump-control (quic-confirmed? (vector-ref pair 1)))))
69 (test "both roles authenticate and confirm with independent connection IDs"
70 (let ((pair (pump-pair #f #t)))
71 (pump-start! pair)
72 (pump-run! pair 0 #f #f)
73 (pump-assert-complete pair)
74 (assert-equal pump-server-id (quic-peer-id (vector-ref pair 0)))
75 (assert-equal pump-client-id (quic-peer-id (vector-ref pair 1)))))
76 (test "HelloRetryRequest traverses Initial packets then both roles confirm"
77 (let ((pair (pump-pair #t #t)))
78 (pump-start! pair)
79 (pump-run! pair 0 #f #f)
80 (pump-assert-complete pair)))
81 (test "reversed flights and duplicate packets preserve authentication"
82 (let ((pair (pump-pair #f #t)))
83 (pump-start! pair)
84 (pump-run! pair 0 #t #t)
85 (pump-assert-complete pair)))
86 (test "Initial datagrams are padded to 1200 bytes"
87 (let ((pair (pump-pair #f #t)))
88 (pump-start! pair)
89 (let ((packets (quic-take-datagrams! (vector-ref pair 0) 0)))
90 (assert-true (pair? packets))
91 (for-each (lambda (packet) (assert-equal 1200 (bytevector-length packet))) packets))))
92 (test "server drops undersized Initial before emitting a flight"
93 (let* ((pair (pump-pair #f #t))
94 (keys (initial-keys pump-original 'client))
95 (packet (seal-quic-packet keys 'initial pump-original pump-client-id 0 #u8(1))))
96 (assert-true (< (bytevector-length packet) 1200))
97 (quic-receive! (vector-ref pair 1) packet 0)
98 (assert-equal '() (quic-take-datagrams! (vector-ref pair 1) 0))
99 (assert-false (quic-closed? (vector-ref pair 1)))
100 (discard-packet-keys! keys)))
101 (test "server flight stays within three times attributable received bytes"
102 (let ((pair (pump-pair #f #t)))
103 (pump-start! pair)
104 (let* ((incoming (pump-deliver! pair 0 0 #f #f))
105 (outgoing (quic-take-datagrams! (vector-ref pair 1) 0)))
106 (assert-true (pair? outgoing))
107 (assert-true (<= (apply + (map bytevector-length outgoing))
108 (* 3 (apply + (map bytevector-length incoming))))))))
109 (test "dropping the first client Initial recovers at a fresh packet number"
110 (let ((pair (pump-pair #f #t)))
111 (pump-start! pair)
112 (let ((first (car (quic-take-datagrams! (vector-ref pair 0) 0))))
113 (quic-tick! (vector-ref pair 0) 1000)
114 (let ((retry (car (quic-take-datagrams! (vector-ref pair 0) 1000))))
115 (assert-false (bytes=? first retry))
116 (quic-receive! (vector-ref pair 1) retry 1000)))
117 (pump-run! pair 1000 #f #f)
118 (pump-assert-complete pair)))
119 (test "lost server flight recovers through new-PN CRYPTO retransmission"
120 (let ((pair (pump-pair #f #t)))
121 (pump-start! pair)
122 (pump-deliver! pair 0 0 #f #f)
123 (assert-true (pair? (quic-take-datagrams! (vector-ref pair 1) 0)))
124 (quic-tick! (vector-ref pair 0) 1000)
125 (quic-tick! (vector-ref pair 1) 1000)
126 (pump-run! pair 1000 #f #f)
127 (pump-assert-complete pair)))
128 (test "untrusted certificate closes the connection and prevents completion"
129 (let ((pair (pump-pair #f #f)))
130 (pump-start! pair)
131 (pump-run! pair 0 #f #f)
132 (assert-true (quic-closed? (vector-ref pair 0)))
133 (assert-false (quic-complete? (vector-ref pair 0)))
134 (assert-equal 296 (quic-error-code (quic-failure (vector-ref pair 0))))))
135 (test "handshake deadline retires keys and queued retransmission data"
136 (let ((pair (pump-pair #f #t)))
137 (pump-start! pair)
138 (quic-tick! (vector-ref pair 0) 10000)
139 (assert-true (quic-closed? (vector-ref pair 0)))
140 (quic-take-datagrams! (vector-ref pair 0) 10000)
141 (assert-equal '() (quic-take-datagrams! (vector-ref pair 0) 10000))
142 (assert-error (quic-send-frame! (vector-ref pair 0) (make-quic-frame 1 '())))))
143 (test "one corrupt protected PN byte on every Initial attempt defeats handshake"
144 (let ((pair (pump-pair #f #t)))
145 (pump-start! pair)
146 (do ((now 0 (+ now 1000))) ((> now 10000))
147 (quic-tick! (vector-ref pair 0) now)
148 (for-each
149 (lambda (packet)
150 (let ((header (decode-packet-header packet 0 8)))
151 (quic-receive! (vector-ref pair 1)
152 (flip-byte packet (quic-header-pn-offset header)) now)))
153 (quic-take-datagrams! (vector-ref pair 0) now))
154 (assert-equal '() (quic-take-datagrams! (vector-ref pair 1) now)))
155 (assert-true (quic-closed? (vector-ref pair 0)))
156 (assert-false (quic-complete? (vector-ref pair 0)))
157 (assert-false (quic-complete? (vector-ref pair 1)))))
158 (test "application frames traverse independent directional 1-RTT keys"
159 (let ((pair (pump-pair #f #t)) (received '()))
160 (pump-start! pair)
161 (pump-run! pair 0 #f #f)
162 (pump-assert-complete pair)
163 (quic-set-frame-handler! (vector-ref pair 1)
164 (lambda (frame) (set! received (cons frame received))))
165 (quic-send-frame! (vector-ref pair 0)
166 (make-quic-frame 10 (list (cons 'stream-id 0) (cons 'offset 0) (cons 'data #u8(1 2 3)))))
167 (pump-run! pair 0 #f #f)
168 (assert-equal 1 (length received))
169 (assert-equal #u8(1 2 3) (frame-ref (car received) 'data))))
170 (test "ACK-only traffic reaches quiescence without an ACK loop"
171 (let ((pair (pump-pair #f #t)))
172 (pump-start! pair)
173 (pump-run! pair 0 #f #f)
174 (pump-assert-complete pair)
175 (assert-equal '() (quic-take-datagrams! (vector-ref pair 0) 0))
176 (assert-equal '() (quic-take-datagrams! (vector-ref pair 1) 0)))))
178(define (pump-initial body)
179 (let* ((keys (initial-keys pump-original 'client))
180 (header (encode-packet-header 'initial pump-original pump-client-id #u8() 0 4
181 (+ (bytevector-length body) 16) length-width: 2))
182 (padded (bytes-append body
183 (make-bytevector (- 1200 (bytevector-length header) 16 (bytevector-length body)) 0)))
184 (packet (seal-quic-packet keys 'initial pump-original pump-client-id 0 padded length-width: 2)))
185 (discard-packet-keys! keys)
186 packet))
188(test-group "QUIC authenticated receive boundary"
189 (test "wrong CID and invalid AEAD are dropped without closing"
190 (let ((pair (pump-pair #f #t)))
191 (pump-start! pair)
192 (let ((packet (car (quic-take-datagrams! (vector-ref pair 0) 0))))
193 (quic-receive! (vector-ref pair 1) (flip-byte packet 6) 0)
194 (quic-receive! (vector-ref pair 1) (flip-byte packet 1199) 0)
195 (assert-false (quic-closed? (vector-ref pair 1)))
196 (assert-equal '() (quic-take-datagrams! (vector-ref pair 1) 0))
197 (quic-receive! (vector-ref pair 1) packet 0)
198 (pump-run! pair 0 #f #f)
199 (pump-assert-complete pair))))
200 (test "authenticated unknown frame closes with FRAME_ENCODING_ERROR"
201 (let* ((pair (pump-pair #f #t)) (server (vector-ref pair 1)))
202 (quic-receive! server (pump-initial #u8(31)) 0)
203 (assert-true (quic-closed? server))
204 (assert-equal 7 (quic-error-code (quic-failure server)))))
205 (test "authenticated ACK for unsent packet closes with PROTOCOL_VIOLATION"
206 (let* ((pair (pump-pair #f #t)) (server (vector-ref pair 1)))
207 (quic-receive! server
208 (pump-initial (encode-frame (make-quic-frame 2 (list (cons 'ranges (list (cons 0 0))) (cons 'delay 0))))) 0)
209 (assert-true (quic-closed? server))
210 (assert-equal 10 (quic-error-code (quic-failure server)))))
211 (test "TLS abort is idempotent and drops queued key-installation events"
212 (let ((client (make-tls-client (list #u8(113 49)) "example.test" (list (cons 15 #u8(1)))
213 (lambda (certificates name) #f))))
214 (tls-start! client)
215 (tls-abort! client)
216 (tls-abort! client)
217 (assert-true (tls-failed? client))
218 (assert-equal '() (tls-take-events! client))
219 (assert-error (tls-input! client 'initial #u8())))))
221(test-group "QUIC full-handshake sabotage control"
222 (test "handshake-sabotage: protected connection completes before deadline"
223 (let ((pair (pump-pair #f #t))
224 (sabotage? (equal? (getenv "QUIC_HP_SABOTAGE") "1")))
225 (pump-start! pair)
226 (do ((now 0 (+ now 1000))) ((> now 10000))
227 (for-each (lambda (connection) (quic-tick! connection now)) (vector->list pair))
228 (for-each
229 (lambda (packet)
230 (let* ((header (decode-packet-header packet 0 8))
231 (wire (if (and sabotage? (eq? (quic-header-kind header) 'initial))
232 (flip-byte packet (quic-header-pn-offset header)) packet)))
233 (quic-receive! (vector-ref pair 1) wire now)))
234 (quic-take-datagrams! (vector-ref pair 0) now))
235 (pump-deliver! pair 1 now #f #f))
236 ;; With QUIC_HP_SABOTAGE=1 this positive completion assertion must be red.
237 (assert-true (and (quic-complete? (vector-ref pair 0))
238 (quic-complete? (vector-ref pair 1)))))))