door_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package door
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "time"
  8. )
  9. func TestGoto(t *testing.T) {
  10. GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},
  11. "\x1b[20;10H": {10, 20},
  12. "\x1b[80;120H": {120, 80},
  13. "\x1b[1;1H": {1, 1},
  14. }
  15. for text, code := range GotoMap {
  16. gt := Goto(code[0], code[1])
  17. if text != gt {
  18. t.Errorf("Goto: Expected %#v (%#v), got %#v", text, code, gt)
  19. }
  20. }
  21. }
  22. func TestReadDropfileFail(t *testing.T) {
  23. d := Door{}
  24. defer func() {
  25. if r := recover(); r == nil {
  26. t.Error("ReadDropfile did not panic on missing dropfile.")
  27. }
  28. }()
  29. d.ReadDropfile("This_File_Will_Not_Be_Found")
  30. }
  31. func TestReadDropFile(t *testing.T) {
  32. tmpFile, err := ioutil.TempFile(os.TempDir(), "test-")
  33. if err != nil {
  34. panic("Cannot create temporary file")
  35. }
  36. // Remember to clean up the file afterwards
  37. defer os.Remove(tmpFile.Name())
  38. dfc := DropfileConfig{2, 20, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
  39. 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",
  40. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  41. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  42. // tmpFile.WriteString("2\n20\n0\nTest BBSID\n1701\nReal Username\nHandle This\n880\n25\n0\n12\n")
  43. tmpFile.Close()
  44. d := Door{}
  45. d.ReadDropfile(tmpFile.Name())
  46. if dfc.Comm_type != d.Config.Comm_type {
  47. t.Errorf("Comm Type expected %#v, got %#v", dfc.Comm_type, d.Config.Comm_type)
  48. }
  49. if dfc.Comm_handle != d.Config.Comm_handle {
  50. t.Errorf("Comm Handle expected %#v, got %#v", dfc.Comm_handle, d.Config.Comm_handle)
  51. }
  52. if dfc.BBSID != d.Config.BBSID {
  53. t.Errorf("BBSID expected %#v, got %#v", dfc.BBSID, d.Config.BBSID)
  54. }
  55. if dfc.User_number != d.Config.User_number {
  56. t.Errorf("User Number expected %#v, got %#v", dfc.User_number, d.Config.User_number)
  57. }
  58. if dfc.Real_name != d.Config.Real_name {
  59. t.Errorf("Real Name expected %#v, got %#v", dfc.Real_name, d.Config.Real_name)
  60. }
  61. if dfc.Handle != d.Config.Handle {
  62. t.Errorf("Handle expected %#v, got %#v", dfc.Handle, d.Config.Handle)
  63. }
  64. if dfc.Time_left != d.Config.Time_left {
  65. t.Errorf("Time Left expected %#v, got %#v", dfc.Time_left, d.Config.Time_left)
  66. }
  67. if dfc.Node != d.Config.Node {
  68. t.Errorf("Node expected %#v, got %#v", dfc.Node, d.Config.Node)
  69. }
  70. start := time.Now()
  71. timeout := time.Now().Add(time.Duration(dfc.Time_left) * time.Minute)
  72. // Verify the start time and timeout values have been set correctly.
  73. startDelta := start.Sub(d.StartTime)
  74. timeoutDelta := timeout.Sub(d.TimeOut)
  75. if startDelta.Seconds() > 1 {
  76. t.Errorf("Start Time differences: test %#v door %#v delta %#v", start, d.StartTime, startDelta)
  77. }
  78. if timeoutDelta.Seconds() > 1 {
  79. t.Errorf("TimeOut differences: test %#v door %#v delta %#v", timeout, d.TimeOut, timeoutDelta)
  80. }
  81. }