parselog_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package ovpm
  2. import (
  3. "bytes"
  4. "io"
  5. "reflect"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func Test_parseStatusLog(t *testing.T) {
  10. const exampleLogFile = `OpenVPN CLIENT LIST
  11. Updated,Mon Mar 26 13:26:10 2018
  12. Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
  13. google.DNS,8.8.8.8:53246,527914279,3204562859,Sat Mar 17 16:26:38 2018
  14. google1.DNS,8.8.4.4:33974,42727443,291595456,Mon Mar 26 08:24:08 2018
  15. ROUTING TABLE
  16. Virtual Address,Common Name,Real Address,Last Ref
  17. 10.20.30.6,google.DNS,8.8.8.8:33974,Mon Mar 26 13:26:04 2018
  18. 10.20.30.5,google1.DNS,8.8.4.4:53246,Mon Mar 26 13:25:57 2018
  19. GLOBAL STATS
  20. Max bcast/mcast queue length,4
  21. END
  22. `
  23. // Mock the status log file.
  24. f := bytes.NewBufferString(exampleLogFile)
  25. type args struct {
  26. f io.Reader
  27. }
  28. tests := []struct {
  29. name string
  30. args args
  31. want []clEntry
  32. want1 []rtEntry
  33. }{
  34. {
  35. "google", args{f},
  36. []clEntry{
  37. clEntry{
  38. CommonName: "google.DNS",
  39. RealAddress: "8.8.8.8:53246",
  40. BytesReceived: 527914279,
  41. BytesSent: 3204562859,
  42. ConnectedSince: stodt("Sat Mar 17 16:26:38 2018"),
  43. },
  44. clEntry{
  45. CommonName: "google1.DNS",
  46. RealAddress: "8.8.4.4:33974",
  47. BytesReceived: 42727443,
  48. BytesSent: 291595456,
  49. ConnectedSince: stodt("Mon Mar 26 08:24:08 2018"),
  50. },
  51. },
  52. []rtEntry{
  53. rtEntry{
  54. VirtualAddress: "10.20.30.6",
  55. CommonName: "google.DNS",
  56. RealAddress: "8.8.8.8:33974",
  57. LastRef: stodt("Mon Mar 26 13:26:04 2018"),
  58. },
  59. rtEntry{
  60. VirtualAddress: "10.20.30.5",
  61. CommonName: "google1.DNS",
  62. RealAddress: "8.8.4.4:53246",
  63. LastRef: stodt("Mon Mar 26 13:25:57 2018"),
  64. },
  65. },
  66. },
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. got, got1 := parseStatusLog(tt.args.f)
  71. if !reflect.DeepEqual(got, tt.want) {
  72. t.Errorf("parseStatusLog() got = %v, want %v", got, tt.want)
  73. }
  74. if !reflect.DeepEqual(got1, tt.want1) {
  75. t.Errorf("parseStatusLog() got1 = %v, want %v", got1, tt.want1)
  76. }
  77. })
  78. }
  79. }
  80. func Test_parseStatusLog_CorruptStatusLog(t *testing.T) {
  81. const exampleLogFile = `OpenVPN CLIENT LIST
  82. Updated,Mon Mar 26 13:26:10 2018
  83. Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
  84. google.DNS,8.8.8.8Name,Real Addressdfs,Last Ref
  85. 10.20.30.6,google.DNS,8.8.8.8:33974,Mon Mar 26 13:26:04 2018
  86. 10.20.30.5,google1.DNS,8..4.4:53246,Mon Mar 26 13:25:57 2018
  87. GLOBAL STATS
  88. Max bcast/mcast queue length,4
  89. END
  90. `
  91. // Mock the status log file.
  92. f := bytes.NewBufferString(exampleLogFile)
  93. cl, rt := parseStatusLog(f)
  94. assert.Empty(t, cl)
  95. assert.Empty(t, rt)
  96. }