add decompress tests
This commit is contained in:
parent
daaef5044e
commit
35b2c420ec
55
server/filesystem/compress_test.go
Normal file
55
server/filesystem/compress_test.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package filesystem
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
. "github.com/franela/goblin"
|
||||
)
|
||||
|
||||
// Given an archive named test.{ext}, with the following file structure:
|
||||
// test/
|
||||
// |──inside/
|
||||
// |────finside.txt
|
||||
// |──outside.txt
|
||||
// this test will ensure that it's being decompressed as expected
|
||||
func TestFilesystem_DecompressFile(t *testing.T) {
|
||||
g := Goblin(t)
|
||||
fs, rfs := NewFs()
|
||||
|
||||
g.Describe("Decompress", func() {
|
||||
|
||||
for _, ext := range []string{"zip", "rar", "tar", "tar.gz"} {
|
||||
g.It("can decompress a "+ext, func() {
|
||||
// copy the file to the new FS
|
||||
c, err := ioutil.ReadFile("./testdata/test." + ext)
|
||||
g.Assert(err).IsNil()
|
||||
err = rfs.CreateServerFile("./test."+ext, c)
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
// decompress
|
||||
err = fs.DecompressFile("/", "test."+ext)
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
// make sure everything is where it is supposed to be
|
||||
_, err = rfs.StatServerFile("test/outside.txt")
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
st, err := rfs.StatServerFile("test/inside")
|
||||
g.Assert(err).IsNil()
|
||||
g.Assert(st.IsDir()).IsTrue()
|
||||
|
||||
_, err = rfs.StatServerFile("test/inside/finside.txt")
|
||||
g.Assert(err).IsNil()
|
||||
g.Assert(st.IsDir()).IsTrue()
|
||||
})
|
||||
}
|
||||
|
||||
g.AfterEach(func() {
|
||||
rfs.reset()
|
||||
atomic.StoreInt64(&fs.diskUsed, 0)
|
||||
atomic.StoreInt64(&fs.diskLimit, 0)
|
||||
})
|
||||
})
|
||||
}
|
|
@ -44,17 +44,21 @@ type rootFs struct {
|
|||
root string
|
||||
}
|
||||
|
||||
func (rfs *rootFs) CreateServerFile(p string, c string) error {
|
||||
func (rfs *rootFs) CreateServerFile(p string, c []byte) error {
|
||||
f, err := os.Create(filepath.Join(rfs.root, "/server", p))
|
||||
|
||||
if err == nil {
|
||||
f.Write([]byte(c))
|
||||
f.Write(c)
|
||||
f.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (rfs *rootFs) CreateServerFileFromString(p string, c string) error {
|
||||
return rfs.CreateServerFile(p, []byte(c))
|
||||
}
|
||||
|
||||
func (rfs *rootFs) StatServerFile(p string) (os.FileInfo, error) {
|
||||
return os.Stat(filepath.Join(rfs.root, "/server", p))
|
||||
}
|
||||
|
@ -79,7 +83,7 @@ func TestFilesystem_Readfile(t *testing.T) {
|
|||
buf := &bytes.Buffer{}
|
||||
|
||||
g.It("opens a file if it exists on the system", func() {
|
||||
err := rfs.CreateServerFile("test.txt", "testing")
|
||||
err := rfs.CreateServerFileFromString("test.txt", "testing")
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = fs.Readfile("test.txt", buf)
|
||||
|
@ -103,7 +107,7 @@ func TestFilesystem_Readfile(t *testing.T) {
|
|||
})
|
||||
|
||||
g.It("cannot open a file outside the root directory", func() {
|
||||
err := rfs.CreateServerFile("/../test.txt", "testing")
|
||||
err := rfs.CreateServerFileFromString("/../test.txt", "testing")
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = fs.Readfile("/../test.txt", buf)
|
||||
|
@ -281,13 +285,13 @@ func TestFilesystem_Rename(t *testing.T) {
|
|||
|
||||
g.Describe("Rename", func() {
|
||||
g.BeforeEach(func() {
|
||||
if err := rfs.CreateServerFile("source.txt", "text content"); err != nil {
|
||||
if err := rfs.CreateServerFileFromString("source.txt", "text content"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
g.It("returns an error if the target already exists", func() {
|
||||
err := rfs.CreateServerFile("target.txt", "taget content")
|
||||
err := rfs.CreateServerFileFromString("target.txt", "taget content")
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = fs.Rename("source.txt", "target.txt")
|
||||
|
@ -314,7 +318,7 @@ func TestFilesystem_Rename(t *testing.T) {
|
|||
})
|
||||
|
||||
g.It("does not allow renaming from a location outside the root", func() {
|
||||
err := rfs.CreateServerFile("/../ext-source.txt", "taget content")
|
||||
err := rfs.CreateServerFileFromString("/../ext-source.txt", "taget content")
|
||||
|
||||
err = fs.Rename("/../ext-source.txt", "target.txt")
|
||||
g.Assert(err).IsNotNil()
|
||||
|
@ -378,7 +382,7 @@ func TestFilesystem_Copy(t *testing.T) {
|
|||
|
||||
g.Describe("Copy", func() {
|
||||
g.BeforeEach(func() {
|
||||
if err := rfs.CreateServerFile("source.txt", "text content"); err != nil {
|
||||
if err := rfs.CreateServerFileFromString("source.txt", "text content"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -392,7 +396,7 @@ func TestFilesystem_Copy(t *testing.T) {
|
|||
})
|
||||
|
||||
g.It("should return an error if the source is outside the root", func() {
|
||||
err := rfs.CreateServerFile("/../ext-source.txt", "text content")
|
||||
err := rfs.CreateServerFileFromString("/../ext-source.txt", "text content")
|
||||
|
||||
err = fs.Copy("../ext-source.txt")
|
||||
g.Assert(err).IsNotNil()
|
||||
|
@ -403,7 +407,7 @@ func TestFilesystem_Copy(t *testing.T) {
|
|||
err := os.MkdirAll(filepath.Join(rfs.root, "/nested/in/dir"), 0755)
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = rfs.CreateServerFile("/../nested/in/dir/ext-source.txt", "external content")
|
||||
err = rfs.CreateServerFileFromString("/../nested/in/dir/ext-source.txt", "external content")
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = fs.Copy("../nested/in/dir/ext-source.txt")
|
||||
|
@ -464,7 +468,7 @@ func TestFilesystem_Copy(t *testing.T) {
|
|||
err := os.MkdirAll(filepath.Join(rfs.root, "/server/nested/in/dir"), 0755)
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = rfs.CreateServerFile("nested/in/dir/source.txt", "test content")
|
||||
err = rfs.CreateServerFileFromString("nested/in/dir/source.txt", "test content")
|
||||
g.Assert(err).IsNil()
|
||||
|
||||
err = fs.Copy("nested/in/dir/source.txt")
|
||||
|
@ -492,7 +496,7 @@ func TestFilesystem_Delete(t *testing.T) {
|
|||
|
||||
g.Describe("Delete", func() {
|
||||
g.BeforeEach(func() {
|
||||
if err := rfs.CreateServerFile("source.txt", "test content"); err != nil {
|
||||
if err := rfs.CreateServerFileFromString("source.txt", "test content"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -500,7 +504,7 @@ func TestFilesystem_Delete(t *testing.T) {
|
|||
})
|
||||
|
||||
g.It("does not delete files outside the root directory", func() {
|
||||
err := rfs.CreateServerFile("/../ext-source.txt", "external content")
|
||||
err := rfs.CreateServerFileFromString("/../ext-source.txt", "external content")
|
||||
|
||||
err = fs.Delete("../ext-source.txt")
|
||||
g.Assert(err).IsNotNil()
|
||||
|
@ -544,7 +548,7 @@ func TestFilesystem_Delete(t *testing.T) {
|
|||
g.Assert(err).IsNil()
|
||||
|
||||
for _, s := range sources {
|
||||
err = rfs.CreateServerFile(s, "test content")
|
||||
err = rfs.CreateServerFileFromString(s, "test content")
|
||||
g.Assert(err).IsNil()
|
||||
}
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ func TestFilesystem_Blocks_Symlinks(t *testing.T) {
|
|||
g := Goblin(t)
|
||||
fs, rfs := NewFs()
|
||||
|
||||
if err := rfs.CreateServerFile("/../malicious.txt", "external content"); err != nil {
|
||||
if err := rfs.CreateServerFileFromString("/../malicious.txt", "external content"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -181,7 +181,7 @@ func TestFilesystem_Blocks_Symlinks(t *testing.T) {
|
|||
})
|
||||
|
||||
g.It("cannot rename a file to a location outside the directory root", func() {
|
||||
rfs.CreateServerFile("my_file.txt", "internal content")
|
||||
rfs.CreateServerFileFromString("my_file.txt", "internal content")
|
||||
|
||||
err := fs.Rename("my_file.txt", "external_dir/my_file.txt")
|
||||
g.Assert(err).IsNotNil()
|
||||
|
|
BIN
server/filesystem/testdata/test.rar
vendored
Normal file
BIN
server/filesystem/testdata/test.rar
vendored
Normal file
Binary file not shown.
BIN
server/filesystem/testdata/test.tar
vendored
Normal file
BIN
server/filesystem/testdata/test.tar
vendored
Normal file
Binary file not shown.
BIN
server/filesystem/testdata/test.tar.gz
vendored
Normal file
BIN
server/filesystem/testdata/test.tar.gz
vendored
Normal file
Binary file not shown.
BIN
server/filesystem/testdata/test.zip
vendored
Normal file
BIN
server/filesystem/testdata/test.zip
vendored
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user