input_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "\x1b[@\x1b[2~\x1b[3~": []int{XKEY_INSERT, XKEY_INSERT, XKEY_DELETE},
  81. "\x1bOP\x1bOQ\x1bOR\x1bOS": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4},
  82. "\x1b[15~\x1b[17~\x1b[18~\x1b[19~": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8},
  83. "\x1b[20~\x1b[21~\x1b[23~\x1b[24~": []int{XKEY_F9, XKEY_F10, XKEY_F11, XKEY_F12},
  84. }
  85. for send, get := range keytest {
  86. buffer := []byte(send)
  87. server.Write(buffer)
  88. recv := make([]int, 0)
  89. for {
  90. input := d.WaitKey(0, 50)
  91. if input != -1 {
  92. recv = append(recv, input)
  93. } else {
  94. break
  95. }
  96. }
  97. if len(recv) != len(get) {
  98. t.Errorf("Send %#v, LEN expected %#v, got %#v", send, get, recv)
  99. } else {
  100. matches := true
  101. for idx, i := range get {
  102. if recv[idx] != i {
  103. matches = false
  104. break
  105. }
  106. }
  107. if !matches {
  108. t.Errorf("Send %#v, MATCH expected %#v, got %#v", send, get, recv)
  109. }
  110. }
  111. }
  112. timeout := d.WaitKey(0, 50)
  113. if timeout != -1 {
  114. t.Errorf("Expected timeout, got %d / %X", timeout, timeout)
  115. } else {
  116. t.Logf("Ok! Buffer should be empty! -1 (timeout)")
  117. }
  118. // Input test
  119. buffer = []byte("1234567890\r")
  120. server.Write(buffer)
  121. input := d.Input(5)
  122. if input != "12345" {
  123. t.Errorf("Expected Input(5) = 12345, but got %#v", input)
  124. }
  125. buffer = []byte("12345678\x08\x089\r")
  126. server.Write(buffer)
  127. input = d.Input(5)
  128. if input != "1239" {
  129. t.Errorf("Expected Input(5) = 1239, but got %#v", input)
  130. }
  131. buffer = []byte("12\x08\x08\x08987\x00\x48654321\r")
  132. server.Write(buffer)
  133. input = d.Input(5)
  134. if input != "98765" {
  135. t.Errorf("Expected Input(5) = 98765, but got %#v", input)
  136. }
  137. server.Close()
  138. hungup := d.WaitKey(1, 0)
  139. if hungup != -2 {
  140. t.Errorf("Expected -2 (hangup), got %d", hungup)
  141. }
  142. if !d.Disconnected {
  143. t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnected)
  144. }
  145. client.Close()
  146. }
  147. func TestDisplayInput(t *testing.T) {
  148. verify := map[int]string{1: " \x08",
  149. 2: " \x08\x08",
  150. 5: " \x08\x08\x08\x08\x08",
  151. }
  152. for count, expect := range verify {
  153. got := DisplayInput(count)
  154. if expect != got {
  155. t.Errorf("DisplayInput %d, expected %#v, got %#v", count, expect, got)
  156. }
  157. }
  158. }