AtlatestRepositorysigil-quic

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

1(import (sigil test)
2 (sigil socket)
3 (sigil crypto)
4 (sigil quic bytes)
5 (sigil quic frame)
6 (sigil quic packet)
7 (sigil quic connection)
8 (sigil quic stream)
9 (sigil quic endpoint))
11(define qm3-original #u8(1 2 3 4 5 6 7 8))
12(define qm3-source #u8(11 12 13 14 15 16 17 18))
14(define (qm3-crypto offset bytes)
15 (make-quic-frame 6 (list (cons 'offset offset) (cons 'data bytes))))
17(define (qm3-initial body)
18 (let* ((keys (initial-keys qm3-original 'client))
19 (header (encode-packet-header 'initial qm3-original qm3-source #u8() 0 4 17 length-width: 2))
20 (payload (bytes-append body
21 (make-bytevector (- 1200 (bytevector-length header) 16 (bytevector-length body)) 0))))
22 (dynamic-wind
23 (lambda () #t)
24 (lambda () (seal-quic-packet keys 'initial qm3-original qm3-source 0 payload length-width: 2))
25 (lambda () (discard-packet-keys! keys)))))
27(define (qm3-live-initial)
28 (let ((client (make-quic-client (list #u8(113 49)) "example.test" '() (lambda (certificates name) #f)
29 destination-id: qm3-original local-id: qm3-source)))
30 (quic-start! client 0)
31 (let ((packet (car (quic-take-datagrams! client 0))))
32 (quic-close! client)
33 packet)))
35(define (qm3-with-pair procedure (keys: (pin? #f) (first-timeout #f)))
36 (let* ((key (ecdsa-p256-generate-keypair))
37 (connection (make-quic-client (list #u8(113 49)) "example.test"
38 (stream-transport-parameters 'client)
39 (lambda (certificates name)
40 (and (equal? certificates (list #u8(48 1 0))) (cdr key)))
41 destination-id: #u8(31 32 33 34 35 36 37 38)))
42 (client-stream (make-quic-stream connection))
43 (client (open-quic-endpoint)) (server (open-quic-endpoint)) (sender (udp-socket))
44 (pair (vector connection #f client-stream #f client server 0 sender)))
45 (dynamic-wind
46 (lambda () #t)
47 (lambda ()
48 (unless (udp-bind sender 0 "127.0.0.1") (error "QM3 sender bind failed"))
49 (quic-endpoint-accept! server
50 (lambda (original source)
51 (vector-set! pair 6 (+ 1 (vector-ref pair 6)))
52 (let* ((candidate (make-quic-server (list #u8(113 49))
53 (stream-transport-parameters 'server)
54 (list #u8(48 1 0)) (car key) original source
55 handshake-timeout: (if (and first-timeout (= (vector-ref pair 6) 1)) first-timeout 10000)))
56 (stream (make-quic-stream candidate)))
57 (vector-set! pair 1 candidate)
58 (vector-set! pair 3 stream)
59 candidate))
60 peer-port: (and pin? (cadr (socket-local-address sender))))
61 (procedure pair))
62 (lambda ()
63 (socket-close sender)
64 (quic-endpoint-close! client)
65 (quic-endpoint-close! server)))))
67(define (qm3-send! pair packet)
68 (assert-equal (bytevector-length packet)
69 (udp-send (vector-ref pair 7) packet "127.0.0.1" (quic-endpoint-port (vector-ref pair 5))))
70 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 5))
72(define (qm3-ready! pair)
73 (quic-endpoint-attach! (vector-ref pair 4) (vector-ref pair 0) (quic-endpoint-port (vector-ref pair 5)))
74 (let loop ((attempt 0))
75 (unless (and (quic-confirmed? (vector-ref pair 0))
76 (quic-endpoint-connection (vector-ref pair 5))
77 (quic-confirmed? (quic-endpoint-connection (vector-ref pair 5))))
78 (when (= attempt 1000) (error "QM3 real client handshake did not finish"))
79 (quic-endpoint-step! (vector-ref pair 4))
80 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 1)
81 (loop (+ attempt 1)))))
83(define (qm3-check-available! pair calls)
84 (let ((server (vector-ref pair 5)))
85 (assert-false (quic-endpoint-closed? server))
86 (assert-false (quic-endpoint-connection server))
87 (assert-equal calls (vector-ref pair 6))
88 (when (and (not (quic-endpoint-closed? server)) (not (quic-endpoint-connection server)))
89 (qm3-ready! pair)
90 (assert-equal (+ calls 1) (vector-ref pair 6)))))
92(test-group "Authenticated private endpoint admission"
93 (test "qm3: parseable Initial with a bad tag cannot claim the factory or port"
94 (qm3-with-pair
95 (lambda (pair)
96 (let ((packet (qm3-initial (encode-frames (list (qm3-crypto 0 #u8(1 0 0 0))) 'initial 'client))))
97 (qm3-send! pair (flip-byte packet (- (bytevector-length packet) 1))))
98 (qm3-check-available! pair 0))))
99 (test "qm3: authenticated PING without CRYPTO cannot consume admission"
100 (qm3-with-pair
101 (lambda (pair)
102 (qm3-send! pair (qm3-initial (encode-frames (list (make-quic-frame 1 '())) 'initial 'client)))
103 (qm3-check-available! pair 0))))
104 (test "qm3: authenticated CRYPTO with a nonzero first offset cannot consume admission"
105 (qm3-with-pair
106 (lambda (pair)
107 (qm3-send! pair (qm3-initial (encode-frames (list (qm3-crypto 1 #u8(1 0 0 0))) 'initial 'client)))
108 (qm3-check-available! pair 0))))
109 (test "qm3: empty CRYPTO at zero cannot consume admission"
110 (qm3-with-pair
111 (lambda (pair)
112 (qm3-send! pair (qm3-initial (encode-frames (list (qm3-crypto 0 #u8())) 'initial 'client)))
113 (qm3-check-available! pair 0))))
114 (test "qm3: authenticated malformed frame encoding cannot invoke the factory"
115 (qm3-with-pair
116 (lambda (pair)
117 (qm3-send! pair (qm3-initial #u8(255)))
118 (qm3-check-available! pair 0))))
119 (test "qm3: early authenticated TLS failure releases admission for a different client port"
120 (qm3-with-pair
121 (lambda (pair)
122 (qm3-send! pair (qm3-initial (encode-frames (list (qm3-crypto 0 #u8(1 0 0 0))) 'initial 'client)))
123 (assert-true (quic-closed? (vector-ref pair 1)))
124 (assert-error (quic-stream-read! (vector-ref pair 3)))
125 (qm3-check-available! pair 1))))
126 (test "qm3: pre-confirmation handshake timeout re-arms the original factory"
127 (qm3-with-pair
128 (lambda (pair)
129 (qm3-send! pair (qm3-live-initial))
130 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 5)
131 (assert-true (quic-closed? (vector-ref pair 1)))
132 (qm3-check-available! pair 1))
133 first-timeout: 1))
134 (test "qm3: early failure restores the configured port restriction"
135 (qm3-with-pair
136 (lambda (pair)
137 (qm3-send! pair (qm3-initial (encode-frames (list (qm3-crypto 0 #u8(1 0 0 0))) 'initial 'client)))
138 (assert-false (quic-endpoint-closed? (vector-ref pair 5)))
139 (assert-false (quic-endpoint-connection (vector-ref pair 5)))
140 (quic-endpoint-attach! (vector-ref pair 4) (vector-ref pair 0) (quic-endpoint-port (vector-ref pair 5)))
141 (quic-endpoint-step! (vector-ref pair 4))
142 (quic-endpoint-step! (vector-ref pair 5) wait-ms: 5)
143 (assert-equal 1 (vector-ref pair 6))
144 (assert-false (quic-endpoint-connection (vector-ref pair 5))))
145 pin?: #t))
146 (test "qm3: authenticated offset-zero ClientHello still confirms both roles"
147 (qm3-with-pair
148 (lambda (pair)
149 (qm3-ready! pair)
150 (assert-equal 1 (vector-ref pair 6))
151 (assert-true (quic-confirmed? (vector-ref pair 0)))
152 (assert-true (quic-confirmed? (vector-ref pair 1)))))))