fix(db): aggregate store pkgs under db dir
This commit is contained in:
parent
8e7dd50efd
commit
4d8a8999a5
15 changed files with 13 additions and 13 deletions
199
src/db/fileinfostore/file_info_store.go
Normal file
199
src/db/fileinfostore/file_info_store.go
Normal file
|
@ -0,0 +1,199 @@
|
|||
package fileinfostore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore"
|
||||
)
|
||||
|
||||
const (
|
||||
InitNs = "Init"
|
||||
InfoNs = "sharing"
|
||||
InitTimeKey = "initTime"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("file info not found")
|
||||
)
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
return err == ErrNotFound
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
IsDir bool `json:"isDir"`
|
||||
Shared bool `json:"shared"`
|
||||
Sha1 string `json:"sha1"`
|
||||
}
|
||||
|
||||
type IFileInfoStore interface {
|
||||
AddSharing(dirPath string) error
|
||||
DelSharing(dirPath string) error
|
||||
GetSharing(dirPath string) (bool, bool)
|
||||
ListSharings(prefix string) (map[string]bool, error)
|
||||
GetInfo(itemPath string) (*FileInfo, error)
|
||||
SetInfo(itemPath string, info *FileInfo) error
|
||||
DelInfo(itemPath string) error
|
||||
SetSha1(itemPath, sign string) error
|
||||
GetInfos(itemPaths []string) (map[string]*FileInfo, error)
|
||||
}
|
||||
|
||||
type FileInfoStore struct {
|
||||
mtx *sync.RWMutex
|
||||
store kvstore.IKVStore
|
||||
}
|
||||
|
||||
func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
|
||||
_, ok := store.GetStringIn(InitNs, InitTimeKey)
|
||||
if !ok {
|
||||
var err error
|
||||
for _, nsName := range []string{
|
||||
InitNs,
|
||||
InfoNs,
|
||||
} {
|
||||
if err = store.AddNamespace(nsName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FileInfoStore{
|
||||
store: store,
|
||||
mtx: &sync.RWMutex{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) AddSharing(dirPath string) error {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(dirPath)
|
||||
if err != nil {
|
||||
if !IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
info = &FileInfo{
|
||||
IsDir: true,
|
||||
}
|
||||
}
|
||||
info.Shared = true
|
||||
return fi.SetInfo(dirPath, info)
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) DelSharing(dirPath string) error {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(dirPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info.Shared = false
|
||||
return fi.SetInfo(dirPath, info)
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) GetSharing(dirPath string) (bool, bool) {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(dirPath)
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
return info.IsDir && info.Shared, true
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) ListSharings(prefix string) (map[string]bool, error) {
|
||||
infoStrs, err := fi.store.ListStringsByPrefixIn(prefix, InfoNs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := &FileInfo{}
|
||||
sharings := map[string]bool{}
|
||||
for itemPath, infoStr := range infoStrs {
|
||||
err = json.Unmarshal([]byte(infoStr), info)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list sharing error: %w", err)
|
||||
}
|
||||
if info.IsDir && info.Shared {
|
||||
sharings[itemPath] = true
|
||||
}
|
||||
}
|
||||
|
||||
return sharings, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) GetInfo(itemPath string) (*FileInfo, error) {
|
||||
infoStr, ok := fi.store.GetStringIn(InfoNs, itemPath)
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
info := &FileInfo{}
|
||||
err := json.Unmarshal([]byte(infoStr), info)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get file info: %w", err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) GetInfos(itemPaths []string) (map[string]*FileInfo, error) {
|
||||
infos := map[string]*FileInfo{}
|
||||
for _, itemPath := range itemPaths {
|
||||
info, err := fi.GetInfo(itemPath)
|
||||
if err != nil {
|
||||
if !IsNotFound(err) {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
infos[itemPath] = info
|
||||
}
|
||||
|
||||
return infos, nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) SetInfo(itemPath string, info *FileInfo) error {
|
||||
infoStr, err := json.Marshal(info)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set file info: %w", err)
|
||||
}
|
||||
|
||||
err = fi.store.SetStringIn(InfoNs, itemPath, string(infoStr))
|
||||
if err != nil {
|
||||
return fmt.Errorf("set file info: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) DelInfo(itemPath string) error {
|
||||
return fi.store.DelStringIn(InfoNs, itemPath)
|
||||
}
|
||||
|
||||
func (fi *FileInfoStore) SetSha1(itemPath, sign string) error {
|
||||
fi.mtx.Lock()
|
||||
defer fi.mtx.Unlock()
|
||||
|
||||
info, err := fi.GetInfo(itemPath)
|
||||
if err != nil {
|
||||
if !IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
info = &FileInfo{
|
||||
IsDir: false,
|
||||
Shared: false,
|
||||
}
|
||||
}
|
||||
info.Sha1 = sign
|
||||
return fi.SetInfo(itemPath, info)
|
||||
}
|
133
src/db/fileinfostore/file_info_store_test.go
Normal file
133
src/db/fileinfostore/file_info_store_test.go
Normal file
|
@ -0,0 +1,133 @@
|
|||
package fileinfostore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
||||
)
|
||||
|
||||
func TestUserStores(t *testing.T) {
|
||||
|
||||
testSharingMethods := func(t *testing.T, store IFileInfoStore) {
|
||||
dirPaths := []string{"admin/path1", "admin/path1/path2"}
|
||||
var err error
|
||||
|
||||
// add sharings
|
||||
for _, dirPath := range dirPaths {
|
||||
err = store.AddSharing(dirPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// list sharings
|
||||
prefix := "admin"
|
||||
sharingMap, err := store.ListSharings(prefix)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sharingDir := range dirPaths {
|
||||
if !sharingMap[sharingDir] {
|
||||
t.Fatalf("sharing(%s) not found", sharingDir)
|
||||
}
|
||||
mustTrue, exist := store.GetSharing(sharingDir)
|
||||
if !mustTrue || !exist {
|
||||
t.Fatalf("get sharing(%t %t) should exist", mustTrue, exist)
|
||||
}
|
||||
}
|
||||
|
||||
// del sharings
|
||||
for _, dirPath := range dirPaths {
|
||||
err = store.DelSharing(dirPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
sharingMap, err = store.ListSharings(prefix)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, dirPath := range dirPaths {
|
||||
if _, ok := sharingMap[dirPath]; ok {
|
||||
t.Fatalf("sharing(%s) should not exist", dirPath)
|
||||
}
|
||||
shared, exist := store.GetSharing(dirPath)
|
||||
if shared {
|
||||
t.Fatalf("get sharing(%t, %t) should not shared but exist", shared, exist)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testInfoMethods := func(t *testing.T, store IFileInfoStore) {
|
||||
pathInfos := map[string]*FileInfo{
|
||||
"admin/item": &FileInfo{
|
||||
Shared: false,
|
||||
IsDir: false,
|
||||
Sha1: "file",
|
||||
},
|
||||
"admin/dir": &FileInfo{
|
||||
Shared: true,
|
||||
IsDir: true,
|
||||
Sha1: "dir",
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
// add infos
|
||||
for itemPath, info := range pathInfos {
|
||||
err = store.SetInfo(itemPath, info)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// get infos
|
||||
for itemPath, expected := range pathInfos {
|
||||
info, err := store.GetInfo(itemPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Shared != expected.Shared || info.IsDir != expected.IsDir || info.Sha1 != expected.Sha1 {
|
||||
t.Fatalf("info not equaled (%v) (%v)", expected, info)
|
||||
}
|
||||
}
|
||||
|
||||
// del sharings
|
||||
for itemPath := range pathInfos {
|
||||
err = store.DelInfo(itemPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// get infos
|
||||
for itemPath := range pathInfos {
|
||||
_, err := store.GetInfo(itemPath)
|
||||
if !IsNotFound(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("testing FileInfoStore", func(t *testing.T) {
|
||||
rootPath, err := ioutil.TempDir("./", "quickshare_userstore_test_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(rootPath)
|
||||
|
||||
kvstore := boltdbpvd.New(rootPath, 1024)
|
||||
defer kvstore.Close()
|
||||
|
||||
store, err := NewFileInfoStore(kvstore)
|
||||
if err != nil {
|
||||
t.Fatal("fail to new kvstore", err)
|
||||
}
|
||||
|
||||
testSharingMethods(t, store)
|
||||
testInfoMethods(t, store)
|
||||
})
|
||||
}
|
421
src/db/userstore/user_store.go
Normal file
421
src/db/userstore/user_store.go
Normal file
|
@ -0,0 +1,421 @@
|
|||
package userstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore"
|
||||
)
|
||||
|
||||
const (
|
||||
AdminRole = "admin"
|
||||
UserRole = "user"
|
||||
VisitorRole = "visitor"
|
||||
InitNs = "usersInit"
|
||||
IDsNs = "ids"
|
||||
UsersNs = "users"
|
||||
RoleListNs = "roleList"
|
||||
InitTimeKey = "initTime"
|
||||
VisitorID = uint64(1)
|
||||
VisitorName = "visitor"
|
||||
|
||||
defaultSpaceLimit = 1024 * 1024 * 1024 // 1GB
|
||||
defaultUploadSpeedLimit = 50 * 1024 * 1024 // 50MB
|
||||
defaultDownloadSpeedLimit = 50 * 1024 * 1024 // 50MB
|
||||
visitorUploadSpeedLimit = 10 * 1024 * 1024 // 10MB
|
||||
visitorDownloadSpeedLimit = 10 * 1024 * 1024 // 10MB
|
||||
)
|
||||
|
||||
var (
|
||||
ErrReachedLimit = errors.New("reached space limit")
|
||||
)
|
||||
|
||||
func IsReachedLimitErr(err error) bool {
|
||||
return err == ErrReachedLimit
|
||||
}
|
||||
|
||||
type Quota struct {
|
||||
SpaceLimit int64 `json:"spaceLimit,string"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||
}
|
||||
|
||||
type UserCfg struct {
|
||||
Name string `json:"name"`
|
||||
Role string `json:"role"`
|
||||
Pwd string `json:"pwd"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uint64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Role string `json:"role"`
|
||||
UsedSpace int64 `json:"usedSpace,string"`
|
||||
Quota *Quota `json:"quota"`
|
||||
}
|
||||
|
||||
type IUserStore interface {
|
||||
Init(rootName, rootPwd string) error
|
||||
IsInited() bool
|
||||
AddUser(user *User) error
|
||||
DelUser(id uint64) error
|
||||
GetUser(id uint64) (*User, error)
|
||||
GetUserByName(name string) (*User, error)
|
||||
SetInfo(id uint64, user *User) error
|
||||
CanIncrUsed(id uint64, capacity int64) (bool, error)
|
||||
SetUsed(id uint64, incr bool, capacity int64) error
|
||||
SetPwd(id uint64, pwd string) error
|
||||
ListUsers() ([]*User, error)
|
||||
AddRole(role string) error
|
||||
DelRole(role string) error
|
||||
ListRoles() (map[string]bool, error)
|
||||
}
|
||||
|
||||
type KVUserStore struct {
|
||||
store kvstore.IKVStore
|
||||
mtx *sync.RWMutex
|
||||
}
|
||||
|
||||
func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
|
||||
_, ok := store.GetStringIn(InitNs, InitTimeKey)
|
||||
if !ok {
|
||||
var err error
|
||||
for _, nsName := range []string{
|
||||
IDsNs,
|
||||
UsersNs,
|
||||
InitNs,
|
||||
RoleListNs,
|
||||
} {
|
||||
if err = store.AddNamespace(nsName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &KVUserStore{
|
||||
store: store,
|
||||
mtx: &sync.RWMutex{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) Init(rootName, rootPwd string) error {
|
||||
var err error
|
||||
admin := &User{
|
||||
ID: 0,
|
||||
Name: rootName,
|
||||
Pwd: rootPwd,
|
||||
Role: AdminRole,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: defaultSpaceLimit,
|
||||
UploadSpeedLimit: defaultUploadSpeedLimit,
|
||||
DownloadSpeedLimit: defaultDownloadSpeedLimit,
|
||||
},
|
||||
}
|
||||
visitor := &User{
|
||||
ID: VisitorID,
|
||||
Name: VisitorName,
|
||||
Pwd: rootPwd,
|
||||
Role: VisitorRole,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: 0,
|
||||
UploadSpeedLimit: visitorUploadSpeedLimit,
|
||||
DownloadSpeedLimit: visitorDownloadSpeedLimit,
|
||||
},
|
||||
}
|
||||
|
||||
for _, user := range []*User{admin, visitor} {
|
||||
err = us.AddUser(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, role := range []string{AdminRole, UserRole, VisitorRole} {
|
||||
err = us.AddRole(role)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) IsInited() bool {
|
||||
_, ok := us.store.GetStringIn(InitNs, InitTimeKey)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (us *KVUserStore) AddUser(user *User) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(user.ID)
|
||||
_, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if ok {
|
||||
return fmt.Errorf("userID (%d) exists", user.ID)
|
||||
}
|
||||
if user.Name == "" || user.Pwd == "" {
|
||||
return errors.New("user name or password can not be empty")
|
||||
}
|
||||
_, ok = us.store.GetStringIn(IDsNs, user.Name)
|
||||
if ok {
|
||||
return fmt.Errorf("user name (%s) exists", user.Name)
|
||||
}
|
||||
|
||||
err := us.store.SetStringIn(IDsNs, user.Name, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userBytes, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(userBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) DelUser(id uint64) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
name, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("userID (%s) does not exist", userID)
|
||||
}
|
||||
|
||||
// TODO: add complement operations if part of the actions fails
|
||||
err1 := us.store.DelStringIn(IDsNs, name)
|
||||
err2 := us.store.DelStringIn(UsersNs, userID)
|
||||
if err1 != nil || err2 != nil {
|
||||
return fmt.Errorf("get id(%s) user(%s)", err1, err2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) GetUser(id uint64) (*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user (%s) not found", userID)
|
||||
}
|
||||
user := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gotID, ok := us.store.GetStringIn(IDsNs, user.Name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user id (%s) not found", user.Name)
|
||||
} else if gotID != userID {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", user.Name, gotID, userID)
|
||||
}
|
||||
|
||||
// TODO: use sync.Pool instead
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (us *KVUserStore) GetUserByName(name string) (*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
userID, ok := us.store.GetStringIn(IDsNs, name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user id (%s) not found", name)
|
||||
}
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user name (%s) not found", userID)
|
||||
}
|
||||
|
||||
user := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user.Name != name {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", userID, user.Name, name)
|
||||
}
|
||||
|
||||
// TODO: use sync.Pool instead
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
gotUser.Pwd = pwd
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) CanIncrUsed(id uint64, capacity int64) (bool, error) {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return false, err
|
||||
} else if gotUser.ID != id {
|
||||
return false, fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
return gotUser.UsedSpace+capacity <= int64(gotUser.Quota.SpaceLimit), nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetUsed(id uint64, incr bool, capacity int64) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
if incr && gotUser.UsedSpace+capacity > int64(gotUser.Quota.SpaceLimit) {
|
||||
return ErrReachedLimit
|
||||
}
|
||||
|
||||
if incr {
|
||||
gotUser.UsedSpace = gotUser.UsedSpace + capacity
|
||||
} else {
|
||||
gotUser.UsedSpace = gotUser.UsedSpace - capacity
|
||||
}
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetInfo(id uint64, user *User) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
// name and password can not be updated here
|
||||
if user.Role != "" {
|
||||
gotUser.Role = user.Role
|
||||
}
|
||||
if user.Quota != nil {
|
||||
gotUser.Quota = user.Quota
|
||||
}
|
||||
if user.UsedSpace > 0 {
|
||||
gotUser.UsedSpace = user.UsedSpace
|
||||
}
|
||||
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) ListUsers() ([]*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
idToInfo, err := us.store.ListStringsIn(UsersNs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
users := []*User{}
|
||||
for _, infoStr := range idToInfo {
|
||||
user := &User{}
|
||||
err = json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Pwd = ""
|
||||
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) AddRole(role string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
_, ok := us.store.GetBoolIn(RoleListNs, role)
|
||||
if ok {
|
||||
return fmt.Errorf("role (%s) exists", role)
|
||||
}
|
||||
|
||||
return us.store.SetBoolIn(RoleListNs, role, true)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) DelRole(role string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
if role == AdminRole || role == UserRole || role == VisitorRole {
|
||||
return errors.New("predefined roles can not be deleted")
|
||||
}
|
||||
|
||||
return us.store.DelBoolIn(RoleListNs, role)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) ListRoles() (map[string]bool, error) {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
return us.store.ListBoolsIn(RoleListNs)
|
||||
}
|
286
src/db/userstore/user_store_test.go
Normal file
286
src/db/userstore/user_store_test.go
Normal file
|
@ -0,0 +1,286 @@
|
|||
package userstore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
||||
)
|
||||
|
||||
func TestUserStores(t *testing.T) {
|
||||
rootName, rootPwd := "root", "rootPwd"
|
||||
|
||||
testUserMethods := func(t *testing.T, store IUserStore) {
|
||||
root, err := store.GetUser(0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if root.Name != rootName {
|
||||
t.Fatal("root user not found")
|
||||
}
|
||||
if root.Pwd != rootPwd {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if root.Role != AdminRole {
|
||||
t.Fatalf("incorrect root role")
|
||||
}
|
||||
if root.Quota.SpaceLimit != defaultSpaceLimit {
|
||||
t.Fatalf("incorrect root SpaceLimit")
|
||||
}
|
||||
if root.Quota.UploadSpeedLimit != defaultUploadSpeedLimit {
|
||||
t.Fatalf("incorrect root UploadSpeedLimit")
|
||||
}
|
||||
if root.Quota.DownloadSpeedLimit != defaultDownloadSpeedLimit {
|
||||
t.Fatalf("incorrect root DownloadSpeedLimit")
|
||||
}
|
||||
|
||||
visitor, err := store.GetUser(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if visitor.Name != VisitorName {
|
||||
t.Fatal("visitor not found")
|
||||
}
|
||||
if visitor.Pwd != rootPwd {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if visitor.Role != VisitorRole {
|
||||
t.Fatalf("incorrect visitor role")
|
||||
}
|
||||
if visitor.Quota.SpaceLimit != 0 {
|
||||
t.Fatalf("incorrect visitor SpaceLimit")
|
||||
}
|
||||
if visitor.Quota.UploadSpeedLimit != visitorUploadSpeedLimit {
|
||||
t.Fatalf("incorrect visitor UploadSpeedLimit")
|
||||
}
|
||||
if visitor.Quota.DownloadSpeedLimit != visitorDownloadSpeedLimit {
|
||||
t.Fatalf("incorrect visitor DownloadSpeedLimit")
|
||||
}
|
||||
|
||||
id, name1 := uint64(2), "test_user1"
|
||||
pwd1, pwd2 := "666", "888"
|
||||
role1, role2 := UserRole, AdminRole
|
||||
spaceLimit1, upLimit1, downLimit1 := int64(17), 5, 7
|
||||
spaceLimit2, upLimit2, downLimit2 := int64(19), 13, 17
|
||||
|
||||
err = store.AddUser(&User{
|
||||
ID: id,
|
||||
Name: name1,
|
||||
Pwd: pwd1,
|
||||
Role: role1,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: spaceLimit1,
|
||||
UploadSpeedLimit: upLimit1,
|
||||
DownloadSpeedLimit: downLimit1,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("there should be no error")
|
||||
}
|
||||
|
||||
user, err := store.GetUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.Name != name1 {
|
||||
t.Fatalf("names not matched %s %s", name1, user.Name)
|
||||
}
|
||||
if user.Pwd != pwd1 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role1 {
|
||||
t.Fatalf("roles not matched %s %s", role1, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit1 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit1, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit1 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit1, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit1 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit1, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
users, err := store.ListUsers()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(users) != 3 {
|
||||
t.Fatalf("users size should be 3 (%d)", len(users))
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID == 0 {
|
||||
if user.Name != rootName || user.Role != AdminRole {
|
||||
t.Fatalf("incorrect root info %v", user)
|
||||
}
|
||||
}
|
||||
if user.ID == id {
|
||||
if user.Name != name1 || user.Role != role1 {
|
||||
t.Fatalf("incorrect user info %v", user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = store.SetPwd(id, pwd2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store.SetInfo(id, &User{
|
||||
ID: id,
|
||||
Role: role2,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: spaceLimit2,
|
||||
UploadSpeedLimit: upLimit2,
|
||||
DownloadSpeedLimit: downLimit2,
|
||||
},
|
||||
})
|
||||
|
||||
usedIncr, usedDecr := int64(spaceLimit2), int64(7)
|
||||
err = store.SetUsed(id, true, usedIncr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetUsed(id, false, usedDecr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetUsed(id, true, int64(spaceLimit2)-(usedIncr-usedDecr)+1)
|
||||
if err == nil || !strings.Contains(err.Error(), "reached space limit") {
|
||||
t.Fatal("should reject big file")
|
||||
} else {
|
||||
err = nil
|
||||
}
|
||||
|
||||
user, err = store.GetUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.Pwd != pwd2 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role2 {
|
||||
t.Fatalf("roles not matched %s %s", role2, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit2 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit2, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit2 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit2, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
if user.UsedSpace != usedIncr-usedDecr {
|
||||
t.Fatalf("used space not matched %d %d", user.UsedSpace, usedIncr-usedDecr)
|
||||
}
|
||||
|
||||
user, err = store.GetUserByName(name1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.ID != id {
|
||||
t.Fatalf("ids not matched %d %d", id, user.ID)
|
||||
}
|
||||
if user.Pwd != pwd2 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role2 {
|
||||
t.Fatalf("roles not matched %s %s", role2, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit2 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit2, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit2 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit2, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
err = store.DelUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users, err = store.ListUsers()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(users) != 2 {
|
||||
t.Fatalf("users size should be 2 (%d)", len(users))
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID == 0 && user.Name != rootName && user.Role != AdminRole {
|
||||
t.Fatalf("incorrect root info %v", user)
|
||||
}
|
||||
if user.ID == VisitorID && user.Name != VisitorName && user.Role != VisitorRole {
|
||||
t.Fatalf("incorrect visitor info %v", user)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
testRoleMethods := func(t *testing.T, store IUserStore) {
|
||||
roles := []string{"role1", "role2"}
|
||||
var err error
|
||||
for _, role := range roles {
|
||||
err = store.AddRole(role)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
roleMap, err := store.ListRoles()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, role := range append(roles, []string{
|
||||
AdminRole, UserRole, VisitorRole,
|
||||
}...) {
|
||||
if !roleMap[role] {
|
||||
t.Fatalf("role(%s) not found", role)
|
||||
}
|
||||
}
|
||||
|
||||
for _, role := range roles {
|
||||
err = store.DelRole(role)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
roleMap, err = store.ListRoles()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, role := range roles {
|
||||
if roleMap[role] {
|
||||
t.Fatalf("role(%s) should not exist", role)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("testing KVUserStore", func(t *testing.T) {
|
||||
rootPath, err := ioutil.TempDir("./", "quickshare_userstore_test_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(rootPath)
|
||||
|
||||
kvstore := boltdbpvd.New(rootPath, 1024)
|
||||
defer kvstore.Close()
|
||||
|
||||
store, err := NewKVUserStore(kvstore)
|
||||
if err != nil {
|
||||
t.Fatal("fail to new kvstore", err)
|
||||
}
|
||||
if err = store.Init(rootName, rootPwd); err != nil {
|
||||
t.Fatal("fail to init kvstore", err)
|
||||
}
|
||||
|
||||
testUserMethods(t, store)
|
||||
testRoleMethods(t, store)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue