Debian Codegraph @

xpress/xpress_test.go

raw
1 package xpress
2
3 import (
4 "bytes"
5 "errors"
6 "strings"
7 "testing"
8 )
9
10 func TestXpressPlainLZ77(t *testing.T) {
11 for _, v := range xpressVectors {
12 if v.huff {
13 continue
14 }
15 out, err := AppendDecompressed(nil, v.data)
16 if err != nil {
17 t.Errorf("%s: %v", v.name, err)
18 continue
19 }
20 if string(out) != string(v.expected) {
21 t.Errorf("%s: mismatched output (%d bytes)", v.name, len(out))
22 }
23 }
24 }
25
26 func TestXpressHuffman(t *testing.T) {
27 for _, v := range xpressVectors {
28 if !v.huff {
29 continue
30 }
31 out, err := AppendHDecompressed(nil, v.data, len(v.expected))
32 if err != nil {
33 t.Errorf("%s: %v", v.name, err)
34 continue
35 }
36 if string(out) != string(v.expected) {
37 t.Errorf("%s: mismatched output (%d bytes)", v.name, len(out))
38 }
39 }
40 }
41
42 func TestXpressTruncated(t *testing.T) {
43 var byName = func(name string) xpressVector {
44 for _, v := range xpressVectors {
45 if strings.HasPrefix(v.name, name) {
46 return v
47 }
48 }
49 t.Fatalf("vector %q not found", name)
50 return xpressVector{}
51 }
52
53 // Plain LZ77: cut off mid-match.
54 plain := byName("Plain1")
55 _, err := AppendDecompressed(nil, plain.data[:8])
56 if !errors.Is(err, errTruncated) {
57 t.Errorf("plain: expected truncated stream, got %v", err)
58 }
59
60 // Huffman: fewer than the mandatory 256 table bytes.
61 huff0 := byName("Huff0")
62 _, err = AppendHDecompressed(nil, huff0.data[:100], 360)
63 if !errors.Is(err, errTruncated) {
64 t.Errorf("huffman: expected truncated stream on short table, got %v", err)
65 }
66
67 // Huffman: declare a larger output than the stream contains. The
68 // stream's end-of-data symbol lands mid-loop with fewer than 3
69 // bytes remaining, so the decoder reports a corrupt stream.
70 huff5 := byName("Huff5")
71 _, err = AppendHDecompressed(nil, huff5.data, 22)
72 if !errors.Is(err, errCorrupt) {
73 t.Errorf("huffman: expected corrupt stream on oversized declare, got %v", err)
74 }
75 }
76
77 func TestXpressExpansionRatio(t *testing.T) {
78 // A match with a 4GB raw length via the LE32 extension must be
79 // rejected by the decompressed-size cap. The flag group is stored
80 // little-endian, so bit 31 (a match) is the last byte 0x40.
81 in := []byte{
82 0x00, 0x00, 0x00, 0x40, // flag group: literal, then match
83 0x41, // literal 'A'
84 0x07, 0x00, // match: offset 1, length nibble 7
85 0xFF, // nibble 15: raw length byte follows
86 0xFF, // raw length byte 255: LE16 follows
87 0x00, 0x00, // -> LE16 0: LE32 follows
88 0xFF, 0xFF, 0xFF, 0xFF, // -> LE32 (4GB - 1)
89 }
90 _, err := AppendDecompressed(nil, in)
91 if !errors.Is(err, errTooLarge) {
92 t.Errorf("plain: expected compression ratio error, got %v", err)
93 }
94 }
95
96 func TestXpressCorruptCode(t *testing.T) {
97 // Huffman table of all-zero lengths is an incomplete prefix code.
98 in := make([]byte, 260)
99 _, err := AppendHDecompressed(nil, in, 1)
100 if err == nil {
101 t.Error("huffman: expected error on invalid code lengths")
102 }
103 }
104
105 func TestXpressOOB(t *testing.T) {
106 // A match flag with only one byte of the two-byte match word.
107 _, err := AppendDecompressed(nil, []byte{0x00, 0x00, 0x00, 0x80, 0x01})
108 if !errors.Is(err, errTruncated) {
109 t.Errorf("plain: expected truncated stream, got %v", err)
110 }
111
112 // A match farther back than the output produced so far.
113 _, err = AppendDecompressed(nil, []byte{0x00, 0x00, 0x00, 0x40, 0x41, 0x50, 0x00})
114 if !errors.Is(err, errCorrupt) {
115 t.Errorf("plain: expected out-of-range offset, got %v", err)
116 }
117 }
118
119 func TestXpressAppendMode(t *testing.T) {
120 // The append-API keeps the caller's prefix and continues decoding.
121 for _, v := range xpressVectors {
122 if v.huff {
123 continue
124 }
125 prefix := []byte("PRE")
126 out, err := AppendDecompressed(prefix, v.data)
127 if err != nil {
128 t.Fatal(err)
129 }
130 if string(out[:len(prefix)]) != "PRE" {
131 t.Error("append: prefix lost")
132 }
133 if string(out[len(prefix):]) != string(v.expected) {
134 t.Error("append: decoded body mismatch")
135 }
136 prefix = make([]byte, MaxSize)
137 out, err = AppendDecompressed(prefix, v.data)
138 if err != nil {
139 t.Fatal(err)
140 }
141 if !bytes.Equal(out[:MaxSize], prefix) {
142 t.Error("append: prefix lost")
143 }
144 if string(out[len(prefix):]) != string(v.expected) {
145 t.Error("append: decoded body mismatch")
146 }
147
148 // On error the output is returned unmodified: the returned slice
149 // must equal the original prefix, not the partially built prefix.
150 prefix2 := []byte("PRE")
151 out2, err := AppendDecompressed(prefix2, []byte{0x00, 0x00, 0x00, 0x40, 0x41, 0x07, 0x00})
152 if !errors.Is(err, errTruncated) {
153 t.Fatalf("expected truncation error, got %v", err)
154 }
155 if !bytes.Equal(out2, prefix2) {
156 t.Errorf("append: error path modified the slice: %q", out2)
157 }
158 }
159 }
160
161 func TestXpressAppendModeHuff(t *testing.T) {
162 // The append-API keeps the caller's prefix and continues decoding.
163 for _, v := range xpressVectors {
164 if !v.huff {
165 continue
166 }
167 prefix := []byte("PRE")
168 out, err := AppendHDecompressed(prefix, v.data, len(v.expected))
169 if err != nil {
170 t.Fatal(err)
171 }
172 if string(out[:len(prefix)]) != "PRE" {
173 t.Error("append: prefix lost")
174 }
175 if string(out[len(prefix):]) != string(v.expected) {
176 t.Error("append: decoded body mismatch")
177 }
178 prefix = make([]byte, MaxSize)
179 out, err = AppendHDecompressed(prefix, v.data, len(v.expected))
180 if err != nil {
181 t.Fatal(err)
182 }
183 if !bytes.Equal(out[:MaxSize], prefix) {
184 t.Error("append: prefix lost")
185 }
186 if string(out[len(prefix):]) != string(v.expected) {
187 t.Error("append: decoded body mismatch")
188 }
189 // On error the output is returned unmodified: the returned slice
190 // must equal the original prefix, not the partially built prefix.
191 prefix2 := []byte("PRE")
192 out2, err := AppendHDecompressed(prefix2, []byte{0x00, 0x00, 0x00, 0x40, 0x41, 0x07, 0x00}, 7)
193 if !errors.Is(err, errTruncated) {
194 t.Fatalf("expected truncation error, got %v", err)
195 }
196 if !bytes.Equal(out2, prefix2) {
197 t.Errorf("append: error path modified the slice: %q", out2)
198 }
199 }
200 }