~fkooman/vpn-server-api

ref: 453cda58bae0131116fbab4e4fc686d867e87ade vpn-server-api/tests/StorageTest.php -rw-r--r-- 5.1 KiB
453cda58François Kooman allow configuring the log format for log entries to syslog, also allow logging originating client IP 5 months ago
                                                                                
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
<?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2019, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace LC\Server\Tests;

use DateTime;
use LC\Server\Storage;
use PDO;
use PHPUnit\Framework\TestCase;

class StorageTest extends TestCase
{
    /** @var \LC\Server\Storage */
    private $storage;

    protected function setUp()
    {
        $this->storage = new Storage(
            new PDO('sqlite::memory:'),
            'schema'
        );
        $this->storage->setDateTime(new DateTime('2017-12-31T09:00:00+00:00'));
        $this->storage->init();
        $this->storage->addCertificate(
            'user_id',
            'common_name',
            'display_name',
            new DateTime('2018-01-01T00:00:00+00:00'),
            new DateTime('2018-06-06T00:00:00+00:00'),
            null
        );
        $this->storage->addCertificate(
            'other_user_id',
            'other_common_name',
            'other_display_name',
            new DateTime('2018-01-01T00:00:00+00:00'),
            new DateTime('2018-06-06T00:00:00+00:00'),
            null
        );
    }

    public function testClientConnect()
    {
        $this->storage->clientConnect(
            'internet',
            'common_name',
            '10.0.0.1',
            'fd00::',
            new DateTime('2018-02-02T08:00:00+00:00')
        );
        $this->assertSame(
            [
                'user_id' => 'user_id',
                'profile_id' => 'internet',
                'common_name' => 'common_name',
                'ip4' => '10.0.0.1',
                'ip6' => 'fd00::',
                'connected_at' => '2018-02-02T08:00:00+00:00',
                'disconnected_at' => null,
                'client_lost' => '0',
            ],
            $this->storage->getLogEntry(new DateTime('2018-02-02T10:00:00+00:00'), '10.0.0.1')
        );

        $this->assertSame(
            [
                'user_id' => 'user_id',
                'is_disabled' => false,
                'has_totp_secret' => false,
                'session_expires_at' => '2017-12-31T09:00:00+00:00',
                'permission_list' => [],
            ],
            $this->storage->getUsers()[0]
        );
    }

    public function testClientConnectDisconnect()
    {
        $this->storage->clientConnect(
            'internet',
            'common_name',
            '10.0.0.1',
            'fd00::',
            new DateTime('2018-02-02T08:00:00+00:00')
        );
        $this->storage->clientDisconnect(
            'internet',
            'common_name',
            '10.0.0.1',
            'fd00::',
            new DateTime('2018-02-02T08:00:00+00:00'),
            new DateTime('2018-02-02T11:00:00+00:00'),
            12345
        );
        $this->assertSame(
            [
                'user_id' => 'user_id',
                'profile_id' => 'internet',
                'common_name' => 'common_name',
                'ip4' => '10.0.0.1',
                'ip6' => 'fd00::',
                'connected_at' => '2018-02-02T08:00:00+00:00',
                'disconnected_at' => '2018-02-02T11:00:00+00:00',
                'client_lost' => '0',
            ],
            $this->storage->getLogEntry(new DateTime('2018-02-02T10:00:00+00:00'), '10.0.0.1')
        );
    }

    public function testClientConnectNoDisconnectReconnect()
    {
        // this test tries to test the situation where a client disconnected
        // without there being a log entry written, maybe because OpenVPN
        // crashed... so this calls 2x 'clientConnect' using the same IP
        // address which should result in the old entry being "closed" and the
        // "client_lost" boolean set...
        $this->storage->clientConnect(
            'internet',
            'common_name',
            '10.0.0.1',
            'fd00::',
            new DateTime('2018-02-02T08:00:00+00:00')
        );
        $this->storage->clientConnect(
            'internet',
            'other_common_name',
            '10.0.0.1',
            'fd00::',
            new DateTime('2018-02-02T12:00:00+00:00')
        );
        $this->assertSame(
            [
                'user_id' => 'user_id',
                'profile_id' => 'internet',
                'common_name' => 'common_name',
                'ip4' => '10.0.0.1',
                'ip6' => 'fd00::',
                'connected_at' => '2018-02-02T08:00:00+00:00',
                'disconnected_at' => '2018-02-02T12:00:00+00:00',
                'client_lost' => '1',
            ],
            $this->storage->getLogEntry(new DateTime('2018-02-02T10:00:00+00:00'), '10.0.0.1')
        );
        $this->assertSame(
            [
                'user_id' => 'other_user_id',
                'profile_id' => 'internet',
                'common_name' => 'other_common_name',
                'ip4' => '10.0.0.1',
                'ip6' => 'fd00::',
                'connected_at' => '2018-02-02T12:00:00+00:00',
                'disconnected_at' => null,
                'client_lost' => '0',
            ],
            $this->storage->getLogEntry(new DateTime('2018-02-02T12:00:01+00:00'), '10.0.0.1')
        );
    }
}