]> git.r.bdr.sh - rbdr/mobius/commitdiff
Fix more filepaths
authorJeff Halter <redacted>
Tue, 21 Jun 2022 17:29:46 +0000 (10:29 -0700)
committerJeff Halter <redacted>
Tue, 21 Jun 2022 17:29:46 +0000 (10:29 -0700)
hotline/file_path.go
hotline/test/config/Files/getFileNameListTestDir/testfile-1k [new file with mode: 0644]
hotline/transaction_handlers.go
hotline/transaction_handlers_test.go

index c7e1574f39b7f89b6f20612063b86795682f9143..460af03464cc2e5376769a30f2a1b5951cb4d785 100644 (file)
@@ -76,15 +76,6 @@ func (fp *FilePath) Len() uint16 {
        return binary.BigEndian.Uint16(fp.ItemCount[:])
 }
 
        return binary.BigEndian.Uint16(fp.ItemCount[:])
 }
 
-func (fp *FilePath) String() string {
-       out := []string{"/"}
-       for _, i := range fp.Items {
-               out = append(out, string(i.Name))
-       }
-
-       return filepath.Join(out...)
-}
-
 func readPath(fileRoot string, filePath, fileName []byte) (fullPath string, err error) {
        var fp FilePath
        if filePath != nil {
 func readPath(fileRoot string, filePath, fileName []byte) (fullPath string, err error) {
        var fp FilePath
        if filePath != nil {
@@ -93,9 +84,14 @@ func readPath(fileRoot string, filePath, fileName []byte) (fullPath string, err
                }
        }
 
                }
        }
 
+       var subPath string
+       for _, pathItem := range fp.Items {
+               subPath = filepath.Join("/", subPath, string(pathItem.Name))
+       }
+
        fullPath = filepath.Join(
                fileRoot,
        fullPath = filepath.Join(
                fileRoot,
-               fp.String(),
+               subPath,
                filepath.Join("/", string(fileName)),
        )
 
                filepath.Join("/", string(fileName)),
        )
 
diff --git a/hotline/test/config/Files/getFileNameListTestDir/testfile-1k b/hotline/test/config/Files/getFileNameListTestDir/testfile-1k
new file mode 100644 (file)
index 0000000..31758a0
Binary files /dev/null and b/hotline/test/config/Files/getFileNameListTestDir/testfile-1k differ
index f080047563bda847aebba3855be3b24266d9cefc..cf55c07afa7d041bf4101be5872e728ed56744c3 100644 (file)
@@ -5,12 +5,12 @@ import (
        "encoding/binary"
        "errors"
        "fmt"
        "encoding/binary"
        "errors"
        "fmt"
-       "github.com/davecgh/go-spew/spew"
        "gopkg.in/yaml.v3"
        "io/ioutil"
        "math/big"
        "os"
        "path"
        "gopkg.in/yaml.v3"
        "io/ioutil"
        "math/big"
        "os"
        "path"
+       "path/filepath"
        "sort"
        "strings"
        "time"
        "sort"
        "strings"
        "time"
@@ -559,11 +559,12 @@ func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err err
                res = append(res, cc.NewErrReply(t, "You are not allowed to create folders."))
                return res, err
        }
                res = append(res, cc.NewErrReply(t, "You are not allowed to create folders."))
                return res, err
        }
-       newFolderPath := cc.Server.Config.FileRoot
        folderName := string(t.GetField(fieldFileName).Data)
 
        folderName = path.Join("/", folderName)
 
        folderName := string(t.GetField(fieldFileName).Data)
 
        folderName = path.Join("/", folderName)
 
+       var subPath string
+
        // fieldFilePath is only present for nested paths
        if t.GetField(fieldFilePath).Data != nil {
                var newFp FilePath
        // fieldFilePath is only present for nested paths
        if t.GetField(fieldFilePath).Data != nil {
                var newFp FilePath
@@ -571,9 +572,12 @@ func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err err
                if err != nil {
                        return nil, err
                }
                if err != nil {
                        return nil, err
                }
-               newFolderPath += newFp.String()
+
+               for _, pathItem := range newFp.Items {
+                       subPath = filepath.Join("/", subPath, string(pathItem.Name))
+               }
        }
        }
