1
0

parselog_test.go 2.0 KB

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