]> git.r.bdr.sh - rbdr/mobius/commitdiff
Implement Make Alias transaction
authorJeff Halter <redacted>
Sat, 28 May 2022 18:12:00 +0000 (11:12 -0700)
committerJeff Halter <redacted>
Sat, 28 May 2022 18:12:00 +0000 (11:12 -0700)
hotline/access.go
hotline/file_store.go
hotline/transaction.go
hotline/transaction_handlers.go
hotline/transaction_handlers_test.go

index e2076ad1af4f738b36675ce39cae552f8c91b9e5..1cdbeed33e9d04bfd104c932a700f58758c1f840 100644 (file)
@@ -40,7 +40,7 @@ const (
        //accessSetFileComment   = 28
        //accessSetFolderComment = 29
        //accessViewDropBoxes    = 30
        //accessSetFileComment   = 28
        //accessSetFolderComment = 29
        //accessViewDropBoxes    = 30
-       //accessMakeAlias        = 31
+       accessMakeAlias     = 31
        accessBroadcast     = 32
        accessNewsDeleteArt = 33
        accessNewsCreateCat = 34
        accessBroadcast     = 32
        accessNewsDeleteArt = 33
        accessNewsCreateCat = 34
index b56d9a180140fb0d643430b6bb5123772a704927..c1c392905c3704791014653d9937f993114af541 100644 (file)
@@ -11,6 +11,8 @@ type FileStore interface {
        Mkdir(name string, perm os.FileMode) error
        Stat(name string) (os.FileInfo, error)
        Open(name string) (*os.File, error)
        Mkdir(name string, perm os.FileMode) error
        Stat(name string) (os.FileInfo, error)
        Open(name string) (*os.File, error)
+       Symlink(oldname, newname string) error
+
        // TODO: implement these
        //Rename(oldpath string, newpath string) error
        //RemoveAll(path string) error
        // TODO: implement these
        //Rename(oldpath string, newpath string) error
        //RemoveAll(path string) error
@@ -30,6 +32,10 @@ func (fs OSFileStore) Open(name string) (*os.File, error) {
        return os.Open(name)
 }
 
        return os.Open(name)
 }
 
+func (fs OSFileStore) Symlink(oldname, newname string) error {
+       return os.Symlink(oldname, newname)
+}
+
 type MockFileStore struct {
        mock.Mock
 }
 type MockFileStore struct {
        mock.Mock
 }
@@ -52,3 +58,8 @@ func (mfs MockFileStore) Open(name string) (*os.File, error) {
        args := mfs.Called(name)
        return args.Get(0).(*os.File), args.Error(1)
 }
        args := mfs.Called(name)
        return args.Get(0).(*os.File), args.Error(1)
 }
+
+func (mfs MockFileStore) Symlink(oldname, newname string) error {
+       args := mfs.Called(oldname, newname)
+       return args.Error(0)
+}
index 835d194b24e5c445bcc741013c8e1f587125ab32..2994785cca1194c6352ad29281e8090907dfde60 100644 (file)
@@ -40,8 +40,8 @@ const (
        tranGetFileInfo          = 206
        tranSetFileInfo          = 207
        tranMoveFile             = 208
        tranGetFileInfo          = 206
        tranSetFileInfo          = 207
        tranMoveFile             = 208
-       // tranMakeFileAlias        = 209 TODO: implement file alias command
-       tranDownloadFldr = 210
+       tranMakeFileAlias        = 209 // TODO: implement file alias command
+       tranDownloadFldr         = 210
        // tranDownloadInfo         = 211 TODO: implement file transfer queue
        // tranDownloadBanner     = 212 TODO: figure out what this is used for
        tranUploadFldr        = 213
        // tranDownloadInfo         = 211 TODO: implement file transfer queue
        // tranDownloadBanner     = 212 TODO: figure out what this is used for
        tranUploadFldr        = 213
index 7e3afa14de1df9217968745a98c43003bc02331b..ade846e6d3d283a3149cfeb06fe7980263eb1309 100644 (file)
@@ -256,6 +256,16 @@ var TransactionHandlers = map[uint16]TransactionType{
                Name:    "tranSetChatSubject",
                Handler: HandleSetChatSubject,
        },
                Name:    "tranSetChatSubject",
                Handler: HandleSetChatSubject,
        },
