7 // "github.com/google/go-cmp/cmp"
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
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 {
30 // if tt.setup != nil {
37 //func (tt *transactionTest) Teardown(srv *Server) error {
38 // if err := srv.DeleteUser(tt.account.Login); err != nil {
42 // if tt.teardown != nil {
50 //func StartTestServer() (srv *Server, lnPort int) {
51 // hotlineServer, _ := NewServer("test/config/")
52 // ln, err := net.Listen("tcp", ":0")
59 // conn, _ := ln.Accept()
60 // go hotlineServer.HandleConnection(conn)
63 // return hotlineServer, ln.Addr().(*net.TCPAddr).Port
66 //func StartTestClient(serverPort int, login, passwd string) (*Client, error) {
69 // err := c.JoinServer(fmt.Sprintf(":%v", serverPort), login, passwd)
77 //func StartTestServerWithClients(clientCount int) ([]*Client, int) {
78 // _, serverPort := StartTestServer()
80 // var clients []*Client
81 // for i := 0; i < clientCount; i++ {
82 // client, err := StartTestClient(serverPort, "admin", "")
86 // clients = append(clients, client)
88 // clients[0].ReadN(2)
90 // return clients, serverPort
94 ////func TestHandleTranAgreed(t *testing.T) {
95 //// clients, _ := StartTestServerWithClients(2)
97 //// chatMsg := "Test Chat"
99 //// // Assert that both clients should receive the user join notification
100 //// var wg sync.WaitGroup
101 //// for _, client := range clients {
103 //// go func(wg *sync.WaitGroup, c *Client) {
106 //// receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data
108 //// want := []byte(fmt.Sprintf("test: %s\r", chatMsg))
109 //// if bytes.Compare(receivedMsg, want) != 0 {
110 //// t.Errorf("%q, want %q", receivedMsg, want)
115 //// trans := clients[1].ReadTransactions()
116 //// spew.Dump(trans)
118 //// // Send the agreement
119 //// clients[1].Connection.Write(
123 //// NewField(fieldUserName, []byte("testUser")),
124 //// NewField(fieldUserIconID, []byte{0x00,0x07}),
132 //func TestChatSend(t *testing.T) {
133 // //srvPort := StartTestServer()
135 // //senderClient := NewClient("senderClient")
136 // //senderClient.JoinServer(fmt.Sprintf(":%v", srvPort), "", "")
138 // //receiverClient := NewClient("receiverClient")
139 // //receiverClient.JoinServer(fmt.Sprintf(":%v", srvPort), "", "")
141 // clients, _ := StartTestServerWithClients(2)
143 // chatMsg := "Test Chat"
145 // // Both clients should receive the chatMsg
146 // var wg sync.WaitGroup
147 // for _, client := range clients {
149 // go func(wg *sync.WaitGroup, c *Client) {
152 // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data
154 // want := []byte(fmt.Sprintf(" test: %s\r", chatMsg))
155 // if bytes.Compare(receivedMsg, want) != 0 {
156 // t.Errorf("%q, want %q", receivedMsg, want)
161 // // Send the chatMsg
166 // NewField(fieldData, []byte(chatMsg)),
174 //func TestSetClientUserInfo(t *testing.T) {
175 // clients, _ := StartTestServerWithClients(2)
177 // newIcon := []byte{0x00, 0x01}
178 // newUserName := "newName"
180 // // Both clients should receive the chatMsg
181 // var wg sync.WaitGroup
182 // for _, client := range clients {
184 // go func(wg *sync.WaitGroup, c *Client) {
187 // tran := c.ReadTransactions()[0]
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)
197 // _, err := clients[1].Connection.Write(
199 // tranSetClientUserInfo, 0,
201 // NewField(fieldUserIconID, newIcon),
202 // NewField(fieldUserName, []byte(newUserName)),
207 // t.Errorf("%v", err)
213 //// TestSendInstantMsg tests that client A can send an instant message to client B
215 //func TestSendInstantMsg(t *testing.T) {
216 // clients, _ := StartTestServerWithClients(2)
218 // instantMsg := "Test IM"
220 // var wg sync.WaitGroup
222 // go func(wg *sync.WaitGroup, c *Client) {
225 // tran := c.WaitForTransaction(tranServerMsg)
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)
232 // }(&wg, clients[0])
234 // _ = clients[1].Send(
235 // NewTransaction(tranGetUserNameList, 0, []Field{}),
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]
242 // //spew.Dump(firstUserID)
245 // err := clients[1].Send(
247 // tranSendInstantMsg, 0,
249 // NewField(fieldData, []byte(instantMsg)),
250 // NewField(fieldUserName, clients[1].UserName),
251 // NewField(fieldUserID, []byte{0, 2}),
252 // NewField(fieldOptions, []byte{0, 1}),
263 //func TestOldPostNews(t *testing.T) {
264 // clients, _ := StartTestServerWithClients(2)
266 // newsPost := "Test News Post"
268 // var wg sync.WaitGroup
270 // go func(wg *sync.WaitGroup, c *Client) {
273 // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data
275 // if strings.Contains(string(receivedMsg), newsPost) == false {
276 // t.Errorf("news post missing")
278 // }(&wg, clients[0])
280 // clients[1].Connection.Write(
282 // tranOldPostNews, 0,
284 // NewField(fieldData, []byte(newsPost)),
293 ////func TestGetFileNameList(t *testing.T) {
294 //// clients, _ := StartTestServerWithClients(2)
296 //// clients[0].Connection.Write(
298 //// tranGetFileNameList, 0,
303 //// ts := clients[0].ReadTransactions()
304 //// testfileSit := ReadFileNameWithInfo(ts[0].Fields[1].Data)
306 //// want := "testfile.sit"
307 //// got := testfileSit.Name
308 //// diff := cmp.Diff(want, got)
312 //// if testfileSit.Name != "testfile.sit" {
313 //// t.Errorf("news post missing")
314 //// t.Errorf("%q, want %q", testfileSit.Name, "testfile.sit")
318 //func TestNewsCategoryList(t *testing.T) {
319 // clients, _ := StartTestServerWithClients(2)
320 // client := clients[0]
324 // tranGetNewsCatNameList, 0,
329 // ts := client.ReadTransactions()
330 // cats := ts[0].GetFields(fieldNewsCatListData15)
332 // newsCat := ReadNewsCategoryListData(cats[0].Data)
333 // want := "TestBundle"
334 // got := newsCat.Name
335 // diff := cmp.Diff(want, got)
340 // newsBundle := ReadNewsCategoryListData(cats[1].Data)
342 // got = newsBundle.Name
343 // diff = cmp.Diff(want, got)
349 //func TestNestedNewsCategoryList(t *testing.T) {
350 // clients, _ := StartTestServerWithClients(2)
351 // client := clients[0]
352 // newsPath := NewsPath{
359 // _, err := client.Connection.Write(
361 // tranGetNewsCatNameList, 0,
365 // newsPath.Payload(),
371 // t.Errorf("%v", err)
374 // ts := client.ReadTransactions()
375 // cats := ts[0].GetFields(fieldNewsCatListData15)
377 // newsCat := ReadNewsCategoryListData(cats[0].Data)
378 // want := "NestedCat"
379 // got := newsCat.Name
380 // diff := cmp.Diff(want, got)
386 //func TestFileDownload(t *testing.T) {
387 // clients, _ := StartTestServerWithClients(2)
388 // client := clients[0]
390 // type want struct {
392 // transferSize []byte
393 // waitingCount []byte
396 // var tests = []struct {
401 // fileName: "testfile.sit",
403 // fileSize: []byte{0x0, 0x0, 0x0, 0x13},
404 // transferSize: []byte{0x0, 0x0, 0x0, 0xa1},
408 // fileName: "testfile.txt",
410 // fileSize: []byte{0x0, 0x0, 0x0, 0x17},
411 // transferSize: []byte{0x0, 0x0, 0x0, 0xa5},
416 // for _, test := range tests {
417 // _, err := client.Connection.Write(
419 // tranDownloadFile, 0,
421 // NewField(fieldFileName, []byte(test.fileName)),
422 // NewField(fieldFilePath, []byte("")),
427 // t.Errorf("%v", err)
429 // tran := client.ReadTransactions()[0]
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)
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)
441 //func TestFileUpload(t *testing.T) {
442 // clients, _ := StartTestServerWithClients(2)
443 // client := clients[0]
445 // var tests = []struct {
450 // fileName: "testfile.sit",
451 // want: Transaction{
453 // NewField(fieldRefNum, []byte{0x16, 0x3f, 0x5f, 0xf}),
459 // for _, test := range tests {
460 // err := client.Send(
462 // tranUploadFile, 0,
464 // NewField(fieldFileName, []byte(test.fileName)),
465 // NewField(fieldFilePath, []byte("")),
470 // t.Errorf("%v", err)
472 // tran := client.ReadTransactions()[0]
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)
484 //// TODO: Make canonical
485 //func TestNewUser(t *testing.T) {
486 // srv, port := StartTestServer()
488 // var tests = []struct {
489 // description string
493 // request Transaction
497 // description: "a valid new account",
498 // teardown: func() {
499 // _ = srv.DeleteUser("testUser")
505 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
507 // request: NewTransaction(
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}),
516 // want: Transaction{
517 // Fields: []Field{},
521 // description: "a newUser request from a user without the required access",
522 // teardown: func() {
523 // _ = srv.DeleteUser("testUser")
529 // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0},
531 // request: NewTransaction(
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}),
540 // want: Transaction{
542 // NewField(fieldError, []byte("You are not allowed to create new accounts.")),
547 // description: "a request to create a user that already exists",
548 // teardown: func() {
549 // _ = srv.DeleteUser("testUser")
555 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
557 // request: NewTransaction(
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}),
566 // want: Transaction{
568 // NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")),
574 // for _, test := range tests {
575 // if test.setup != nil {
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)
583 // c := NewClient("")
584 // err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password)
586 // t.Errorf("login failed: %v", err)
589 // if err := c.Send(test.request); err != nil {
590 // t.Errorf("%v", err)
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)
601 // srv.DeleteUser(test.account.Login)
603 // if test.teardown != nil {
609 //func TestDeleteUser(t *testing.T) {
610 // srv, port := StartTestServer()
612 // var tests = []transactionTest{
614 // description: "a deleteUser request from a user without the required access",
619 // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0},
621 // request: NewTransaction(
622 // tranDeleteUser, 0,
624 // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("foo")))),
627 // want: Transaction{
629 // NewField(fieldError, []byte("You are not allowed to delete accounts.")),
634 // description: "a valid deleteUser request",
636 // _ = srv.NewUser("foo", "foo", "foo", []byte{0, 0, 0, 0, 0, 0, 0, 0})
642 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
644 // request: NewTransaction(
645 // tranDeleteUser, 0,
647 // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("foo")))),
650 // want: Transaction{
651 // Fields: []Field{},
656 // for _, test := range tests {
659 // c := NewClient("")
660 // err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password)
662 // t.Errorf("login failed: %v", err)
665 // if err := c.Send(test.request); err != nil {
666 // t.Errorf("%v", err)
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)
677 // test.Teardown(srv)
681 //func TestDeleteFile(t *testing.T) {
682 // srv, port := StartTestServer()
684 // var tests = []transactionTest{
686 // description: "a request without the required access",
691 // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0},
693 // request: NewTransaction(
694 // tranDeleteFile, 0,
696 // NewField(fieldFileName, []byte("testFile")),
697 // NewField(fieldFilePath, []byte("")),
700 // want: Transaction{
701 // Fields: []Field{},
705 // description: "a valid deleteFile request",
707 // _ = ioutil.WriteFile(srv.Config.FileRoot+"testFile", []byte{0x00}, 0666)
713 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
715 // request: NewTransaction(
716 // tranDeleteFile, 0,
718 // NewField(fieldFileName, []byte("testFile")),
719 // NewField(fieldFilePath, []byte("")),
722 // want: Transaction{
723 // Fields: []Field{},
727 // description: "an invalid request for a file that does not exist",
732 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
734 // request: NewTransaction(
735 // tranDeleteFile, 0,
737 // NewField(fieldFileName, []byte("testFile")),
738 // NewField(fieldFilePath, []byte("")),
741 // want: Transaction{
743 // NewField(fieldError, []byte("Cannot delete file testFile because it does not exist or cannot be found.")),
749 // for _, test := range tests {
752 // c := NewClient("")
754 // if err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password); err != nil {
755 // t.Errorf("login failed: %v", err)
758 // if err := c.Send(test.request); err != nil {
759 // t.Errorf("%v", err)
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)
770 // test.Teardown(srv)
774 //func Test_authorize(t *testing.T) {
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)
780 // type args struct {
784 // tests := []struct {
792 // access: &[]byte{4, 0, 0, 0, 0, 0, 0, 0x02},
793 // reqAccess: accessDownloadFile,
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)