door_test.go 5.9 KB

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