Return tests to passing state

This commit is contained in:
Dane Everitt 2020-10-01 20:40:25 -07:00
parent 367fdfad54
commit 90ae815b1d
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 14 additions and 9 deletions

View File

@ -59,6 +59,12 @@ func (fs *Filesystem) Readfile(p string, w io.Writer) error {
return err return err
} }
if st, err := os.Stat(cleaned); err != nil {
return err
} else if st.IsDir() {
return ErrIsDirectory
}
f, err := os.Open(cleaned) f, err := os.Open(cleaned)
if err != nil { if err != nil {
return err return err
@ -164,7 +170,7 @@ func (fs *Filesystem) Rename(from string, to string) error {
// Ensure that the directory we're moving into exists correctly on the system. Only do this if // Ensure that the directory we're moving into exists correctly on the system. Only do this if
// we're not at the root directory level. // we're not at the root directory level.
if d != fs.Path() { if d != fs.Path() {
if mkerr := os.MkdirAll(d, 0644); mkerr != nil { if mkerr := os.MkdirAll(d, 0755); mkerr != nil {
return errors.Wrap(mkerr, "failed to create directory structure for file rename") return errors.Wrap(mkerr, "failed to create directory structure for file rename")
} }
} }

View File

@ -9,7 +9,6 @@ import (
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"unicode/utf8" "unicode/utf8"
) )
@ -104,7 +103,7 @@ func TestFilesystem_Readfile(t *testing.T) {
}) })
g.It("returns an error if the \"file\" is a directory", func() { g.It("returns an error if the \"file\" is a directory", func() {
err := rfs.CreateServerFile("test.txt", "testing") err := os.Mkdir(filepath.Join(rfs.root, "/server/test.txt"), 0755)
g.Assert(err).IsNil() g.Assert(err).IsNil()
err = fs.Readfile("test.txt", buf) err = fs.Readfile("test.txt", buf)
@ -118,7 +117,7 @@ func TestFilesystem_Readfile(t *testing.T) {
err = fs.Readfile("/../test.txt", buf) err = fs.Readfile("/../test.txt", buf)
g.Assert(err).IsNotNil() g.Assert(err).IsNotNil()
g.Assert(strings.Contains(err.Error(), "file does not exist")).IsTrue() g.Assert(errors.Is(err, ErrBadPathResolution)).IsTrue()
}) })
g.AfterEach(func() { g.AfterEach(func() {
@ -179,7 +178,7 @@ func TestFilesystem_Writefile(t *testing.T) {
err := fs.Writefile("/some/../foo/../../test.txt", r) err := fs.Writefile("/some/../foo/../../test.txt", r)
g.Assert(err).IsNotNil() g.Assert(err).IsNotNil()
g.Assert(strings.Contains(err.Error(), "file does not exist")).IsTrue() g.Assert(errors.Is(err, ErrBadPathResolution)).IsTrue()
}) })
g.It("cannot write a file that exceedes the disk limits", func() { g.It("cannot write a file that exceedes the disk limits", func() {
@ -269,7 +268,7 @@ func TestFilesystem_CreateDirectory(t *testing.T) {
g.It("should not allow the creation of directories outside the root", func() { g.It("should not allow the creation of directories outside the root", func() {
err := fs.CreateDirectory("test", "e/../../something") err := fs.CreateDirectory("test", "e/../../something")
g.Assert(err).IsNotNil() g.Assert(err).IsNotNil()
g.Assert(strings.Contains(err.Error(), "file does not exist")).IsTrue() g.Assert(errors.Is(err, ErrBadPathResolution)).IsTrue()
}) })
g.It("should not increment the disk usage", func() { g.It("should not increment the disk usage", func() {
@ -319,7 +318,7 @@ func TestFilesystem_Rename(t *testing.T) {
g.It("does not allow renaming to a location outside the root", func() { g.It("does not allow renaming to a location outside the root", func() {
err := fs.Rename("source.txt", "../target.txt") err := fs.Rename("source.txt", "../target.txt")
g.Assert(err).IsNotNil() g.Assert(err).IsNotNil()
g.Assert(errors.Is(err, os.ErrNotExist)).IsTrue() g.Assert(errors.Is(err, ErrBadPathResolution)).IsTrue()
}) })
g.It("does not allow renaming from a location outside the root", func() { g.It("does not allow renaming from a location outside the root", func() {
@ -327,7 +326,7 @@ func TestFilesystem_Rename(t *testing.T) {
err = fs.Rename("/../ext-source.txt", "target.txt") err = fs.Rename("/../ext-source.txt", "target.txt")
g.Assert(err).IsNotNil() g.Assert(err).IsNotNil()
g.Assert(errors.Is(err, os.ErrNotExist)).IsTrue() g.Assert(errors.Is(err, ErrBadPathResolution)).IsTrue()
}) })
g.It("allows a file to be renamed", func() { g.It("allows a file to be renamed", func() {
@ -473,7 +472,7 @@ func TestFilesystem_Copy(t *testing.T) {
err := os.MkdirAll(filepath.Join(rfs.root, "/server/nested/in/dir"), 0755) err := os.MkdirAll(filepath.Join(rfs.root, "/server/nested/in/dir"), 0755)
g.Assert(err).IsNil() g.Assert(err).IsNil()
err = rfs.CreateServerFile("source.txt", "test content") err = rfs.CreateServerFile("nested/in/dir/source.txt", "test content")
g.Assert(err).IsNil() g.Assert(err).IsNil()
err = fs.Copy("nested/in/dir/source.txt") err = fs.Copy("nested/in/dir/source.txt")