]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | // | |
aebc4d36 | 4 | // import ( |
6988a057 JH |
5 | // "bytes" |
6 | // "fmt" | |
7 | // "github.com/google/go-cmp/cmp" | |
8 | // "io/ioutil" | |
9 | // "math/big" | |
10 | // "net" | |
11 | // "strings" | |
12 | // "sync" | |
13 | // "testing" | |
aebc4d36 | 14 | // ) |
6988a057 | 15 | // |
aebc4d36 | 16 | // type transactionTest struct { |
6988a057 JH |
17 | // description string // Human understandable description |
18 | // account Account // Account struct for a user that will test transaction will execute under | |
19 | // request Transaction // transaction that will be sent by the client to the server | |
20 | // want Transaction // transaction that the client expects to receive in response | |
21 | // setup func() // Optional setup required for the test scenario | |
22 | // teardown func() // Optional teardown for test scenario | |
aebc4d36 | 23 | // } |
6988a057 | 24 | // |
aebc4d36 | 25 | // func (tt *transactionTest) Setup(srv *Server) error { |
6988a057 JH |
26 | // if err := srv.NewUser(tt.account.Login, tt.account.Name, NegatedUserString([]byte(tt.account.Password)), tt.account.Access); err != nil { |
27 | // return err | |
28 | // } | |
29 | // | |
30 | // if tt.setup != nil { | |
31 | // tt.setup() | |
32 | // } | |
33 | // | |
34 | // return nil | |
aebc4d36 | 35 | // } |
6988a057 | 36 | // |
aebc4d36 | 37 | // func (tt *transactionTest) Teardown(srv *Server) error { |
6988a057 JH |
38 | // if err := srv.DeleteUser(tt.account.Login); err != nil { |
39 | // return err | |
40 | // } | |
41 | // | |
42 | // if tt.teardown != nil { | |
43 | // tt.teardown() | |
44 | // } | |
45 | // | |
46 | // return nil | |
aebc4d36 | 47 | // } |
6988a057 | 48 | // |
aebc4d36 JH |
49 | // // StartTestServer |
50 | // func StartTestServer() (srv *Server, lnPort int) { | |
6988a057 JH |
51 | // hotlineServer, _ := NewServer("test/config/") |
52 | // ln, err := net.Listen("tcp", ":0") | |
53 | // | |
54 | // if err != nil { | |
55 | // panic(err) | |
56 | // } | |
57 | // go func() { | |
58 | // for { | |
59 | // conn, _ := ln.Accept() | |
60 | // go hotlineServer.HandleConnection(conn) | |
61 | // } | |
62 | // }() | |
63 | // return hotlineServer, ln.Addr().(*net.TCPAddr).Port | |
aebc4d36 | 64 | // } |
6988a057 | 65 | // |
aebc4d36 | 66 | // func StartTestClient(serverPort int, login, passwd string) (*Client, error) { |
6988a057 JH |
67 | // c := NewClient("") |
68 | // | |
69 | // err := c.JoinServer(fmt.Sprintf(":%v", serverPort), login, passwd) | |
70 | // if err != nil { | |
71 | // return nil, err | |
72 | // } | |
73 | // | |
74 | // return c, nil | |
aebc4d36 | 75 | // } |
6988a057 | 76 | // |
aebc4d36 | 77 | // func StartTestServerWithClients(clientCount int) ([]*Client, int) { |
6988a057 JH |
78 | // _, serverPort := StartTestServer() |
79 | // | |
80 | // var clients []*Client | |
81 | // for i := 0; i < clientCount; i++ { | |
82 | // client, err := StartTestClient(serverPort, "admin", "") | |
83 | // if err != nil { | |
84 | // panic(err) | |
85 | // } | |
86 | // clients = append(clients, client) | |
87 | // } | |
88 | // clients[0].ReadN(2) | |
89 | // | |
90 | // return clients, serverPort | |
aebc4d36 | 91 | // } |
6988a057 JH |
92 | // |
93 | ||
aebc4d36 JH |
94 | // //func TestHandleTranAgreed(t *testing.T) { |
95 | // // clients, _ := StartTestServerWithClients(2) | |
96 | // // | |
97 | // // chatMsg := "Test Chat" | |
98 | // // | |
99 | // // // Assert that both clients should receive the user join notification | |
100 | // // var wg sync.WaitGroup | |
101 | // // for _, client := range clients { | |
102 | // // wg.Add(1) | |
103 | // // go func(wg *sync.WaitGroup, c *Client) { | |
104 | // // defer wg.Done() | |
105 | // // | |
106 | // // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data | |
107 | // // | |
108 | // // want := []byte(fmt.Sprintf("test: %s\r", chatMsg)) | |
109 | // // if bytes.Compare(receivedMsg, want) != 0 { | |
110 | // // t.Errorf("%q, want %q", receivedMsg, want) | |
111 | // // } | |
112 | // // }(&wg, client) | |
113 | // // } | |
114 | // // | |
115 | // // trans := clients[1].ReadTransactions() | |
116 | // // spew.Dump(trans) | |
117 | // // | |
118 | // // // Send the agreement | |
119 | // // clients[1].Connection.Write( | |
120 | // // NewTransaction( | |
121 | // // tranAgreed, 0, | |
122 | // // []Field{ | |
123 | // // NewField(fieldUserName, []byte("testUser")), | |
124 | // // NewField(fieldUserIconID, []byte{0x00,0x07}), | |
125 | // // }, | |
126 | // // ).Payload(), | |
127 | // // ) | |
128 | // // | |
129 | // // wg.Wait() | |
130 | // //} | |
131 | // | |
132 | // func TestChatSend(t *testing.T) { | |
6988a057 JH |
133 | // //srvPort := StartTestServer() |
134 | // // | |
135 | // //senderClient := NewClient("senderClient") | |
136 | // //senderClient.JoinServer(fmt.Sprintf(":%v", srvPort), "", "") | |
137 | // // | |
138 | // //receiverClient := NewClient("receiverClient") | |
139 | // //receiverClient.JoinServer(fmt.Sprintf(":%v", srvPort), "", "") | |
140 | // | |
141 | // clients, _ := StartTestServerWithClients(2) | |
142 | // | |
143 | // chatMsg := "Test Chat" | |
144 | // | |
145 | // // Both clients should receive the chatMsg | |
146 | // var wg sync.WaitGroup | |
147 | // for _, client := range clients { | |
148 | // wg.Add(1) | |
149 | // go func(wg *sync.WaitGroup, c *Client) { | |
150 | // defer wg.Done() | |
151 | // | |
152 | // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data | |
153 | // | |
154 | // want := []byte(fmt.Sprintf(" test: %s\r", chatMsg)) | |
155 | // if bytes.Compare(receivedMsg, want) != 0 { | |
156 | // t.Errorf("%q, want %q", receivedMsg, want) | |
157 | // } | |
158 | // }(&wg, client) | |
159 | // } | |
160 | // | |
161 | // // Send the chatMsg | |
162 | // clients[1].Send( | |
163 | // NewTransaction( | |
164 | // tranChatSend, 0, | |
165 | // []Field{ | |
166 | // NewField(fieldData, []byte(chatMsg)), | |
167 | // }, | |
168 | // ), | |
169 | // ) | |
170 | // | |
171 | // wg.Wait() | |
aebc4d36 | 172 | // } |
6988a057 | 173 | // |
aebc4d36 | 174 | // func TestSetClientUserInfo(t *testing.T) { |
6988a057 JH |
175 | // clients, _ := StartTestServerWithClients(2) |
176 | // | |
177 | // newIcon := []byte{0x00, 0x01} | |
178 | // newUserName := "newName" | |
179 | // | |
180 | // // Both clients should receive the chatMsg | |
181 | // var wg sync.WaitGroup | |
182 | // for _, client := range clients { | |
183 | // wg.Add(1) | |
184 | // go func(wg *sync.WaitGroup, c *Client) { | |
185 | // defer wg.Done() | |
186 | // | |
187 | // tran := c.ReadTransactions()[0] | |
188 | // | |
189 | // want := []byte(newUserName) | |
190 | // got := tran.GetField(fieldUserName).Data | |
191 | // if bytes.Compare(got, want) != 0 { | |
192 | // t.Errorf("%q, want %q", got, want) | |
193 | // } | |
194 | // }(&wg, client) | |
195 | // } | |
196 | // | |
197 | // _, err := clients[1].Connection.Write( | |
198 | // NewTransaction( | |
199 | // tranSetClientUserInfo, 0, | |
200 | // []Field{ | |
201 | // NewField(fieldUserIconID, newIcon), | |
202 | // NewField(fieldUserName, []byte(newUserName)), | |
203 | // }, | |
204 | // ).Payload(), | |
205 | // ) | |
206 | // if err != nil { | |
207 | // t.Errorf("%v", err) | |
208 | // } | |
209 | // | |
210 | // wg.Wait() | |
aebc4d36 | 211 | // } |
6988a057 | 212 | // |
aebc4d36 JH |
213 | // // TestSendInstantMsg tests that client A can send an instant message to client B |
214 | // // | |
215 | // func TestSendInstantMsg(t *testing.T) { | |
6988a057 JH |
216 | // clients, _ := StartTestServerWithClients(2) |
217 | // | |
218 | // instantMsg := "Test IM" | |
219 | // | |
220 | // var wg sync.WaitGroup | |
221 | // wg.Add(1) | |
222 | // go func(wg *sync.WaitGroup, c *Client) { | |
223 | // defer wg.Done() | |
224 | // | |
225 | // tran := c.WaitForTransaction(tranServerMsg) | |
226 | // | |
227 | // receivedMsg := tran.GetField(fieldData).Data | |
228 | // want := []byte(fmt.Sprintf("%s", instantMsg)) | |
229 | // if bytes.Compare(receivedMsg, want) != 0 { | |
230 | // t.Errorf("%q, want %q", receivedMsg, want) | |
231 | // } | |
232 | // }(&wg, clients[0]) | |
233 | // | |
234 | // _ = clients[1].Send( | |
235 | // NewTransaction(tranGetUserNameList, 0, []Field{}), | |
236 | // ) | |
237 | // //connectedUsersTran := clients[1].ReadTransactions()[0] | |
238 | // ////connectedUsers := connectedUsersTran.Fields[0].Data[0:2] | |
239 | // //spew.Dump(connectedUsersTran.Fields) | |
240 | // //firstUserID := connectedUsersTran.Fields[0].Data[0:2] | |
241 | // // | |
242 | // //spew.Dump(firstUserID) | |
243 | // | |
244 | // // Send the IM | |
245 | // err := clients[1].Send( | |
246 | // NewTransaction( | |
247 | // tranSendInstantMsg, 0, | |
248 | // []Field{ | |
249 | // NewField(fieldData, []byte(instantMsg)), | |
250 | // NewField(fieldUserName, clients[1].UserName), | |
251 | // NewField(fieldUserID, []byte{0, 2}), | |
252 | // NewField(fieldOptions, []byte{0, 1}), | |
253 | // }, | |
254 | // ), | |
255 | // ) | |
256 | // if err != nil { | |
257 | // t.Error(err) | |
258 | // } | |
259 | // | |
260 | // wg.Wait() | |
aebc4d36 | 261 | // } |
6988a057 | 262 | // |
aebc4d36 | 263 | // func TestOldPostNews(t *testing.T) { |
6988a057 JH |
264 | // clients, _ := StartTestServerWithClients(2) |
265 | // | |
266 | // newsPost := "Test News Post" | |
267 | // | |
268 | // var wg sync.WaitGroup | |
269 | // wg.Add(1) | |
270 | // go func(wg *sync.WaitGroup, c *Client) { | |
271 | // defer wg.Done() | |
272 | // | |
273 | // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data | |
274 | // | |
275 | // if strings.Contains(string(receivedMsg), newsPost) == false { | |
276 | // t.Errorf("news post missing") | |
277 | // } | |
278 | // }(&wg, clients[0]) | |
279 | // | |
280 | // clients[1].Connection.Write( | |
281 | // NewTransaction( | |
282 | // tranOldPostNews, 0, | |
283 | // []Field{ | |
284 | // NewField(fieldData, []byte(newsPost)), | |
285 | // }, | |
286 | // ).Payload(), | |
287 | // ) | |
288 | // | |
289 | // wg.Wait() | |
aebc4d36 JH |
290 | // } |
291 | // | |
292 | // // TODO: Fixme | |
293 | // //func TestGetFileNameList(t *testing.T) { | |
294 | // // clients, _ := StartTestServerWithClients(2) | |
295 | // // | |
296 | // // clients[0].Connection.Write( | |
297 | // // NewTransaction( | |
298 | // // tranGetFileNameList, 0, | |
299 | // // []Field{}, | |
300 | // // ).Payload(), | |
301 | // // ) | |
302 | // // | |
303 | // // ts := clients[0].ReadTransactions() | |
304 | // // testfileSit := ReadFileNameWithInfo(ts[0].Fields[1].Data) | |
305 | // // | |
306 | // // want := "testfile.sit" | |
307 | // // got := testfileSit.Name | |
308 | // // diff := cmp.Diff(want, got) | |
309 | // // if diff != "" { | |
310 | // // t.Fatalf(diff) | |
311 | // // } | |
312 | // // if testfileSit.Name != "testfile.sit" { | |
313 | // // t.Errorf("news post missing") | |
314 | // // t.Errorf("%q, want %q", testfileSit.Name, "testfile.sit") | |
315 | // // } | |
316 | // //} | |
317 | // | |
318 | // func TestNewsCategoryList(t *testing.T) { | |
6988a057 JH |
319 | // clients, _ := StartTestServerWithClients(2) |
320 | // client := clients[0] | |
321 | // | |
322 | // client.Send( | |
323 | // NewTransaction( | |
324 | // tranGetNewsCatNameList, 0, | |
325 | // []Field{}, | |
326 | // ), | |
327 | // ) | |
328 | // | |
329 | // ts := client.ReadTransactions() | |
330 | // cats := ts[0].GetFields(fieldNewsCatListData15) | |
331 | // | |
332 | // newsCat := ReadNewsCategoryListData(cats[0].Data) | |
333 | // want := "TestBundle" | |
334 | // got := newsCat.Name | |
335 | // diff := cmp.Diff(want, got) | |
336 | // if diff != "" { | |
337 | // t.Fatalf(diff) | |
338 | // } | |
339 | // | |
340 | // newsBundle := ReadNewsCategoryListData(cats[1].Data) | |
341 | // want = "TestCat" | |
342 | // got = newsBundle.Name | |
343 | // diff = cmp.Diff(want, got) | |
344 | // if diff != "" { | |
345 | // t.Fatalf(diff) | |
346 | // } | |
aebc4d36 | 347 | // } |
6988a057 | 348 | // |
aebc4d36 | 349 | // func TestNestedNewsCategoryList(t *testing.T) { |
6988a057 JH |
350 | // clients, _ := StartTestServerWithClients(2) |
351 | // client := clients[0] | |
352 | // newsPath := NewsPath{ | |
353 | // []string{ | |
354 | // "TestBundle", | |
355 | // "NestedBundle", | |
356 | // }, | |
357 | // } | |
358 | // | |
359 | // _, err := client.Connection.Write( | |
360 | // NewTransaction( | |
361 | // tranGetNewsCatNameList, 0, | |
362 | // []Field{ | |
363 | // NewField( | |
364 | // fieldNewsPath, | |
365 | // newsPath.Payload(), | |
366 | // ), | |
367 | // }, | |
368 | // ).Payload(), | |
369 | // ) | |
370 | // if err != nil { | |
371 | // t.Errorf("%v", err) | |
372 | // } | |
373 | // | |
374 | // ts := client.ReadTransactions() | |
375 | // cats := ts[0].GetFields(fieldNewsCatListData15) | |
376 | // | |
377 | // newsCat := ReadNewsCategoryListData(cats[0].Data) | |
378 | // want := "NestedCat" | |
379 | // got := newsCat.Name | |
380 | // diff := cmp.Diff(want, got) | |
381 | // if diff != "" { | |
382 | // t.Fatalf(diff) | |
383 | // } | |
aebc4d36 | 384 | // } |
6988a057 | 385 | // |
aebc4d36 | 386 | // func TestFileDownload(t *testing.T) { |
6988a057 JH |
387 | // clients, _ := StartTestServerWithClients(2) |
388 | // client := clients[0] | |
389 | // | |
390 | // type want struct { | |
391 | // fileSize []byte | |
392 | // transferSize []byte | |
393 | // waitingCount []byte | |
394 | // refNum []byte | |
395 | // } | |
396 | // var tests = []struct { | |
397 | // fileName string | |
398 | // want want | |
399 | // }{ | |
400 | // { | |
401 | // fileName: "testfile.sit", | |
402 | // want: want{ | |
403 | // fileSize: []byte{0x0, 0x0, 0x0, 0x13}, | |
404 | // transferSize: []byte{0x0, 0x0, 0x0, 0xa1}, | |
405 | // }, | |
406 | // }, | |
407 | // { | |
408 | // fileName: "testfile.txt", | |
409 | // want: want{ | |
410 | // fileSize: []byte{0x0, 0x0, 0x0, 0x17}, | |
411 | // transferSize: []byte{0x0, 0x0, 0x0, 0xa5}, | |
412 | // }, | |
413 | // }, | |
414 | // } | |
415 | // | |
416 | // for _, test := range tests { | |
417 | // _, err := client.Connection.Write( | |
418 | // NewTransaction( | |
419 | // tranDownloadFile, 0, | |
420 | // []Field{ | |
421 | // NewField(fieldFileName, []byte(test.fileName)), | |
422 | // NewField(fieldFilePath, []byte("")), | |
423 | // }, | |
424 | // ).Payload(), | |
425 | // ) | |
426 | // if err != nil { | |
427 | // t.Errorf("%v", err) | |
428 | // } | |
429 | // tran := client.ReadTransactions()[0] | |
430 | // | |
431 | // if got := tran.GetField(fieldFileSize).Data; bytes.Compare(got, test.want.fileSize) != 0 { | |
432 | // t.Errorf("TestFileDownload: fileSize got %#v, want %#v", got, test.want.fileSize) | |
433 | // } | |
434 | // | |
435 | // if got := tran.GetField(fieldTransferSize).Data; bytes.Compare(got, test.want.transferSize) != 0 { | |
436 | // t.Errorf("TestFileDownload: fieldTransferSize: %s: got %#v, want %#v", test.fileName, got, test.want.transferSize) | |
437 | // } | |
438 | // } | |
aebc4d36 | 439 | // } |
6988a057 | 440 | // |
aebc4d36 | 441 | // func TestFileUpload(t *testing.T) { |
6988a057 JH |
442 | // clients, _ := StartTestServerWithClients(2) |
443 | // client := clients[0] | |
444 | // | |
445 | // var tests = []struct { | |
446 | // fileName string | |
447 | // want Transaction | |
448 | // }{ | |
449 | // { | |
450 | // fileName: "testfile.sit", | |
451 | // want: Transaction{ | |
452 | // Fields: []Field{ | |
453 | // NewField(fieldRefNum, []byte{0x16, 0x3f, 0x5f, 0xf}), | |
454 | // }, | |
455 | // }, | |
456 | // }, | |
457 | // } | |
458 | // | |
459 | // for _, test := range tests { | |
460 | // err := client.Send( | |
461 | // NewTransaction( | |
462 | // tranUploadFile, 0, | |
463 | // []Field{ | |
464 | // NewField(fieldFileName, []byte(test.fileName)), | |
465 | // NewField(fieldFilePath, []byte("")), | |
466 | // }, | |
467 | // ), | |
468 | // ) | |
469 | // if err != nil { | |
470 | // t.Errorf("%v", err) | |
471 | // } | |
472 | // tran := client.ReadTransactions()[0] | |
473 | // | |
474 | // for _, f := range test.want.Fields { | |
475 | // got := tran.GetField(f.Uint16ID()).Data | |
476 | // want := test.want.GetField(fieldRefNum).Data | |
477 | // if bytes.Compare(got, want) != 0 { | |
478 | // t.Errorf("xxx: yyy got %#v, want %#v", got, want) | |
479 | // } | |
480 | // } | |
481 | // } | |
aebc4d36 | 482 | // } |
6988a057 | 483 | // |
aebc4d36 JH |
484 | // // TODO: Make canonical |
485 | // func TestNewUser(t *testing.T) { | |
6988a057 JH |
486 | // srv, port := StartTestServer() |
487 | // | |
488 | // var tests = []struct { | |
489 | // description string | |
490 | // setup func() | |
491 | // teardown func() | |
492 | // account Account | |
493 | // request Transaction | |
494 | // want Transaction | |
495 | // }{ | |
496 | // { | |
497 | // description: "a valid new account", | |
498 | // teardown: func() { | |
499 | // _ = srv.DeleteUser("testUser") | |
500 | // }, | |
501 | // account: Account{ | |
502 | // Login: "test", | |
503 | // Name: "unnamed", | |
504 | // Password: "test", | |
505 | // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
506 | // }, | |
507 | // request: NewTransaction( | |
508 | // tranNewUser, 0, | |
509 | // []Field{ | |
510 | // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))), | |
511 | // NewField(fieldUserName, []byte("testUserName")), | |
512 | // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))), | |
513 | // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | |
514 | // }, | |
515 | // ), | |
516 | // want: Transaction{ | |
517 | // Fields: []Field{}, | |
518 | // }, | |
519 | // }, | |
520 | // { | |
521 | // description: "a newUser request from a user without the required access", | |
522 | // teardown: func() { | |
523 | // _ = srv.DeleteUser("testUser") | |
524 | // }, | |
525 | // account: Account{ | |
526 | // Login: "test", | |
527 | // Name: "unnamed", | |
528 | // Password: "test", | |
529 | // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0}, | |
530 | // }, | |
531 | // request: NewTransaction( | |
532 | // tranNewUser, 0, | |
533 | // []Field{ | |
534 | // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("testUser")))), | |
535 | // NewField(fieldUserName, []byte("testUserName")), | |
536 | // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))), | |
537 | // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | |
538 | // }, | |
539 | // ), | |
540 | // want: Transaction{ | |
541 | // Fields: []Field{ | |
542 | // NewField(fieldError, []byte("You are not allowed to create new accounts.")), | |
543 | // }, | |
544 | // }, | |
545 | // }, | |
546 | // { | |
547 | // description: "a request to create a user that already exists", | |
548 | // teardown: func() { | |
549 | // _ = srv.DeleteUser("testUser") | |
550 | // }, | |
551 | // account: Account{ | |
552 | // Login: "test", | |
553 | // Name: "unnamed", | |
554 | // Password: "test", | |
555 | // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
556 | // }, | |
557 | // request: NewTransaction( | |
558 | // tranNewUser, 0, | |
559 | // []Field{ | |
560 | // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("guest")))), | |
561 | // NewField(fieldUserName, []byte("testUserName")), | |
562 | // NewField(fieldUserPassword, []byte(NegatedUserString([]byte("testPw")))), | |
563 | // NewField(fieldUserAccess, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | |
564 | // }, | |
565 | // ), | |
566 | // want: Transaction{ | |
567 | // Fields: []Field{ | |
568 | // NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")), | |
569 | // }, | |
570 | // }, | |
571 | // }, | |
572 | // } | |
573 | // | |
574 | // for _, test := range tests { | |
575 | // if test.setup != nil { | |
576 | // test.setup() | |
577 | // } | |
578 | // | |
579 | // if err := srv.NewUser(test.account.Login, test.account.Name, NegatedUserString([]byte(test.account.Password)), test.account.Access); err != nil { | |
580 | // t.Errorf("%v", err) | |
581 | // } | |
582 | // | |
583 | // c := NewClient("") | |
584 | // err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password) | |
585 | // if err != nil { | |
586 | // t.Errorf("login failed: %v", err) | |
587 | // } | |
588 | // | |
589 | // if err := c.Send(test.request); err != nil { | |
590 | // t.Errorf("%v", err) | |
591 | // } | |
592 | // | |
593 | // tran := c.ReadTransactions()[0] | |
594 | // for _, want := range test.want.Fields { | |
595 | // got := tran.GetField(want.Uint16ID()) | |
596 | // if bytes.Compare(got.Data, want.Data) != 0 { | |
597 | // t.Errorf("%v: field mismatch: want: %#v got: %#v", test.description, want.Data, got.Data) | |
598 | // } | |
599 | // } | |
600 | // | |
601 | // srv.DeleteUser(test.account.Login) | |
602 | // | |
603 | // if test.teardown != nil { | |
604 | // test.teardown() | |
605 | // } | |
606 | // } | |
aebc4d36 | 607 | // } |
6988a057 | 608 | // |
aebc4d36 | 609 | // func TestDeleteUser(t *testing.T) { |
6988a057 JH |
610 | // srv, port := StartTestServer() |
611 | // | |
612 | // var tests = []transactionTest{ | |
613 | // { | |
614 | // description: "a deleteUser request from a user without the required access", | |
615 | // account: Account{ | |
616 | // Login: "test", | |
617 | // Name: "unnamed", | |
618 | // Password: "test", | |
619 | // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0}, | |
620 | // }, | |
621 | // request: NewTransaction( | |
622 | // tranDeleteUser, 0, | |
623 | // []Field{ | |
624 | // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("foo")))), | |
625 | // }, | |
626 | // ), | |
627 | // want: Transaction{ | |
628 | // Fields: []Field{ | |
629 | // NewField(fieldError, []byte("You are not allowed to delete accounts.")), | |
630 | // }, | |
631 | // }, | |
632 | // }, | |
633 | // { | |
634 | // description: "a valid deleteUser request", | |
635 | // setup: func() { | |
636 | // _ = srv.NewUser("foo", "foo", "foo", []byte{0, 0, 0, 0, 0, 0, 0, 0}) | |
637 | // }, | |
638 | // account: Account{ | |
639 | // Login: "test", | |
640 | // Name: "unnamed", | |
641 | // Password: "test", | |
642 | // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
643 | // }, | |
644 | // request: NewTransaction( | |
645 | // tranDeleteUser, 0, | |
646 | // []Field{ | |
647 | // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("foo")))), | |
648 | // }, | |
649 | // ), | |
650 | // want: Transaction{ | |
651 | // Fields: []Field{}, | |
652 | // }, | |
653 | // }, | |
654 | // } | |
655 | // | |
656 | // for _, test := range tests { | |
657 | // test.Setup(srv) | |
658 | // | |
659 | // c := NewClient("") | |
660 | // err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password) | |
661 | // if err != nil { | |
662 | // t.Errorf("login failed: %v", err) | |
663 | // } | |
664 | // | |
665 | // if err := c.Send(test.request); err != nil { | |
666 | // t.Errorf("%v", err) | |
667 | // } | |
668 | // | |
669 | // tran := c.ReadTransactions()[0] | |
670 | // for _, want := range test.want.Fields { | |
671 | // got := tran.GetField(want.Uint16ID()) | |
672 | // if bytes.Compare(got.Data, want.Data) != 0 { | |
673 | // t.Errorf("%v: field mismatch: want: %#v got: %#v", test.description, want.Data, got.Data) | |
674 | // } | |
675 | // } | |
676 | // | |
677 | // test.Teardown(srv) | |
678 | // } | |
aebc4d36 | 679 | // } |
6988a057 | 680 | // |
aebc4d36 | 681 | // func TestDeleteFile(t *testing.T) { |
6988a057 JH |
682 | // srv, port := StartTestServer() |
683 | // | |
684 | // var tests = []transactionTest{ | |
685 | // { | |
686 | // description: "a request without the required access", | |
687 | // account: Account{ | |
688 | // Login: "test", | |
689 | // Name: "unnamed", | |
690 | // Password: "test", | |
691 | // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0}, | |
692 | // }, | |
693 | // request: NewTransaction( | |
694 | // tranDeleteFile, 0, | |
695 | // []Field{ | |
696 | // NewField(fieldFileName, []byte("testFile")), | |
697 | // NewField(fieldFilePath, []byte("")), | |
698 | // }, | |
699 | // ), | |
700 | // want: Transaction{ | |
701 | // Fields: []Field{}, | |
702 | // }, | |
703 | // }, | |
704 | // { | |
705 | // description: "a valid deleteFile request", | |
706 | // setup: func() { | |
707 | // _ = ioutil.WriteFile(srv.Config.FileRoot+"testFile", []byte{0x00}, 0666) | |
708 | // }, | |
709 | // account: Account{ | |
710 | // Login: "test", | |
711 | // Name: "unnamed", | |
712 | // Password: "test", | |
713 | // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
714 | // }, | |
715 | // request: NewTransaction( | |
716 | // tranDeleteFile, 0, | |
717 | // []Field{ | |
718 | // NewField(fieldFileName, []byte("testFile")), | |
719 | // NewField(fieldFilePath, []byte("")), | |
720 | // }, | |
721 | // ), | |
722 | // want: Transaction{ | |
723 | // Fields: []Field{}, | |
724 | // }, | |
725 | // }, | |
726 | // { | |
727 | // description: "an invalid request for a file that does not exist", | |
728 | // account: Account{ | |
729 | // Login: "test", | |
730 | // Name: "unnamed", | |
731 | // Password: "test", | |
732 | // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255}, | |
733 | // }, | |
734 | // request: NewTransaction( | |
735 | // tranDeleteFile, 0, | |
736 | // []Field{ | |
737 | // NewField(fieldFileName, []byte("testFile")), | |
738 | // NewField(fieldFilePath, []byte("")), | |
739 | // }, | |
740 | // ), | |
741 | // want: Transaction{ | |
742 | // Fields: []Field{ | |
743 | // NewField(fieldError, []byte("Cannot delete file testFile because it does not exist or cannot be found.")), | |
744 | // }, | |
745 | // }, | |
746 | // }, | |
747 | // } | |
748 | // | |
749 | // for _, test := range tests { | |
750 | // test.Setup(srv) | |
751 | // | |
752 | // c := NewClient("") | |
753 | // | |
754 | // if err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password); err != nil { | |
755 | // t.Errorf("login failed: %v", err) | |
756 | // } | |
757 | // | |
758 | // if err := c.Send(test.request); err != nil { | |
759 | // t.Errorf("%v", err) | |
760 | // } | |
761 | // | |
762 | // tran := c.ReadTransactions()[0] | |
763 | // for _, want := range test.want.Fields { | |
764 | // got := tran.GetField(want.Uint16ID()) | |
765 | // if bytes.Compare(got.Data, want.Data) != 0 { | |
766 | // t.Errorf("%v: field mismatch: want: %#v got: %#v", test.description, want.Data, got.Data) | |
767 | // } | |
768 | // } | |
769 | // | |
770 | // test.Teardown(srv) | |
771 | // } | |
aebc4d36 | 772 | // } |
6988a057 | 773 | // |
aebc4d36 | 774 | // func Test_authorize(t *testing.T) { |
6988a057 JH |
775 | // accessBitmap := big.NewInt(int64(0)) |
776 | // accessBitmap.SetBit(accessBitmap, accessCreateFolder, 1) | |
777 | // fmt.Printf("%v %b %x\n", accessBitmap, accessBitmap, accessBitmap) | |
778 | // fmt.Printf("%b\n", 0b10000) | |
779 | // | |
780 | // type args struct { | |
781 | // access *[]byte | |
782 | // reqAccess int | |
783 | // } | |
784 | // tests := []struct { | |
785 | // name string | |
786 | // args args | |
787 | // want bool | |
788 | // }{ | |
789 | // { | |
790 | // name: "fooz", | |
791 | // args: args{ | |
792 | // access: &[]byte{4, 0, 0, 0, 0, 0, 0, 0x02}, | |
793 | // reqAccess: accessDownloadFile, | |
794 | // }, | |
795 | // want: true, | |
796 | // }, | |
797 | // } | |
798 | // for _, tt := range tests { | |
799 | // t.Run(tt.name, func(t *testing.T) { | |
800 | // if got := authorize(tt.args.access, tt.args.reqAccess); got != tt.want { | |
801 | // t.Errorf("authorize() = %v, want %v", got, tt.want) | |
802 | // } | |
803 | // }) | |
804 | // } | |
aebc4d36 | 805 | // } |