...
Run Format

Source file src/archive/tar/reader_test.go

Documentation: archive/tar

     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 tar
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/md5"
    10  	"errors"
    11  	"fmt"
    12  	"io"
    13  	"io/ioutil"
    14  	"math"
    15  	"os"
    16  	"path"
    17  	"reflect"
    18  	"strconv"
    19  	"strings"
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func TestReader(t *testing.T) {
    25  	vectors := []struct {
    26  		file    string    // Test input file
    27  		headers []*Header // Expected output headers
    28  		chksums []string  // MD5 checksum of files, leave as nil if not checked
    29  		err     error     // Expected error to occur
    30  	}{{
    31  		file: "testdata/gnu.tar",
    32  		headers: []*Header{{
    33  			Name:     "small.txt",
    34  			Mode:     0640,
    35  			Uid:      73025,
    36  			Gid:      5000,
    37  			Size:     5,
    38  			ModTime:  time.Unix(1244428340, 0),
    39  			Typeflag: '0',
    40  			Uname:    "dsymonds",
    41  			Gname:    "eng",
    42  			Format:   FormatGNU,
    43  		}, {
    44  			Name:     "small2.txt",
    45  			Mode:     0640,
    46  			Uid:      73025,
    47  			Gid:      5000,
    48  			Size:     11,
    49  			ModTime:  time.Unix(1244436044, 0),
    50  			Typeflag: '0',
    51  			Uname:    "dsymonds",
    52  			Gname:    "eng",
    53  			Format:   FormatGNU,
    54  		}},
    55  		chksums: []string{
    56  			"e38b27eaccb4391bdec553a7f3ae6b2f",
    57  			"c65bd2e50a56a2138bf1716f2fd56fe9",
    58  		},
    59  	}, {
    60  		file: "testdata/sparse-formats.tar",
    61  		headers: []*Header{{
    62  			Name:     "sparse-gnu",
    63  			Mode:     420,
    64  			Uid:      1000,
    65  			Gid:      1000,
    66  			Size:     200,
    67  			ModTime:  time.Unix(1392395740, 0),
    68  			Typeflag: 0x53,
    69  			Linkname: "",
    70  			Uname:    "david",
    71  			Gname:    "david",
    72  			Devmajor: 0,
    73  			Devminor: 0,
    74  			Format:   FormatGNU,
    75  		}, {
    76  			Name:     "sparse-posix-0.0",
    77  			Mode:     420,
    78  			Uid:      1000,
    79  			Gid:      1000,
    80  			Size:     200,
    81  			ModTime:  time.Unix(1392342187, 0),
    82  			Typeflag: 0x30,
    83  			Linkname: "",
    84  			Uname:    "david",
    85  			Gname:    "david",
    86  			Devmajor: 0,
    87  			Devminor: 0,
    88  			PAXRecords: map[string]string{
    89  				"GNU.sparse.size":      "200",
    90  				"GNU.sparse.numblocks": "95",
    91  				"GNU.sparse.map":       "1,1,3,1,5,1,7,1,9,1,11,1,13,1,15,1,17,1,19,1,21,1,23,1,25,1,27,1,29,1,31,1,33,1,35,1,37,1,39,1,41,1,43,1,45,1,47,1,49,1,51,1,53,1,55,1,57,1,59,1,61,1,63,1,65,1,67,1,69,1,71,1,73,1,75,1,77,1,79,1,81,1,83,1,85,1,87,1,89,1,91,1,93,1,95,1,97,1,99,1,101,1,103,1,105,1,107,1,109,1,111,1,113,1,115,1,117,1,119,1,121,1,123,1,125,1,127,1,129,1,131,1,133,1,135,1,137,1,139,1,141,1,143,1,145,1,147,1,149,1,151,1,153,1,155,1,157,1,159,1,161,1,163,1,165,1,167,1,169,1,171,1,173,1,175,1,177,1,179,1,181,1,183,1,185,1,187,1,189,1",
    92  			},
    93  			Format: FormatPAX,
    94  		}, {
    95  			Name:     "sparse-posix-0.1",
    96  			Mode:     420,
    97  			Uid:      1000,
    98  			Gid:      1000,
    99  			Size:     200,
   100  			ModTime:  time.Unix(1392340456, 0),
   101  			Typeflag: 0x30,
   102  			Linkname: "",
   103  			Uname:    "david",
   104  			Gname:    "david",
   105  			Devmajor: 0,
   106  			Devminor: 0,
   107  			PAXRecords: map[string]string{
   108  				"GNU.sparse.size":      "200",
   109  				"GNU.sparse.numblocks": "95",
   110  				"GNU.sparse.map":       "1,1,3,1,5,1,7,1,9,1,11,1,13,1,15,1,17,1,19,1,21,1,23,1,25,1,27,1,29,1,31,1,33,1,35,1,37,1,39,1,41,1,43,1,45,1,47,1,49,1,51,1,53,1,55,1,57,1,59,1,61,1,63,1,65,1,67,1,69,1,71,1,73,1,75,1,77,1,79,1,81,1,83,1,85,1,87,1,89,1,91,1,93,1,95,1,97,1,99,1,101,1,103,1,105,1,107,1,109,1,111,1,113,1,115,1,117,1,119,1,121,1,123,1,125,1,127,1,129,1,131,1,133,1,135,1,137,1,139,1,141,1,143,1,145,1,147,1,149,1,151,1,153,1,155,1,157,1,159,1,161,1,163,1,165,1,167,1,169,1,171,1,173,1,175,1,177,1,179,1,181,1,183,1,185,1,187,1,189,1",
   111  				"GNU.sparse.name":      "sparse-posix-0.1",
   112  			},
   113  			Format: FormatPAX,
   114  		}, {
   115  			Name:     "sparse-posix-1.0",
   116  			Mode:     420,
   117  			Uid:      1000,
   118  			Gid:      1000,
   119  			Size:     200,
   120  			ModTime:  time.Unix(1392337404, 0),
   121  			Typeflag: 0x30,
   122  			Linkname: "",
   123  			Uname:    "david",
   124  			Gname:    "david",
   125  			Devmajor: 0,
   126  			Devminor: 0,
   127  			PAXRecords: map[string]string{
   128  				"GNU.sparse.major":    "1",
   129  				"GNU.sparse.minor":    "0",
   130  				"GNU.sparse.realsize": "200",
   131  				"GNU.sparse.name":     "sparse-posix-1.0",
   132  			},
   133  			Format: FormatPAX,
   134  		}, {
   135  			Name:     "end",
   136  			Mode:     420,
   137  			Uid:      1000,
   138  			Gid:      1000,
   139  			Size:     4,
   140  			ModTime:  time.Unix(1392398319, 0),
   141  			Typeflag: 0x30,
   142  			Linkname: "",
   143  			Uname:    "david",
   144  			Gname:    "david",
   145  			Devmajor: 0,
   146  			Devminor: 0,
   147  			Format:   FormatGNU,
   148  		}},
   149  		chksums: []string{
   150  			"6f53234398c2449fe67c1812d993012f",
   151  			"6f53234398c2449fe67c1812d993012f",
   152  			"6f53234398c2449fe67c1812d993012f",
   153  			"6f53234398c2449fe67c1812d993012f",
   154  			"b0061974914468de549a2af8ced10316",
   155  		},
   156  	}, {
   157  		file: "testdata/star.tar",
   158  		headers: []*Header{{
   159  			Name:       "small.txt",
   160  			Mode:       0640,
   161  			Uid:        73025,
   162  			Gid:        5000,
   163  			Size:       5,
   164  			ModTime:    time.Unix(1244592783, 0),
   165  			Typeflag:   '0',
   166  			Uname:      "dsymonds",
   167  			Gname:      "eng",
   168  			AccessTime: time.Unix(1244592783, 0),
   169  			ChangeTime: time.Unix(1244592783, 0),
   170  		}, {
   171  			Name:       "small2.txt",
   172  			Mode:       0640,
   173  			Uid:        73025,
   174  			Gid:        5000,
   175  			Size:       11,
   176  			ModTime:    time.Unix(1244592783, 0),
   177  			Typeflag:   '0',
   178  			Uname:      "dsymonds",
   179  			Gname:      "eng",
   180  			AccessTime: time.Unix(1244592783, 0),
   181  			ChangeTime: time.Unix(1244592783, 0),
   182  		}},
   183  	}, {
   184  		file: "testdata/v7.tar",
   185  		headers: []*Header{{
   186  			Name:     "small.txt",
   187  			Mode:     0444,
   188  			Uid:      73025,
   189  			Gid:      5000,
   190  			Size:     5,
   191  			ModTime:  time.Unix(1244593104, 0),
   192  			Typeflag: '0',
   193  		}, {
   194  			Name:     "small2.txt",
   195  			Mode:     0444,
   196  			Uid:      73025,
   197  			Gid:      5000,
   198  			Size:     11,
   199  			ModTime:  time.Unix(1244593104, 0),
   200  			Typeflag: '0',
   201  		}},
   202  	}, {
   203  		file: "testdata/pax.tar",
   204  		headers: []*Header{{
   205  			Name:       "a/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100",
   206  			Mode:       0664,
   207  			Uid:        1000,
   208  			Gid:        1000,
   209  			Uname:      "shane",
   210  			Gname:      "shane",
   211  			Size:       7,
   212  			ModTime:    time.Unix(1350244992, 23960108),
   213  			ChangeTime: time.Unix(1350244992, 23960108),
   214  			AccessTime: time.Unix(1350244992, 23960108),
   215  			Typeflag:   TypeReg,
   216  			PAXRecords: map[string]string{
   217  				"path":  "a/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100",
   218  				"mtime": "1350244992.023960108",
   219  				"atime": "1350244992.023960108",
   220  				"ctime": "1350244992.023960108",
   221  			},
   222  			Format: FormatPAX,
   223  		}, {
   224  			Name:       "a/b",
   225  			Mode:       0777,
   226  			Uid:        1000,
   227  			Gid:        1000,
   228  			Uname:      "shane",
   229  			Gname:      "shane",
   230  			Size:       0,
   231  			ModTime:    time.Unix(1350266320, 910238425),
   232  			ChangeTime: time.Unix(1350266320, 910238425),
   233  			AccessTime: time.Unix(1350266320, 910238425),
   234  			Typeflag:   TypeSymlink,
   235  			Linkname:   "123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100",
   236  			PAXRecords: map[string]string{
   237  				"linkpath": "123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100",
   238  				"mtime":    "1350266320.910238425",
   239  				"atime":    "1350266320.910238425",
   240  				"ctime":    "1350266320.910238425",
   241  			},
   242  			Format: FormatPAX,
   243  		}},
   244  	}, {
   245  		file: "testdata/pax-bad-hdr-file.tar",
   246  		err:  ErrHeader,
   247  	}, {
   248  		file: "testdata/pax-bad-mtime-file.tar",
   249  		err:  ErrHeader,
   250  	}, {
   251  		file: "testdata/pax-pos-size-file.tar",
   252  		headers: []*Header{{
   253  			Name:     "foo",
   254  			Mode:     0640,
   255  			Uid:      319973,
   256  			Gid:      5000,
   257  			Size:     999,
   258  			ModTime:  time.Unix(1442282516, 0),
   259  			Typeflag: '0',
   260  			Uname:    "joetsai",
   261  			Gname:    "eng",
   262  			PAXRecords: map[string]string{
   263  				"size": "000000000000000000000999",
   264  			},
   265  			Format: FormatPAX,
   266  		}},
   267  		chksums: []string{
   268  			"0afb597b283fe61b5d4879669a350556",
   269  		},
   270  	}, {
   271  		file: "testdata/pax-records.tar",
   272  		headers: []*Header{{
   273  			Typeflag: TypeReg,
   274  			Name:     "file",
   275  			Uname:    strings.Repeat("long", 10),
   276  			ModTime:  time.Unix(0, 0),
   277  			PAXRecords: map[string]string{
   278  				"GOLANG.pkg": "tar",
   279  				"comment":    "Hello, 世界",
   280  				"uname":      strings.Repeat("long", 10),
   281  			},
   282  			Format: FormatPAX,
   283  		}},
   284  	}, {
   285  		file: "testdata/pax-global-records.tar",
   286  		headers: []*Header{{
   287  			Typeflag:   TypeXGlobalHeader,
   288  			Name:       "global1",
   289  			PAXRecords: map[string]string{"path": "global1", "mtime": "1500000000.0"},
   290  			Format:     FormatPAX,
   291  		}, {
   292  			Typeflag: TypeReg,
   293  			Name:     "file1",
   294  			ModTime:  time.Unix(0, 0),
   295  			Format:   FormatUSTAR,
   296  		}, {
   297  			Typeflag:   TypeReg,
   298  			Name:       "file2",
   299  			PAXRecords: map[string]string{"path": "file2"},
   300  			ModTime:    time.Unix(0, 0),
   301  			Format:     FormatPAX,
   302  		}, {
   303  			Typeflag:   TypeXGlobalHeader,
   304  			Name:       "GlobalHead.0.0",
   305  			PAXRecords: map[string]string{"path": ""},
   306  			Format:     FormatPAX,
   307  		}, {
   308  			Typeflag: TypeReg,
   309  			Name:     "file3",
   310  			ModTime:  time.Unix(0, 0),
   311  			Format:   FormatUSTAR,
   312  		}, {
   313  			Typeflag:   TypeReg,
   314  			Name:       "file4",
   315  			ModTime:    time.Unix(1400000000, 0),
   316  			PAXRecords: map[string]string{"mtime": "1400000000"},
   317  			Format:     FormatPAX,
   318  		}},
   319  	}, {
   320  		file: "testdata/nil-uid.tar", // golang.org/issue/5290
   321  		headers: []*Header{{
   322  			Name:     "P1050238.JPG.log",
   323  			Mode:     0664,
   324  			Uid:      0,
   325  			Gid:      0,
   326  			Size:     14,
   327  			ModTime:  time.Unix(1365454838, 0),
   328  			Typeflag: TypeReg,
   329  			Linkname: "",
   330  			Uname:    "eyefi",
   331  			Gname:    "eyefi",
   332  			Devmajor: 0,
   333  			Devminor: 0,
   334  			Format:   FormatGNU,
   335  		}},
   336  	}, {
   337  		file: "testdata/xattrs.tar",
   338  		headers: []*Header{{
   339  			Name:       "small.txt",
   340  			Mode:       0644,
   341  			Uid:        1000,
   342  			Gid:        10,
   343  			Size:       5,
   344  			ModTime:    time.Unix(1386065770, 448252320),
   345  			Typeflag:   '0',
   346  			Uname:      "alex",
   347  			Gname:      "wheel",
   348  			AccessTime: time.Unix(1389782991, 419875220),
   349  			ChangeTime: time.Unix(1389782956, 794414986),
   350  			Xattrs: map[string]string{
   351  				"user.key":  "value",
   352  				"user.key2": "value2",
   353  				// Interestingly, selinux encodes the terminating null inside the xattr
   354  				"security.selinux": "unconfined_u:object_r:default_t:s0\x00",
   355  			},
   356  			PAXRecords: map[string]string{
   357  				"mtime":                         "1386065770.44825232",
   358  				"atime":                         "1389782991.41987522",
   359  				"ctime":                         "1389782956.794414986",
   360  				"SCHILY.xattr.user.key":         "value",
   361  				"SCHILY.xattr.user.key2":        "value2",
   362  				"SCHILY.xattr.security.selinux": "unconfined_u:object_r:default_t:s0\x00",
   363  			},
   364  			Format: FormatPAX,
   365  		}, {
   366  			Name:       "small2.txt",
   367  			Mode:       0644,
   368  			Uid:        1000,
   369  			Gid:        10,
   370  			Size:       11,
   371  			ModTime:    time.Unix(1386065770, 449252304),
   372  			Typeflag:   '0',
   373  			Uname:      "alex",
   374  			Gname:      "wheel",
   375  			AccessTime: time.Unix(1389782991, 419875220),
   376  			ChangeTime: time.Unix(1386065770, 449252304),
   377  			Xattrs: map[string]string{
   378  				"security.selinux": "unconfined_u:object_r:default_t:s0\x00",
   379  			},
   380  			PAXRecords: map[string]string{
   381  				"mtime":                         "1386065770.449252304",
   382  				"atime":                         "1389782991.41987522",
   383  				"ctime":                         "1386065770.449252304",
   384  				"SCHILY.xattr.security.selinux": "unconfined_u:object_r:default_t:s0\x00",
   385  			},
   386  			Format: FormatPAX,
   387  		}},
   388  	}, {
   389  		// Matches the behavior of GNU, BSD, and STAR tar utilities.
   390  		file: "testdata/gnu-multi-hdrs.tar",
   391  		headers: []*Header{{
   392  			Name:     "GNU2/GNU2/long-path-name",
   393  			Linkname: "GNU4/GNU4/long-linkpath-name",
   394  			ModTime:  time.Unix(0, 0),
   395  			Typeflag: '2',
   396  			Format:   FormatGNU,
   397  		}},
   398  	}, {
   399  		// GNU tar file with atime and ctime fields set.
   400  		// Created with the GNU tar v1.27.1.
   401  		//	tar --incremental -S -cvf gnu-incremental.tar test2
   402  		file: "testdata/gnu-incremental.tar",
   403  		headers: []*Header{{
   404  			Name:       "test2/",
   405  			Mode:       16877,
   406  			Uid:        1000,
   407  			Gid:        1000,
   408  			Size:       14,
   409  			ModTime:    time.Unix(1441973427, 0),
   410  			Typeflag:   'D',
   411  			Uname:      "rawr",
   412  			Gname:      "dsnet",
   413  			AccessTime: time.Unix(1441974501, 0),
   414  			ChangeTime: time.Unix(1441973436, 0),
   415  			Format:     FormatGNU,
   416  		}, {
   417  			Name:       "test2/foo",
   418  			Mode:       33188,
   419  			Uid:        1000,
   420  			Gid:        1000,
   421  			Size:       64,
   422  			ModTime:    time.Unix(1441973363, 0),
   423  			Typeflag:   '0',
   424  			Uname:      "rawr",
   425  			Gname:      "dsnet",
   426  			AccessTime: time.Unix(1441974501, 0),
   427  			ChangeTime: time.Unix(1441973436, 0),
   428  			Format:     FormatGNU,
   429  		}, {
   430  			Name:       "test2/sparse",
   431  			Mode:       33188,
   432  			Uid:        1000,
   433  			Gid:        1000,
   434  			Size:       536870912,
   435  			ModTime:    time.Unix(1441973427, 0),
   436  			Typeflag:   'S',
   437  			Uname:      "rawr",
   438  			Gname:      "dsnet",
   439  			AccessTime: time.Unix(1441991948, 0),
   440  			ChangeTime: time.Unix(1441973436, 0),
   441  			Format:     FormatGNU,
   442  		}},
   443  	}, {
   444  		// Matches the behavior of GNU and BSD tar utilities.
   445  		file: "testdata/pax-multi-hdrs.tar",
   446  		headers: []*Header{{
   447  			Name:     "bar",
   448  			Linkname: "PAX4/PAX4/long-linkpath-name",
   449  			ModTime:  time.Unix(0, 0),
   450  			Typeflag: '2',
   451  			PAXRecords: map[string]string{
   452  				"linkpath": "PAX4/PAX4/long-linkpath-name",
   453  			},
   454  			Format: FormatPAX,
   455  		}},
   456  	}, {
   457  		// Both BSD and GNU tar truncate long names at first NUL even
   458  		// if there is data following that NUL character.
   459  		// This is reasonable as GNU long names are C-strings.
   460  		file: "testdata/gnu-long-nul.tar",
   461  		headers: []*Header{{
   462  			Name:     "0123456789",
   463  			Mode:     0644,
   464  			Uid:      1000,
   465  			Gid:      1000,
   466  			ModTime:  time.Unix(1486082191, 0),
   467  			Typeflag: '0',
   468  			Uname:    "rawr",
   469  			Gname:    "dsnet",
   470  			Format:   FormatGNU,
   471  		}},
   472  	}, {
   473  		// This archive was generated by Writer but is readable by both
   474  		// GNU and BSD tar utilities.
   475  		// The archive generated by GNU is nearly byte-for-byte identical
   476  		// to the Go version except the Go version sets a negative Devminor
   477  		// just to force the GNU format.
   478  		file: "testdata/gnu-utf8.tar",
   479  		headers: []*Header{{
   480  			Name: "☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹",
   481  			Mode: 0644,
   482  			Uid:  1000, Gid: 1000,
   483  			ModTime:  time.Unix(0, 0),
   484  			Typeflag: '0',
   485  			Uname:    "☺",
   486  			Gname:    "⚹",
   487  			Format:   FormatGNU,
   488  		}},
   489  	}, {
   490  		// This archive was generated by Writer but is readable by both
   491  		// GNU and BSD tar utilities.
   492  		// The archive generated by GNU is nearly byte-for-byte identical
   493  		// to the Go version except the Go version sets a negative Devminor
   494  		// just to force the GNU format.
   495  		file: "testdata/gnu-not-utf8.tar",
   496  		headers: []*Header{{
   497  			Name:     "hi\x80\x81\x82\x83bye",
   498  			Mode:     0644,
   499  			Uid:      1000,
   500  			Gid:      1000,
   501  			ModTime:  time.Unix(0, 0),
   502  			Typeflag: '0',
   503  			Uname:    "rawr",
   504  			Gname:    "dsnet",
   505  			Format:   FormatGNU,
   506  		}},
   507  	}, {
   508  		// BSD tar v3.1.2 and GNU tar v1.27.1 both rejects PAX records
   509  		// with NULs in the key.
   510  		file: "testdata/pax-nul-xattrs.tar",
   511  		err:  ErrHeader,
   512  	}, {
   513  		// BSD tar v3.1.2 rejects a PAX path with NUL in the value, while
   514  		// GNU tar v1.27.1 simply truncates at first NUL.
   515  		// We emulate the behavior of BSD since it is strange doing NUL
   516  		// truncations since PAX records are length-prefix strings instead
   517  		// of NUL-terminated C-strings.
   518  		file: "testdata/pax-nul-path.tar",
   519  		err:  ErrHeader,
   520  	}, {
   521  		file: "testdata/neg-size.tar",
   522  		err:  ErrHeader,
   523  	}, {
   524  		file: "testdata/issue10968.tar",
   525  		err:  ErrHeader,
   526  	}, {
   527  		file: "testdata/issue11169.tar",
   528  		err:  ErrHeader,
   529  	}, {
   530  		file: "testdata/issue12435.tar",
   531  		err:  ErrHeader,
   532  	}, {
   533  		// Ensure that we can read back the original Header as written with
   534  		// a buggy pre-Go1.8 tar.Writer.
   535  		file: "testdata/invalid-go17.tar",
   536  		headers: []*Header{{
   537  			Name:     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/foo",
   538  			Uid:      010000000,
   539  			ModTime:  time.Unix(0, 0),
   540  			Typeflag: '0',
   541  		}},
   542  	}, {
   543  		// USTAR archive with a regular entry with non-zero device numbers.
   544  		file: "testdata/ustar-file-devs.tar",
   545  		headers: []*Header{{
   546  			Name:     "file",
   547  			Mode:     0644,
   548  			Typeflag: '0',
   549  			ModTime:  time.Unix(0, 0),
   550  			Devmajor: 1,
   551  			Devminor: 1,
   552  			Format:   FormatUSTAR,
   553  		}},
   554  	}, {
   555  		// Generated by Go, works on BSD tar v3.1.2 and GNU tar v.1.27.1.
   556  		file: "testdata/gnu-nil-sparse-data.tar",
   557  		headers: []*Header{{
   558  			Name:     "sparse.db",
   559  			Typeflag: TypeGNUSparse,
   560  			Size:     1000,
   561  			ModTime:  time.Unix(0, 0),
   562  			Format:   FormatGNU,
   563  		}},
   564  	}, {
   565  		// Generated by Go, works on BSD tar v3.1.2 and GNU tar v.1.27.1.
   566  		file: "testdata/gnu-nil-sparse-hole.tar",
   567  		headers: []*Header{{
   568  			Name:     "sparse.db",
   569  			Typeflag: TypeGNUSparse,
   570  			Size:     1000,
   571  			ModTime:  time.Unix(0, 0),
   572  			Format:   FormatGNU,
   573  		}},
   574  	}, {
   575  		// Generated by Go, works on BSD tar v3.1.2 and GNU tar v.1.27.1.
   576  		file: "testdata/pax-nil-sparse-data.tar",
   577  		headers: []*Header{{
   578  			Name:     "sparse.db",
   579  			Typeflag: TypeReg,
   580  			Size:     1000,
   581  			ModTime:  time.Unix(0, 0),
   582  			PAXRecords: map[string]string{
   583  				"size":                "1512",
   584  				"GNU.sparse.major":    "1",
   585  				"GNU.sparse.minor":    "0",
   586  				"GNU.sparse.realsize": "1000",
   587  				"GNU.sparse.name":     "sparse.db",
   588  			},
   589  			Format: FormatPAX,
   590  		}},
   591  	}, {
   592  		// Generated by Go, works on BSD tar v3.1.2 and GNU tar v.1.27.1.
   593  		file: "testdata/pax-nil-sparse-hole.tar",
   594  		headers: []*Header{{
   595  			Name:     "sparse.db",
   596  			Typeflag: TypeReg,
   597  			Size:     1000,
   598  			ModTime:  time.Unix(0, 0),
   599  			PAXRecords: map[string]string{
   600  				"size":                "512",
   601  				"GNU.sparse.major":    "1",
   602  				"GNU.sparse.minor":    "0",
   603  				"GNU.sparse.realsize": "1000",
   604  				"GNU.sparse.name":     "sparse.db",
   605  			},
   606  			Format: FormatPAX,
   607  		}},
   608  	}, {
   609  		file: "testdata/trailing-slash.tar",
   610  		headers: []*Header{{
   611  			Typeflag: TypeDir,
   612  			Name:     strings.Repeat("123456789/", 30),
   613  			ModTime:  time.Unix(0, 0),
   614  			PAXRecords: map[string]string{
   615  				"path": strings.Repeat("123456789/", 30),
   616  			},
   617  			Format: FormatPAX,
   618  		}},
   619  	}}
   620  
   621  	for _, v := range vectors {
   622  		t.Run(path.Base(v.file), func(t *testing.T) {
   623  			f, err := os.Open(v.file)
   624  			if err != nil {
   625  				t.Fatalf("unexpected error: %v", err)
   626  			}
   627  			defer f.Close()
   628  
   629  			// Capture all headers and checksums.
   630  			var (
   631  				tr      = NewReader(f)
   632  				hdrs    []*Header
   633  				chksums []string
   634  				rdbuf   = make([]byte, 8)
   635  			)
   636  			for {
   637  				var hdr *Header
   638  				hdr, err = tr.Next()
   639  				if err != nil {
   640  					if err == io.EOF {
   641  						err = nil // Expected error
   642  					}
   643  					break
   644  				}
   645  				hdrs = append(hdrs, hdr)
   646  
   647  				if v.chksums == nil {
   648  					continue
   649  				}
   650  				h := md5.New()
   651  				_, err = io.CopyBuffer(h, tr, rdbuf) // Effectively an incremental read
   652  				if err != nil {
   653  					break
   654  				}
   655  				chksums = append(chksums, fmt.Sprintf("%x", h.Sum(nil)))
   656  			}
   657  
   658  			for i, hdr := range hdrs {
   659  				if i >= len(v.headers) {
   660  					t.Fatalf("entry %d: unexpected header:\ngot %+v", i, *hdr)
   661  					continue
   662  				}
   663  				if !reflect.DeepEqual(*hdr, *v.headers[i]) {
   664  					t.Fatalf("entry %d: incorrect header:\ngot  %+v\nwant %+v", i, *hdr, *v.headers[i])
   665  				}
   666  			}
   667  			if len(hdrs) != len(v.headers) {
   668  				t.Fatalf("got %d headers, want %d headers", len(hdrs), len(v.headers))
   669  			}
   670  
   671  			for i, sum := range chksums {
   672  				if i >= len(v.chksums) {
   673  					t.Fatalf("entry %d: unexpected sum: got %s", i, sum)
   674  					continue
   675  				}
   676  				if sum != v.chksums[i] {
   677  					t.Fatalf("entry %d: incorrect checksum: got %s, want %s", i, sum, v.chksums[i])
   678  				}
   679  			}
   680  
   681  			if err != v.err {
   682  				t.Fatalf("unexpected error: got %v, want %v", err, v.err)
   683  			}
   684  			f.Close()
   685  		})
   686  	}
   687  }
   688  
   689  func TestPartialRead(t *testing.T) {
   690  	type testCase struct {
   691  		cnt    int    // Number of bytes to read
   692  		output string // Expected value of string read
   693  	}
   694  	vectors := []struct {
   695  		file  string
   696  		cases []testCase
   697  	}{{
   698  		file: "testdata/gnu.tar",
   699  		cases: []testCase{
   700  			{4, "Kilt"},
   701  			{6, "Google"},
   702  		},
   703  	}, {
   704  		file: "testdata/sparse-formats.tar",
   705  		cases: []testCase{
   706  			{2, "\x00G"},
   707  			{4, "\x00G\x00o"},
   708  			{6, "\x00G\x00o\x00G"},
   709  			{8, "\x00G\x00o\x00G\x00o"},
   710  			{4, "end\n"},
   711  		},
   712  	}}
   713  
   714  	for _, v := range vectors {
   715  		t.Run(path.Base(v.file), func(t *testing.T) {
   716  			f, err := os.Open(v.file)
   717  			if err != nil {
   718  				t.Fatalf("Open() error: %v", err)
   719  			}
   720  			defer f.Close()
   721  
   722  			tr := NewReader(f)
   723  			for i, tc := range v.cases {
   724  				hdr, err := tr.Next()
   725  				if err != nil || hdr == nil {
   726  					t.Fatalf("entry %d, Next(): got %v, want %v", i, err, nil)
   727  				}
   728  				buf := make([]byte, tc.cnt)
   729  				if _, err := io.ReadFull(tr, buf); err != nil {
   730  					t.Fatalf("entry %d, ReadFull(): got %v, want %v", i, err, nil)
   731  				}
   732  				if string(buf) != tc.output {
   733  					t.Fatalf("entry %d, ReadFull(): got %q, want %q", i, string(buf), tc.output)
   734  				}
   735  			}
   736  
   737  			if _, err := tr.Next(); err != io.EOF {
   738  				t.Fatalf("Next(): got %v, want EOF", err)
   739  			}
   740  		})
   741  	}
   742  }
   743  
   744  func TestUninitializedRead(t *testing.T) {
   745  	f, err := os.Open("testdata/gnu.tar")
   746  	if err != nil {
   747  		t.Fatalf("Unexpected error: %v", err)
   748  	}
   749  	defer f.Close()
   750  
   751  	tr := NewReader(f)
   752  	_, err = tr.Read([]byte{})
   753  	if err == nil || err != io.EOF {
   754  		t.Errorf("Unexpected error: %v, wanted %v", err, io.EOF)
   755  	}
   756  
   757  }
   758  
   759  type reader struct{ io.Reader }
   760  type readSeeker struct{ io.ReadSeeker }
   761  type readBadSeeker struct{ io.ReadSeeker }
   762  
   763  func (rbs *readBadSeeker) Seek(int64, int) (int64, error) { return 0, fmt.Errorf("illegal seek") }
   764  
   765  // TestReadTruncation test the ending condition on various truncated files and
   766  // that truncated files are still detected even if the underlying io.Reader
   767  // satisfies io.Seeker.
   768  func TestReadTruncation(t *testing.T) {
   769  	var ss []string
   770  	for _, p := range []string{
   771  		"testdata/gnu.tar",
   772  		"testdata/ustar-file-reg.tar",
   773  		"testdata/pax-path-hdr.tar",
   774  		"testdata/sparse-formats.tar",
   775  	} {
   776  		buf, err := ioutil.ReadFile(p)
   777  		if err != nil {
   778  			t.Fatalf("unexpected error: %v", err)
   779  		}
   780  		ss = append(ss, string(buf))
   781  	}
   782  
   783  	data1, data2, pax, sparse := ss[0], ss[1], ss[2], ss[3]
   784  	data2 += strings.Repeat("\x00", 10*512)
   785  	trash := strings.Repeat("garbage ", 64) // Exactly 512 bytes
   786  
   787  	vectors := []struct {
   788  		input string // Input stream
   789  		cnt   int    // Expected number of headers read
   790  		err   error  // Expected error outcome
   791  	}{
   792  		{"", 0, io.EOF}, // Empty file is a "valid" tar file
   793  		{data1[:511], 0, io.ErrUnexpectedEOF},
   794  		{data1[:512], 1, io.ErrUnexpectedEOF},
   795  		{data1[:1024], 1, io.EOF},
   796  		{data1[:1536], 2, io.ErrUnexpectedEOF},
   797  		{data1[:2048], 2, io.EOF},
   798  		{data1, 2, io.EOF},
   799  		{data1[:2048] + data2[:1536], 3, io.EOF},
   800  		{data2[:511], 0, io.ErrUnexpectedEOF},
   801  		{data2[:512], 1, io.ErrUnexpectedEOF},
   802  		{data2[:1195], 1, io.ErrUnexpectedEOF},
   803  		{data2[:1196], 1, io.EOF}, // Exact end of data and start of padding
   804  		{data2[:1200], 1, io.EOF},
   805  		{data2[:1535], 1, io.EOF},
   806  		{data2[:1536], 1, io.EOF}, // Exact end of padding
   807  		{data2[:1536] + trash[:1], 1, io.ErrUnexpectedEOF},
   808  		{data2[:1536] + trash[:511], 1, io.ErrUnexpectedEOF},
   809  		{data2[:1536] + trash, 1, ErrHeader},
   810  		{data2[:2048], 1, io.EOF}, // Exactly 1 empty block
   811  		{data2[:2048] + trash[:1], 1, io.ErrUnexpectedEOF},
   812  		{data2[:2048] + trash[:511], 1, io.ErrUnexpectedEOF},
   813  		{data2[:2048] + trash, 1, ErrHeader},
   814  		{data2[:2560], 1, io.EOF}, // Exactly 2 empty blocks (normal end-of-stream)
   815  		{data2[:2560] + trash[:1], 1, io.EOF},
   816  		{data2[:2560] + trash[:511], 1, io.EOF},
   817  		{data2[:2560] + trash, 1, io.EOF},
   818  		{data2[:3072], 1, io.EOF},
   819  		{pax, 0, io.EOF}, // PAX header without data is a "valid" tar file
   820  		{pax + trash[:1], 0, io.ErrUnexpectedEOF},
   821  		{pax + trash[:511], 0, io.ErrUnexpectedEOF},
   822  		{sparse[:511], 0, io.ErrUnexpectedEOF},
   823  		{sparse[:512], 0, io.ErrUnexpectedEOF},
   824  		{sparse[:3584], 1, io.EOF},
   825  		{sparse[:9200], 1, io.EOF}, // Terminate in padding of sparse header
   826  		{sparse[:9216], 1, io.EOF},
   827  		{sparse[:9728], 2, io.ErrUnexpectedEOF},
   828  		{sparse[:10240], 2, io.EOF},
   829  		{sparse[:11264], 2, io.ErrUnexpectedEOF},
   830  		{sparse, 5, io.EOF},
   831  		{sparse + trash, 5, io.EOF},
   832  	}
   833  
   834  	for i, v := range vectors {
   835  		for j := 0; j < 6; j++ {
   836  			var tr *Reader
   837  			var s1, s2 string
   838  
   839  			switch j {
   840  			case 0:
   841  				tr = NewReader(&reader{strings.NewReader(v.input)})
   842  				s1, s2 = "io.Reader", "auto"
   843  			case 1:
   844  				tr = NewReader(&reader{strings.NewReader(v.input)})
   845  				s1, s2 = "io.Reader", "manual"
   846  			case 2:
   847  				tr = NewReader(&readSeeker{strings.NewReader(v.input)})
   848  				s1, s2 = "io.ReadSeeker", "auto"
   849  			case 3:
   850  				tr = NewReader(&readSeeker{strings.NewReader(v.input)})
   851  				s1, s2 = "io.ReadSeeker", "manual"
   852  			case 4:
   853  				tr = NewReader(&readBadSeeker{strings.NewReader(v.input)})
   854  				s1, s2 = "ReadBadSeeker", "auto"
   855  			case 5:
   856  				tr = NewReader(&readBadSeeker{strings.NewReader(v.input)})
   857  				s1, s2 = "ReadBadSeeker", "manual"
   858  			}
   859  
   860  			var cnt int
   861  			var err error
   862  			for {
   863  				if _, err = tr.Next(); err != nil {
   864  					break
   865  				}
   866  				cnt++
   867  				if s2 == "manual" {
   868  					if _, err = tr.writeTo(ioutil.Discard); err != nil {
   869  						break
   870  					}
   871  				}
   872  			}
   873  			if err != v.err {
   874  				t.Errorf("test %d, NewReader(%s) with %s discard: got %v, want %v",
   875  					i, s1, s2, err, v.err)
   876  			}
   877  			if cnt != v.cnt {
   878  				t.Errorf("test %d, NewReader(%s) with %s discard: got %d headers, want %d headers",
   879  					i, s1, s2, cnt, v.cnt)
   880  			}
   881  		}
   882  	}
   883  }
   884  
   885  // TestReadHeaderOnly tests that Reader does not attempt to read special
   886  // header-only files.
   887  func TestReadHeaderOnly(t *testing.T) {
   888  	f, err := os.Open("testdata/hdr-only.tar")
   889  	if err != nil {
   890  		t.Fatalf("unexpected error: %v", err)
   891  	}
   892  	defer f.Close()
   893  
   894  	var hdrs []*Header
   895  	tr := NewReader(f)
   896  	for {
   897  		hdr, err := tr.Next()
   898  		if err == io.EOF {
   899  			break
   900  		}
   901  		if err != nil {
   902  			t.Errorf("Next(): got %v, want %v", err, nil)
   903  			continue
   904  		}
   905  		hdrs = append(hdrs, hdr)
   906  
   907  		// If a special flag, we should read nothing.
   908  		cnt, _ := io.ReadFull(tr, []byte{0})
   909  		if cnt > 0 && hdr.Typeflag != TypeReg {
   910  			t.Errorf("ReadFull(...): got %d bytes, want 0 bytes", cnt)
   911  		}
   912  	}
   913  
   914  	// File is crafted with 16 entries. The later 8 are identical to the first
   915  	// 8 except that the size is set.
   916  	if len(hdrs) != 16 {
   917  		t.Fatalf("len(hdrs): got %d, want %d", len(hdrs), 16)
   918  	}
   919  	for i := 0; i < 8; i++ {
   920  		hdr1, hdr2 := hdrs[i+0], hdrs[i+8]
   921  		hdr1.Size, hdr2.Size = 0, 0
   922  		if !reflect.DeepEqual(*hdr1, *hdr2) {
   923  			t.Errorf("incorrect header:\ngot  %+v\nwant %+v", *hdr1, *hdr2)
   924  		}
   925  	}
   926  }
   927  
   928  func TestMergePAX(t *testing.T) {
   929  	vectors := []struct {
   930  		in   map[string]string
   931  		want *Header
   932  		ok   bool
   933  	}{{
   934  		in: map[string]string{
   935  			"path":  "a/b/c",
   936  			"uid":   "1000",
   937  			"mtime": "1350244992.023960108",
   938  		},
   939  		want: &Header{
   940  			Name:    "a/b/c",
   941  			Uid:     1000,
   942  			ModTime: time.Unix(1350244992, 23960108),
   943  			PAXRecords: map[string]string{
   944  				"path":  "a/b/c",
   945  				"uid":   "1000",
   946  				"mtime": "1350244992.023960108",
   947  			},
   948  		},
   949  		ok: true,
   950  	}, {
   951  		in: map[string]string{
   952  			"gid": "gtgergergersagersgers",
   953  		},
   954  		ok: false,
   955  	}, {
   956  		in: map[string]string{
   957  			"missing":          "missing",
   958  			"SCHILY.xattr.key": "value",
   959  		},
   960  		want: &Header{
   961  			Xattrs: map[string]string{"key": "value"},
   962  			PAXRecords: map[string]string{
   963  				"missing":          "missing",
   964  				"SCHILY.xattr.key": "value",
   965  			},
   966  		},
   967  		ok: true,
   968  	}}
   969  
   970  	for i, v := range vectors {
   971  		got := new(Header)
   972  		err := mergePAX(got, v.in)
   973  		if v.ok && !reflect.DeepEqual(*got, *v.want) {
   974  			t.Errorf("test %d, mergePAX(...):\ngot  %+v\nwant %+v", i, *got, *v.want)
   975  		}
   976  		if ok := err == nil; ok != v.ok {
   977  			t.Errorf("test %d, mergePAX(...): got %v, want %v", i, ok, v.ok)
   978  		}
   979  	}
   980  }
   981  
   982  func TestParsePAX(t *testing.T) {
   983  	vectors := []struct {
   984  		in   string
   985  		want map[string]string
   986  		ok   bool
   987  	}{
   988  		{"", nil, true},
   989  		{"6 k=1\n", map[string]string{"k": "1"}, true},
   990  		{"10 a=name\n", map[string]string{"a": "name"}, true},
   991  		{"9 a=name\n", map[string]string{"a": "name"}, true},
   992  		{"30 mtime=1350244992.023960108\n", map[string]string{"mtime": "1350244992.023960108"}, true},
   993  		{"3 somelongkey=\n", nil, false},
   994  		{"50 tooshort=\n", nil, false},
   995  		{"13 key1=haha\n13 key2=nana\n13 key3=kaka\n",
   996  			map[string]string{"key1": "haha", "key2": "nana", "key3": "kaka"}, true},
   997  		{"13 key1=val1\n13 key2=val2\n8 key1=\n",
   998  			map[string]string{"key1": "", "key2": "val2"}, true},
   999  		{"22 GNU.sparse.size=10\n26 GNU.sparse.numblocks=2\n" +
  1000  			"23 GNU.sparse.offset=1\n25 GNU.sparse.numbytes=2\n" +
  1001  			"23 GNU.sparse.offset=3\n25 GNU.sparse.numbytes=4\n",
  1002  			map[string]string{paxGNUSparseSize: "10", paxGNUSparseNumBlocks: "2", paxGNUSparseMap: "1,2,3,4"}, true},
  1003  		{"22 GNU.sparse.size=10\n26 GNU.sparse.numblocks=1\n" +
  1004  			"25 GNU.sparse.numbytes=2\n23 GNU.sparse.offset=1\n",
  1005  			nil, false},
  1006  		{"22 GNU.sparse.size=10\n26 GNU.sparse.numblocks=1\n" +
  1007  			"25 GNU.sparse.offset=1,2\n25 GNU.sparse.numbytes=2\n",
  1008  			nil, false},
  1009  	}
  1010  
  1011  	for i, v := range vectors {
  1012  		r := strings.NewReader(v.in)
  1013  		got, err := parsePAX(r)
  1014  		if !reflect.DeepEqual(got, v.want) && !(len(got) == 0 && len(v.want) == 0) {
  1015  			t.Errorf("test %d, parsePAX():\ngot  %v\nwant %v", i, got, v.want)
  1016  		}
  1017  		if ok := err == nil; ok != v.ok {
  1018  			t.Errorf("test %d, parsePAX(): got %v, want %v", i, ok, v.ok)
  1019  		}
  1020  	}
  1021  }
  1022  
  1023  func TestReadOldGNUSparseMap(t *testing.T) {
  1024  	populateSparseMap := func(sa sparseArray, sps []string) []string {
  1025  		for i := 0; len(sps) > 0 && i < sa.MaxEntries(); i++ {
  1026  			copy(sa.Entry(i), sps[0])
  1027  			sps = sps[1:]
  1028  		}
  1029  		if len(sps) > 0 {
  1030  			copy(sa.IsExtended(), "\x80")
  1031  		}
  1032  		return sps
  1033  	}
  1034  
  1035  	makeInput := func(format Format, size string, sps ...string) (out []byte) {
  1036  		// Write the initial GNU header.
  1037  		var blk block
  1038  		gnu := blk.GNU()
  1039  		sparse := gnu.Sparse()
  1040  		copy(gnu.RealSize(), size)
  1041  		sps = populateSparseMap(sparse, sps)
  1042  		if format != FormatUnknown {
  1043  			blk.SetFormat(format)
  1044  		}
  1045  		out = append(out, blk[:]...)
  1046  
  1047  		// Write extended sparse blocks.
  1048  		for len(sps) > 0 {
  1049  			var blk block
  1050  			sps = populateSparseMap(blk.Sparse(), sps)
  1051  			out = append(out, blk[:]...)
  1052  		}
  1053  		return out
  1054  	}
  1055  
  1056  	makeSparseStrings := func(sp []sparseEntry) (out []string) {
  1057  		var f formatter
  1058  		for _, s := range sp {
  1059  			var b [24]byte
  1060  			f.formatNumeric(b[:12], s.Offset)
  1061  			f.formatNumeric(b[12:], s.Length)
  1062  			out = append(out, string(b[:]))
  1063  		}
  1064  		return out
  1065  	}
  1066  
  1067  	vectors := []struct {
  1068  		input    []byte
  1069  		wantMap  sparseDatas
  1070  		wantSize int64
  1071  		wantErr  error
  1072  	}{{
  1073  		input:   makeInput(FormatUnknown, ""),
  1074  		wantErr: ErrHeader,
  1075  	}, {
  1076  		input:    makeInput(FormatGNU, "1234", "fewa"),
  1077  		wantSize: 01234,
  1078  		wantErr:  ErrHeader,
  1079  	}, {
  1080  		input:    makeInput(FormatGNU, "0031"),
  1081  		wantSize: 031,
  1082  	}, {
  1083  		input:   makeInput(FormatGNU, "80"),
  1084  		wantErr: ErrHeader,
  1085  	}, {
  1086  		input: makeInput(FormatGNU, "1234",
  1087  			makeSparseStrings(sparseDatas{{0, 0}, {1, 1}})...),
  1088  		wantMap:  sparseDatas{{0, 0}, {1, 1}},
  1089  		wantSize: 01234,
  1090  	}, {
  1091  		input: makeInput(FormatGNU, "1234",
  1092  			append(makeSparseStrings(sparseDatas{{0, 0}, {1, 1}}), []string{"", "blah"}...)...),
  1093  		wantMap:  sparseDatas{{0, 0}, {1, 1}},
  1094  		wantSize: 01234,
  1095  	}, {
  1096  		input: makeInput(FormatGNU, "3333",
  1097  			makeSparseStrings(sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}})...),
  1098  		wantMap:  sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}},
  1099  		wantSize: 03333,
  1100  	}, {
  1101  		input: makeInput(FormatGNU, "",
  1102  			append(append(
  1103  				makeSparseStrings(sparseDatas{{0, 1}, {2, 1}}),
  1104  				[]string{"", ""}...),
  1105  				makeSparseStrings(sparseDatas{{4, 1}, {6, 1}})...)...),
  1106  		wantMap: sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}},
  1107  	}, {
  1108  		input: makeInput(FormatGNU, "",
  1109  			makeSparseStrings(sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}, {8, 1}, {10, 1}})...)[:blockSize],
  1110  		wantErr: io.ErrUnexpectedEOF,
  1111  	}, {
  1112  		input: makeInput(FormatGNU, "",
  1113  			makeSparseStrings(sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}, {8, 1}, {10, 1}})...)[:3*blockSize/2],
  1114  		wantErr: io.ErrUnexpectedEOF,
  1115  	}, {
  1116  		input: makeInput(FormatGNU, "",
  1117  			makeSparseStrings(sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}, {8, 1}, {10, 1}})...),
  1118  		wantMap: sparseDatas{{0, 1}, {2, 1}, {4, 1}, {6, 1}, {8, 1}, {10, 1}},
  1119  	}, {
  1120  		input: makeInput(FormatGNU, "",
  1121  			makeSparseStrings(sparseDatas{{10 << 30, 512}, {20 << 30, 512}})...),
  1122  		wantMap: sparseDatas{{10 << 30, 512}, {20 << 30, 512}},
  1123  	}}
  1124  
  1125  	for i, v := range vectors {
  1126  		var blk block
  1127  		var hdr Header
  1128  		v.input = v.input[copy(blk[:], v.input):]
  1129  		tr := Reader{r: bytes.NewReader(v.input)}
  1130  		got, err := tr.readOldGNUSparseMap(&hdr, &blk)
  1131  		if !equalSparseEntries(got, v.wantMap) {
  1132  			t.Errorf("test %d, readOldGNUSparseMap(): got %v, want %v", i, got, v.wantMap)
  1133  		}
  1134  		if err != v.wantErr {
  1135  			t.Errorf("test %d, readOldGNUSparseMap() = %v, want %v", i, err, v.wantErr)
  1136  		}
  1137  		if hdr.Size != v.wantSize {
  1138  			t.Errorf("test %d, Header.Size = %d, want %d", i, hdr.Size, v.wantSize)
  1139  		}
  1140  	}
  1141  }
  1142  
  1143  func TestReadGNUSparsePAXHeaders(t *testing.T) {
  1144  	padInput := func(s string) string {
  1145  		return s + string(zeroBlock[:blockPadding(int64(len(s)))])
  1146  	}
  1147  
  1148  	vectors := []struct {
  1149  		inputData string
  1150  		inputHdrs map[string]string
  1151  		wantMap   sparseDatas
  1152  		wantSize  int64
  1153  		wantName  string
  1154  		wantErr   error
  1155  	}{{
  1156  		inputHdrs: nil,
  1157  		wantErr:   nil,
  1158  	}, {
  1159  		inputHdrs: map[string]string{
  1160  			paxGNUSparseNumBlocks: strconv.FormatInt(math.MaxInt64, 10),
  1161  			paxGNUSparseMap:       "0,1,2,3",
  1162  		},
  1163  		wantErr: ErrHeader,
  1164  	}, {
  1165  		inputHdrs: map[string]string{
  1166  			paxGNUSparseNumBlocks: "4\x00",
  1167  			paxGNUSparseMap:       "0,1,2,3",
  1168  		},
  1169  		wantErr: ErrHeader,
  1170  	}, {
  1171  		inputHdrs: map[string]string{
  1172  			paxGNUSparseNumBlocks: "4",
  1173  			paxGNUSparseMap:       "0,1,2,3",
  1174  		},
  1175  		wantErr: ErrHeader,
  1176  	}, {
  1177  		inputHdrs: map[string]string{
  1178  			paxGNUSparseNumBlocks: "2",
  1179  			paxGNUSparseMap:       "0,1,2,3",
  1180  		},
  1181  		wantMap: sparseDatas{{0, 1}, {2, 3}},
  1182  	}, {
  1183  		inputHdrs: map[string]string{
  1184  			paxGNUSparseNumBlocks: "2",
  1185  			paxGNUSparseMap:       "0, 1,2,3",
  1186  		},
  1187  		wantErr: ErrHeader,
  1188  	}, {
  1189  		inputHdrs: map[string]string{
  1190  			paxGNUSparseNumBlocks: "2",
  1191  			paxGNUSparseMap:       "0,1,02,3",
  1192  			paxGNUSparseRealSize:  "4321",
  1193  		},
  1194  		wantMap:  sparseDatas{{0, 1}, {2, 3}},
  1195  		wantSize: 4321,
  1196  	}, {
  1197  		inputHdrs: map[string]string{
  1198  			paxGNUSparseNumBlocks: "2",
  1199  			paxGNUSparseMap:       "0,one1,2,3",
  1200  		},
  1201  		wantErr: ErrHeader,
  1202  	}, {
  1203  		inputHdrs: map[string]string{
  1204  			paxGNUSparseMajor:     "0",
  1205  			paxGNUSparseMinor:     "0",
  1206  			paxGNUSparseNumBlocks: "2",
  1207  			paxGNUSparseMap:       "0,1,2,3",
  1208  			paxGNUSparseSize:      "1234",
  1209  			paxGNUSparseRealSize:  "4321",
  1210  			paxGNUSparseName:      "realname",
  1211  		},
  1212  		wantMap:  sparseDatas{{0, 1}, {2, 3}},
  1213  		wantSize: 1234,
  1214  		wantName: "realname",
  1215  	}, {
  1216  		inputHdrs: map[string]string{
  1217  			paxGNUSparseMajor:     "0",
  1218  			paxGNUSparseMinor:     "0",
  1219  			paxGNUSparseNumBlocks: "1",
  1220  			paxGNUSparseMap:       "10737418240,512",
  1221  			paxGNUSparseSize:      "10737418240",
  1222  			paxGNUSparseName:      "realname",
  1223  		},
  1224  		wantMap:  sparseDatas{{10737418240, 512}},
  1225  		wantSize: 10737418240,
  1226  		wantName: "realname",
  1227  	}, {
  1228  		inputHdrs: map[string]string{
  1229  			paxGNUSparseMajor:     "0",
  1230  			paxGNUSparseMinor:     "0",
  1231  			paxGNUSparseNumBlocks: "0",
  1232  			paxGNUSparseMap:       "",
  1233  		},
  1234  		wantMap: sparseDatas{},
  1235  	}, {
  1236  		inputHdrs: map[string]string{
  1237  			paxGNUSparseMajor:     "0",
  1238  			paxGNUSparseMinor:     "1",
  1239  			paxGNUSparseNumBlocks: "4",
  1240  			paxGNUSparseMap:       "0,5,10,5,20,5,30,5",
  1241  		},
  1242  		wantMap: sparseDatas{{0, 5}, {10, 5}, {20, 5}, {30, 5}},
  1243  	}, {
  1244  		inputHdrs: map[string]string{
  1245  			paxGNUSparseMajor:     "1",
  1246  			paxGNUSparseMinor:     "0",
  1247  			paxGNUSparseNumBlocks: "4",
  1248  			paxGNUSparseMap:       "0,5,10,5,20,5,30,5",
  1249  		},
  1250  		wantErr: io.ErrUnexpectedEOF,
  1251  	}, {
  1252  		inputData: padInput("0\n"),
  1253  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1254  		wantMap:   sparseDatas{},
  1255  	}, {
  1256  		inputData: padInput("0\n")[:blockSize-1] + "#",
  1257  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1258  		wantMap:   sparseDatas{},
  1259  	}, {
  1260  		inputData: padInput("0"),
  1261  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1262  		wantErr:   io.ErrUnexpectedEOF,
  1263  	}, {
  1264  		inputData: padInput("ab\n"),
  1265  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1266  		wantErr:   ErrHeader,
  1267  	}, {
  1268  		inputData: padInput("1\n2\n3\n"),
  1269  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1270  		wantMap:   sparseDatas{{2, 3}},
  1271  	}, {
  1272  		inputData: padInput("1\n2\n"),
  1273  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1274  		wantErr:   io.ErrUnexpectedEOF,
  1275  	}, {
  1276  		inputData: padInput("1\n2\n\n"),
  1277  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1278  		wantErr:   ErrHeader,
  1279  	}, {
  1280  		inputData: string(zeroBlock[:]) + padInput("0\n"),
  1281  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1282  		wantErr:   ErrHeader,
  1283  	}, {
  1284  		inputData: strings.Repeat("0", blockSize) + padInput("1\n5\n1\n"),
  1285  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1286  		wantMap:   sparseDatas{{5, 1}},
  1287  	}, {
  1288  		inputData: padInput(fmt.Sprintf("%d\n", int64(math.MaxInt64))),
  1289  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1290  		wantErr:   ErrHeader,
  1291  	}, {
  1292  		inputData: padInput(strings.Repeat("0", 300) + "1\n" + strings.Repeat("0", 1000) + "5\n" + strings.Repeat("0", 800) + "2\n"),
  1293  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1294  		wantMap:   sparseDatas{{5, 2}},
  1295  	}, {
  1296  		inputData: padInput("2\n10737418240\n512\n21474836480\n512\n"),
  1297  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1298  		wantMap:   sparseDatas{{10737418240, 512}, {21474836480, 512}},
  1299  	}, {
  1300  		inputData: padInput("100\n" + func() string {
  1301  			var ss []string
  1302  			for i := 0; i < 100; i++ {
  1303  				ss = append(ss, fmt.Sprintf("%d\n%d\n", int64(i)<<30, 512))
  1304  			}
  1305  			return strings.Join(ss, "")
  1306  		}()),
  1307  		inputHdrs: map[string]string{paxGNUSparseMajor: "1", paxGNUSparseMinor: "0"},
  1308  		wantMap: func() (spd sparseDatas) {
  1309  			for i := 0; i < 100; i++ {
  1310  				spd = append(spd, sparseEntry{int64(i) << 30, 512})
  1311  			}
  1312  			return spd
  1313  		}(),
  1314  	}}
  1315  
  1316  	for i, v := range vectors {
  1317  		var hdr Header
  1318  		hdr.PAXRecords = v.inputHdrs
  1319  		r := strings.NewReader(v.inputData + "#") // Add canary byte
  1320  		tr := Reader{curr: &regFileReader{r, int64(r.Len())}}
  1321  		got, err := tr.readGNUSparsePAXHeaders(&hdr)
  1322  		if !equalSparseEntries(got, v.wantMap) {
  1323  			t.Errorf("test %d, readGNUSparsePAXHeaders(): got %v, want %v", i, got, v.wantMap)
  1324  		}
  1325  		if err != v.wantErr {
  1326  			t.Errorf("test %d, readGNUSparsePAXHeaders() = %v, want %v", i, err, v.wantErr)
  1327  		}
  1328  		if hdr.Size != v.wantSize {
  1329  			t.Errorf("test %d, Header.Size = %d, want %d", i, hdr.Size, v.wantSize)
  1330  		}
  1331  		if hdr.Name != v.wantName {
  1332  			t.Errorf("test %d, Header.Name = %s, want %s", i, hdr.Name, v.wantName)
  1333  		}
  1334  		if v.wantErr == nil && r.Len() == 0 {
  1335  			t.Errorf("test %d, canary byte unexpectedly consumed", i)
  1336  		}
  1337  	}
  1338  }
  1339  
  1340  // testNonEmptyReader wraps an io.Reader and ensures that
  1341  // Read is never called with an empty buffer.
  1342  type testNonEmptyReader struct{ io.Reader }
  1343  
  1344  func (r testNonEmptyReader) Read(b []byte) (int, error) {
  1345  	if len(b) == 0 {
  1346  		return 0, errors.New("unexpected empty Read call")
  1347  	}
  1348  	return r.Reader.Read(b)
  1349  }
  1350  
  1351  func TestFileReader(t *testing.T) {
  1352  	type (
  1353  		testRead struct { // Read(cnt) == (wantStr, wantErr)
  1354  			cnt     int
  1355  			wantStr string
  1356  			wantErr error
  1357  		}
  1358  		testWriteTo struct { // WriteTo(testFile{ops}) == (wantCnt, wantErr)
  1359  			ops     fileOps
  1360  			wantCnt int64
  1361  			wantErr error
  1362  		}
  1363  		testRemaining struct { // LogicalRemaining() == wantLCnt, PhysicalRemaining() == wantPCnt
  1364  			wantLCnt int64
  1365  			wantPCnt int64
  1366  		}
  1367  		testFnc interface{} // testRead | testWriteTo | testRemaining
  1368  	)
  1369  
  1370  	type (
  1371  		makeReg struct {
  1372  			str  string
  1373  			size int64
  1374  		}
  1375  		makeSparse struct {
  1376  			makeReg makeReg
  1377  			spd     sparseDatas
  1378  			size    int64
  1379  		}
  1380  		fileMaker interface{} // makeReg | makeSparse
  1381  	)
  1382  
  1383  	vectors := []struct {
  1384  		maker fileMaker
  1385  		tests []testFnc
  1386  	}{{
  1387  		maker: makeReg{"", 0},
  1388  		tests: []testFnc{
  1389  			testRemaining{0, 0},
  1390  			testRead{0, "", io.EOF},
  1391  			testRead{1, "", io.EOF},
  1392  			testWriteTo{nil, 0, nil},
  1393  			testRemaining{0, 0},
  1394  		},
  1395  	}, {
  1396  		maker: makeReg{"", 1},
  1397  		tests: []testFnc{
  1398  			testRemaining{1, 1},
  1399  			testRead{5, "", io.ErrUnexpectedEOF},
  1400  			testWriteTo{nil, 0, io.ErrUnexpectedEOF},
  1401  			testRemaining{1, 1},
  1402  		},
  1403  	}, {
  1404  		maker: makeReg{"hello", 5},
  1405  		tests: []testFnc{
  1406  			testRemaining{5, 5},
  1407  			testRead{5, "hello", io.EOF},
  1408  			testRemaining{0, 0},
  1409  		},
  1410  	}, {
  1411  		maker: makeReg{"hello, world", 50},
  1412  		tests: []testFnc{
  1413  			testRemaining{50, 50},
  1414  			testRead{7, "hello, ", nil},
  1415  			testRemaining{43, 43},
  1416  			testRead{5, "world", nil},
  1417  			testRemaining{38, 38},
  1418  			testWriteTo{nil, 0, io.ErrUnexpectedEOF},
  1419  			testRead{1, "", io.ErrUnexpectedEOF},
  1420  			testRemaining{38, 38},
  1421  		},
  1422  	}, {
  1423  		maker: makeReg{"hello, world", 5},
  1424  		tests: []testFnc{
  1425  			testRemaining{5, 5},
  1426  			testRead{0, "", nil},
  1427  			testRead{4, "hell", nil},
  1428  			testRemaining{1, 1},
  1429  			testWriteTo{fileOps{"o"}, 1, nil},
  1430  			testRemaining{0, 0},
  1431  			testWriteTo{nil, 0, nil},
  1432  			testRead{0, "", io.EOF},
  1433  		},
  1434  	}, {
  1435  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{0, 2}, {5, 3}}, 8},
  1436  		tests: []testFnc{
  1437  			testRemaining{8, 5},
  1438  			testRead{3, "ab\x00", nil},
  1439  			testRead{10, "\x00\x00cde", io.EOF},
  1440  			testRemaining{0, 0},
  1441  		},
  1442  	}, {
  1443  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{0, 2}, {5, 3}}, 8},
  1444  		tests: []testFnc{
  1445  			testRemaining{8, 5},
  1446  			testWriteTo{fileOps{"ab", int64(3), "cde"}, 8, nil},
  1447  			testRemaining{0, 0},
  1448  		},
  1449  	}, {
  1450  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{0, 2}, {5, 3}}, 10},
  1451  		tests: []testFnc{
  1452  			testRemaining{10, 5},
  1453  			testRead{100, "ab\x00\x00\x00cde\x00\x00", io.EOF},
  1454  			testRemaining{0, 0},
  1455  		},
  1456  	}, {
  1457  		maker: makeSparse{makeReg{"abc", 5}, sparseDatas{{0, 2}, {5, 3}}, 10},
  1458  		tests: []testFnc{
  1459  			testRemaining{10, 5},
  1460  			testRead{100, "ab\x00\x00\x00c", io.ErrUnexpectedEOF},
  1461  			testRemaining{4, 2},
  1462  		},
  1463  	}, {
  1464  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 2}}, 8},
  1465  		tests: []testFnc{
  1466  			testRemaining{8, 5},
  1467  			testRead{8, "\x00abc\x00\x00de", io.EOF},
  1468  			testRemaining{0, 0},
  1469  		},
  1470  	}, {
  1471  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 0}, {6, 0}, {6, 2}}, 8},
  1472  		tests: []testFnc{
  1473  			testRemaining{8, 5},
  1474  			testRead{8, "\x00abc\x00\x00de", io.EOF},
  1475  			testRemaining{0, 0},
  1476  		},
  1477  	}, {
  1478  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 0}, {6, 0}, {6, 2}}, 8},
  1479  		tests: []testFnc{
  1480  			testRemaining{8, 5},
  1481  			testWriteTo{fileOps{int64(1), "abc", int64(2), "de"}, 8, nil},
  1482  			testRemaining{0, 0},
  1483  		},
  1484  	}, {
  1485  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 2}}, 10},
  1486  		tests: []testFnc{
  1487  			testRead{100, "\x00abc\x00\x00de\x00\x00", io.EOF},
  1488  		},
  1489  	}, {
  1490  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 2}}, 10},
  1491  		tests: []testFnc{
  1492  			testWriteTo{fileOps{int64(1), "abc", int64(2), "de", int64(1), "\x00"}, 10, nil},
  1493  		},
  1494  	}, {
  1495  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 2}, {8, 0}, {8, 0}, {8, 0}, {8, 0}}, 10},
  1496  		tests: []testFnc{
  1497  			testRead{100, "\x00abc\x00\x00de\x00\x00", io.EOF},
  1498  		},
  1499  	}, {
  1500  		maker: makeSparse{makeReg{"", 0}, sparseDatas{}, 2},
  1501  		tests: []testFnc{
  1502  			testRead{100, "\x00\x00", io.EOF},
  1503  		},
  1504  	}, {
  1505  		maker: makeSparse{makeReg{"", 8}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1506  		tests: []testFnc{
  1507  			testRead{100, "\x00", io.ErrUnexpectedEOF},
  1508  		},
  1509  	}, {
  1510  		maker: makeSparse{makeReg{"ab", 2}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1511  		tests: []testFnc{
  1512  			testRead{100, "\x00ab", errMissData},
  1513  		},
  1514  	}, {
  1515  		maker: makeSparse{makeReg{"ab", 8}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1516  		tests: []testFnc{
  1517  			testRead{100, "\x00ab", io.ErrUnexpectedEOF},
  1518  		},
  1519  	}, {
  1520  		maker: makeSparse{makeReg{"abc", 3}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1521  		tests: []testFnc{
  1522  			testRead{100, "\x00abc\x00\x00", errMissData},
  1523  		},
  1524  	}, {
  1525  		maker: makeSparse{makeReg{"abc", 8}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1526  		tests: []testFnc{
  1527  			testRead{100, "\x00abc\x00\x00", io.ErrUnexpectedEOF},
  1528  		},
  1529  	}, {
  1530  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1531  		tests: []testFnc{
  1532  			testRead{100, "\x00abc\x00\x00de", errMissData},
  1533  		},
  1534  	}, {
  1535  		maker: makeSparse{makeReg{"abcde", 5}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1536  		tests: []testFnc{
  1537  			testWriteTo{fileOps{int64(1), "abc", int64(2), "de"}, 8, errMissData},
  1538  		},
  1539  	}, {
  1540  		maker: makeSparse{makeReg{"abcde", 8}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1541  		tests: []testFnc{
  1542  			testRead{100, "\x00abc\x00\x00de", io.ErrUnexpectedEOF},
  1543  		},
  1544  	}, {
  1545  		maker: makeSparse{makeReg{"abcdefghEXTRA", 13}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1546  		tests: []testFnc{
  1547  			testRemaining{15, 13},
  1548  			testRead{100, "\x00abc\x00\x00defgh\x00\x00\x00\x00", errUnrefData},
  1549  			testWriteTo{nil, 0, errUnrefData},
  1550  			testRemaining{0, 5},
  1551  		},
  1552  	}, {
  1553  		maker: makeSparse{makeReg{"abcdefghEXTRA", 13}, sparseDatas{{1, 3}, {6, 5}}, 15},
  1554  		tests: []testFnc{
  1555  			testRemaining{15, 13},
  1556  			testWriteTo{fileOps{int64(1), "abc", int64(2), "defgh", int64(4)}, 15, errUnrefData},
  1557  			testRead{100, "", errUnrefData},
  1558  			testRemaining{0, 5},
  1559  		},
  1560  	}}
  1561  
  1562  	for i, v := range vectors {
  1563  		var fr fileReader
  1564  		switch maker := v.maker.(type) {
  1565  		case makeReg:
  1566  			r := testNonEmptyReader{strings.NewReader(maker.str)}
  1567  			fr = &regFileReader{r, maker.size}
  1568  		case makeSparse:
  1569  			if !validateSparseEntries(maker.spd, maker.size) {
  1570  				t.Fatalf("invalid sparse map: %v", maker.spd)
  1571  			}
  1572  			sph := invertSparseEntries(maker.spd, maker.size)
  1573  			r := testNonEmptyReader{strings.NewReader(maker.makeReg.str)}
  1574  			fr = &regFileReader{r, maker.makeReg.size}
  1575  			fr = &sparseFileReader{fr, sph, 0}
  1576  		default:
  1577  			t.Fatalf("test %d, unknown make operation: %T", i, maker)
  1578  		}
  1579  
  1580  		for j, tf := range v.tests {
  1581  			switch tf := tf.(type) {
  1582  			case testRead:
  1583  				b := make([]byte, tf.cnt)
  1584  				n, err := fr.Read(b)
  1585  				if got := string(b[:n]); got != tf.wantStr || err != tf.wantErr {
  1586  					t.Errorf("test %d.%d, Read(%d):\ngot  (%q, %v)\nwant (%q, %v)", i, j, tf.cnt, got, err, tf.wantStr, tf.wantErr)
  1587  				}
  1588  			case testWriteTo:
  1589  				f := &testFile{ops: tf.ops}
  1590  				got, err := fr.WriteTo(f)
  1591  				if _, ok := err.(testError); ok {
  1592  					t.Errorf("test %d.%d, WriteTo(): %v", i, j, err)
  1593  				} else if got != tf.wantCnt || err != tf.wantErr {
  1594  					t.Errorf("test %d.%d, WriteTo() = (%d, %v), want (%d, %v)", i, j, got, err, tf.wantCnt, tf.wantErr)
  1595  				}
  1596  				if len(f.ops) > 0 {
  1597  					t.Errorf("test %d.%d, expected %d more operations", i, j, len(f.ops))
  1598  				}
  1599  			case testRemaining:
  1600  				if got := fr.LogicalRemaining(); got != tf.wantLCnt {
  1601  					t.Errorf("test %d.%d, LogicalRemaining() = %d, want %d", i, j, got, tf.wantLCnt)
  1602  				}
  1603  				if got := fr.PhysicalRemaining(); got != tf.wantPCnt {
  1604  					t.Errorf("test %d.%d, PhysicalRemaining() = %d, want %d", i, j, got, tf.wantPCnt)
  1605  				}
  1606  			default:
  1607  				t.Fatalf("test %d.%d, unknown test operation: %T", i, j, tf)
  1608  			}
  1609  		}
  1610  	}
  1611  }
  1612  

View as plain text