~fkooman/vpn-server-api

ref: 453cda58bae0131116fbab4e4fc686d867e87ade vpn-server-api/tests/Api/ConnectionsModuleTest.php -rw-r--r-- 5.4 KiB
453cda58François Kooman allow configuring the log format for log entries to syslog, also allow logging originating client IP 9 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
165
166
167
168
169
170
171
172
173
174
175
176
<?php

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

namespace LC\Server\Tests\Api;

use DateTime;
use LC\Common\Config;
use LC\Common\Http\BasicAuthenticationHook;
use LC\Common\Http\Request;
use LC\Common\Http\Service;
use LC\Server\Storage;
use PDO;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;

class ConnectionsModuleTest extends TestCase
{
    /** @var \LC\Common\Http\Service */
    private $service;

    protected function setUp()
    {
        $storage = new Storage(
            new PDO('sqlite::memory:'),
            'schema',
            new DateTime()
        );
        $storage->init();
        $storage->updateSessionInfo('foo', new DateTime('2018-01-01 00:00:00'), ['students']);

        $storage->addCertificate('foo', '12345678901234567890123456789012', '12345678901234567890123456789012', new DateTime('@12345678'), new DateTime('@23456789'), null);
        $storage->clientConnect('internet', '12345678901234567890123456789012', '10.10.10.10', 'fd00:4242:4242:4242::', new DateTime('@12345678'));

        $config = Config::fromFile(sprintf('%s/data/config.php', __DIR__));
        $connectionsModule = new TestConnectionsModule(
            $config,
            $storage,
            new NullLogger()
        );
        $connectionsModule->setDateTime(new DateTime('2018-01-01 00:00:00'));

        $this->service = new Service();
        $this->service->addModule($connectionsModule);

        $bearerAuthentication = new BasicAuthenticationHook(
            [
                'vpn-server-node' => 'aabbcc',
            ]
        );

        $this->service->addBeforeHook('auth', $bearerAuthentication);
    }

    public function testConnect()
    {
        $this->assertTrue(
            $this->makeRequest(
                ['vpn-server-node', 'aabbcc'],
                'POST',
                'connect',
                [],
                [
                    'profile_id' => 'internet',
                    'common_name' => '12345678901234567890123456789012',
                    'originating_ip' => '1.2.3.4',
                    'ip4' => '10.10.10.10',
                    'ip6' => 'fd00:4242:4242:4242::',
                    'connected_at' => '12345678',
                ]
            )
        );
    }

    public function testConnectInAcl()
    {
        $this->assertTrue(
            $this->makeRequest(
                ['vpn-server-node', 'aabbcc'],
                'POST',
                'connect',
                [],
                [
                    'profile_id' => 'acl',
                    'common_name' => '12345678901234567890123456789012',
                    'originating_ip' => '1.2.3.4',
                    'ip4' => '10.10.10.10',
                    'ip6' => 'fd00:4242:4242:4242::',
                    'connected_at' => '12345678',
                ]
            )
        );
    }

    public function testConnectNotInAcl()
    {
        $this->assertSame(
            [
                'ok' => false,
                'error' => '[CONNECT] ERROR: unable to connect, user permissions are [students], but requires any of [employees]',
            ],
            $this->makeRequest(
                ['vpn-server-node', 'aabbcc'],
                'POST',
                'connect',
                [],
                [
                    'profile_id' => 'acl2',
                    'common_name' => '12345678901234567890123456789012',
                    'originating_ip' => '1.2.3.4',
                    'ip4' => '10.10.10.10',
                    'ip6' => 'fd00:4242:4242:4242::',
                    'connected_at' => '12345678',
                ]
            )
        );
    }

    public function testDisconnect()
    {
        $this->assertTrue(
            $this->makeRequest(
                ['vpn-server-node', 'aabbcc'],
                'POST',
                'disconnect',
                [],
                [
                    'profile_id' => 'internet',
                    'common_name' => '12345678901234567890123456789012',
                    'originating_ip' => '1.2.3.4',
                    'ip4' => '10.10.10.10',
                    'ip6' => 'fd00:4242:4242:4242::',
                    'connected_at' => '12345678',
                    'disconnected_at' => '23456789',
                    'bytes_transferred' => '2222222',
                ]
            )
        );
    }

    private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = [])
    {
        $response = $this->service->run(
            new Request(
                [
                    'SERVER_PORT' => 80,
                    'SERVER_NAME' => 'vpn.example',
                    'REQUEST_METHOD' => $requestMethod,
                    'SCRIPT_NAME' => '/index.php',
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
                    'PHP_AUTH_USER' => $basicAuth[0],
                    'PHP_AUTH_PW' => $basicAuth[1],
                ],
                $getData,
                $postData
            )
        );

        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
        if ($responseArray['ok']) {
            if (\array_key_exists('data', $responseArray)) {
                return $responseArray['data'];
            }

            return true;
        }

        // in case of errors...
        return $responseArray;
    }
}