AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-path-response.sgl

1(import (sigil test)
2 (sigil crypto)
3 (sigil quic bytes)
4 (sigil quic frame)
5 (sigil quic connection)
6 (sigil quic tls))
7
8;; Public connections authenticate every observed response. Delivery metadata
9;; associates each handler event with its actual datagram size and order.
10(define (path-events pair side type)
11 (filter (lambda (entry) (= type (quic-frame-type (vector-ref entry 0))))
12 (reverse (vector-ref pair (+ side 2)))))
14(define (path-deliver! pair side packets now)
15 (let loop ((remaining packets) (index 0))
16 (unless (null? remaining)
17 (vector-set! pair 4 (bytevector-length (car remaining)))
18 (vector-set! pair 5 index)
19 (quic-receive! (vector-ref pair (- 1 side)) (car remaining) now)
20 (loop (cdr remaining) (+ index 1)))))
22(define (path-flush! pair side now)
23 (let ((packets (quic-take-datagrams! (vector-ref pair side) now)))
24 (path-deliver! pair side packets now)
25 packets))
27(define (path-live? pair)
28 (let loop ((side 0))
29 (or (= side 2)
30 (let ((connection (vector-ref pair side)))
31 (and (quic-confirmed? connection) (not (quic-closed? connection))
32 (not (quic-failure connection)) (loop (+ side 1)))))))
34(define (path-with-pair target-side handler? procedure)
35 (let ((key (ecdsa-p256-generate-keypair)) (client #f) (server #f) (pair #f))
36 (dynamic-wind
37 (lambda () #t)
38 (lambda ()
39 (set! client
40 (make-quic-client (list #u8(113 49)) 'pinned-key '()
41 (make-tls-pin-verifier (cdr key)) local-id: #u8(1)
42 destination-id: #u8(10 11 12 13 14 15 16 17)))
43 (set! server
44 (make-quic-server (list #u8(113 49)) '() (list #u8(48 1 0)) (car key)
45 #u8(10 11 12 13 14 15 16 17) #u8(1) local-id: #u8(2)))
46 (set! pair (vector client server '() '() 0 0))
47 (do ((side 0 (+ side 1))) ((= side 2))
48 (when (or handler? (not (= side target-side)))
49 (let ((index (+ side 2)))
50 (quic-set-frame-handler! (vector-ref pair side)
51 (lambda (frame)
52 (vector-set! pair index
53 (cons (vector frame (vector-ref pair 4) (vector-ref pair 5))
54 (vector-ref pair index))))))))
55 (quic-start! client 0)
56 (do ((round 0 (+ round 1))) ((= round 8))
57 (path-flush! pair 0 0)
58 (path-flush! pair 1 0))
59 (unless (path-live? pair) (error "Path fixture handshake did not complete"))
60 (vector-set! pair 2 '())
61 (vector-set! pair 3 '())
62 (procedure pair (- 1 target-side) target-side))
63 (lambda ()
64 (when client (quic-abort! client))
65 (when server (quic-abort! server))
66 (do ((i 0 (+ i 1))) ((= i (bytevector-length (car key))))
67 (bytevector-u8-set! (car key) i 0))))))
69(define path-data #u8(0 1 0 3 4 0 6 255))
71(define (path-challenge! pair side)
72 (quic-send-frame! (vector-ref pair side)
73 (make-quic-frame 26 (list (cons 'data path-data)))))
75(define (path-replies? entries count)
76 (and (= count (length entries))
77 (let loop ((remaining entries))
78 (or (null? remaining)
79 (and (equal? path-data (frame-ref (vector-ref (car remaining) 0) 'data))
80 (= 1200 (vector-ref (car remaining) 1))
81 (loop (cdr remaining)))))))
83(define (path-normal! target-side handler?)
84 (path-with-pair target-side handler?
85 (lambda (pair source target)
86 (path-challenge! pair source)
87 (let ((challenge (quic-take-datagrams! (vector-ref pair source) 0)))
88 (unless (= 1 (length challenge)) (error "Expected one challenge datagram"))
89 (path-deliver! pair source challenge 0)
90 (path-flush! pair target 0)
91 ;; Duplicate PN may elicit another ACK, never another response.
92 (path-deliver! pair source challenge 0)
93 (path-flush! pair target 0)
94 (let ((after-duplicate (length (path-events pair source 27))))
95 ;; The same challenge bytes in a fresh packet still need a reply.
96 (path-challenge! pair source)
97 (path-flush! pair source 0)
98 (path-flush! pair target 0)
99 (assert-true
100 (and (path-live? pair)
101 (= 1200 (bytevector-length (car challenge)))
102 (= 1 after-duplicate)
103 (null? (path-events pair target 26))
104 (path-replies? (path-events pair source 27) 2))))))))
106(define (path-priority! target-side)
107 (path-with-pair target-side #t
108 (lambda (pair source target)
109 (do ((i 0 (+ i 1))) ((= i 12))
110 (quic-send-frame! (vector-ref pair target) (make-quic-frame 1 '())))
111 (path-challenge! pair source)
112 (path-flush! pair source 0)
113 (path-flush! pair target 0)
114 (let ((replies (path-events pair source 27)))
115 (assert-true
116 (and (path-live? pair) (path-replies? replies 1)
117 ;; An ACK may be first; the twelve queued PINGs must follow.
118 (<= (vector-ref (car replies) 2) 1)
119 (null? (path-events pair target 26))))))))
121(define (path-lost! target-side)
122 (path-with-pair target-side #t
123 (lambda (pair source target)
124 (path-challenge! pair source)
125 (path-flush! pair source 0)
126 ;; Drop ACK and response. The expanded response is the only 1200-byte
127 ;; datagram here; no application data is queued by this fixture.
128 (let ((lost (quic-take-datagrams! (vector-ref pair target) 0)))
129 (quic-tick! (vector-ref pair target) 1000)
130 (path-flush! pair target 1000)
131 (let ((after-pto (length (path-events pair source 27))))
132 (path-challenge! pair source)
133 (path-flush! pair source 1000)
134 (path-flush! pair target 1000)
135 (assert-true
136 (and (path-live? pair)
137 (= 1 (length (filter (lambda (wire) (= 1200 (bytevector-length wire))) lost)))
138 (= 0 after-pto)
139 (path-replies? (path-events pair source 27) 1))))))))
141(define (path-forged! target-side)
142 (path-with-pair target-side #t
143 (lambda (pair source target)
144 (path-challenge! pair source)
145 (let ((packets (quic-take-datagrams! (vector-ref pair source) 0)))
146 (unless (= 1 (length packets)) (error "Expected one forged challenge candidate"))
147 (path-deliver! pair source
148 (map (lambda (wire) (flip-byte wire (- (bytevector-length wire) 1))) packets) 0)
149 (let ((output (path-flush! pair target 0)))
150 (quic-send-frame! (vector-ref pair source) (make-quic-frame 1 '()))
151 (path-flush! pair source 0)
152 (path-flush! pair target 0)
153 (assert-true
154 (and (path-live? pair) (null? output)
155 (null? (path-events pair target 26))
156 (null? (path-events pair source 27)))))))))
158(define (path-ping! target-side)
159 (path-with-pair target-side #t
160 (lambda (pair source target)
161 (quic-send-frame! (vector-ref pair source) (make-quic-frame 1 '()))
162 (let* ((ping (path-flush! pair source 0)) (ack (path-flush! pair target 0)))
163 (assert-true
164 (and (path-live? pair) (= 1 (length ping)) (= 1 (length ack))
165 (< (bytevector-length (car ping)) 1200)
166 (< (bytevector-length (car ack)) 1200)
167 (null? (path-events pair target 26))
168 (null? (path-events pair source 27))))))))
170(test-group "Authenticated responses on the pinned path"
171 (test "ql8-path: client echoes challenges once with expanded datagrams"
172 (path-normal! 0 #t))
173 (test "ql8-path: server echoes challenges once with expanded datagrams"
174 (path-normal! 1 #t))
175 (test "ql8-path: client responds without an application handler"
176 (path-normal! 0 #f))
177 (test "ql8-path: server responds without an application handler"
178 (path-normal! 1 #f))
179 (test "ql8-path: client response precedes application backlog"
180 (path-priority! 0))
181 (test "ql8-path: server response precedes application backlog"
182 (path-priority! 1))
183 (test "ql8-path: client replaces lost responses only after a new challenge"
184 (path-lost! 0))
185 (test "ql8-path: server replaces lost responses only after a new challenge"
186 (path-lost! 1))
187 (test "ql8-path-control: client ignores forged challenges and stays usable"
188 (path-forged! 0))
189 (test "ql8-path-control: server ignores forged challenges and stays usable"
190 (path-forged! 1))
191 (test "ql8-path-control: client ordinary PING and ACK remain small"
192 (path-ping! 0))
193 (test "ql8-path-control: server ordinary PING and ACK remain small"
194 (path-ping! 1)))