dropfile_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package door
  2. import (
  3. "os"
  4. "strconv"
  5. "testing"
  6. "time"
  7. )
  8. // Create a valid door32.sys file from the config
  9. func CreateDoor32File(config *DropfileConfig, file *os.File) error {
  10. var err error
  11. // These are tests, but let's do it right!
  12. var lines [11]string = [11]string{"0",
  13. "0", "38400", "", "1", "<User Real Name>",
  14. "<Handle>", "0", "0", "1", "1"}
  15. lines[0] = strconv.Itoa(config.Comm_type)
  16. lines[1] = strconv.Itoa(config.Comm_handle)
  17. lines[3] = config.BBSID
  18. lines[4] = strconv.Itoa(config.User_number)
  19. lines[5] = config.Real_name
  20. lines[6] = config.Handle
  21. lines[7] = strconv.Itoa(config.Security_level)
  22. lines[8] = strconv.Itoa(config.Time_left)
  23. lines[10] = strconv.Itoa(config.Node)
  24. // Write out the lines
  25. for _, line := range lines {
  26. _, err = file.WriteString(line + "\n")
  27. if err != nil {
  28. return err
  29. }
  30. }
  31. return err
  32. }
  33. // Create a valid door.sys file from the config
  34. func CreateDoorSysFile(config *DropfileConfig, file *os.File) error {
  35. var err error
  36. // These are tests, but let's do this right.
  37. var lines [52]string = [52]string{"0", "38400", "8", "0", "57600",
  38. "Y", "Y", "Y", "Y", "<User Real Name>", "<User From>",
  39. "XXX XXX-XXXX", "XXX XXX-XXXX", "PASSWORD", "0", "0",
  40. "01/01/23", "0", "0", "GR", "23", "Y", "123ABC", "0",
  41. "12/31/99", "0", "Z", "0", "0", "0", "0", "04/01/23",
  42. "C:\\MAIN", "C:\\GEN", "<SysOp Name>", "<Handle>", "00:00",
  43. "Y", "Y", "Y", "0", "1", "01/01/23", "00:00", "23:59",
  44. "0", "0", "0", "0", "<Comment>", "0", "0"}
  45. if config.Comm_type == 0 {
  46. lines[0] = "COM0:"
  47. } else {
  48. lines[0] = "COM1:"
  49. }
  50. lines[3] = strconv.Itoa(config.Node)
  51. lines[9] = config.Real_name
  52. lines[14] = strconv.Itoa(config.Security_level)
  53. lines[17] = strconv.Itoa(config.Time_left * 60)
  54. lines[18] = strconv.Itoa(config.Time_left)
  55. lines[25] = strconv.Itoa(config.User_number)
  56. lines[35] = config.Handle
  57. // Write out the lines
  58. for _, line := range lines {
  59. _, err = file.WriteString(line + "\n")
  60. if err != nil {
  61. return err
  62. }
  63. }
  64. return err
  65. }
  66. func TestInvalidDropfileFail(t *testing.T) {
  67. d := Door{}
  68. defer func() {
  69. if r := recover(); r == nil {
  70. t.Error("ReadDropfile did not panic on missing dropfile.")
  71. }
  72. }()
  73. d.ReadDropfile("dropfile_unknown.sys")
  74. }
  75. func TestMissingDropfileFail(t *testing.T) {
  76. d := Door{}
  77. defer func() {
  78. if r := recover(); r == nil {
  79. t.Error("ReadDropfile did not panic on missing dropfile.")
  80. }
  81. }()
  82. d.ReadDropfile("MissingDropFile_door.sys")
  83. }
  84. func TestReadDoor32DropFile(t *testing.T) {
  85. tmpFile, err := os.CreateTemp("", "test-door32.sys-*")
  86. if err != nil {
  87. panic("Cannot create temporary file")
  88. }
  89. // Clean up the dropfile afterwards
  90. defer os.Remove(tmpFile.Name())
  91. dfc := DropfileConfig{2, 20, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 12}
  92. err = CreateDoor32File(&dfc, tmpFile)
  93. if err != nil {
  94. t.Error("tmpFile.WriteString:", err)
  95. }
  96. err = tmpFile.Close()
  97. if err != nil {
  98. t.Error("tmpFile.Close:", err)
  99. }
  100. /*
  101. // Are my testing dropfiles correct?
  102. tmpFile, err = os.Create("door32.sys")
  103. CreateDoor32File(&dfc, tmpFile)
  104. tmpFile.Close()
  105. */
  106. d := Door{}
  107. d.ReadDropfile(tmpFile.Name())
  108. if dfc.Comm_type != d.Config.Comm_type {
  109. t.Errorf("Comm Type expected %#v, got %#v", dfc.Comm_type, d.Config.Comm_type)
  110. }
  111. if dfc.Comm_handle != d.Config.Comm_handle {
  112. t.Errorf("Comm Handle expected %#v, got %#v", dfc.Comm_handle, d.Config.Comm_handle)
  113. }
  114. if dfc.BBSID != d.Config.BBSID {
  115. t.Errorf("BBSID expected %#v, got %#v", dfc.BBSID, d.Config.BBSID)
  116. }
  117. if dfc.User_number != d.Config.User_number {
  118. t.Errorf("User Number expected %#v, got %#v", dfc.User_number, d.Config.User_number)
  119. }
  120. if dfc.Real_name != d.Config.Real_name {
  121. t.Errorf("Real Name expected %#v, got %#v", dfc.Real_name, d.Config.Real_name)
  122. }
  123. if dfc.Handle != d.Config.Handle {
  124. t.Errorf("Handle expected %#v, got %#v", dfc.Handle, d.Config.Handle)
  125. }
  126. if dfc.Time_left != d.Config.Time_left {
  127. t.Errorf("Time Left expected %#v, got %#v", dfc.Time_left, d.Config.Time_left)
  128. }
  129. if dfc.Node != d.Config.Node {
  130. t.Errorf("Node expected %#v, got %#v", dfc.Node, d.Config.Node)
  131. }
  132. start := time.Now()
  133. timeout := time.Now().Add(time.Duration(dfc.Time_left) * time.Minute)
  134. // Verify the start time and timeout values have been set correctly.
  135. startDelta := start.Sub(d.StartTime)
  136. timeoutDelta := timeout.Sub(d.TimeOut)
  137. left := d.TimeLeft()
  138. used := d.TimeUsed()
  139. if used.Seconds() > 1 {
  140. t.Errorf("Time Used (from door) > 1 second: %#v", used)
  141. }
  142. time_left_seconds := dfc.Time_left * 60
  143. if time_left_seconds-int(left.Seconds()) > 1 {
  144. t.Errorf("Time Left differences > 1 second: test %#v door %#v", time_left_seconds, left)
  145. }
  146. if startDelta.Seconds() > 1 {
  147. t.Errorf("Start Time differences: test %#v door %#v delta %#v", start, d.StartTime, startDelta)
  148. }
  149. if timeoutDelta.Seconds() > 1 {
  150. t.Errorf("TimeOut differences: test %#v door %#v delta %#v", timeout, d.TimeOut, timeoutDelta)
  151. }
  152. }
  153. func TestReadDoorSysDropFile(t *testing.T) {
  154. tmpFile, err := os.CreateTemp("", "test-door.sys-*")
  155. if err != nil {
  156. panic("Cannot create temporary file")
  157. }
  158. // Clean up the dropfile afterwards
  159. defer os.Remove(tmpFile.Name())
  160. dfc := DropfileConfig{2, 20, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 12}
  161. err = CreateDoorSysFile(&dfc, tmpFile)
  162. if err != nil {
  163. t.Error("tmpFile.WriteString:", err)
  164. }
  165. err = tmpFile.Close()
  166. if err != nil {
  167. t.Error("tmpFile.Close:", err)
  168. }
  169. /*
  170. // Are my testing dropfiles correct?
  171. tmpFile, err = os.Create("door.sys")
  172. CreateDoorSysFile(&dfc, tmpFile)
  173. tmpFile.Close()
  174. */
  175. d := Door{}
  176. d.ReadDropfile(tmpFile.Name())
  177. /*
  178. if dfc.Comm_type != d.Config.Comm_type {
  179. t.Errorf("Comm Type expected %#v, got %#v", dfc.Comm_type, d.Config.Comm_type)
  180. }
  181. if dfc.Comm_handle != d.Config.Comm_handle {
  182. t.Errorf("Comm Handle expected %#v, got %#v", dfc.Comm_handle, d.Config.Comm_handle)
  183. }
  184. if dfc.BBSID != d.Config.BBSID {
  185. t.Errorf("BBSID expected %#v, got %#v", dfc.BBSID, d.Config.BBSID)
  186. }
  187. */
  188. if dfc.User_number != d.Config.User_number {
  189. t.Errorf("User Number expected %#v, got %#v", dfc.User_number, d.Config.User_number)
  190. }
  191. if dfc.Real_name != d.Config.Real_name {
  192. t.Errorf("Real Name expected %#v, got %#v", dfc.Real_name, d.Config.Real_name)
  193. }
  194. if dfc.Handle != d.Config.Handle {
  195. t.Errorf("Handle expected %#v, got %#v", dfc.Handle, d.Config.Handle)
  196. }
  197. if dfc.Time_left != d.Config.Time_left {
  198. t.Errorf("Time Left expected %#v, got %#v", dfc.Time_left, d.Config.Time_left)
  199. }
  200. if dfc.Node != d.Config.Node {
  201. t.Errorf("Node expected %#v, got %#v", dfc.Node, d.Config.Node)
  202. }
  203. start := time.Now()
  204. timeout := time.Now().Add(time.Duration(dfc.Time_left) * time.Minute)
  205. // Verify the start time and timeout values have been set correctly.
  206. startDelta := start.Sub(d.StartTime)
  207. timeoutDelta := timeout.Sub(d.TimeOut)
  208. left := d.TimeLeft()
  209. used := d.TimeUsed()
  210. if used.Seconds() > 1 {
  211. t.Errorf("Time Used (from door) > 1 second: %#v", used)
  212. }
  213. time_left_seconds := dfc.Time_left * 60
  214. if time_left_seconds-int(left.Seconds()) > 1 {
  215. t.Errorf("Time Left differences > 1 second: test %#v door %#v", time_left_seconds, left)
  216. }
  217. if startDelta.Seconds() > 1 {
  218. t.Errorf("Start Time differences: test %#v door %#v delta %#v", start, d.StartTime, startDelta)
  219. }
  220. if timeoutDelta.Seconds() > 1 {
  221. t.Errorf("TimeOut differences: test %#v door %#v delta %#v", timeout, d.TimeOut, timeoutDelta)
  222. }
  223. }