github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/archive/zip/zip_test.go (about) 1 // Copyright 2011 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 // Tests that involve both reading and writing. 6 7 package zip 8 9 import ( 10 "bytes" 11 "fmt" 12 "hash" 13 "internal/testenv" 14 "io" 15 "io/ioutil" 16 "sort" 17 "strings" 18 "testing" 19 "time" 20 ) 21 22 func TestOver65kFiles(t *testing.T) { 23 if testing.Short() && testenv.Builder() == "" { 24 t.Skip("skipping in short mode") 25 } 26 buf := new(bytes.Buffer) 27 w := NewWriter(buf) 28 const nFiles = (1 << 16) + 42 29 for i := 0; i < nFiles; i++ { 30 _, err := w.CreateHeader(&FileHeader{ 31 Name: fmt.Sprintf("%d.dat", i), 32 Method: Store, // avoid Issue 6136 and Issue 6138 33 }) 34 if err != nil { 35 t.Fatalf("creating file %d: %v", i, err) 36 } 37 } 38 if err := w.Close(); err != nil { 39 t.Fatalf("Writer.Close: %v", err) 40 } 41 s := buf.String() 42 zr, err := NewReader(strings.NewReader(s), int64(len(s))) 43 if err != nil { 44 t.Fatalf("NewReader: %v", err) 45 } 46 if got := len(zr.File); got != nFiles { 47 t.Fatalf("File contains %d files, want %d", got, nFiles) 48 } 49 for i := 0; i < nFiles; i++ { 50 want := fmt.Sprintf("%d.dat", i) 51 if zr.File[i].Name != want { 52 t.Fatalf("File(%d) = %q, want %q", i, zr.File[i].Name, want) 53 } 54 } 55 } 56 57 func TestModTime(t *testing.T) { 58 var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC) 59 fh := new(FileHeader) 60 fh.SetModTime(testTime) 61 outTime := fh.ModTime() 62 if !outTime.Equal(testTime) { 63 t.Errorf("times don't match: got %s, want %s", outTime, testTime) 64 } 65 } 66 67 func testHeaderRoundTrip(fh *FileHeader, wantUncompressedSize uint32, wantUncompressedSize64 uint64, t *testing.T) { 68 fi := fh.FileInfo() 69 fh2, err := FileInfoHeader(fi) 70 if err != nil { 71 t.Fatal(err) 72 } 73 if got, want := fh2.Name, fh.Name; got != want { 74 t.Errorf("Name: got %s, want %s\n", got, want) 75 } 76 if got, want := fh2.UncompressedSize, wantUncompressedSize; got != want { 77 t.Errorf("UncompressedSize: got %d, want %d\n", got, want) 78 } 79 if got, want := fh2.UncompressedSize64, wantUncompressedSize64; got != want { 80 t.Errorf("UncompressedSize64: got %d, want %d\n", got, want) 81 } 82 if got, want := fh2.ModifiedTime, fh.ModifiedTime; got != want { 83 t.Errorf("ModifiedTime: got %d, want %d\n", got, want) 84 } 85 if got, want := fh2.ModifiedDate, fh.ModifiedDate; got != want { 86 t.Errorf("ModifiedDate: got %d, want %d\n", got, want) 87 } 88 89 if sysfh, ok := fi.Sys().(*FileHeader); !ok && sysfh != fh { 90 t.Errorf("Sys didn't return original *FileHeader") 91 } 92 } 93 94 func TestFileHeaderRoundTrip(t *testing.T) { 95 fh := &FileHeader{ 96 Name: "foo.txt", 97 UncompressedSize: 987654321, 98 ModifiedTime: 1234, 99 ModifiedDate: 5678, 100 } 101 testHeaderRoundTrip(fh, fh.UncompressedSize, uint64(fh.UncompressedSize), t) 102 } 103 104 func TestFileHeaderRoundTrip64(t *testing.T) { 105 fh := &FileHeader{ 106 Name: "foo.txt", 107 UncompressedSize64: 9876543210, 108 ModifiedTime: 1234, 109 ModifiedDate: 5678, 110 } 111 testHeaderRoundTrip(fh, uint32max, fh.UncompressedSize64, t) 112 } 113 114 type repeatedByte struct { 115 off int64 116 b byte 117 n int64 118 } 119 120 // rleBuffer is a run-length-encoded byte buffer. 121 // It's an io.Writer (like a bytes.Buffer) and also an io.ReaderAt, 122 // allowing random-access reads. 123 type rleBuffer struct { 124 buf []repeatedByte 125 } 126 127 func (r *rleBuffer) Size() int64 { 128 if len(r.buf) == 0 { 129 return 0 130 } 131 last := &r.buf[len(r.buf)-1] 132 return last.off + last.n 133 } 134 135 func (r *rleBuffer) Write(p []byte) (n int, err error) { 136 var rp *repeatedByte 137 if len(r.buf) > 0 { 138 rp = &r.buf[len(r.buf)-1] 139 // Fast path, if p is entirely the same byte repeated. 140 if lastByte := rp.b; len(p) > 0 && p[0] == lastByte { 141 all := true 142 for _, b := range p { 143 if b != lastByte { 144 all = false 145 break 146 } 147 } 148 if all { 149 rp.n += int64(len(p)) 150 return len(p), nil 151 } 152 } 153 } 154 155 for _, b := range p { 156 if rp == nil || rp.b != b { 157 r.buf = append(r.buf, repeatedByte{r.Size(), b, 1}) 158 rp = &r.buf[len(r.buf)-1] 159 } else { 160 rp.n++ 161 } 162 } 163 return len(p), nil 164 } 165 166 func (r *rleBuffer) ReadAt(p []byte, off int64) (n int, err error) { 167 if len(p) == 0 { 168 return 169 } 170 skipParts := sort.Search(len(r.buf), func(i int) bool { 171 part := &r.buf[i] 172 return part.off+part.n > off 173 }) 174 parts := r.buf[skipParts:] 175 if len(parts) > 0 { 176 skipBytes := off - parts[0].off 177 for len(parts) > 0 { 178 part := parts[0] 179 for i := skipBytes; i < part.n; i++ { 180 if n == len(p) { 181 return 182 } 183 p[n] = part.b 184 n++ 185 } 186 parts = parts[1:] 187 skipBytes = 0 188 } 189 } 190 if n != len(p) { 191 err = io.ErrUnexpectedEOF 192 } 193 return 194 } 195 196 // Just testing the rleBuffer used in the Zip64 test above. Not used by the zip code. 197 func TestRLEBuffer(t *testing.T) { 198 b := new(rleBuffer) 199 var all []byte 200 writes := []string{"abcdeee", "eeeeeee", "eeeefghaaiii"} 201 for _, w := range writes { 202 b.Write([]byte(w)) 203 all = append(all, w...) 204 } 205 if len(b.buf) != 10 { 206 t.Fatalf("len(b.buf) = %d; want 10", len(b.buf)) 207 } 208 209 for i := 0; i < len(all); i++ { 210 for j := 0; j < len(all)-i; j++ { 211 buf := make([]byte, j) 212 n, err := b.ReadAt(buf, int64(i)) 213 if err != nil || n != len(buf) { 214 t.Errorf("ReadAt(%d, %d) = %d, %v; want %d, nil", i, j, n, err, len(buf)) 215 } 216 if !bytes.Equal(buf, all[i:i+j]) { 217 t.Errorf("ReadAt(%d, %d) = %q; want %q", i, j, buf, all[i:i+j]) 218 } 219 } 220 } 221 } 222 223 // fakeHash32 is a dummy Hash32 that always returns 0. 224 type fakeHash32 struct { 225 hash.Hash32 226 } 227 228 func (fakeHash32) Write(p []byte) (int, error) { return len(p), nil } 229 func (fakeHash32) Sum32() uint32 { return 0 } 230 231 func TestZip64(t *testing.T) { 232 if testing.Short() { 233 t.Skip("slow test; skipping") 234 } 235 const size = 1 << 32 // before the "END\n" part 236 buf := testZip64(t, size) 237 testZip64DirectoryRecordLength(buf, t) 238 } 239 240 func TestZip64EdgeCase(t *testing.T) { 241 if testing.Short() { 242 t.Skip("slow test; skipping") 243 } 244 // Test a zip file with uncompressed size 0xFFFFFFFF. 245 // That's the magic marker for a 64-bit file, so even though 246 // it fits in a 32-bit field we must use the 64-bit field. 247 // Go 1.5 and earlier got this wrong, 248 // writing an invalid zip file. 249 const size = 1<<32 - 1 - int64(len("END\n")) // before the "END\n" part 250 buf := testZip64(t, size) 251 testZip64DirectoryRecordLength(buf, t) 252 } 253 254 func testZip64(t testing.TB, size int64) *rleBuffer { 255 const chunkSize = 1024 256 chunks := int(size / chunkSize) 257 // write size bytes plus "END\n" to a zip file 258 buf := new(rleBuffer) 259 w := NewWriter(buf) 260 f, err := w.CreateHeader(&FileHeader{ 261 Name: "huge.txt", 262 Method: Store, 263 }) 264 if err != nil { 265 t.Fatal(err) 266 } 267 f.(*fileWriter).crc32 = fakeHash32{} 268 chunk := make([]byte, chunkSize) 269 for i := range chunk { 270 chunk[i] = '.' 271 } 272 for i := 0; i < chunks; i++ { 273 _, err := f.Write(chunk) 274 if err != nil { 275 t.Fatal("write chunk:", err) 276 } 277 } 278 if frag := int(size % chunkSize); frag > 0 { 279 _, err := f.Write(chunk[:frag]) 280 if err != nil { 281 t.Fatal("write chunk:", err) 282 } 283 } 284 end := []byte("END\n") 285 _, err = f.Write(end) 286 if err != nil { 287 t.Fatal("write end:", err) 288 } 289 if err := w.Close(); err != nil { 290 t.Fatal(err) 291 } 292 293 // read back zip file and check that we get to the end of it 294 r, err := NewReader(buf, int64(buf.Size())) 295 if err != nil { 296 t.Fatal("reader:", err) 297 } 298 f0 := r.File[0] 299 rc, err := f0.Open() 300 if err != nil { 301 t.Fatal("opening:", err) 302 } 303 rc.(*checksumReader).hash = fakeHash32{} 304 for i := 0; i < chunks; i++ { 305 _, err := io.ReadFull(rc, chunk) 306 if err != nil { 307 t.Fatal("read:", err) 308 } 309 } 310 if frag := int(size % chunkSize); frag > 0 { 311 _, err := io.ReadFull(rc, chunk[:frag]) 312 if err != nil { 313 t.Fatal("read:", err) 314 } 315 } 316 gotEnd, err := ioutil.ReadAll(rc) 317 if err != nil { 318 t.Fatal("read end:", err) 319 } 320 if !bytes.Equal(gotEnd, end) { 321 t.Errorf("End of zip64 archive %q, want %q", gotEnd, end) 322 } 323 err = rc.Close() 324 if err != nil { 325 t.Fatal("closing:", err) 326 } 327 if size+int64(len("END\n")) >= 1<<32-1 { 328 if got, want := f0.UncompressedSize, uint32(uint32max); got != want { 329 t.Errorf("UncompressedSize %#x, want %#x", got, want) 330 } 331 } 332 333 if got, want := f0.UncompressedSize64, uint64(size)+uint64(len(end)); got != want { 334 t.Errorf("UncompressedSize64 %#x, want %#x", got, want) 335 } 336 337 return buf 338 } 339 340 // Issue 9857 341 func testZip64DirectoryRecordLength(buf *rleBuffer, t *testing.T) { 342 d := make([]byte, 1024) 343 if _, err := buf.ReadAt(d, buf.Size()-int64(len(d))); err != nil { 344 t.Fatal("read:", err) 345 } 346 347 sigOff := findSignatureInBlock(d) 348 dirOff, err := findDirectory64End(buf, buf.Size()-int64(len(d))+int64(sigOff)) 349 if err != nil { 350 t.Fatal("findDirectory64End:", err) 351 } 352 353 d = make([]byte, directory64EndLen) 354 if _, err := buf.ReadAt(d, dirOff); err != nil { 355 t.Fatal("read:", err) 356 } 357 358 b := readBuf(d) 359 if sig := b.uint32(); sig != directory64EndSignature { 360 t.Fatalf("Expected directory64EndSignature (%d), got %d", directory64EndSignature, sig) 361 } 362 363 size := b.uint64() 364 if size != directory64EndLen-12 { 365 t.Fatalf("Expected length of %d, got %d", directory64EndLen-12, size) 366 } 367 } 368 369 func testValidHeader(h *FileHeader, t *testing.T) { 370 var buf bytes.Buffer 371 z := NewWriter(&buf) 372 373 f, err := z.CreateHeader(h) 374 if err != nil { 375 t.Fatalf("error creating header: %v", err) 376 } 377 if _, err := f.Write([]byte("hi")); err != nil { 378 t.Fatalf("error writing content: %v", err) 379 } 380 if err := z.Close(); err != nil { 381 t.Fatalf("error closing zip writer: %v", err) 382 } 383 384 b := buf.Bytes() 385 zf, err := NewReader(bytes.NewReader(b), int64(len(b))) 386 if err != nil { 387 t.Fatalf("got %v, expected nil", err) 388 } 389 zh := zf.File[0].FileHeader 390 if zh.Name != h.Name || zh.Method != h.Method || zh.UncompressedSize64 != uint64(len("hi")) { 391 t.Fatalf("got %q/%d/%d expected %q/%d/%d", zh.Name, zh.Method, zh.UncompressedSize64, h.Name, h.Method, len("hi")) 392 } 393 } 394 395 // Issue 4302. 396 func TestHeaderInvalidTagAndSize(t *testing.T) { 397 const timeFormat = "20060102T150405.000.txt" 398 399 ts := time.Now() 400 filename := ts.Format(timeFormat) 401 402 h := FileHeader{ 403 Name: filename, 404 Method: Deflate, 405 Extra: []byte(ts.Format(time.RFC3339Nano)), // missing tag and len, but Extra is best-effort parsing 406 } 407 h.SetModTime(ts) 408 409 testValidHeader(&h, t) 410 } 411 412 func TestHeaderTooShort(t *testing.T) { 413 h := FileHeader{ 414 Name: "foo.txt", 415 Method: Deflate, 416 Extra: []byte{zip64ExtraId}, // missing size and second half of tag, but Extra is best-effort parsing 417 } 418 testValidHeader(&h, t) 419 } 420 421 func TestHeaderIgnoredSize(t *testing.T) { 422 h := FileHeader{ 423 Name: "foo.txt", 424 Method: Deflate, 425 Extra: []byte{zip64ExtraId & 0xFF, zip64ExtraId >> 8, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}, // bad size but shouldn't be consulted 426 } 427 testValidHeader(&h, t) 428 } 429 430 // Issue 4393. It is valid to have an extra data header 431 // which contains no body. 432 func TestZeroLengthHeader(t *testing.T) { 433 h := FileHeader{ 434 Name: "extadata.txt", 435 Method: Deflate, 436 Extra: []byte{ 437 85, 84, 5, 0, 3, 154, 144, 195, 77, // tag 21589 size 5 438 85, 120, 0, 0, // tag 30805 size 0 439 }, 440 } 441 testValidHeader(&h, t) 442 } 443 444 // Just benchmarking how fast the Zip64 test above is. Not related to 445 // our zip performance, since the test above disabled CRC32 and flate. 446 func BenchmarkZip64Test(b *testing.B) { 447 for i := 0; i < b.N; i++ { 448 testZip64(b, 1<<26) 449 } 450 }