AtlatestRepositorysigil-http3

sigil-http3 / tree / testtest-webtransport-router.sgl

1(import (sigil test)
2 (sigil quic bytes)
3 (sigil quic wire)
4 (sigil http3 error)
5 (sigil http3 fields)
6 (sigil http3 frame)
7 (sigil http3 qpack)
8 (sigil http3 webtransport wire)
9 (sigil http3 webtransport router))
11(define (wt-router-fixture (keys: (state 'ready) (allow? #t)))
12 (let* ((commands '())
13 (current state)
14 (router
15 (make-webtransport-router
16 authorize?: (lambda (fields) allow?)
17 negotiation: (lambda () current)
18 on-command: (lambda (command) (set! commands (cons command commands))))))
19 (vector router (lambda () (reverse commands)) (lambda (next) (set! current next)))))
21(define (wt-router-commands fixture)
22 ((vector-ref fixture 1)))
24(define (wt-router-fields)
25 (map (lambda (field) (cons (h3-ascii-bytes (car field)) (h3-ascii-bytes (cdr field))))
26 '((":method" . "CONNECT") (":protocol" . "webtransport") (":scheme" . "https")
27 (":authority" . "unit.test") (":path" . "/echo") ("origin" . "https://unit.test"))))
29(define (wt-router-connect! fixture id)
30 (webtransport-router-input! (vector-ref fixture 0) id
31 (encode-h3-frame 1 (encode-qpack-fields (wt-router-fields))) #f))
33(define (wt-router-code thunk)
34 (guard (exn ((h3-error? exn) (h3-error-code exn)) (else (raise exn)))
35 (thunk)
36 #f))
38(test-group "WebTransport CONNECT and stream routing"
39 (test "fragmented CONNECT admits a session only after complete validated headers"
40 (let* ((fixture (wt-router-fixture))
41 (router (vector-ref fixture 0))
42 (headers (encode-h3-frame 1 (encode-qpack-fields (wt-router-fields)))))
43 (webtransport-router-input! router 0 (bytevector-copy headers 0 1) #f)
44 (assert-equal '() (wt-router-commands fixture))
45 (webtransport-router-input! router 0 (bytevector-copy headers 1) #f)
46 (assert-equal '(response open) (map (lambda (command) (vector-ref command 0)) (wt-router-commands fixture)))
47 (assert-equal 200 (vector-ref (car (wt-router-commands fixture)) 2))))
49 (test "bidi and uni prefixes are stripped once and payloads retain their FIN"
50 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
51 (wt-router-connect! fixture 0)
52 (webtransport-router-input! router 4 #u8(64) #f)
53 (webtransport-router-input! router 4 #u8(65 0 7) #f)
54 (webtransport-router-input! router 4 #u8(8 9) #t)
55 (webtransport-router-input! router 14 #u8(64 84 0 10) #t)
56 (assert-equal
57 (list (vector 'stream 0 4 #u8(7) #f) (vector 'stream 0 4 #u8(8 9) #t)
58 (vector 'stream 0 14 #u8(10) #t))
59 (cddr (wt-router-commands fixture)))))
61 (test "a session stream arriving before CONNECT is released after the open command"
62 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
63 (webtransport-router-input! router 4 #u8(64 65 0 7 8) #t)
64 (webtransport-router-datagram! router #u8(0 99))
65 (assert-equal '() (wt-router-commands fixture))
66 (wt-router-connect! fixture 0)
67 (assert-equal '(response open stream)
68 (map (lambda (command) (vector-ref command 0)) (wt-router-commands fixture)))
69 (assert-equal '#(stream 0 4 #u8(7 8) #t) (caddr (wt-router-commands fixture)))))
71 (test "SETTINGS reordering defers requests without losing early stream payloads"
72 (let* ((fixture (wt-router-fixture state: 'pending)) (router (vector-ref fixture 0)))
73 (wt-router-connect! fixture 0)
74 (webtransport-router-input! router 4 #u8(64 65 0 7) #f)
75 (webtransport-router-step! router 1)
76 (assert-equal '() (wt-router-commands fixture))
77 ((vector-ref fixture 2) 'ready)
78 (webtransport-router-step! router 2)
79 (assert-equal '(response open stream)
80 (map (lambda (command) (vector-ref command 0)) (wt-router-commands fixture)))
81 (assert-equal '#(stream 0 4 #u8(7) #f) (caddr (wt-router-commands fixture)))))
83 (test "expired SETTINGS input cannot be revived by a late negotiation result"
84 (let* ((fixture (wt-router-fixture state: 'pending)) (router (vector-ref fixture 0)))
85 (wt-router-connect! fixture 0)
86 ((vector-ref fixture 2) 'ready)
87 (webtransport-router-step! router 5000)
88 (assert-equal (list (vector 'retire 0 #x3994bd84)) (wt-router-commands fixture))
89 (assert-error (webtransport-router-step! router 4999))))
91 (test "authorization and lifetime session limits reject without another open"
92 (let ((denied (wt-router-fixture allow?: #f)))
93 (wt-router-connect! denied 0)
94 (assert-equal '(response) (map (lambda (command) (vector-ref command 0)) (wt-router-commands denied)))
95 (assert-equal 403 (vector-ref (car (wt-router-commands denied)) 2)))
96 (let ((fixture (wt-router-fixture)))
97 (wt-router-connect! fixture 0)
98 (wt-router-connect! fixture 4)
99 (assert-equal '(response open response)
100 (map (lambda (command) (vector-ref command 0)) (wt-router-commands fixture)))
101 (assert-equal 429 (vector-ref (caddr (wt-router-commands fixture)) 2))))
103 (test "early stream individual and aggregate budgets retire before growing"
104 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
105 (webtransport-router-input! router 4
106 (bytes-append (encode-webtransport-prefix 0 #f) (make-bytevector 4096 1)) #f)
107 (webtransport-router-input! router 4 (make-bytevector 4096 1) #f)
108 (assert-equal '() (wt-router-commands fixture))
109 (webtransport-router-input! router 4 #u8(1) #f)
110 (assert-equal (list (vector 'retire 4 #x3994bd84)) (wt-router-commands fixture)))
111 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
112 (for-each
113 (lambda (id)
114 (webtransport-router-input! router id
115 (bytes-append (encode-webtransport-prefix 0 #f) (make-bytevector 4096 1)) #f)
116 (webtransport-router-input! router id (make-bytevector 4096 1) #f))
117 '(4 8 12 16))
118 (webtransport-router-input! router 20 #u8(64 65 0 1) #f)
119 (assert-equal (list (vector 'retire 20 #x3994bd84)) (wt-router-commands fixture))))
121 (test "peer close propagates all associated IDs then drops late datagrams"
122 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
123 (wt-router-connect! fixture 0)
124 (webtransport-router-input! router 4 #u8(64 65 0 7) #f)
125 (webtransport-router-input! router 14 #u8(64 84 0 8) #f)
126 (webtransport-router-input! router 0 (encode-h3-frame 0 (encode-webtransport-close 9 #u8(65))) #t)
127 (assert-equal
128 (list (vector 'close 0 9 #u8(65) '(14 4)) (vector 'end 0))
129 (drop-n (wt-router-commands fixture) 4))
130 (webtransport-router-datagram! router #u8(0 99))
131 (assert-equal 6 (length (wt-router-commands fixture)))))
133 (test "stream reset and CONNECT reset propagate distinct commands once"
134 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
135 (wt-router-connect! fixture 0)
136 (webtransport-router-input! router 4 #u8(64 65 0 7) #f)
137 (webtransport-router-reset! router 4 77)
138 (webtransport-router-reset! router 0 88)
139 (webtransport-router-reset! router 0 88)
140 (assert-equal (list (vector 'reset 0 4 77) (vector 'aborted 0 88 '(4)))
141 (drop-n (wt-router-commands fixture) 3))))
143 (test "large CONNECT DATA is chunked for incremental unknown-capsule skipping"
144 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0))
145 (wire (encode-h3-frame 0
146 (bytes-append #u8(2) (encode-varint 20000) (make-bytevector 20000 1)
147 (encode-webtransport-close 0 #u8())))))
148 (wt-router-connect! fixture 0)
149 (let loop ((offset 0))
150 (let ((end (min (bytevector-length wire) (+ offset 4096))))
151 (webtransport-router-input! router 0 (bytevector-copy wire offset end) (= end (bytevector-length wire)))
152 (when (< end (bytevector-length wire)) (loop end))))
153 (assert-equal '(response open close end)
154 (map (lambda (command) (vector-ref command 0)) (wt-router-commands fixture)))))
156 (test "self-associated streams and datagrams without negotiation fail explicitly"
157 (let* ((fixture (wt-router-fixture state: 'pending)) (router (vector-ref fixture 0)))
158 (assert-equal #x33 (wt-router-code (lambda () (webtransport-router-datagram! router #u8(0 1)))))
159 (assert-equal #x108
160 (wt-router-code (lambda () (webtransport-router-input! router 4 #u8(64 65 4) #f)))))
161 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
162 (wt-router-connect! fixture 0)
163 (webtransport-router-datagram! router #u8(0 7))
164 (assert-equal '#(datagram 0 #u8(7)) (caddr (wt-router-commands fixture)))))
166 (test "connection teardown is observational and idempotent"
167 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
168 (wt-router-connect! fixture 0)
169 (webtransport-router-input! router 4 #u8(64 65 0 7) #f)
170 (webtransport-router-close! router)
171 (webtransport-router-close! router)
172 (assert-equal '#(connection-closed 0 (4)) (list-ref (wt-router-commands fixture) 3))
173 (assert-error (webtransport-router-input! router 8 #u8(64 65 0) #f))))
175 (test "unknown uni streams and truncated prefixes produce bounded retirement"
176 (let* ((fixture (wt-router-fixture)) (router (vector-ref fixture 0)))
177 (webtransport-router-input! router 14 #u8(33 1 2 3) #t)
178 (webtransport-router-input! router 4 #u8(64 65) #t)
179 (assert-equal (list (vector 'retire 14 #x100) (vector 'retire 4 #x106))
180 (wt-router-commands fixture)))))