| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package cbrotli_test |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"io/ioutil" |
| 13 |
"math" |
| 14 |
"math/rand" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/google/brotli/go/cbrotli" |
| 19 |
) |
| 20 |
|
| 21 |
func checkCompressedData(compressedData, wantOriginalData []byte) error { |
| 22 |
uncompressed, err := cbrotli.Decode(compressedData) |
| 23 |
if err != nil { |
| 24 |
return fmt.Errorf("brotli decompress failed: %v", err) |
| 25 |
} |
| 26 |
if !bytes.Equal(uncompressed, wantOriginalData) { |
| 27 |
if len(wantOriginalData) != len(uncompressed) { |
| 28 |
return fmt.Errorf(""+ |
| 29 |
"Data doesn't uncompress to the original value.\n"+ |
| 30 |
"Length of original: %v\n"+ |
| 31 |
"Length of uncompressed: %v", |
| 32 |
len(wantOriginalData), len(uncompressed)) |
| 33 |
} |
| 34 |
for i := range wantOriginalData { |
| 35 |
if wantOriginalData[i] != uncompressed[i] { |
| 36 |
return fmt.Errorf(""+ |
| 37 |
"Data doesn't uncompress to the original value.\n"+ |
| 38 |
"Original at %v is %v\n"+ |
| 39 |
"Uncompressed at %v is %v", |
| 40 |
i, wantOriginalData[i], i, uncompressed[i]) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
func TestEncoderNoWrite(t *testing.T) { |
| 48 |
out := bytes.Buffer{} |
| 49 |
e := cbrotli.NewWriter(&out, cbrotli.WriterOptions{Quality: 5}) |
| 50 |
if err := e.Close(); err != nil { |
| 51 |
t.Errorf("Close()=%v, want nil", err) |
| 52 |
} |
| 53 |
|
| 54 |
if _, err := e.Write([]byte("hi")); err == nil { |
| 55 |
t.Errorf("No error after Close() + Write()") |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func TestEncoderEmptyWrite(t *testing.T) { |
| 60 |
out := bytes.Buffer{} |
| 61 |
e := cbrotli.NewWriter(&out, cbrotli.WriterOptions{Quality: 5}) |
| 62 |
n, err := e.Write([]byte("")) |
| 63 |
if n != 0 || err != nil { |
| 64 |
t.Errorf("Write()=%v,%v, want 0, nil", n, err) |
| 65 |
} |
| 66 |
if err := e.Close(); err != nil { |
| 67 |
t.Errorf("Close()=%v, want nil", err) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestWriter(t *testing.T) { |
| 72 |
|
| 73 |
input := []byte("<html><body><H1>Hello world</H1></body></html>") |
| 74 |
out := bytes.Buffer{} |
| 75 |
e := cbrotli.NewWriter(&out, cbrotli.WriterOptions{Quality: 1}) |
| 76 |
in := bytes.NewReader([]byte(input)) |
| 77 |
n, err := io.Copy(e, in) |
| 78 |
if err != nil { |
| 79 |
t.Errorf("Copy Error: %v", err) |
| 80 |
} |
| 81 |
if int(n) != len(input) { |
| 82 |
t.Errorf("Copy() n=%v, want %v", n, len(input)) |
| 83 |
} |
| 84 |
if err := e.Close(); err != nil { |
| 85 |
t.Errorf("Close Error after copied %d bytes: %v", n, err) |
| 86 |
} |
| 87 |
if err := checkCompressedData(out.Bytes(), input); err != nil { |
| 88 |
t.Error(err) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestEncoderStreams(t *testing.T) { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
const lgWin = 16 |
| 97 |
windowSize := int(math.Pow(2, lgWin)) |
| 98 |
input := make([]byte, 8*windowSize) |
| 99 |
rand.Read(input) |
| 100 |
out := bytes.Buffer{} |
| 101 |
e := cbrotli.NewWriter(&out, cbrotli.WriterOptions{Quality: 11, LGWin: lgWin}) |
| 102 |
halfInput := input[:len(input)/2] |
| 103 |
in := bytes.NewReader(halfInput) |
| 104 |
|
| 105 |
n, err := io.Copy(e, in) |
| 106 |
if err != nil { |
| 107 |
t.Errorf("Copy Error: %v", err) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if out.Len() == 0 { |
| 113 |
t.Errorf("Output length is 0 after %d bytes written", n) |
| 114 |
} |
| 115 |
if err := e.Close(); err != nil { |
| 116 |
t.Errorf("Close Error after copied %d bytes: %v", n, err) |
| 117 |
} |
| 118 |
if err := checkCompressedData(out.Bytes(), halfInput); err != nil { |
| 119 |
t.Error(err) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func TestEncoderLargeInput(t *testing.T) { |
| 124 |
input := make([]byte, 1000000) |
| 125 |
rand.Read(input) |
| 126 |
out := bytes.Buffer{} |
| 127 |
e := cbrotli.NewWriter(&out, cbrotli.WriterOptions{Quality: 5}) |
| 128 |
in := bytes.NewReader(input) |
| 129 |
|
| 130 |
n, err := io.Copy(e, in) |
| 131 |
if err != nil { |
| 132 |
t.Errorf("Copy Error: %v", err) |
| 133 |
} |
| 134 |
if int(n) != len(input) { |
| 135 |
t.Errorf("Copy() n=%v, want %v", n, len(input)) |
| 136 |
} |
| 137 |
if err := e.Close(); err != nil { |
| 138 |
t.Errorf("Close Error after copied %d bytes: %v", n, err) |
| 139 |
} |
| 140 |
if err := checkCompressedData(out.Bytes(), input); err != nil { |
| 141 |
t.Error(err) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestEncoderFlush(t *testing.T) { |
| 146 |
const payload = 32766 |
| 147 |
input := make([]byte, payload) |
| 148 |
rand.Read(input) |
| 149 |
out := bytes.Buffer{} |
| 150 |
e := cbrotli.NewWriter(&out, cbrotli.WriterOptions{Quality: 5}) |
| 151 |
in := bytes.NewReader(input) |
| 152 |
_, err := io.Copy(e, in) |
| 153 |
if err != nil { |
| 154 |
t.Fatalf("Copy Error: %v", err) |
| 155 |
} |
| 156 |
if err := e.Flush(); err != nil { |
| 157 |
t.Fatalf("Flush(): %v", err) |
| 158 |
} |
| 159 |
if out.Len() == 0 { |
| 160 |
t.Fatalf("0 bytes written after Flush()") |
| 161 |
} |
| 162 |
decompressed := make([]byte, payload) |
| 163 |
reader := cbrotli.NewReader(bytes.NewReader(out.Bytes())) |
| 164 |
n, err := reader.Read(decompressed) |
| 165 |
if n >= len(decompressed) || err != nil { |
| 166 |
t.Errorf("Expected {<%v, nil}, but got {%v, %v}", len(decompressed), n, err) |
| 167 |
} |
| 168 |
m, err := reader.Read(decompressed[n:]) |
| 169 |
n += m |
| 170 |
if n != len(decompressed) || err != nil { |
| 171 |
t.Errorf("Expected {%v, nil}, but got {%v, %v}", len(decompressed), n, err) |
| 172 |
} |
| 173 |
reader.Close() |
| 174 |
if !bytes.Equal(decompressed, input) { |
| 175 |
t.Errorf(""+ |
| 176 |
"Decompress after flush: %v\n"+ |
| 177 |
"%q\n"+ |
| 178 |
"want:\n%q", |
| 179 |
err, decompressed, input) |
| 180 |
} |
| 181 |
if err := e.Close(); err != nil { |
| 182 |
t.Errorf("Close(): %v", err) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
type readerWithTimeout struct { |
| 187 |
io.ReadCloser |
| 188 |
} |
| 189 |
|
| 190 |
func (r readerWithTimeout) Read(p []byte) (int, error) { |
| 191 |
type result struct { |
| 192 |
n int |
| 193 |
err error |
| 194 |
} |
| 195 |
ch := make(chan result) |
| 196 |
go func() { |
| 197 |
n, err := r.ReadCloser.Read(p) |
| 198 |
ch <- result{n, err} |
| 199 |
}() |
| 200 |
select { |
| 201 |
case result := <-ch: |
| 202 |
return result.n, result.err |
| 203 |
case <-time.After(5 * time.Second): |
| 204 |
return 0, fmt.Errorf("read timed out") |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestDecoderStreaming(t *testing.T) { |
| 209 |
pr, pw := io.Pipe() |
| 210 |
writer := cbrotli.NewWriter(pw, cbrotli.WriterOptions{Quality: 5, LGWin: 20}) |
| 211 |
reader := readerWithTimeout{cbrotli.NewReader(pr)} |
| 212 |
defer func() { |
| 213 |
if err := reader.Close(); err != nil { |
| 214 |
t.Errorf("reader.Close: %v", err) |
| 215 |
} |
| 216 |
go ioutil.ReadAll(pr) |
| 217 |
if err := writer.Close(); err != nil { |
| 218 |
t.Errorf("writer.Close: %v", err) |
| 219 |
} |
| 220 |
}() |
| 221 |
|
| 222 |
ch := make(chan []byte) |
| 223 |
errch := make(chan error) |
| 224 |
go func() { |
| 225 |
for { |
| 226 |
segment, ok := <-ch |
| 227 |
if !ok { |
| 228 |
return |
| 229 |
} |
| 230 |
if n, err := writer.Write(segment); err != nil || n != len(segment) { |
| 231 |
errch <- fmt.Errorf("write=%v,%v, want %v,%v", n, err, len(segment), nil) |
| 232 |
return |
| 233 |
} |
| 234 |
if err := writer.Flush(); err != nil { |
| 235 |
errch <- fmt.Errorf("flush: %v", err) |
| 236 |
return |
| 237 |
} |
| 238 |
} |
| 239 |
}() |
| 240 |
defer close(ch) |
| 241 |
|
| 242 |
segments := [...][]byte{ |
| 243 |
[]byte("first"), |
| 244 |
[]byte("second"), |
| 245 |
[]byte("third"), |
| 246 |
} |
| 247 |
for k, segment := range segments { |
| 248 |
t.Run(fmt.Sprintf("Segment%d", k), func(t *testing.T) { |
| 249 |
select { |
| 250 |
case ch <- segment: |
| 251 |
case err := <-errch: |
| 252 |
t.Fatalf("write: %v", err) |
| 253 |
case <-time.After(5 * time.Second): |
| 254 |
t.Fatalf("timed out") |
| 255 |
} |
| 256 |
wantLen := len(segment) |
| 257 |
got := make([]byte, wantLen) |
| 258 |
if n, err := reader.Read(got); err != nil || n != wantLen || !bytes.Equal(got, segment) { |
| 259 |
t.Fatalf("read[%d]=%q,%v,%v, want %q,%v,%v", k, got, n, err, segment, wantLen, nil) |
| 260 |
} |
| 261 |
}) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func TestReader(t *testing.T) { |
| 266 |
content := bytes.Repeat([]byte("hello world!"), 10000) |
| 267 |
encoded, _ := cbrotli.Encode(content, cbrotli.WriterOptions{Quality: 5}) |
| 268 |
r := cbrotli.NewReader(bytes.NewReader(encoded)) |
| 269 |
var decodedOutput bytes.Buffer |
| 270 |
n, err := io.Copy(&decodedOutput, r) |
| 271 |
if err != nil { |
| 272 |
t.Fatalf("Copy(): n=%v, err=%v", n, err) |
| 273 |
} |
| 274 |
if err := r.Close(); err != nil { |
| 275 |
t.Errorf("Close(): %v", err) |
| 276 |
} |
| 277 |
if got := decodedOutput.Bytes(); !bytes.Equal(got, content) { |
| 278 |
t.Errorf(""+ |
| 279 |
"Reader output:\n"+ |
| 280 |
"%q\n"+ |
| 281 |
"want:\n"+ |
| 282 |
"<%d bytes>", |
| 283 |
got, len(content)) |
| 284 |
} |
| 285 |
buf := make([]byte, 4) |
| 286 |
if _, err := r.Read(buf); err == nil { |
| 287 |
t.Errorf("Read-after-Close shoule have returned error") |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func TestDecode(t *testing.T) { |
| 292 |
content := bytes.Repeat([]byte("hello world!"), 10000) |
| 293 |
encoded, _ := cbrotli.Encode(content, cbrotli.WriterOptions{Quality: 5}) |
| 294 |
decoded, err := cbrotli.Decode(encoded) |
| 295 |
if err != nil { |
| 296 |
t.Errorf("Decode: %v", err) |
| 297 |
} |
| 298 |
if !bytes.Equal(decoded, content) { |
| 299 |
t.Errorf(""+ |
| 300 |
"Decode content:\n"+ |
| 301 |
"%q\n"+ |
| 302 |
"want:\n"+ |
| 303 |
"<%d bytes>", |
| 304 |
decoded, len(content)) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func TestDecodeFuzz(t *testing.T) { |
| 309 |
|
| 310 |
content := bytes.Repeat([]byte("hello world!"), 100) |
| 311 |
src := rand.NewSource(0) |
| 312 |
encoded, err := cbrotli.Encode(content, cbrotli.WriterOptions{Quality: 5}) |
| 313 |
if err != nil { |
| 314 |
t.Fatalf("Encode(<%d bytes>, _) = _, %s", len(content), err) |
| 315 |
} |
| 316 |
if len(encoded) == 0 { |
| 317 |
t.Fatalf("Encode(<%d bytes>, _) produced empty output", len(content)) |
| 318 |
} |
| 319 |
for i := 0; i < 100; i++ { |
| 320 |
enc := append([]byte{}, encoded...) |
| 321 |
for j := 0; j < 5; j++ { |
| 322 |
enc[int(src.Int63())%len(enc)] = byte(src.Int63() % 256) |
| 323 |
} |
| 324 |
cbrotli.Decode(enc) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
func TestDecodeTrailingData(t *testing.T) { |
| 329 |
content := bytes.Repeat([]byte("hello world!"), 100) |
| 330 |
encoded, _ := cbrotli.Encode(content, cbrotli.WriterOptions{Quality: 5}) |
| 331 |
_, err := cbrotli.Decode(append(encoded, 0)) |
| 332 |
if err == nil { |
| 333 |
t.Errorf("Expected 'excessive input' error") |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func TestEncodeDecode(t *testing.T) { |
| 338 |
for _, test := range []struct { |
| 339 |
data []byte |
| 340 |
repeats int |
| 341 |
}{ |
| 342 |
{nil, 0}, |
| 343 |
{[]byte("A"), 1}, |
| 344 |
{[]byte("<html><body><H1>Hello world</H1></body></html>"), 10}, |
| 345 |
{[]byte("<html><body><H1>Hello world</H1></body></html>"), 1000}, |
| 346 |
} { |
| 347 |
t.Logf("case %q x %d", test.data, test.repeats) |
| 348 |
input := bytes.Repeat(test.data, test.repeats) |
| 349 |
encoded, err := cbrotli.Encode(input, cbrotli.WriterOptions{Quality: 5}) |
| 350 |
if err != nil { |
| 351 |
t.Errorf("Encode: %v", err) |
| 352 |
} |
| 353 |
|
| 354 |
if maxSize := len(input)/2 + 20; len(encoded) >= maxSize { |
| 355 |
t.Errorf(""+ |
| 356 |
"Encode returned %d bytes, want <%d\n"+ |
| 357 |
"Encoded=%q", |
| 358 |
len(encoded), maxSize, encoded) |
| 359 |
} |
| 360 |
decoded, err := cbrotli.Decode(encoded) |
| 361 |
if err != nil { |
| 362 |
t.Errorf("Decode: %v", err) |
| 363 |
} |
| 364 |
if !bytes.Equal(decoded, input) { |
| 365 |
var want string |
| 366 |
if len(input) > 320 { |
| 367 |
want = fmt.Sprintf("<%d bytes>", len(input)) |
| 368 |
} else { |
| 369 |
want = fmt.Sprintf("%q", input) |
| 370 |
} |
| 371 |
t.Errorf(""+ |
| 372 |
"Decode content:\n"+ |
| 373 |
"%q\n"+ |
| 374 |
"want:\n"+ |
| 375 |
"%s", |
| 376 |
decoded, want) |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func TestEncodeDecodeWithDictionary(t *testing.T) { |
| 382 |
q := 5 |
| 383 |
l := 4096 |
| 384 |
|
| 385 |
input := make([]byte, l) |
| 386 |
for i := 0; i < l; i++ { |
| 387 |
input[i] = byte(i*7 + i*i*5) |
| 388 |
} |
| 389 |
|
| 390 |
pd := cbrotli.NewPreparedDictionary(input, cbrotli.DtRaw, q) |
| 391 |
defer pd.Close() |
| 392 |
|
| 393 |
encoded, err := cbrotli.Encode(input, cbrotli.WriterOptions{Quality: q, Dictionary: pd}) |
| 394 |
if err != nil { |
| 395 |
t.Errorf("Encode: %v", err) |
| 396 |
} |
| 397 |
limit := 20 |
| 398 |
if len(encoded) > limit { |
| 399 |
t.Errorf("Output length exceeds expectations: %d > %d", len(encoded), limit) |
| 400 |
} |
| 401 |
|
| 402 |
decoded, err := cbrotli.DecodeWithRawDictionary(encoded, input) |
| 403 |
if err != nil { |
| 404 |
t.Errorf("Decode: %v", err) |
| 405 |
} |
| 406 |
if !bytes.Equal(decoded, input) { |
| 407 |
var want string |
| 408 |
if len(input) > 320 { |
| 409 |
want = fmt.Sprintf("<%d bytes>", len(input)) |
| 410 |
} else { |
| 411 |
want = fmt.Sprintf("%q", input) |
| 412 |
} |
| 413 |
t.Errorf(""+ |
| 414 |
"Decode content:\n"+ |
| 415 |
"%q\n"+ |
| 416 |
"want:\n"+ |
| 417 |
"%s", |
| 418 |
decoded, want) |
| 419 |
} |
| 420 |
} |