218 lines
4.8 KiB
Go
218 lines
4.8 KiB
Go
package zipper
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
content = "test-content"
|
|
)
|
|
|
|
type dummyZipper struct {
|
|
Zipper
|
|
buffer map[string][]byte
|
|
multi bool
|
|
closed bool
|
|
}
|
|
|
|
func (dz dummyZipper) Flush() error {
|
|
return nil
|
|
}
|
|
|
|
func (dz *dummyZipper) Close() error {
|
|
if dz.closed {
|
|
return nil
|
|
}
|
|
dz.closed = true
|
|
return dz.Zipper.Close()
|
|
}
|
|
|
|
func (dz dummyZipper) Compress(fileToZip *os.File, pathInArchive string) (err error) {
|
|
if dz.closed {
|
|
return fmt.Errorf("compressor closed")
|
|
}
|
|
buf := bytes.Buffer{}
|
|
if _, err = io.Copy(&buf, fileToZip); err == nil {
|
|
dz.buffer[PrependPath(fileToZip.Name(), pathInArchive)] = buf.Bytes()
|
|
}
|
|
return
|
|
}
|
|
|
|
func (dz dummyZipper) OpenWriter(zipFile *os.File) (err error) {
|
|
dz.SetWriter(&dz)
|
|
return
|
|
}
|
|
|
|
func (dz dummyZipper) MultiFileSupported() (multi bool) {
|
|
return dz.multi
|
|
}
|
|
|
|
func newZipper(archiveFilename string, multi bool) dummyZipper {
|
|
dz := dummyZipper{
|
|
Zipper: NewBaseZipper(archiveFilename),
|
|
buffer: make(map[string][]byte),
|
|
multi: multi,
|
|
closed: false,
|
|
}
|
|
dz.SetCompressor(dz)
|
|
return dz
|
|
}
|
|
|
|
func TestZipper(t *testing.T) {
|
|
archiveFolder := "testdata"
|
|
archiveFilename := archiveFolder + "/dummy.arc"
|
|
testFileName := "testZipper.data"
|
|
if os.WriteFile(testFileName, []byte(content), 0644) == nil {
|
|
defer os.Remove(testFileName)
|
|
}
|
|
dz := newZipper(archiveFilename, false)
|
|
if dz.GetWriter() != nil {
|
|
t.Log("Found unexpected writer")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test adding files
|
|
if dz.AddZipFiles([]string{testFileName}, "testfolder") != nil {
|
|
t.Log("Unable to add a single file")
|
|
t.Fail()
|
|
}
|
|
if fileContent, ok := dz.buffer["testfolder/"+testFileName]; ok {
|
|
if string(fileContent) != content {
|
|
t.Logf("Unexpected testfile content: '%s'", string(fileContent))
|
|
t.Fail()
|
|
}
|
|
} else {
|
|
t.Log("Failed to create appropriate subfolder in archive")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test modification of compressor
|
|
if dz.SetCompressor(dz) == nil {
|
|
t.Log("Unexpectedly succeeded in changing the compressor interface")
|
|
t.Fail()
|
|
}
|
|
if dz.SetCompressor(nil) == nil {
|
|
t.Log("Unexpectedly succeeded in removing the compressor interface")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test adding more files than allowed
|
|
if dz.AddFileToZip(testFileName, "badFolder") == nil {
|
|
t.Log("Unexpectedly succeeded in adding more than one file")
|
|
t.Fail()
|
|
}
|
|
|
|
// Clean up
|
|
dz.Close()
|
|
os.Remove(archiveFilename)
|
|
os.Remove(archiveFolder)
|
|
}
|
|
|
|
type dummyUnzipper struct {
|
|
Unzipper
|
|
buffer map[string][]byte
|
|
closed bool
|
|
}
|
|
|
|
func (duz *dummyUnzipper) Close() error {
|
|
if duz.closed {
|
|
return nil
|
|
}
|
|
duz.closed = true
|
|
return duz.Unzipper.Close()
|
|
}
|
|
|
|
func (duz *dummyUnzipper) Decompress(targetFolderPathInFileSystem string) (err error) {
|
|
if duz.buffer == nil {
|
|
return fmt.Errorf("nothing to decompress")
|
|
}
|
|
for path, data := range duz.buffer {
|
|
outputPath := PrependPath(path, targetFolderPathInFileSystem)
|
|
if err = MakeSurePathExists(outputPath, 0644); err == nil {
|
|
if err = os.WriteFile(outputPath, data, 0644); err != nil {
|
|
break
|
|
}
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (duz *dummyUnzipper) OpenReader(zipFile *os.File) (err error) {
|
|
var binary []byte
|
|
if binary, err = io.ReadAll(zipFile); err == nil {
|
|
duz.SetReader(duz)
|
|
err = json.Unmarshal(binary, &duz.buffer)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func newUnzipper(archiveFilename string) dummyUnzipper {
|
|
duz := dummyUnzipper{
|
|
Unzipper: NewBaseUnzipper(archiveFilename),
|
|
buffer: make(map[string][]byte),
|
|
}
|
|
duz.SetDecompressor(&duz)
|
|
return duz
|
|
}
|
|
|
|
func TestUnzipper(t *testing.T) {
|
|
archiveFolder := "testdata2"
|
|
archiveFilename := archiveFolder + "/dummy.arc"
|
|
data := make(map[string][]byte)
|
|
contentFolder := "extractedTest"
|
|
contentFilename := contentFolder + "/dummy.file"
|
|
data[contentFilename] = []byte(content)
|
|
if MakeSurePathExists(archiveFilename, 0644) == nil {
|
|
if archiveContent, err := json.Marshal(data); err == nil {
|
|
if os.WriteFile(archiveFilename, archiveContent, 0644) == nil {
|
|
defer func() {
|
|
os.Remove(archiveFilename)
|
|
os.Remove(archiveFolder)
|
|
}()
|
|
}
|
|
}
|
|
}
|
|
|
|
duz := newUnzipper(archiveFilename)
|
|
if duz.GetReader() != nil {
|
|
t.Log("Found unexpected reader")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test modification of decompressor
|
|
if duz.SetDecompressor(&duz) == nil {
|
|
t.Log("Unexpectedly succeeded in changing the decompressor interface")
|
|
t.Fail()
|
|
}
|
|
if duz.SetDecompressor(nil) == nil {
|
|
t.Log("Unexpectedly succeeded in removing the decompressor interface")
|
|
t.Fail()
|
|
}
|
|
|
|
// Test decompression
|
|
if duz.ExtractFiles("") != nil {
|
|
t.Log("Decompression failed")
|
|
t.Fail()
|
|
} else {
|
|
if data, err := os.ReadFile(contentFilename); err != nil {
|
|
t.Log("Expected extracted file is missing")
|
|
t.Fail()
|
|
} else if string(data) != content {
|
|
t.Logf("Unexpected extracted file content: '%s'", string(data))
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
// Clean up
|
|
duz.Close()
|
|
os.Remove(contentFilename)
|
|
os.Remove(contentFolder)
|
|
}
|