]> git.r.bdr.sh - rbdr/mobius/commitdiff
Backfill tests and clean up
authorJeff Halter <redacted>
Tue, 7 Jun 2022 23:03:25 +0000 (16:03 -0700)
committerJeff Halter <redacted>
Tue, 7 Jun 2022 23:03:25 +0000 (16:03 -0700)
hotline/files.go
hotline/server.go
hotline/transaction_handlers.go
hotline/transaction_handlers_test.go

index d07f4455ebe188774a99164a5b033f55c141de7b..4f55eba13f29946a2cec4feed35040271e4276f2 100644 (file)
@@ -10,6 +10,8 @@ import (
        "strings"
 )
 
+const incompleteFileSuffix = ".incomplete"
+
 func downcaseFileExtension(filename string) string {
        splitStr := strings.Split(filename, ".")
        ext := strings.ToLower(
@@ -177,8 +179,6 @@ func EncodeFilePath(filePath string) []byte {
        return bytes
 }
 
-const incompleteFileSuffix = ".incomplete"
-
 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
 func effectiveFile(filePath string) (*os.File, error) {
        file, err := os.Open(filePath)
index d5da45808e546bfb0b440fe866978c327b6d9098..768433bc60b253f853c3b944580467e150344fc5 100644 (file)
@@ -65,7 +65,12 @@ type PrivateChat struct {
 }
 
 func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
-       s.Logger.Infow("Hotline server started", "version", VERSION)
+       s.Logger.Infow("Hotline server started",
+               "version", VERSION,
+               "API port", fmt.Sprintf(":%v", s.Port),
+               "Transfer port", fmt.Sprintf(":%v", s.Port+1),
+       )
+
        var wg sync.WaitGroup
 
        wg.Add(1)
@@ -79,13 +84,7 @@ func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFu
        return nil
 }
 
-func (s *Server) APIPort() int {
-       return s.APIListener.Addr().(*net.TCPAddr).Port
-}
-
 func (s *Server) ServeFileTransfers(ln net.Listener) error {
-       s.Logger.Infow("Hotline file transfer server started", "Addr", fmt.Sprintf(":%v", s.Port+1))
-
        for {
                conn, err := ln.Accept()
                if err != nil {
@@ -138,7 +137,6 @@ func (s *Server) sendTransaction(t Transaction) error {
 }
 
 func (s *Server) Serve(ctx context.Context, cancelRoot context.CancelFunc, ln net.Listener) error {
-       s.Logger.Infow("Hotline server started", "Addr", fmt.Sprintf(":%v", s.Port))
 
        for {
                conn, err := ln.Accept()
@@ -195,11 +193,7 @@ func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredL
        }
        server.APIListener = ln
 
-       if netPort != 0 {
-               netPort += 1
-       }
-
-       ln2, err := net.Listen("tcp", fmt.Sprintf("%s:%v", netInterface, netPort))
+       ln2, err := net.Listen("tcp", fmt.Sprintf("%s:%v", netInterface, netPort+1))
        server.FileListener = ln2
        if err != nil {
                return nil, err
@@ -210,7 +204,6 @@ func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredL
                return nil, err
        }
 
-       server.Logger.Debugw("Loading Agreement", "path", configDir+agreementFile)
        if server.Agreement, err = os.ReadFile(configDir + agreementFile); err != nil {
                return nil, err
        }
index 20e7651019c6a7e39d3389aefdbbd5961df9cce6..f4c5a2fda080e349683ec78ee8d0fa9e615fcbff 100644 (file)
@@ -301,7 +301,7 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
        msg := t.GetField(fieldData)
        ID := t.GetField(fieldUserID)
 
-       reply := *NewTransaction(
+       reply := NewTransaction(
                tranServerMsg,
                &ID.Data,
                NewField(fieldData, msg.Data),
@@ -316,12 +316,12 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
                reply.Fields = append(reply.Fields, NewField(fieldQuotingMsg, t.GetField(fieldQuotingMsg).Data))
        }
 
-       res = append(res, reply)
+       res = append(res, *reply)
 
        id, _ := byteToInt(ID.Data)
-       otherClient := cc.Server.Clients[uint16(id)]
-       if otherClient == nil {
-               return res, errors.New("ohno")
+       otherClient, ok := cc.Server.Clients[uint16(id)]
+       if !ok {
+               return res, errors.New("invalid client ID")
        }
 
        // Respond with auto reply if other client has it enabled
index d7ec098a42dc01a2787d1b1e7383fcf86a97b2e1..c174822723ad435073dc9f08e96f273a314be518 100644 (file)
@@ -2136,3 +2136,121 @@ func TestHandleDisconnectUser(t *testing.T) {
                })
        }
 }
+
+func TestHandleSendInstantMsg(t *testing.T) {
+       type args struct {
+               cc *ClientConn
+               t  *Transaction
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantRes []Transaction
+               wantErr assert.ErrorAssertionFunc
+       }{
+               {
+                       name: "when client 1 sends a message to client 2",
+                       args: args{
+                               cc: &ClientConn{
+                                       ID:       &[]byte{0, 1},
+                                       UserName: []byte("User1"),
+                                       Server: &Server{
+                                               Clients: map[uint16]*ClientConn{
+                                                       uint16(2): {
+                                                               AutoReply: []byte(nil),
+                                                       },
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranSendInstantMsg,
+                                       &[]byte{0, 1},
+                                       NewField(fieldData, []byte("hai")),
+                                       NewField(fieldUserID, []byte{0, 2}),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               *NewTransaction(
+                                       tranServerMsg,
+                                       &[]byte{0, 2},
+                                       NewField(fieldData, []byte("hai")),
+                                       NewField(fieldUserName, []byte("User1")),
+                                       NewField(fieldUserID, []byte{0, 1}),
+                                       NewField(fieldOptions, []byte{0, 1}),
+                               ),
+                               {
+                                       clientID:  &[]byte{0, 1},
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0x0, 0x6c},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields:    []Field(nil),
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+               {
+                       name: "when client 2 has autoreply enabled",
+                       args: args{
+                               cc: &ClientConn{
+                                       ID:       &[]byte{0, 1},
+                                       UserName: []byte("User1"),
+                                       Server: &Server{
+                                               Clients: map[uint16]*ClientConn{
+                                                       uint16(2): {
+                                                               ID:        &[]byte{0, 2},
+                                                               UserName:  []byte("User2"),
+                                                               AutoReply: []byte("autohai"),
+                                                       },
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranSendInstantMsg,
+                                       &[]byte{0, 1},
+                                       NewField(fieldData, []byte("hai")),
+                                       NewField(fieldUserID, []byte{0, 2}),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               *NewTransaction(
+                                       tranServerMsg,
+                                       &[]byte{0, 2},
+                                       NewField(fieldData, []byte("hai")),
+                                       NewField(fieldUserName, []byte("User1")),
+                                       NewField(fieldUserID, []byte{0, 1}),
+                                       NewField(fieldOptions, []byte{0, 1}),
+                               ),
+                               *NewTransaction(
+                                       tranServerMsg,
+                                       &[]byte{0, 1},
+                                       NewField(fieldData, []byte("autohai")),
+                                       NewField(fieldUserName, []byte("User2")),
+                                       NewField(fieldUserID, []byte{0, 2}),
+                                       NewField(fieldOptions, []byte{0, 1}),
+                               ),
+                               {
+                                       clientID:  &[]byte{0, 1},
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0x0, 0x6c},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields:    []Field(nil),
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
+                       if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
+                               return
+                       }
+
+                       tranAssertEqual(t, tt.wantRes, gotRes)
+               })
+       }
+}