AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-connection-network.sgl

1(import (sigil test)
2 (sigil quic bytes)
3 (sigil quic wire)
4 (sigil quic frame)
5 (sigil quic error)
6 (sigil quic connection network))
7
8;; These are explicitly trusted, post-handshake fixtures for the socket-free
9;; owner. TLS authentication and packet metadata hooks are tested separately.
10(define (net-local-parameters)
11 (list (cons 0 #u8(10 11 12 13 14 15 16 17)) (cons 15 #u8(1))))
13(define (net-peer-parameters)
14 (list (cons 15 #u8(2))))
16(define (net-with-owner procedure)
17 (let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
18 (net-local-parameters) (net-peer-parameters))))
19 (dynamic-wind
20 (lambda () #t)
21 (lambda () (procedure network))
22 (lambda () (network-close! network)))))
24(define (net-new sequence retire)
25 (make-quic-frame 24
26 (list (cons 'sequence sequence) (cons 'retire-prior-to retire)
27 (cons 'connection-id (bytevector (+ sequence 2)))
28 (cons 'token (make-bytevector 16 0)))))
30(define (net-observe! network address number now)
31 (network-observe! network address 1200 number (list (make-quic-frame 1 '())) now))
33(define (net-retiring network)
34 (map (lambda (frame) (frame-ref frame 'sequence)) (network-retirements network)))
36(define (net-destination-is? network address sequence cid)
37 (let ((destination (network-destination network address)))
38 (and destination (= sequence (vector-ref destination 0))
39 (bytes=? cid (vector-ref destination 1)))))
41(define (net-retirement-error? procedure)
42 (guard (exn
43 (else (and (quic-error? exn) (= 10 (quic-error-code exn))
44 (= 25 (quic-error-frame-type exn)))))
45 (procedure)
46 #f))
48(define (net-binding-error? procedure)
49 (guard (exn
50 (else (and (quic-error? exn) (= 10 (quic-error-code exn))
51 (= 24 (quic-error-frame-type exn)))))
52 (procedure)
53 #f))
55(test-group "Confirmed connection CID and path ownership"
56 (test "connection-network: initial path owns peer sequence zero and copied destination metadata"
57 (net-with-owner
58 (lambda (network)
59 (let ((destination (network-destination network #u8(10))))
60 (assert-true
61 (and destination (= 0 (vector-ref destination 0))
62 (bytes=? #u8(2) (vector-ref destination 1))
63 (= 0 (network-route network #u8(1)))
64 (not (network-destination network #u8(11)))))
65 (bytevector-u8-set! (vector-ref destination 1) 0 9)
66 (assert-true (bytes=? #u8(2) (vector-ref (network-destination network #u8(10)) 1)))))))
67 (test "connection-network: local advertisements route only after commit and retire against actual packet DCID"
68 (net-with-owner
69 (lambda (network)
70 (let ((frame (network-issue-id! network #u8(4) (make-bytevector 16 0))))
71 (assert-true
72 (and frame (not (network-route network #u8(4)))
73 (net-retirement-error? (lambda () (network-retire-local-id! network 1 #u8(1))))
74 (network-check-id! network frame) (network-commit-id! network frame)
75 (= 1 (network-route network #u8(4)))
76 (net-retirement-error? (lambda () (network-retire-local-id! network 1 #u8(4))))
77 (network-retire-local-id! network 0 #u8(4))
78 (not (network-route network #u8(1)))
79 (network-retire-local-id! network 1 #u8(1))
80 (not (network-check-id! network frame))
81 (not (network-commit-id! network frame))
82 (not (network-route network #u8(4)))))))))
83 (test "connection-network: missing peer spare pauses probes without charging a candidate"
84 (net-with-owner
85 (lambda (network)
86 (net-observe! network #u8(11) 1 0)
87 (assert-true
88 (and (not (network-challenge! network #u8(11) 0))
89 (not (network-reserve-send! network #u8(11) 1 0))
90 (bytes=? #u8(10) (network-address network))
91 (network-peer-id! network (net-new 1 0))
92 (let ((probe (network-challenge! network #u8(11) 0)))
93 (and probe (= 1 (vector-ref probe 1))
94 (bytes=? #u8(3) (vector-ref probe 2))
95 (bytes=? #u8(10) (network-address network)))))))))
96 (test "connection-network: validated migration retains the old CID until its independent probe finishes"
97 (net-with-owner
98 (lambda (network)
99 (network-peer-id! network (net-new 1 0))
100 (net-observe! network #u8(11) 1 0)
101 (let ((old (network-challenge! network #u8(10) 0))
102 (next (network-challenge! network #u8(11) 0)))
103 (assert-true
104 (and old next (network-response! network (vector-ref next 3) 1)
105 (bytes=? #u8(11) (network-address network))
106 (net-destination-is? network #u8(11) 1 #u8(3))
107 (net-destination-is? network #u8(10) 0 #u8(2))
108 (null? (net-retiring network))
109 (network-response! network (vector-ref old 3) 1)
110 (not (network-destination network #u8(10)))
111 (equal? '(0) (net-retiring network))
112 (equal? '(0) (net-retiring network))
113 (network-retirement-ack! network 0)
114 (null? (net-retiring network))))))))
115 (test "connection-network: candidate timeout retires its CID and preserves the working path"
116 (net-with-owner
117 (lambda (network)
118 (network-peer-id! network (net-new 1 0))
119 (net-observe! network #u8(11) 1 0)
120 (network-tick! network 3000)
121 (assert-true
122 (and (not (network-candidate network))
123 (not (network-destination network #u8(11)))
124 (equal? '(1) (net-retiring network))
125 (net-destination-is? network #u8(10) 0 #u8(2))
126 (network-peer-id! network (net-new 2 0))
127 (net-observe! network #u8(12) 2 3001)
128 (let ((probe (network-challenge! network #u8(12) 3001)))
129 (and probe (= 2 (vector-ref probe 1))
130 (bytes=? #u8(4) (vector-ref probe 2)))))))))
131 (test "connection-network: superseded candidate CID is retired rather than reassigned"
132 (net-with-owner
133 (lambda (network)
134 (network-peer-id! network (net-new 1 0))
135 (net-observe! network #u8(11) 1 0)
136 (net-observe! network #u8(12) 2 1)
137 (assert-true
138 (and (not (network-destination network #u8(11)))
139 (not (network-destination network #u8(12)))
140 (equal? '(1) (net-retiring network))
141 (network-peer-id! network (net-new 2 0))
142 (net-destination-is? network #u8(12) 2 #u8(4))
143 (net-destination-is? network #u8(10) 0 #u8(2)))))))
144 (test "connection-network: peer RetirePriorTo replaces the current CID before further output"
145 (net-with-owner
146 (lambda (network)
147 (assert-true
148 (and (network-peer-id! network (net-new 1 1))
149 (net-destination-is? network #u8(10) 1 #u8(3))
150 (equal? '(0) (net-retiring network))
151 (bytes=? #u8(10) (network-address network))
152 (null? (network-take-events! network)))))))
153 (test "connection-network: only non-probing decoded frames authorize candidate validation"
154 (net-with-owner
155 (lambda (network)
156 (network-peer-id! network (net-new 1 0))
157 (network-observe! network #u8(11) 1200 10 (list (net-new 1 0)) 0)
158 (assert-true
159 (and (not (network-challenge! network #u8(11) 0))
160 (bytes=? #u8(10) (network-address network))
161 (net-observe! network #u8(11) 1 1)
162 (network-challenge! network #u8(11) 1))))))
163 (test "connection-network: constructor rejects mismatched IDs and unsupported local advertisements"
164 (assert-error
165 (make-connection-network #u8(1) #u8(9) 'server #u8(10) 0
166 (net-local-parameters) (net-peer-parameters)))
167 (assert-error
168 (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
169 (cons (cons 14 (encode-varint 9)) (net-local-parameters)) (net-peer-parameters)))
170 (let ((network (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
171 (net-local-parameters) (cons (cons 14 (encode-varint 100)) (net-peer-parameters)))))
172 (dynamic-wind
173 (lambda () #t)
174 (lambda () (assert-equal 8 (network-local-limit network)))
175 (lambda () (network-close! network)))))
176 (test "connection-network: per-path reservations cannot borrow original path credit"
177 (net-with-owner
178 (lambda (network)
179 (network-peer-id! network (net-new 1 0))
180 (network-observe! network #u8(11) 10 1 (list (make-quic-frame 1 '())) 0)
181 (assert-true
182 (and (network-reserve-send! network #u8(10) 1200 0)
183 (network-reserve-send! network #u8(11) 30 0)
184 (not (network-reserve-send! network #u8(11) 1 0))
185 (not (network-challenge! network #u8(11) 0))
186 (bytes=? #u8(10) (network-address network)))))))
187 (test "connection-network: preferred metadata installs owned sequence one on both directions"
188 (let* ((preferred (make-bytevector 42 0)) (server #f) (client #f))
189 (bytevector-u8-set! preferred 24 1)
190 (bytevector-u8-set! preferred 25 99)
191 (dynamic-wind
192 (lambda () #t)
193 (lambda ()
194 (set! server
195 (make-connection-network #u8(1) #u8(2) 'server #u8(10) 0
196 (cons (cons 13 preferred) (net-local-parameters)) (net-peer-parameters)))
197 (set! client
198 (make-connection-network #u8(2) #u8(1) 'client #u8(10) 0
199 (net-peer-parameters) (cons (cons 13 preferred) (net-local-parameters))))
200 (bytevector-u8-set! preferred 25 88)
201 (assert-true
202 (and (= 1 (network-route server #u8(99))) (= 2 (network-local-count server))
203 (net-binding-error?
204 (lambda ()
205 (network-peer-id! client
206 (make-quic-frame 24
207 (list (cons 'sequence 1) (cons 'retire-prior-to 0)
208 (cons 'connection-id #u8(99)) (cons 'token (make-bytevector 16 1)))))))
209 (network-peer-id! client
210 (make-quic-frame 24
211 (list (cons 'sequence 1) (cons 'retire-prior-to 0)
212 (cons 'connection-id #u8(99)) (cons 'token (make-bytevector 16 0)))))
213 (net-destination-is? client #u8(10) 0 #u8(1)))))
214 (lambda ()
215 (when server (network-close! server))
216 (when client (network-close! client))))))
217 (test "connection-network: close clears path CID and control ownership and rejects mutations"
218 (net-with-owner
219 (lambda (network)
220 (network-peer-id! network (net-new 1 0))
221 (net-observe! network #u8(11) 1 0)
222 (network-close! network)
223 (network-close! network)
224 (assert-false (network-address network))
225 (assert-false (network-candidate network))
226 (assert-false (network-destination network #u8(10)))
227 (assert-false (network-route network #u8(1)))
228 (assert-equal '() (network-retirements network))
229 (assert-equal '() (network-route-lengths network))
230 (assert-equal 0 (network-local-count network))
231 (assert-error (network-peer-id! network (net-new 2 0)))
232 (assert-error (net-observe! network #u8(12) 2 1))
233 (assert-error (network-issue-id! network #u8(4) (make-bytevector 16 0)))))))