door_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package door
  2. // Need net, flag for setupSockets
  3. import (
  4. "flag"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "time"
  9. )
  10. func TestGoto(t *testing.T) {
  11. GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},
  12. "\x1b[20;10H": {10, 20},
  13. "\x1b[80;120H": {120, 80},
  14. "\x1b[1;1H": {1, 1},
  15. }
  16. for text, code := range GotoMap {
  17. gt := Goto(code[0], code[1])
  18. if text != gt {
  19. t.Errorf("Goto: Expected %#v (%#v), got %#v", text, code, gt)
  20. }
  21. }
  22. }
  23. func TestReadDropfileFail(t *testing.T) {
  24. d := Door{}
  25. defer func() {
  26. if r := recover(); r == nil {
  27. t.Error("ReadDropfile did not panic on missing dropfile.")
  28. }
  29. }()
  30. d.ReadDropfile("This_File_Will_Not_Be_Found")
  31. }
  32. func TestLogfileFailure(t *testing.T) {
  33. tmpFile, err := os.CreateTemp("", "test-*")
  34. if err != nil {
  35. panic("Cannot create temporary file")
  36. }
  37. defer func() {
  38. if r := recover(); r == nil {
  39. t.Error("Init did not panic on logfile error.")
  40. }
  41. }()
  42. // Remember to clean up the file afterwards
  43. defer os.Remove(tmpFile.Name())
  44. // This test should fail before we even need sockets
  45. var fd int = 0
  46. // Create door32.sys file
  47. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 13}
  48. _, 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",
  49. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  50. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  51. if err != nil {
  52. t.Error("tmpFile.WriteString:", err)
  53. }
  54. err = tmpFile.Close()
  55. if err != nil {
  56. t.Error("tmpFile.Close:", err)
  57. }
  58. d := Door{}
  59. // If I call d.Init() more then once flag complains about flag redefined.
  60. // Reset flags
  61. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  62. // preset commandline args so door can init
  63. os.Args = []string{"door", "-d", tmpFile.Name()}
  64. d.Init("/badname-test")
  65. }
  66. func TestReadDropFile(t *testing.T) {
  67. tmpFile, err := os.CreateTemp("", "test-*")
  68. if err != nil {
  69. panic("Cannot create temporary file")
  70. }
  71. // Clean up the dropfile afterwards
  72. defer os.Remove(tmpFile.Name())
  73. dfc := DropfileConfig{2, 20, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
  74. _, 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",
  75. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  76. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  77. if err != nil {
  78. t.Error("tmpFile.WriteString:", err)
  79. }
  80. err = tmpFile.Close()
  81. if err != nil {
  82. t.Error("tmpFile.Close:", err)
  83. }
  84. d := Door{}
  85. d.ReadDropfile(tmpFile.Name())
  86. if dfc.Comm_type != d.Config.Comm_type {
  87. t.Errorf("Comm Type expected %#v, got %#v", dfc.Comm_type, d.Config.Comm_type)
  88. }
  89. if dfc.Comm_handle != d.Config.Comm_handle {
  90. t.Errorf("Comm Handle expected %#v, got %#v", dfc.Comm_handle, d.Config.Comm_handle)
  91. }
  92. if dfc.BBSID != d.Config.BBSID {
  93. t.Errorf("BBSID expected %#v, got %#v", dfc.BBSID, d.Config.BBSID)
  94. }
  95. if dfc.User_number != d.Config.User_number {
  96. t.Errorf("User Number expected %#v, got %#v", dfc.User_number, d.Config.User_number)
  97. }
  98. if dfc.Real_name != d.Config.Real_name {
  99. t.Errorf("Real Name expected %#v, got %#v", dfc.Real_name, d.Config.Real_name)
  100. }
  101. if dfc.Handle != d.Config.Handle {
  102. t.Errorf("Handle expected %#v, got %#v", dfc.Handle, d.Config.Handle)
  103. }
  104. if dfc.Time_left != d.Config.Time_left {
  105. t.Errorf("Time Left expected %#v, got %#v", dfc.Time_left, d.Config.Time_left)
  106. }
  107. if dfc.Node != d.Config.Node {
  108. t.Errorf("Node expected %#v, got %#v", dfc.Node, d.Config.Node)
  109. }
  110. start := time.Now()
  111. timeout := time.Now().Add(time.Duration(dfc.Time_left) * time.Minute)
  112. // Verify the start time and timeout values have been set correctly.
  113. startDelta := start.Sub(d.StartTime)
  114. timeoutDelta := timeout.Sub(d.TimeOut)
  115. left := d.TimeLeft()
  116. used := d.TimeUsed()
  117. if used.Seconds() > 1 {
  118. t.Errorf("Time Used (from door) > 1 second: %#v", used)
  119. }
  120. time_left_seconds := dfc.Time_left * 60
  121. if time_left_seconds-int(left.Seconds()) > 1 {
  122. t.Errorf("Time Left differences > 1 second: test %#v door %#v", time_left_seconds, left)
  123. }
  124. if startDelta.Seconds() > 1 {
  125. t.Errorf("Start Time differences: test %#v door %#v delta %#v", start, d.StartTime, startDelta)
  126. }
  127. if timeoutDelta.Seconds() > 1 {
  128. t.Errorf("TimeOut differences: test %#v door %#v delta %#v", timeout, d.TimeOut, timeoutDelta)
  129. }
  130. }
  131. func TestDetectFail(t *testing.T) {
  132. tmpFile, err := os.CreateTemp("", "test-*")
  133. if err != nil {
  134. panic("Cannot create temporary file")
  135. }
  136. // Remember to clean up the file afterwards
  137. defer os.Remove(tmpFile.Name())
  138. // establish network socket connection to set Comm_handle
  139. server, client := setupSockets()
  140. // We're not testing closed connections, so:
  141. defer server.Close()
  142. defer client.Close()
  143. // Send nothing
  144. var fd int = socket_to_fd(client)
  145. defer close_fd(fd)
  146. // Create door32.sys file
  147. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 13}
  148. _, 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",
  149. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  150. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  151. if err != nil {
  152. t.Error("tmpFile.WriteString:", err)
  153. }
  154. err = tmpFile.Close()
  155. if err != nil {
  156. t.Error("tmpFile.Close:", err)
  157. }
  158. d := Door{ReaderCanClose: true}
  159. // Because we're not the only one calling door.Init(), the
  160. // door global variables might be from a previous test run.
  161. Unicode = false
  162. CP437 = false
  163. Full_CP437 = false
  164. Width = 0
  165. Height = 0
  166. // If I call d.Init() more then once flag complains about flag redefined.
  167. // Reset flags
  168. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  169. // preset commandline args so door can init
  170. os.Args = []string{"door", "-d", tmpFile.Name()}
  171. d.Init("menu-test")
  172. t.Logf("Init completed.")
  173. defer d.Close()
  174. // clean up log file
  175. // I don't need to. Tests are run in /tmp/go-buildNNNN.
  176. // defer os.Remove("menu-test-13.log")
  177. if Unicode || CP437 {
  178. t.Errorf("Expected FALSE, got Unicode %t CP437 %t", Unicode, CP437)
  179. }
  180. if Width != 0 || Height != 0 {
  181. t.Errorf("Expected 0, 0, got Width %d, Height %d", Width, Height)
  182. }
  183. t.Logf("Closing server and client...")
  184. server.Close()
  185. client.Close()
  186. // time.Sleep(time.Duration(1) * time.Second)
  187. t.Logf("Done.")
  188. }