AtlatestRepositorysigil-http3

sigil-http3 / tree / testtest-qpack.sgl

1(import (sigil test)
2 (sigil quic bytes)
3 (sigil http3 error)
4 (sigil http3 qpack)
5 (sigil http3 qpack huffman))
6
7;; Fixtures here are ASCII, and the byte-oriented QPACK API preserves octets.
8;; Do not depend on a native test runner exposing the UTF-8 convenience API.
9(define (fixture-bytes text)
10 (let ((out (make-bytevector (string-length text) 0)))
11 (do ((index 0 (+ index 1))) ((= index (string-length text)) out)
12 (let ((octet (char->integer (string-ref text index))))
13 (unless (<= 0 octet 127)
14 (error "QPACK fixture must be ASCII"))
15 (bytevector-u8-set! out index octet)))))
17(define (qpack-field name value)
18 (cons (fixture-bytes name) (fixture-bytes value)))
20(define (qpack-rejects? thunk)
21 (guard (exn
22 ((h3-error? exn) (= (h3-error-code exn) #x200))
23 (else (raise exn)))
24 (thunk)
25 #f))
27(test-group "QPACK prefixed integers"
28 (test "RFC 7541 integer 1337 uses independent expected bytes"
29 (assert-equal #u8(31 154 10) (encode-qpack-integer 1337 5))
30 (assert-equal '(1337 . 3) (decode-qpack-integer #u8(31 154 10) 0 5)))
32 (test "an exact prefix maximum needs a zero continuation"
33 (assert-equal #u8(255 0) (encode-qpack-integer 63 6 192))
34 (assert-equal '(63 . 2) (decode-qpack-integer #u8(255 0) 0 6)))
36 (test "truncation and continuation work limits reject"
37 (for-each
38 (lambda (wire)
39 (assert-true
40 (qpack-rejects? (lambda () (decode-qpack-integer wire 0 8)))))
41 (list #u8() #u8(255) #u8(255 128)
42 #u8(255 255 255 255 255 255 255 255 255 255 127)))))
44(test-group "QPACK Huffman strings"
45 (test "RFC 7541 www.example.com vector"
46 (assert-equal (fixture-bytes "www.example.com")
47 (decode-qpack-huffman (hex->bytes "f1e3c2e5f23a6ba0ab90f4ff"))))
49 (test "empty input and exact byte-boundary code"
50 (assert-equal #u8() (decode-qpack-huffman #u8()))
51 (assert-equal (fixture-bytes "&") (decode-qpack-huffman #u8(248))))
53 (test "EOS, long padding and zero padding reject"
54 (for-each
55 (lambda (wire)
56 (assert-true (qpack-rejects? (lambda () (decode-qpack-huffman wire)))))
57 (list #u8(255 255 255 255) #u8(255) #u8(0))))
59 (test "decoded output limit applies before writing"
60 (assert-true
61 (qpack-rejects?
62 (lambda ()
63 (decode-qpack-huffman (hex->bytes "f1e3c2e5f23a6ba0ab90f4ff") 4))))))
65(test-group "Static-only QPACK field sections"
66 (test "indexed response and literal authority match known bytes"
67 (assert-equal #u8(0 0 217)
68 (encode-qpack-fields (list (qpack-field ":status" "200"))))
69 (assert-equal (list (qpack-field ":authority" "www.example.com"))
70 (decode-qpack-fields
71 (bytes-append #u8(0 0 80 140) (hex->bytes "f1e3c2e5f23a6ba0ab90f4ff")))))
73 (test "literal names and values preserve opaque octets and duplicates"
74 (assert-equal (list (cons #u8(120) #u8(255)) (cons #u8(120) #u8(0)))
75 (decode-qpack-fields #u8(0 0 33 120 1 255 33 120 1 0))))
77 (test "literal name may itself be Huffman encoded"
78 (assert-equal (list (qpack-field "www.example.com" ""))
79 (decode-qpack-fields
80 (bytes-append #u8(0 0 47 5) (hex->bytes "f1e3c2e5f23a6ba0ab90f4ff") #u8(0)))))
82 (test "CONNECT fields round trip without a dynamic table"
83 (let ((fields
84 (list (qpack-field ":method" "CONNECT")
85 (qpack-field ":protocol" "webtransport")
86 (qpack-field ":scheme" "https")
87 (qpack-field ":authority" "example.test:4433")
88 (qpack-field ":path" "/echo"))))
89 (assert-equal fields (decode-qpack-fields (encode-qpack-fields fields)))))
91 (test "nonnegative unused Base is permitted with static fields"
92 (assert-equal (list (qpack-field ":status" "200"))
93 (decode-qpack-fields #u8(0 9 217))))
95 (test "dynamic references, inserts, negative Base and invalid static indexes reject"
96 (for-each
97 (lambda (wire)
98 (assert-true (qpack-rejects? (lambda () (decode-qpack-fields wire)))))
99 (list #u8(1 0 217) #u8(0 128 217) #u8(0 0 128)
100 #u8(0 0 64 0) #u8(0 0 16) #u8(0 0 0)
101 #u8(0 0 255 36))))
103 (test "every incomplete prefix of a literal field rejects"
104 (let ((wire #u8(0 0 33 120 3 97 98 99)))
105 (do ((length 3 (+ length 1))) ((= length (bytevector-length wire)))
106 (assert-true
107 (qpack-rejects?
108 (lambda () (decode-qpack-fields (bytevector-copy wire 0 length))))))))
110 (test "field-count, decoded-size, and wire-size budgets reject"
111 (assert-true
112 (qpack-rejects?
113 (lambda () (decode-qpack-fields (bytes-append #u8(0 0) (make-bytevector 129 217))))))
114 ;; Wire payload stays below 64 KiB; decoded names plus 32 bytes per
115 ;; field exceed the field-section budget independently of that cap.
116 (let ((large (make-bytevector 16350 97)))
117 (assert-true
118 (qpack-rejects?
119 (lambda ()
120 (decode-qpack-fields
121 (bytes-append #u8(0 0)
122 (apply bytes-append
123 (make-list 4 (bytes-append #u8(80) (encode-qpack-integer 16350 7) large)))))))))
124 (assert-true
125 (qpack-rejects? (lambda () (decode-qpack-fields (make-bytevector 65537 0)))))))