-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathvpn_test.go
616 lines (508 loc) · 13.7 KB
/
vpn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package ovpm
import (
"fmt"
"io"
"reflect"
"strings"
"testing"
"time"
"github.com/cad/ovpm/pki"
"github.com/cad/ovpm/supervisor"
"github.com/sirupsen/logrus"
)
var fs map[string]string
func setupTestCase() {
// Initialize.
fs = make(map[string]string)
vpnProc.Stop()
}
func TestVPNInit(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Prepare:
// Test:
// Check database if the database has no server.
var server dbServerModel
db.First(&server)
// Isn't server empty struct?
if !db.NewRecord(&server) {
t.Fatalf("server is expected to be empty struct(new record) but it isn't %+v", server)
}
// Wrongfully initialize server.
if err := TheServer().Init("localhost", "asdf", UDPProto, "", "", "", "", false); err == nil {
t.Fatalf("error is expected to be not nil but it's nil instead")
}
// Initialize the server.
TheServer().Init("localhost", "", UDPProto, "", "", "", "", false)
// Check database if the database has no server.
var server2 dbServerModel
db.First(&server2)
// Is server empty struct?
if db.NewRecord(&server2) {
t.Fatalf("server is expected to be not empty struct(new record) but it is %+v", server2)
}
}
func TestVPNDeinit(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Prepare:
// Initialize the server.
TheServer().Init("localhost", "", UDPProto, "", "", "", "", false)
u, err := CreateNewUser("user", "p", false, 0, true, "description")
if err != nil {
t.Fatal(err)
}
u.Delete()
// Test:
var server dbServerModel
db.First(&server)
// Isn't server empty struct?
if db.NewRecord(&server) {
t.Fatalf("server is expected to be not empty struct(new record) but it is %+v", server)
}
// Test if Revoked table contains the removed user's entries.
var revoked dbRevokedModel
db.First(&revoked)
if db.NewRecord(&revoked) {
t.Errorf("revoked shouldn't be empty")
}
// Deinitialize.
TheServer().Deinit()
// Get server from db.
var server2 dbServerModel
db.First(&server2)
// Isn't server empty struct?
if !db.NewRecord(&server2) {
t.Fatalf("server is expected to be empty struct(new record) but it is not %+v", server2)
}
// Test if Revoked table contains the removed user's entries.
var revoked2 dbRevokedModel
db.First(&revoked2)
// Is revoked empty?
if !db.NewRecord(&revoked2) {
t.Errorf("revoked should be empty")
}
}
func TestVPNUpdate(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Prepare:
TheServer().Init("localhost", "", UDPProto, "", "", "", "", false)
// Test:
var updatetests = []struct {
vpnnet string
dns string
useLZO *bool
vpnChanged bool
dnsChanged bool
}{
{"", "", nil, false, false},
{"192.168.9.0/24", "", nil, true, false},
{"", "2.2.2.2", nil, false, true},
{"9.9.9.0/24", "1.1.1.1", nil, true, true},
{"9.9.9.0/24", "1.1.1.1", nil, true, true},
}
for i, tt := range updatetests {
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
oldIP := svr.Net
oldDNS := svr.DNS
svr.Update(tt.vpnnet, tt.dns, tt.useLZO)
svr = nil
svr = TheServer()
if (svr.Net != oldIP) != tt.vpnChanged {
fmt.Println(i, svr.Net, oldIP, svr.Net == oldIP, tt.vpnChanged)
t.Fatalf("expected vpn change: %t but opposite happened", tt.vpnChanged)
}
if (svr.DNS != oldDNS) != tt.dnsChanged {
t.Fatalf("expected vpn change: %t but opposite happened", tt.dnsChanged)
}
}
}
func TestVPNIsInitialized(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Prepare:
// Test:
// Is initialized?
if TheServer().IsInitialized() {
t.Fatalf("IsInitialized() is expected to return false but it returned true")
}
// Initialize the server.
TheServer().Init("localhost", "", UDPProto, "", "", "", "", false)
// Isn't initialized?
if !TheServer().IsInitialized() {
t.Fatalf("IsInitialized() is expected to return true but it returned false")
}
}
func TestVPNTheServer(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Prepare:
// Test:
svr := TheServer()
// Isn't server nil?
if svr.IsInitialized() {
t.Fatal("server is expected to be not initialized it is")
}
// Initialize server.
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
svr = TheServer()
// Is server nil?
if !svr.IsInitialized() {
t.Fatal("server is expected to be initialized but it's not")
}
}
func TestVPNDumpsClientConfig(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Prepare:
user, err := CreateNewUser("user", "password", false, 0, true, "description")
if err != nil {
t.Fatal(err)
}
// Test:
clientConfigBlob, err := svr.DumpsClientConfig(user.GetUsername())
if err != nil {
t.Fatalf("expected to dump client config but we got error instead: %v", err)
}
// Is empty?
if len(clientConfigBlob) == 0 {
t.Fatal("expected the dump not empty but it's empty instead")
}
}
func TestVPNDumpClientConfig(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Prepare:
noGW := false
user, err := CreateNewUser("user", "password", noGW, 0, true, "description")
if err != nil {
t.Fatalf("can not create user: %v", err)
}
// Test:
if err = svr.DumpClientConfig(user.GetUsername(), "/tmp/user.ovpn"); err != nil {
t.Fatalf("expected to dump client config but we got error instead: %v", err)
}
// Read file.
clientConfigBlob := fs["/tmp/user.ovpn"]
// Is empty?
if len(clientConfigBlob) == 0 {
t.Fatal("expected the dump not empty but it's empty instead")
}
// Is noGW honored?
if strings.Contains(clientConfigBlob, "route-nopull") != noGW {
logrus.Info(clientConfigBlob)
t.Fatalf("client config generator doesn't honor NoGW")
}
user.Delete()
noGW = true
user, err = CreateNewUser("user", "password", noGW, 0, true, "description")
if err != nil {
t.Fatalf("can not create user: %v", err)
}
if err = TheServer().DumpClientConfig(user.GetUsername(), "/tmp/user.ovpn"); err != nil {
t.Fatalf("expected to dump client config but we got error instead: %v", err)
}
// Read file.
clientConfigBlob = fs["/tmp/user.ovpn"]
}
func TestVPNGetSystemCA(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Prepare:
svr := TheServer()
// Test:
ca, err := svr.GetSystemCA()
if err == nil {
t.Fatalf("GetSystemCA() is expected to give error but it didn't instead")
}
// Initialize system.
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
ca, err = svr.GetSystemCA()
if err != nil {
t.Fatalf("GetSystemCA() is expected to get system ca, but it gave us an error instead: %v", err)
}
// Is it empty?
if len(ca.Cert) == 0 {
t.Fatalf("ca.Cert is expected to be not empty, but it's empty instead")
}
if len(ca.Key) == 0 {
t.Fatalf("ca.Key is expected to be not empty, but it's empty instead")
}
}
func TestVPNStartVPNProc(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
// Prepare:
// Test:
// Isn't it stopped?
if vpnProc.Status() != supervisor.STOPPED {
t.Fatalf("expected state is STOPPED, got %s instead", vpnProc.Status())
}
// Call start without server initialization.
svr.StartVPNProc()
// Isn't it still stopped?
if vpnProc.Status() != supervisor.STOPPED {
t.Fatalf("expected state is STOPPED, got %s instead", vpnProc.Status())
}
// Initialize OVPM server.
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Call start again..
svr.StartVPNProc()
// Isn't it RUNNING?
if vpnProc.Status() != supervisor.RUNNING {
t.Fatalf("expected state is RUNNING, got %s instead", vpnProc.Status())
}
}
func TestVPNStopVPNProc(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Prepare:
vpnProc.Start()
// Test:
// Isn't it running?
if vpnProc.Status() != supervisor.RUNNING {
t.Fatalf("expected state is RUNNING, got %s instead", vpnProc.Status())
}
// Call stop.
svr.StopVPNProc()
// Isn't it stopped?
if vpnProc.Status() != supervisor.STOPPED {
t.Fatalf("expected state is STOPPED, got %s instead", vpnProc.Status())
}
}
func TestVPNRestartVPNProc(t *testing.T) {
// Init:
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Prepare:
// Test:
// Call restart.
svr.RestartVPNProc()
// Isn't it running?
if vpnProc.Status() != supervisor.RUNNING {
t.Fatalf("expected state is RUNNING, got %s instead", vpnProc.Status())
}
// Call restart again.
svr.RestartVPNProc()
// Isn't it running?
if vpnProc.Status() != supervisor.RUNNING {
t.Fatalf("expected state is RUNNING, got %s instead", vpnProc.Status())
}
}
func TestVPNEmit(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Prepare:
// Test:
svr.Emit()
var emittests = []string{
_DefaultVPNConfPath,
_DefaultKeyPath,
_DefaultCertPath,
_DefaultCRLPath,
_DefaultCACertPath,
_DefaultCAKeyPath,
_DefaultDHParamsPath,
}
for _, tt := range emittests {
if len(fs[tt]) == 0 {
t.Errorf("%s is expected to be not empty but it is", tt)
}
}
// TODO(cad): Write test cases for ccd/ files as well.
}
func TestVPNemitToFile(t *testing.T) {
// Initialize:
// Prepare:
path := "/test/file"
content := "blah blah blah"
// Test:
// Is path exist?
if _, ok := fs[path]; ok {
t.Fatalf("key '%s' expected to be non-existent on fs, but it is instead", path)
}
// Emit the contents.
if err := TheServer().emitToFile(path, content, 0); err != nil {
t.Fatalf("expected to be able to emit to the filesystem but we got this error instead: %v", err)
}
// Is the content on the filesystem correct?
if fs[path] != content {
t.Fatalf("content on the filesytem is expected to be same with '%s' but it's '%s' instead", content, fs[path])
}
}
type fakeProcess struct {
state supervisor.State
}
func (f *fakeProcess) Start() {
f.state = supervisor.RUNNING
}
func (f *fakeProcess) Stop() {
f.state = supervisor.STOPPED
}
func (f *fakeProcess) Restart() {
f.state = supervisor.RUNNING
}
func (f *fakeProcess) Status() supervisor.State {
return f.state
}
func TestGetConnectedUsers(t *testing.T) {
// Init:
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Mock funcs.
svr.openFunc = func(path string) (io.Reader, error) {
return nil, nil
}
// Create the corresponding users for test.
usr1, err := CreateNewUser("usr1", "1234", true, 0, false, "description")
if err != nil {
t.Fatalf("user creation failed: %v", err)
}
usr2, err := CreateNewUser("usr2", "1234", true, 0, false, "description")
if err != nil {
t.Fatalf("user creation failed: %v", err)
}
now := time.Now()
svr.parseStatusLogFunc = func(f io.Reader) ([]clEntry, []rtEntry) {
clt := []clEntry{
clEntry{
CommonName: usr1.GetUsername(),
RealAddress: "1.1.1.1",
ConnectedSince: now,
BytesReceived: 1,
BytesSent: 5,
},
clEntry{
CommonName: usr2.GetUsername(),
RealAddress: "1.1.1.2",
ConnectedSince: now,
BytesReceived: 2,
BytesSent: 6,
},
}
rtt := []rtEntry{
rtEntry{
CommonName: usr1.GetUsername(),
RealAddress: "1.1.1.1",
LastRef: now,
VirtualAddress: "10.10.10.1",
},
rtEntry{
CommonName: usr2.GetUsername(),
RealAddress: "1.1.1.2",
LastRef: now,
VirtualAddress: "10.10.10.2",
},
}
return clt, rtt
}
// Test:
tests := []struct {
name string
want []User
wantErr bool
}{
{"default", []User{*usr2, *usr1}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TheServer().GetConnectedUsers()
if (err != nil) != tt.wantErr {
t.Errorf("GetConnectedUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
for _, wu := range tt.want {
var found bool
for _, u := range got {
if wu.GetUsername() == u.GetUsername() {
found = true
}
}
if !found {
t.Errorf("wanted user (%s) is not present in the response we got %v", wu.GetUsername(), got)
}
}
})
}
}
func TestVPN_ExpiresAt(t *testing.T) {
// Initialize:
db := CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Test:
cert, err := pki.ReadCertFromPEM(svr.Cert)
if err != nil {
t.Fatalf("test preperation failed: %v", err)
}
if !reflect.DeepEqual(svr.ExpiresAt(), cert.NotAfter) {
t.Errorf("got (%s), want (%s)", svr.ExpiresAt(), cert.NotAfter)
}
}
func TestVPN_CAExpiresAt(t *testing.T) {
// Initialize:
db := CreateDB("sqlite3", ":memory:")
defer db.Cease()
svr := TheServer()
svr.Init("localhost", "", UDPProto, "", "", "", "", false)
// Test:
cert, err := pki.ReadCertFromPEM(svr.CACert)
if err != nil {
t.Fatalf("test preperation failed: %v", err)
}
if !reflect.DeepEqual(svr.CAExpiresAt(), cert.NotAfter) {
t.Errorf("got (%s), want (%s)", svr.CAExpiresAt(), cert.NotAfter)
}
}
func init() {
// Init
Testing = true
fs = make(map[string]string)
CreateDB("sqlite3", ":memory:")
defer db.Cease()
// Monkeypatch emitToFile()
TheServer().emitToFileFunc = func(path, content string, mode uint) error {
fs[path] = content
return nil
}
vpnProc = &fakeProcess{state: supervisor.STOPPED}
}