input_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package door
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "time"
  9. )
  10. func TestDoorInputConnection(t *testing.T) {
  11. tmpFile, err := ioutil.TempFile(os.TempDir(), "test-")
  12. if err != nil {
  13. panic("Cannot create temporary file")
  14. }
  15. // Remember to clean up the file afterwards
  16. defer os.Remove(tmpFile.Name())
  17. // establish network socket connection to set Comm_handle
  18. server, client := setupSockets()
  19. // Ok, we have a server socket, and the client socket (that the door would talk to)
  20. // unicode 190x43 response
  21. buffer := []byte("\x1b[1;1R\x1b[2;3R\x1b[43;190R")
  22. server.Write(buffer)
  23. // Access Fd (File descriptor) of client for dropfile
  24. var fd int = socket_to_fd(client)
  25. // Create door32.sys file
  26. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
  27. 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",
  28. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  29. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  30. tmpFile.Close()
  31. d := Door{}
  32. // If I call d.Init() more then once flag complains about flag redefined.
  33. // Reset flags
  34. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  35. // preset commandline args so door can init
  36. os.Args = []string{"door", "-d", tmpFile.Name()}
  37. d.Init("input-test")
  38. // clean up logfile
  39. defer os.Remove("input-test-12.log")
  40. // Ok!
  41. if !Unicode {
  42. t.Errorf("Unicode not true %t", Unicode)
  43. }
  44. if Width != 190 {
  45. t.Errorf("Width not 190: %d", Width)
  46. }
  47. if Height != 43 {
  48. t.Errorf("Height not 43: %d", Height)
  49. }
  50. // These are the commands sent to detect ... throw this all away.
  51. buffer = make([]byte, 128)
  52. server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  53. r, err := server.Read(buffer)
  54. // t.Errorf("Buffer : %#v\n", buffer[:r])
  55. server.SetReadDeadline(time.Time{})
  56. keytest := map[string][]int{
  57. "\x1b": []int{0x1b},
  58. "\x0d\x00": []int{0x0d},
  59. "\x0d\x0a": []int{0x0d},
  60. "\x0dCQ": []int{0x0d, 'C', 'Q'},
  61. "\x0dA": []int{0x0d, 'A'},
  62. "\x0dCAT": []int{0x0d, 'C', 'A', 'T'},
  63. "\x00\x50\x00\x48\x00\x4b\x00\x4d": []int{XKEY_DOWN_ARROW, XKEY_UP_ARROW, XKEY_LEFT_ARROW, XKEY_RIGHT_ARROW},
  64. "\x00\x47\x00\x4f\x00\x49\x00\x51": []int{XKEY_HOME, XKEY_END, XKEY_PGUP, XKEY_PGDN},
  65. "\x00\x3b\x00\x3c\x00\x3d\x00\x3e": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4},
  66. "\x00\x3f\x00\x40\x00\x41\x00\x42": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8},
  67. "\x00\x43\x00\x44\x00\x52\x00\x53": []int{XKEY_F9, XKEY_F10, XKEY_INSERT, XKEY_DELETE},
  68. "\x1b[A\x1b[B\x1b[C\x1b[D": []int{XKEY_UP_ARROW, XKEY_DOWN_ARROW, XKEY_RIGHT_ARROW, XKEY_LEFT_ARROW},
  69. "\x1b[H\x1b[F\x1b[K\x1b[V\x1b[U": []int{XKEY_HOME, XKEY_END, XKEY_END, XKEY_PGUP, XKEY_PGDN},
  70. "\x1b[5~\x1b[6~": []int{XKEY_PGUP, XKEY_PGDN},
  71. "\x1b[@\x1b[2~\x1b[3~": []int{XKEY_INSERT, XKEY_INSERT, XKEY_DELETE},
  72. "\x1bOP\x1bOQ\x1bOR\x1bOS": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4},
  73. "\x1b[15~\x1b[17~\x1b[18~\x1b[19~": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8},
  74. "\x1b[20~\x1b[21~\x1b[23~\x1b[24~": []int{XKEY_F9, XKEY_F10, XKEY_F11, XKEY_F12},
  75. }
  76. for send, get := range keytest {
  77. buffer := []byte(send)
  78. server.Write(buffer)
  79. recv := make([]int, 0)
  80. for {
  81. input := d.WaitKey(0, 50)
  82. if input != -1 {
  83. recv = append(recv, input)
  84. } else {
  85. break
  86. }
  87. }
  88. if len(recv) != len(get) {
  89. t.Errorf("Send %#v, LEN expected %#v, got %#v", send, get, recv)
  90. } else {
  91. matches := true
  92. for idx, i := range get {
  93. if recv[idx] != i {
  94. matches = false
  95. break
  96. }
  97. }
  98. if !matches {
  99. t.Errorf("Send %#v, MATCH expected %#v, got %#v", send, get, recv)
  100. }
  101. }
  102. }
  103. buffer = make([]byte, 128)
  104. server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  105. r, err = server.Read(buffer)
  106. if r != 0 {
  107. t.Errorf("Buffer After KeyTest: %#v\n", buffer[:r])
  108. }
  109. server.SetReadDeadline(time.Time{})
  110. timeout := d.WaitKey(0, 50)
  111. if timeout != -1 {
  112. t.Errorf("Expected timeout, got %d / %X", timeout, timeout)
  113. } else {
  114. t.Logf("Ok! Buffer should be empty! -1 (timeout)")
  115. }
  116. // Input test
  117. buffer = []byte("1234567890\r")
  118. server.Write(buffer)
  119. input := d.Input(5)
  120. if input != "12345" {
  121. t.Errorf("Expected Input(5) = 12345, but got %#v", input)
  122. }
  123. // I'm not sure what they extra characters are in the buffer here.
  124. buffer = make([]byte, 128)
  125. server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  126. r, err = server.Read(buffer)
  127. result := string(buffer[:r])
  128. expected := " \x08\x08\x08\x08\x0812345\x07\x07\x07\x07\x07"
  129. if result != expected {
  130. t.Errorf("Buffer Input(5): Expected %#v, got %#v\n", expected, result)
  131. }
  132. server.SetReadDeadline(time.Time{})
  133. buffer = []byte("12345678\x08\x089\r")
  134. server.Write(buffer)
  135. input = d.Input(5)
  136. if input != "1239" {
  137. t.Errorf("Expected Input(5) = 1239, but got %#v", input)
  138. }
  139. buffer = []byte("12\x08\x08\x08987\x00\x48654321\r")
  140. server.Write(buffer)
  141. input = d.Input(5)
  142. if input != "98765" {
  143. t.Errorf("Expected Input(5) = 98765, but got %#v", input)
  144. }
  145. server.Close()
  146. hungup := d.WaitKey(1, 0)
  147. if hungup != -2 {
  148. t.Errorf("Expected -2 (hangup), got %d", hungup)
  149. }
  150. if !d.Disconnected {
  151. t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnected)
  152. }
  153. /*
  154. if !d.HasKey() {
  155. t.Error("HasKey should return true (disconnected).")
  156. }
  157. */
  158. hungup = d.GetKey()
  159. if hungup != -2 {
  160. t.Errorf("Expected -2 (hangup), got %d", hungup)
  161. }
  162. client.Close()
  163. blank := d.Input(5)
  164. if blank != "" {
  165. t.Errorf("Input should return blank (hangup).")
  166. }
  167. hungup = d.getch()
  168. if hungup != -2 {
  169. t.Errorf("Expected -2 (hangup), got %d", hungup)
  170. }
  171. }
  172. func TestDisplayInput(t *testing.T) {
  173. verify := map[int]string{1: " \x08",
  174. 2: " \x08\x08",
  175. 5: " \x08\x08\x08\x08\x08",
  176. }
  177. for count, expect := range verify {
  178. got := DisplayInput(count)
  179. if expect != got {
  180. t.Errorf("DisplayInput %d, expected %#v, got %#v", count, expect, got)
  181. }
  182. }
  183. }