Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for iso extraction #68

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions iso_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package xtractr_test

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/kdomanski/iso9660"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/xtractr"
)

func TestIso(t *testing.T) {
t.Parallel()

testFilesInfo := createTestFiles(t)

writer, err := iso9660.NewWriter()
require.NoError(t, err, "failed to create writer")
defer func() {
err = writer.Cleanup()
require.NoError(t, err, "failed to cleanup writer")
}()

walkErr := filepath.Walk(testFilesInfo.srcFilesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("unexpected error: %w", err)
}
if info.IsDir() {
return nil
}

fileToAdd, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer fileToAdd.Close()

err = writer.AddFile(fileToAdd, strings.TrimLeft(fileToAdd.Name(), testFilesInfo.srcFilesDir))
if err != nil {
return fmt.Errorf("failed to add file: %w", err)
}
return nil
})
require.NoError(t, walkErr, "failed to walk files")

isoFileName := filepath.Join(testFilesInfo.dstFilesDir, "archive.iso")
isoFile, err := os.Create(isoFileName)
defer safeCloser(t, isoFile)
require.NoError(t, err, "failed to create ISO file")

err = writer.WriteTo(isoFile, "test")
require.NoError(t, err, "failed to write ISO")

size, files, archives, err := xtractr.ExtractFile(&xtractr.XFile{
FilePath: isoFileName,
OutputDir: filepath.Clean(testFilesInfo.dstFilesDir),
FileMode: 0o600,
DirMode: 0o700,
})
require.NoError(t, err)
assert.Equal(t, int64(testFilesInfo.dataSize), size)
assert.Len(t, files, testFilesInfo.fileCount)
assert.Len(t, archives, testFilesInfo.archiveCount)
}
103 changes: 17 additions & 86 deletions tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/dsnet/compress/bzip2"
Expand All @@ -31,94 +30,32 @@ type (

func TestTar(t *testing.T) {
t.Parallel()
const (
loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`
testDataSize = 1544
testFileCount = 5
testArchiveCount = 1
)

randomDigits := []uint8{
157, 242, 143, 106, 163, 159, 194, 141, 32, 22, 249, 78,
225, 206, 190, 199, 99, 146, 53, 149, 239, 179, 72, 2, 197, 196, 91, 81, 192,
241, 69, 166, 213, 172, 111, 117, 210, 51, 136, 185, 130, 109, 139, 57, 150, 63,
85, 86, 204, 10, 26, 1, 186, 234, 96, 187, 205, 138, 224, 77, 114, 226, 16, 222,
151, 175, 200, 116, 36, 198, 173, 168, 230, 4, 18, 245, 31, 214, 158, 105, 171,
123, 195, 137, 40, 93, 83, 215, 6, 118, 161, 223, 43, 167, 7, 3, 113, 148, 201,
125,
}

testFiles := []string{
"README.txt",
"level1/",
"level1/level1.txt",
"level1/level1.bin",
"level1/level2/",
"level1/level2/level2.txt",
"level1/level2/level2.bin",
}

testDataDir, err := os.MkdirTemp(".", "xtractr_test_*_data")
require.NoError(t, err, "creating temp directory failed")
t.Cleanup(func() {
os.RemoveAll(testDataDir)
})

sourceFilesDir := filepath.Join(testDataDir, "sources")
err = os.MkdirAll(sourceFilesDir, 0o700)
require.NoError(t, err)

var destFilesDir string
for _, file := range testFiles {
fullPath := filepath.Join(sourceFilesDir, file)
var err error
switch {
case file[len(file)-1] == '/':
err = os.MkdirAll(fullPath, 0o700)
case filepath.Ext(file) == ".txt":
err = os.WriteFile(fullPath, []byte(loremIpsum), 0o600)
default:
err = os.WriteFile(fullPath, randomDigits, 0o600)
}
require.NoError(t, err)

destFilesDir = filepath.Join(testDataDir, "out")
err = os.MkdirAll(destFilesDir, 0o700)
require.NoError(t, err)
}

tests := []struct {
name string
compressor compressor
extension string
skipWindows bool
name string
compressor compressor
extension string
}{
{"tar", &tarCompressor{}, "tar", false},
{"tarZ", &tarZCompressor{}, "tar.z", true},
{"tarBzip", &tarBzipCompressor{}, "tar.bz2", false},
{"tarXZ", &tarXZCompressor{}, "tar.xz", false},
{"tarGzip", &tarGzipCompressor{}, "tar.gz", false},
{"tar", &tarCompressor{}, "tar"},
{"tarZ", &tarZCompressor{}, "tar.z"},
{"tarBzip", &tarBzipCompressor{}, "tar.bz2"},
{"tarXZ", &tarXZCompressor{}, "tar.xz"},
{"tarGzip", &tarGzipCompressor{}, "tar.gz"},
}

testFilesInfo := createTestFiles(t)
require.NotNil(t, testFilesInfo)

for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
t.Parallel()

if runtime.GOOS == "windows" && test.skipWindows {
t.Log("skipping test on windows")
}

archiveBase := filepath.Join(destFilesDir, "archive")
err := test.compressor.Compress(t, sourceFilesDir, archiveBase)
archiveBase := filepath.Join(testFilesInfo.dstFilesDir, "archive")
err := test.compressor.Compress(t, testFilesInfo.srcFilesDir, archiveBase)
require.NoError(t, err)

extractDir := filepath.Join(destFilesDir, test.name)
extractDir := filepath.Join(testFilesInfo.dstFilesDir, test.name)
err = os.Mkdir(extractDir, 0o700)
require.NoError(t, err)

Expand All @@ -129,19 +66,13 @@ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`
DirMode: 0o700,
})
require.NoError(t, err)
assert.Equal(t, int64(testDataSize), size)
assert.Len(t, files, testFileCount)
assert.Len(t, archives, testArchiveCount)
assert.Equal(t, int64(testFilesInfo.dataSize), size)
assert.Len(t, files, testFilesInfo.fileCount)
assert.Len(t, archives, testFilesInfo.archiveCount)
})
}
}

