-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_test.go
More file actions
89 lines (79 loc) · 2.16 KB
/
compression_test.go
File metadata and controls
89 lines (79 loc) · 2.16 KB
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
package cache
import (
"bytes"
"compress/gzip"
"errors"
"testing"
)
func TestEncodeValueRespectsLimitEqualsLen(t *testing.T) {
out, err := encodeValue(CompressionNone, 3, []byte("abc"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(out) != "abc" {
t.Fatalf("unexpected output: %s", string(out))
}
}
func TestDecodeValuePassThrough(t *testing.T) {
in := []byte("plain")
out, err := decodeValue(in)
if err != nil {
t.Fatalf("decode err: %v", err)
}
if string(out) != "plain" {
t.Fatalf("expected passthrough")
}
}
func TestDecodeValueShortInput(t *testing.T) {
out, err := decodeValue([]byte("tiny"))
if err != nil {
t.Fatalf("decode short err: %v", err)
}
if string(out) != "tiny" {
t.Fatalf("expected passthrough on short input")
}
}
func TestDecodeValueSnappyUnsupported(t *testing.T) {
in := append([]byte("CMP1"), 's')
in = append(in, []byte{0x00}...)
if _, err := decodeValue(in); !errors.Is(err, ErrUnsupportedCodec) {
t.Fatalf("expected unsupported codec error, got %v", err)
}
}
func TestEncodeValueGzipEarlySizeCheck(t *testing.T) {
if _, err := encodeValue(CompressionGzip, 1, []byte("toolong")); !errors.Is(err, ErrValueTooLarge) {
t.Fatalf("expected size error, got %v", err)
}
}
func TestDecodeValueGzipSuccess(t *testing.T) {
encoded, err := encodeValue(CompressionGzip, 0, []byte("ok"))
if err != nil {
t.Fatalf("encode failed: %v", err)
}
decoded, err := decodeValue(encoded)
if err != nil {
t.Fatalf("decode failed: %v", err)
}
if string(decoded) != "ok" {
t.Fatalf("unexpected decode value: %s", string(decoded))
}
}
func TestDecodeValueLegacyGzipFixture(t *testing.T) {
var gz bytes.Buffer
zw := gzip.NewWriter(&gz)
if _, err := zw.Write([]byte("legacy-compressed")); err != nil {
t.Fatalf("gzip write failed: %v", err)
}
if err := zw.Close(); err != nil {
t.Fatalf("gzip close failed: %v", err)
}
fixture := append([]byte("CMP1"), 'g')
fixture = append(fixture, gz.Bytes()...)
decoded, err := decodeValue(fixture)
if err != nil {
t.Fatalf("decode legacy fixture failed: %v", err)
}
if string(decoded) != "legacy-compressed" {
t.Fatalf("unexpected decode value: %q", string(decoded))
}
}