input_test.go 6.9 KB

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