AtlatestRepositorysigil-quic
sigil-quic / tree / testtest-connection-space.sgl
1
(import (sigil test)2
(sigil process)3
(sigil quic bytes)4
(sigil quic wire)5
(sigil quic frame)6
(sigil quic packet)7
(sigil quic error)8
(sigil quic connection space))10
(define (check-space-wire expected actual)11
(assert-equal12
(if (equal? (getenv "QUIC_SPACE_RED") "1") (flip-byte expected 0) expected) actual))14
(define (capture-space-error thunk)15
(guard (exception (else exception)) (thunk) #f))17
(define (assert-transport-error thunk code type)18
(let ((exception (capture-space-error thunk)))19
(assert-true (error-object? exception))20
(assert-true (quic-error? exception))21
(assert-equal code (quic-error-code exception))22
(assert-equal type (quic-error-frame-type exception))))24
(define (fill-space-sends! space count)25
(do ((number 0 (+ number 1))) ((= number count))26
(packet-space-record-sent! space number 100 number #t)))28
(test-group "QUIC packet spaces and transport errors"29
(test "space-control: reordered consecutive packet ACK"30
(let ((space (make-packet-space 'handshake)))31
(for-each (lambda (number) (packet-space-receive! space number #t)) '(7 5 6))32
(check-space-wire #u8(2 7 0 0 2) (encode-frame (packet-space-take-ack! space)))))33
(test "space-control: gaps encode independent ACK ranges"34
(let ((space (make-packet-space 'application)))35
(for-each (lambda (number) (packet-space-receive! space number #t)) '(10 9 6 4))36
(check-space-wire #u8(2 10 0 2 1 1 0 0 0) (encode-frame (packet-space-take-ack! space)))))37
(test "space-control: history retirement keeps bounded ACKs"38
(let ((space (make-packet-space 'initial range-limit: 2)))39
(for-each (lambda (number) (packet-space-receive! space number #t)) '(10 20 30))40
(assert-equal 20 (packet-space-floor space))41
(check-space-wire #u8(2 30 0 1 0 8 0) (encode-frame (packet-space-take-ack! space)))))42
(test "space-control: unsent ACK preserves transport error metadata"43
(let* ((space (make-packet-space 'initial))44
(exception (capture-space-error (lambda () (packet-space-ack! space '((0 . 0)))))))45
(assert-true (quic-error? exception))46
(check-space-wire #u8(10 2)47
(bytes-append (encode-varint (quic-error-code exception)) (encode-varint (quic-error-frame-type exception))))))48
(test "fresh spaces keep independent send and receive number spaces"49
(let ((initial (make-packet-space 'initial)) (handshake (make-packet-space 'handshake)))50
(assert-equal 'initial (packet-space-level initial))51
(assert-equal 0 (packet-space-next initial))52
(assert-equal -1 (packet-space-largest initial))53
(assert-equal 0 (packet-space-floor initial))54
(assert-equal 'new (packet-space-received-status initial 0))55
(assert-false (packet-space-ack-needed? initial))56
(assert-false (packet-space-take-ack! initial))57
(packet-space-receive! initial 0 #t)58
(assert-equal 'new (packet-space-received-status handshake 0))59
(assert-equal -1 (packet-space-largest handshake))))60
(test "constructor limits reject invalid levels zero and excessive budgets"61
(assert-error (make-packet-space 'early))62
(assert-error (make-packet-space 'initial range-limit: 0))63
(assert-error (make-packet-space 'initial range-limit: 257))64
(assert-error (make-packet-space 'initial pending-limit: 0))65
(assert-error (make-packet-space 'initial pending-limit: 257))66
(assert-error (make-packet-space 'initial byte-limit: 0))67
(assert-error (make-packet-space 'initial byte-limit: 1048577)))68
(test "duplicate eliciting packets request ACK without adding ranges"69
(let ((space (make-packet-space 'initial)))70
(assert-equal 'new (packet-space-receive! space 3 #t))71
(assert-equal 'duplicate (packet-space-receive! space 3 #t))72
(assert-equal '((3 . 3)) (frame-ref (packet-space-take-ack! space) 'ranges))73
(assert-false (packet-space-ack-needed? space))74
(assert-equal 'duplicate (packet-space-receive! space 3 #t))75
(assert-true (packet-space-ack-needed? space))))76
(test "ACK-only packets enter history but never create ACK loops"77
(let ((space (make-packet-space 'initial)))78
(packet-space-receive! space 1 #f)79
(assert-false (packet-space-take-ack! space))80
(packet-space-receive! space 2 #t)81
(assert-equal '((1 . 2)) (frame-ref (packet-space-take-ack! space) 'ranges))82
(packet-space-receive! space 1 #f)83
(assert-false (packet-space-take-ack! space))))84
(test "bridging a hole merges both surrounding intervals"85
(let ((space (make-packet-space 'handshake)))86
(for-each (lambda (number) (packet-space-receive! space number #t)) '(5 3 1 4 2 0))87
(assert-equal '((0 . 5)) (frame-ref (packet-space-take-ack! space) 'ranges))88
(assert-equal 5 (packet-space-largest space))))89
(test "retired duplicates never become new after range merging frees capacity"90
(let ((space (make-packet-space 'initial range-limit: 2)))91
(for-each (lambda (number) (packet-space-receive! space number #t)) '(1 3 5))92
(assert-equal 3 (packet-space-floor space))93
(assert-equal 'old (packet-space-receive! space 1 #t))94
(assert-equal 'old (packet-space-receive! space 2 #t))95
(packet-space-receive! space 4 #t)96
(assert-equal '((3 . 5)) (frame-ref (packet-space-take-ack! space) 'ranges))97
(assert-equal 3 (packet-space-floor space))98
(assert-equal 'old (packet-space-received-status space 1))99
(assert-equal 'duplicate (packet-space-received-status space 3))))100
(test "large packet numbers and gaps retain exact 62-bit ACK values"101
(let ((space (make-packet-space 'application range-limit: 2)))102
(packet-space-receive! space 0 #t)103
(packet-space-receive! space (- quic-varint-maximum 1) #t)104
(packet-space-receive! space quic-varint-maximum #t)105
(let* ((wire (encode-frame (packet-space-take-ack! space)))106
(frame (car (decode-frames wire 'application 'server))))107
(assert-equal (list (cons (- quic-varint-maximum 1) quic-varint-maximum) '(0 . 0))108
(frame-ref frame 'ranges)))))109
(test "invalid receive numbers and flags do not mutate history"110
(let ((space (make-packet-space 'initial)))111
(for-each (lambda (number) (assert-error (packet-space-receive! space number #t)))112
(list -1 0.0 #f (+ quic-varint-maximum 1)))113
(assert-error (packet-space-receive! space 0 1))114
(assert-equal -1 (packet-space-largest space))115
(assert-false (packet-space-take-ack! space))))116
(test "send commitment is contiguous and rejects PN reuse gaps and inexactness"117
(let ((space (make-packet-space 'initial)))118
(assert-error (packet-space-record-sent! space 1 100 0 #t))119
(assert-error (packet-space-record-sent! space 0.0 100 0 #t))120
(assert-equal 0 (packet-space-next space))121
(packet-space-record-sent! space 0 100 0 #t)122
(assert-equal 1 (packet-space-next space))123
(assert-error (packet-space-record-sent! space 0 100 1 #t))124
(assert-equal 100 (packet-space-pending-bytes space))))125
(test "pending packet and byte budgets can be checked before encryption"126
(let ((space (make-packet-space 'handshake pending-limit: 1 byte-limit: 150)))127
(assert-true (packet-space-can-send? space 100 #t))128
(packet-space-record-sent! space 0 100 0 #t)129
(assert-false (packet-space-can-send? space 1 #t))130
(assert-true (packet-space-can-send? space 50 #f))131
(assert-error (packet-space-record-sent! space 1 50 1 #t))132
(assert-equal 1 (packet-space-next space))133
(packet-space-record-sent! space 1 50 1 #f)134
(assert-equal 2 (packet-space-next space))135
(assert-equal 100 (packet-space-pending-bytes space)))136
(let ((space (make-packet-space 'application byte-limit: 150)))137
(packet-space-record-sent! space 0 100 0 #t)138
(assert-false (packet-space-can-send? space 51 #t))139
(assert-true (packet-space-can-send? space 50 #t))))140
(test "ACK releases only matching pending metadata and byte credit"141
(let ((space (make-packet-space 'application)))142
(fill-space-sends! space 3)143
(let ((acked (packet-space-ack! space '((0 . 1)))))144
(assert-equal '(1 0) (map sent-packet-info-number acked))145
(assert-equal '(1 0) (map sent-packet-info-time acked))146
(assert-equal '(100 100) (map sent-packet-info-size acked)))147
(assert-equal '(2) (map sent-packet-info-number (packet-space-pending space)))148
(assert-equal 100 (packet-space-pending-bytes space))149
(assert-equal '() (packet-space-ack! space '((0 . 1))))150
(assert-equal 1 (length (packet-space-ack! space '((2 . 2)))))151
(assert-equal 0 (packet-space-pending-bytes space))))152
(test "unsent ACK is a protocol violation and leaves in-flight state unchanged"153
(let ((space (make-packet-space 'handshake)))154
(fill-space-sends! space 2)155
(assert-transport-error (lambda () (packet-space-ack! space '((0 . 2)))) 10 2)156
(assert-equal 200 (packet-space-pending-bytes space))157
(assert-equal 2 (length (packet-space-pending space)))))158
(test "malformed ACK ranges fail before removing pending packets"159
(let ((space (make-packet-space 'application)))160
(fill-space-sends! space 3)161
(for-each (lambda (ranges) (assert-error (packet-space-ack! space ranges)))162
'(() ((2 . 1)) ((0 . 1) (2 . 2)) ((1 . 2) (0 . 0))))163
(assert-equal 300 (packet-space-pending-bytes space))))164
(test "ACK-only sends are valid ACK targets without retained loss metadata"165
(let ((space (make-packet-space 'initial)))166
(packet-space-record-sent! space 0 30 0 #f)167
(assert-equal '() (packet-space-pending space))168
(assert-equal '() (packet-space-ack! space '((0 . 0))))))169
(test "loss retirement frees resources without reusing packet numbers"170
(let ((space (make-packet-space 'initial)))171
(fill-space-sends! space 2)172
(assert-equal 0 (sent-packet-info-number (packet-space-retire! space 0)))173
(assert-false (packet-space-retire! space 0))174
(assert-equal 100 (packet-space-pending-bytes space))175
(assert-equal 2 (packet-space-next space))176
(assert-equal '() (packet-space-ack! space '((0 . 0))))177
(packet-space-record-sent! space 2 100 2 #t)178
(assert-equal '(2 1) (map sent-packet-info-number (packet-space-pending space)))))179
(test "discard clears ACK and ledger state and prevents number-space reuse"180
(let ((space (make-packet-space 'initial)))181
(fill-space-sends! space 2)182
(packet-space-receive! space 0 #t)183
(packet-space-discard! space)184
(packet-space-discard! space)185
(assert-true (packet-space-discarded? space))186
(assert-false (packet-space-ack-needed? space))187
(assert-equal '() (packet-space-pending space))188
(assert-equal 0 (packet-space-pending-bytes space))189
(assert-error (packet-space-next space))190
(assert-error (packet-space-receive! space 1 #t))191
(assert-error (packet-space-record-sent! space 0 100 0 #t))192
(assert-error (packet-space-ack! space '((0 . 0))))))193
(test "invalid send size time and flags leave next and credit unchanged"194
(let ((space (make-packet-space 'application)))195
(assert-error (packet-space-record-sent! space 0 0 0 #t))196
(assert-error (packet-space-record-sent! space 0 65528 0 #t))197
(assert-error (packet-space-record-sent! space 0 100 -1 #t))198
(assert-error (packet-space-record-sent! space 0 100 0.0 #t))199
(assert-error (packet-space-record-sent! space 0 100 0 1))200
(assert-equal 0 (packet-space-next space))201
(assert-equal 0 (packet-space-pending-bytes space))))202
(test "transport metadata stays a standard error object with its reason"203
(let ((exception (capture-space-error (lambda () (raise-quic-error 13 #f "buffer exceeded")))))204
(assert-true (error-object? exception))205
(assert-true (quic-error? exception))206
(assert-equal 13 (quic-error-code exception))207
(assert-false (quic-error-frame-type exception))208
(assert-equal "buffer exceeded" (quic-error-reason exception)))209
(assert-false (quic-error? (capture-space-error (lambda () (error "ordinary error")))))210
(assert-false (quic-error? #f)))211
(test "transport metadata validates code frame and reason inputs"212
(assert-error (raise-quic-error -1 #f "bad"))213
(assert-error (raise-quic-error 10 -1 "bad"))214
(assert-error (raise-quic-error 10 #f #f))215
(assert-error (raise-quic-error 10 #f (make-string 1025 #\x)))216
(assert-error (quic-error-code #f)))217
(test "authenticated frame placement retains PROTOCOL_VIOLATION"218
(assert-transport-error (lambda () (decode-frames #u8(30) 'application 'client)) 10 30)219
(assert-transport-error (lambda () (decode-frames #u8(16 0) 'initial 'server)) 10 16))220
(test "authenticated reserved bits retain PROTOCOL_VIOLATION without a frame type"221
(let* ((material (traffic-keys (make-bytevector 32 7)))222
(header (encode-packet-header 'initial #u8() #u8() #u8() 0 4 17))223
(offset (- (bytevector-length header) 4)))224
(bytevector-u8-set! header 0 (+ 4 (bytevector-u8-ref header 0)))225
(let ((packet (bytes-append header226
(protect-payload (vector-ref material 1) (vector-ref material 2) 0 header #u8(1)))))227
(protect-header! packet offset (vector-ref material 3))228
(assert-transport-error (lambda () (open-quic-packet packet 0 0 material -1)) 10 #f)))))