From fe09e6f86589cabafac1913de9f8aacd0399c9e6 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 24 Oct 2020 18:27:19 +0200 Subject: [PATCH] dump: test proper permissions and directory name --- internal/dump/tar_test.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/internal/dump/tar_test.go b/internal/dump/tar_test.go index a8ce336fd..e5b0385be 100644 --- a/internal/dump/tar_test.go +++ b/internal/dump/tar_test.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" "time" @@ -68,6 +69,14 @@ func TestWriteTar(t *testing.T) { }, target: "/", }, + { + name: "file and symlink in root", + args: archiver.TestDir{ + "file1": archiver.TestFile{Content: "string"}, + "file2": archiver.TestSymlink{Target: "file1"}, + }, + target: "/", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -128,7 +137,7 @@ func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error { } matchPath := filepath.Join(testDir, hdr.Name) - match, err := os.Stat(matchPath) + match, err := os.Lstat(matchPath) if err != nil { return err } @@ -140,6 +149,10 @@ func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error { return fmt.Errorf("modTime does not match, got: %s, want: %s", fileTime, tarTime) } + if os.FileMode(hdr.Mode).Perm() != match.Mode().Perm() || os.FileMode(hdr.Mode)&^os.ModePerm != 0 { + return fmt.Errorf("mode does not match, got: %v, want: %v", os.FileMode(hdr.Mode), match.Mode()) + } + if hdr.Typeflag == tar.TypeDir { // this is a folder if hdr.Name == "." { @@ -151,7 +164,17 @@ func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error { if filepath.Base(hdr.Name) != filebase { return fmt.Errorf("foldernames don't match got %v want %v", filepath.Base(hdr.Name), filebase) } - + if !strings.HasSuffix(hdr.Name, "/") { + return fmt.Errorf("foldernames must end with separator got %v", hdr.Name) + } + } else if hdr.Typeflag == tar.TypeSymlink { + target, err := fs.Readlink(matchPath) + if err != nil { + return err + } + if target != hdr.Linkname { + return fmt.Errorf("symlink target does not match, got %s want %s", target, hdr.Linkname) + } } else { if match.Size() != hdr.Size { return fmt.Errorf("size does not match got %v want %v", hdr.Size, match.Size())