door_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. package door
  2. // Need net, flag for setupSockets
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net"
  8. "os"
  9. "testing"
  10. "time"
  11. )
  12. // Should tests not delete the logfiles?
  13. // To make use of this, set KEEP_LOGS bool = true and:
  14. // go test -c; ./door.test -test.v -test.timeout 40s
  15. // Running just one test:
  16. // go test -c; ./door.test -test.run DoorCP437 -test.v
  17. const KEEP_LOGS bool = false
  18. const VERBOSE_TEST bool = true // false
  19. func TestGoto(t *testing.T) {
  20. GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},
  21. "\x1b[20;10H": {10, 20},
  22. "\x1b[80;120H": {120, 80},
  23. "\x1b[1;1H": {1, 1},
  24. }
  25. for text, code := range GotoMap {
  26. gt := Goto(code[0], code[1])
  27. if text != gt {
  28. t.Errorf("Goto: Expected %#v (%#v), got %#v", text, code, gt)
  29. }
  30. }
  31. }
  32. func TestReadDropfileFail(t *testing.T) {
  33. d := Door{}
  34. defer func() {
  35. if r := recover(); r == nil {
  36. t.Error("ReadDropfile did not panic on missing dropfile.")
  37. }
  38. }()
  39. d.ReadDropfile("This_File_Will_Not_Be_Found")
  40. }
  41. func TestLogfileFailure(t *testing.T) {
  42. tmpFile, err := os.CreateTemp("", "test-*")
  43. if err != nil {
  44. panic("Cannot create temporary file")
  45. }
  46. defer func() {
  47. if r := recover(); r == nil {
  48. t.Error("Init did not panic on logfile error.")
  49. }
  50. }()
  51. // Remember to clean up the file afterwards
  52. defer os.Remove(tmpFile.Name())
  53. // This test should fail before we even need sockets
  54. var fd int = 0
  55. // Create door32.sys file
  56. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 13}
  57. _, err = 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",
  58. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  59. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  60. if err != nil {
  61. t.Error("tmpFile.WriteString:", err)
  62. }
  63. err = tmpFile.Close()
  64. if err != nil {
  65. t.Error("tmpFile.Close:", err)
  66. }
  67. d := Door{}
  68. // If I call d.Init() more then once flag complains about flag redefined.
  69. // Reset flags
  70. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  71. // preset commandline args so door can init
  72. os.Args = []string{"door", "-d", tmpFile.Name()}
  73. d.Init("/badname-test")
  74. }
  75. func TestReadDropFile(t *testing.T) {
  76. tmpFile, err := os.CreateTemp("", "test-*")
  77. if err != nil {
  78. panic("Cannot create temporary file")
  79. }
  80. // Clean up the dropfile afterwards
  81. defer os.Remove(tmpFile.Name())
  82. dfc := DropfileConfig{2, 20, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
  83. _, err = 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",
  84. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  85. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  86. if err != nil {
  87. t.Error("tmpFile.WriteString:", err)
  88. }
  89. err = tmpFile.Close()
  90. if err != nil {
  91. t.Error("tmpFile.Close:", err)
  92. }
  93. d := Door{}
  94. d.ReadDropfile(tmpFile.Name())
  95. if dfc.Comm_type != d.Config.Comm_type {
  96. t.Errorf("Comm Type expected %#v, got %#v", dfc.Comm_type, d.Config.Comm_type)
  97. }
  98. if dfc.Comm_handle != d.Config.Comm_handle {
  99. t.Errorf("Comm Handle expected %#v, got %#v", dfc.Comm_handle, d.Config.Comm_handle)
  100. }
  101. if dfc.BBSID != d.Config.BBSID {
  102. t.Errorf("BBSID expected %#v, got %#v", dfc.BBSID, d.Config.BBSID)
  103. }
  104. if dfc.User_number != d.Config.User_number {
  105. t.Errorf("User Number expected %#v, got %#v", dfc.User_number, d.Config.User_number)
  106. }
  107. if dfc.Real_name != d.Config.Real_name {
  108. t.Errorf("Real Name expected %#v, got %#v", dfc.Real_name, d.Config.Real_name)
  109. }
  110. if dfc.Handle != d.Config.Handle {
  111. t.Errorf("Handle expected %#v, got %#v", dfc.Handle, d.Config.Handle)
  112. }
  113. if dfc.Time_left != d.Config.Time_left {
  114. t.Errorf("Time Left expected %#v, got %#v", dfc.Time_left, d.Config.Time_left)
  115. }
  116. if dfc.Node != d.Config.Node {
  117. t.Errorf("Node expected %#v, got %#v", dfc.Node, d.Config.Node)
  118. }
  119. start := time.Now()
  120. timeout := time.Now().Add(time.Duration(dfc.Time_left) * time.Minute)
  121. // Verify the start time and timeout values have been set correctly.
  122. startDelta := start.Sub(d.StartTime)
  123. timeoutDelta := timeout.Sub(d.TimeOut)
  124. left := d.TimeLeft()
  125. used := d.TimeUsed()
  126. if used.Seconds() > 1 {
  127. t.Errorf("Time Used (from door) > 1 second: %#v", used)
  128. }
  129. time_left_seconds := dfc.Time_left * 60
  130. if time_left_seconds-int(left.Seconds()) > 1 {
  131. t.Errorf("Time Left differences > 1 second: test %#v door %#v", time_left_seconds, left)
  132. }
  133. if startDelta.Seconds() > 1 {
  134. t.Errorf("Start Time differences: test %#v door %#v delta %#v", start, d.StartTime, startDelta)
  135. }
  136. if timeoutDelta.Seconds() > 1 {
  137. t.Errorf("TimeOut differences: test %#v door %#v delta %#v", timeout, d.TimeOut, timeoutDelta)
  138. }
  139. }
  140. func TestDetectFail(t *testing.T) {
  141. tmpFile, err := os.CreateTemp("", "test-*")
  142. if err != nil {
  143. panic("Cannot create temporary file")
  144. }
  145. // Remember to clean up the file afterwards
  146. defer os.Remove(tmpFile.Name())
  147. // establish network socket connection to set Comm_handle
  148. server, client := setupSockets()
  149. // We're not testing closed connections, so:
  150. defer server.Close()
  151. defer client.Close()
  152. // Send nothing
  153. var fd int = socket_to_fd(client)
  154. defer close_fd(fd)
  155. // Create door32.sys file
  156. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 13}
  157. _, err = 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",
  158. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  159. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  160. if err != nil {
  161. t.Error("tmpFile.WriteString:", err)
  162. }
  163. err = tmpFile.Close()
  164. if err != nil {
  165. t.Error("tmpFile.Close:", err)
  166. }
  167. d := Door{ReaderCanClose: true}
  168. // Because we're not the only one calling door.Init(), the
  169. // door global variables might be from a previous test run.
  170. Unicode = false
  171. CP437 = false
  172. Full_CP437 = false
  173. Width = 0
  174. Height = 0
  175. // If I call d.Init() more then once flag complains about flag redefined.
  176. // Reset flags
  177. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  178. // preset commandline args so door can init
  179. os.Args = []string{"door", "-d", tmpFile.Name()}
  180. d.Init("menu-test")
  181. t.Logf("Init completed.")
  182. defer d.Close()
  183. // clean up log file
  184. // I don't need to. Tests are run in /tmp/go-buildNNNN.
  185. // defer os.Remove("menu-test-13.log")
  186. if Unicode || CP437 {
  187. t.Errorf("Expected FALSE, got Unicode %t CP437 %t", Unicode, CP437)
  188. }
  189. if Width != 0 || Height != 0 {
  190. t.Errorf("Expected 0, 0, got Width %d, Height %d", Width, Height)
  191. }
  192. t.Logf("Closing server and client...")
  193. server.Close()
  194. client.Close()
  195. // time.Sleep(time.Duration(1) * time.Second)
  196. t.Logf("Done.")
  197. }
  198. // Pass d*Door to test. Otherwise we get a copy of the object passed!
  199. // go vet (shows this error/issue)
  200. // Generic tests that will be called from CP437 and Unicode routines
  201. func InputTests(t *testing.T, server net.Conn, d *Door, mode string) {
  202. var err error
  203. var buffer string
  204. keytest := map[string][]rune{
  205. "\x1b": []rune{0x1b},
  206. "\x0d\x00": []rune{0x0d},
  207. "\x0d\x0a": []rune{0x0d},
  208. "\x0dCQ": []rune{0x0d, 'C', 'Q'},
  209. "\x0dA": []rune{0x0d, 'A'},
  210. "\x0dCAT": []rune{0x0d, 'C', 'A', 'T'},
  211. "\U0001f920\u2615": []rune{'\U0001f920', '\u2615'},
  212. }
  213. keyextest := map[string][]Extended{
  214. "\x00\x50\x00\x48\x00\x4b\x00\x4d": []Extended{DOWN_ARROW, UP_ARROW, LEFT_ARROW, RIGHT_ARROW},
  215. "\x00\x47\x00\x4f\x00\x49\x00\x51": []Extended{HOME, END, PAGE_UP, PAGE_DOWN},
  216. "\x00\x3b\x00\x3c\x00\x3d\x00\x3e": []Extended{F1, F2, F3, F4},
  217. "\x00\x3f\x00\x40\x00\x41\x00\x42": []Extended{F5, F6, F7, F8},
  218. "\x00\x43\x00\x44\x00\x52\x00\x53": []Extended{F9, F10, INSERT, DELETE},
  219. "\x1b[A\x1b[B\x1b[C\x1b[D": []Extended{UP_ARROW, DOWN_ARROW, RIGHT_ARROW, LEFT_ARROW},
  220. "\x1b[H\x1b[F\x1b[K\x1b[V\x1b[U": []Extended{HOME, END, END, PAGE_UP, PAGE_DOWN},
  221. "\x1b[5~\x1b[6~": []Extended{PAGE_UP, PAGE_DOWN},
  222. "\x1b[@\x1b[2~\x1b[3~": []Extended{INSERT, INSERT, DELETE},
  223. "\x1bOP\x1bOQ\x1bOR\x1bOS": []Extended{F1, F2, F3, F4},
  224. "\x1b[15~\x1b[17~\x1b[18~\x1b[19~": []Extended{F5, F6, F7, F8},
  225. "\x1b[20~\x1b[21~\x1b[23~\x1b[24~": []Extended{F9, F10, F11, F12},
  226. }
  227. keyWait := time.Duration(50 * time.Millisecond)
  228. // Verify input is empty
  229. for {
  230. input, ex, err := d.WaitKey(keyWait)
  231. if err == nil {
  232. t.Errorf("Keys: %#v, %#v\n", input, ex)
  233. } else {
  234. break
  235. }
  236. }
  237. t.Logf("Starting keytest (%s)\n", mode)
  238. for send, get := range keytest {
  239. if VERBOSE_TEST {
  240. t.Logf("Sending %#v: Expect: %#v\n", send, get)
  241. }
  242. var buffer []byte = []byte(send)
  243. _, err = server.Write(buffer)
  244. if err != nil {
  245. t.Error("server.Write:", err)
  246. }
  247. time.Sleep(time.Millisecond)
  248. var recv []rune = make([]rune, 0)
  249. var retries int = 0
  250. for {
  251. input, _, err := d.WaitKey(keyWait)
  252. if VERBOSE_TEST {
  253. t.Logf(">> %#v, %#v\n", input, err)
  254. }
  255. if err == nil {
  256. recv = append(recv, input)
  257. if len(recv) != len(get) {
  258. continue
  259. }
  260. break
  261. } else {
  262. t.Logf("WaitKey Err: %#v\n", err)
  263. if len(recv) != len(get) {
  264. if retries < 5 {
  265. retries++
  266. t.Logf("Retry %d want %d got %d\n", retries, len(get), len(recv))
  267. continue
  268. }
  269. break
  270. }
  271. }
  272. }
  273. if len(recv) != len(get) {
  274. t.Errorf("Sent %#v, LEN expected %#v, got %#v", send, get, recv)
  275. } else {
  276. matches := true
  277. for idx, i := range get {
  278. if recv[idx] != i {
  279. matches = false
  280. break
  281. }
  282. }
  283. if !matches {
  284. t.Errorf("Sent %#v, MATCH expected %#v, got %#v", send, get, recv)
  285. }
  286. }
  287. }
  288. t.Logf("Starting keyextest (%s)\n", mode)
  289. for send, get := range keyextest {
  290. if VERBOSE_TEST {
  291. t.Logf("Sending %#v: Expect: %#v\n", send, get)
  292. }
  293. var buffer []byte = []byte(send)
  294. _, err = server.Write(buffer)
  295. if err != nil {
  296. t.Error("server.Write:", err)
  297. }
  298. time.Sleep(time.Millisecond)
  299. var recv []Extended = make([]Extended, 0)
  300. var retries int = 0
  301. for {
  302. _, ex, err := d.WaitKey(keyWait)
  303. if err == nil {
  304. recv = append(recv, ex)
  305. if len(recv) != len(get) {
  306. continue
  307. }
  308. break
  309. } else {
  310. t.Logf("WaitKey err: #%v\n", err)
  311. if len(recv) != len(get) {
  312. if retries < 5 {
  313. retries++
  314. t.Logf("Retry %d want %d got %d\n", retries, len(get), len(recv))
  315. continue
  316. }
  317. break
  318. }
  319. }
  320. }
  321. if len(recv) != len(get) {
  322. t.Errorf("Sent %#v, LEN expected %#v, got %#v", send, get, recv)
  323. } else {
  324. matches := true
  325. for idx, i := range get {
  326. if recv[idx] != i {
  327. matches = false
  328. break
  329. }
  330. }
  331. if !matches {
  332. t.Errorf("Sent %#v, MATCH expected %#v, got %#v", send, get, recv)
  333. }
  334. }
  335. }
  336. clear_socket(server, t)
  337. t.Logf("Starting WaitKey timeout (%s)...\n", mode)
  338. _, _, timeout := d.WaitKey(keyWait)
  339. if timeout == nil {
  340. t.Errorf("Expected timeout, got %d / %X", timeout, timeout)
  341. } else {
  342. t.Logf("Ok! Buffer should be empty! -1 (timeout)")
  343. }
  344. t.Logf("Starting input test (%s)\n", mode)
  345. // Input test
  346. buffer = "1234567890\r"
  347. _, err = server.Write([]byte(buffer))
  348. if err != nil {
  349. t.Error("server.Write:", err)
  350. }
  351. time.Sleep(time.Millisecond)
  352. var input string = d.Input(5)
  353. if input != "12345" {
  354. t.Errorf("Expected Input(5) = 12345, but got %#v", input)
  355. }
  356. // read everything
  357. var result string = clear_socket(server, t)
  358. expected := " \x08\x08\x08\x08\x0812345\x07\x07\x07\x07"
  359. if result != expected {
  360. t.Errorf("Buffer Input(5): Expected %#v, got %#v\n", expected, result)
  361. }
  362. err = server.SetReadDeadline(time.Time{})
  363. if err != nil {
  364. t.Error("server.SetReadDeadLine:", err)
  365. }
  366. buffer = "12345678\x08\x089\r"
  367. _, err = server.Write([]byte(buffer))
  368. if err != nil {
  369. t.Error("server.Write:", err)
  370. }
  371. time.Sleep(time.Millisecond)
  372. input = d.Input(5)
  373. if input != "1239" {
  374. t.Errorf("Expected Input(5) = 1239, but got %#v", input)
  375. }
  376. buffer = "12\x08\x08\x08987\x00\x48654321\r"
  377. _, err = server.Write([]byte(buffer))
  378. if err != nil {
  379. t.Error("server.Write:", err)
  380. }
  381. time.Sleep(time.Millisecond)
  382. input = d.Input(5)
  383. if input != "98765" {
  384. t.Errorf("Expected Input(5) = 98765, but got %#v", input)
  385. }
  386. t.Logf("Starting cursor pos test (%s)\n", mode)
  387. var cpos []CursorPos = []CursorPos{{1, 1}, {50, 5}, {4, 40}, {80, 25}, {160, 55}}
  388. for _, pos := range cpos {
  389. if VERBOSE_TEST {
  390. t.Logf("Sending CursorPos(%d,%d)\n", pos.X, pos.Y)
  391. }
  392. buffer = CursorReply(pos)
  393. _, err := server.Write([]byte(buffer))
  394. if err != nil {
  395. t.Error("server.Write:", err)
  396. }
  397. }
  398. for _, pos := range cpos {
  399. _, ex, err := d.WaitKey(keyWait)
  400. if err != nil {
  401. t.Error("WaitKey:", err)
  402. continue
  403. }
  404. if ex != CURSOR {
  405. t.Errorf("Expected Extended = CURSOR (was %s)", ex.String())
  406. continue
  407. }
  408. cp, ok := d.GetCursorPos()
  409. if !ok {
  410. t.Error("Extended sent CURSOR, but GetCursorPos failed.")
  411. continue
  412. }
  413. if (cp.X != pos.X) || (cp.Y != pos.Y) {
  414. t.Errorf("Expected (%d,%d), Got (%d,%d)", pos.X, pos.Y, cp.X, cp.Y)
  415. }
  416. }
  417. t.Logf("Starting Mouse test (%s)\n", mode)
  418. var mouse []Mouse = []Mouse{{1, 4, 5}, {4, 4, 5}, {45, 80, 25}, {2, 55, 17}}
  419. for _, m := range mouse {
  420. if VERBOSE_TEST {
  421. t.Logf("Sending Mouse(%d @ %d,%d)\n", m.Button, m.X, m.Y)
  422. }
  423. buffer = MouseReply(m)
  424. _, err = server.Write([]byte(buffer))
  425. if err != nil {
  426. t.Error("server.Write:", err)
  427. }
  428. }
  429. for _, pos := range mouse {
  430. _, ex, err := d.WaitKey(keyWait)
  431. if err != nil {
  432. t.Error("WaitKey:", err)
  433. continue
  434. }
  435. if ex != MOUSE {
  436. t.Errorf("Expected Extended = MOUSE (was %s)", ex.String())
  437. continue
  438. }
  439. mpos, ok := d.GetMouse()
  440. if !ok {
  441. t.Error("Extended sent MOUSE, but GetMouse failed.")
  442. continue
  443. }
  444. if (pos.Button != mpos.Button) || (pos.X != mpos.X) || (pos.Y != mpos.Y) {
  445. t.Errorf("Expected (%d @ %d, %d), Got (%d @ %d, %d)",
  446. pos.Button, pos.X, pos.Y, mpos.Button, mpos.X, mpos.Y)
  447. }
  448. }
  449. }
  450. func TestDoorCP437(t *testing.T) {
  451. var tmpFile *os.File
  452. var err error
  453. // Dropfile
  454. tmpFile, err = os.CreateTemp("", "test-*")
  455. if err != nil {
  456. panic("Cannot create temporary file")
  457. }
  458. defer os.Remove(tmpFile.Name())
  459. // establish network socket connection to set Comm_handle
  460. var server, client net.Conn
  461. server, client = setupSockets()
  462. // Ok, we have a server socket, and the client socket (that the door would talk to)
  463. // CP437 80x25 response
  464. buffer := CP437WidthHeight(80, 25)
  465. _, err = server.Write([]byte(buffer))
  466. if err != nil {
  467. t.Error("server.Write:", err)
  468. }
  469. time.Sleep(time.Millisecond)
  470. // Access Fd (File descriptor) of client for dropfile
  471. var fd int = socket_to_fd(client)
  472. defer close_fd(fd)
  473. // Create door32.sys file
  474. var node int = 10
  475. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, node}
  476. _, err = 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",
  477. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  478. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  479. if err != nil {
  480. t.Error("tmpFile.WriteString:", err)
  481. }
  482. err = tmpFile.Close()
  483. if err != nil {
  484. t.Error("tmpFile.Close:", err)
  485. }
  486. d := Door{} // Deprecated: ReaderCanClose: true}
  487. // Because we're not the only one calling door.Init(), the
  488. // door global variables might be from a previous test run.
  489. // Reset back to defaults.
  490. Unicode = false
  491. CP437 = false
  492. Full_CP437 = false
  493. Width = 0
  494. Height = 0
  495. // If I call d.Init() more then once flag complains about flag redefined.
  496. // Reset flags
  497. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  498. // preset commandline args so door can init
  499. os.Args = []string{"door", "-d", tmpFile.Name()}
  500. d.Init("input-test")
  501. defer d.Close()
  502. // clean up logfile - (filename is from Init name + node #)
  503. if !KEEP_LOGS {
  504. defer os.Remove(fmt.Sprintf("input-test-%d.log", node))
  505. }
  506. if VERBOSE_TEST {
  507. t.Logf("Verify Unicode %dX%d\n", 80, 25)
  508. }
  509. // Ok!
  510. if Unicode {
  511. t.Errorf("Unicode is %t", Unicode)
  512. }
  513. if !CP437 {
  514. t.Errorf("Expect CP437 true, is %t", CP437)
  515. }
  516. if Width != 80 {
  517. t.Errorf("Width not 80: %d", Width)
  518. }
  519. if Height != 25 {
  520. t.Errorf("Height not 25: %d", Height)
  521. }
  522. clear_socket(server, t)
  523. InputTests(t, server, &d, "CP437")
  524. t.Logf("server close")
  525. log.Println("server close")
  526. server.Close()
  527. time.Sleep(time.Millisecond)
  528. keyWait := time.Duration(50 * time.Millisecond)
  529. _, _, err = d.WaitKey(keyWait)
  530. if err != ErrDisconnected {
  531. t.Errorf("Expected ErrDisconnected, got %#v", err)
  532. }
  533. if !d.Disconnect() {
  534. t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnect())
  535. }
  536. _, _, err = d.WaitKey(time.Millisecond)
  537. if err != ErrDisconnected {
  538. t.Errorf("Expected ErrDisconnected, got %#v", err)
  539. }
  540. t.Logf("client close")
  541. client.Close()
  542. time.Sleep(time.Millisecond)
  543. t.Logf("Input on closed server and client")
  544. var blank string = d.Input(5)
  545. if blank != "" {
  546. t.Errorf("Input should return blank (hangup).")
  547. }
  548. _, _, err = d.WaitKey(time.Millisecond)
  549. if err != ErrDisconnected {
  550. t.Errorf("Expected ErrDisconnected, got %#v", err)
  551. }
  552. d.Write("\x00")
  553. time.Sleep(time.Millisecond)
  554. }
  555. func TestDoorUnicode(t *testing.T) {
  556. var tmpFile *os.File
  557. var err error
  558. // Dropfile
  559. tmpFile, err = os.CreateTemp("", "test-*")
  560. if err != nil {
  561. panic("Cannot create temporary file")
  562. }
  563. defer os.Remove(tmpFile.Name())
  564. // establish network socket connection to set Comm_handle
  565. var server, client net.Conn
  566. server, client = setupSockets()
  567. // Ok, we have a server socket, and the client socket (that the door would talk to)
  568. // unicode 190x43 response
  569. buffer := UnicodeWidthHeight(190, 43)
  570. _, err = server.Write([]byte(buffer))
  571. if err != nil {
  572. t.Error("server.Write:", err)
  573. }
  574. time.Sleep(time.Millisecond)
  575. // Access Fd (File descriptor) of client for dropfile
  576. var fd int = socket_to_fd(client)
  577. defer close_fd(fd)
  578. // Create door32.sys file
  579. var node int = 11
  580. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, node}
  581. _, err = 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",
  582. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  583. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  584. if err != nil {
  585. t.Error("tmpFile.WriteString:", err)
  586. }
  587. err = tmpFile.Close()
  588. if err != nil {
  589. t.Error("tmpFile.Close:", err)
  590. }
  591. d := Door{} // Deprecated: ReaderCanClose: true}
  592. // Because we're not the only one calling door.Init(), the
  593. // door global variables might be from a previous test run.
  594. // Reset back to defaults.
  595. Unicode = false
  596. CP437 = false
  597. Full_CP437 = false
  598. Width = 0
  599. Height = 0
  600. // If I call d.Init() more then once flag complains about flag redefined.
  601. // Reset flags
  602. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  603. // preset commandline args so door can init
  604. os.Args = []string{"door", "-d", tmpFile.Name()}
  605. d.Init("input-test")
  606. defer d.Close()
  607. // clean up logfile - (filename is from Init name + node #)
  608. if !KEEP_LOGS {
  609. defer os.Remove(fmt.Sprintf("input-test-%d.log", node))
  610. }
  611. // Ok!
  612. if !Unicode {
  613. t.Errorf("Unicode not true %t", Unicode)
  614. }
  615. if Width != 190 {
  616. t.Errorf("Width not 190: %d", Width)
  617. }
  618. if Height != 43 {
  619. t.Errorf("Height not 43: %d", Height)
  620. }
  621. clear_socket(server, t)
  622. keyWait := time.Duration(50 * time.Millisecond)
  623. InputTests(t, server, &d, "Unicode")
  624. buffer = "\U0001f9201234\r"
  625. // double wide (2) + 4 = 6, we're inputting 5.
  626. _, err = server.Write([]byte(buffer))
  627. if err != nil {
  628. t.Error("server.Write:", err)
  629. }
  630. time.Sleep(time.Millisecond)
  631. // Unicode mode can handle unicode input. CP437 mode can't -- really.
  632. input := d.Input(5)
  633. expect := "\U0001f920123"
  634. if input != expect {
  635. t.Errorf("Expected Input(5) = %#v, but got %#v", expect, input)
  636. }
  637. clear_socket(server, t)
  638. t.Logf("server close")
  639. log.Println("server close")
  640. server.Close()
  641. time.Sleep(time.Millisecond)
  642. _, _, err = d.WaitKey(keyWait)
  643. if err != ErrDisconnected {
  644. t.Errorf("Expected ErrDisconnected, got %#v", err)
  645. }
  646. if !d.Disconnect() {
  647. t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnect())
  648. }
  649. _, _, err = d.WaitKey(time.Millisecond)
  650. if err != ErrDisconnected {
  651. t.Errorf("Expected ErrDisconnected, got %#v", err)
  652. }
  653. t.Logf("client close")
  654. client.Close()
  655. time.Sleep(time.Millisecond)
  656. t.Logf("Input on closed server and client")
  657. var blank string = d.Input(5)
  658. if blank != "" {
  659. t.Errorf("Input should return blank (hangup).")
  660. }
  661. _, _, err = d.WaitKey(time.Millisecond)
  662. if err != ErrDisconnected {
  663. t.Errorf("Expected ErrDisconnected, got %#v", err)
  664. }
  665. d.Write("\x00")
  666. time.Sleep(time.Millisecond)
  667. }