AtlatestRepositorysigil-quic
sigil-quic / tree / testtest-frame-work.sgl
1
(import (sigil test)2
(sigil quic bytes)3
(sigil quic wire)4
(sigil quic error)5
(sigil quic frame)6
(sigil quic packet)7
(sigil quic connection))9
(define ql2w-original #u8(1 2 3 4 5 6 7 8))10
(define ql2w-client-id #u8(11 12 13 14 15 16 17 18))11
(define ql2w-server-id #u8(21 22 23 24 25 26 27 28))13
(define (ql2w-with-client procedure)14
(let ((client (make-quic-client (list #u8(113 49)) "example.test" '() (lambda (chain name) #f)15
local-id: ql2w-client-id destination-id: ql2w-original handshake-timeout: 60000))16
(write-keys (initial-keys ql2w-original 'server))17
(read-keys (initial-keys ql2w-original 'client)))18
(dynamic-wind19
(lambda () #t)20
(lambda ()21
(quic-start! client 0)22
(quic-take-datagrams! client 0)23
(procedure client write-keys read-keys))24
(lambda ()25
(quic-close! client)26
(discard-packet-keys! write-keys)27
(discard-packet-keys! read-keys)))))29
(define (ql2w-send! client keys number body (keys: (prefix-padding? #f) (now 0)))30
(let* ((header (encode-packet-header 'initial ql2w-client-id ql2w-server-id #u8() number 431
(+ 16 (bytevector-length body)) length-width: 2))32
(padding (make-bytevector (- 1200 16 (bytevector-length header) (bytevector-length body)) 0))33
(payload (if prefix-padding? (bytes-append padding body) (bytes-append body padding))))34
(quic-receive! client35
(seal-quic-packet keys 'initial ql2w-client-id ql2w-server-id number payload length-width: 2) now)))37
(define (ql2w-crypto byte)38
(encode-frame (make-quic-frame 6 (list (cons 'offset 100) (cons 'data (bytevector byte))))))40
(define (ql2w-ack! client keys ranges (keys: (number #f)))41
(let* ((packets (quic-take-datagrams! client 0))42
(opened (and (pair? packets) (open-quic-packet (car packets) 0 8 keys -1)))43
(frames (if opened (decode-frames (opened-quic-packet-payload opened) 'initial 'client) '())))44
(assert-equal 1 (length packets))45
(assert-true (opened-quic-packet? opened))46
(when (and opened number) (assert-equal number (opened-quic-packet-number opened)))47
(assert-true (pair? frames))48
(when (pair? frames)49
(assert-equal 2 (quic-frame-type (car frames)))50
(when (= (quic-frame-type (car frames)) 2)51
(assert-equal ranges (frame-ref (car frames) 'ranges))))))53
;; Each incoming PING causes one actual client ACK packet. These ACK packets54
;; plus ClientHello PN 0 make a 257-range ACK of even PNs 512..0 legitimate.55
(define (ql2w-sent-history! client write-keys read-keys)56
(do ((number 0 (+ number 1))) ((= number 512))57
(ql2w-send! client write-keys number #u8(1))58
(assert-false (quic-closed? client))59
(ql2w-ack! client read-keys (list (cons 0 number)) number: (+ number 1))))61
(define (ql2w-ack-wire type ranges largest)62
(bytes-append (bytevector type) (encode-varint largest) #u8(0)63
(encode-varint (- ranges 1)) #u8(0)64
(make-bytevector (* 2 (- ranges 1)) 0)65
(if (= type 3) #u8(0 0 0) #u8())))67
(define (ql2w-ranges count)68
(let loop ((index 0) (out '()))69
(if (= index count)70
(reverse out)71
(let ((number (* 2 (- count index 1))))72
(loop (+ index 1) (cons (cons number number) out))))))74
(test-group "Local frame decoding work budgets"75
(test "ql2-work: excessive frame packet is ignored atomically and not acknowledged"76
(ql2w-with-client77
(lambda (client write-keys read-keys)78
;; CRYPTO plus 255 PINGs plus a PADDING run exceeds the local cap.79
(ql2w-send! client write-keys 0 (bytes-append (ql2w-crypto 1) (make-bytevector 255 1)))80
(assert-false (quic-closed? client))81
(assert-false (quic-failure client))82
(assert-equal '() (quic-take-datagrams! client 0))83
;; If any prefix was applied, this different byte would conflict.84
(ql2w-send! client write-keys 1 (ql2w-crypto 2))85
(assert-false (quic-closed? client))86
(ql2w-ack! client read-keys (list (cons 1 1))))))87
(test "ql2-work: valid excessive ACK and ACK_ECN ranges leave the peer live and unacknowledged"88
(ql2w-with-client89
(lambda (client write-keys read-keys)90
(ql2w-sent-history! client write-keys read-keys)91
(for-each92
(lambda (entry)93
(ql2w-send! client write-keys (car entry)94
(bytes-append #u8(1) (ql2w-ack-wire (cdr entry) 257 512)))95
(assert-false (quic-closed? client))96
(assert-false (quic-failure client))97
(assert-equal '() (quic-take-datagrams! client 0)))98
'((512 . 2) (513 . 3)))99
(ql2w-send! client write-keys 514 #u8(1))100
(assert-false (quic-closed? client))101
(ql2w-ack! client read-keys (list (cons 514 514) (cons 0 511))))))102
(test "ql2-work: ignored overflow packets do not extend the idle lifetime"103
(ql2w-with-client104
(lambda (client write-keys read-keys)105
(for-each106
(lambda (entry)107
(ql2w-send! client write-keys (car entry) (make-bytevector 256 1) now: (cdr entry)))108
'((0 . 10000) (1 . 20000) (2 . 29999)))109
(quic-tick! client 30000)110
(assert-true (quic-idle-expired? client))111
(assert-equal '() (quic-take-datagrams! client 30000)))))112
(test "ql2-work: the final frame at the exact decoding boundary is applied"113
(ql2w-with-client114
(lambda (client write-keys read-keys)115
;; One PADDING run, 254 PINGs and a final close are exactly 256 frames.116
(ql2w-send! client write-keys 0117
(bytes-append (make-bytevector 254 1)118
(encode-frame (make-quic-frame 28119
(list (cons 'error 0) (cons 'frame-type 0) (cons 'reason #u8())))))120
prefix-padding?: #t)121
(assert-true (quic-closed? client))122
(assert-true (quic-frame? (quic-failure client)))123
(when (quic-frame? (quic-failure client))124
(assert-equal 28 (quic-frame-type (quic-failure client)))))))125
(test "ql2-work: exact ACK and ECN range boundaries still process a following PING"126
(ql2w-with-client127
(lambda (client write-keys read-keys)128
(ql2w-sent-history! client write-keys read-keys)129
(for-each130
(lambda (entry)131
(ql2w-send! client write-keys (car entry)132
(bytes-append (ql2w-ack-wire (cdr entry) 256 510) #u8(1)))133
(assert-false (quic-closed? client))134
(assert-false (quic-failure client))135
(ql2w-ack! client read-keys (list (cons 0 (car entry)))))136
'((512 . 2) (513 . 3))))))137
(test "ql2-work: malformed ACK arithmetic below the cap still closes with FRAME_ENCODING_ERROR"138
(ql2w-with-client139
(lambda (client write-keys read-keys)140
(ql2w-send! client write-keys 0 #u8(2 0 0 1 0 0 0))141
(assert-true (quic-closed? client))142
(let ((failure (quic-failure client)))143
(assert-equal 7 (and (quic-error? failure) (quic-error-code failure)))))))144
(test "ql2-work: forbidden frame placement below the cap still closes with PROTOCOL_VIOLATION"145
(ql2w-with-client146
(lambda (client write-keys read-keys)147
(ql2w-send! client write-keys 0 #u8(8 0 42))148
(assert-true (quic-closed? client))149
(let ((failure (quic-failure client)))150
(assert-equal 10 (and (quic-error? failure) (quic-error-code failure)))151
(assert-equal 8 (and (quic-error? failure) (quic-error-frame-type failure)))))))152
(test "ql2-work: local encoders retain strict frame and ACK range limits"153
(let loop ((left 257) (frames '()))154
(if (= left 0)155
(begin156
(assert-error (encode-frames frames 'initial 'client))157
(assert-equal 256 (bytevector-length (encode-frames (cdr frames) 'initial 'client))))158
(loop (- left 1) (cons (make-quic-frame 1 '()) frames))))159
(assert-error (encode-frame (make-quic-frame 2 (list (cons 'ranges (ql2w-ranges 257)) (cons 'delay 0)))))160
(assert-true (bytevector? (encode-frame161
(make-quic-frame 2 (list (cons 'ranges (ql2w-ranges 256)) (cons 'delay 0))))))))