diff --git a/.gitignore b/.gitignore index 0fc978d..3e32be2 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ go.work bin/ *.dd *.zip -!internal/testdata/* +!internal/testdata/windows.zip +!internal/testdata/windows.dd.zip diff --git a/internal/sys/sys_test.go b/internal/sys/sys_test.go index 8ea3dad..4bb3619 100644 --- a/internal/sys/sys_test.go +++ b/internal/sys/sys_test.go @@ -7,7 +7,7 @@ import ( ) func TestCall(t *testing.T) { - t.Run("Test layered call", func(t *testing.T) { + t.Run("Test call", func(t *testing.T) { stdout := new(strings.Builder) stderr := new(strings.Builder) diff --git a/internal/testdata/windows.dd.zip b/internal/testdata/windows.dd.zip new file mode 100644 index 0000000..c0a1e42 Binary files /dev/null and b/internal/testdata/windows.dd.zip differ diff --git a/internal/zip/zip_test.go b/internal/zip/zip_test.go index 6c8e7ef..281effd 100644 --- a/internal/zip/zip_test.go +++ b/internal/zip/zip_test.go @@ -1,2 +1,43 @@ // Zip archive implementation tests. package zip + +import ( + "os" + "path/filepath" + "slices" + "testing" + + "github.com/cuhsat/fact/internal/test" +) + +func TestIndex(t *testing.T) { + t.Run("Test index", func(t *testing.T) { + idx, err := Index(test.Testdata("windows.zip")) + + if err != nil { + t.Fatal(err) + } + + if !slices.Contains(idx, "Users/Test/NTUSER.DAT") { + t.Fatal("file not found") + } + }) +} + +func TestUnzip(t *testing.T) { + t.Run("Test unzip", func(t *testing.T) { + tmp, _ := os.MkdirTemp(os.TempDir(), "zip") + + err := Unzip(test.Testdata("windows.zip"), tmp) + + if err != nil { + t.Fatal(err) + } + + _, err = os.Stat(filepath.Join(tmp, "Windows")) + + if os.IsNotExist(err) { + t.Fatal("folder not found") + } + }) +} diff --git a/pkg/fmount/dd/dd.go b/pkg/fmount/dd/dd.go index c3be72e..397f7a5 100644 --- a/pkg/fmount/dd/dd.go +++ b/pkg/fmount/dd/dd.go @@ -24,9 +24,7 @@ func Is(img string) (is bool, err error) { func Mount(img, dir string, so bool) (sysroot string, err error) { if len(dir) == 0 { - b := filepath.Base(img) - - dir = strings.TrimSuffix(b, filepath.Ext(b)) + dir = baseFile(img) } if err = os.MkdirAll(dir, mode); err != nil { @@ -151,6 +149,12 @@ func Unmount(img string) (err error) { return } +func baseFile(name string) string { + b := filepath.Base(name) + + return strings.TrimSuffix(b, filepath.Ext(b)) +} + func detectMagic(name string) (has bool, err error) { f, err := os.Open(name) diff --git a/pkg/fmount/dd/dd_test.go b/pkg/fmount/dd/dd_test.go index 1dc6bda..6cbdaa8 100644 --- a/pkg/fmount/dd/dd_test.go +++ b/pkg/fmount/dd/dd_test.go @@ -1,2 +1,53 @@ // DD implementation tests. package dd + +import ( + "os" + "path/filepath" + "testing" + + "github.com/cuhsat/fact/internal/test" + "github.com/cuhsat/fact/internal/zip" +) + +func TestDD(t *testing.T) { + cases := []struct { + name, file string + }{ + { + name: "Test mount for Windows", + file: test.Testdata("windows.dd.zip"), + }, + } + + for _, tt := range cases { + tmp, _ := os.MkdirTemp(os.TempDir(), "dd") + mnt, _ := os.MkdirTemp(os.TempDir(), "dd-mnt") + + err := zip.Unzip(tt.file, tmp) + + if err != nil { + t.Fatal(err) + } + + t.Run(tt.name, func(t *testing.T) { + img := filepath.Join(tmp, baseFile(tt.file)) + + p, err := Mount(img, mnt, true) + + if err != nil { + t.Fatal(err) + } + + if p != filepath.Join(mnt, "p1") { + t.Fatal("sysroot unexpected", p) + } + + err = Unmount(img) + + if err != nil { + t.Fatal(err) + } + }) + } +} diff --git a/pkg/fmount/fmount.go b/pkg/fmount/fmount.go index f3f0720..0aa3776 100644 --- a/pkg/fmount/fmount.go +++ b/pkg/fmount/fmount.go @@ -28,12 +28,6 @@ func DetectType(img, t string) (string, error) { } func Extract(img string) (p string, err error) { - dir, err := os.Getwd() - - if err != nil { - return - } - i, err := zip.Index(img) if err != nil { @@ -45,11 +39,18 @@ func Extract(img string) (p string, err error) { return } - if err = zip.Unzip(img, dir); err != nil { + dir := filepath.Dir(img) + + p = path.Join(dir, i[0]) + + if _, err = os.Stat(p); !os.IsNotExist(err) { + err = errors.New("file already exists") return } - p = path.Join(dir, i[0]) + if err = zip.Unzip(img, dir); err != nil { + return + } return }