1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
package hotline
import (
"bytes"
"context"
"io"
"log/slog"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestClient() *Client {
return NewClient("testuser", slog.New(slog.NewTextHandler(os.Stdout, nil)))
}
func TestClient_Handshake(t *testing.T) {
t.Run("successful handshake", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
c := newTestClient()
c.Connection = clientConn
// Server side: read the client handshake and respond
go func() {
buf := make([]byte, 12)
_, _ = io.ReadFull(serverConn, buf)
assert.Equal(t, ClientHandshake, buf)
_, _ = serverConn.Write(ServerHandshake)
}()
err := c.Handshake()
assert.NoError(t, err)
})
t.Run("server returns error response", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
c := newTestClient()
c.Connection = clientConn
go func() {
buf := make([]byte, 12)
_, _ = io.ReadFull(serverConn, buf)
// Send a non-standard response
_, _ = serverConn.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
}()
err := c.Handshake()
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected handshake response")
})
t.Run("connection closed during read", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
c := newTestClient()
c.Connection = clientConn
go func() {
buf := make([]byte, 12)
_, _ = io.ReadFull(serverConn, buf)
serverConn.Close()
}()
err := c.Handshake()
assert.Error(t, err)
assert.Contains(t, err.Error(), "handshake read err")
})
}
func TestClient_Send(t *testing.T) {
t.Run("sends non-reply transaction and tracks it", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
c := newTestClient()
c.Connection = clientConn
tran := NewTransaction(TranChatSend, [2]byte{0, 1}, NewField(FieldData, []byte("hello")))
// Read what client sends
go func() {
buf := make([]byte, 4096)
_, _ = serverConn.Read(buf)
}()
err := c.Send(tran)
assert.NoError(t, err)
// Verify the transaction was added to activeTasks
assert.Contains(t, c.activeTasks, tran.ID)
})
t.Run("reply transactions are not tracked", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
c := newTestClient()
c.Connection = clientConn
tran := NewTransaction(TranChatSend, [2]byte{0, 1})
tran.IsReply = 1
go func() {
buf := make([]byte, 4096)
_, _ = serverConn.Read(buf)
}()
err := c.Send(tran)
assert.NoError(t, err)
assert.NotContains(t, c.activeTasks, tran.ID)
})
}
func TestClient_HandleTransaction(t *testing.T) {
t.Run("dispatches to registered handler", func(t *testing.T) {
c := newTestClient()
c.Connection = &clientMockConn{WBuf: &bytes.Buffer{}}
handlerCalled := false
c.HandleFunc(TranChatMsg, func(ctx context.Context, client *Client, t *Transaction) ([]Transaction, error) {
handlerCalled = true
return nil, nil
})
tran := &Transaction{Type: TranChatMsg}
err := c.HandleTransaction(context.Background(), tran)
assert.NoError(t, err)
assert.True(t, handlerCalled)
})
t.Run("reply matches original request type", func(t *testing.T) {
c := newTestClient()
c.Connection = &clientMockConn{WBuf: &bytes.Buffer{}}
// Register a handler for the original type
var receivedType TranType
c.HandleFunc(TranGetUserNameList, func(ctx context.Context, client *Client, t *Transaction) ([]Transaction, error) {
receivedType = t.Type
return nil, nil
})
// Simulate sending a request first
origTran := NewTransaction(TranGetUserNameList, [2]byte{0, 1})
c.activeTasks[origTran.ID] = &origTran
// Create a reply with the same ID
reply := &Transaction{
ID: origTran.ID,
IsReply: 1,
}
err := c.HandleTransaction(context.Background(), reply)
assert.NoError(t, err)
assert.Equal(t, TranGetUserNameList, receivedType)
})
t.Run("reply with no matching request returns error", func(t *testing.T) {
c := newTestClient()
c.Connection = &clientMockConn{WBuf: &bytes.Buffer{}}
reply := &Transaction{
ID: [4]byte{0, 0, 0, 99},
IsReply: 1,
}
err := c.HandleTransaction(context.Background(), reply)
assert.Error(t, err)
assert.Contains(t, err.Error(), "no matching request")
})
t.Run("unhandled transaction type does not error", func(t *testing.T) {
c := newTestClient()
c.Connection = &clientMockConn{WBuf: &bytes.Buffer{}}
tran := &Transaction{Type: [2]byte{0xFF, 0xFF}}
err := c.HandleTransaction(context.Background(), tran)
assert.NoError(t, err)
})
}
func TestClient_Disconnect(t *testing.T) {
t.Run("closes connection and done channel", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer serverConn.Close()
c := newTestClient()
c.Connection = clientConn
c.done = make(chan struct{})
doneCh := c.done // save reference before Disconnect sets it to nil
err := c.Disconnect()
assert.NoError(t, err)
// Verify done channel is closed
select {
case <-doneCh:
// expected - channel is closed
default:
t.Error("done channel should be closed")
}
// Verify connection is closed by attempting a write
_, err = clientConn.Write([]byte("test"))
assert.Error(t, err)
})
t.Run("handles nil done channel", func(t *testing.T) {
clientConn, serverConn := net.Pipe()
defer serverConn.Close()
c := newTestClient()
c.Connection = clientConn
// done is nil by default (before Connect is called)
err := c.Disconnect()
assert.NoError(t, err)
})
}
func TestClient_HandleFunc(t *testing.T) {
c := newTestClient()
handler := func(ctx context.Context, client *Client, t *Transaction) ([]Transaction, error) {
return nil, nil
}
c.HandleFunc(TranChatMsg, handler)
_, ok := c.Handlers[TranChatMsg]
require.True(t, ok, "handler should be registered")
}
func TestNewClient(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
c := NewClient("myuser", logger)
assert.Equal(t, "myuser", c.Pref.Username)
assert.NotNil(t, c.Handlers)
assert.NotNil(t, c.activeTasks)
}
// clientMockConn implements net.Conn for testing Send without a net.Pipe.
type clientMockConn struct {
RBuf *bytes.Buffer
WBuf *bytes.Buffer
}
func (mc *clientMockConn) Read(b []byte) (n int, err error) {
if mc.RBuf == nil {
return 0, io.EOF
}
return mc.RBuf.Read(b)
}
func (mc *clientMockConn) Write(b []byte) (n int, err error) {
return mc.WBuf.Write(b)
}
func (mc *clientMockConn) Close() error { return nil }
func (mc *clientMockConn) LocalAddr() net.Addr { return nil }
func (mc *clientMockConn) RemoteAddr() net.Addr { return nil }
func (mc *clientMockConn) SetDeadline(_ time.Time) error { return nil }
func (mc *clientMockConn) SetReadDeadline(_ time.Time) error { return nil }
func (mc *clientMockConn) SetWriteDeadline(_ time.Time) error { return nil }
|