-       newFolderPath = path.Join(newFolderPath, folderName)
+       newFolderPath := path.Join(cc.Server.Config.FileRoot, subPath, folderName)
 
        // TODO: check path and folder name lengths
 
 
        // TODO: check path and folder name lengths
 
@@ -1659,8 +1663,6 @@ func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, e
                return res, err
        }
 
                return res, err
        }
 
-       spew.Dump(fullPath)
-
        var fp FilePath
        if t.GetField(fieldFilePath).Data != nil {
                if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
        var fp FilePath
        if t.GetField(fieldFilePath).Data != nil {
                if err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data); err != nil {
@@ -1670,7 +1672,7 @@ func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, e
 
        // Handle special case for drop box folders
        if fp.IsDropbox() && !authorize(cc.Account.Access, accessViewDropBoxes) {
 
        // Handle special case for drop box folders
        if fp.IsDropbox() && !authorize(cc.Account.Access, accessViewDropBoxes) {
-               res = append(res, cc.NewReply(t))
+               res = append(res, cc.NewErrReply(t, "You are not allowed to view drop boxes."))
                return res, err
        }
 
                return res, err
        }
 
index 0e2c9499c62fcddac2a8ce4ae31dd98be25823e1..8f9db57011e262240c2b1297ce17679a159a12d1 100644 (file)
@@ -2485,3 +2485,124 @@ func TestHandleDeleteFile(t *testing.T) {
                })
        }
 }
                })
        }
 }
+
+func TestHandleGetFileNameList(t *testing.T) {
+       type args struct {
+               cc *ClientConn
+               t  *Transaction
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantRes []Transaction
+               wantErr assert.ErrorAssertionFunc
+       }{
+               {
+                       name: "when fieldFilePath is a drop box, but user does not have accessViewDropBoxes ",
+                       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 filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
+                                                       }(),
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranGetFileNameList, &[]byte{0, 1},
+                                       NewField(fieldFilePath, []byte{
+                                               0x00, 0x01,
+                                               0x00, 0x00,
+                                               0x08,
+                                               0x64, 0x72, 0x6f, 0x70, 0x20, 0x62, 0x6f, 0x78, // "drop box"
+                                       }),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0, 0x00},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 1},
+                                       Fields: []Field{
+                                               NewField(fieldError, []byte("You are not allowed to view drop boxes.")),
+                                       },
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+               {
+                       name: "with file root",
+                       args: args{
+                               cc: &ClientConn{
+                                       Server: &Server{
+                                               Config: &Config{
+                                                       FileRoot: func() string {
+                                                               path, _ := os.Getwd()
+                                                               return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
+                                                       }(),
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranGetFileNameList, &[]byte{0, 1},
+                                       NewField(fieldFilePath, []byte{
+                                               0x00, 0x00,
+                                               0x00, 0x00,
+                                       }),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0, 0xc8},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields: []Field{
+                                               NewField(
+                                                       fieldFileNameWithInfo,
+                                                       func() []byte {
+                                                               fnwi := FileNameWithInfo{
+                                                                       fileNameWithInfoHeader: fileNameWithInfoHeader{
+                                                                               Type:       [4]byte{0x54, 0x45, 0x58, 0x54},
+                                                                               Creator:    [4]byte{0x54, 0x54, 0x58, 0x54},
+                                                                               FileSize:   [4]byte{0, 0, 0x04, 0},
+                                                                               RSVD:       [4]byte{},
+                                                                               NameScript: [2]byte{},
+                                                                               NameSize:   [2]byte{0, 0x0b},
+                                                                       },
+                                                                       name: []byte("testfile-1k"),
+                                                               }
+                                                               b, _ := fnwi.MarshalBinary()
+                                                               return b
+                                                       }(),
+                                               ),
+                                       },
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotRes, err := HandleGetFileNameList(tt.args.cc, tt.args.t)
+                       if !tt.wantErr(t, err, fmt.Sprintf("HandleGetFileNameList(%v, %v)", tt.args.cc, tt.args.t)) {
+                               return
+                       }
+
+                       tranAssertEqual(t, tt.wantRes, gotRes)
+               })
+       }
+}