2020-11-08 23:15:39 +00:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
2021-04-17 20:29:18 +00:00
|
|
|
"io"
|
2020-11-08 23:15:39 +00:00
|
|
|
"testing"
|
2021-04-17 20:29:18 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
. "github.com/franela/goblin"
|
2020-11-08 23:15:39 +00:00
|
|
|
)
|
|
|
|
|
2021-04-17 20:29:18 +00:00
|
|
|
type stackTracer interface {
|
|
|
|
StackTrace() errors.StackTrace
|
|
|
|
}
|
|
|
|
|
2020-11-08 23:15:39 +00:00
|
|
|
func TestFilesystem_PathResolutionError(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
|
2021-04-17 20:29:18 +00:00
|
|
|
g.Describe("NewFilesystemError", func() {
|
|
|
|
g.It("includes a stack trace for the error", func() {
|
|
|
|
err := newFilesystemError(ErrCodeUnknownError, nil)
|
|
|
|
|
|
|
|
_, ok := err.(stackTracer)
|
|
|
|
g.Assert(ok).IsTrue()
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("properly wraps the underlying error cause", func() {
|
|
|
|
underlying := io.EOF
|
|
|
|
err := newFilesystemError(ErrCodeUnknownError, underlying)
|
|
|
|
|
|
|
|
_, ok := err.(stackTracer)
|
|
|
|
g.Assert(ok).IsTrue()
|
|
|
|
|
|
|
|
_, ok = err.(*Error)
|
|
|
|
g.Assert(ok).IsFalse()
|
|
|
|
|
|
|
|
fserr, ok := errors.Unwrap(err).(*Error)
|
|
|
|
g.Assert(ok).IsTrue()
|
|
|
|
g.Assert(fserr.Unwrap()).IsNotNil()
|
|
|
|
g.Assert(fserr.Unwrap()).Equal(underlying)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-08 23:15:39 +00:00
|
|
|
g.Describe("NewBadPathResolutionError", func() {
|
|
|
|
g.It("is can detect itself as an error correctly", func() {
|
|
|
|
err := NewBadPathResolution("foo", "bar")
|
2020-12-16 04:51:13 +00:00
|
|
|
g.Assert(IsErrorCode(err, ErrCodePathResolution)).IsTrue()
|
2020-11-08 23:15:39 +00:00
|
|
|
g.Assert(err.Error()).Equal("filesystem: server path [foo] resolves to a location outside the server root: bar")
|
2020-12-16 04:51:13 +00:00
|
|
|
g.Assert(IsErrorCode(&Error{code: ErrCodeIsDirectory}, ErrCodePathResolution)).IsFalse()
|
2020-11-08 23:15:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("returns <empty> if no destination path is provided", func() {
|
|
|
|
err := NewBadPathResolution("foo", "")
|
2021-04-17 20:29:18 +00:00
|
|
|
g.Assert(err).IsNotNil()
|
2020-11-08 23:15:39 +00:00
|
|
|
g.Assert(err.Error()).Equal("filesystem: server path [foo] resolves to a location outside the server root: <empty>")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|