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.
23 func bytediff(a, b []byte) string {
30 sa := strings.Split(strings.TrimSpace(hex.Dump(a)), "\n")
31 sb := strings.Split(strings.TrimSpace(hex.Dump(b)), "\n")
32 for len(sa) > 0 && len(sb) > 0 {
34 ss = append(ss, identity+sa[0])
36 ss = append(ss, uniqueA+sa[0])
37 ss = append(ss, uniqueB+sb[0])
39 sa, sb = sa[1:], sb[1:]
42 ss = append(ss, uniqueA+sa[0])
46 ss = append(ss, uniqueB+sb[0])
49 return strings.Join(ss, "\n")
52 func TestWriter(t *testing.T) {
54 testHeader struct { // WriteHeader(hdr) == wantErr
58 testWrite struct { // Write(str) == (wantCnt, wantErr)
63 testReadFrom struct { // ReadFrom(testFile{ops}) == (wantCnt, wantErr)
68 testClose struct { // Close() == wantErr
71 testFnc interface{} // testHeader | testWrite | testReadFrom | testClose
75 file string // Optional filename of expected output
78 // The writer test file was produced with this command:
80 // ln -s small.txt link.txt
81 // tar -b 1 --format=ustar -c -f writer.tar small.txt small2.txt link.txt
82 file: "testdata/writer.tar",
93 ModTime: time.Unix(1246508266, 0),
95 testWrite{"Kilts", 5, nil},
106 ModTime: time.Unix(1245217492, 0),
108 testWrite{"Google.com\n", 11, nil},
111 Typeflag: TypeSymlink,
113 Linkname: "small.txt",
119 ModTime: time.Unix(1314603082, 0),
121 testWrite{"", 0, nil},
126 // The truncated test file was produced using these commands:
127 // dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt
128 // tar -b 1 -c -f- /tmp/16gig.txt | dd bs=512 count=8 > writer-big.tar
129 file: "testdata/writer-big.tar",
133 Name: "tmp/16gig.txt",
140 ModTime: time.Unix(1254699560, 0),
145 // This truncated file was produced using this library.
146 // It was verified to work with GNU tar 1.27.1 and BSD tar 3.1.2.
147 // dd if=/dev/zero bs=1G count=16 >> writer-big-long.tar
148 // gnutar -xvf writer-big-long.tar
149 // bsdtar -xvf writer-big-long.tar
151 // This file is in PAX format.
152 file: "testdata/writer-big-long.tar",
156 Name: strings.Repeat("longname/", 15) + "16gig.txt",
163 ModTime: time.Unix(1399583047, 0),
167 // This file was produced using GNU tar v1.17.
168 // gnutar -b 4 --format=ustar (longname/)*15 + file.txt
169 file: "testdata/ustar.tar",
173 Name: strings.Repeat("longname/", 15) + "file.txt",
180 ModTime: time.Unix(1360135598, 0),
182 testWrite{"hello\n", 6, nil},
186 // This file was produced using GNU tar v1.26:
187 // echo "Slartibartfast" > file.txt
188 // ln file.txt hard.txt
189 // tar -b 1 --format=ustar -c -f hardlink.tar file.txt hard.txt
190 file: "testdata/hardlink.tar",
201 ModTime: time.Unix(1425484303, 0),
203 testWrite{"Slartibartfast\n", 15, nil},
208 Linkname: "file.txt",
214 ModTime: time.Unix(1425484303, 0),
216 testWrite{"", 0, nil},
224 Name: "bad-null.txt",
225 Xattrs: map[string]string{"null\x00null\x00": "fizzbuzz"},
232 Name: "null\x00.txt",
236 file: "testdata/pax-records.tar",
241 Uname: strings.Repeat("long", 10),
242 PAXRecords: map[string]string{
243 "path": "FILE", // Should be ignored
244 "GNU.sparse.map": "0,0", // Should be ignored
245 "comment": "Hello, 世界",
252 // Craft a theoretically valid PAX archive with global headers.
253 // The GNU and BSD tar tools do not parse these the same way.
255 // BSD tar v3.1.2 parses and ignores all global headers;
256 // the behavior is verified by researching the source code.
258 // $ bsdtar -tvf pax-global-records.tar
259 // ---------- 0 0 0 0 Dec 31 1969 file1
260 // ---------- 0 0 0 0 Dec 31 1969 file2
261 // ---------- 0 0 0 0 Dec 31 1969 file3
262 // ---------- 0 0 0 0 May 13 2014 file4
264 // GNU tar v1.27.1 applies global headers to subsequent records,
265 // but does not do the following properly:
266 // * It does not treat an empty record as deletion.
267 // * It does not use subsequent global headers to update previous ones.
269 // $ gnutar -tvf pax-global-records.tar
270 // ---------- 0/0 0 2017-07-13 19:40 global1
271 // ---------- 0/0 0 2017-07-13 19:40 file2
272 // gnutar: Substituting `.' for empty member name
273 // ---------- 0/0 0 1969-12-31 16:00
274 // gnutar: Substituting `.' for empty member name
275 // ---------- 0/0 0 2014-05-13 09:53
277 // According to the PAX specification, this should have been the result:
278 // ---------- 0/0 0 2017-07-13 19:40 global1
279 // ---------- 0/0 0 2017-07-13 19:40 file2
280 // ---------- 0/0 0 2017-07-13 19:40 file3
281 // ---------- 0/0 0 2014-05-13 09:53 file4
282 file: "testdata/pax-global-records.tar",
285 Typeflag: TypeXGlobalHeader,
286 PAXRecords: map[string]string{"path": "global1", "mtime": "1500000000.0"},
289 Typeflag: TypeReg, Name: "file1",
294 PAXRecords: map[string]string{"path": "file2"},
297 Typeflag: TypeXGlobalHeader,
298 PAXRecords: map[string]string{"path": ""}, // Should delete "path", but keep "mtime"
301 Typeflag: TypeReg, Name: "file3",
306 ModTime: time.Unix(1400000000, 0),
307 PAXRecords: map[string]string{"mtime": "1400000000"},
312 file: "testdata/gnu-utf8.tar",
316 Name: "☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹",
318 Uid: 1000, Gid: 1000,
321 ModTime: time.Unix(0, 0),
327 file: "testdata/gnu-not-utf8.tar",
331 Name: "hi\x80\x81\x82\x83bye",
337 ModTime: time.Unix(0, 0),
342 // TODO(dsnet): Re-enable this test when adding sparse support.
343 // See https://golang.org/issue/22735
346 file: "testdata/gnu-nil-sparse-data.tar",
349 Typeflag: TypeGNUSparse,
352 SparseHoles: []sparseEntry{{Offset: 1000, Length: 0}},
354 testWrite{strings.Repeat("0123456789", 100), 1000, nil},
358 file: "testdata/gnu-nil-sparse-hole.tar",
361 Typeflag: TypeGNUSparse,
364 SparseHoles: []sparseEntry{{Offset: 0, Length: 1000}},
366 testWrite{strings.Repeat("\x00", 1000), 1000, nil},
370 file: "testdata/pax-nil-sparse-data.tar",
376 SparseHoles: []sparseEntry{{Offset: 1000, Length: 0}},
378 testWrite{strings.Repeat("0123456789", 100), 1000, nil},
382 file: "testdata/pax-nil-sparse-hole.tar",
388 SparseHoles: []sparseEntry{{Offset: 0, Length: 1000}},
390 testWrite{strings.Repeat("\x00", 1000), 1000, nil},
394 file: "testdata/gnu-sparse-big.tar",
397 Typeflag: TypeGNUSparse,
400 SparseHoles: []sparseEntry{
401 {Offset: 0e10, Length: 1e10 - 100},
402 {Offset: 1e10, Length: 1e10 - 100},
403 {Offset: 2e10, Length: 1e10 - 100},
404 {Offset: 3e10, Length: 1e10 - 100},
405 {Offset: 4e10, Length: 1e10 - 100},
406 {Offset: 5e10, Length: 1e10 - 100},
409 testReadFrom{fileOps{
410 int64(1e10 - blockSize),
411 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
412 int64(1e10 - blockSize),
413 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
414 int64(1e10 - blockSize),
415 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
416 int64(1e10 - blockSize),
417 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
418 int64(1e10 - blockSize),
419 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
420 int64(1e10 - blockSize),
421 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
426 file: "testdata/pax-sparse-big.tar",
432 SparseHoles: []sparseEntry{
433 {Offset: 0e10, Length: 1e10 - 100},
434 {Offset: 1e10, Length: 1e10 - 100},
435 {Offset: 2e10, Length: 1e10 - 100},
436 {Offset: 3e10, Length: 1e10 - 100},
437 {Offset: 4e10, Length: 1e10 - 100},
438 {Offset: 5e10, Length: 1e10 - 100},
441 testReadFrom{fileOps{
442 int64(1e10 - blockSize),
443 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
444 int64(1e10 - blockSize),
445 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
446 int64(1e10 - blockSize),
447 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
448 int64(1e10 - blockSize),
449 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
450 int64(1e10 - blockSize),
451 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
452 int64(1e10 - blockSize),
453 strings.Repeat("\x00", blockSize-100) + strings.Repeat("0123456789", 10),
459 file: "testdata/trailing-slash.tar",
461 testHeader{Header{Name: strings.Repeat("123456789/", 30)}, nil},
465 // Automatically promote zero value of Typeflag depending on the name.
466 file: "testdata/file-and-dir.tar",
468 testHeader{Header{Name: "small.txt", Size: 5}, nil},
469 testWrite{"Kilts", 5, nil},
470 testHeader{Header{Name: "dir/"}, nil},
475 equalError := func(x, y error) bool {
476 _, ok1 := x.(headerError)
477 _, ok2 := y.(headerError)
483 for _, v := range vectors {
484 t.Run(path.Base(v.file), func(t *testing.T) {
485 const maxSize = 10 << 10 // 10KiB
486 buf := new(bytes.Buffer)
487 tw := NewWriter(iotest.TruncateWriter(buf, maxSize))
489 for i, tf := range v.tests {
490 switch tf := tf.(type) {
492 err := tw.WriteHeader(&tf.hdr)
493 if !equalError(err, tf.wantErr) {
494 t.Fatalf("test %d, WriteHeader() = %v, want %v", i, err, tf.wantErr)
497 got, err := tw.Write([]byte(tf.str))
498 if got != tf.wantCnt || !equalError(err, tf.wantErr) {
499 t.Fatalf("test %d, Write() = (%d, %v), want (%d, %v)", i, got, err, tf.wantCnt, tf.wantErr)
502 f := &testFile{ops: tf.ops}
503 got, err := tw.readFrom(f)
504 if _, ok := err.(testError); ok {
505 t.Errorf("test %d, ReadFrom(): %v", i, err)
506 } else if got != tf.wantCnt || !equalError(err, tf.wantErr) {
507 t.Errorf("test %d, ReadFrom() = (%d, %v), want (%d, %v)", i, got, err, tf.wantCnt, tf.wantErr)
510 t.Errorf("test %d, expected %d more operations", i, len(f.ops))
514 if !equalError(err, tf.wantErr) {
515 t.Fatalf("test %d, Close() = %v, want %v", i, err, tf.wantErr)
518 t.Fatalf("test %d, unknown test operation: %T", i, tf)
523 want, err := ioutil.ReadFile(v.file)
525 t.Fatalf("ReadFile() = %v, want nil", err)
528 if !bytes.Equal(want, got) {
529 t.Fatalf("incorrect result: (-got +want)\n%v", bytediff(got, want))
536 func TestPax(t *testing.T) {
537 // Create an archive with a large name
538 fileinfo, err := os.Stat("testdata/small.txt")
542 hdr, err := FileInfoHeader(fileinfo, "")
544 t.Fatalf("os.Stat: %v", err)
546 // Force a PAX long name to be written
547 longName := strings.Repeat("ab", 100)
548 contents := strings.Repeat(" ", int(hdr.Size))
551 writer := NewWriter(&buf)
552 if err := writer.WriteHeader(hdr); err != nil {
555 if _, err = writer.Write([]byte(contents)); err != nil {
558 if err := writer.Close(); err != nil {
561 // Simple test to make sure PAX extensions are in effect
562 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.0")) {
563 t.Fatal("Expected at least one PAX header to be written.")
565 // Test that we can get a long name back out of the archive.
566 reader := NewReader(&buf)
567 hdr, err = reader.Next()
571 if hdr.Name != longName {
572 t.Fatal("Couldn't recover long file name")
576 func TestPaxSymlink(t *testing.T) {
577 // Create an archive with a large linkname
578 fileinfo, err := os.Stat("testdata/small.txt")
582 hdr, err := FileInfoHeader(fileinfo, "")
583 hdr.Typeflag = TypeSymlink
585 t.Fatalf("os.Stat:1 %v", err)
587 // Force a PAX long linkname to be written
588 longLinkname := strings.Repeat("1234567890/1234567890", 10)
589 hdr.Linkname = longLinkname
593 writer := NewWriter(&buf)
594 if err := writer.WriteHeader(hdr); err != nil {
597 if err := writer.Close(); err != nil {
600 // Simple test to make sure PAX extensions are in effect
601 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.0")) {
602 t.Fatal("Expected at least one PAX header to be written.")
604 // Test that we can get a long name back out of the archive.
605 reader := NewReader(&buf)
606 hdr, err = reader.Next()
610 if hdr.Linkname != longLinkname {
611 t.Fatal("Couldn't recover long link name")
615 func TestPaxNonAscii(t *testing.T) {
616 // Create an archive with non ascii. These should trigger a pax header
617 // because pax headers have a defined utf-8 encoding.
618 fileinfo, err := os.Stat("testdata/small.txt")
623 hdr, err := FileInfoHeader(fileinfo, "")
625 t.Fatalf("os.Stat:1 %v", err)
629 chineseFilename := "文件名"
630 chineseGroupname := "組"
631 chineseUsername := "用戶名"
633 hdr.Name = chineseFilename
634 hdr.Gname = chineseGroupname
635 hdr.Uname = chineseUsername
637 contents := strings.Repeat(" ", int(hdr.Size))
640 writer := NewWriter(&buf)
641 if err := writer.WriteHeader(hdr); err != nil {
644 if _, err = writer.Write([]byte(contents)); err != nil {
647 if err := writer.Close(); err != nil {
650 // Simple test to make sure PAX extensions are in effect
651 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.0")) {
652 t.Fatal("Expected at least one PAX header to be written.")
654 // Test that we can get a long name back out of the archive.
655 reader := NewReader(&buf)
656 hdr, err = reader.Next()
660 if hdr.Name != chineseFilename {
661 t.Fatal("Couldn't recover unicode name")
663 if hdr.Gname != chineseGroupname {
664 t.Fatal("Couldn't recover unicode group")
666 if hdr.Uname != chineseUsername {
667 t.Fatal("Couldn't recover unicode user")
671 func TestPaxXattrs(t *testing.T) {
672 xattrs := map[string]string{
676 // Create an archive with an xattr
677 fileinfo, err := os.Stat("testdata/small.txt")
681 hdr, err := FileInfoHeader(fileinfo, "")
683 t.Fatalf("os.Stat: %v", err)
688 writer := NewWriter(&buf)
689 if err := writer.WriteHeader(hdr); err != nil {
692 if _, err = writer.Write([]byte(contents)); err != nil {
695 if err := writer.Close(); err != nil {
698 // Test that we can get the xattrs back out of the archive.
699 reader := NewReader(&buf)
700 hdr, err = reader.Next()
704 if !reflect.DeepEqual(hdr.Xattrs, xattrs) {
705 t.Fatalf("xattrs did not survive round trip: got %+v, want %+v",
710 func TestPaxHeadersSorted(t *testing.T) {
711 fileinfo, err := os.Stat("testdata/small.txt")
715 hdr, err := FileInfoHeader(fileinfo, "")
717 t.Fatalf("os.Stat: %v", err)
719 contents := strings.Repeat(" ", int(hdr.Size))
721 hdr.Xattrs = map[string]string{
729 writer := NewWriter(&buf)
730 if err := writer.WriteHeader(hdr); err != nil {
733 if _, err = writer.Write([]byte(contents)); err != nil {
736 if err := writer.Close(); err != nil {
739 // Simple test to make sure PAX extensions are in effect
740 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.0")) {
741 t.Fatal("Expected at least one PAX header to be written.")
744 // xattr bar should always appear before others
746 bytes.Index(buf.Bytes(), []byte("bar=bar")),
747 bytes.Index(buf.Bytes(), []byte("baz=baz")),
748 bytes.Index(buf.Bytes(), []byte("foo=foo")),
749 bytes.Index(buf.Bytes(), []byte("qux=qux")),
751 if !sort.IntsAreSorted(indices) {
752 t.Fatal("PAX headers are not sorted")
756 func TestUSTARLongName(t *testing.T) {
757 // Create an archive with a path that failed to split with USTAR extension in previous versions.
758 fileinfo, err := os.Stat("testdata/small.txt")
762 hdr, err := FileInfoHeader(fileinfo, "")
763 hdr.Typeflag = TypeDir
765 t.Fatalf("os.Stat:1 %v", err)
767 // Force a PAX long name to be written. The name was taken from a practical example
768 // that fails and replaced ever char through numbers to anonymize the sample.
769 longName := "/0000_0000000/00000-000000000/0000_0000000/00000-0000000000000/0000_0000000/00000-0000000-00000000/0000_0000000/00000000/0000_0000000/000/0000_0000000/00000000v00/0000_0000000/000000/0000_0000000/0000000/0000_0000000/00000y-00/0000/0000/00000000/0x000000/"
774 writer := NewWriter(&buf)
775 if err := writer.WriteHeader(hdr); err != nil {
778 if err := writer.Close(); err != nil {
781 // Test that we can get a long name back out of the archive.
782 reader := NewReader(&buf)
783 hdr, err = reader.Next()
787 if hdr.Name != longName {
788 t.Fatal("Couldn't recover long name")
792 func TestValidTypeflagWithPAXHeader(t *testing.T) {
793 var buffer bytes.Buffer
794 tw := NewWriter(&buffer)
796 fileName := strings.Repeat("ab", 100)
803 if err := tw.WriteHeader(hdr); err != nil {
804 t.Fatalf("Failed to write header: %s", err)
806 if _, err := tw.Write([]byte("fooo")); err != nil {
807 t.Fatalf("Failed to write the file's data: %s", err)
811 tr := NewReader(&buffer)
814 header, err := tr.Next()
819 t.Fatalf("Failed to read header: %s", err)
821 if header.Typeflag != TypeReg {
822 t.Fatalf("Typeflag should've been %d, found %d", TypeReg, header.Typeflag)
827 // failOnceWriter fails exactly once and then always reports success.
828 type failOnceWriter bool
830 func (w *failOnceWriter) Write(b []byte) (int, error) {
832 return 0, io.ErrShortWrite
838 func TestWriterErrors(t *testing.T) {
839 t.Run("HeaderOnly", func(t *testing.T) {
840 tw := NewWriter(new(bytes.Buffer))
841 hdr := &Header{Name: "dir/", Typeflag: TypeDir}
842 if err := tw.WriteHeader(hdr); err != nil {
843 t.Fatalf("WriteHeader() = %v, want nil", err)
845 if _, err := tw.Write([]byte{0x00}); err != ErrWriteTooLong {
846 t.Fatalf("Write() = %v, want %v", err, ErrWriteTooLong)
850 t.Run("NegativeSize", func(t *testing.T) {
851 tw := NewWriter(new(bytes.Buffer))
852 hdr := &Header{Name: "small.txt", Size: -1}
853 if err := tw.WriteHeader(hdr); err == nil {
854 t.Fatalf("WriteHeader() = nil, want non-nil error")
858 t.Run("BeforeHeader", func(t *testing.T) {
859 tw := NewWriter(new(bytes.Buffer))
860 if _, err := tw.Write([]byte("Kilts")); err != ErrWriteTooLong {
861 t.Fatalf("Write() = %v, want %v", err, ErrWriteTooLong)
865 t.Run("AfterClose", func(t *testing.T) {
866 tw := NewWriter(new(bytes.Buffer))
867 hdr := &Header{Name: "small.txt"}
868 if err := tw.WriteHeader(hdr); err != nil {
869 t.Fatalf("WriteHeader() = %v, want nil", err)
871 if err := tw.Close(); err != nil {
872 t.Fatalf("Close() = %v, want nil", err)
874 if _, err := tw.Write([]byte("Kilts")); err != ErrWriteAfterClose {
875 t.Fatalf("Write() = %v, want %v", err, ErrWriteAfterClose)
877 if err := tw.Flush(); err != ErrWriteAfterClose {
878 t.Fatalf("Flush() = %v, want %v", err, ErrWriteAfterClose)
880 if err := tw.Close(); err != nil {
881 t.Fatalf("Close() = %v, want nil", err)
885 t.Run("PrematureFlush", func(t *testing.T) {
886 tw := NewWriter(new(bytes.Buffer))
887 hdr := &Header{Name: "small.txt", Size: 5}
888 if err := tw.WriteHeader(hdr); err != nil {
889 t.Fatalf("WriteHeader() = %v, want nil", err)
891 if err := tw.Flush(); err == nil {
892 t.Fatalf("Flush() = %v, want non-nil error", err)
896 t.Run("PrematureClose", func(t *testing.T) {
897 tw := NewWriter(new(bytes.Buffer))
898 hdr := &Header{Name: "small.txt", Size: 5}
899 if err := tw.WriteHeader(hdr); err != nil {
900 t.Fatalf("WriteHeader() = %v, want nil", err)
902 if err := tw.Close(); err == nil {
903 t.Fatalf("Close() = %v, want non-nil error", err)
907 t.Run("Persistence", func(t *testing.T) {
908 tw := NewWriter(new(failOnceWriter))
909 if err := tw.WriteHeader(&Header{}); err != io.ErrShortWrite {
910 t.Fatalf("WriteHeader() = %v, want %v", err, io.ErrShortWrite)
912 if err := tw.WriteHeader(&Header{Name: "small.txt"}); err == nil {
913 t.Errorf("WriteHeader() = got %v, want non-nil error", err)
915 if _, err := tw.Write(nil); err == nil {
916 t.Errorf("Write() = %v, want non-nil error", err)
918 if err := tw.Flush(); err == nil {
919 t.Errorf("Flush() = %v, want non-nil error", err)
921 if err := tw.Close(); err == nil {
922 t.Errorf("Close() = %v, want non-nil error", err)
927 func TestSplitUSTARPath(t *testing.T) {
930 vectors := []struct {
931 input string // Input path
932 prefix string // Expected output prefix
933 suffix string // Expected output suffix
934 ok bool // Split success?
937 {"abc", "", "", false},
938 {"用戶名", "", "", false},
939 {sr("a", nameSize), "", "", false},
940 {sr("a", nameSize) + "/", "", "", false},
941 {sr("a", nameSize) + "/a", sr("a", nameSize), "a", true},
942 {sr("a", prefixSize) + "/", "", "", false},
943 {sr("a", prefixSize) + "/a", sr("a", prefixSize), "a", true},
944 {sr("a", nameSize+1), "", "", false},
945 {sr("/", nameSize+1), sr("/", nameSize-1), "/", true},
946 {sr("a", prefixSize) + "/" + sr("b", nameSize),
947 sr("a", prefixSize), sr("b", nameSize), true},
948 {sr("a", prefixSize) + "//" + sr("b", nameSize), "", "", false},
949 {sr("a/", nameSize), sr("a/", 77) + "a", sr("a/", 22), true},
952 for _, v := range vectors {
953 prefix, suffix, ok := splitUSTARPath(v.input)
954 if prefix != v.prefix || suffix != v.suffix || ok != v.ok {
955 t.Errorf("splitUSTARPath(%q):\ngot (%q, %q, %v)\nwant (%q, %q, %v)",
956 v.input, prefix, suffix, ok, v.prefix, v.suffix, v.ok)
961 // TestIssue12594 tests that the Writer does not attempt to populate the prefix
962 // field when encoding a header in the GNU format. The prefix field is valid
963 // in USTAR and PAX, but not GNU.
964 func TestIssue12594(t *testing.T) {
966 "0/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/file.txt",
967 "0/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/file.txt",
968 "0/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/333/file.txt",
969 "0/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/file.txt",
970 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000/file.txt",
971 "/home/support/.openoffice.org/3/user/uno_packages/cache/registry/com.sun.star.comp.deployment.executable.PackageRegistryBackend",
974 for i, name := range names {
978 if err := tw.WriteHeader(&Header{
980 Uid: 1 << 25, // Prevent USTAR format
982 t.Errorf("test %d, unexpected WriteHeader error: %v", i, err)
984 if err := tw.Close(); err != nil {
985 t.Errorf("test %d, unexpected Close error: %v", i, err)
988 // The prefix field should never appear in the GNU format.
990 copy(blk[:], b.Bytes())
991 prefix := string(blk.USTAR().Prefix())
992 if i := strings.IndexByte(prefix, 0); i >= 0 {
993 prefix = prefix[:i] // Truncate at the NUL terminator
995 if blk.GetFormat() == FormatGNU && len(prefix) > 0 && strings.HasPrefix(name, prefix) {
996 t.Errorf("test %d, found prefix in GNU format: %s", i, prefix)
1000 hdr, err := tr.Next()
1002 t.Errorf("test %d, unexpected Next error: %v", i, err)
1004 if hdr.Name != name {
1005 t.Errorf("test %d, hdr.Name = %s, want %s", i, hdr.Name, name)
1010 // testNonEmptyWriter wraps an io.Writer and ensures that
1011 // Write is never called with an empty buffer.
1012 type testNonEmptyWriter struct{ io.Writer }
1014 func (w testNonEmptyWriter) Write(b []byte) (int, error) {
1016 return 0, errors.New("unexpected empty Write call")
1018 return w.Writer.Write(b)
1021 func TestFileWriter(t *testing.T) {
1023 testWrite struct { // Write(str) == (wantCnt, wantErr)
1028 testReadFrom struct { // ReadFrom(testFile{ops}) == (wantCnt, wantErr)
1033 testRemaining struct { // LogicalRemaining() == wantLCnt, PhysicalRemaining() == wantPCnt
1037 testFnc interface{} // testWrite | testReadFrom | testRemaining
1050 fileMaker interface{} // makeReg | makeSparse
1053 vectors := []struct {
1057 maker: makeReg{0, ""},
1059 testRemaining{0, 0},
1060 testWrite{"", 0, nil},
1061 testWrite{"a", 0, ErrWriteTooLong},
1062 testReadFrom{fileOps{""}, 0, nil},
1063 testReadFrom{fileOps{"a"}, 0, ErrWriteTooLong},
1064 testRemaining{0, 0},
1067 maker: makeReg{1, "a"},
1069 testRemaining{1, 1},
1070 testWrite{"", 0, nil},
1071 testWrite{"a", 1, nil},
1072 testWrite{"bcde", 0, ErrWriteTooLong},
1073 testWrite{"", 0, nil},
1074 testReadFrom{fileOps{""}, 0, nil},
1075 testReadFrom{fileOps{"a"}, 0, ErrWriteTooLong},
1076 testRemaining{0, 0},
1079 maker: makeReg{5, "hello"},
1081 testRemaining{5, 5},
1082 testWrite{"hello", 5, nil},
1083 testRemaining{0, 0},
1086 maker: makeReg{5, "\x00\x00\x00\x00\x00"},
1088 testRemaining{5, 5},
1089 testReadFrom{fileOps{"\x00\x00\x00\x00\x00"}, 5, nil},
1090 testRemaining{0, 0},
1093 maker: makeReg{5, "\x00\x00\x00\x00\x00"},
1095 testRemaining{5, 5},
1096 testReadFrom{fileOps{"\x00\x00\x00\x00\x00extra"}, 5, ErrWriteTooLong},
1097 testRemaining{0, 0},
1100 maker: makeReg{5, "abc\x00\x00"},
1102 testRemaining{5, 5},
1103 testWrite{"abc", 3, nil},
1104 testRemaining{2, 2},
1105 testReadFrom{fileOps{"\x00\x00"}, 2, nil},
1106 testRemaining{0, 0},
1109 maker: makeReg{5, "\x00\x00abc"},
1111 testRemaining{5, 5},
1112 testWrite{"\x00\x00", 2, nil},
1113 testRemaining{3, 3},
1114 testWrite{"abc", 3, nil},
1115 testReadFrom{fileOps{"z"}, 0, ErrWriteTooLong},
1116 testWrite{"z", 0, ErrWriteTooLong},
1117 testRemaining{0, 0},
1120 maker: makeSparse{makeReg{5, "abcde"}, sparseHoles{{2, 3}}, 8},
1122 testRemaining{8, 5},
1123 testWrite{"ab\x00\x00\x00cde", 8, nil},
1124 testWrite{"a", 0, ErrWriteTooLong},
1125 testRemaining{0, 0},
1128 maker: makeSparse{makeReg{5, "abcde"}, sparseHoles{{2, 3}}, 8},
1130 testWrite{"ab\x00\x00\x00cdez", 8, ErrWriteTooLong},
1131 testRemaining{0, 0},
1134 maker: makeSparse{makeReg{5, "abcde"}, sparseHoles{{2, 3}}, 8},
1136 testWrite{"ab\x00", 3, nil},
1137 testRemaining{5, 3},
1138 testWrite{"\x00\x00cde", 5, nil},
1139 testWrite{"a", 0, ErrWriteTooLong},
1140 testRemaining{0, 0},
1143 maker: makeSparse{makeReg{5, "abcde"}, sparseHoles{{2, 3}}, 8},
1145 testWrite{"ab", 2, nil},
1146 testRemaining{6, 3},
1147 testReadFrom{fileOps{int64(3), "cde"}, 6, nil},
1148 testRemaining{0, 0},
1151 maker: makeSparse{makeReg{5, "abcde"}, sparseHoles{{2, 3}}, 8},
1153 testReadFrom{fileOps{"ab", int64(3), "cde"}, 8, nil},
1154 testRemaining{0, 0},
1157 maker: makeSparse{makeReg{5, "abcde"}, sparseHoles{{2, 3}}, 8},
1159 testReadFrom{fileOps{"ab", int64(3), "cdeX"}, 8, ErrWriteTooLong},
1160 testRemaining{0, 0},
1163 maker: makeSparse{makeReg{4, "abcd"}, sparseHoles{{2, 3}}, 8},
1165 testReadFrom{fileOps{"ab", int64(3), "cd"}, 7, io.ErrUnexpectedEOF},
1166 testRemaining{1, 0},
1169 maker: makeSparse{makeReg{4, "abcd"}, sparseHoles{{2, 3}}, 8},
1171 testReadFrom{fileOps{"ab", int64(3), "cde"}, 7, errMissData},
1172 testRemaining{1, 0},
1175 maker: makeSparse{makeReg{6, "abcde"}, sparseHoles{{2, 3}}, 8},
1177 testReadFrom{fileOps{"ab", int64(3), "cde"}, 8, errUnrefData},
1178 testRemaining{0, 1},
1181 maker: makeSparse{makeReg{4, "abcd"}, sparseHoles{{2, 3}}, 8},
1183 testWrite{"ab", 2, nil},
1184 testRemaining{6, 2},
1185 testWrite{"\x00\x00\x00", 3, nil},
1186 testRemaining{3, 2},
1187 testWrite{"cde", 2, errMissData},
1188 testRemaining{1, 0},
1191 maker: makeSparse{makeReg{6, "abcde"}, sparseHoles{{2, 3}}, 8},
1193 testWrite{"ab", 2, nil},
1194 testRemaining{6, 4},
1195 testWrite{"\x00\x00\x00", 3, nil},
1196 testRemaining{3, 4},
1197 testWrite{"cde", 3, errUnrefData},
1198 testRemaining{0, 1},
1201 maker: makeSparse{makeReg{3, "abc"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1203 testRemaining{7, 3},
1204 testWrite{"\x00\x00abc\x00\x00", 7, nil},
1205 testRemaining{0, 0},
1208 maker: makeSparse{makeReg{3, "abc"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1210 testRemaining{7, 3},
1211 testReadFrom{fileOps{int64(2), "abc", int64(1), "\x00"}, 7, nil},
1212 testRemaining{0, 0},
1215 maker: makeSparse{makeReg{3, ""}, sparseHoles{{0, 2}, {5, 2}}, 7},
1217 testWrite{"abcdefg", 0, errWriteHole},
1220 maker: makeSparse{makeReg{3, "abc"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1222 testWrite{"\x00\x00abcde", 5, errWriteHole},
1225 maker: makeSparse{makeReg{3, "abc"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1227 testWrite{"\x00\x00abc\x00\x00z", 7, ErrWriteTooLong},
1228 testRemaining{0, 0},
1231 maker: makeSparse{makeReg{3, "abc"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1233 testWrite{"\x00\x00", 2, nil},
1234 testRemaining{5, 3},
1235 testWrite{"abc", 3, nil},
1236 testRemaining{2, 0},
1237 testWrite{"\x00\x00", 2, nil},
1238 testRemaining{0, 0},
1241 maker: makeSparse{makeReg{2, "ab"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1243 testWrite{"\x00\x00", 2, nil},
1244 testWrite{"abc", 2, errMissData},
1245 testWrite{"\x00\x00", 0, errMissData},
1248 maker: makeSparse{makeReg{4, "abc"}, sparseHoles{{0, 2}, {5, 2}}, 7},
1250 testWrite{"\x00\x00", 2, nil},
1251 testWrite{"abc", 3, nil},
1252 testWrite{"\x00\x00", 2, errUnrefData},
1256 for i, v := range vectors {
1258 bb := new(bytes.Buffer)
1259 w := testNonEmptyWriter{bb}
1261 switch maker := v.maker.(type) {
1263 fw = ®FileWriter{w, maker.size}
1264 wantStr = maker.wantStr
1266 if !validateSparseEntries(maker.sph, maker.size) {
1267 t.Fatalf("invalid sparse map: %v", maker.sph)
1269 spd := invertSparseEntries(maker.sph, maker.size)
1270 fw = ®FileWriter{w, maker.makeReg.size}
1271 fw = &sparseFileWriter{fw, spd, 0}
1272 wantStr = maker.makeReg.wantStr
1274 t.Fatalf("test %d, unknown make operation: %T", i, maker)
1277 for j, tf := range v.tests {
1278 switch tf := tf.(type) {
1280 got, err := fw.Write([]byte(tf.str))
1281 if got != tf.wantCnt || err != tf.wantErr {
1282 t.Errorf("test %d.%d, Write(%s):\ngot (%d, %v)\nwant (%d, %v)", i, j, tf.str, got, err, tf.wantCnt, tf.wantErr)
1285 f := &testFile{ops: tf.ops}
1286 got, err := fw.ReadFrom(f)
1287 if _, ok := err.(testError); ok {
1288 t.Errorf("test %d.%d, ReadFrom(): %v", i, j, err)
1289 } else if got != tf.wantCnt || err != tf.wantErr {
1290 t.Errorf("test %d.%d, ReadFrom() = (%d, %v), want (%d, %v)", i, j, got, err, tf.wantCnt, tf.wantErr)
1293 t.Errorf("test %d.%d, expected %d more operations", i, j, len(f.ops))
1296 if got := fw.LogicalRemaining(); got != tf.wantLCnt {
1297 t.Errorf("test %d.%d, LogicalRemaining() = %d, want %d", i, j, got, tf.wantLCnt)
1299 if got := fw.PhysicalRemaining(); got != tf.wantPCnt {
1300 t.Errorf("test %d.%d, PhysicalRemaining() = %d, want %d", i, j, got, tf.wantPCnt)
1303 t.Fatalf("test %d.%d, unknown test operation: %T", i, j, tf)
1307 if got := bb.String(); got != wantStr {
1308 t.Fatalf("test %d, String() = %q, want %q", i, got, wantStr)