AtlatestRepositorysigil-quic
sigil-quic / tree / testtest-packet.sgl
1
(import (sigil test)2
(sigil math)3
(sigil process)4
(sigil quic bytes)5
(sigil quic wire)6
(sigil quic packet)7
(sigil quic vectors))9
(define (packet-test-dcid) (hex->bytes "8394c8f03e515708"))10
(define (packet-test-scid) (hex->bytes "f067a5502a4262b5"))11
(define (packet-test-material) (traffic-keys (make-bytevector 32 7)))12
(define (packet-client-plaintext)13
(let ((payload (make-bytevector 1162 0)))14
(bytevector-copy! payload 0 (fixture 'quic-client-payload))15
payload))17
(define (check-packet-wire expected actual)18
(assert-equal19
(if (equal? (getenv "QUIC_PACKET_RED") "1") (flip-byte expected 0) expected) actual))21
(define (packet-authenticates? bytes material)22
(guard (exn (else #f))23
(and (open-quic-packet bytes 0 1 material -1) #t)))25
(define (packet-with-reserved-bits material kind bits)26
(let* ((header (encode-packet-header kind #u8(1) #u8() #u8() 7 4 17))27
(pn-offset (- (bytevector-length header) 4)))28
(bytevector-u8-set! header 0 (+ (bytevector-u8-ref header 0) bits))29
(let ((packet (bytes-append header30
(protect-payload (vector-ref material 1) (vector-ref material 2) 7 header #u8(1)))))31
(protect-header! packet pn-offset (vector-ref material 3)))))33
(test-group "QUIC protected packets and headers"34
(test "packet-control: RFC9001 client unprotected header"35
(check-packet-wire (fixture 'quic-client-header)36
(encode-packet-header 'initial (packet-test-dcid) #u8() #u8() 2 4 1178)))37
(test "packet-control: RFC9001 server unprotected header"38
(check-packet-wire (fixture 'quic-server-header)39
(encode-packet-header 'initial #u8() (packet-test-scid) #u8() 1 2 115)))40
(test "packet-control: RFC9001 complete client packet"41
(check-packet-wire (fixture 'quic-client-packet)42
(seal-quic-packet (initial-keys (packet-test-dcid) 'client)43
'initial (packet-test-dcid) #u8() 2 (packet-client-plaintext) pn-length: 4)))44
(test "packet-control: RFC9001 complete server packet"45
(check-packet-wire (fixture 'quic-server-packet)46
(seal-quic-packet (initial-keys (packet-test-dcid) 'server)47
'initial #u8() (packet-test-scid) 1 (fixture 'quic-server-payload) pn-length: 2)))48
(test "packet-control: RFC9001 client packet opening"49
(let ((opened (open-quic-packet (fixture 'quic-client-packet) 0 050
(initial-keys (packet-test-dcid) 'client) -1)))51
(assert-equal 2 (opened-quic-packet-number opened))52
(check-packet-wire (packet-client-plaintext) (opened-quic-packet-payload opened))))53
(test "packet-control: RFC9001 server packet opening"54
(let ((opened (open-quic-packet (fixture 'quic-server-packet) 0 055
(initial-keys (packet-test-dcid) 'server) -1)))56
(assert-equal 1 (opened-quic-packet-number opened))57
(check-packet-wire (fixture 'quic-server-payload) (opened-quic-packet-payload opened))))58
(test "packet-control: RFC9000 packet number truncation"59
(check-packet-wire #u8(92 2) (encode-packet-number #xac5c02 2)))60
(test "packet-control: RFC9000 packet number reconstruction"61
(check-packet-wire #u8(168 47 155 50)62
(encode-packet-number (reconstruct-packet-number #x9b32 2 #xa82f30ea) 4)))63
(test "published long-header boundaries and IDs parse independently"64
(let ((client (decode-packet-header (fixture 'quic-client-packet) 0 0))65
(server (decode-packet-header (fixture 'quic-server-packet) 0 0)))66
(assert-equal 'initial (quic-header-kind client))67
(assert-equal 1 (quic-header-version client))68
(assert-equal 0 (quic-header-start client))69
(assert-equal 1200 (quic-header-end client))70
(assert-equal 18 (quic-header-pn-offset client))71
(assert-equal (packet-test-dcid) (quic-header-destination-id client))72
(assert-equal #u8() (quic-header-source-id client))73
(assert-equal #u8() (quic-header-token client))74
(assert-equal 135 (quic-header-end server))75
(assert-equal #u8() (quic-header-destination-id server))76
(assert-equal (packet-test-scid) (quic-header-source-id server))))77
(test "all protected kinds and PN widths seal with confined HP samples"78
(let ((material (packet-test-material)))79
(for-each80
(lambda (kind)81
(do ((width 1 (+ width 1))) ((= width 5))82
(let* ((packet (seal-quic-packet material kind #u8(1) #u8() 7 #u8(1) pn-length: width))83
(opened (open-quic-packet packet 0 1 material -1))84
(expected (make-bytevector (max 1 (- 4 width)) 0)))85
(bytevector-u8-set! expected 0 1)86
(assert-equal kind (quic-header-kind (opened-quic-packet-header opened)))87
(assert-equal 7 (opened-quic-packet-number opened))88
(assert-equal expected (opened-quic-packet-payload opened))))) '(initial handshake application))))89
(test "application key-phase and spin are authenticated independently"90
(let* ((material (packet-test-material))91
(packet (seal-quic-packet material 'application #u8(1) #u8() 8 #u8(1) key-phase: 1 spin: 1))92
(opened (open-quic-packet packet 0 1 material -1)))93
(assert-equal 1 (opened-quic-packet-key-phase opened))94
(assert-equal 1 (opened-quic-packet-spin opened))95
(assert-false (open-quic-packet (flip-byte packet 0) 0 1 material -1))))96
(test "coalesced long and short packets preserve exact packet boundaries"97
(let* ((material (packet-test-material))98
(initial (seal-quic-packet material 'initial #u8(1) #u8(2) 1 #u8(1)))99
(handshake (seal-quic-packet material 'handshake #u8(1) #u8(2) 2 #u8(1)))100
(application (seal-quic-packet material 'application #u8(1) #u8() 3 #u8(1)))101
(datagram (bytes-append initial handshake application))102
(one (open-quic-packet datagram 0 1 material -1))103
(two (open-quic-packet datagram (quic-header-end (opened-quic-packet-header one)) 1 material -1))104
(three (open-quic-packet datagram (quic-header-end (opened-quic-packet-header two)) 1 material -1)))105
(assert-equal 1 (opened-quic-packet-number one))106
(assert-equal 2 (opened-quic-packet-number two))107
(assert-equal 3 (opened-quic-packet-number three))108
(assert-equal (bytevector-length initial) (quic-header-end (opened-quic-packet-header one)))109
(assert-equal (bytevector-length initial) (quic-header-start (opened-quic-packet-header two)))110
(assert-equal (- (bytevector-length handshake) 21) (quic-header-pn-offset (opened-quic-packet-header two)))111
(assert-equal (bytevector-length datagram) (quic-header-end (opened-quic-packet-header three)))))112
(test "short header consumes the datagram remainder and cannot precede another packet"113
(let* ((material (packet-test-material))114
(packet (seal-quic-packet material 'application #u8(1) #u8() 1 #u8(1))))115
(assert-false (open-quic-packet (bytes-append packet packet) 0 1 material -1))))116
(test "HP sample cannot borrow bytes from a coalesced following packet"117
(let* ((material (packet-test-material))118
(following (seal-quic-packet material 'handshake #u8() #u8() 1 #u8(1)))119
(short-sample (bytes-append #u8(192 0 0 0 1 0 0 0 19) (make-bytevector 19 0) following)))120
(assert-error (decode-packet-header short-sample 0 0))))121
(test "declared long length beyond the datagram is rejected"122
(assert-error123
(decode-packet-header (bytes-append #u8(192 0 0 0 1 0 0 0 63) (make-bytevector 20 0)) 0 0)))124
(test "all proper prefixes of complete Initial packets fail before authentication"125
(for-each126
(lambda (wire)127
(do ((length 0 (+ length 1))) ((= length (bytevector-length wire)))128
(assert-error (decode-packet-header (bytevector-copy wire 0 length) 0 0))))129
(list (fixture 'quic-client-packet) (fixture 'quic-server-packet))))130
(test "each ciphertext and tag byte mutation fails authentication"131
(let* ((material (initial-keys (packet-test-dcid) 'server))132
(wire (fixture 'quic-server-packet)))133
(do ((i 20 (+ i 1))) ((= i (bytevector-length wire)))134
(assert-false (open-quic-packet (flip-byte wire i) 0 0 material -1)))))135
(test "PN and reserved HP byte corruption fail without early reserved-bit errors"136
(let* ((material (initial-keys (packet-test-dcid) 'client))137
(wire (fixture 'quic-client-packet)))138
(do ((i 18 (+ i 1))) ((= i 22))139
(assert-false (open-quic-packet (flip-byte wire i) 0 0 material -1)))140
(let ((corrupted (bytevector-copy wire)))141
(bytevector-u8-set! corrupted 0 (bitwise-xor (bytevector-u8-ref corrupted 0) 4))142
(assert-false (open-quic-packet corrupted 0 0 material -1)))))143
(test "correctly authenticated nonzero reserved bits require a protocol error"144
(let ((material (packet-test-material)))145
(assert-error (open-quic-packet (packet-with-reserved-bits material 'initial 4) 0 1 material -1))146
(assert-error (open-quic-packet (packet-with-reserved-bits material 'handshake 8) 0 1 material -1))147
(assert-error (open-quic-packet (packet-with-reserved-bits material 'application 8) 0 1 material -1))))148
(test "header parsing rejects zero fixed bits and excessive CID lengths"149
(assert-error (decode-packet-header #u8(0) 0 0))150
(assert-error (decode-packet-header #u8(128 0 0 0 1 0 0) 0 0))151
(assert-error (decode-packet-header #u8(192 0 0 0 1 21) 0 0))152
(assert-error (decode-packet-header #u8(192 0 0 0 1 0 21) 0 0)))153
(test "huge token and packet lengths are bounded before copying"154
(assert-error (decode-packet-header (hex->bytes "c0000000010000ffffffffffffffff") 0 0))155
(assert-error (decode-packet-header (hex->bytes "e0000000010000ffffffffffffffff") 0 0)))156
(test "Version Negotiation ignores fixed bit and parses invariant IDs separately"157
(let ((header (decode-packet-header (hex->bytes "800000000001aa01bb00000001") 0 0)))158
(assert-equal 'version-negotiation (quic-header-kind header))159
(assert-equal '(1) (quic-header-versions header))160
(assert-equal #u8(170) (quic-header-destination-id header))161
(assert-equal #u8(187) (quic-header-source-id header)))162
(assert-error (decode-packet-header (hex->bytes "80000000000000") 0 0))163
(assert-error (decode-packet-header (hex->bytes "80000000000000000001") 0 0))164
(assert-error165
(decode-packet-header (bytes-append (hex->bytes "80000000000000") (make-bytevector (* 257 4) 0)) 0 0)))166
(test "unknown versions retain invariant framing and cannot enter AEAD path"167
(let* ((wire (hex->bytes "800000000201aa01bb")) (header (decode-packet-header wire 0 0)))168
(assert-equal 'unsupported (quic-header-kind header))169
(assert-equal 2 (quic-header-version header))170
(assert-error (open-quic-packet wire 0 0 (packet-test-material) -1))))171
(test "Retry token and tag are separated without claiming integrity validation"172
(let* ((wire (bytes-append (hex->bytes "f00000000101aa01bb2a") (make-bytevector 16 0)))173
(header (decode-packet-header wire 0 0)))174
(assert-equal 'retry (quic-header-kind header))175
(assert-equal #u8(42) (quic-header-token header))176
(assert-equal (make-bytevector 16 0) (quic-header-integrity-tag header))177
(assert-error (open-quic-packet wire 0 0 (packet-test-material) -1)))178
(assert-error179
(decode-packet-header (bytes-append (hex->bytes "f0000000010000") (make-bytevector 16 0)) 0 0)))180
(test "Initial tokens and explicit length-field widths are retained"181
(let* ((material (packet-test-material))182
(packet (seal-quic-packet material 'initial #u8(1) #u8(2) 1 #u8(1) token: #u8(9 8) length-width: 2))183
(opened (open-quic-packet packet 0 0 material -1)))184
(assert-equal #u8(9 8) (quic-header-token (opened-quic-packet-header opened)))185
(assert-equal #u8(1) (opened-quic-packet-payload opened))))186
(test "packet number reconstruction handles window wraps and tie boundaries"187
(for-each188
(lambda (item)189
(assert-equal (caddr item) (reconstruct-packet-number (cadr item) 1 (car item))))190
'((255 0 256) (256 255 255) (127 0 256) (255 128 384) (256 130 130) (-1 0 0))))191
(test "packet number operations preserve values beyond fixnum range"192
(let ((number (- quic-varint-maximum 3)))193
(assert-equal number (reconstruct-packet-number 252 1 (- quic-varint-maximum 10)))194
(assert-equal #u8(255 255 255 252) (encode-packet-number number 4))195
(assert-equal 4294967292 (read-packet-number #u8(0 255 255 255 252 0) 1 4))196
(let* ((material (packet-test-material))197
(packet (seal-quic-packet material 'application #u8(1) #u8() number #u8(1) pn-length: 1))198
(opened (open-quic-packet packet 0 1 material (- quic-varint-maximum 10))))199
(assert-equal number (opened-quic-packet-number opened))))200
(assert-equal quic-varint-maximum (reconstruct-packet-number 255 1 quic-varint-maximum))201
(assert-error (reconstruct-packet-number 0 1 quic-varint-maximum)))202
(test "invalid packet-number numbers widths and bounds are rejected"203
(assert-error (encode-packet-number -1 1))204
(assert-error (encode-packet-number 1.0 1))205
(assert-error (encode-packet-number (+ quic-varint-maximum 1) 4))206
(assert-error (encode-packet-number 0 0))207
(assert-error (read-packet-number #u8(1) 0 2))208
(assert-error (read-packet-number #u8(1) -1 1))209
(assert-error (reconstruct-packet-number 256 1 -1))210
(assert-error (reconstruct-packet-number 0 1 -2))211
(assert-error (packet-nonce (make-bytevector 12 0) 1.0)))212
(test "packet input offsets limits and encode-only fields are validated"213
(let ((material (packet-test-material)))214
(assert-error (decode-packet-header #u8() 0 0))215
(assert-error (decode-packet-header #u8(64) 0 21))216
(assert-error (decode-packet-header #u8(64) 1 0))217
(assert-error (decode-packet-header #u8(64) 0.0 0))218
(assert-error (decode-packet-header (make-bytevector (+ packet-datagram-limit 1) 0) 0 0))219
(assert-error (seal-quic-packet material 'initial #u8() #u8() 0 #u8()))220
(assert-error (seal-quic-packet material 'application #u8() #u8(1) 0 #u8(1)))221
(assert-error (seal-quic-packet material 'handshake #u8() #u8() 0 #u8(1) token: #u8(1)))222
(assert-error (seal-quic-packet material 'initial #u8() #u8() 0 #u8(1) key-phase: 1))223
(assert-error (seal-quic-packet material 'initial (make-bytevector 21 0) #u8() 0 #u8(1)))224
(assert-error (seal-quic-packet material 'initial #u8() #u8() 0 (make-bytevector packet-datagram-limit 0)))))225
(test "failed and successful opens leave the caller datagram unchanged"226
(let* ((material (packet-test-material))227
(packet (seal-quic-packet material 'handshake #u8(1) #u8(2) 1 #u8(1)))228
(before (bytevector-copy packet)))229
(open-quic-packet packet 0 1 material -1)230
(assert-equal before packet)231
(let* ((bad (flip-byte packet (- (bytevector-length packet) 1))) (copy (bytevector-copy bad)))232
(assert-false (open-quic-packet bad 0 1 material -1))233
(assert-equal copy bad))))234
(test "traffic keys own secret copies and discard clears all components"235
(let* ((secret (make-bytevector 32 7)) (material (traffic-keys secret))236
(references (vector->list material)))237
(assert-equal '(32 16 12 16) (map bytevector-length references))238
(discard-packet-keys! material)239
(discard-packet-keys! material)240
(for-each (lambda (bytes) (assert-equal (make-bytevector (bytevector-length bytes) 0) bytes)) references)241
(assert-equal (make-bytevector 32 7) secret)242
(assert-error (seal-quic-packet material 'initial #u8() #u8() 0 #u8(1))))243
(assert-error (traffic-keys #u8(1)))244
(assert-error (discard-packet-keys! #f))))