+       tranMakeFileAlias: {
+               Access:  accessAlwaysAllow,
+               Name:    "tranMakeFileAlias",
+               Handler: HandleMakeAlias,
+               RequiredFields: []requiredField{
+                       {ID: fieldFileName, minLen: 1},
+                       {ID: fieldFilePath, minLen: 1},
+                       {ID: fieldFileNewPath, minLen: 1},
+               },
+       },
        tranSetClientUserInfo: {
                Access:  accessAlwaysAllow,
                Name:    "tranSetClientUserInfo",
        tranSetClientUserInfo: {
                Access:  accessAlwaysAllow,
                Name:    "tranSetClientUserInfo",
@@ -1609,3 +1619,41 @@ func HandleSetChatSubject(cc *ClientConn, t *Transaction) (res []Transaction, er
 
        return res, err
 }
 
        return res, err
 }
+
+// HandleMakeAlias makes a file alias using the specified path.
+// Fields used in the request:
+// 201 File name
+// 202 File path
+// 212 File new path   Destination path
+//
+// Fields used in the reply:
+// None
+func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
+       if !authorize(cc.Account.Access, accessMakeAlias) {
+               res = append(res, cc.NewErrReply(t, "You are not allowed to make aliases."))
+               return res, err
+       }
+       fileName := t.GetField(fieldFileName).Data
+       filePath := t.GetField(fieldFilePath).Data
+       fileNewPath := t.GetField(fieldFileNewPath).Data
+
+       fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
+       if err != nil {
+               return res, err
+       }
+
+       fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, fileNewPath, fileName)
+       if err != nil {
+               return res, err
+       }
+
+       cc.Server.Logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
+
+       if err := FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
+               res = append(res, cc.NewErrReply(t, "Error creating alias"))
+               return res, nil
+       }
+
+       res = append(res, cc.NewReply(t))
+       return res, err
+}
index 7c5ee43268cad1433177115e637cafb5832bf656..a83f4cca48a9c2cdb7d269be699fb3f0e0a141e6 100644 (file)
@@ -1,10 +1,12 @@
 package hotline
 
 import (
 package hotline
 
 import (
+       "errors"
        "github.com/stretchr/testify/assert"
        "io/fs"
        "math/rand"
        "os"
        "github.com/stretchr/testify/assert"
        "io/fs"
        "math/rand"
        "os"
+       "strings"
        "testing"
 )
 
        "testing"
 )
 
@@ -855,3 +857,187 @@ func TestHandleUploadFile(t *testing.T) {
                })
        }
 }
                })
        }
 }
+
+func TestHandleMakeAlias(t *testing.T) {
+       type args struct {
+               cc *ClientConn
+               t  *Transaction
+       }
+       tests := []struct {
+               name    string
+               setup   func()
+               args    args
+               wantRes []Transaction
+               wantErr bool
+       }{
+               {
+                       name: "with valid input and required permissions",
+                       setup: func() {
+                               mfs := MockFileStore{}
+                               path, _ := os.Getwd()
+                               mfs.On(
+                                       "Symlink",
+                                       path+"/test/config/Files/foo/testFile",
+                                       path+"/test/config/Files/bar/testFile",
+                               ).Return(nil)
+                               FS = mfs
+                       },
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessMakeAlias)
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                                       Server: &Server{
+                                               Config: &Config{
+                                                       FileRoot: func() string {
+                                                               path, _ := os.Getwd()
+                                                               return path + "/test/config/Files"
+                                                       }(),
+                                               },
+                                               Logger: NewTestLogger(),
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranMakeFileAlias, &[]byte{0, 1},
+                                       NewField(fieldFileName, []byte("testFile")),
+                                       NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
+                                       NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0, 0xd1},
+                                       ID:        []byte{0x9a, 0xcb, 0x04, 0x42},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields:    []Field(nil),
+                               },
+                       },
+                       wantErr: false,
+               },
+               {
+                       name: "when symlink returns an error",
+                       setup: func() {
+                               mfs := MockFileStore{}
+                               path, _ := os.Getwd()
+                               mfs.On(
+                                       "Symlink",
+                                       path+"/test/config/Files/foo/testFile",
+                                       path+"/test/config/Files/bar/testFile",
+                               ).Return(errors.New("ohno"))
+                               FS = mfs
+                       },
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessMakeAlias)
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                                       Server: &Server{
+                                               Config: &Config{
+                                                       FileRoot: func() string {
+                                                               path, _ := os.Getwd()
+                                                               return path + "/test/config/Files"
+                                                       }(),
+                                               },
+                                               Logger: NewTestLogger(),
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranMakeFileAlias, &[]byte{0, 1},
+                                       NewField(fieldFileName, []byte("testFile")),
+                                       NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
+                                       NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0, 0x00},
+                                       ID:        []byte{0x9a, 0xcb, 0x04, 0x42},
+                                       ErrorCode: []byte{0, 0, 0, 1},
+                                       Fields: []Field{
+                                               NewField(fieldError, []byte("Error creating alias")),
+                                       },
+                               },
+                       },
+                       wantErr: false,
+               },
+               {
+                       name:  "when user does not have required permission",
+                       setup: func() {},
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                                       Server: &Server{
+                                               Config: &Config{
+                                                       FileRoot: func() string {
+                                                               path, _ := os.Getwd()
+                                                               return path + "/test/config/Files"
+                                                       }(),
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranMakeFileAlias, &[]byte{0, 1},
+                                       NewField(fieldFileName, []byte("testFile")),
+                                       NewField(fieldFilePath, []byte{
+                                               0x00, 0x01,
+                                               0x00, 0x00,
+                                               0x03,
+                                               0x2e, 0x2e, 0x2e,
+                                       }),
+                                       NewField(fieldFileNewPath, []byte{
+                                               0x00, 0x01,
+                                               0x00, 0x00,
+                                               0x03,
+                                               0x2e, 0x2e, 0x2e,
+                                       }),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0, 0x00},
+                                       ID:        []byte{0x9a, 0xcb, 0x04, 0x42},
+                                       ErrorCode: []byte{0, 0, 0, 1},
+                                       Fields: []Field{
+                                               NewField(fieldError, []byte("You are not allowed to make aliases.")),
+                                       },
+                               },
+                       },
+                       wantErr: false,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       tt.setup()
+
+                       gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
+                       if (err != nil) != tt.wantErr {
+                               t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
+                               return
+                       }
+
+                       tranAssertEqual(t, tt.wantRes, gotRes)
+               })
+       }
+}