1
0

user_internal_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package ovpm
  2. import (
  3. "io"
  4. "reflect"
  5. "testing"
  6. "time"
  7. )
  8. func TestUser_ConnectionStatus(t *testing.T) {
  9. // Init:
  10. db := CreateDB("sqlite3", ":memory:")
  11. defer db.Cease()
  12. svr := TheServer()
  13. svr.Init("localhost", "", UDPProto, "", "")
  14. origOpenFunc := svr.openFunc
  15. defer func() { svr.openFunc = origOpenFunc }()
  16. svr.openFunc = func(path string) (io.Reader, error) {
  17. return nil, nil
  18. }
  19. usr1, err := CreateNewUser("usr1", "1234", true, 0, false)
  20. if err != nil {
  21. t.Fatalf("user creation failed: %v", err)
  22. }
  23. now := time.Now()
  24. svr.parseStatusLogFunc = func(f io.Reader) ([]clEntry, []rtEntry) {
  25. clt := []clEntry{
  26. clEntry{
  27. CommonName: usr1.GetUsername(),
  28. RealAddress: "1.1.1.1",
  29. ConnectedSince: now,
  30. BytesReceived: 1,
  31. BytesSent: 5,
  32. },
  33. }
  34. rtt := []rtEntry{
  35. rtEntry{
  36. CommonName: usr1.GetUsername(),
  37. RealAddress: "1.1.1.1",
  38. LastRef: now,
  39. VirtualAddress: "10.10.10.1",
  40. },
  41. }
  42. return clt, rtt
  43. }
  44. // Test:
  45. type fields struct {
  46. dbUserModel dbUserModel
  47. isConnected bool
  48. connectedSince time.Time
  49. bytesReceived uint64
  50. bytesSent uint64
  51. }
  52. tests := []struct {
  53. name string
  54. fields fields
  55. wantIsConnected bool
  56. wantConnectedSince time.Time
  57. wantBytesSent uint64
  58. wantBytesReceived uint64
  59. }{
  60. {"default", fields{dbUserModel: dbUserModel{Username: "usr1"}}, true, now, 5, 1},
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.name, func(t *testing.T) {
  64. u := &User{
  65. dbUserModel: tt.fields.dbUserModel,
  66. isConnected: tt.fields.isConnected,
  67. connectedSince: tt.fields.connectedSince,
  68. bytesReceived: tt.fields.bytesReceived,
  69. bytesSent: tt.fields.bytesSent,
  70. }
  71. gotIsConnected, gotConnectedSince, gotBytesSent, gotBytesReceived := u.ConnectionStatus()
  72. if gotIsConnected != tt.wantIsConnected {
  73. t.Errorf("User.ConnectionStatus() gotIsConnected = %v, want %v", gotIsConnected, tt.wantIsConnected)
  74. }
  75. if !reflect.DeepEqual(gotConnectedSince, tt.wantConnectedSince) {
  76. t.Errorf("User.ConnectionStatus() gotConnectedSince = %v, want %v", gotConnectedSince, tt.wantConnectedSince)
  77. }
  78. if gotBytesSent != tt.wantBytesSent {
  79. t.Errorf("User.ConnectionStatus() gotBytesSent = %v, want %v", gotBytesSent, tt.wantBytesSent)
  80. }
  81. if gotBytesReceived != tt.wantBytesReceived {
  82. t.Errorf("User.ConnectionStatus() gotBytesReceived = %v, want %v", gotBytesReceived, tt.wantBytesReceived)
  83. }
  84. })
  85. }
  86. }
  87. func init() {
  88. Testing = true
  89. }