AtlatestRepositorysigil-http3

sigil-http3 / tree / tools / interop / testtest-echo.sgl

1(import (sigil test)
2 (sigil quic bytes)
3 (sigil http3 error)
4 (sigil http3 fields)
5 (sigil http3 frame)
6 (sigil http3 qpack)
7 (sigil http3 webtransport wire)
8 (sigil http3 interop echo))
9
10;; Explicitly simulated output operations. Passing these tests does not prove
11;; QUIC DATAGRAM or Chrome WebTransport session interoperability.
12(define (echo-test-fixture (keys: (credits 1) (datagrams? #t) (negotiation 'ready)))
13 (let* ((writes '())
14 (retired '())
15 (datagrams '())
16 (events '())
17 (next-uni 15)
18 (echo
19 (make-interop-echo authority: "unit.test" origins: '("https://unit.test")
20 send-stream: (lambda (id bytes fin?) (set! writes (cons (vector id bytes fin?) writes)))
21 open-uni:
22 (lambda ()
23 (and (> credits 0)
24 (let ((id next-uni))
25 (set! credits (- credits 1))
26 (set! next-uni (+ next-uni 4))
27 id)))
28 retire: (lambda (id code) (set! retired (cons (vector id code) retired)))
29 send-datagram:
30 (lambda (bytes)
31 (and datagrams? (begin (set! datagrams (cons bytes datagrams)) #t)))
32 negotiation: (lambda () negotiation)
33 on-event: (lambda (event) (set! events (cons event events))))))
34 (vector echo (lambda () (reverse writes)) (lambda () (reverse retired))
35 (lambda () (reverse datagrams)) (lambda () (reverse events)))))
37(define (echo-test-connect! fixture (keys: (id 0) (authority "unit.test")
38 (origin "https://unit.test") (path "/echo")))
39 (interop-echo-input! (vector-ref fixture 0) id
40 (encode-h3-frame 1
41 (encode-qpack-fields
42 (map (lambda (field) (cons (h3-ascii-bytes (car field)) (h3-ascii-bytes (cdr field))))
43 (list (cons ":method" "CONNECT") (cons ":protocol" "webtransport")
44 (cons ":scheme" "https") (cons ":authority" authority)
45 (cons ":path" path) (cons "origin" origin))))) #f))
47(define (echo-test-writes fixture)
48 ((vector-ref fixture 1)))
50(define (echo-test-retired fixture)
51 ((vector-ref fixture 2)))
53(define (echo-test-code thunk)
54 (guard (exn ((h3-error? exn) (h3-error-code exn)) (else (raise exn)))
55 (thunk)
56 #f))
58(test-group "Echo application with simulated transport output"
59 (test "accepted CONNECT stays open and bidi echoes contain no response prefix"
60 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
61 (echo-test-connect! fixture)
62 (let* ((response (car (echo-test-writes fixture)))
63 (fields (decode-qpack-fields (vector-ref (decode-h3-frame (vector-ref response 1)) 1))))
64 (assert-false (vector-ref response 2))
65 (assert-true (h3-ascii=? (h3-field-ref fields ":status") "200")))
66 (interop-echo-input! echo 4 #u8(64 65 0 7 8) #f)
67 (interop-echo-input! echo 4 #u8(9) #t)
68 (assert-equal (list (vector 4 #u8(7 8) #f) (vector 4 #u8(9) #t))
69 (cdr (echo-test-writes fixture)))))
71 (test "uni echo pairs one server stream and sends its prefix once"
72 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
73 (echo-test-connect! fixture)
74 (interop-echo-input! echo 14 #u8(64 84 0 11) #f)
75 (interop-echo-input! echo 14 #u8(12 13) #t)
76 (assert-equal
77 (list (vector 15 #u8(64 84 0) #f) (vector 15 #u8(11) #f) (vector 15 #u8(12 13) #t))
78 (cdr (echo-test-writes fixture)))))
80 (test "missing uni send credit retires the input without a new output queue"
81 (let* ((fixture (echo-test-fixture credits: 0)) (echo (vector-ref fixture 0)))
82 (echo-test-connect! fixture)
83 (interop-echo-input! echo 14 #u8(64 84 0 11) #f)
84 (interop-echo-input! echo 14 #u8(12) #t)
85 (assert-equal (list (vector 14 #x3994bd84)) (echo-test-retired fixture))
86 (assert-equal 1 (length (echo-test-writes fixture)))))
88 (test "authority, path and origin are exact echo-only policy checks"
89 (for-each
90 (lambda (values)
91 (let ((fixture (echo-test-fixture)))
92 (echo-test-connect! fixture authority: (car values) origin: (cadr values) path: (caddr values))
93 (let* ((response (car (echo-test-writes fixture)))
94 (fields (decode-qpack-fields (vector-ref (decode-h3-frame (vector-ref response 1)) 1))))
95 (assert-true (vector-ref response 2))
96 (assert-true (h3-ascii=? (h3-field-ref fields ":status") "403")))))
97 '(("other.test" "https://unit.test" "/echo")
98 ("unit.test" "https://other.test" "/echo") ("unit.test" "https://unit.test" "/files"))))
100 (test "datagrams preserve their session prefix and blocked output is dropped"
101 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
102 (echo-test-connect! fixture)
103 (interop-echo-datagram! echo #u8(0 7 8))
104 (assert-equal '(#u8(0 7 8)) ((vector-ref fixture 3))))
105 (let* ((fixture (echo-test-fixture datagrams?: #f)) (echo (vector-ref fixture 0)))
106 (echo-test-connect! fixture)
107 (interop-echo-datagram! echo #u8(0 7 8))
108 (assert-equal '() ((vector-ref fixture 3)))
109 (assert-equal '#(datagram-echo-dropped 0 2) (car (reverse ((vector-ref fixture 4)))))))
111 (test "peer session close retires paired uni output and sends one CONNECT FIN"
112 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
113 (echo-test-connect! fixture)
114 (interop-echo-input! echo 4 #u8(64 65 0 7) #f)
115 (interop-echo-input! echo 14 #u8(64 84 0 8) #f)
116 (interop-echo-input! echo 0 (encode-h3-frame 0 (encode-webtransport-close 0 #u8())) #t)
117 (assert-equal (list (vector 14 #x10c) (vector 15 #x10c) (vector 4 #x10c))
118 (echo-test-retired fixture))
119 (assert-equal '#(0 #u8() #t) (car (reverse (echo-test-writes fixture))))
120 (interop-echo-datagram! echo #u8(0 99))
121 (interop-echo-close! echo)
122 (interop-echo-close! echo)
123 (assert-equal 5 (length (echo-test-writes fixture)))
124 (assert-equal '() ((vector-ref fixture 3)))))
126 (test "CONNECT reset cancels both uni directions without sending FIN"
127 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
128 (echo-test-connect! fixture)
129 (interop-echo-input! echo 14 #u8(64 84 0 8) #f)
130 (interop-echo-reset! echo 0 88)
131 (interop-echo-reset! echo 0 88)
132 (assert-equal (list (vector 0 #x10c) (vector 14 #x10c) (vector 15 #x10c))
133 (echo-test-retired fixture))
134 (assert-equal 3 (length (echo-test-writes fixture)))))
136 (test "authenticated callback rate is bounded and resets on the next window"
137 (let* ((fixture (echo-test-fixture negotiation: 'pending)) (echo (vector-ref fixture 0)))
138 (do ((index 0 (+ index 1))) ((= index 128)) (interop-echo-input! echo 4 #u8() #f))
139 (assert-equal #x107 (echo-test-code (lambda () (interop-echo-input! echo 4 #u8() #f))))
140 (interop-echo-step! echo 1000)
141 (interop-echo-input! echo 4 #u8() #f)
142 (assert-error (interop-echo-step! echo 999))))
144 (test "authenticated byte rate counts discarded input as well"
145 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
146 (interop-echo-input! echo 14 #u8(33) #f)
147 (do ((index 0 (+ index 1))) ((= index 15)) (interop-echo-input! echo 14 (make-bytevector 4096 0) #f))
148 (interop-echo-input! echo 14 (make-bytevector 4095 0) #f)
149 (assert-equal #x107 (echo-test-code (lambda () (interop-echo-input! echo 14 #u8(1) #f))))))
151 (test "request response rate is separate from session and byte limits"
152 (let* ((fixture (echo-test-fixture)) (echo (vector-ref fixture 0)))
153 (do ((index 0 (+ index 1))) ((= index 8)) (echo-test-connect! fixture id: (* index 4)))
154 (assert-equal 8 (length (echo-test-writes fixture)))
155 (assert-equal #x107 (echo-test-code (lambda () (echo-test-connect! fixture id: 32))))
156 (interop-echo-step! echo 1000)
157 (echo-test-connect! fixture id: 36)
158 (assert-equal 9 (length (echo-test-writes fixture))))))