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