From 35b2c420ec8ce597288ff3866d8dfdf5a7352434 Mon Sep 17 00:00:00 2001 From: Julien Tant Date: Sun, 25 Apr 2021 16:44:54 -0700 Subject: [PATCH] add decompress tests --- server/filesystem/compress_test.go | 55 +++++++++++++++++++++++++ server/filesystem/filesystem_test.go | 32 +++++++------- server/filesystem/path_test.go | 4 +- server/filesystem/testdata/test.rar | Bin 0 -> 185 bytes server/filesystem/testdata/test.tar | Bin 0 -> 3072 bytes server/filesystem/testdata/test.tar.gz | Bin 0 -> 176 bytes server/filesystem/testdata/test.zip | Bin 0 -> 582 bytes 7 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 server/filesystem/compress_test.go create mode 100644 server/filesystem/testdata/test.rar create mode 100644 server/filesystem/testdata/test.tar create mode 100644 server/filesystem/testdata/test.tar.gz create mode 100644 server/filesystem/testdata/test.zip diff --git a/server/filesystem/compress_test.go b/server/filesystem/compress_test.go new file mode 100644 index 0000000..35ca767 --- /dev/null +++ b/server/filesystem/compress_test.go @@ -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) + }) + }) +} diff --git a/server/filesystem/filesystem_test.go b/server/filesystem/filesystem_test.go index 8427738..fbdf9fc 100644 --- a/server/filesystem/filesystem_test.go +++ b/server/filesystem/filesystem_test.go @@ -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() } diff --git a/server/filesystem/path_test.go b/server/filesystem/path_test.go index ada1dfb..9c288f2 100644 --- a/server/filesystem/path_test.go +++ b/server/filesystem/path_test.go @@ -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() diff --git a/server/filesystem/testdata/test.rar b/server/filesystem/testdata/test.rar new file mode 100644 index 0000000000000000000000000000000000000000..30aa2b34918911e04353dfb3928da1509dd04e00 GIT binary patch literal 185 zcmWGaEK-zWXJq*Nu<127BP%-t8zW;wLjyxK-|_-ICT8vi29^ef|IJJcK+wRzC{U7G zT%w;}T2h>ulB!oyQNqP69Qv&_;fd)7v!b-sTTC&vh=a9c=7DtRr$K0_PH&*j9I+oJ n5;8GK>Oh?gtU#+6p;mDt=>ysl2vlp)BP1YO9u~;V%EABud*n3* literal 0 HcmV?d00001 diff --git a/server/filesystem/testdata/test.tar b/server/filesystem/testdata/test.tar new file mode 100644 index 0000000000000000000000000000000000000000..9842206021e19f5df880c8e1f84116d7ebdd6bf8 GIT binary patch literal 3072 zcmeH{OA5m<3`BL5oS>2XiOy4K3fUx3E70Rt#$A-G60l7d(#BwnWaiCC<|HGuEyRG# z3|Q)T=3AhEfjoP1`Q5}EVTf(NRl4s`+29&~0wi?78UG%3 z`Q`sCKDfzSs*YBk=krfq`8(YOFm%bJx8w_g*CcSYe$P#t=)k1E zF>k}EsiJDDto$#^O#6J+Q#Q+Umdd%`CT2H(#{7umf1L34YW4MXZ=$3B2b+C=f8M$N zO_yzR?%$)VoR+S?{vX=(|D`2=+2+{uH?G8Ax?-P|F^7Nmy+_QyqDv*$di`WIRL@Gn(-NFirWTB_Q)5su`K=8F0B+1#CG314t(bfZfl8P(LG^zR&sGHIV5rdR21VIS?OWI2au;I+hAcN6qWQ#CE6WO9Bm_@z_0