1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package unpacker
import (
"bufio"
"os"
"os/exec"
"path/filepath"
"testing"
)
func createArchive(t *testing.T) *os.File {
mk, err := exec.LookPath("mksquashfs")
if err != nil {
t.SkipNow()
}
f, err := os.CreateTemp("", "archive-")
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(mk, ".", f.Name(), "-noappend", "-no-progress")
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
return f
}
func isExist(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func TestSquashfs(t *testing.T) {
// Run on default TMPDIR which is unlikely to be a tmpfs but may be.
t.Run("default", func(t *testing.T) {
testSquashfs(t, "")
})
// Run on /dev/shm which should be a tmpfs - catches #5668
t.Run("dev_shm", func(t *testing.T) {
if _, err := os.Stat("/dev/shm"); err != nil {
t.Skipf("Could not access /dev/shm")
}
testSquashfs(t, "/dev/shm")
})
}
func testSquashfs(t *testing.T, tmpParent string) {
s := NewSquashfs()
if !s.HasUnsquashfs() {
t.Skip("unsquashfs not found")
}
dir, err := os.MkdirTemp(tmpParent, "test-squashfs-")
if err != nil {
t.Fatalf("while creating tmpdir: %v", err)
}
defer os.RemoveAll(dir)
// create archive with files present in this directory
archive := createArchive(t)
defer os.Remove(archive.Name())
savedPath := s.UnsquashfsPath
// test with an empty unsquashfs path
s.UnsquashfsPath = ""
if err := s.ExtractAll(archive, dir); err == nil {
t.Errorf("unexpected success with empty unsquashfs path")
}
// test with a bad unsquashfs path
s.UnsquashfsPath = "/unsquashfs-no-exists"
if err := s.ExtractAll(archive, dir); err == nil {
t.Errorf("unexpected success with bad unsquashfs path")
}
s.UnsquashfsPath = savedPath
// extract all into temporary folder
if err := s.ExtractAll(archive, dir); err != nil {
t.Error(err)
}
// check if squashfs.go was extracted
path := filepath.Join(dir, "squashfs.go")
if !isExist(path) {
t.Errorf("extraction failed, %s is missing", path)
}
os.Remove(path)
// check if squashfs_test.go was extracted
path = filepath.Join(dir, "squashfs_test.go")
if !isExist(path) {
t.Errorf("extraction failed, %s is missing", path)
}
os.Remove(path)
// test with an empty file list
if err := s.ExtractFiles([]string{}, archive, dir); err == nil {
t.Errorf("unexpected success with empty file list")
}
// extract squashfs_test.go only
if err := s.ExtractFiles([]string{"squashfs_test.go"}, bufio.NewReader(archive), dir); err != nil {
t.Error(err)
}
// check that squashfs.go was not extracted
path = filepath.Join(dir, "squashfs.go")
if isExist(path) {
t.Errorf("file extraction failed, %s is present", path)
}
// check that squashfs_test.go was extracted
path = filepath.Join(dir, "squashfs_test.go")
if !isExist(path) {
t.Errorf("file extraction failed, %s is missing", path)
}
}
func TestMain(m *testing.M) {
cmdFunc = unsquashfsCmd
os.Exit(m.Run())
}
|