AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-endpoint-bind.sgl

1(import (sigil test)
2 (sigil io)
3 (sigil process)
4 (sigil socket)
5 (sigil crypto)
6 (sigil quic bytes)
7 (sigil quic wire)
8 (sigil quic packet)
9 (sigil quic address)
10 (sigil quic connection)
11 (sigil quic stream)
12 (sigil quic endpoint))
14(define ec-original #u8(51 52 53 54 55 56 57 58))
15(define ec-client-id #u8(61 62 63 64 65 66 67 68))
17;; Pair: client/server connections, client/server streams, client/server
18;; endpoints, factory call count. Every resource is owned before later setup.
19(define (ec-with-pair server-bind client-bind server-target client-target procedure)
20 (let ((key #f) (client #f) (server #f) (client-endpoint #f)
21 (server-endpoint #f) (pair #f))
22 (dynamic-wind
23 (lambda () #t)
24 (lambda ()
25 (set! key (ecdsa-p256-generate-keypair))
26 (set! client
27 (make-quic-client (list #u8(113 49)) "example.test"
28 (append (stream-transport-parameters 'client)
29 (list (cons 32 (encode-varint 1200))))
30 (lambda (certificates name)
31 (and (equal? certificates (list #u8(48 1 0))) (cdr key)))
32 destination-id: ec-original local-id: ec-client-id))
33 (set! client-endpoint (open-quic-endpoint host: client-bind))
34 (set! server-endpoint (open-quic-endpoint host: server-bind))
35 (set! pair
36 (vector client #f (make-quic-stream client) #f
37 client-endpoint server-endpoint 0))
38 (quic-endpoint-accept-retry! server-endpoint
39 (lambda (original source retry)
40 (vector-set! pair 6 (+ 1 (vector-ref pair 6)))
41 (set! server
42 (make-quic-server (list #u8(113 49))
43 (append (stream-transport-parameters 'server)
44 (list (cons 32 (encode-varint 1200))))
45 (list #u8(48 1 0)) (car key) original source
46 retry-source-id: retry))
47 (vector-set! pair 1 server)
48 (vector-set! pair 3 (make-quic-stream server))
49 server)
50 peer-host: client-target peer-port: (quic-endpoint-port client-endpoint))
51 (quic-endpoint-attach! client-endpoint client
52 (quic-endpoint-port server-endpoint) peer-host: server-target)
53 (procedure pair))
54 (lambda ()
55 (when client-endpoint (quic-endpoint-close! client-endpoint))
56 (when server-endpoint (quic-endpoint-close! server-endpoint))
57 (when client (quic-abort! client))
58 (when server (quic-abort! server))
59 (when (and (pair? key) (bytevector? (car key)))
60 (do ((index 0 (+ index 1))) ((= index (bytevector-length (car key))))
61 (bytevector-u8-set! (car key) index 0)))))))
63(define (ec-await pair stage predicate)
64 (guard (exn
65 (else
66 (println "BIND-PUMP stage=~a exception=~a" stage exn)
67 (raise exn)))
68 (let loop ((round 0))
69 (cond
70 ((predicate) #t)
71 ((or (= round 2000)
72 (quic-endpoint-closed? (vector-ref pair 4))
73 (quic-endpoint-closed? (vector-ref pair 5)))
74 (println "BIND-PUMP stage=~a round=~a factories=~a" stage round (vector-ref pair 6))
75 (for-each
76 (lambda (index)
77 (let ((endpoint (vector-ref pair index)))
78 (println "BIND-ENDPOINT index=~a host=~a closed=~a failure=~a"
79 index (quic-endpoint-host endpoint)
80 (quic-endpoint-closed? endpoint) (quic-endpoint-failure endpoint))))
81 (list 4 5))
82 (for-each
83 (lambda (index)
84 (let ((connection (vector-ref pair index)))
85 (when connection
86 (println "BIND-CONNECTION index=~a complete=~a confirmed=~a closed=~a failure=~a"
87 index (quic-complete? connection) (quic-confirmed? connection)
88 (quic-closed? connection) (quic-failure connection)))))
89 (list 0 1))
90 #f)
91 (else
92 (quic-endpoint-step! (vector-ref pair 4))
93 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
94 (loop (+ round 1)))))))
96(define (ec-require stage condition)
97 (unless condition (error (string-append "Configured endpoint fixture: " stage)))
98 #t)
100(define (ec-exchange server-bind client-bind server-target client-target)
101 (ec-with-pair server-bind client-bind server-target client-target
102 (lambda (pair)
103 (and
104 (ec-await pair 'confirmation
105 (lambda ()
106 (and (vector-ref pair 1)
107 (quic-confirmed? (vector-ref pair 0))
108 (quic-confirmed? (vector-ref pair 1)))))
109 (let ((client (vector-ref pair 0)) (server (vector-ref pair 1))
110 (client-stream (vector-ref pair 2)) (server-stream (vector-ref pair 3))
111 (request #u8()) (response #u8()) (client-datagram #f) (server-datagram #f))
112 (and
113 (ec-require "single Retry admission" (= 1 (vector-ref pair 6)))
114 (ec-require "server actual peer"
115 (bytes=?
116 (quic-address client-target (quic-endpoint-port (vector-ref pair 4)))
117 (quic-endpoint-peer-address (vector-ref pair 5))))
118 (ec-require "client actual peer"
119 (bytes=?
120 (quic-address server-target (quic-endpoint-port (vector-ref pair 5)))
121 (quic-endpoint-peer-address (vector-ref pair 4))))
122 (ec-require "client binary write"
123 (= 4 (quic-stream-write! client-stream #u8(0 255 128 4) fin?: #t)))
124 (ec-require "client DATAGRAM queue" (quic-send-datagram! client #u8(255 1)))
125 ;; The client's STREAM must arrive before the server can write
126 ;; on that client-initiated bidirectional stream.
127 (ec-await pair 'request
128 (lambda ()
129 (set! request (bytes-append request (quic-stream-read! server-stream)))
130 (unless server-datagram (set! server-datagram (quic-read-datagram! server)))
131 (and (quic-stream-eof? server-stream) server-datagram)))
132 (ec-require "server binary write"
133 (= 5 (quic-stream-write! server-stream #u8(255 0 128 7 8) fin?: #t)))
134 (ec-require "server DATAGRAM queue" (quic-send-datagram! server #u8(0 2)))
135 (ec-await pair 'response
136 (lambda ()
137 (set! response (bytes-append response (quic-stream-read! client-stream)))
138 (unless client-datagram (set! client-datagram (quic-read-datagram! client)))
139 (and (quic-stream-eof? client-stream) client-datagram)))
140 (bytes=? #u8(0 255 128 4) request)
141 (bytes=? #u8(255 0 128 7 8) response)
142 (bytes=? #u8(0 2) client-datagram)
143 (bytes=? #u8(255 1) server-datagram)))))))
145(define (ec-bound? host family)
146 (let ((endpoint #f))
147 (dynamic-wind
148 (lambda () #t)
149 (lambda ()
150 (guard (exn (else #f))
151 (set! endpoint (open-quic-endpoint host: host))
152 (and (equal? host (quic-endpoint-host endpoint))
153 (eq? family (quic-endpoint-family endpoint))
154 (<= 1 (quic-endpoint-port endpoint) 65535))))
155 (lambda () (when endpoint (quic-endpoint-close! endpoint))))))
157(define (ec-initial)
158 (let* ((keys (initial-keys ec-original 'client))
159 (header (encode-packet-header 'initial ec-original ec-client-id #u8() 0 4 17
160 length-width: 2))
161 (payload
162 (bytes-append #u8(6 0 1 1)
163 (make-bytevector (- 1200 (bytevector-length header) 16 4) 0))))
164 (dynamic-wind
165 (lambda () #t)
166 (lambda ()
167 (seal-quic-packet keys 'initial ec-original ec-client-id 0 payload length-width: 2))
168 (lambda () (discard-packet-keys! keys)))))
170(define (ec-restricted-host?)
171 (let ((endpoint #f) (pinned #f) (other #f) (calls 0))
172 (dynamic-wind
173 (lambda () #t)
174 (lambda ()
175 (guard (exn (else #f))
176 (set! endpoint (open-quic-endpoint))
177 (set! pinned (udp-socket 'ipv4))
178 (set! other (udp-socket 'ipv4))
179 (and pinned other
180 (udp-bind pinned 0 "127.0.0.2")
181 (let ((port (cadr (socket-local-address pinned))))
182 (and
183 (udp-bind other port "127.0.0.3")
184 (socket-set-non-blocking! other #t)
185 (begin
186 (quic-endpoint-accept-retry! endpoint
187 (lambda (original source retry)
188 (set! calls (+ calls 1))
189 (error "endpoint bind fixture: unexpected admission"))
190 peer-host: "127.0.0.2" peer-port: port)
191 (= 1200 (udp-send other (ec-initial) "127.0.0.1"
192 (quic-endpoint-port endpoint))))
193 (begin
194 (do ((round 0 (+ round 1))) ((= round 5))
195 (quic-endpoint-step! endpoint wait-ms: 1))
196 (and (= calls 0) (not (quic-endpoint-connection endpoint))
197 (not (quic-endpoint-closed? endpoint))
198 (not (udp-receive other 65535)))))))))
199 (lambda ()
200 (when other (socket-close other))
201 (when pinned (socket-close pinned))
202 (when endpoint (quic-endpoint-close! endpoint))))))
204(test-group "Configured QUIC UDP endpoints"
205 (test "endpoint-bind: IPv4 alias exposes the actual bound host and family"
206 (assert-true (ec-bound? "127.0.0.2" 'ipv4)))
207 (test "endpoint-bind: IPv6 loopback exposes the actual bound host and family"
208 (assert-true (ec-bound? "::1" 'ipv6)))
209 (test "endpoint-bind: invalid numeric hosts and ports are rejected"
210 (for-each
211 (lambda (host) (assert-error (open-quic-endpoint host: host)))
212 (list #f "localhost" "127.0.0.999" "::1%eth0"))
213 (assert-error (open-quic-endpoint host: "::1" port: -1))
214 (assert-error (open-quic-endpoint host: "127.0.0.2" port: 65536)))
215 (test "endpoint-bind: distinct IPv4 addresses complete Retry and binary transfer"
216 (assert-true (ec-exchange "127.0.0.1" "127.0.0.2" "127.0.0.1" "127.0.0.2")))
217 (test "endpoint-bind: IPv6 completes Retry and binary transfer"
218 (assert-true (ec-exchange "::1" "::1" "::1" "::1")))
219 (test "endpoint-bind: IPv4 wildcard returns traffic to the actual peer"
220 (assert-true (ec-exchange "0.0.0.0" "127.0.0.2" "127.0.0.1" "127.0.0.2")))
221 (test "endpoint-bind: IPv6 wildcard completes Retry and transfer"
222 (assert-true (ec-exchange "::" "::1" "::1" "::1")))
223 (test "endpoint-bind: configured peer host restricts admission at the pinned port"
224 (assert-true (ec-restricted-host?)))
225 (test "endpoint-bind: assigned IPv4 interface completes Retry and transfer"
226 (let ((host (or (getenv "QUIC_TEST_INTERFACE_IPV4") "127.0.0.2")))
227 (assert-true (ec-exchange host host host host))))
228 (test "endpoint-bind: assigned IPv6 interface completes Retry and transfer"
229 (let ((host (or (getenv "QUIC_TEST_INTERFACE_IPV6") "::1")))
230 (assert-true (ec-exchange host host host host)))))