1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
package self
import (
"bytes"
"fmt"
"math/rand"
"github.com/marten-seemann/qpack"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Self Tests", func() {
getEncoder := func() (*qpack.Encoder, *bytes.Buffer) {
output := &bytes.Buffer{}
return qpack.NewEncoder(output), output
}
randomString := func(l int) string {
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
s := make([]byte, l)
for i := range s {
s[i] = charset[rand.Intn(len(charset))]
}
return string(s)
}
It("encodes and decodes a single header field", func() {
hf := qpack.HeaderField{
Name: randomString(15),
Value: randomString(15),
}
encoder, output := getEncoder()
Expect(encoder.WriteField(hf)).To(Succeed())
headerFields, err := qpack.NewDecoder(nil).DecodeFull(output.Bytes())
Expect(err).ToNot(HaveOccurred())
Expect(headerFields).To(Equal([]qpack.HeaderField{hf}))
})
It("encodes and decodes multiple header fields", func() {
hfs := []qpack.HeaderField{
{Name: "foo", Value: "bar"},
{Name: "lorem", Value: "ipsum"},
{Name: randomString(15), Value: randomString(20)},
}
encoder, output := getEncoder()
for _, hf := range hfs {
Expect(encoder.WriteField(hf)).To(Succeed())
}
headerFields, err := qpack.NewDecoder(nil).DecodeFull(output.Bytes())
Expect(err).ToNot(HaveOccurred())
Expect(headerFields).To(Equal(hfs))
})
It("encodes and decodes multiple requests", func() {
hfs1 := []qpack.HeaderField{{Name: "foo", Value: "bar"}}
hfs2 := []qpack.HeaderField{
{Name: "lorem", Value: "ipsum"},
{Name: randomString(15), Value: randomString(20)},
}
encoder, output := getEncoder()
for _, hf := range hfs1 {
Expect(encoder.WriteField(hf)).To(Succeed())
}
req1 := append([]byte{}, output.Bytes()...)
output.Reset()
for _, hf := range hfs2 {
Expect(encoder.WriteField(hf)).To(Succeed())
}
req2 := append([]byte{}, output.Bytes()...)
var headerFields []qpack.HeaderField
decoder := qpack.NewDecoder(func(hf qpack.HeaderField) { headerFields = append(headerFields, hf) })
_, err := decoder.Write(req1)
Expect(err).ToNot(HaveOccurred())
Expect(headerFields).To(Equal(hfs1))
headerFields = nil
_, err = decoder.Write(req2)
Expect(err).ToNot(HaveOccurred())
Expect(headerFields).To(Equal(hfs2))
})
// replace one character by a random character at a random position
replaceRandomCharacter := func(s string) string {
pos := rand.Intn(len(s))
new := s[:pos]
for {
if c := randomString(1); c != string(s[pos]) {
new += c
break
}
}
new += s[pos+1:]
return new
}
check := func(encoded []byte, hf qpack.HeaderField) {
headerFields, err := qpack.NewDecoder(nil).DecodeFull(encoded)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
ExpectWithOffset(1, headerFields).To(HaveLen(1))
ExpectWithOffset(1, headerFields[0]).To(Equal(hf))
}
// use an entry with a value, for example "set-cookie"
It("uses the static table for field names, for fields without values", func() {
var hf qpack.HeaderField
for {
if entry := staticTable[rand.Intn(len(staticTable))]; len(entry.Value) == 0 {
hf = qpack.HeaderField{Name: entry.Name}
break
}
}
encoder, output := getEncoder()
Expect(encoder.WriteField(hf)).To(Succeed())
encodedLen := output.Len()
check(output.Bytes(), hf)
encoder, output = getEncoder()
oldName := hf.Name
hf.Name = replaceRandomCharacter(hf.Name)
Expect(encoder.WriteField(hf)).To(Succeed())
fmt.Fprintf(GinkgoWriter, "Encoding field name:\n\t%s: %d bytes\n\t%s: %d bytes\n", oldName, encodedLen, hf.Name, output.Len())
Expect(output.Len()).To(BeNumerically(">", encodedLen))
})
// use an entry with a value, for example "set-cookie",
// but now use a custom value
It("uses the static table for field names, for fields without values", func() {
var hf qpack.HeaderField
for {
if entry := staticTable[rand.Intn(len(staticTable))]; len(entry.Value) == 0 {
hf = qpack.HeaderField{
Name: entry.Name,
Value: randomString(5),
}
break
}
}
encoder, output := getEncoder()
Expect(encoder.WriteField(hf)).To(Succeed())
encodedLen := output.Len()
check(output.Bytes(), hf)
encoder, output = getEncoder()
oldName := hf.Name
hf.Name = replaceRandomCharacter(hf.Name)
Expect(encoder.WriteField(hf)).To(Succeed())
fmt.Fprintf(GinkgoWriter, "Encoding field name:\n\t%s: %d bytes\n\t%s: %d bytes\n", oldName, encodedLen, hf.Name, output.Len())
Expect(output.Len()).To(BeNumerically(">", encodedLen))
})
// use an entry with a value, for example
// cache-control -> Value: "max-age=0"
// but encode a different value
// cache-control -> xyz
It("uses the static table for field names, for fields with values", func() {
var hf qpack.HeaderField
for {
// Only use values with at least 2 characters.
// This makes sure that Huffman enocding doesn't compress them as much as encoding it using the static table would.
if entry := staticTable[rand.Intn(len(staticTable))]; len(entry.Value) > 1 {
hf = qpack.HeaderField{
Name: entry.Name,
Value: randomString(20),
}
break
}
}
encoder, output := getEncoder()
Expect(encoder.WriteField(hf)).To(Succeed())
encodedLen := output.Len()
check(output.Bytes(), hf)
encoder, output = getEncoder()
oldName := hf.Name
hf.Name = replaceRandomCharacter(hf.Name)
Expect(encoder.WriteField(hf)).To(Succeed())
fmt.Fprintf(GinkgoWriter, "Encoding field name:\n\t%s: %d bytes\n\t%s: %d bytes\n", oldName, encodedLen, hf.Name, output.Len())
Expect(output.Len()).To(BeNumerically(">", encodedLen))
})
It("uses the static table for field values", func() {
var hf qpack.HeaderField
for {
// Only use values with at least 2 characters.
// This makes sure that Huffman enocding doesn't compress them as much as encoding it using the static table would.
if entry := staticTable[rand.Intn(len(staticTable))]; len(entry.Value) > 1 {
hf = qpack.HeaderField{
Name: entry.Name,
Value: entry.Value,
}
break
}
}
encoder, output := getEncoder()
Expect(encoder.WriteField(hf)).To(Succeed())
encodedLen := output.Len()
check(output.Bytes(), hf)
encoder, output = getEncoder()
oldValue := hf.Value
hf.Value = replaceRandomCharacter(hf.Value)
Expect(encoder.WriteField(hf)).To(Succeed())
fmt.Fprintf(GinkgoWriter,
"Encoding field value:\n\t%s: %s -> %d bytes\n\t%s: %s -> %d bytes\n",
hf.Name, oldValue, encodedLen,
hf.Name, hf.Value, output.Len(),
)
Expect(output.Len()).To(BeNumerically(">", encodedLen))
})
})
|