door_test.go 5.8 KB

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