diff --git a/testdata/test.7z b/testdata/test.7z index 640dfac..6aa1530 100644 Binary files a/testdata/test.7z and b/testdata/test.7z differ diff --git a/testdata/test.rar b/testdata/test.rar index 70e6354..08e313e 100644 Binary files a/testdata/test.rar and b/testdata/test.rar differ diff --git a/testdata/test.zip b/testdata/test.zip index 254c019..1e10a33 100644 Binary files a/testdata/test.zip and b/testdata/test.zip differ diff --git a/unarr_test.go b/unarr_test.go index a3514f4..8452373 100644 --- a/unarr_test.go +++ b/unarr_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" ) @@ -49,3 +50,129 @@ func TestExtract(t *testing.T) { a.Close() } } + +func TestExtractFromMemory(t *testing.T) { + tmpdir, err := ioutil.TempDir(os.TempDir(), "unarr") + if err != nil { + t.Error(err) + } + + defer os.RemoveAll(tmpdir) + + files := []string{"test.zip", "test.rar", "test.7z"} + + for _, f := range files { + data, err := ioutil.ReadFile(filepath.Join("testdata", f)) + if err != nil { + t.Error(err) + } + + a, err := NewArchiveFromMemory(data) + if err != nil { + t.Error(err) + } + + a.Extract(tmpdir) + a.Close() + } +} + +func TestReadAll(t *testing.T) { + exts := []string{"zip", "rar", "7z"} + + for _, e := range exts { + a, err := NewArchive(filepath.Join("testdata", "test."+e)) + if err != nil { + t.Error(err) + } + + err = a.Entry() + if err != nil { + t.Error(err) + } + + data, err := a.ReadAll() + if err != nil { + t.Error(err) + } + + if strings.TrimSpace(string(data)) != "unarr" { + t.Error("Invalid data") + } + + a.Close() + } +} + +func TestReadAllFromMemory(t *testing.T) { + exts := []string{"zip", "rar", "7z"} + + for _, e := range exts { + m, err := ioutil.ReadFile(filepath.Join("testdata", "test."+e)) + if err != nil { + t.Error(err) + } + + a, err := NewArchiveFromMemory(m) + if err != nil { + t.Error(err) + } + + err = a.Entry() + if err != nil { + t.Error(err) + } + + data, err := a.ReadAll() + if err != nil { + t.Error(err) + } + + if strings.TrimSpace(string(data)) != "unarr" { + t.Error("Invalid data") + } + + a.Close() + } +} + +func TestEntryFor(t *testing.T) { + exts := []string{"zip", "rar", "7z"} + + for _, e := range exts { + a, err := NewArchive(filepath.Join("testdata", "test."+e)) + if err != nil { + t.Error(err) + } + + err = a.EntryFor(e) + if err != nil { + t.Error(err) + } + + data, err := a.ReadAll() + if err != nil { + t.Error(err) + } + + if strings.TrimSpace(string(data)) != "unarr" { + t.Error("Invalid data") + } + + a.Close() + } +} + +func TestList(t *testing.T) { + files := []string{"test.zip", "test.rar", "test.7z"} + + for _, f := range files { + a, err := NewArchive(filepath.Join("testdata", f)) + if err != nil { + t.Error(err) + } + + a.List() + a.Close() + } +}