Skip to content

Commit

Permalink
test: add some ACLStorageProvider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 28, 2023
1 parent 139354c commit 5b6fd4e
Showing 1 changed file with 182 additions and 0 deletions.
182 changes: 182 additions & 0 deletions storage/acl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package storage

import (
"github.com/devgianlu/go-fileshare"
"io"
"io/fs"
"testing"
)

type mockDirEntry struct {
name string
dir bool
}

func (m *mockDirEntry) Name() string {
return m.name
}

func (m *mockDirEntry) IsDir() bool {
return m.dir
}

func (m *mockDirEntry) Type() fs.FileMode {
panic("mock")
}

func (m *mockDirEntry) Info() (fs.FileInfo, error) {
panic("mock")
}

type mockStorageProvider struct {
dirEntries []fs.DirEntry
}

func (p *mockStorageProvider) CreateFile(string) (io.WriteCloser, error) {
return nil, nil
}

func (p *mockStorageProvider) OpenFile(string) (io.ReadCloser, fs.FileInfo, error) {
return nil, nil, nil
}

func (p *mockStorageProvider) ReadDir(string) ([]fs.DirEntry, error) {
return p.dirEntries, nil
}

func TestAclStorageProvider_CanRead(t *testing.T) {
user := &fileshare.User{
Nickname: "test",
Admin: false,
ACL: []fileshare.PathACL{
{
Path: "/test/foo/bar",
Read: true,
Write: true,
},
},
}

storage := NewACLStorageProvider(&mockStorageProvider{}, nil)

truePayloads := []string{
"/test/foo/bar",
"/test/foo/bar/",
"/test/foo",
"/test/foo/",
"/test/foo/../foo/bar",
"/test/foo/../../test/foo/bar",
"/test/baz/../foo/bar",
"/test/baz/baz/../../foo/bar",
}
for _, payload := range truePayloads {
if !storage.CanRead(payload, user) {
t.Fatalf("%s: expected true, got false", payload)
}
}

falsePayloads := []string{
"/test",
"/test/",
"/test/baz",
"/test/foo/..",
"/test/foo/../..",
"/test/foo/../baz",
"/test/foo/../../baz",
"/",
"/..",
"/../..",
}
for _, payload := range falsePayloads {
if storage.CanRead(payload, user) {
t.Fatalf("%s: expected false, got true", payload)
}
}
}

func TestAclStorageProvider_CanWrite(t *testing.T) {
user := &fileshare.User{
Nickname: "test",
Admin: false,
ACL: []fileshare.PathACL{
{
Path: "/test/foo/bar",
Read: true,
Write: true,
},
},
}

storage := NewACLStorageProvider(&mockStorageProvider{}, nil)

truePayloads := []string{
"/test/foo/bar",
"/test/foo/bar/",
"/test/foo/../foo/bar",
"/test/foo/../../test/foo/bar",
"/test/baz/../foo/bar",
"/test/baz/baz/../../foo/bar",
}
for _, payload := range truePayloads {
if !storage.CanWrite(payload, user) {
t.Fatalf("%s: expected true, got false", payload)
}
}

falsePayloads := []string{
"/test",
"/test/",
"/test/foo",
"/test/foo/",
"/test/baz",
"/test/foo/..",
"/test/foo/../..",
"/test/foo/../baz",
"/test/foo/../../baz",
"/",
"/..",
"/../..",
}
for _, payload := range falsePayloads {
if storage.CanWrite(payload, user) {
t.Fatalf("%s: expected false, got true", payload)
}
}
}

func TestAclStorageProvider_ReadDir(t *testing.T) {
user := &fileshare.User{
Nickname: "test",
Admin: false,
ACL: []fileshare.PathACL{
{
Path: "/test",
Read: true,
Write: true,
},
},
}

storage := NewACLStorageProvider(&mockStorageProvider{
dirEntries: []fs.DirEntry{
&mockDirEntry{"bar.txt", false},
&mockDirEntry{"foo", true},
&mockDirEntry{"test", true},
},
}, nil)

payloads := []string{
"/",
"/..",
"/../..",
"/test/..",
"/test/../..",
"/test/foo/../..",
"/test/foo/bar/../../..",
}
for _, payload := range payloads {
if entries, _ := storage.ReadDir(payload, user); len(entries) != 1 || entries[0].Name() != "test" {
t.Fatalf("%s: expected \"test\" entry, got %v", payload, entries)
}
}
}

0 comments on commit 5b6fd4e

Please sign in to comment.