]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_handlers_test.go
Fix "No Agreement" behavior for 1.2.3 clients
[rbdr/mobius] / hotline / transaction_handlers_test.go
index 424a8e5180e67b3c40699ffb23c791a48f35adde..c504bbe8f11d6e2eabb635c8caa03c91a4459331 100644 (file)
@@ -228,22 +228,22 @@ func TestHandleGetUserNameList(t *testing.T) {
                                                Clients: map[uint16]*ClientConn{
                                                        uint16(1): {
                                                                ID:       &[]byte{0, 1},
                                                Clients: map[uint16]*ClientConn{
                                                        uint16(1): {
                                                                ID:       &[]byte{0, 1},
-                                                               Icon:     &[]byte{0, 2},
-                                                               Flags:    &[]byte{0, 3},
+                                                               Icon:     []byte{0, 2},
+                                                               Flags:    []byte{0, 3},
                                                                UserName: []byte{0, 4},
                                                                Agreed:   true,
                                                        },
                                                        uint16(2): {
                                                                ID:       &[]byte{0, 2},
                                                                UserName: []byte{0, 4},
                                                                Agreed:   true,
                                                        },
                                                        uint16(2): {
                                                                ID:       &[]byte{0, 2},
-                                                               Icon:     &[]byte{0, 2},
-                                                               Flags:    &[]byte{0, 3},
+                                                               Icon:     []byte{0, 2},
+                                                               Flags:    []byte{0, 3},
                                                                UserName: []byte{0, 4},
                                                                Agreed:   true,
                                                        },
                                                        uint16(3): {
                                                                ID:       &[]byte{0, 3},
                                                                UserName: []byte{0, 4},
                                                                Agreed:   true,
                                                        },
                                                        uint16(3): {
                                                                ID:       &[]byte{0, 3},
-                                                               Icon:     &[]byte{0, 2},
-                                                               Flags:    &[]byte{0, 3},
+                                                               Icon:     []byte{0, 2},
+                                                               Flags:    []byte{0, 3},
                                                                UserName: []byte{0, 4},
                                                                Agreed:   false,
                                                        },
                                                                UserName: []byte{0, 4},
                                                                Agreed:   false,
                                                        },
@@ -978,7 +978,13 @@ func TestHandleUploadFile(t *testing.T) {
                        args: args{
                                cc: &ClientConn{
                                        Server: &Server{
                        args: args{
                                cc: &ClientConn{
                                        Server: &Server{
-                                               FileTransfers: map[uint32]*FileTransfer{},
+                                               FS:            &OSFileStore{},
+                                               fileTransfers: map[[4]byte]*FileTransfer{},
+                                               Config: &Config{
+                                                       FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
+                                               }},
+                                       transfers: map[int]map[[4]byte]*FileTransfer{
+                                               FileUpload: {},
                                        },
                                        Account: &Account{
                                                Access: func() *[]byte {
                                        },
                                        Account: &Account{
                                                Access: func() *[]byte {
@@ -1026,9 +1032,6 @@ func TestHandleUploadFile(t *testing.T) {
                                                        return &access
                                                }(),
                                        },
                                                        return &access
                                                }(),
                                        },
-                                       Server: &Server{
-                                               FileTransfers: map[uint32]*FileTransfer{},
-                                       },
                                },
                                t: NewTransaction(
                                        tranUploadFile, &[]byte{0, 1},
                                },
                                t: NewTransaction(
                                        tranUploadFile, &[]byte{0, 1},
@@ -1687,6 +1690,52 @@ func TestHandleListUsers(t *testing.T) {
                        },
                        wantErr: assert.NoError,
                },
                        },
                        wantErr: assert.NoError,
                },
+               {
+                       name: "when user has required permission",
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessOpenUser)
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                                       Server: &Server{
+                                               Accounts: map[string]*Account{
+                                                       "guest": {
+                                                               Name:     "guest",
+                                                               Login:    "guest",
+                                                               Password: "zz",
+                                                               Access:   &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
+                                                       },
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranGetClientInfoText, &[]byte{0, 1},
+                                       NewField(fieldUserID, []byte{0, 1}),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0x01, 0x2f},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields: []Field{
+                                               NewField(fieldData, []byte{
+                                                       0x00, 0x04, 0x00, 0x66, 0x00, 0x05, 0x67, 0x75, 0x65, 0x73, 0x74, 0x00, 0x69, 0x00, 0x05, 0x98,
+                                                       0x8a, 0x9a, 0x8c, 0x8b, 0x00, 0x6e, 0x00, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+                                                       0x00, 0x6a, 0x00, 0x01, 0x78,
+                                               }),
+                                       },
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
@@ -1744,7 +1793,9 @@ func TestHandleDownloadFile(t *testing.T) {
                        name: "with a valid file",
                        args: args{
                                cc: &ClientConn{
                        name: "with a valid file",
                        args: args{
                                cc: &ClientConn{
-                                       Transfers: make(map[int][]*FileTransfer),
+                                       transfers: map[int]map[[4]byte]*FileTransfer{
+                                               FileDownload: {},
+                                       },
                                        Account: &Account{
                                                Access: func() *[]byte {
                                                        var bits accessBitmap
                                        Account: &Account{
                                                Access: func() *[]byte {
                                                        var bits accessBitmap
@@ -1755,7 +1806,7 @@ func TestHandleDownloadFile(t *testing.T) {
                                        },
                                        Server: &Server{
                                                FS:            &OSFileStore{},
                                        },
                                        Server: &Server{
                                                FS:            &OSFileStore{},
-                                               FileTransfers: make(map[uint32]*FileTransfer),
+                                               fileTransfers: map[[4]byte]*FileTransfer{},
                                                Config: &Config{
                                                        FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
                                                },
                                                Config: &Config{
                                                        FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
                                                },
@@ -1790,8 +1841,9 @@ func TestHandleDownloadFile(t *testing.T) {
                        name: "when client requests to resume 1k test file at offset 256",
                        args: args{
                                cc: &ClientConn{
                        name: "when client requests to resume 1k test file at offset 256",
                        args: args{
                                cc: &ClientConn{
-                                       Transfers: make(map[int][]*FileTransfer),
-                                       Account: &Account{
+                                       transfers: map[int]map[[4]byte]*FileTransfer{
+                                               FileDownload: {},
+                                       }, Account: &Account{
                                                Access: func() *[]byte {
                                                        var bits accessBitmap
                                                        bits.Set(accessDownloadFile)
                                                Access: func() *[]byte {
                                                        var bits accessBitmap
                                                        bits.Set(accessDownloadFile)
@@ -1801,6 +1853,7 @@ func TestHandleDownloadFile(t *testing.T) {
                                        },
                                        Server: &Server{
                                                FS: &OSFileStore{},
                                        },
                                        Server: &Server{
                                                FS: &OSFileStore{},
+
                                                // FS: func() *MockFileStore {
                                                //      path, _ := os.Getwd()
                                                //      testFile, err := os.Open(path + "/test/config/Files/testfile-1k")
                                                // FS: func() *MockFileStore {
                                                //      path, _ := os.Getwd()
                                                //      testFile, err := os.Open(path + "/test/config/Files/testfile-1k")
@@ -1818,7 +1871,7 @@ func TestHandleDownloadFile(t *testing.T) {
                                                //
                                                //      return mfs
                                                // }(),
                                                //
                                                //      return mfs
                                                // }(),
-                                               FileTransfers: make(map[uint32]*FileTransfer),
+                                               fileTransfers: map[[4]byte]*FileTransfer{},
                                                Config: &Config{
                                                        FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
                                                },
                                                Config: &Config{
                                                        FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
                                                },
@@ -2234,10 +2287,49 @@ func TestHandleSendInstantMsg(t *testing.T) {
                wantRes []Transaction
                wantErr assert.ErrorAssertionFunc
        }{
                wantRes []Transaction
                wantErr assert.ErrorAssertionFunc
        }{
+               {
+                       name: "without required permission",
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranDelNewsArt,
+                                       &[]byte{0, 0},
+                               ),
+                       },
+                       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 send private messages.")),
+                                       },
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
                {
                        name: "when client 1 sends a message to client 2",
                        args: args{
                                cc: &ClientConn{
                {
                        name: "when client 1 sends a message to client 2",
                        args: args{
                                cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessSendPrivMsg)
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
                                        ID:       &[]byte{0, 1},
                                        UserName: []byte("User1"),
                                        Server: &Server{
                                        ID:       &[]byte{0, 1},
                                        UserName: []byte("User1"),
                                        Server: &Server{
@@ -2280,6 +2372,14 @@ func TestHandleSendInstantMsg(t *testing.T) {
                        name: "when client 2 has autoreply enabled",
                        args: args{
                                cc: &ClientConn{
                        name: "when client 2 has autoreply enabled",
                        args: args{
                                cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessSendPrivMsg)
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
                                        ID:       &[]byte{0, 1},
                                        UserName: []byte("User1"),
                                        Server: &Server{
                                        ID:       &[]byte{0, 1},
                                        UserName: []byte("User1"),
                                        Server: &Server{
@@ -2611,3 +2711,295 @@ func TestHandleGetFileNameList(t *testing.T) {
                })
        }
 }
                })
        }
 }
+
+func TestHandleGetClientInfoText(t *testing.T) {
+       type args struct {
+               cc *ClientConn
+               t  *Transaction
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantRes []Transaction
+               wantErr assert.ErrorAssertionFunc
+       }{
+               {
+                       name: "when user does not have required permission",
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                                       Server: &Server{
+                                               Accounts: map[string]*Account{},
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranGetClientInfoText, &[]byte{0, 1},
+                                       NewField(fieldUserID, []byte{0, 1}),
+                               ),
+                       },
+                       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 get client info.")),
+                                       },
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+               {
+                       name: "with a valid user",
+                       args: args{
+                               cc: &ClientConn{
+                                       UserName:   []byte("Testy McTest"),
+                                       RemoteAddr: "1.2.3.4:12345",
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessGetClientInfo)
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                               Name:  "test",
+                                               Login: "test",
+                                       },
+                                       Server: &Server{
+                                               Accounts: map[string]*Account{},
+                                               Clients: map[uint16]*ClientConn{
+                                                       uint16(1): {
+                                                               UserName:   []byte("Testy McTest"),
+                                                               RemoteAddr: "1.2.3.4:12345",
+                                                               Account: &Account{
+                                                                       Access: func() *[]byte {
+                                                                               var bits accessBitmap
+                                                                               bits.Set(accessGetClientInfo)
+                                                                               access := bits[:]
+                                                                               return &access
+                                                                       }(),
+                                                                       Name:  "test",
+                                                                       Login: "test",
+                                                               },
+                                                       },
+                                               },
+                                       },
+                                       transfers: map[int]map[[4]byte]*FileTransfer{
+                                               FileDownload:   {},
+                                               FileUpload:     {},
+                                               FolderDownload: {},
+                                               FolderUpload:   {},
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranGetClientInfoText, &[]byte{0, 1},
+                                       NewField(fieldUserID, []byte{0, 1}),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0x1, 0x2f},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields: []Field{
+                                               NewField(fieldData, []byte(
+                                                       strings.Replace(`Nickname:   Testy McTest
+Name:       test
+Account:    test
+Address:    1.2.3.4:12345
+
+-------- File Downloads ---------
+
+None.
+
+------- Folder Downloads --------
+
+None.
+
+--------- File Uploads ----------
+
+None.
+
+-------- Folder Uploads ---------
+
+None.
+
+------- Waiting Downloads -------
+
+None.
+
+`, "\n", "\r", -1)),
+                                               ),
+                                               NewField(fieldUserName, []byte("Testy McTest")),
+                                       },
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotRes, err := HandleGetClientInfoText(tt.args.cc, tt.args.t)
+                       if !tt.wantErr(t, err, fmt.Sprintf("HandleGetClientInfoText(%v, %v)", tt.args.cc, tt.args.t)) {
+                               return
+                       }
+                       tranAssertEqual(t, tt.wantRes, gotRes)
+               })
+       }
+}
+
+func TestHandleTranAgreed(t *testing.T) {
+       type args struct {
+               cc *ClientConn
+               t  *Transaction
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantRes []Transaction
+               wantErr assert.ErrorAssertionFunc
+       }{
+               {
+                       name: "normal request flow",
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessDisconUser)
+                                                       bits.Set(accessAnyName)
+                                                       access := bits[:]
+                                                       return &access
+                                               }()},
+                                       Icon:    []byte{0, 1},
+                                       Flags:   []byte{0, 1},
+                                       Version: []byte{0, 1},
+                                       ID:      &[]byte{0, 1},
+                                       logger:  NewTestLogger(),
+                                       Server: &Server{
+                                               Config: &Config{
+                                                       BannerFile: "banner.jpg",
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranAgreed, nil,
+                                       NewField(fieldUserName, []byte("username")),
+                                       NewField(fieldUserIconID, []byte{0, 1}),
+                                       NewField(fieldOptions, []byte{0, 0}),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       clientID:  &[]byte{0, 1},
+                                       Flags:     0x00,
+                                       IsReply:   0x00,
+                                       Type:      []byte{0, 0x7a},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields: []Field{
+                                               NewField(fieldBannerType, []byte("JPEG")),
+                                       },
+                               },
+                               {
+                                       clientID:  &[]byte{0, 1},
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0, 0x79},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields:    []Field{},
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotRes, err := HandleTranAgreed(tt.args.cc, tt.args.t)
+                       if !tt.wantErr(t, err, fmt.Sprintf("HandleTranAgreed(%v, %v)", tt.args.cc, tt.args.t)) {
+                               return
+                       }
+                       tranAssertEqual(t, tt.wantRes, gotRes)
+               })
+       }
+}
+
+func TestHandleSetClientUserInfo(t *testing.T) {
+       type args struct {
+               cc *ClientConn
+               t  *Transaction
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantRes []Transaction
+               wantErr assert.ErrorAssertionFunc
+       }{
+               {
+                       name: "when client does not have accessAnyName",
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() *[]byte {
+                                                       var bits accessBitmap
+                                                       access := bits[:]
+                                                       return &access
+                                               }(),
+                                       },
+                                       ID:       &[]byte{0, 1},
+                                       UserName: []byte("Guest"),
+                                       Flags:    []byte{0, 1},
+                                       Server: &Server{
+                                               Clients: map[uint16]*ClientConn{
+                                                       uint16(1): {
+                                                               ID: &[]byte{0, 1},
+                                                       },
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranSetClientUserInfo, nil,
+                                       NewField(fieldUserIconID, []byte{0, 1}),
+                                       NewField(fieldUserName, []byte("NOPE")),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               {
+                                       clientID:  &[]byte{0, 1},
+                                       Flags:     0x00,
+                                       IsReply:   0x00,
+                                       Type:      []byte{0x01, 0x2d},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields: []Field{
+                                               NewField(fieldUserID, []byte{0, 1}),
+                                               NewField(fieldUserIconID, []byte{0, 1}),
+                                               NewField(fieldUserFlags, []byte{0, 1}),
+                                               NewField(fieldUserName, []byte("Guest"))},
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotRes, err := HandleSetClientUserInfo(tt.args.cc, tt.args.t)
+                       if !tt.wantErr(t, err, fmt.Sprintf("HandleSetClientUserInfo(%v, %v)", tt.args.cc, tt.args.t)) {
+                               return
+                       }
+
+                       tranAssertEqual(t, tt.wantRes, gotRes)
+               })
+       }
+}