AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-receive-bounds.sgl

1(import (sigil test)
2 (sigil socket)
3 (sigil quic bytes)
4 (sigil quic error)
5 (sigil quic frame)
6 (sigil quic packet)
7 (sigil quic connection)
8 (sigil quic endpoint))
9
10(define ql5-original #u8(1 2 3 4 5 6 7 8))
11(define ql5-client-id #u8(11 12 13 14 15 16 17 18))
12(define ql5-server-id #u8(21 22 23 24 25 26 27 28))
14(define (ql5-close-frame)
15 (make-quic-frame 28 (list (cons 'error 0) (cons 'frame-type 0) (cons 'reason #u8()))))
17(define (ql5-packet keys number close? (keys: (destination ql5-client-id)))
18 (seal-quic-packet keys 'initial destination ql5-server-id number
19 (if close? (encode-frames (list (ql5-close-frame)) 'initial 'server) #u8(0))))
21(define (ql5-coalesce count make-packet)
22 (let loop ((index 0) (size 0) (parts '()))
23 (if (= index count)
24 (let ((out (make-bytevector size 0)) (offset 0))
25 (for-each
26 (lambda (packet)
27 (bytevector-copy! out offset packet)
28 (set! offset (+ offset (bytevector-length packet))))
29 (reverse parts))
30 out)
31 (let ((packet (make-packet index)))
32 (loop (+ index 1) (+ size (bytevector-length packet)) (cons packet parts))))))
34(define (ql5-with-client procedure (keys: (failure-limit quic-aes-gcm-failure-limit) (start? #t)))
35 (let ((connection (make-quic-client (list #u8(113 49)) "example.test" '() (lambda (certificates name) #f)
36 local-id: ql5-client-id destination-id: ql5-original
37 aead-failure-limit: failure-limit))
38 (keys (initial-keys ql5-original 'server)))
39 (dynamic-wind
40 (lambda () #t)
41 (lambda ()
42 (when start?
43 (quic-start! connection 0)
44 (quic-take-datagrams! connection 0))
45 (procedure connection keys))
46 (lambda ()
47 (quic-close! connection)
48 (discard-packet-keys! keys)))))
50(define (ql5-with-endpoint procedure)
51 (ql5-with-client
52 (lambda (connection keys)
53 (let ((endpoint (open-quic-endpoint)) (sender (udp-socket)) (noise (udp-socket)))
54 (dynamic-wind
55 (lambda () #t)
56 (lambda ()
57 (unless (and (udp-bind sender 0 "127.0.0.1") (udp-bind noise 0 "127.0.0.1"))
58 (error "QL5 test: sender bind failed"))
59 (quic-endpoint-attach! endpoint connection (cadr (socket-local-address sender)))
60 (procedure connection endpoint sender noise keys))
61 (lambda ()
62 (quic-endpoint-close! endpoint)
63 (socket-close sender)
64 (socket-close noise)))))
65 start?: #f))
67(define (ql5-send! socket endpoint bytes)
68 (assert-equal (bytevector-length bytes)
69 (udp-send socket bytes "127.0.0.1" (quic-endpoint-port endpoint))))
71(test-group "Bounded QUIC receive work"
72 (test "ql5: packets after an eight-packet valid prefix wait for retransmission"
73 (ql5-with-client
74 (lambda (connection keys)
75 (let ((close (ql5-packet keys 8 #t)))
76 (quic-receive! connection
77 (bytes-append (ql5-coalesce 8 (lambda (number) (ql5-packet keys number #f))) close) 0)
78 (assert-false (quic-closed? connection))
79 (assert-false (quic-failure connection))
80 (quic-receive! connection close 0)
81 (assert-true (quic-closed? connection))
82 (assert-equal 28 (quic-frame-type (quic-failure connection)))))))
83 (test "ql5: a coalesced bad-tag flood spends at most eight authentication failures"
84 (ql5-with-client
85 (lambda (connection keys)
86 (let* ((packet (ql5-packet keys 0 #f))
87 (corrupt (flip-byte packet (- (bytevector-length packet) 1)))
88 (flood (ql5-coalesce 1000 (lambda (number) corrupt))))
89 (assert-true (<= (bytevector-length flood) packet-datagram-limit))
90 (quic-receive! connection flood 0)
91 (assert-false (quic-closed? connection))
92 (quic-receive! connection (ql5-packet keys 1 #f) 0)
93 (assert-false (quic-closed? connection))
94 (quic-receive! connection corrupt 0)
95 (assert-true (quic-closed? connection))
96 (assert-equal 15 (quic-error-code (quic-failure connection)))))
97 failure-limit: 9))
98 (test "ql5: unrouted packets consume the header-processing budget too"
99 (ql5-with-client
100 (lambda (connection keys)
101 (quic-receive! connection
102 (bytes-append
103 (ql5-coalesce 8
104 (lambda (number) (ql5-packet keys number #f destination: ql5-server-id)))
105 (ql5-packet keys 8 #t)) 0)
106 (assert-false (quic-closed? connection))
107 (quic-receive! connection (ql5-packet keys 8 #t) 0)
108 (assert-true (quic-closed? connection)))))
109 (test "ql5: the eighth authenticated packet is still processed"
110 (ql5-with-client
111 (lambda (connection keys)
112 (quic-receive! connection
113 (bytes-append (ql5-coalesce 7 (lambda (number) (ql5-packet keys number #f)))
114 (ql5-packet keys 7 #t)) 0)
115 (assert-true (quic-closed? connection))
116 (assert-equal 28 (quic-frame-type (quic-failure connection))))))
117 (test "ql5: rejected ports do not spend the admitted receive budget"
118 (ql5-with-endpoint
119 (lambda (connection endpoint sender noise keys)
120 (do ((i 0 (+ i 1))) ((= i 40)) (ql5-send! noise endpoint #u8(0)))
121 (ql5-send! sender endpoint (ql5-packet keys 0 #t))
122 (quic-endpoint-step! endpoint wait-ms: 5)
123 (assert-true (quic-closed? connection))
124 (assert-true (quic-endpoint-closed? endpoint)))))
125 (test "ql5: the separate drain ceiling yields before an unbounded noise scan"
126 (ql5-with-endpoint
127 (lambda (connection endpoint sender noise keys)
128 (do ((i 0 (+ i 1))) ((= i 96)) (ql5-send! noise endpoint #u8(0)))
129 (ql5-send! sender endpoint (ql5-packet keys 0 #t))
130 (quic-endpoint-step! endpoint wait-ms: 5)
131 (assert-false (quic-closed? connection))
132 (quic-endpoint-step! endpoint wait-ms: 5)
133 (assert-true (quic-closed? connection)))))
134 (test "ql5: admitted datagrams retain their own per-step ceiling"
135 (ql5-with-endpoint
136 (lambda (connection endpoint sender noise keys)
137 (do ((i 0 (+ i 1))) ((= i 32)) (ql5-send! sender endpoint (ql5-packet keys i #f)))
138 (ql5-send! sender endpoint (ql5-packet keys 32 #t))
139 (quic-endpoint-step! endpoint wait-ms: 5)
140 (assert-false (quic-closed? connection))
141 (quic-endpoint-step! endpoint wait-ms: 5)
142 (assert-true (quic-closed? connection))))))