door_test.go 6.4 KB

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