From 226fca41b67356a0d399a3f4ed5d2fa7323ba2e4 Mon Sep 17 00:00:00 2001 From: JinnyYi <82445294+JinnyYi@users.noreply.github.com> Date: Fri, 24 Sep 2021 17:09:21 +0800 Subject: [PATCH] test: Add GSP-749 test cases (#59) --- Makefile | 2 +- storager.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c204a21..bf72303 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ format: vet: go vet ./... -build: tidy check +build: tidy format check go build ./... tidy: diff --git a/storager.go b/storager.go index b9f3c6a..082f3fa 100644 --- a/storager.go +++ b/storager.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "math/rand" + "path/filepath" "testing" "github.com/google/uuid" @@ -38,6 +39,8 @@ func TestStorager(t *testing.T, store types.Storager) { }) }) + workDir := store.Metadata().WorkDir + Convey("When Read a file", func() { size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size)) @@ -433,5 +436,100 @@ func TestStorager(t *testing.T, store types.Storager) { So(osize, ShouldEqual, size) }) }) + + Convey("When testing GSP-749 unify path behavior", func() { + Convey("When using absolute path", func() { + size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size)) + if err != nil { + t.Error(err) + } + + path := uuid.New().String() + absPath := filepath.Join(workDir, path) + _, err = store.Write(absPath, bytes.NewReader(content), size) + if err != nil { + t.Error(err) + } + defer func() { + err := store.Delete(absPath) + if err != nil { + t.Error(err) + } + }() + + Convey("Stat should get Object without error", func() { + o, err := store.Stat(absPath) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + So(o, ShouldNotBeNil) + So(o.Path, ShouldEqual, filepath.ToSlash(absPath)) + }) + }) + + Convey("Read should get Object content without error", func() { + var buf bytes.Buffer + n, err := store.Read(absPath, &buf) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The content should be match", func() { + So(buf, ShouldNotBeNil) + + So(n, ShouldEqual, size) + So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content)) + }) + }) + }) + + Convey("When using backslash in path", func() { + size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size)) + if err != nil { + t.Error(err) + } + + path := uuid.New().String() + "\\" + uuid.New().String() + _, err = store.Write(path, bytes.NewReader(content), size) + if err != nil { + t.Error(err) + } + defer func() { + err := store.Delete(path) + if err != nil { + t.Error(err) + } + }() + + Convey("Stat should get Object without error", func() { + o, err := store.Stat(path) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + So(o, ShouldNotBeNil) + So(o.Path, ShouldEqual, filepath.ToSlash(path)) + }) + }) + + Convey("Read should get Object content without error", func() { + var buf bytes.Buffer + n, err := store.Read(path, &buf) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The content should be match", func() { + So(buf, ShouldNotBeNil) + + So(n, ShouldEqual, size) + So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content)) + }) + }) + }) + }) }) }