door_test.go 6.4 KB

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