input_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package door
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "os"
  7. "testing"
  8. )
  9. func TestDoorInputConnection(t *testing.T) {
  10. tmpFile, err := ioutil.TempFile(os.TempDir(), "test-")
  11. if err != nil {
  12. panic("Cannot create temporary file")
  13. }
  14. // Remember to clean up the file afterwards
  15. defer os.Remove(tmpFile.Name())
  16. // establish network socket connection to set Comm_handle
  17. sock, err := net.Listen("tcp", "127.0.0.1:0")
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer sock.Close()
  22. // Get address of listening socket
  23. address := sock.Addr().String()
  24. client, err := net.Dial("tcp", address)
  25. if err != nil {
  26. panic(err)
  27. }
  28. defer client.Close()
  29. server, err := sock.Accept()
  30. if err != nil {
  31. panic(err)
  32. }
  33. defer server.Close()
  34. sock.Close()
  35. // Ok, we have a server socket, and the client socket (that the door would talk to)
  36. // t.Logf("Server: %#v\n", server)
  37. // t.Logf("Client: %#v\n", client)
  38. // unicode 190x43 response
  39. buffer := []byte("\x1b[1;1R\x1b[2;3R\x1b[43;190R")
  40. server.Write(buffer)
  41. // Access Fd (File descriptor) of client for dropfile
  42. client_conn := client.(*net.TCPConn)
  43. client_file, err := client_conn.File()
  44. var fd int = int(client_file.Fd())
  45. // Create door32.sys file
  46. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
  47. tmpFile.WriteString(fmt.Sprintf("%d\n%d\n%d\n%s\n%d\n%s\n%s\n%d\n%d\n%d\n%d\n",
  48. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  49. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  50. tmpFile.Close()
  51. d := Door{}
  52. // preset commandline args so door can init
  53. os.Args = []string{"door", "-d", tmpFile.Name()}
  54. d.Init()
  55. // Ok!
  56. if !Unicode {
  57. t.Errorf("Unicode not true %t", Unicode)
  58. }
  59. if Width != 190 {
  60. t.Errorf("Width not 190: %d", Width)
  61. }
  62. if Height != 43 {
  63. t.Errorf("Height not 43: %d", Height)
  64. }
  65. keytest := map[string][]int{
  66. "\x1b": []int{0x1b},
  67. "\x0d\x00": []int{0x0d},
  68. "\x0d\x0a": []int{0x0d},
  69. "\x0dCQ": []int{0x0d, 'C', 'Q'},
  70. "\x0dA": []int{0x0d, 'A'},
  71. "\x0dCAT": []int{0x0d, 'C', 'A', 'T'},
  72. "\x00\x50\x00\x48\x00\x4b\x00\x4d": []int{XKEY_DOWN_ARROW, XKEY_UP_ARROW, XKEY_LEFT_ARROW, XKEY_RIGHT_ARROW},
  73. "\x00\x47\x00\x4f\x00\x49\x00\x51": []int{XKEY_HOME, XKEY_END, XKEY_PGUP, XKEY_PGDN},
  74. "\x00\x3b\x00\x3c\x00\x3d\x00\x3e": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4},
  75. "\x00\x3f\x00\x40\x00\x41\x00\x42": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8},
  76. "\x00\x43\x00\x44\x00\x52\x00\x53": []int{XKEY_F9, XKEY_F10, XKEY_INSERT, XKEY_DELETE},
  77. "\x1b[A\x1b[B\x1b[C\x1b[D": []int{XKEY_UP_ARROW, XKEY_DOWN_ARROW, XKEY_RIGHT_ARROW, XKEY_LEFT_ARROW},
  78. "\x1b[H\x1b[F\x1b[K\x1b[V\x1b[U": []int{XKEY_HOME, XKEY_END, XKEY_END, XKEY_PGUP, XKEY_PGDN},
  79. "\x1b[5~\x1b[6~": []int{XKEY_PGUP, XKEY_PGDN},
  80. "\x1bOP\x1bOQ\x1bOR\x1bOS": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4},
  81. "\x1b[15~\x1b[17~\x1b[18~\x1b[19~": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8},
  82. }
  83. for send, get := range keytest {
  84. buffer := []byte(send)
  85. server.Write(buffer)
  86. recv := make([]int, 0)
  87. for {
  88. input := d.WaitKey(0, 50)
  89. if input != -1 {
  90. recv = append(recv, input)
  91. } else {
  92. break
  93. }
  94. }
  95. if len(recv) != len(get) {
  96. t.Errorf("Send %#v, LEN expected %#v, got %#v", send, get, recv)
  97. } else {
  98. matches := true
  99. for idx, i := range get {
  100. if recv[idx] != i {
  101. matches = false
  102. break
  103. }
  104. }
  105. if !matches {
  106. t.Errorf("Send %#v, MATCH expected %#v, got %#v", send, get, recv)
  107. }
  108. }
  109. }
  110. input := d.WaitKey(0, 50)
  111. if input != -1 {
  112. t.Errorf("Expected timeout, got %d / %X", input, input)
  113. } else {
  114. t.Logf("Ok! Buffer should be empty! -1 (timeout)")
  115. }
  116. server.Close()
  117. hungup := d.WaitKey(1, 0)
  118. if hungup != -2 {
  119. t.Errorf("Expected -2 (hangup), got %d", hungup)
  120. }
  121. if !d.Disconnected {
  122. t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnected)
  123. }
  124. client.Close()
  125. }