]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "github.com/stretchr/testify/assert" | |
5 | "math/rand" | |
6 | "reflect" | |
7 | "testing" | |
8 | ) | |
9 | ||
10 | func TestHandleSetChatSubject(t *testing.T) { | |
11 | type args struct { | |
12 | cc *ClientConn | |
13 | t *Transaction | |
14 | } | |
15 | tests := []struct { | |
16 | name string | |
17 | args args | |
18 | want []Transaction | |
19 | wantErr bool | |
20 | }{ | |
21 | { | |
22 | name: "sends chat subject to private chat members", | |
23 | args: args{ | |
24 | cc: &ClientConn{ | |
25 | UserName: &[]byte{0x00, 0x01}, | |
26 | Server: &Server{ | |
27 | PrivateChats: map[uint32]*PrivateChat{ | |
28 | uint32(1): { | |
29 | Subject: "unset", | |
30 | ClientConn: map[uint16]*ClientConn{ | |
31 | uint16(1): { | |
32 | Account: &Account{ | |
33 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
34 | }, | |
35 | ID: &[]byte{0, 1}, | |
36 | }, | |
37 | uint16(2): { | |
38 | Account: &Account{ | |
39 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
40 | }, | |
41 | ID: &[]byte{0, 2}, | |
42 | }, | |
43 | }, | |
44 | }, | |
45 | }, | |
46 | Clients: map[uint16]*ClientConn{ | |
47 | uint16(1): { | |
48 | Account: &Account{ | |
49 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
50 | }, | |
51 | ID: &[]byte{0, 1}, | |
52 | }, | |
53 | uint16(2): { | |
54 | Account: &Account{ | |
55 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
56 | }, | |
57 | ID: &[]byte{0, 2}, | |
58 | }, | |
59 | }, | |
60 | }, | |
61 | }, | |
62 | t: &Transaction{ | |
63 | Flags: 0x00, | |
64 | IsReply: 0x00, | |
65 | Type: []byte{0, 0x6a}, | |
66 | ID: []byte{0, 0, 0, 1}, | |
67 | ErrorCode: []byte{0, 0, 0, 0}, | |
68 | Fields: []Field{ | |
69 | NewField(fieldChatID, []byte{0, 0, 0, 1}), | |
70 | NewField(fieldChatSubject, []byte("Test Subject")), | |
71 | }, | |
72 | }, | |
73 | }, | |
74 | want: []Transaction{ | |
75 | { | |
76 | clientID: &[]byte{0, 1}, | |
77 | Flags: 0x00, | |
78 | IsReply: 0x00, | |
79 | Type: []byte{0, 0x77}, | |
80 | ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) | |
81 | ErrorCode: []byte{0, 0, 0, 0}, | |
82 | Fields: []Field{ | |
83 | NewField(fieldChatID, []byte{0, 0, 0, 1}), | |
84 | NewField(fieldChatSubject, []byte("Test Subject")), | |
85 | }, | |
86 | }, | |
87 | { | |
88 | clientID: &[]byte{0, 2}, | |
89 | Flags: 0x00, | |
90 | IsReply: 0x00, | |
91 | Type: []byte{0, 0x77}, | |
92 | ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1) | |
93 | ErrorCode: []byte{0, 0, 0, 0}, | |
94 | Fields: []Field{ | |
95 | NewField(fieldChatID, []byte{0, 0, 0, 1}), | |
96 | NewField(fieldChatSubject, []byte("Test Subject")), | |
97 | }, | |
98 | }, | |
99 | }, | |
100 | wantErr: false, | |
101 | }, | |
102 | } | |
103 | for _, tt := range tests { | |
104 | rand.Seed(1) // reset seed between tests to make transaction IDs predictable | |
105 | ||
106 | t.Run(tt.name, func(t *testing.T) { | |
107 | got, err := HandleSetChatSubject(tt.args.cc, tt.args.t) | |
108 | if (err != nil) != tt.wantErr { | |
109 | t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr) | |
110 | return | |
111 | } | |
112 | if !assert.Equal(t, tt.want, got) { | |
113 | t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want) | |
114 | } | |
115 | }) | |
116 | } | |
117 | } | |
118 | ||
119 | func TestHandleLeaveChat(t *testing.T) { | |
120 | type args struct { | |
121 | cc *ClientConn | |
122 | t *Transaction | |
123 | } | |
124 | tests := []struct { | |
125 | name string | |
126 | args args | |
127 | want []Transaction | |
128 | wantErr bool | |
129 | }{ | |
130 | { | |
131 | name: "returns expected transactions", | |
132 | args: args{ | |
133 | cc: &ClientConn{ | |
134 | ID: &[]byte{0, 2}, | |
135 | Server: &Server{ | |
136 | PrivateChats: map[uint32]*PrivateChat{ | |
137 | uint32(1): { | |
138 | ClientConn: map[uint16]*ClientConn{ | |
139 | uint16(1): { | |
140 | Account: &Account{ | |
141 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
142 | }, | |
143 | ID: &[]byte{0, 1}, | |
144 | }, | |
145 | uint16(2): { | |
146 | Account: &Account{ | |
147 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
148 | }, | |
149 | ID: &[]byte{0, 2}, | |
150 | }, | |
151 | }, | |
152 | }, | |
153 | }, | |
154 | Clients: map[uint16]*ClientConn{ | |
155 | uint16(1): { | |
156 | Account: &Account{ | |
157 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
158 | }, | |
159 | ID: &[]byte{0, 1}, | |
160 | }, | |
161 | uint16(2): { | |
162 | Account: &Account{ | |
163 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
164 | }, | |
165 | ID: &[]byte{0, 2}, | |
166 | }, | |
167 | }, | |
168 | }, | |
169 | }, | |
170 | t: NewTransaction(tranDeleteUser,nil, NewField(fieldChatID, []byte{0, 0, 0, 1})), | |
171 | }, | |
172 | want: []Transaction{ | |
173 | { | |
174 | clientID: &[]byte{0, 1}, | |
175 | Flags: 0x00, | |
176 | IsReply: 0x00, | |
177 | Type: []byte{0, 0x76}, | |
178 | ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) | |
179 | ErrorCode: []byte{0, 0, 0, 0}, | |
180 | Fields: []Field{ | |
181 | NewField(fieldChatID, []byte{0, 0, 0, 1}), | |
182 | NewField(fieldUserID, []byte{0, 2}), | |
183 | }, | |
184 | }, | |
185 | }, | |
186 | wantErr: false, | |
187 | }, | |
188 | } | |
189 | for _, tt := range tests { | |
190 | rand.Seed(1) | |
191 | t.Run(tt.name, func(t *testing.T) { | |
192 | got, err := HandleLeaveChat(tt.args.cc, tt.args.t) | |
193 | if (err != nil) != tt.wantErr { | |
194 | t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr) | |
195 | return | |
196 | } | |
197 | if !assert.Equal(t, tt.want, got) { | |
198 | t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want) | |
199 | } | |
200 | }) | |
201 | } | |
202 | } | |
203 | ||
204 | ||
205 | func TestHandleGetUserNameList(t *testing.T) { | |
206 | type args struct { | |
207 | cc *ClientConn | |
208 | t *Transaction | |
209 | } | |
210 | tests := []struct { | |
211 | name string | |
212 | args args | |
213 | want []Transaction | |
214 | wantErr bool | |
215 | }{ | |
216 | { | |
217 | name: "replies with userlist transaction", | |
218 | args: args{ | |
219 | cc: &ClientConn{ | |
220 | ||
221 | ID: &[]byte{1, 1}, | |
222 | Server: &Server{ | |
223 | Clients: map[uint16]*ClientConn{ | |
224 | uint16(1): { | |
225 | ID: &[]byte{0, 1}, | |
226 | Icon: &[]byte{0, 2}, | |
227 | Flags: &[]byte{0, 3}, | |
228 | UserName: &[]byte{0, 4}, | |
229 | }, | |
230 | }, | |
231 | }, | |
232 | }, | |
233 | t: &Transaction{ | |
234 | ID: []byte{0, 0, 0, 1}, | |
235 | Type: []byte{0, 1}, | |
236 | }, | |
237 | }, | |
238 | want: []Transaction{ | |
239 | { | |
240 | clientID: &[]byte{1, 1}, | |
241 | Flags: 0x00, | |
242 | IsReply: 0x01, | |
243 | Type: []byte{0, 1}, | |
244 | ID: []byte{0, 0, 0, 1}, | |
245 | ErrorCode: []byte{0, 0, 0, 0}, | |
246 | Fields: []Field{ | |
247 | NewField( | |
248 | fieldUsernameWithInfo, | |
249 | []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04}, | |
250 | ), | |
251 | }, | |
252 | }, | |
253 | }, | |
254 | wantErr: false, | |
255 | }, | |
256 | } | |
257 | for _, tt := range tests { | |
258 | t.Run(tt.name, func(t *testing.T) { | |
259 | got, err := HandleGetUserNameList(tt.args.cc, tt.args.t) | |
260 | if (err != nil) != tt.wantErr { | |
261 | t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr) | |
262 | return | |
263 | } | |
264 | if !reflect.DeepEqual(got, tt.want) { | |
265 | t.Errorf("HandleGetUserNameList() got = %v, want %v", got, tt.want) | |
266 | } | |
267 | }) | |
268 | } | |
269 | } | |
270 | ||
271 | func TestHandleChatSend(t *testing.T) { | |
272 | type args struct { | |
273 | cc *ClientConn | |
274 | t *Transaction | |
275 | } | |
276 | tests := []struct { | |
277 | name string | |
278 | args args | |
279 | want []Transaction | |
280 | wantErr bool | |
281 | }{ | |
282 | { | |
283 | name: "sends chat msg transaction to all clients", | |
284 | args: args{ | |
285 | cc: &ClientConn{ | |
286 | UserName: &[]byte{0x00, 0x01}, | |
287 | Server: &Server{ | |
288 | Clients: map[uint16]*ClientConn{ | |
289 | uint16(1): { | |
290 | Account: &Account{ | |
291 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
292 | }, | |
293 | ID: &[]byte{0, 1}, | |
294 | }, | |
295 | uint16(2): { | |
296 | Account: &Account{ | |
297 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
298 | }, | |
299 | ID: &[]byte{0, 2}, | |
300 | }, | |
301 | }, | |
302 | }, | |
303 | }, | |
304 | t: &Transaction{ | |
305 | Fields: []Field{ | |
306 | NewField(fieldData, []byte("hai")), | |
307 | }, | |
308 | }, | |
309 | }, | |
310 | want: []Transaction{ | |
311 | { | |
312 | clientID: &[]byte{0, 1}, | |
313 | Flags: 0x00, | |
314 | IsReply: 0x00, | |
315 | Type: []byte{0, 0x6a}, | |
316 | ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) | |
317 | ErrorCode: []byte{0, 0, 0, 0}, | |
318 | Fields: []Field{ | |
319 | NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}), | |
320 | }, | |
321 | }, | |
322 | { | |
323 | clientID: &[]byte{0, 2}, | |
324 | Flags: 0x00, | |
325 | IsReply: 0x00, | |
326 | Type: []byte{0, 0x6a}, | |
327 | ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1) | |
328 | ErrorCode: []byte{0, 0, 0, 0}, | |
329 | Fields: []Field{ | |
330 | NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}), | |
331 | }, | |
332 | }, | |
333 | }, | |
334 | wantErr: false, | |
335 | }, | |
336 | { | |
337 | name: "only sends chat msg to clients with accessReadChat permission", | |
338 | args: args{ | |
339 | cc: &ClientConn{ | |
340 | UserName: &[]byte{0x00, 0x01}, | |
341 | Server: &Server{ | |
342 | Clients: map[uint16]*ClientConn{ | |
343 | uint16(1): { | |
344 | Account: &Account{ | |
345 | Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
346 | }, | |
347 | ID: &[]byte{0, 1}, | |
348 | }, | |
349 | uint16(2): { | |
350 | Account: &Account{ | |
351 | Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0}, | |
352 | }, | |
353 | ID: &[]byte{0, 2}, | |
354 | }, | |
355 | }, | |
356 | }, | |
357 | }, | |
358 | t: &Transaction{ | |
359 | Fields: []Field{ | |
360 | NewField(fieldData, []byte("hai")), | |
361 | }, | |
362 | }, | |
363 | }, | |
364 | want: []Transaction{ | |
365 | { | |
366 | clientID: &[]byte{0, 1}, | |
367 | Flags: 0x00, | |
368 | IsReply: 0x00, | |
369 | Type: []byte{0, 0x6a}, | |
370 | ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) | |
371 | ErrorCode: []byte{0, 0, 0, 0}, | |
372 | Fields: []Field{ | |
373 | NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}), | |
374 | }, | |
375 | }, | |
376 | }, | |
377 | wantErr: false, | |
378 | }, | |
379 | } | |
380 | for _, tt := range tests { | |
381 | rand.Seed(1) // reset seed between tests to make transaction IDs predictable | |
382 | t.Run(tt.name, func(t *testing.T) { | |
383 | got, err := HandleChatSend(tt.args.cc, tt.args.t) | |
384 | ||
385 | if (err != nil) != tt.wantErr { | |
386 | t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr) | |
387 | return | |
388 | } | |
389 | if !assert.Equal(t, tt.want, got) { | |
390 | t.Errorf("HandleChatSend() got = %v, want %v", got, tt.want) | |
391 | } | |
392 | }) | |
393 | } | |
394 | } |