AtlatestRepositorysigil-http3

sigil-http3 / tree / testtest-streams.sgl

1(import (sigil test)
2 (sigil quic wire)
3 (sigil quic frame)
4 (sigil quic error)
5 (sigil quic transport parameters)
6 (sigil http3 transport streams))
7
8(define (stream-test-state)
9 (let ((state (make-server-streams receive-window: 4 bidirectional: 2 unidirectional: 3)))
10 (streams-set-peer! state
11 (list (cons 4 (encode-varint 5)) (cons 5 (encode-varint 4))
12 (cons 7 (encode-varint 4)) (cons 9 (encode-varint 1))))
13 state))
15;; These are literal QUIC frame bytes, decoded by the pinned frame codec.
16(define (stream-test-input! state wire)
17 (streams-input! state (car (decode-frames wire 'application 'client))))
19(define (stream-test-code thunk)
20 (guard (exn
21 ((quic-error? exn) (quic-error-code exn))
22 (else (raise exn)))
23 (thunk)
24 #f))
26(test-group "Concurrent server stream transport"
27 (test "advertised receive directions match server role and storage budget"
28 (let* ((state (stream-test-state))
29 (parameters (server-stream-parameters state)))
30 (assert-equal 20 (transport-parameter-integer parameters 4 -1))
31 (assert-equal 0 (transport-parameter-integer parameters 5 -1))
32 (assert-equal 4 (transport-parameter-integer parameters 6 -1))
33 (assert-equal 4 (transport-parameter-integer parameters 7 -1))
34 (assert-equal 2 (transport-parameter-integer parameters 8 -1))
35 (assert-equal 3 (transport-parameter-integer parameters 9 -1))))
37 (test "CONNECT, control, and session streams remain independent"
38 (let ((state (stream-test-state)))
39 (stream-test-input! state #u8(14 0 0 3 97 98 99))
40 (stream-test-input! state #u8(14 2 0 2 0 4))
41 (stream-test-input! state #u8(15 4 0 2 120 121))
42 (assert-equal #u8(0 4) (streams-read! state 2))
43 (assert-equal #u8(120 121) (streams-read! state 4))
44 (assert-equal #u8(97 98 99) (streams-read! state 0))
45 (assert-true (streams-eof? state 4))
46 (assert-false (streams-eof? state 0))))
48 (test "out-of-order FIN waits for the gap and rejects conflicting final sizes"
49 (let ((state (stream-test-state)))
50 (stream-test-input! state #u8(15 0 2 2 3 4))
51 (assert-equal #u8() (streams-read! state 0))
52 (assert-false (streams-eof? state 0))
53 (stream-test-input! state #u8(14 0 0 2 1 2))
54 (assert-equal #u8(1 2 3 4) (streams-read! state 0))
55 (assert-true (streams-eof? state 0))
56 (assert-equal 6
57 (stream-test-code (lambda () (stream-test-input! state #u8(4 0 0 3)))))))
59 (test "duplicates do not consume receive credit twice"
60 (let ((state (stream-test-state)))
61 (stream-test-input! state #u8(14 0 0 4 1 2 3 4))
62 (stream-test-input! state #u8(14 0 0 4 1 2 3 4))
63 (for-each
64 (lambda (id)
65 (stream-test-input! state (bytevector 14 id 0 4 1 2 3 4)))
66 '(4 2 6 10))
67 (assert-equal #u8(1 2 3 4) (streams-read! state 10))
68 (assert-equal 5 (length (streams-ids state)))))
70 (test "excess stream counts, stream data, and RESET offsets fail explicitly"
71 (let ((state (stream-test-state)))
72 (assert-equal 4
73 (stream-test-code (lambda () (stream-test-input! state #u8(14 8 0 0)))))
74 (assert-equal 4
75 (stream-test-code (lambda () (stream-test-input! state #u8(14 14 0 0)))))
76 (assert-equal 3
77 (stream-test-code (lambda () (stream-test-input! state #u8(14 0 4 1 99)))))
78 (assert-equal 3
79 (stream-test-code (lambda () (stream-test-input! state #u8(4 0 0 5)))))))
81 (test "unopened local streams and forbidden directions raise STREAM_STATE_ERROR"
82 (let ((state (stream-test-state)))
83 (assert-equal 5
84 (stream-test-code (lambda () (stream-test-input! state #u8(14 3 0 0)))))
85 (assert-equal 3 (streams-open-uni! state))
86 (assert-equal 5
87 (stream-test-code (lambda () (stream-test-input! state #u8(14 3 0 0)))))
88 (stream-test-input! state #u8(14 2 0 0))
89 (assert-equal 5
90 (stream-test-code (lambda () (stream-test-input! state #u8(5 2 42)))))
91 (assert-equal 5
92 (stream-test-code (lambda () (stream-test-input! state #u8(17 2 9)))))))
94 (test "writes honor both flow limits and server unidirectional credit"
95 (let ((state (stream-test-state)))
96 (assert-equal 3 (streams-open-uni! state))
97 (assert-false (streams-open-uni! state))
98 (assert-equal #u8(14 3 0 3 1 2 3)
99 (encode-frame (streams-write! state 3 #u8(1 2 3) #f)))
100 (stream-test-input! state #u8(17 3 8))
101 (assert-equal #u8(14 3 3 2 4 5)
102 (encode-frame (streams-write! state 3 #u8(4 5) #f)))
103 (assert-false (streams-write! state 3 #u8(6) #f))
104 (stream-test-input! state #u8(16 9))
105 (assert-equal #u8(15 3 5 3 6 7 8)
106 (encode-frame (streams-write! state 3 #u8(6 7 8) #t)))
107 (stream-test-input! state #u8(19 3))
108 (assert-equal 7 (streams-open-uni! state))
109 (assert-equal 11 (streams-open-uni! state))
110 (assert-false (streams-open-uni! state))))
112 (test "STOP_SENDING requests retransmission cancellation and one exact RESET"
113 (let ((state (stream-test-state)))
114 (stream-test-input! state #u8(14 0 0 1 99))
115 (streams-write! state 0 #u8(7 8) #f)
116 (let ((result (stream-test-input! state #u8(5 0 42))))
117 (assert-equal '(0) (vector-ref result 1))
118 (assert-equal #u8(4 0 42 2) (encode-frame (car (vector-ref result 0)))))
119 (assert-equal '() (vector-ref (stream-test-input! state #u8(5 0 42)) 0))))
121 (test "reset error zero remains distinct from no reset"
122 (let ((state (stream-test-state)))
123 (stream-test-input! state #u8(4 0 0 4))
124 (assert-equal 0 (streams-reset-code state 0))
125 (assert-false (streams-eof? state 0))
126 (assert-error (streams-read! state 0))))
128 (test "consuming bytes replenishes bounded per-stream and connection credit"
129 (let ((state (stream-test-state)))
130 (stream-test-input! state #u8(14 0 0 4 1 2 3 4))
131 (assert-equal #u8(1 2 3 4) (streams-read! state 0))
132 (assert-equal (list #u8(17 0 8) #u8(16 24))
133 (map encode-frame (streams-credit-frames! state)))
134 (stream-test-input! state #u8(14 0 4 4 5 6 7 8))
135 (assert-equal #u8(5 6 7 8) (streams-read! state 0))
136 (assert-equal (list #u8(17 0 12) #u8(16 28))
137 (map encode-frame (streams-credit-frames! state)))
138 (assert-equal '() (streams-credit-frames! state))))
140 (test "local retirement sends direction-correct controls and exact final send size once"
141 (let ((state (stream-test-state)))
142 (stream-test-input! state #u8(14 0 0 1 99))
143 (streams-write! state 0 #u8(7 8) #f)
144 (assert-equal (list #u8(5 0 42) #u8(4 0 42 2))
145 (map encode-frame (streams-retire! state 0 42)))
146 (assert-equal '() (streams-retire! state 0 42))
147 (assert-error (streams-read! state 0))
148 (assert-error (streams-write! state 0 #u8(9) #f))
149 (assert-equal 3 (streams-open-uni! state))
150 (streams-write! state 3 #u8(9) #f)
151 (assert-equal (list #u8(4 3 42 1)) (map encode-frame (streams-retire! state 3 42)))
152 (stream-test-input! state #u8(14 2 0 1 9))
153 (assert-equal (list #u8(5 2 42)) (map encode-frame (streams-retire! state 2 42)))))
155 (test "retiring a gapped stream discards later bytes but keeps final-size validation"
156 (let ((state (stream-test-state)))
157 (stream-test-input! state #u8(14 0 2 2 3 4))
158 (streams-retire! state 0 42)
159 (assert-equal (list #u8(16 24)) (map encode-frame (streams-credit-frames! state)))
160 (stream-test-input! state #u8(14 0 0 2 1 2))
161 (assert-equal '() (streams-credit-frames! state))
162 (assert-equal 6
163 (stream-test-code (lambda () (stream-test-input! state #u8(4 0 0 3)))))
164 (stream-test-input! state #u8(4 0 0 4))
165 (assert-equal 0 (streams-reset-code state 0))
166 (assert-equal '() (streams-credit-frames! state))
167 (assert-equal 6
168 (stream-test-code (lambda () (stream-test-input! state #u8(15 0 3 2 4 5)))))))
170 (test "read then cancelled bytes reclaim connection credit exactly once"
171 (let ((state (stream-test-state)))
172 (stream-test-input! state #u8(14 0 0 3 1 2 3))
173 (assert-equal #u8(1 2) (streams-read! state 0 2))
174 (streams-retire! state 0 42)
175 (assert-equal (list #u8(16 23)) (map encode-frame (streams-credit-frames! state)))
176 (stream-test-input! state #u8(15 0 3 1 4))
177 (assert-equal (list #u8(16 24)) (map encode-frame (streams-credit-frames! state)))
178 (stream-test-input! state #u8(15 0 3 1 4))
179 (assert-equal '() (streams-credit-frames! state))
180 (assert-equal 6
181 (stream-test-code (lambda () (stream-test-input! state #u8(14 0 4 1 5)))))))
183 (test "peer RESET final size reclaims unread connection credit and needs no STOP"
184 (let ((state (stream-test-state)))
185 (stream-test-input! state #u8(14 0 0 2 1 2))
186 (streams-read! state 0 1)
187 (stream-test-input! state #u8(4 0 0 4))
188 (assert-equal (list #u8(16 24)) (map encode-frame (streams-credit-frames! state)))
189 (assert-equal (list #u8(4 0 42 0)) (map encode-frame (streams-retire! state 0 42)))
190 (stream-test-input! state #u8(4 0 0 4))
191 (assert-equal '() (streams-credit-frames! state))))
193 (test "a completely received FIN needs no STOP and retirement does not open streams"
194 (let ((state (stream-test-state)))
195 (stream-test-input! state #u8(15 0 0 2 1 2))
196 (assert-equal (list #u8(4 0 42 0)) (map encode-frame (streams-retire! state 0 42)))
197 (assert-error (streams-retire! state 4 42))
198 (assert-error (streams-retire! state 0 -1))))
200 (test "close clears streams and is idempotent"
201 (let ((state (stream-test-state)))
202 (stream-test-input! state #u8(14 0 0 1 99))
203 (streams-close! state)
204 (streams-close! state)
205 (assert-error (streams-read! state 0)))))