AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-peer-ids.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 peer ids))
7
8(define (pic-with-pool procedure)
9 (let ((pool (make-peer-id-pool #u8(1))))
10 (dynamic-wind
11 (lambda () #t)
12 (lambda () (procedure pool))
13 (lambda () (peer-id-pool-close! pool)))))
15(define (pic-frame sequence retire cid token)
16 (make-quic-frame 24
17 (list (cons 'sequence sequence) (cons 'retire-prior-to retire)
18 (cons 'connection-id cid) (cons 'token token))))
20(define (pic-new! pool sequence retire)
21 (peer-id-pool-receive! pool
22 (pic-frame sequence retire (bytevector (+ sequence 1)) (make-bytevector 16 0))))
24(define (pic-error? code procedure)
25 (guard (exn
26 (else (and (quic-error? exn) (= code (quic-error-code exn))
27 (= 24 (quic-error-frame-type exn)))))
28 (procedure)
29 #f))
31(define (pic-retiring pool)
32 (map (lambda (frame) (frame-ref frame 'sequence)) (peer-id-pool-retirements pool)))
34(test-group "Peer CID capacity, retirement and path ownership"
35 (test "peer-ids: invalid construction refuses ambiguous authenticated metadata"
36 (for-each
37 (lambda (procedure) (assert-error (procedure)))
38 (list
39 (lambda () (make-peer-id-pool (make-bytevector 21 0)))
40 (lambda () (make-peer-id-pool #u8(1) limit: 1))
41 (lambda () (make-peer-id-pool #u8(1) limit: 9))
42 (lambda () (make-peer-id-pool #u8(1) token: #u8(0)))
43 (lambda () (make-peer-id-pool #u8() token: (make-bytevector 16 0)))
44 (lambda () (make-peer-id-pool #u8(1) preferred: (cons #u8(1) (make-bytevector 16 0))))
45 (lambda () (make-peer-id-pool #u8() preferred: (cons #u8(2) (make-bytevector 16 0)))))))
46 (test "peer-ids: initial CID is owned and already assigned to its original path"
47 (let* ((cid (make-bytevector 1 1)) (pool (make-peer-id-pool cid)))
48 (dynamic-wind
49 (lambda () #t)
50 (lambda ()
51 (bytevector-u8-set! cid 0 3)
52 (let ((copy (peer-id-pool-id pool 0)))
53 (bytevector-u8-set! copy 0 4)
54 (assert-true
55 (and (bytes=? #u8(1) (peer-id-pool-id pool 0))
56 (not (peer-id-pool-acquire! pool))
57 (equal? '(0) (peer-id-pool-sequences pool))))))
58 (lambda () (peer-id-pool-close! pool)))))
59 (test "peer-ids: incoming CID and token are owned and assigned at most once"
60 (pic-with-pool
61 (lambda (pool)
62 (let* ((cid (make-bytevector 1 2)) (token (make-bytevector 16 0))
63 (frame (pic-frame 1 0 cid token)))
64 (peer-id-pool-receive! pool frame)
65 (bytevector-u8-set! cid 0 3)
66 (bytevector-u8-set! token 0 5)
67 (assert-true
68 (and (bytes=? #u8(2) (peer-id-pool-id pool 1))
69 (pic-new! pool 1 0) (= 1 (peer-id-pool-acquire! pool))
70 (not (peer-id-pool-acquire! pool)) (= 2 (peer-id-pool-count pool))))))))
71 (test "peer-ids: duplicate advertisement neither consumes capacity nor releases ownership"
72 (pic-with-pool
73 (lambda (pool)
74 (pic-new! pool 1 0)
75 (peer-id-pool-acquire! pool)
76 (assert-true
77 (and (pic-new! pool 1 0) (= 2 (peer-id-pool-count pool))
78 (not (peer-id-pool-acquire! pool)) (null? (pic-retiring pool)))))))
79 (test "peer-ids: conflicting sequence CID or token and reused CID are typed violations"
80 (pic-with-pool
81 (lambda (pool)
82 (pic-new! pool 1 0)
83 (assert-true
84 (and (pic-error? 10
85 (lambda () (peer-id-pool-receive! pool (pic-frame 1 0 #u8(3) (make-bytevector 16 0)))))
86 (pic-error? 10
87 (lambda () (peer-id-pool-receive! pool (pic-frame 1 0 #u8(2) (make-bytevector 16 1)))))
88 (pic-error? 10
89 (lambda () (peer-id-pool-receive! pool (pic-frame 2 0 #u8(2) (make-bytevector 16 0)))))
90 (bytes=? #u8(2) (peer-id-pool-id pool 1)) (= 2 (peer-id-pool-count pool)))))))
91 (test "peer-ids: active limit failure leaves the original inventory unchanged"
92 (pic-with-pool
93 (lambda (pool)
94 (pic-new! pool 1 0)
95 (assert-true
96 (and (pic-error? 9 (lambda () (pic-new! pool 2 0)))
97 (equal? '(0 1) (peer-id-pool-sequences pool))
98 (not (peer-id-pool-id pool 2)) (null? (pic-retiring pool)))))))
99 (test "peer-ids: RetirePriorTo frees active capacity before admitting its replacement"
100 (pic-with-pool
101 (lambda (pool)
102 (pic-new! pool 1 0)
103 (assert-true
104 (and (pic-new! pool 2 1) (= 2 (peer-id-pool-count pool))
105 (not (peer-id-pool-id pool 0))
106 (equal? '(1 2) (peer-id-pool-sequences pool))
107 (equal? '(0) (pic-retiring pool)))))))
108 (test "peer-ids: reordered smaller RetirePriorTo cannot undo a peer retirement floor"
109 (pic-with-pool
110 (lambda (pool)
111 (pic-new! pool 2 2)
112 (assert-true
113 (and (not (pic-new! pool 1 0)) (= 1 (peer-id-pool-count pool))
114 (not (peer-id-pool-id pool 1))
115 (equal? '(0 1) (pic-retiring pool))
116 (= 2 (peer-id-pool-acquire! pool)))))))
117 (test "peer-ids: acknowledged retirement cannot resurrect while an older path stays live"
118 (pic-with-pool
119 (lambda (pool)
120 (pic-new! pool 2 0)
121 (peer-id-pool-retire! pool 2)
122 (assert-true
123 (and (peer-id-pool-ack! pool 2) (not (peer-id-pool-ack! pool 2))
124 (bytes=? #u8(1) (peer-id-pool-id pool 0))
125 (not (pic-new! pool 2 0)) (not (peer-id-pool-id pool 2))
126 (not (peer-id-pool-acquire! pool))
127 (equal? '(2) (pic-retiring pool)))))))
128 (test "peer-ids: unseen older sequence gaps are retired without displacing a live exception"
129 (pic-with-pool
130 (lambda (pool)
131 (pic-new! pool 3 0)
132 (peer-id-pool-retire! pool 3)
133 (peer-id-pool-ack! pool 3)
134 (assert-true
135 (and (not (pic-new! pool 1 0)) (not (peer-id-pool-id pool 1))
136 (= 1 (peer-id-pool-count pool))
137 (bytes=? #u8(1) (peer-id-pool-id pool 0))
138 (equal? '(1) (pic-retiring pool)))))))
139 (test "peer-ids: twice-limit pending retirement budget is bounded and errors are atomic"
140 (pic-with-pool
141 (lambda (pool)
142 (for-each
143 (lambda (sequence)
144 (pic-new! pool sequence 0)
145 (peer-id-pool-retire! pool sequence))
146 '(2 3 4 5))
147 (pic-new! pool 6 0)
148 (assert-true
149 (and (pic-error? 9 (lambda () (pic-new! pool 1 0)))
150 (pic-error? 9 (lambda () (peer-id-pool-retire! pool 6)))
151 (pic-error? 9 (lambda () (pic-new! pool 7 7)))
152 (= 2 (peer-id-pool-count pool))
153 (equal? '(0 6) (peer-id-pool-sequences pool))
154 (equal? '(2 3 4 5) (pic-retiring pool))
155 (peer-id-pool-ack! pool 2)
156 (peer-id-pool-retire! pool 6)
157 (equal? '(3 4 5 6) (pic-retiring pool)))))))
158 (test "peer-ids: retiring IDs remain pending and cannot be assigned again"
159 (pic-with-pool
160 (lambda (pool)
161 (pic-new! pool 1 0)
162 (peer-id-pool-acquire! pool)
163 (assert-true
164 (and (peer-id-pool-retire! pool 1) (not (peer-id-pool-retire! pool 1))
165 (not (pic-new! pool 1 0)) (not (peer-id-pool-id pool 1))
166 (not (peer-id-pool-acquire! pool))
167 (equal? '(1) (pic-retiring pool))
168 (equal? '(1) (pic-retiring pool)))))))
169 (test "peer-ids: zero-length peer source disables spare issue and retirement"
170 (let ((pool (make-peer-id-pool #u8())))
171 (dynamic-wind
172 (lambda () #t)
173 (lambda ()
174 (assert-true
175 (and (bytes=? #u8() (peer-id-pool-id pool 0))
176 (pic-error? 10 (lambda () (pic-new! pool 1 0)))
177 (not (peer-id-pool-acquire! pool))))
178 (assert-error (peer-id-pool-retire! pool 0)))
179 (lambda () (peer-id-pool-close! pool)))))
180 (test "peer-ids: authenticated preferred CID is owned sequence one and consumes capacity"
181 (let* ((cid (make-bytevector 1 2)) (token (make-bytevector 16 0))
182 (pool (make-peer-id-pool #u8(1) preferred: (cons cid token))))
183 (dynamic-wind
184 (lambda () #t)
185 (lambda ()
186 (bytevector-u8-set! cid 0 4)
187 (bytevector-u8-set! token 0 5)
188 (assert-true
189 (and (= 2 (peer-id-pool-count pool)) (pic-new! pool 1 0)
190 (= 1 (peer-id-pool-acquire! pool))
191 (pic-error? 9 (lambda () (pic-new! pool 2 0))))))
192 (lambda () (peer-id-pool-close! pool)))))
193 (test "peer-ids: acknowledgment never forgets a live or unknown CID"
194 (pic-with-pool
195 (lambda (pool)
196 (assert-true
197 (and (not (peer-id-pool-ack! pool 0)) (not (peer-id-pool-ack! pool 1))
198 (bytes=? #u8(1) (peer-id-pool-id pool 0)) (= 1 (peer-id-pool-count pool)))))))
199 (test "peer-ids: maximum wire sequence retires without overflow or resurrection"
200 (pic-with-pool
201 (lambda (pool)
202 (let ((frame (pic-frame quic-varint-maximum 0 #u8(2) (make-bytevector 16 0))))
203 (peer-id-pool-receive! pool frame)
204 (peer-id-pool-retire! pool quic-varint-maximum)
205 (peer-id-pool-ack! pool quic-varint-maximum)
206 (assert-true
207 (and (not (peer-id-pool-receive! pool frame))
208 (not (peer-id-pool-id pool quic-varint-maximum))
209 (bytes=? #u8(1) (peer-id-pool-id pool 0))
210 (equal? (list quic-varint-maximum) (pic-retiring pool))))))))
211 (test "peer-ids: malformed NEW fields are typed frame errors without effects"
212 (pic-with-pool
213 (lambda (pool)
214 (for-each
215 (lambda (frame)
216 (assert-true (pic-error? 7 (lambda () (peer-id-pool-receive! pool frame)))))
217 (list
218 (pic-frame 1 2 #u8(2) (make-bytevector 16 0))
219 (pic-frame -1 0 #u8(2) (make-bytevector 16 0))
220 (pic-frame 1 0 #u8() (make-bytevector 16 0))
221 (pic-frame 1 0 #u8(2) #u8(0))))
222 (assert-equal '(0) (peer-id-pool-sequences pool))
223 (assert-equal '() (pic-retiring pool)))))
224 (test "peer-ids: initial sequence learns a token once and then enforces its binding"
225 (pic-with-pool
226 (lambda (pool)
227 (let* ((token (make-bytevector 16 0))
228 (frame (pic-frame 0 0 #u8(1) token)))
229 (peer-id-pool-receive! pool frame)
230 (bytevector-u8-set! token 0 1)
231 (assert-true
232 (and (pic-error? 10 (lambda () (peer-id-pool-receive! pool frame)))
233 (peer-id-pool-receive! pool (pic-frame 0 0 #u8(1) (make-bytevector 16 0)))
234 (= 1 (peer-id-pool-count pool)) (not (peer-id-pool-acquire! pool))))))))
235 (test "peer-ids: a duplicate sequence can advance RetirePriorTo without releasing its path"
236 (let ((pool (make-peer-id-pool #u8(1) limit: 3)))
237 (dynamic-wind
238 (lambda () #t)
239 (lambda ()
240 (pic-new! pool 1 0)
241 (pic-new! pool 2 0)
242 (peer-id-pool-acquire! pool)
243 (peer-id-pool-acquire! pool)
244 (assert-true
245 (and (pic-new! pool 2 2) (= 1 (peer-id-pool-count pool))
246 (equal? '(0 1) (pic-retiring pool))
247 (not (peer-id-pool-acquire! pool)) (not (pic-new! pool 1 0))
248 (bytes=? #u8(3) (peer-id-pool-id pool 2)))))
249 (lambda () (peer-id-pool-close! pool)))))
250 (test "peer-ids: close clears inventories and refuses subsequent mutations"
251 (pic-with-pool
252 (lambda (pool)
253 (pic-new! pool 1 0)
254 (peer-id-pool-retire! pool 1)
255 (peer-id-pool-close! pool)
256 (peer-id-pool-close! pool)
257 (assert-equal 0 (peer-id-pool-count pool))
258 (assert-equal '() (peer-id-pool-sequences pool))
259 (assert-equal '() (pic-retiring pool))
260 (assert-false (peer-id-pool-id pool 0))
261 (assert-error (pic-new! pool 1 0))
262 (assert-error (peer-id-pool-acquire! pool))
263 (assert-error (peer-id-pool-retire! pool 1))
264 (assert-error (peer-id-pool-ack! pool 1))))))