Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maed223 committed Jan 28, 2025
1 parent 18d2969 commit 537f88b
Showing 1 changed file with 152 additions and 2 deletions.
154 changes: 152 additions & 2 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -355,6 +356,78 @@ func TestPack_symlinks(t *testing.T) {
}
}

func TestPack_windowsSymlinks(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific symlink test")
}

td, err := ioutil.TempDir("", "go-slug")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)

// Create test directories
internal := filepath.Join(td, "internal")
if err := os.MkdirAll(internal, 0700); err != nil {
t.Fatal(err)
}

// Create a file to link to
targetPath := filepath.Join(internal, "target.txt")
if err := ioutil.WriteFile(targetPath, []byte("test content"), 0644); err != nil {
t.Fatal(err)
}

// Create a directory with irregular mode (simulating a Windows mount point)
mountPath := filepath.Join(internal, "mount")
if err := os.MkdirAll(mountPath, 0700); err != nil {
t.Fatal(err)
}

// Manually set the irregular flag - this simulates what Windows would do
// for a mount point
if err := os.Chmod(mountPath, 0700|os.ModeIrregular); err != nil {
t.Fatal(err)
}

// Pack up the temp dir
slug := bytes.NewBuffer(nil)
_, err = Pack(internal, slug, false)
if err != nil {
t.Fatal(err)
}

// Inspect the result
gzipR, err := gzip.NewReader(slug)
if err != nil {
t.Fatal(err)
}
tarR := tar.NewReader(gzipR)

var foundIrregular bool
for {
hdr, err := tarR.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}

if filepath.Base(hdr.Name) == "mount" {
foundIrregular = true
if hdr.Typeflag != tar.TypeSymlink {
t.Error("Windows mount point not treated as symlink in archive")
}
}
}

if !foundIrregular {
t.Error("Did not find Windows mount point in archive")
}
}

func TestAllowSymlinkTarget(t *testing.T) {
tcases := []struct {
desc string
Expand Down Expand Up @@ -605,6 +678,66 @@ func TestUnpack(t *testing.T) {
verifyPerms(t, filepath.Join(dst, "exe"), 0755)
}

func TestUnpack_windowsSymlinks(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific symlink test")
}

dir, err := ioutil.TempDir("", "slug")
if err != nil {
t.Fatalf("err:%v", err)
}
defer os.RemoveAll(dir)
in := filepath.Join(dir, "slug.tar.gz")

// Create a test archive with a simulated Windows mount point
wfh, err := os.Create(in)
if err != nil {
t.Fatalf("err: %v", err)
}

gzipW := gzip.NewWriter(wfh)
tarW := tar.NewWriter(gzipW)

// Add a header that represents a Windows mount point
hdr := &tar.Header{
Name: "mount",
Typeflag: tar.TypeSymlink, // Should be unpacked as irregular on Windows
Mode: int64(0700 | fs.ModeIrregular),
}
if err := tarW.WriteHeader(hdr); err != nil {
t.Fatal(err)
}

tarW.Close()
gzipW.Close()
wfh.Close()

fh, err := os.Open(in)
if err != nil {
t.Fatal(err)
}

dst := filepath.Join(dir, "dst")
if err := os.MkdirAll(dst, 0700); err != nil {
t.Fatal(err)
}

if err := Unpack(fh, dst); err != nil {
t.Fatal(err)
}

// Verify the unpacked file has the irregular flag set
fi, err := os.Lstat(filepath.Join(dst, "mount"))
if err != nil {
t.Fatal(err)
}

if fi.Mode()&os.ModeIrregular == 0 {
t.Error("Unpacked Windows mount point missing irregular flag")
}
}

func TestUnpack_HeaderOrdering(t *testing.T) {
// Tests that when a tar file has subdirectories ordered before parent directories, the
// timestamps get restored correctly in the plaform where the tests are run.
Expand Down Expand Up @@ -1084,7 +1217,7 @@ func TestUnpackMaliciousFiles(t *testing.T) {
}

func TestCheckFileMode(t *testing.T) {
for _, tc := range []struct {
cases := []struct {
desc string
mode os.FileMode
keep bool
Expand All @@ -1094,7 +1227,24 @@ func TestCheckFileMode(t *testing.T) {
{"includes directories", os.ModeDir, true, false},
{"includes symlinks", os.ModeSymlink, true, false},
{"excludes unrecognized modes", os.ModeDevice, false, false},
} {
}

// Windows-specific test case
if runtime.GOOS == "windows" {
cases = append(cases, struct {
desc string
mode os.FileMode
keep bool
body bool
}{
"includes irregular files (Windows mount points)",
os.ModeIrregular,
true,
false,
})
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
keep, body := checkFileMode(tc.mode)
if keep != tc.keep || body != tc.body {
Expand Down

0 comments on commit 537f88b

Please sign in to comment.