AtlatestRepositorysigil-quic
sigil-quic / tree / testtest-idle-timeout.sgl
1
(import (sigil test)2
(sigil crypto)3
(sigil socket)4
(sigil quic bytes)5
(sigil quic wire)6
(sigil quic frame)7
(sigil quic connection)8
(sigil quic stream)9
(sigil quic endpoint)10
(sigil quic transport parameters))12
(define (qm2-parameters role timeout)13
(append (stream-transport-parameters role receive-window: 16)14
(if timeout (list (cons 1 (encode-varint timeout))) '())))16
(define (qm2-deliver! pair side now)17
(for-each (lambda (packet) (quic-receive! (vector-ref pair (- 1 side)) packet now))18
(quic-take-datagrams! (vector-ref pair side) now)))20
(define (qm2-pair client-timeout server-timeout)21
(let* ((key (ecdsa-p256-generate-keypair)) (original #u8(1 2 3 4 5 6 7 8))22
(client-id #u8(11 12 13 14 15 16 17 18))23
(client (make-quic-client (list #u8(113 49)) "example.test" (qm2-parameters 'client client-timeout)24
(lambda (certificates name)25
(and (equal? certificates (list #u8(48 1 0))) (cdr key)))26
local-id: client-id destination-id: original))27
(server (make-quic-server (list #u8(113 49)) (qm2-parameters 'server server-timeout)28
(list #u8(48 1 0)) (car key) original client-id))29
(pair (vector client server (make-quic-stream client) (make-quic-stream server))))30
(quic-start! client 0)31
(do ((i 0 (+ i 1))) ((= i 8))32
(qm2-deliver! pair 0 0)33
(qm2-deliver! pair 1 0))34
(assert-true (quic-confirmed? client))35
(assert-true (quic-confirmed? server))36
pair))38
(define (qm2-ping! connection now)39
(quic-send-frame! connection (make-quic-frame 1 '()))40
(quic-take-datagrams! connection now))42
(define (qm2-assert-expired connection now)43
(assert-true (quic-closed? connection))44
(assert-true (quic-idle-expired? connection))45
(assert-false (quic-failure connection))46
(assert-equal '() (quic-take-datagrams! connection now)))48
(test-group "QUIC idle timeout regressions"49
(test "qm2: both roles advertise the default max_idle_timeout"50
(let ((pair (qm2-pair #f #f)))51
(for-each52
(lambda (connection)53
(assert-equal 30000 (transport-parameter-integer (quic-local-parameters connection) 1 0))54
(assert-equal 30000 (transport-parameter-integer (quic-peer-parameters connection) 1 0)))55
(list (vector-ref pair 0) (vector-ref pair 1)))))56
(test "qm2: minimum nonzero timeout closes silently and releases attached state"57
(for-each58
(lambda (timeouts)59
(let* ((pair (qm2-pair (car timeouts) (cadr timeouts))) (closed 0))60
(quic-on-close! (vector-ref pair 0) (lambda () (set! closed (+ closed 1))))61
(for-each62
(lambda (connection)63
(quic-tick! connection 3999)64
(assert-false (quic-closed? connection))65
(quic-tick! connection 4000)66
(qm2-assert-expired connection 4000))67
(list (vector-ref pair 0) (vector-ref pair 1)))68
(quic-tick! (vector-ref pair 0) 4001)69
(assert-equal 1 closed)70
(assert-error (quic-stream-read! (vector-ref pair 2)))71
(assert-error (quic-stream-read! (vector-ref pair 3)))))72
'((6000 4000) (0 4000) (4000 0))))73
(test "qm2: explicit zero on both sides disables idle expiry"74
(let ((pair (qm2-pair 0 0)))75
(for-each76
(lambda (connection)77
(quic-tick! connection 100000)78
(assert-false (quic-closed? connection))79
(assert-false (quic-idle-expired? connection)))80
(list (vector-ref pair 0) (vector-ref pair 1)))))81
(test "qm2: tiny negotiated idle timeout is floored at three PTOs"82
(let* ((pair (qm2-pair 1 0)) (client (vector-ref pair 0)))83
(quic-tick! client 2999)84
(assert-false (quic-closed? client))85
(quic-tick! client 3000)86
(qm2-assert-expired client 3000)))87
(test "qm2: new authenticated input restarts the idle timer"88
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0)))89
(for-each (lambda (packet) (quic-receive! client packet 3000))90
(qm2-ping! (vector-ref pair 1) 3000))91
(quic-tick! client 6999)92
(assert-false (quic-closed? client))93
(quic-tick! client 7000)94
(qm2-assert-expired client 7000)))95
(test "qm2: unauthenticated input cannot postpone idle expiry"96
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0))97
(packets (qm2-ping! (vector-ref pair 1) 3500)))98
(for-each99
(lambda (packet)100
(quic-receive! client (flip-byte packet (- (bytevector-length packet) 1)) 3500)) packets)101
(quic-tick! client 4000)102
(qm2-assert-expired client 4000)))103
(test "qm2: only the first eliciting send since receipt restarts the timer"104
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0)))105
(assert-true (pair? (qm2-ping! client 3000)))106
(assert-true (pair? (qm2-ping! client 6000)))107
(quic-take-datagrams! client 6999)108
(assert-false (quic-closed? client))109
(quic-tick! client 7000)110
(qm2-assert-expired client 7000)))111
(test "qm2: ACK-only output does not restart the timer"112
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0)))113
(for-each (lambda (packet) (quic-receive! client packet 3000))114
(qm2-ping! (vector-ref pair 1) 3000))115
(assert-true (pair? (quic-take-datagrams! client 6000)))116
(quic-tick! client 7000)117
(qm2-assert-expired client 7000)))118
(test "qm2: replay of an authenticated old packet cannot keep a connection alive"119
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0))120
(packets (qm2-ping! (vector-ref pair 1) 1000)))121
(for-each (lambda (packet) (quic-receive! client packet 1000)) packets)122
(quic-take-datagrams! client 1000)123
(for-each (lambda (packet) (quic-receive! client packet 4500)) packets)124
(quic-tick! client 5000)125
(qm2-assert-expired client 5000)))126
(test "qm2: late receive and output calls expire before admitting new activity"127
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0))128
(packets (qm2-ping! (vector-ref pair 1) 3500)))129
(for-each (lambda (packet) (quic-receive! client packet 4000)) packets)130
(qm2-assert-expired client 4000))131
(let* ((pair (qm2-pair 4000 4000)) (client (vector-ref pair 0)))132
(quic-send-frame! client (make-quic-frame 1 '()))133
(assert-equal '() (quic-take-datagrams! client 4000))134
(qm2-assert-expired client 4000)))135
(test "qm2: idle floor follows the current bounded PTO backoff"136
(let* ((pair (qm2-pair 1 0)) (client (vector-ref pair 0)))137
(qm2-ping! client 100)138
(quic-tick! client 1100)139
(quic-take-datagrams! client 1100)140
(quic-tick! client 3100)141
(quic-take-datagrams! client 3100)142
(quic-take-datagrams! client 6100)143
(assert-false (quic-closed? client))144
(quic-tick! client 12100)145
(qm2-assert-expired client 12100)))146
(test "qm2: a confirmed private UDP endpoint expires and releases its socket"147
(let* ((key (ecdsa-p256-generate-keypair)) (original #u8(1 2 3 4 5 6 7 8))148
(connection (make-quic-client (list #u8(113 49)) "example.test" (qm2-parameters 'client 1)149
(lambda (certificates name) (cdr key)) destination-id: original))150
(client (open-quic-endpoint)) (server (open-quic-endpoint))151
(server-connection #f) (server-stream #f) (replacement (udp-socket)))152
(dynamic-wind153
(lambda () #t)154
(lambda ()155
(make-quic-stream connection)156
(quic-endpoint-accept! server157
(lambda (dcid scid)158
(set! server-connection (make-quic-server (list #u8(113 49)) (qm2-parameters 'server 1)159
(list #u8(48 1 0)) (car key) dcid scid))160
(set! server-stream (make-quic-stream server-connection))161
server-connection))162
(quic-endpoint-attach! client connection (quic-endpoint-port server))163
(let loop ((attempt 0))164
(unless (and server-connection (quic-confirmed? server-connection) (quic-confirmed? connection))165
(when (= attempt 1000) (error "QM2 endpoint handshake did not finish"))166
(quic-endpoint-step! client wait-ms: 1)167
(quic-endpoint-step! server wait-ms: 1)168
(loop (+ attempt 1))))169
(do ((i 0 (+ i 1))) ((= i 8))170
(quic-endpoint-step! client)171
(quic-endpoint-step! server))172
(let loop ((attempt 0))173
(when (and (< attempt 90) (not (quic-endpoint-closed? server)))174
(quic-endpoint-step! server wait-ms: 50)175
(loop (+ attempt 1))))176
(assert-true (quic-endpoint-closed? server))177
(assert-true (quic-idle-expired? server-connection))178
(assert-false (quic-failure server-connection))179
(assert-error (quic-stream-read! server-stream))180
(assert-true (udp-bind replacement (quic-endpoint-port server) "127.0.0.1")))181
(lambda ()182
(socket-close replacement)183
(quic-endpoint-close! client)184
(quic-endpoint-close! server))))))