]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/compress/gzip/gunzip_test.go
libgo: update to Go1.14beta1
[thirdparty/gcc.git] / libgo / go / compress / gzip / gunzip_test.go
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 "bytes"
9 "compress/flate"
10 "encoding/base64"
11 "io"
12 "io/ioutil"
13 "os"
14 "strings"
15 "testing"
16 "time"
17 )
18
19 type gunzipTest struct {
20 name string
21 desc string
22 raw string
23 gzip []byte
24 err error
25 }
26
27 var gunzipTests = []gunzipTest{
28 { // has 1 empty fixed-huffman block
29 "empty.txt",
30 "empty.txt",
31 "",
32 []byte{
33 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0x5e, 0x14, 0x4a,
34 0x00, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e,
35 0x74, 0x78, 0x74, 0x00, 0x03, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 },
38 nil,
39 },
40 {
41 "",
42 "empty - with no file name",
43 "",
44 []byte{
45 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88,
46 0x00, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 },
49 nil,
50 },
51 { // has 1 non-empty fixed huffman block
52 "hello.txt",
53 "hello.txt",
54 "hello world\n",
55 []byte{
56 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
57 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
58 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
59 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
60 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
61 0x00, 0x00,
62 },
63 nil,
64 },
65 { // concatenation
66 "hello.txt",
67 "hello.txt x2",
68 "hello world\n" +
69 "hello world\n",
70 []byte{
71 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
72 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
73 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
74 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
75 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
76 0x00, 0x00,
77 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
78 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
79 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
80 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
81 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
82 0x00, 0x00,
83 },
84 nil,
85 },
86 { // has a fixed huffman block with some length-distance pairs
87 "shesells.txt",
88 "shesells.txt",
89 "she sells seashells by the seashore\n",
90 []byte{
91 0x1f, 0x8b, 0x08, 0x08, 0x72, 0x66, 0x8b, 0x4a,
92 0x00, 0x03, 0x73, 0x68, 0x65, 0x73, 0x65, 0x6c,
93 0x6c, 0x73, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b,
94 0xce, 0x48, 0x55, 0x28, 0x4e, 0xcd, 0xc9, 0x29,
95 0x06, 0x92, 0x89, 0xc5, 0x19, 0x60, 0x56, 0x52,
96 0xa5, 0x42, 0x09, 0x58, 0x18, 0x28, 0x90, 0x5f,
97 0x94, 0xca, 0x05, 0x00, 0x76, 0xb0, 0x3b, 0xeb,
98 0x24, 0x00, 0x00, 0x00,
99 },
100 nil,
101 },
102 { // has dynamic huffman blocks
103 "gettysburg",
104 "gettysburg",
105 " Four score and seven years ago our fathers brought forth on\n" +
106 "this continent, a new nation, conceived in Liberty, and dedicated\n" +
107 "to the proposition that all men are created equal.\n" +
108 " Now we are engaged in a great Civil War, testing whether that\n" +
109 "nation, or any nation so conceived and so dedicated, can long\n" +
110 "endure.\n" +
111 " We are met on a great battle-field of that war.\n" +
112 " We have come to dedicate a portion of that field, as a final\n" +
113 "resting place for those who here gave their lives that that\n" +
114 "nation might live. It is altogether fitting and proper that\n" +
115 "we should do this.\n" +
116 " But, in a larger sense, we can not dedicate — we can not\n" +
117 "consecrate — we can not hallow — this ground.\n" +
118 " The brave men, living and dead, who struggled here, have\n" +
119 "consecrated it, far above our poor power to add or detract.\n" +
120 "The world will little note, nor long remember what we say here,\n" +
121 "but it can never forget what they did here.\n" +
122 " It is for us the living, rather, to be dedicated here to the\n" +
123 "unfinished work which they who fought here have thus far so\n" +
124 "nobly advanced. It is rather for us to be here dedicated to\n" +
125 "the great task remaining before us — that from these honored\n" +
126 "dead we take increased devotion to that cause for which they\n" +
127 "gave the last full measure of devotion —\n" +
128 " that we here highly resolve that these dead shall not have\n" +
129 "died in vain — that this nation, under God, shall have a new\n" +
130 "birth of freedom — and that government of the people, by the\n" +
131 "people, for the people, shall not perish from this earth.\n" +
132 "\n" +
133 "Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania\n",
134 []byte{
135 0x1f, 0x8b, 0x08, 0x08, 0xd1, 0x12, 0x2b, 0x4a,
136 0x00, 0x03, 0x67, 0x65, 0x74, 0x74, 0x79, 0x73,
137 0x62, 0x75, 0x72, 0x67, 0x00, 0x65, 0x54, 0xcd,
138 0x6e, 0xd4, 0x30, 0x10, 0xbe, 0xfb, 0x29, 0xe6,
139 0x01, 0x42, 0xa5, 0x0a, 0x09, 0xc1, 0x11, 0x90,
140 0x40, 0x48, 0xa8, 0xe2, 0x80, 0xd4, 0xf3, 0x24,
141 0x9e, 0x24, 0x56, 0xbd, 0x9e, 0xc5, 0x76, 0x76,
142 0x95, 0x1b, 0x0f, 0xc1, 0x13, 0xf2, 0x24, 0x7c,
143 0x63, 0x77, 0x9b, 0x4a, 0x5c, 0xaa, 0x6e, 0x6c,
144 0xcf, 0x7c, 0x7f, 0x33, 0x44, 0x5f, 0x74, 0xcb,
145 0x54, 0x26, 0xcd, 0x42, 0x9c, 0x3c, 0x15, 0xb9,
146 0x48, 0xa2, 0x5d, 0x38, 0x17, 0xe2, 0x45, 0xc9,
147 0x4e, 0x67, 0xae, 0xab, 0xe0, 0xf7, 0x98, 0x75,
148 0x5b, 0xd6, 0x4a, 0xb3, 0xe6, 0xba, 0x92, 0x26,
149 0x57, 0xd7, 0x50, 0x68, 0xd2, 0x54, 0x43, 0x92,
150 0x54, 0x07, 0x62, 0x4a, 0x72, 0xa5, 0xc4, 0x35,
151 0x68, 0x1a, 0xec, 0x60, 0x92, 0x70, 0x11, 0x4f,
152 0x21, 0xd1, 0xf7, 0x30, 0x4a, 0xae, 0xfb, 0xd0,
153 0x9a, 0x78, 0xf1, 0x61, 0xe2, 0x2a, 0xde, 0x55,
154 0x25, 0xd4, 0xa6, 0x73, 0xd6, 0xb3, 0x96, 0x60,
155 0xef, 0xf0, 0x9b, 0x2b, 0x71, 0x8c, 0x74, 0x02,
156 0x10, 0x06, 0xac, 0x29, 0x8b, 0xdd, 0x25, 0xf9,
157 0xb5, 0x71, 0xbc, 0x73, 0x44, 0x0f, 0x7a, 0xa5,
158 0xab, 0xb4, 0x33, 0x49, 0x0b, 0x2f, 0xbd, 0x03,
159 0xd3, 0x62, 0x17, 0xe9, 0x73, 0xb8, 0x84, 0x48,
160 0x8f, 0x9c, 0x07, 0xaa, 0x52, 0x00, 0x6d, 0xa1,
161 0xeb, 0x2a, 0xc6, 0xa0, 0x95, 0x76, 0x37, 0x78,
162 0x9a, 0x81, 0x65, 0x7f, 0x46, 0x4b, 0x45, 0x5f,
163 0xe1, 0x6d, 0x42, 0xe8, 0x01, 0x13, 0x5c, 0x38,
164 0x51, 0xd4, 0xb4, 0x38, 0x49, 0x7e, 0xcb, 0x62,
165 0x28, 0x1e, 0x3b, 0x82, 0x93, 0x54, 0x48, 0xf1,
166 0xd2, 0x7d, 0xe4, 0x5a, 0xa3, 0xbc, 0x99, 0x83,
167 0x44, 0x4f, 0x3a, 0x77, 0x36, 0x57, 0xce, 0xcf,
168 0x2f, 0x56, 0xbe, 0x80, 0x90, 0x9e, 0x84, 0xea,
169 0x51, 0x1f, 0x8f, 0xcf, 0x90, 0xd4, 0x60, 0xdc,
170 0x5e, 0xb4, 0xf7, 0x10, 0x0b, 0x26, 0xe0, 0xff,
171 0xc4, 0xd1, 0xe5, 0x67, 0x2e, 0xe7, 0xc8, 0x93,
172 0x98, 0x05, 0xb8, 0xa8, 0x45, 0xc0, 0x4d, 0x09,
173 0xdc, 0x84, 0x16, 0x2b, 0x0d, 0x9a, 0x21, 0x53,
174 0x04, 0x8b, 0xd2, 0x0b, 0xbd, 0xa2, 0x4c, 0xa7,
175 0x60, 0xee, 0xd9, 0xe1, 0x1d, 0xd1, 0xb7, 0x4a,
176 0x30, 0x8f, 0x63, 0xd5, 0xa5, 0x8b, 0x33, 0x87,
177 0xda, 0x1a, 0x18, 0x79, 0xf3, 0xe3, 0xa6, 0x17,
178 0x94, 0x2e, 0xab, 0x6e, 0xa0, 0xe3, 0xcd, 0xac,
179 0x50, 0x8c, 0xca, 0xa7, 0x0d, 0x76, 0x37, 0xd1,
180 0x23, 0xe7, 0x05, 0x57, 0x8b, 0xa4, 0x22, 0x83,
181 0xd9, 0x62, 0x52, 0x25, 0xad, 0x07, 0xbb, 0xbf,
182 0xbf, 0xff, 0xbc, 0xfa, 0xee, 0x20, 0x73, 0x91,
183 0x29, 0xff, 0x7f, 0x02, 0x71, 0x62, 0x84, 0xb5,
184 0xf6, 0xb5, 0x25, 0x6b, 0x41, 0xde, 0x92, 0xb7,
185 0x76, 0x3f, 0x91, 0x91, 0x31, 0x1b, 0x41, 0x84,
186 0x62, 0x30, 0x0a, 0x37, 0xa4, 0x5e, 0x18, 0x3a,
187 0x99, 0x08, 0xa5, 0xe6, 0x6d, 0x59, 0x22, 0xec,
188 0x33, 0x39, 0x86, 0x26, 0xf5, 0xab, 0x66, 0xc8,
189 0x08, 0x20, 0xcf, 0x0c, 0xd7, 0x47, 0x45, 0x21,
190 0x0b, 0xf6, 0x59, 0xd5, 0xfe, 0x5c, 0x8d, 0xaa,
191 0x12, 0x7b, 0x6f, 0xa1, 0xf0, 0x52, 0x33, 0x4f,
192 0xf5, 0xce, 0x59, 0xd3, 0xab, 0x66, 0x10, 0xbf,
193 0x06, 0xc4, 0x31, 0x06, 0x73, 0xd6, 0x80, 0xa2,
194 0x78, 0xc2, 0x45, 0xcb, 0x03, 0x65, 0x39, 0xc9,
195 0x09, 0xd1, 0x06, 0x04, 0x33, 0x1a, 0x5a, 0xf1,
196 0xde, 0x01, 0xb8, 0x71, 0x83, 0xc4, 0xb5, 0xb3,
197 0xc3, 0x54, 0x65, 0x33, 0x0d, 0x5a, 0xf7, 0x9b,
198 0x90, 0x7c, 0x27, 0x1f, 0x3a, 0x58, 0xa3, 0xd8,
199 0xfd, 0x30, 0x5f, 0xb7, 0xd2, 0x66, 0xa2, 0x93,
200 0x1c, 0x28, 0xb7, 0xe9, 0x1b, 0x0c, 0xe1, 0x28,
201 0x47, 0x26, 0xbb, 0xe9, 0x7d, 0x7e, 0xdc, 0x96,
202 0x10, 0x92, 0x50, 0x56, 0x7c, 0x06, 0xe2, 0x27,
203 0xb4, 0x08, 0xd3, 0xda, 0x7b, 0x98, 0x34, 0x73,
204 0x9f, 0xdb, 0xf6, 0x62, 0xed, 0x31, 0x41, 0x13,
205 0xd3, 0xa2, 0xa8, 0x4b, 0x3a, 0xc6, 0x1d, 0xe4,
206 0x2f, 0x8c, 0xf8, 0xfb, 0x97, 0x64, 0xf4, 0xb6,
207 0x2f, 0x80, 0x5a, 0xf3, 0x56, 0xe0, 0x40, 0x50,
208 0xd5, 0x19, 0xd0, 0x1e, 0xfc, 0xca, 0xe5, 0xc9,
209 0xd4, 0x60, 0x00, 0x81, 0x2e, 0xa3, 0xcc, 0xb6,
210 0x52, 0xf0, 0xb4, 0xdb, 0x69, 0x99, 0xce, 0x7a,
211 0x32, 0x4c, 0x08, 0xed, 0xaa, 0x10, 0x10, 0xe3,
212 0x6f, 0xee, 0x99, 0x68, 0x95, 0x9f, 0x04, 0x71,
213 0xb2, 0x49, 0x2f, 0x62, 0xa6, 0x5e, 0xb4, 0xef,
214 0x02, 0xed, 0x4f, 0x27, 0xde, 0x4a, 0x0f, 0xfd,
215 0xc1, 0xcc, 0xdd, 0x02, 0x8f, 0x08, 0x16, 0x54,
216 0xdf, 0xda, 0xca, 0xe0, 0x82, 0xf1, 0xb4, 0x31,
217 0x7a, 0xa9, 0x81, 0xfe, 0x90, 0xb7, 0x3e, 0xdb,
218 0xd3, 0x35, 0xc0, 0x20, 0x80, 0x33, 0x46, 0x4a,
219 0x63, 0xab, 0xd1, 0x0d, 0x29, 0xd2, 0xe2, 0x84,
220 0xb8, 0xdb, 0xfa, 0xe9, 0x89, 0x44, 0x86, 0x7c,
221 0xe8, 0x0b, 0xe6, 0x02, 0x6a, 0x07, 0x9b, 0x96,
222 0xd0, 0xdb, 0x2e, 0x41, 0x4c, 0xa1, 0xd5, 0x57,
223 0x45, 0x14, 0xfb, 0xe3, 0xa6, 0x72, 0x5b, 0x87,
224 0x6e, 0x0c, 0x6d, 0x5b, 0xce, 0xe0, 0x2f, 0xe2,
225 0x21, 0x81, 0x95, 0xb0, 0xe8, 0xb6, 0x32, 0x0b,
226 0xb2, 0x98, 0x13, 0x52, 0x5d, 0xfb, 0xec, 0x63,
227 0x17, 0x8a, 0x9e, 0x23, 0x22, 0x36, 0xee, 0xcd,
228 0xda, 0xdb, 0xcf, 0x3e, 0xf1, 0xc7, 0xf1, 0x01,
229 0x12, 0x93, 0x0a, 0xeb, 0x6f, 0xf2, 0x02, 0x15,
230 0x96, 0x77, 0x5d, 0xef, 0x9c, 0xfb, 0x88, 0x91,
231 0x59, 0xf9, 0x84, 0xdd, 0x9b, 0x26, 0x8d, 0x80,
232 0xf9, 0x80, 0x66, 0x2d, 0xac, 0xf7, 0x1f, 0x06,
233 0xba, 0x7f, 0xff, 0xee, 0xed, 0x40, 0x5f, 0xa5,
234 0xd6, 0xbd, 0x8c, 0x5b, 0x46, 0xd2, 0x7e, 0x48,
235 0x4a, 0x65, 0x8f, 0x08, 0x42, 0x60, 0xf7, 0x0f,
236 0xb9, 0x16, 0x0b, 0x0c, 0x1a, 0x06, 0x00, 0x00,
237 },
238 nil,
239 },
240 { // has 1 non-empty fixed huffman block then garbage
241 "hello.txt",
242 "hello.txt + garbage",
243 "hello world\n",
244 []byte{
245 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
246 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
247 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
248 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
249 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
250 0x00, 0x00, 'g', 'a', 'r', 'b', 'a', 'g', 'e', '!', '!', '!',
251 },
252 ErrHeader,
253 },
254 { // has 1 non-empty fixed huffman block not enough header
255 "hello.txt",
256 "hello.txt + garbage",
257 "hello world\n",
258 []byte{
259 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
260 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
261 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
262 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
263 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
264 0x00, 0x00, gzipID1,
265 },
266 io.ErrUnexpectedEOF,
267 },
268 { // has 1 non-empty fixed huffman block but corrupt checksum
269 "hello.txt",
270 "hello.txt + corrupt checksum",
271 "hello world\n",
272 []byte{
273 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
274 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
275 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
276 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
277 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0c, 0x00,
278 0x00, 0x00,
279 },
280 ErrChecksum,
281 },
282 { // has 1 non-empty fixed huffman block but corrupt size
283 "hello.txt",
284 "hello.txt + corrupt size",
285 "hello world\n",
286 []byte{
287 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
288 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
289 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
290 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
291 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0xff, 0x00,
292 0x00, 0x00,
293 },
294 ErrChecksum,
295 },
296 {
297 "f1l3n4m3.tXt",
298 "header with all fields used",
299 "",
300 []byte{
301 0x1f, 0x8b, 0x08, 0x1e, 0x70, 0xf0, 0xf9, 0x4a,
302 0x00, 0xaa, 0x09, 0x00, 0x7a, 0x7a, 0x05, 0x00,
303 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x31, 0x6c,
304 0x33, 0x6e, 0x34, 0x6d, 0x33, 0x2e, 0x74, 0x58,
305 0x74, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
306 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
307 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
308 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
309 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
310 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
311 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
312 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
313 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
314 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
315 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
316 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e,
317 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
318 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
319 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
320 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e,
321 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
322 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
323 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
324 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
325 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
326 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
327 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
328 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
329 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
330 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
331 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
332 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
333 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
334 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
335 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
336 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
337 0xff, 0x00, 0x92, 0xfd, 0x01, 0x00, 0x00, 0xff,
338 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339 0x00,
340 },
341 nil,
342 },
343 {
344 "",
345 "truncated gzip file amid raw-block",
346 "hello",
347 []byte{
348 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
349 0x00, 0x0c, 0x00, 0xf3, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
350 },
351 io.ErrUnexpectedEOF,
352 },
353 {
354 "",
355 "truncated gzip file amid fixed-block",
356 "He",
357 []byte{
358 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
359 0xf2, 0x48, 0xcd,
360 },
361 io.ErrUnexpectedEOF,
362 },
363 }
364
365 func TestDecompressor(t *testing.T) {
366 // Keep resetting this reader.
367 // It is intended behavior that Reader.Reset can be called on a zero-value
368 // Reader and be the equivalent as if NewReader was used instead.
369 r1 := new(Reader)
370
371 b := new(bytes.Buffer)
372 for _, tt := range gunzipTests {
373 // Test NewReader.
374 in := bytes.NewReader(tt.gzip)
375 r2, err := NewReader(in)
376 if err != nil {
377 t.Errorf("%s: NewReader: %s", tt.desc, err)
378 continue
379 }
380 defer r2.Close()
381 if tt.name != r2.Name {
382 t.Errorf("%s: got name %s", tt.desc, r2.Name)
383 }
384 b.Reset()
385 n, err := io.Copy(b, r2)
386 if err != tt.err {
387 t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
388 }
389 s := b.String()
390 if s != tt.raw {
391 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
392 }
393
394 // Test Reader.Reset.
395 in = bytes.NewReader(tt.gzip)
396 err = r1.Reset(in)
397 if err != nil {
398 t.Errorf("%s: Reset: %s", tt.desc, err)
399 continue
400 }
401 if tt.name != r1.Name {
402 t.Errorf("%s: got name %s", tt.desc, r1.Name)
403 }
404 b.Reset()
405 n, err = io.Copy(b, r1)
406 if err != tt.err {
407 t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
408 }
409 s = b.String()
410 if s != tt.raw {
411 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
412 }
413 }
414 }
415
416 func TestIssue6550(t *testing.T) {
417 // Apple’s notarization service will recursively attempt to decompress
418 // files in order to find binaries to notarize. Since the service is
419 // unable to decompress this file, it may reject the entire toolchain. Use a
420 // base64-encoded version to avoid this.
421 // See golang.org/issue/34986
422 f, err := os.Open("testdata/issue6550.gz.base64")
423 if err != nil {
424 t.Fatal(err)
425 }
426 gzip, err := NewReader(base64.NewDecoder(base64.StdEncoding, f))
427 if err != nil {
428 t.Fatalf("NewReader(testdata/issue6550.gz): %v", err)
429 }
430 defer gzip.Close()
431 done := make(chan bool, 1)
432 go func() {
433 _, err := io.Copy(ioutil.Discard, gzip)
434 if err == nil {
435 t.Errorf("Copy succeeded")
436 } else {
437 t.Logf("Copy failed (correctly): %v", err)
438 }
439 done <- true
440 }()
441 select {
442 case <-time.After(1 * time.Second):
443 t.Errorf("Copy hung")
444 case <-done:
445 // ok
446 }
447 }
448
449 func TestMultistreamFalse(t *testing.T) {
450 // Find concatenation test.
451 var tt gunzipTest
452 for _, tt = range gunzipTests {
453 if strings.HasSuffix(tt.desc, " x2") {
454 goto Found
455 }
456 }
457 t.Fatal("cannot find hello.txt x2 in gunzip tests")
458
459 Found:
460 br := bytes.NewReader(tt.gzip)
461 var r Reader
462 if err := r.Reset(br); err != nil {
463 t.Fatalf("first reset: %v", err)
464 }
465
466 // Expect two streams with "hello world\n", then real EOF.
467 const hello = "hello world\n"
468
469 r.Multistream(false)
470 data, err := ioutil.ReadAll(&r)
471 if string(data) != hello || err != nil {
472 t.Fatalf("first stream = %q, %v, want %q, %v", string(data), err, hello, nil)
473 }
474
475 if err := r.Reset(br); err != nil {
476 t.Fatalf("second reset: %v", err)
477 }
478 r.Multistream(false)
479 data, err = ioutil.ReadAll(&r)
480 if string(data) != hello || err != nil {
481 t.Fatalf("second stream = %q, %v, want %q, %v", string(data), err, hello, nil)
482 }
483
484 if err := r.Reset(br); err != io.EOF {
485 t.Fatalf("third reset: err=%v, want io.EOF", err)
486 }
487 }
488
489 func TestNilStream(t *testing.T) {
490 // Go liberally interprets RFC 1952 section 2.2 to mean that a gzip file
491 // consist of zero or more members. Thus, we test that a nil stream is okay.
492 _, err := NewReader(bytes.NewReader(nil))
493 if err != io.EOF {
494 t.Fatalf("NewReader(nil) on empty stream: got %v, want io.EOF", err)
495 }
496 }
497
498 func TestTruncatedStreams(t *testing.T) {
499 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"
500
501 // Intentionally iterate starting with at least one byte in the stream.
502 for i := 1; i < len(data)-1; i++ {
503 r, err := NewReader(strings.NewReader(data[:i]))
504 if err != nil {
505 if err != io.ErrUnexpectedEOF {
506 t.Errorf("NewReader(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
507 }
508 continue
509 }
510 _, err = io.Copy(ioutil.Discard, r)
511 if ferr, ok := err.(*flate.ReadError); ok {
512 err = ferr.Err
513 }
514 if err != io.ErrUnexpectedEOF {
515 t.Errorf("io.Copy(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
516 }
517 }
518 }