github.com/akerouanton/docker@v1.11.0-rc3/pkg/tarsum/tarsum_test.go (about) 1 package tarsum 2 3 import ( 4 "archive/tar" 5 "bytes" 6 "compress/gzip" 7 "crypto/md5" 8 "crypto/rand" 9 "crypto/sha1" 10 "crypto/sha256" 11 "crypto/sha512" 12 "encoding/hex" 13 "fmt" 14 "io" 15 "io/ioutil" 16 "os" 17 "strings" 18 "testing" 19 ) 20 21 type testLayer struct { 22 filename string 23 options *sizedOptions 24 jsonfile string 25 gzip bool 26 tarsum string 27 version Version 28 hash THash 29 } 30 31 var testLayers = []testLayer{ 32 { 33 filename: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar", 34 jsonfile: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/json", 35 version: Version0, 36 tarsum: "tarsum+sha256:4095cc12fa5fdb1ab2760377e1cd0c4ecdd3e61b4f9b82319d96fcea6c9a41c6"}, 37 { 38 filename: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar", 39 jsonfile: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/json", 40 version: VersionDev, 41 tarsum: "tarsum.dev+sha256:db56e35eec6ce65ba1588c20ba6b1ea23743b59e81fb6b7f358ccbde5580345c"}, 42 { 43 filename: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar", 44 jsonfile: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/json", 45 gzip: true, 46 tarsum: "tarsum+sha256:4095cc12fa5fdb1ab2760377e1cd0c4ecdd3e61b4f9b82319d96fcea6c9a41c6"}, 47 { 48 // Tests existing version of TarSum when xattrs are present 49 filename: "testdata/xattr/layer.tar", 50 jsonfile: "testdata/xattr/json", 51 version: Version0, 52 tarsum: "tarsum+sha256:07e304a8dbcb215b37649fde1a699f8aeea47e60815707f1cdf4d55d25ff6ab4"}, 53 { 54 // Tests next version of TarSum when xattrs are present 55 filename: "testdata/xattr/layer.tar", 56 jsonfile: "testdata/xattr/json", 57 version: VersionDev, 58 tarsum: "tarsum.dev+sha256:6c58917892d77b3b357b0f9ad1e28e1f4ae4de3a8006bd3beb8beda214d8fd16"}, 59 { 60 filename: "testdata/511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158/layer.tar", 61 jsonfile: "testdata/511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158/json", 62 tarsum: "tarsum+sha256:c66bd5ec9f87b8f4c6135ca37684618f486a3dd1d113b138d0a177bfa39c2571"}, 63 { 64 options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory) 65 tarsum: "tarsum+sha256:8bf12d7e67c51ee2e8306cba569398b1b9f419969521a12ffb9d8875e8836738"}, 66 { 67 // this tar has two files with the same path 68 filename: "testdata/collision/collision-0.tar", 69 tarsum: "tarsum+sha256:08653904a68d3ab5c59e65ef58c49c1581caa3c34744f8d354b3f575ea04424a"}, 70 { 71 // this tar has the same two files (with the same path), but reversed order. ensuring is has different hash than above 72 filename: "testdata/collision/collision-1.tar", 73 tarsum: "tarsum+sha256:b51c13fbefe158b5ce420d2b930eef54c5cd55c50a2ee4abdddea8fa9f081e0d"}, 74 { 75 // this tar has newer of collider-0.tar, ensuring is has different hash 76 filename: "testdata/collision/collision-2.tar", 77 tarsum: "tarsum+sha256:381547080919bb82691e995508ae20ed33ce0f6948d41cafbeb70ce20c73ee8e"}, 78 { 79 // this tar has newer of collider-1.tar, ensuring is has different hash 80 filename: "testdata/collision/collision-3.tar", 81 tarsum: "tarsum+sha256:f886e431c08143164a676805205979cd8fa535dfcef714db5515650eea5a7c0f"}, 82 { 83 options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory) 84 tarsum: "tarsum+md5:0d7529ec7a8360155b48134b8e599f53", 85 hash: md5THash, 86 }, 87 { 88 options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory) 89 tarsum: "tarsum+sha1:f1fee39c5925807ff75ef1925e7a23be444ba4df", 90 hash: sha1Hash, 91 }, 92 { 93 options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory) 94 tarsum: "tarsum+sha224:6319390c0b061d639085d8748b14cd55f697cf9313805218b21cf61c", 95 hash: sha224Hash, 96 }, 97 { 98 options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory) 99 tarsum: "tarsum+sha384:a578ce3ce29a2ae03b8ed7c26f47d0f75b4fc849557c62454be4b5ffd66ba021e713b48ce71e947b43aab57afd5a7636", 100 hash: sha384Hash, 101 }, 102 { 103 options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory) 104 tarsum: "tarsum+sha512:e9bfb90ca5a4dfc93c46ee061a5cf9837de6d2fdf82544d6460d3147290aecfabf7b5e415b9b6e72db9b8941f149d5d69fb17a394cbfaf2eac523bd9eae21855", 105 hash: sha512Hash, 106 }, 107 } 108 109 type sizedOptions struct { 110 num int64 111 size int64 112 isRand bool 113 realFile bool 114 } 115 116 // make a tar: 117 // * num is the number of files the tar should have 118 // * size is the bytes per file 119 // * isRand is whether the contents of the files should be a random chunk (otherwise it's all zeros) 120 // * realFile will write to a TempFile, instead of an in memory buffer 121 func sizedTar(opts sizedOptions) io.Reader { 122 var ( 123 fh io.ReadWriter 124 err error 125 ) 126 if opts.realFile { 127 fh, err = ioutil.TempFile("", "tarsum") 128 if err != nil { 129 return nil 130 } 131 } else { 132 fh = bytes.NewBuffer([]byte{}) 133 } 134 tarW := tar.NewWriter(fh) 135 defer tarW.Close() 136 for i := int64(0); i < opts.num; i++ { 137 err := tarW.WriteHeader(&tar.Header{ 138 Name: fmt.Sprintf("/testdata%d", i), 139 Mode: 0755, 140 Uid: 0, 141 Gid: 0, 142 Size: opts.size, 143 }) 144 if err != nil { 145 return nil 146 } 147 var rBuf []byte 148 if opts.isRand { 149 rBuf = make([]byte, 8) 150 _, err = rand.Read(rBuf) 151 if err != nil { 152 return nil 153 } 154 } else { 155 rBuf = []byte{0, 0, 0, 0, 0, 0, 0, 0} 156 } 157 158 for i := int64(0); i < opts.size/int64(8); i++ { 159 tarW.Write(rBuf) 160 } 161 } 162 return fh 163 } 164 165 func emptyTarSum(gzip bool) (TarSum, error) { 166 reader, writer := io.Pipe() 167 tarWriter := tar.NewWriter(writer) 168 169 // Immediately close tarWriter and write-end of the 170 // Pipe in a separate goroutine so we don't block. 171 go func() { 172 tarWriter.Close() 173 writer.Close() 174 }() 175 176 return NewTarSum(reader, !gzip, Version0) 177 } 178 179 // Test errors on NewTarsumForLabel 180 func TestNewTarSumForLabelInvalid(t *testing.T) { 181 reader := strings.NewReader("") 182 183 if _, err := NewTarSumForLabel(reader, true, "invalidlabel"); err == nil { 184 t.Fatalf("Expected an error, got nothing.") 185 } 186 187 if _, err := NewTarSumForLabel(reader, true, "invalid+sha256"); err == nil { 188 t.Fatalf("Expected an error, got nothing.") 189 } 190 if _, err := NewTarSumForLabel(reader, true, "tarsum.v1+invalid"); err == nil { 191 t.Fatalf("Expected an error, got nothing.") 192 } 193 } 194 195 func TestNewTarSumForLabel(t *testing.T) { 196 197 layer := testLayers[0] 198 199 reader, err := os.Open(layer.filename) 200 if err != nil { 201 t.Fatal(err) 202 } 203 label := strings.Split(layer.tarsum, ":")[0] 204 ts, err := NewTarSumForLabel(reader, false, label) 205 if err != nil { 206 t.Fatal(err) 207 } 208 209 // Make sure it actually worked by reading a little bit of it 210 nbByteToRead := 8 * 1024 211 dBuf := make([]byte, nbByteToRead) 212 _, err = ts.Read(dBuf) 213 if err != nil { 214 t.Errorf("failed to read %vKB from %s: %s", nbByteToRead, layer.filename, err) 215 } 216 } 217 218 // TestEmptyTar tests that tarsum does not fail to read an empty tar 219 // and correctly returns the hex digest of an empty hash. 220 func TestEmptyTar(t *testing.T) { 221 // Test without gzip. 222 ts, err := emptyTarSum(false) 223 if err != nil { 224 t.Fatal(err) 225 } 226 227 zeroBlock := make([]byte, 1024) 228 buf := new(bytes.Buffer) 229 230 n, err := io.Copy(buf, ts) 231 if err != nil { 232 t.Fatal(err) 233 } 234 235 if n != int64(len(zeroBlock)) || !bytes.Equal(buf.Bytes(), zeroBlock) { 236 t.Fatalf("tarSum did not write the correct number of zeroed bytes: %d", n) 237 } 238 239 expectedSum := ts.Version().String() + "+sha256:" + hex.EncodeToString(sha256.New().Sum(nil)) 240 resultSum := ts.Sum(nil) 241 242 if resultSum != expectedSum { 243 t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum) 244 } 245 246 // Test with gzip. 247 ts, err = emptyTarSum(true) 248 if err != nil { 249 t.Fatal(err) 250 } 251 buf.Reset() 252 253 n, err = io.Copy(buf, ts) 254 if err != nil { 255 t.Fatal(err) 256 } 257 258 bufgz := new(bytes.Buffer) 259 gz := gzip.NewWriter(bufgz) 260 n, err = io.Copy(gz, bytes.NewBuffer(zeroBlock)) 261 gz.Close() 262 gzBytes := bufgz.Bytes() 263 264 if n != int64(len(zeroBlock)) || !bytes.Equal(buf.Bytes(), gzBytes) { 265 t.Fatalf("tarSum did not write the correct number of gzipped-zeroed bytes: %d", n) 266 } 267 268 resultSum = ts.Sum(nil) 269 270 if resultSum != expectedSum { 271 t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum) 272 } 273 274 // Test without ever actually writing anything. 275 if ts, err = NewTarSum(bytes.NewReader([]byte{}), true, Version0); err != nil { 276 t.Fatal(err) 277 } 278 279 resultSum = ts.Sum(nil) 280 281 if resultSum != expectedSum { 282 t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum) 283 } 284 } 285 286 var ( 287 md5THash = NewTHash("md5", md5.New) 288 sha1Hash = NewTHash("sha1", sha1.New) 289 sha224Hash = NewTHash("sha224", sha256.New224) 290 sha384Hash = NewTHash("sha384", sha512.New384) 291 sha512Hash = NewTHash("sha512", sha512.New) 292 ) 293 294 // Test all the build-in read size : buf8K, buf16K, buf32K and more 295 func TestTarSumsReadSize(t *testing.T) { 296 // Test always on the same layer (that is big enough) 297 layer := testLayers[0] 298 299 for i := 0; i < 5; i++ { 300 301 reader, err := os.Open(layer.filename) 302 if err != nil { 303 t.Fatal(err) 304 } 305 ts, err := NewTarSum(reader, false, layer.version) 306 if err != nil { 307 t.Fatal(err) 308 } 309 310 // Read and discard bytes so that it populates sums 311 nbByteToRead := (i + 1) * 8 * 1024 312 dBuf := make([]byte, nbByteToRead) 313 _, err = ts.Read(dBuf) 314 if err != nil { 315 t.Errorf("failed to read %vKB from %s: %s", nbByteToRead, layer.filename, err) 316 continue 317 } 318 } 319 } 320 321 func TestTarSums(t *testing.T) { 322 for _, layer := range testLayers { 323 var ( 324 fh io.Reader 325 err error 326 ) 327 if len(layer.filename) > 0 { 328 fh, err = os.Open(layer.filename) 329 if err != nil { 330 t.Errorf("failed to open %s: %s", layer.filename, err) 331 continue 332 } 333 } else if layer.options != nil { 334 fh = sizedTar(*layer.options) 335 } else { 336 // What else is there to test? 337 t.Errorf("what to do with %#v", layer) 338 continue 339 } 340 if file, ok := fh.(*os.File); ok { 341 defer file.Close() 342 } 343 344 var ts TarSum 345 if layer.hash == nil { 346 // double negatives! 347 ts, err = NewTarSum(fh, !layer.gzip, layer.version) 348 } else { 349 ts, err = NewTarSumHash(fh, !layer.gzip, layer.version, layer.hash) 350 } 351 if err != nil { 352 t.Errorf("%q :: %q", err, layer.filename) 353 continue 354 } 355 356 // Read variable number of bytes to test dynamic buffer 357 dBuf := make([]byte, 1) 358 _, err = ts.Read(dBuf) 359 if err != nil { 360 t.Errorf("failed to read 1B from %s: %s", layer.filename, err) 361 continue 362 } 363 dBuf = make([]byte, 16*1024) 364 _, err = ts.Read(dBuf) 365 if err != nil { 366 t.Errorf("failed to read 16KB from %s: %s", layer.filename, err) 367 continue 368 } 369 370 // Read and discard remaining bytes 371 _, err = io.Copy(ioutil.Discard, ts) 372 if err != nil { 373 t.Errorf("failed to copy from %s: %s", layer.filename, err) 374 continue 375 } 376 var gotSum string 377 if len(layer.jsonfile) > 0 { 378 jfh, err := os.Open(layer.jsonfile) 379 if err != nil { 380 t.Errorf("failed to open %s: %s", layer.jsonfile, err) 381 continue 382 } 383 buf, err := ioutil.ReadAll(jfh) 384 if err != nil { 385 t.Errorf("failed to readAll %s: %s", layer.jsonfile, err) 386 continue 387 } 388 gotSum = ts.Sum(buf) 389 } else { 390 gotSum = ts.Sum(nil) 391 } 392 393 if layer.tarsum != gotSum { 394 t.Errorf("expecting [%s], but got [%s]", layer.tarsum, gotSum) 395 } 396 var expectedHashName string 397 if layer.hash != nil { 398 expectedHashName = layer.hash.Name() 399 } else { 400 expectedHashName = DefaultTHash.Name() 401 } 402 if expectedHashName != ts.Hash().Name() { 403 t.Errorf("expecting hash [%v], but got [%s]", expectedHashName, ts.Hash().Name()) 404 } 405 } 406 } 407 408 func TestIteration(t *testing.T) { 409 headerTests := []struct { 410 expectedSum string // TODO(vbatts) it would be nice to get individual sums of each 411 version Version 412 hdr *tar.Header 413 data []byte 414 }{ 415 { 416 "tarsum+sha256:626c4a2e9a467d65c33ae81f7f3dedd4de8ccaee72af73223c4bc4718cbc7bbd", 417 Version0, 418 &tar.Header{ 419 Name: "file.txt", 420 Size: 0, 421 Typeflag: tar.TypeReg, 422 Devminor: 0, 423 Devmajor: 0, 424 }, 425 []byte(""), 426 }, 427 { 428 "tarsum.dev+sha256:6ffd43a1573a9913325b4918e124ee982a99c0f3cba90fc032a65f5e20bdd465", 429 VersionDev, 430 &tar.Header{ 431 Name: "file.txt", 432 Size: 0, 433 Typeflag: tar.TypeReg, 434 Devminor: 0, 435 Devmajor: 0, 436 }, 437 []byte(""), 438 }, 439 { 440 "tarsum.dev+sha256:b38166c059e11fb77bef30bf16fba7584446e80fcc156ff46d47e36c5305d8ef", 441 VersionDev, 442 &tar.Header{ 443 Name: "another.txt", 444 Uid: 1000, 445 Gid: 1000, 446 Uname: "slartibartfast", 447 Gname: "users", 448 Size: 4, 449 Typeflag: tar.TypeReg, 450 Devminor: 0, 451 Devmajor: 0, 452 }, 453 []byte("test"), 454 }, 455 { 456 "tarsum.dev+sha256:4cc2e71ac5d31833ab2be9b4f7842a14ce595ec96a37af4ed08f87bc374228cd", 457 VersionDev, 458 &tar.Header{ 459 Name: "xattrs.txt", 460 Uid: 1000, 461 Gid: 1000, 462 Uname: "slartibartfast", 463 Gname: "users", 464 Size: 4, 465 Typeflag: tar.TypeReg, 466 Xattrs: map[string]string{ 467 "user.key1": "value1", 468 "user.key2": "value2", 469 }, 470 }, 471 []byte("test"), 472 }, 473 { 474 "tarsum.dev+sha256:65f4284fa32c0d4112dd93c3637697805866415b570587e4fd266af241503760", 475 VersionDev, 476 &tar.Header{ 477 Name: "xattrs.txt", 478 Uid: 1000, 479 Gid: 1000, 480 Uname: "slartibartfast", 481 Gname: "users", 482 Size: 4, 483 Typeflag: tar.TypeReg, 484 Xattrs: map[string]string{ 485 "user.KEY1": "value1", // adding different case to ensure different sum 486 "user.key2": "value2", 487 }, 488 }, 489 []byte("test"), 490 }, 491 { 492 "tarsum+sha256:c12bb6f1303a9ddbf4576c52da74973c00d14c109bcfa76b708d5da1154a07fa", 493 Version0, 494 &tar.Header{ 495 Name: "xattrs.txt", 496 Uid: 1000, 497 Gid: 1000, 498 Uname: "slartibartfast", 499 Gname: "users", 500 Size: 4, 501 Typeflag: tar.TypeReg, 502 Xattrs: map[string]string{ 503 "user.NOT": "CALCULATED", 504 }, 505 }, 506 []byte("test"), 507 }, 508 } 509 for _, htest := range headerTests { 510 s, err := renderSumForHeader(htest.version, htest.hdr, htest.data) 511 if err != nil { 512 t.Fatal(err) 513 } 514 515 if s != htest.expectedSum { 516 t.Errorf("expected sum: %q, got: %q", htest.expectedSum, s) 517 } 518 } 519 520 } 521 522 func renderSumForHeader(v Version, h *tar.Header, data []byte) (string, error) { 523 buf := bytes.NewBuffer(nil) 524 // first build our test tar 525 tw := tar.NewWriter(buf) 526 if err := tw.WriteHeader(h); err != nil { 527 return "", err 528 } 529 if _, err := tw.Write(data); err != nil { 530 return "", err 531 } 532 tw.Close() 533 534 ts, err := NewTarSum(buf, true, v) 535 if err != nil { 536 return "", err 537 } 538 tr := tar.NewReader(ts) 539 for { 540 hdr, err := tr.Next() 541 if hdr == nil || err == io.EOF { 542 // Signals the end of the archive. 543 break 544 } 545 if err != nil { 546 return "", err 547 } 548 if _, err = io.Copy(ioutil.Discard, tr); err != nil { 549 return "", err 550 } 551 } 552 return ts.Sum(nil), nil 553 } 554 555 func Benchmark9kTar(b *testing.B) { 556 buf := bytes.NewBuffer([]byte{}) 557 fh, err := os.Open("testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar") 558 if err != nil { 559 b.Error(err) 560 return 561 } 562 n, err := io.Copy(buf, fh) 563 if err != nil { 564 b.Error(err) 565 return 566 } 567 fh.Close() 568 569 reader := bytes.NewReader(buf.Bytes()) 570 571 b.SetBytes(n) 572 b.ResetTimer() 573 for i := 0; i < b.N; i++ { 574 reader.Seek(0, 0) 575 ts, err := NewTarSum(reader, true, Version0) 576 if err != nil { 577 b.Error(err) 578 return 579 } 580 io.Copy(ioutil.Discard, ts) 581 ts.Sum(nil) 582 } 583 } 584 585 func Benchmark9kTarGzip(b *testing.B) { 586 buf := bytes.NewBuffer([]byte{}) 587 fh, err := os.Open("testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar") 588 if err != nil { 589 b.Error(err) 590 return 591 } 592 n, err := io.Copy(buf, fh) 593 if err != nil { 594 b.Error(err) 595 return 596 } 597 fh.Close() 598 599 reader := bytes.NewReader(buf.Bytes()) 600 601 b.SetBytes(n) 602 b.ResetTimer() 603 for i := 0; i < b.N; i++ { 604 reader.Seek(0, 0) 605 ts, err := NewTarSum(reader, false, Version0) 606 if err != nil { 607 b.Error(err) 608 return 609 } 610 io.Copy(ioutil.Discard, ts) 611 ts.Sum(nil) 612 } 613 } 614 615 // this is a single big file in the tar archive 616 func Benchmark1mbSingleFileTar(b *testing.B) { 617 benchmarkTar(b, sizedOptions{1, 1024 * 1024, true, true}, false) 618 } 619 620 // this is a single big file in the tar archive 621 func Benchmark1mbSingleFileTarGzip(b *testing.B) { 622 benchmarkTar(b, sizedOptions{1, 1024 * 1024, true, true}, true) 623 } 624 625 // this is 1024 1k files in the tar archive 626 func Benchmark1kFilesTar(b *testing.B) { 627 benchmarkTar(b, sizedOptions{1024, 1024, true, true}, false) 628 } 629 630 // this is 1024 1k files in the tar archive 631 func Benchmark1kFilesTarGzip(b *testing.B) { 632 benchmarkTar(b, sizedOptions{1024, 1024, true, true}, true) 633 } 634 635 func benchmarkTar(b *testing.B, opts sizedOptions, isGzip bool) { 636 var fh *os.File 637 tarReader := sizedTar(opts) 638 if br, ok := tarReader.(*os.File); ok { 639 fh = br 640 } 641 defer os.Remove(fh.Name()) 642 defer fh.Close() 643 644 b.SetBytes(opts.size * opts.num) 645 b.ResetTimer() 646 for i := 0; i < b.N; i++ { 647 ts, err := NewTarSum(fh, !isGzip, Version0) 648 if err != nil { 649 b.Error(err) 650 return 651 } 652 io.Copy(ioutil.Discard, ts) 653 ts.Sum(nil) 654 fh.Seek(0, 0) 655 } 656 }