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
95 ////func TestHandleTranAgreed(t *testing.T) {
96 //// clients, _ := StartTestServerWithClients(2)
98 //// chatMsg := "Test Chat"
100 //// // Assert that both clients should receive the user join notification
101 //// var wg sync.WaitGroup
102 //// for _, client := range clients {
104 //// go func(wg *sync.WaitGroup, c *Client) {
107 //// receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data
109 //// want := []byte(fmt.Sprintf("test: %s\r", chatMsg))
110 //// if bytes.Compare(receivedMsg, want) != 0 {
111 //// t.Errorf("%q, want %q", receivedMsg, want)
116 //// trans := clients[1].ReadTransactions()
117 //// spew.Dump(trans)
119 //// // Send the agreement
120 //// clients[1].Connection.Write(
124 //// NewField(fieldUserName, []byte("testUser")),
125 //// NewField(fieldUserIconID, []byte{0x00,0x07}),
133 //func TestChatSend(t *testing.T) {
134 // //srvPort := StartTestServer()
136 // //senderClient := NewClient("senderClient")
137 // //senderClient.JoinServer(fmt.Sprintf(":%v", srvPort), "", "")
139 // //receiverClient := NewClient("receiverClient")
140 // //receiverClient.JoinServer(fmt.Sprintf(":%v", srvPort), "", "")
142 // clients, _ := StartTestServerWithClients(2)
144 // chatMsg := "Test Chat"
146 // // Both clients should receive the chatMsg
147 // var wg sync.WaitGroup
148 // for _, client := range clients {
150 // go func(wg *sync.WaitGroup, c *Client) {
153 // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data
155 // want := []byte(fmt.Sprintf(" test: %s\r", chatMsg))
156 // if bytes.Compare(receivedMsg, want) != 0 {
157 // t.Errorf("%q, want %q", receivedMsg, want)
162 // // Send the chatMsg
167 // NewField(fieldData, []byte(chatMsg)),
175 //func TestSetClientUserInfo(t *testing.T) {
176 // clients, _ := StartTestServerWithClients(2)
178 // newIcon := []byte{0x00, 0x01}
179 // newUserName := "newName"
181 // // Both clients should receive the chatMsg
182 // var wg sync.WaitGroup
183 // for _, client := range clients {
185 // go func(wg *sync.WaitGroup, c *Client) {
188 // tran := c.ReadTransactions()[0]
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)
198 // _, err := clients[1].Connection.Write(
200 // tranSetClientUserInfo, 0,
202 // NewField(fieldUserIconID, newIcon),
203 // NewField(fieldUserName, []byte(newUserName)),
208 // t.Errorf("%v", err)
214 //// TestSendInstantMsg tests that client A can send an instant message to client B
216 //func TestSendInstantMsg(t *testing.T) {
217 // clients, _ := StartTestServerWithClients(2)
219 // instantMsg := "Test IM"
221 // var wg sync.WaitGroup
223 // go func(wg *sync.WaitGroup, c *Client) {
226 // tran := c.WaitForTransaction(tranServerMsg)
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)
233 // }(&wg, clients[0])
235 // _ = clients[1].Send(
236 // NewTransaction(tranGetUserNameList, 0, []Field{}),
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]
243 // //spew.Dump(firstUserID)
246 // err := clients[1].Send(
248 // tranSendInstantMsg, 0,
250 // NewField(fieldData, []byte(instantMsg)),
251 // NewField(fieldUserName, clients[1].UserName),
252 // NewField(fieldUserID, []byte{0, 2}),
253 // NewField(fieldOptions, []byte{0, 1}),
264 //func TestOldPostNews(t *testing.T) {
265 // clients, _ := StartTestServerWithClients(2)
267 // newsPost := "Test News Post"
269 // var wg sync.WaitGroup
271 // go func(wg *sync.WaitGroup, c *Client) {
274 // receivedMsg := c.ReadTransactions()[0].GetField(fieldData).Data
276 // if strings.Contains(string(receivedMsg), newsPost) == false {
277 // t.Errorf("news post missing")
279 // }(&wg, clients[0])
281 // clients[1].Connection.Write(
283 // tranOldPostNews, 0,
285 // NewField(fieldData, []byte(newsPost)),
294 ////func TestGetFileNameList(t *testing.T) {
295 //// clients, _ := StartTestServerWithClients(2)
297 //// clients[0].Connection.Write(
299 //// tranGetFileNameList, 0,
304 //// ts := clients[0].ReadTransactions()
305 //// testfileSit := ReadFileNameWithInfo(ts[0].Fields[1].Data)
307 //// want := "testfile.sit"
308 //// got := testfileSit.Name
309 //// diff := cmp.Diff(want, got)
313 //// if testfileSit.Name != "testfile.sit" {
314 //// t.Errorf("news post missing")
315 //// t.Errorf("%q, want %q", testfileSit.Name, "testfile.sit")
319 //func TestNewsCategoryList(t *testing.T) {
320 // clients, _ := StartTestServerWithClients(2)
321 // client := clients[0]
325 // tranGetNewsCatNameList, 0,
330 // ts := client.ReadTransactions()
331 // cats := ts[0].GetFields(fieldNewsCatListData15)
333 // newsCat := ReadNewsCategoryListData(cats[0].Data)
334 // want := "TestBundle"
335 // got := newsCat.Name
336 // diff := cmp.Diff(want, got)
341 // newsBundle := ReadNewsCategoryListData(cats[1].Data)
343 // got = newsBundle.Name
344 // diff = cmp.Diff(want, got)
350 //func TestNestedNewsCategoryList(t *testing.T) {
351 // clients, _ := StartTestServerWithClients(2)
352 // client := clients[0]
353 // newsPath := NewsPath{
360 // _, err := client.Connection.Write(
362 // tranGetNewsCatNameList, 0,
366 // newsPath.Payload(),
372 // t.Errorf("%v", err)
375 // ts := client.ReadTransactions()
376 // cats := ts[0].GetFields(fieldNewsCatListData15)
378 // newsCat := ReadNewsCategoryListData(cats[0].Data)
379 // want := "NestedCat"
380 // got := newsCat.Name
381 // diff := cmp.Diff(want, got)
387 //func TestFileDownload(t *testing.T) {
388 // clients, _ := StartTestServerWithClients(2)
389 // client := clients[0]
391 // type want struct {
393 // transferSize []byte
394 // waitingCount []byte
397 // var tests = []struct {
402 // fileName: "testfile.sit",
404 // fileSize: []byte{0x0, 0x0, 0x0, 0x13},
405 // transferSize: []byte{0x0, 0x0, 0x0, 0xa1},
409 // fileName: "testfile.txt",
411 // fileSize: []byte{0x0, 0x0, 0x0, 0x17},
412 // transferSize: []byte{0x0, 0x0, 0x0, 0xa5},
417 // for _, test := range tests {
418 // _, err := client.Connection.Write(
420 // tranDownloadFile, 0,
422 // NewField(fieldFileName, []byte(test.fileName)),
423 // NewField(fieldFilePath, []byte("")),
428 // t.Errorf("%v", err)
430 // tran := client.ReadTransactions()[0]
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)
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)
442 //func TestFileUpload(t *testing.T) {
443 // clients, _ := StartTestServerWithClients(2)
444 // client := clients[0]
446 // var tests = []struct {
451 // fileName: "testfile.sit",
452 // want: Transaction{
454 // NewField(fieldRefNum, []byte{0x16, 0x3f, 0x5f, 0xf}),
460 // for _, test := range tests {
461 // err := client.Send(
463 // tranUploadFile, 0,
465 // NewField(fieldFileName, []byte(test.fileName)),
466 // NewField(fieldFilePath, []byte("")),
471 // t.Errorf("%v", err)
473 // tran := client.ReadTransactions()[0]
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)
485 //// TODO: Make canonical
486 //func TestNewUser(t *testing.T) {
487 // srv, port := StartTestServer()
489 // var tests = []struct {
490 // description string
494 // request Transaction
498 // description: "a valid new account",
499 // teardown: func() {
500 // _ = srv.DeleteUser("testUser")
506 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
508 // request: NewTransaction(
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}),
517 // want: Transaction{
518 // Fields: []Field{},
522 // description: "a newUser request from a user without the required access",
523 // teardown: func() {
524 // _ = srv.DeleteUser("testUser")
530 // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0},
532 // request: NewTransaction(
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}),
541 // want: Transaction{
543 // NewField(fieldError, []byte("You are not allowed to create new accounts.")),
548 // description: "a request to create a user that already exists",
549 // teardown: func() {
550 // _ = srv.DeleteUser("testUser")
556 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
558 // request: NewTransaction(
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}),
567 // want: Transaction{
569 // NewField(fieldError, []byte("Cannot create account guest because there is already an account with that login.")),
575 // for _, test := range tests {
576 // if test.setup != nil {
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)
584 // c := NewClient("")
585 // err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password)
587 // t.Errorf("login failed: %v", err)
590 // if err := c.Send(test.request); err != nil {
591 // t.Errorf("%v", err)
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)
602 // srv.DeleteUser(test.account.Login)
604 // if test.teardown != nil {
610 //func TestDeleteUser(t *testing.T) {
611 // srv, port := StartTestServer()
613 // var tests = []transactionTest{
615 // description: "a deleteUser request from a user without the required access",
620 // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0},
622 // request: NewTransaction(
623 // tranDeleteUser, 0,
625 // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("foo")))),
628 // want: Transaction{
630 // NewField(fieldError, []byte("You are not allowed to delete accounts.")),
635 // description: "a valid deleteUser request",
637 // _ = srv.NewUser("foo", "foo", "foo", []byte{0, 0, 0, 0, 0, 0, 0, 0})
643 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
645 // request: NewTransaction(
646 // tranDeleteUser, 0,
648 // NewField(fieldUserLogin, []byte(NegatedUserString([]byte("foo")))),
651 // want: Transaction{
652 // Fields: []Field{},
657 // for _, test := range tests {
660 // c := NewClient("")
661 // err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password)
663 // t.Errorf("login failed: %v", err)
666 // if err := c.Send(test.request); err != nil {
667 // t.Errorf("%v", err)
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)
678 // test.Teardown(srv)
682 //func TestDeleteFile(t *testing.T) {
683 // srv, port := StartTestServer()
685 // var tests = []transactionTest{
687 // description: "a request without the required access",
692 // Access: []byte{0, 0, 0, 0, 0, 0, 0, 0},
694 // request: NewTransaction(
695 // tranDeleteFile, 0,
697 // NewField(fieldFileName, []byte("testFile")),
698 // NewField(fieldFilePath, []byte("")),
701 // want: Transaction{
702 // Fields: []Field{},
706 // description: "a valid deleteFile request",
708 // _ = ioutil.WriteFile(srv.Config.FileRoot+"testFile", []byte{0x00}, 0666)
714 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
716 // request: NewTransaction(
717 // tranDeleteFile, 0,
719 // NewField(fieldFileName, []byte("testFile")),
720 // NewField(fieldFilePath, []byte("")),
723 // want: Transaction{
724 // Fields: []Field{},
728 // description: "an invalid request for a file that does not exist",
733 // Access: []byte{255, 255, 255, 255, 255, 255, 255, 255},
735 // request: NewTransaction(
736 // tranDeleteFile, 0,
738 // NewField(fieldFileName, []byte("testFile")),
739 // NewField(fieldFilePath, []byte("")),
742 // want: Transaction{
744 // NewField(fieldError, []byte("Cannot delete file testFile because it does not exist or cannot be found.")),
750 // for _, test := range tests {
753 // c := NewClient("")
755 // if err := c.JoinServer(fmt.Sprintf(":%v", port), test.account.Login, test.account.Password); err != nil {
756 // t.Errorf("login failed: %v", err)
759 // if err := c.Send(test.request); err != nil {
760 // t.Errorf("%v", err)
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)
771 // test.Teardown(srv)
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)
781 // type args struct {
785 // tests := []struct {
793 // access: &[]byte{4, 0, 0, 0, 0, 0, 0, 0x02},
794 // reqAccess: accessDownloadFile,
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)