func safeCloser(t *testing.T, c io.Closer) {
t.Helper()
err := c.Close()
require.NoError(t, err)
}

func writeTar(sourceDir string, destWriter io.Writer) error {
tarWriter := tar.NewWriter(destWriter)
outErr := filepath.Walk(sourceDir, func(path string, info os.FileInfo, _ error) error {
Expand Down
100 changes: 100 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package xtractr_test

// Shared utility functions/structs used in testing only.

import (
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

type testFilesInfo struct {
srcFilesDir string
dstFilesDir string
dataSize int
fileCount int
archiveCount int
}

// Create test files for the tests and returns information
// about the created files and directories.
func createTestFiles(t *testing.T) *testFilesInfo {
t.Helper()
const (
loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`
testDataSize = 1544
testFileCount = 5
testArchiveCount = 1
)

randomDigits := []uint8{
157, 242, 143, 106, 163, 159, 194, 141, 32, 22, 249, 78,
225, 206, 190, 199, 99, 146, 53, 149, 239, 179, 72, 2, 197, 196, 91, 81, 192,
241, 69, 166, 213, 172, 111, 117, 210, 51, 136, 185, 130, 109, 139, 57, 150, 63,
85, 86, 204, 10, 26, 1, 186, 234, 96, 187, 205, 138, 224, 77, 114, 226, 16, 222,
151, 175, 200, 116, 36, 198, 173, 168, 230, 4, 18, 245, 31, 214, 158, 105, 171,
123, 195, 137, 40, 93, 83, 215, 6, 118, 161, 223, 43, 167, 7, 3, 113, 148, 201,
125,
}

testFiles := []string{
"README.txt",
"level1/",
"level1/level1.txt",
"level1/level1.bin",
"level1/level2/",
"level1/level2/level2.txt",
"level1/level2/level2.bin",
}

testDataDir, err := os.MkdirTemp(".", "xtractr_test_*_data")
require.NoError(t, err, "creating temp directory failed")
t.Cleanup(func() {
os.RemoveAll(testDataDir)
})

srcFilesDir := filepath.Join(testDataDir, "sources")
err = os.MkdirAll(srcFilesDir, 0o700)
require.NoError(t, err)

var destFilesDir string
for _, file := range testFiles {
fullPath := filepath.Join(srcFilesDir, file)
var err error
switch {
case file[len(file)-1] == '/':
err = os.MkdirAll(fullPath, 0o700)
case filepath.Ext(file) == ".txt":
err = os.WriteFile(fullPath, []byte(loremIpsum), 0o600)
default:
err = os.WriteFile(fullPath, randomDigits, 0o600)
}
require.NoError(t, err)

destFilesDir = filepath.Join(testDataDir, "out")
err = os.MkdirAll(destFilesDir, 0o700)
require.NoError(t, err)
}

return &testFilesInfo{
srcFilesDir: srcFilesDir,
dstFilesDir: destFilesDir,
dataSize: testDataSize,
fileCount: testFileCount,
archiveCount: testArchiveCount,
}
}

func safeCloser(t *testing.T, c io.Closer) {
t.Helper()
err := c.Close()
require.NoError(t, err)
}
Loading