Skip to content

Commit

Permalink
Add new tests using tar files from stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Nov 20, 2023
1 parent 7b1a8fa commit 63072b7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/fs"
"sort"
"strings"
"time"
)

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"path"
"strings"
"unicode/utf8"
)

const (
Expand Down Expand Up @@ -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)
}

Expand Down
44 changes: 44 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"
"testing/fstest"

Expand Down Expand Up @@ -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)
}()
}
}

0 comments on commit 63072b7

Please sign in to comment.