From 63072b771506770276847400afc47fa9a9260116 Mon Sep 17 00:00:00 2001 From: Nicolas Lepage <19571875+nlepage@users.noreply.github.com> Date: Mon, 20 Nov 2023 11:28:56 +0100 Subject: [PATCH] Add new tests using tar files from stdlib --- entry.go | 3 ++- fs.go | 3 ++- fs_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/entry.go b/entry.go index 1b5c189..c76b8c7 100644 --- a/entry.go +++ b/entry.go @@ -6,6 +6,7 @@ import ( "io" "io/fs" "sort" + "strings" "time" ) @@ -155,7 +156,7 @@ type entriesByName []fs.DirEntry var _ sort.Interface = entriesByName{} func (entries entriesByName) Less(i, j int) bool { - return entries[i].Name() < entries[j].Name() + return strings.Compare(entries[i].Name(), entries[j].Name()) < 0 } func (entries entriesByName) Len() int { diff --git a/fs.go b/fs.go index 1acaaeb..ce52085 100644 --- a/fs.go +++ b/fs.go @@ -7,6 +7,7 @@ import ( "io/fs" "path" "strings" + "unicode/utf8" ) const ( @@ -180,7 +181,7 @@ func (tfs *tarfs) Sub(dir string) (fs.FS, error) { } func (tfs *tarfs) get(op, path string) (entry, error) { - if !fs.ValidPath(path) { + if !fs.ValidPath(path) && utf8.ValidString(path) { return nil, newErr(op, path, fs.ErrInvalid) } diff --git a/fs_test.go b/fs_test.go index e32a0e8..09ed0dd 100644 --- a/fs_test.go +++ b/fs_test.go @@ -4,6 +4,8 @@ import ( "io" "io/fs" "os" + "path/filepath" + "runtime" "testing" "testing/fstest" @@ -429,3 +431,45 @@ func TestIgnoreGlobalHeader(t *testing.T) { err = fstest.TestFS(tfs, "bar", "dir1", "dir1/file11") require.NoError(err) } + +func TestVariousTarTypes(t *testing.T) { + assert := assert.New(t) + + for _, file := range []struct { + path string + expecteds []string + }{ + {"file-and-dir.tar", []string{"small.txt"}}, + {"gnu.tar", []string{"small.txt", "small2.txt"}}, + // {"gnu-incremental.tar", []string{"test2/foo", "test2/sparse"}}, + {"gnu-long-nul.tar", []string{"0123456789"}}, + {"gnu-multi-hdrs.tar", []string{"GNU2/GNU2/long-path-name"}}, + {"gnu-nil-sparse-data.tar", []string{"sparse.db"}}, + {"gnu-nil-sparse-hole.tar", []string{"sparse.db"}}, + {"gnu-not-utf8.tar", []string{"hi\200\201\202\203bye"}}, + // gnu-sparse-big.tar: too big + {"gnu-utf8.tar", []string{"☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹☺☻☹"}}, + {"hardlink.tar", []string{"file.txt", "hard.txt"}}, + // {"hdr-only.tar", []string{"file"}}, + } { + func() { + f, err := os.Open(filepath.Join(runtime.GOROOT(), "src/archive/tar/testdata", file.path)) + if !assert.NoError(err) { + return + } + defer f.Close() + + tfs, err := New(f) + if !assert.NoError(err) { + return + } + assert.NoError(err) + + err = fstest.TestFS(tfs, file.expecteds...) + if !assert.NoError(err) { + return + } + assert.NoError(err) + }() + } +}