Debian Codegraph @

gzip/gunzip_test.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package gzip
6
7 import (
8 "bufio"
9 "bytes"
10 oldgz "compress/gzip"
11 "crypto/rand"
12 "errors"
13 "io"
14 "os"
15 "strings"
16 "testing"
17 "time"
18 )
19
20 type gunzipTest struct {
21 name string
22 desc string
23 raw string
24 gzip []byte
25 err error
26 }
27
28 var gunzipTests = []gunzipTest{
29 { // has 1 empty fixed-huffman block
30 "empty.txt",
31 "empty.txt",
32 "",
33 []byte{
34 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0x5e, 0x14, 0x4a,
35 0x00, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e,
36 0x74, 0x78, 0x74, 0x00, 0x03, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 },
39 nil,
40 },
41 {
42 "",
43 "empty - with no file name",
44 "",
45 []byte{
46 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88,
47 0x00, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 },
50 nil,
51 },
52 { // has 1 non-empty fixed huffman block
53 "hello.txt",
54 "hello.txt",
55 "hello world\n",
56 []byte{
57 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
58 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
59 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
60 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
61 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
62 0x00, 0x00,
63 },
64 nil,
65 },
66 { // concatenation
67 "hello.txt",
68 "hello.txt x2",
69 "hello world\n" +
70 "hello world\n",
71 []byte{
72 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
73 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
74 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
75 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
76 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
77 0x00, 0x00,
78 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
79 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
80 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
81 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
82 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
83 0x00, 0x00,
84 },
85 nil,
86 },
87 { // has a fixed huffman block with some length-distance pairs
88 "shesells.txt",
89 "shesells.txt",
90 "she sells seashells by the seashore\n",
91 []byte{
92 0x1f, 0x8b, 0x08, 0x08, 0x72, 0x66, 0x8b, 0x4a,
93 0x00, 0x03, 0x73, 0x68, 0x65, 0x73, 0x65, 0x6c,
94 0x6c, 0x73, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b,
95 0xce, 0x48, 0x55, 0x28, 0x4e, 0xcd, 0xc9, 0x29,
96 0x06, 0x92, 0x89, 0xc5, 0x19, 0x60, 0x56, 0x52,
97 0xa5, 0x42, 0x09, 0x58, 0x18, 0x28, 0x90, 0x5f,
98 0x94, 0xca, 0x05, 0x00, 0x76, 0xb0, 0x3b, 0xeb,
99 0x24, 0x00, 0x00, 0x00,
100 },
101 nil,
102 },
103 { // has dynamic huffman blocks
104 "gettysburg",
105 "gettysburg",
106 " Four score and seven years ago our fathers brought forth on\n" +
107 "this continent, a new nation, conceived in Liberty, and dedicated\n" +
108 "to the proposition that all men are created equal.\n" +
109 " Now we are engaged in a great Civil War, testing whether that\n" +
110 "nation, or any nation so conceived and so dedicated, can long\n" +
111 "endure.\n" +
112 " We are met on a great battle-field of that war.\n" +
113 " We have come to dedicate a portion of that field, as a final\n" +
114 "resting place for those who here gave their lives that that\n" +
115 "nation might live. It is altogether fitting and proper that\n" +
116 "we should do this.\n" +
117 " But, in a larger sense, we can not dedicate — we can not\n" +
118 "consecrate — we can not hallow — this ground.\n" +
119 " The brave men, living and dead, who struggled here, have\n" +
120 "consecrated it, far above our poor power to add or detract.\n" +
121 "The world will little note, nor long remember what we say here,\n" +
122 "but it can never forget what they did here.\n" +
123 " It is for us the living, rather, to be dedicated here to the\n" +
124 "unfinished work which they who fought here have thus far so\n" +
125 "nobly advanced. It is rather for us to be here dedicated to\n" +
126 "the great task remaining before us — that from these honored\n" +
127 "dead we take increased devotion to that cause for which they\n" +
128 "gave the last full measure of devotion —\n" +
129 " that we here highly resolve that these dead shall not have\n" +
130 "died in vain — that this nation, under God, shall have a new\n" +
131 "birth of freedom — and that government of the people, by the\n" +
132 "people, for the people, shall not perish from this earth.\n" +
133 "\n" +
134 "Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania\n",
135 []byte{
136 0x1f, 0x8b, 0x08, 0x08, 0xd1, 0x12, 0x2b, 0x4a,
137 0x00, 0x03, 0x67, 0x65, 0x74, 0x74, 0x79, 0x73,
138 0x62, 0x75, 0x72, 0x67, 0x00, 0x65, 0x54, 0xcd,
139 0x6e, 0xd4, 0x30, 0x10, 0xbe, 0xfb, 0x29, 0xe6,
140 0x01, 0x42, 0xa5, 0x0a, 0x09, 0xc1, 0x11, 0x90,
141 0x40, 0x48, 0xa8, 0xe2, 0x80, 0xd4, 0xf3, 0x24,
142 0x9e, 0x24, 0x56, 0xbd, 0x9e, 0xc5, 0x76, 0x76,
143 0x95, 0x1b, 0x0f, 0xc1, 0x13, 0xf2, 0x24, 0x7c,
144 0x63, 0x77, 0x9b, 0x4a, 0x5c, 0xaa, 0x6e, 0x6c,
145 0xcf, 0x7c, 0x7f, 0x33, 0x44, 0x5f, 0x74, 0xcb,
146 0x54, 0x26, 0xcd, 0x42, 0x9c, 0x3c, 0x15, 0xb9,
147 0x48, 0xa2, 0x5d, 0x38, 0x17, 0xe2, 0x45, 0xc9,
148 0x4e, 0x67, 0xae, 0xab, 0xe0, 0xf7, 0x98, 0x75,
149 0x5b, 0xd6, 0x4a, 0xb3, 0xe6, 0xba, 0x92, 0x26,
150 0x57, 0xd7, 0x50, 0x68, 0xd2, 0x54, 0x43, 0x92,
151 0x54, 0x07, 0x62, 0x4a, 0x72, 0xa5, 0xc4, 0x35,
152 0x68, 0x1a, 0xec, 0x60, 0x92, 0x70, 0x11, 0x4f,
153 0x21, 0xd1, 0xf7, 0x30, 0x4a, 0xae, 0xfb, 0xd0,
154 0x9a, 0x78, 0xf1, 0x61, 0xe2, 0x2a, 0xde, 0x55,
155 0x25, 0xd4, 0xa6, 0x73, 0xd6, 0xb3, 0x96, 0x60,
156 0xef, 0xf0, 0x9b, 0x2b, 0x71, 0x8c, 0x74, 0x02,
157 0x10, 0x06, 0xac, 0x29, 0x8b, 0xdd, 0x25, 0xf9,
158 0xb5, 0x71, 0xbc, 0x73, 0x44, 0x0f, 0x7a, 0xa5,
159 0xab, 0xb4, 0x33, 0x49, 0x0b, 0x2f, 0xbd, 0x03,
160 0xd3, 0x62, 0x17, 0xe9, 0x73, 0xb8, 0x84, 0x48,
161 0x8f, 0x9c, 0x07, 0xaa, 0x52, 0x00, 0x6d, 0xa1,
162 0xeb, 0x2a, 0xc6, 0xa0, 0x95, 0x76, 0x37, 0x78,
163 0x9a, 0x81, 0x65, 0x7f, 0x46, 0x4b, 0x45, 0x5f,
164 0xe1, 0x6d, 0x42, 0xe8, 0x01, 0x13, 0x5c, 0x38,
165 0x51, 0xd4, 0xb4, 0x38, 0x49, 0x7e, 0xcb, 0x62,
166 0x28, 0x1e, 0x3b, 0x82, 0x93, 0x54, 0x48, 0xf1,
167 0xd2, 0x7d, 0xe4, 0x5a, 0xa3, 0xbc, 0x99, 0x83,
168 0x44, 0x4f, 0x3a, 0x77, 0x36, 0x57, 0xce, 0xcf,
169 0x2f, 0x56, 0xbe, 0x80, 0x90, 0x9e, 0x84, 0xea,
170 0x51, 0x1f, 0x8f, 0xcf, 0x90, 0xd4, 0x60, 0xdc,
171 0x5e, 0xb4, 0xf7, 0x10, 0x0b, 0x26, 0xe0, 0xff,
172 0xc4, 0xd1, 0xe5, 0x67, 0x2e, 0xe7, 0xc8, 0x93,
173 0x98, 0x05, 0xb8, 0xa8, 0x45, 0xc0, 0x4d, 0x09,
174 0xdc, 0x84, 0x16, 0x2b, 0x0d, 0x9a, 0x21, 0x53,
175 0x04, 0x8b, 0xd2, 0x0b, 0xbd, 0xa2, 0x4c, 0xa7,
176 0x60, 0xee, 0xd9, 0xe1, 0x1d, 0xd1, 0xb7, 0x4a,
177 0x30, 0x8f, 0x63, 0xd5, 0xa5, 0x8b, 0x33, 0x87,
178 0xda, 0x1a, 0x18, 0x79, 0xf3, 0xe3, 0xa6, 0x17,
179 0x94, 0x2e, 0xab, 0x6e, 0xa0, 0xe3, 0xcd, 0xac,
180 0x50, 0x8c, 0xca, 0xa7, 0x0d, 0x76, 0x37, 0xd1,
181 0x23, 0xe7, 0x05, 0x57, 0x8b, 0xa4, 0x22, 0x83,
182 0xd9, 0x62, 0x52, 0x25, 0xad, 0x07, 0xbb, 0xbf,
183 0xbf, 0xff, 0xbc, 0xfa, 0xee, 0x20, 0x73, 0x91,
184 0x29, 0xff, 0x7f, 0x02, 0x71, 0x62, 0x84, 0xb5,
185 0xf6, 0xb5, 0x25, 0x6b, 0x41, 0xde, 0x92, 0xb7,
186 0x76, 0x3f, 0x91, 0x91, 0x31, 0x1b, 0x41, 0x84,
187 0x62, 0x30, 0x0a, 0x37, 0xa4, 0x5e, 0x18, 0x3a,
188 0x99, 0x08, 0xa5, 0xe6, 0x6d, 0x59, 0x22, 0xec,
189 0x33, 0x39, 0x86, 0x26, 0xf5, 0xab, 0x66, 0xc8,
190 0x08, 0x20, 0xcf, 0x0c, 0xd7, 0x47, 0x45, 0x21,
191 0x0b, 0xf6, 0x59, 0xd5, 0xfe, 0x5c, 0x8d, 0xaa,
192 0x12, 0x7b, 0x6f, 0xa1, 0xf0, 0x52, 0x33, 0x4f,
193 0xf5, 0xce, 0x59, 0xd3, 0xab, 0x66, 0x10, 0xbf,
194 0x06, 0xc4, 0x31, 0x06, 0x73, 0xd6, 0x80, 0xa2,
195 0x78, 0xc2, 0x45, 0xcb, 0x03, 0x65, 0x39, 0xc9,
196 0x09, 0xd1, 0x06, 0x04, 0x33, 0x1a, 0x5a, 0xf1,
197 0xde, 0x01, 0xb8, 0x71, 0x83, 0xc4, 0xb5, 0xb3,
198 0xc3, 0x54, 0x65, 0x33, 0x0d, 0x5a, 0xf7, 0x9b,
199 0x90, 0x7c, 0x27, 0x1f, 0x3a, 0x58, 0xa3, 0xd8,
200 0xfd, 0x30, 0x5f, 0xb7, 0xd2, 0x66, 0xa2, 0x93,
201 0x1c, 0x28, 0xb7, 0xe9, 0x1b, 0x0c, 0xe1, 0x28,
202 0x47, 0x26, 0xbb, 0xe9, 0x7d, 0x7e, 0xdc, 0x96,
203 0x10, 0x92, 0x50, 0x56, 0x7c, 0x06, 0xe2, 0x27,
204 0xb4, 0x08, 0xd3, 0xda, 0x7b, 0x98, 0x34, 0x73,
205 0x9f, 0xdb, 0xf6, 0x62, 0xed, 0x31, 0x41, 0x13,
206 0xd3, 0xa2, 0xa8, 0x4b, 0x3a, 0xc6, 0x1d, 0xe4,
207 0x2f, 0x8c, 0xf8, 0xfb, 0x97, 0x64, 0xf4, 0xb6,
208 0x2f, 0x80, 0x5a, 0xf3, 0x56, 0xe0, 0x40, 0x50,
209 0xd5, 0x19, 0xd0, 0x1e, 0xfc, 0xca, 0xe5, 0xc9,
210 0xd4, 0x60, 0x00, 0x81, 0x2e, 0xa3, 0xcc, 0xb6,
211 0x52, 0xf0, 0xb4, 0xdb, 0x69, 0x99, 0xce, 0x7a,
212 0x32, 0x4c, 0x08, 0xed, 0xaa, 0x10, 0x10, 0xe3,
213 0x6f, 0xee, 0x99, 0x68, 0x95, 0x9f, 0x04, 0x71,
214 0xb2, 0x49, 0x2f, 0x62, 0xa6, 0x5e, 0xb4, 0xef,
215 0x02, 0xed, 0x4f, 0x27, 0xde, 0x4a, 0x0f, 0xfd,
216 0xc1, 0xcc, 0xdd, 0x02, 0x8f, 0x08, 0x16, 0x54,
217 0xdf, 0xda, 0xca, 0xe0, 0x82, 0xf1, 0xb4, 0x31,
218 0x7a, 0xa9, 0x81, 0xfe, 0x90, 0xb7, 0x3e, 0xdb,
219 0xd3, 0x35, 0xc0, 0x20, 0x80, 0x33, 0x46, 0x4a,
220 0x63, 0xab, 0xd1, 0x0d, 0x29, 0xd2, 0xe2, 0x84,
221 0xb8, 0xdb, 0xfa, 0xe9, 0x89, 0x44, 0x86, 0x7c,
222 0xe8, 0x0b, 0xe6, 0x02, 0x6a, 0x07, 0x9b, 0x96,
223 0xd0, 0xdb, 0x2e, 0x41, 0x4c, 0xa1, 0xd5, 0x57,
224 0x45, 0x14, 0xfb, 0xe3, 0xa6, 0x72, 0x5b, 0x87,
225 0x6e, 0x0c, 0x6d, 0x5b, 0xce, 0xe0, 0x2f, 0xe2,
226 0x21, 0x81, 0x95, 0xb0, 0xe8, 0xb6, 0x32, 0x0b,
227 0xb2, 0x98, 0x13, 0x52, 0x5d, 0xfb, 0xec, 0x63,
228 0x17, 0x8a, 0x9e, 0x23, 0x22, 0x36, 0xee, 0xcd,
229 0xda, 0xdb, 0xcf, 0x3e, 0xf1, 0xc7, 0xf1, 0x01,
230 0x12, 0x93, 0x0a, 0xeb, 0x6f, 0xf2, 0x02, 0x15,
231 0x96, 0x77, 0x5d, 0xef, 0x9c, 0xfb, 0x88, 0x91,
232 0x59, 0xf9, 0x84, 0xdd, 0x9b, 0x26, 0x8d, 0x80,
233 0xf9, 0x80, 0x66, 0x2d, 0xac, 0xf7, 0x1f, 0x06,
234 0xba, 0x7f, 0xff, 0xee, 0xed, 0x40, 0x5f, 0xa5,
235 0xd6, 0xbd, 0x8c, 0x5b, 0x46, 0xd2, 0x7e, 0x48,
236 0x4a, 0x65, 0x8f, 0x08, 0x42, 0x60, 0xf7, 0x0f,
237 0xb9, 0x16, 0x0b, 0x0c, 0x1a, 0x06, 0x00, 0x00,
238 },
239 nil,
240 },
241 { // has 1 non-empty fixed huffman block then garbage
242 "hello.txt",
243 "hello.txt + garbage",
244 "hello world\n",
245 []byte{
246 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
247 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
248 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
249 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
250 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
251 0x00, 0x00, 'g', 'a', 'r', 'b', 'a', 'g', 'e', '!', '!', '!',
252 },
253 ErrHeader,
254 },
255 { // has 1 non-empty fixed huffman block not enough header
256 "hello.txt",
257 "hello.txt + garbage",
258 "hello world\n",
259 []byte{
260 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
261 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
262 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
263 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
264 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
265 0x00, 0x00, gzipID1,
266 },
267 io.ErrUnexpectedEOF,
268 },
269 { // has 1 non-empty fixed huffman block but corrupt checksum
270 "hello.txt",
271 "hello.txt + corrupt checksum",
272 "hello world\n",
273 []byte{
274 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
275 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
276 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
277 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
278 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0c, 0x00,
279 0x00, 0x00,
280 },
281 ErrChecksum,
282 },
283 { // has 1 non-empty fixed huffman block but corrupt size
284 "hello.txt",
285 "hello.txt + corrupt size",
286 "hello world\n",
287 []byte{
288 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
289 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
290 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
291 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
292 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0xff, 0x00,
293 0x00, 0x00,
294 },
295 ErrChecksum,
296 },
297 {
298 "f1l3n4m3.tXt",
299 "header with all fields used",
300 "",
301 []byte{
302 0x1f, 0x8b, 0x08, 0x1e, 0x70, 0xf0, 0xf9, 0x4a,
303 0x00, 0xaa, 0x09, 0x00, 0x7a, 0x7a, 0x05, 0x00,
304 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x31, 0x6c,
305 0x33, 0x6e, 0x34, 0x6d, 0x33, 0x2e, 0x74, 0x58,
306 0x74, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
307 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
308 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
309 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
310 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
311 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
312 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
313 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
314 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
315 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
316 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
317 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e,
318 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
319 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
320 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
321 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e,
322 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
323 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
324 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
325 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
326 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
327 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
328 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
329 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
330 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
331 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
332 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
333 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
334 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
335 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
336 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
337 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
338 0xff, 0x00, 0x92, 0xfd, 0x01, 0x00, 0x00, 0xff,
339 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340 0x00,
341 },
342 nil,
343 },
344 {
345 "",
346 "truncated gzip file amid raw-block",
347 "hello",
348 []byte{
349 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
350 0x00, 0x0c, 0x00, 0xf3, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
351 },
352 io.ErrUnexpectedEOF,
353 },
354 {
355 "",
356 "truncated gzip file amid fixed-block",
357 "He",
358 []byte{
359 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
360 0xf2, 0x48, 0xcd,
361 },
362 io.ErrUnexpectedEOF,
363 },
364 }
365
366 func TestDecompressor(t *testing.T) {
367 b := new(bytes.Buffer)
368 for _, tt := range gunzipTests {
369 in := bytes.NewReader(tt.gzip)
370 gzip, err := NewReader(in)
371 if err != nil {
372 t.Errorf("%s: NewReader: %s", tt.name, err)
373 continue
374 }
375 defer gzip.Close()
376 if tt.name != gzip.Name {
377 t.Errorf("%s: got name %s", tt.name, gzip.Name)
378 }
379 b.Reset()
380 n, err := io.Copy(b, gzip)
381 if err != tt.err {
382 t.Errorf("%s: io.Copy: %v want %v", tt.name, err, tt.err)
383 }
384 s := b.String()
385 if s != tt.raw {
386 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw)
387 }
388
389 // Test Reader Reset.
390 in = bytes.NewReader(tt.gzip)
391 err = gzip.Reset(in)
392 if err != nil {
393 t.Errorf("%s: Reset: %s", tt.name, err)
394 continue
395 }
396 if tt.name != gzip.Name {
397 t.Errorf("%s: got name %s", tt.name, gzip.Name)
398 }
399 b.Reset()
400 n, err = io.Copy(b, gzip)
401 if err != tt.err {
402 t.Errorf("%s: io.Copy: %v want %v", tt.name, err, tt.err)
403 }
404 s = b.String()
405 if s != tt.raw {
406 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw)
407 }
408 }
409 }
410
411 func TestIssue6550(t *testing.T) {
412 b, err := os.ReadFile("testdata/issue6550.gz")
413 if err != nil {
414 t.Fatal(err)
415 }
416 // issue6550.gz has invalid flag bit set.
417 // TestReservedBits tests that. Fix it up for this test.
418 b[3] &= 0x1f
419 gzip, err := NewReader(bytes.NewReader(b))
420 if err != nil {
421 t.Fatalf("NewReader(testdata/issue6550.gz): %v", err)
422 }
423 defer gzip.Close()
424 done := make(chan bool, 1)
425 go func() {
426 _, err := io.Copy(io.Discard, gzip)
427 if err == nil {
428 t.Errorf("Copy succeeded")
429 } else {
430 t.Logf("Copy failed (correctly): %v", err)
431 }
432 done <- true
433 }()
434 select {
435 case <-time.After(1 * time.Second):
436 t.Errorf("Copy hung")
437 case <-done:
438 // ok
439 }
440 }
441
442 func TestReservedBits(t *testing.T) {
443 f, err := os.Open("testdata/issue6550.gz")
444 if err != nil {
445 t.Fatal(err)
446 }
447 defer f.Close()
448 _, err = NewReader(f)
449 if err == nil {
450 t.Fatalf("Reserved bits did not result in error")
451 }
452 if !errors.Is(err, ErrHeader) {
453 t.Fatal("Expected ErrHeader, got", err)
454 }
455 }
456
457 func TestInitialReset(t *testing.T) {
458 var r Reader
459 if err := r.Reset(bytes.NewReader(gunzipTests[1].gzip)); err != nil {
460 t.Error(err)
461 }
462 var buf bytes.Buffer
463 if _, err := io.Copy(&buf, &r); err != nil {
464 t.Error(err)
465 }
466 if s := buf.String(); s != gunzipTests[1].raw {
467 t.Errorf("got %q want %q", s, gunzipTests[1].raw)
468 }
469 }
470
471 func TestMultistreamFalse(t *testing.T) {
472 // Find concatenation test.
473 var tt gunzipTest
474 for _, tt = range gunzipTests {
475 if strings.HasSuffix(tt.desc, " x2") {
476 goto Found
477 }
478 }
479 t.Fatal("cannot find hello.txt x2 in gunzip tests")
480
481 Found:
482 br := bytes.NewReader(tt.gzip)
483 var r Reader
484 if err := r.Reset(br); err != nil {
485 t.Fatalf("first reset: %v", err)
486 }
487
488 // Expect two streams with "hello world\n", then real EOF.
489 const hello = "hello world\n"
490
491 r.Multistream(false)
492 data, err := io.ReadAll(&r)
493 if string(data) != hello || err != nil {
494 t.Fatalf("first stream = %q, %v, want %q, %v", string(data), err, hello, nil)
495 }
496
497 if err := r.Reset(br); err != nil {
498 t.Fatalf("second reset: %v", err)
499 }
500 r.Multistream(false)
501 data, err = io.ReadAll(&r)
502 if string(data) != hello || err != nil {
503 t.Fatalf("second stream = %q, %v, want %q, %v", string(data), err, hello, nil)
504 }
505
506 if err := r.Reset(br); err != io.EOF {
507 t.Fatalf("third reset: err=%v, want io.EOF", err)
508 }
509 }
510
511 func TestWriteTo(t *testing.T) {
512 input := make([]byte, 100000)
513 n, err := rand.Read(input)
514 if err != nil {
515 t.Fatal(err)
516 }
517 if n != len(input) {
518 t.Fatal("did not fill buffer")
519 }
520 compressed := &bytes.Buffer{}
521 // Do it twice to test MultiStream functionality
522 for range 2 {
523 w, err := NewWriterLevel(compressed, -2)
524 if err != nil {
525 t.Fatal(err)
526 }
527 n, err = w.Write(input)
528 if err != nil {
529 t.Fatal(err)
530 }
531 if n != len(input) {
532 t.Fatal("did not fill buffer")
533 }
534 w.Close()
535 }
536 input = append(input, input...)
537 buf := compressed.Bytes()
538
539 dec, err := NewReader(bytes.NewBuffer(buf))
540 if err != nil {
541 t.Fatal(err)
542 }
543 // ReadAll does not use WriteTo, but we wrap it in a NopCloser to be sure.
544 readall, err := io.ReadAll(io.NopCloser(dec))
545 if err != nil {
546 t.Fatal(err)
547 }
548 if len(readall) != len(input) {
549 t.Errorf("did not decompress everything, want %d, got %d", len(input), len(readall))
550 }
551 if !bytes.Equal(readall, input) {
552 t.Error("output did not match input")
553 }
554
555 dec, err = NewReader(bytes.NewBuffer(buf))
556 if err != nil {
557 t.Fatal(err)
558 }
559 wtbuf := &bytes.Buffer{}
560
561 written, err := dec.WriteTo(wtbuf)
562 if err != nil {
563 t.Fatal(err)
564 }
565 if written != int64(len(input)) {
566 t.Error("Returned length did not match, expected", len(input), "got", written)
567 }
568 if wtbuf.Len() != len(input) {
569 t.Error("Actual Length did not match, expected", len(input), "got", wtbuf.Len())
570 }
571 if !bytes.Equal(wtbuf.Bytes(), input) {
572 t.Fatal("output did not match input")
573 }
574 }
575
576 func TestNilStream(t *testing.T) {
577 // Go liberally interprets RFC 1952 section 2.2 to mean that a gzip file
578 // consist of zero or more members. Thus, we test that a nil stream is okay.
579 _, err := NewReader(bytes.NewReader(nil))
580 if err != io.EOF {
581 t.Fatalf("NewReader(nil) on empty stream: got %v, want io.EOF", err)
582 }
583 }
584
585 func TestTruncatedStreams(t *testing.T) {
586 const data = "\x1f\x8b\b\x04\x00\tn\x88\x00\xff\a\x00foo bar\xcbH\xcd\xc9\xc9\xd7Q(\xcf/\xcaI\x01\x04:r\xab\xff\f\x00\x00\x00"
587
588 // Intentionally iterate starting with at least one byte in the stream.
589 for i := 1; i < len(data)-1; i++ {
590 r, err := NewReader(strings.NewReader(data[:i]))
591 if err != nil {
592 if err != io.ErrUnexpectedEOF {
593 t.Errorf("NewReader(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
594 }
595 continue
596 }
597 _, err = io.Copy(io.Discard, r)
598 if err != io.ErrUnexpectedEOF {
599 t.Errorf("io.Copy(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
600 }
601 }
602 }
603
604 func BenchmarkGunzipCopy(b *testing.B) {
605 dat, _ := os.ReadFile("testdata/test.json")
606 dat = append(dat, dat...)
607 dat = append(dat, dat...)
608 dat = append(dat, dat...)
609 dat = append(dat, dat...)
610 dat = append(dat, dat...)
611 dst := &bytes.Buffer{}
612 w, _ := NewWriterLevel(dst, 1)
613 _, err := w.Write(dat)
614 if err != nil {
615 b.Fatal(err)
616 }
617 w.Close()
618 input := dst.Bytes()
619 b.SetBytes(int64(len(dat)))
620 b.ResetTimer()
621 for n := 0; n < b.N; n++ {
622 r, err := NewReader(bytes.NewBuffer(input))
623 if err != nil {
624 b.Fatal(err)
625 }
626 _, err = io.Copy(io.Discard, r)
627 if err != nil {
628 b.Fatal(err)
629 }
630 }
631 }
632
633 func BenchmarkGunzipNoWriteTo(b *testing.B) {
634 dat, _ := os.ReadFile("testdata/test.json")
635 dat = append(dat, dat...)
636 dat = append(dat, dat...)
637 dat = append(dat, dat...)
638 dat = append(dat, dat...)
639 dat = append(dat, dat...)
640 dst := &bytes.Buffer{}
641 w, _ := NewWriterLevel(dst, 1)
642 _, err := w.Write(dat)
643 if err != nil {
644 b.Fatal(err)
645 }
646 w.Close()
647 input := dst.Bytes()
648 r, err := NewReader(bytes.NewBuffer(input))
649 if err != nil {
650 b.Fatal(err)
651 }
652 b.SetBytes(int64(len(dat)))
653 b.ResetTimer()
654 for n := 0; n < b.N; n++ {
655 err := r.Reset(bytes.NewBuffer(input))
656 if err != nil {
657 b.Fatal(err)
658 }
659 _, err = io.Copy(io.Discard, io.NopCloser(r))
660 if err != nil {
661 b.Fatal(err)
662 }
663 }
664 }
665
666 func BenchmarkGunzipStdlib(b *testing.B) {
667 dat, _ := os.ReadFile("testdata/test.json")
668 dat = append(dat, dat...)
669 dat = append(dat, dat...)
670 dat = append(dat, dat...)
671 dat = append(dat, dat...)
672 dat = append(dat, dat...)
673 dst := &bytes.Buffer{}
674 w, _ := NewWriterLevel(dst, 1)
675 _, err := w.Write(dat)
676 if err != nil {
677 b.Fatal(err)
678 }
679 w.Close()
680 input := dst.Bytes()
681 r, err := oldgz.NewReader(bytes.NewBuffer(input))
682 if err != nil {
683 b.Fatal(err)
684 }
685 b.SetBytes(int64(len(dat)))
686 b.ResetTimer()
687 for n := 0; n < b.N; n++ {
688 err := r.Reset(bytes.NewBuffer(input))
689 if err != nil {
690 b.Fatal(err)
691 }
692 _, err = io.Copy(io.Discard, r)
693 if err != nil {
694 b.Fatal(err)
695 }
696 }
697 }
698 func TestTruncatedGunzip(t *testing.T) {
699 in := []byte(strings.Repeat("ASDFASDFASDFASDFASDF", 1000))
700 var buf bytes.Buffer
701 enc := NewWriter(&buf)
702 _, err := enc.Write(in)
703 if err != nil {
704 t.Fatal(err)
705 }
706 enc.Close()
707 testdata := buf.Bytes()
708 for i := 5; i < len(testdata); i += 10 {
709 timer := time.NewTimer(time.Second)
710 done := make(chan struct{})
711 fail := make(chan struct{})
712 go func() {
713 r, err := NewReader(bytes.NewBuffer(testdata[:i]))
714 if err == nil {
715 b, err := io.ReadAll(r)
716 if err == nil && !bytes.Equal(testdata[:i], b) {
717 close(fail)
718 }
719 }
720 close(done)
721 }()
722 select {
723 case <-timer.C:
724 t.Fatal("Timeout decoding")
725 case <-fail:
726 t.Fatal("No error, but mismatch")
727 case <-done:
728 timer.Stop()
729 }
730 }
731 }
732
733 func TestBufferedPartialCopyGzip(t *testing.T) {
734 var (
735 in = []byte("hello\nworld")
736 compressedIn []byte
737 )
738
739 var buf bytes.Buffer
740 gzw := NewWriter(&buf)
741 if _, err := gzw.Write(in); err != nil {
742 panic(err)
743 }
744 if err := gzw.Flush(); err != nil {
745 panic(err)
746 }
747 if err := gzw.Close(); err != nil {
748 panic(err)
749 }
750
751 compressedIn = buf.Bytes()
752
753 gz, err := NewReader(bytes.NewReader(compressedIn))
754 if err != nil {
755 t.Errorf("constructing a reader: %v", err)
756 }
757
758 br := bufio.NewReader(gz)
759 if _, err := br.ReadBytes('\n'); err != nil {
760 t.Errorf("reading to the first newline: %v", err)
761 }
762
763 var out bytes.Buffer
764 _, err = io.Copy(&out, br)
765 if !bytes.Equal(out.Bytes(), []byte("world")) {
766 t.Errorf("unexpected output when reading the remainder")
767 }
768 if err != nil {
769 t.Errorf("reading the remainder: %v", err)
770 }
771 }