AtlatestRepositorysigil-quic
sigil-quic / tree / testtest-application-close.sgl
1
(import (sigil test)2
(sigil io)3
(sigil crypto)4
(sigil quic bytes)5
(sigil quic wire)6
(sigil quic error)7
(sigil quic frame)8
(sigil quic packet)9
(sigil quic connection))11
(define ql2b-original #u8(1 2 3 4 5 6 7 8))12
(define ql2b-client-id #u8(11 12 13 14 15 16 17 18))14
(define (ql2b-client (keys: (packet-limit quic-aes-gcm-packet-limit)))15
(make-quic-client (list #u8(113 49)) "example.test" '() (lambda (chain name) #f)16
local-id: ql2b-client-id destination-id: ql2b-original aead-packet-limit: packet-limit))18
(define (ql2b-with-pair procedure (keys: (client-limit quic-aes-gcm-packet-limit)))19
(let* ((key (ecdsa-p256-generate-keypair))20
(client (make-quic-client (list #u8(113 49)) "example.test" '()21
(lambda (chain name) (and (equal? chain (list #u8(48 1 0))) (cdr key)))22
local-id: ql2b-client-id destination-id: ql2b-original aead-packet-limit: client-limit))23
(server (make-quic-server (list #u8(113 49)) '() (list #u8(48 1 0)) (car key)24
ql2b-original ql2b-client-id)))25
(dynamic-wind26
(lambda () #t)27
(lambda ()28
(quic-start! client 0)29
(do ((i 0 (+ i 1))) ((= i 8))30
(for-each (lambda (packet) (quic-receive! server packet 0)) (quic-take-datagrams! client 0))31
(for-each (lambda (packet) (quic-receive! client packet 0)) (quic-take-datagrams! server 0)))32
(assert-true (quic-confirmed? client))33
(assert-true (quic-confirmed? server))34
(procedure client server))35
(lambda () (quic-close! client) (quic-close! server)))))37
(define (ql2b-check-peer-close! sender receiver type code reason)38
(let ((packets (quic-take-datagrams! sender 0)))39
(assert-equal 1 (length packets))40
(for-each (lambda (packet) (quic-receive! receiver packet 0)) packets)41
(assert-true (quic-closed? receiver))42
(let ((failure (quic-failure receiver)))43
(assert-true (quic-frame? failure))44
(when (quic-frame? failure)45
(assert-equal type (quic-frame-type failure))46
(assert-equal code (frame-ref failure 'error))47
(assert-equal reason (frame-ref failure 'reason))))48
(assert-equal '() (quic-take-datagrams! receiver 0))))50
(test-group "Application and transport close separation"51
(test "ql2-close: application error metadata has a distinct bounded namespace"52
(let ((exception (guard (exn (else exn)) (raise-quic-application-error 900 "closed"))))53
(assert-true (quic-application-error? exception))54
(assert-false (quic-error? exception))55
(assert-equal 900 (quic-application-error-code exception))56
(assert-equal "closed" (quic-application-error-reason exception)))57
(assert-error (raise-quic-application-error (+ quic-varint-maximum 1) "bad"))58
(assert-error (raise-quic-application-error 0 (make-string 600 #\λ))))59
(test "ql2-close: early application close hides code and reason in Initial"60
(let ((client (ql2b-client)) (keys (initial-keys ql2b-original 'client)))61
(dynamic-wind62
(lambda () #t)63
(lambda ()64
(quic-close! client code: 900 reason: "private application state")65
(assert-true (quic-application-error? (quic-failure client)))66
(let* ((packets (quic-take-datagrams! client 0))67
(opened (and (pair? packets) (open-quic-packet (car packets) 0 8 keys -1)))68
(frames (and opened (decode-frames (opened-quic-packet-payload opened) 'initial 'client))))69
(assert-equal 1 (length packets))70
(assert-true (pair? frames))71
(when (pair? frames)72
(assert-equal 28 (quic-frame-type (car frames)))73
(assert-equal 12 (frame-ref (car frames) 'error))74
(assert-equal #u8() (frame-ref (car frames) 'reason)))))75
(lambda () (quic-close! client) (discard-packet-keys! keys)))))76
(test "ql2-close: client application close uses the reserved final seal"77
(ql2b-with-pair78
(lambda (client server)79
;; HANDSHAKE_DONE's ACK consumed one application seal. Two PINGs80
;; consume two more; the fourth and final seal must accept type 0x1d.81
(do ((i 0 (+ i 1))) ((= i 2)) (quic-send-frame! client (make-quic-frame 1 '())))82
(let ((pings (quic-take-datagrams! client 0)))83
(assert-equal 2 (length pings))84
(for-each (lambda (packet) (quic-receive! server packet 0)) pings))85
(quic-close! client code: 900 reason: "done λ")86
(assert-true (quic-application-error? (quic-failure client)))87
(ql2b-check-peer-close! client server 29 900 (string->utf8 "done λ")))88
client-limit: 4))89
(test "ql2-close: confirmed server carries the full application varint code"90
(ql2b-with-pair91
(lambda (client server)92
(quic-close! server code: quic-varint-maximum reason: "server done")93
(ql2b-check-peer-close! server client 29 quic-varint-maximum (string->utf8 "server done")))))94
(test "ql2-close: explicit transport close preserves its separate code and frame type"95
(ql2b-with-pair96
(lambda (client server)97
(quic-transport-close! client code: 10 frame-type: 16 reason: "local diagnostic")98
(assert-true (quic-error? (quic-failure client)))99
(assert-false (quic-application-error? (quic-failure client)))100
(assert-equal 16 (quic-error-frame-type (quic-failure client)))101
(ql2b-check-peer-close! client server 28 10 #u8())102
(assert-equal 16 (frame-ref (quic-failure server) 'frame-type)))))103
(test "ql2-close: invalid local application metadata leaves the connection usable"104
(let ((client (ql2b-client)))105
(dynamic-wind106
(lambda () #t)107
(lambda ()108
(assert-error (quic-close! client code: -1))109
(assert-false (quic-closed? client))110
(assert-error (quic-close! client reason: (make-string 600 #\λ)))111
(assert-false (quic-closed? client))112
(assert-error (quic-close! client code: (+ quic-varint-maximum 1)))113
(assert-false (quic-closed? client))114
(assert-false (quic-failure client))115
(assert-equal '() (quic-take-datagrams! client 0)))116
(lambda () (quic-close! client)))))117
(test "ql2-close: repeated closes retain the first result and run hooks once"118
(let ((client (ql2b-client)) (calls 0))119
(quic-on-close! client (lambda () (set! calls (+ calls 1))))120
(quic-close! client code: 900 reason: "first")121
(let ((failure (quic-failure client)))122
(quic-close! client code: 901 reason: "second")123
(assert-eq failure (quic-failure client))124
(assert-equal 1 calls)125
(assert-equal 1 (length (quic-take-datagrams! client 0)))126
(assert-equal '() (quic-take-datagrams! client 0))))))