summaryrefslogtreecommitdiff
path: root/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.kt
blob: 53bfd00a2bed2adf68691ece41035d19f83e3ae5 (plain)
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
/*
 * Copyright (c) 2012-2016 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.core

import android.content.Context
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import de.blinkt.openvpn.R
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.io.IOException
import java.io.StringReader
import java.util.*

/**
 * Created by arne on 03.10.16.
 */

const val miniconfig = "client\nremote test.blinkt.de\n"
const val fakeCerts = "<ca>\n" +
        "-----BEGIN CERTIFICATE-----\n" +
        "\n" +
        "-----END CERTIFICATE-----\n" +
        "\n" +
        "</ca>\n" +
        "\n" +
        "<cert>\n" +
        "-----BEGIN CERTIFICATE-----\n" +
        "\n" +
        "-----END CERTIFICATE-----\n" +
        "\n" +
        "</cert>\n" +
        "\n" +
        "<key>\n" +
        "-----BEGIN PRIVATE KEY-----\n" +
        "\n" +
        "-----END PRIVATE KEY-----\n" +
        "\n" +
        "</key>"


@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1])
class TestConfigParser {
    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun testHttpProxyPass() {
        val httpproxypass = "<http-proxy-user-pass>\n" +
                "foo\n" +
                "bar\n" +
                "</http-proxy-user-pass>\n"

        val cp = ConfigParser()
        cp.parseConfig(StringReader(miniconfig + httpproxypass))
        val p = cp.convertProfile()
        Assert.assertFalse(p.mCustomConfigOptions.contains(httpproxypass))


    }

    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun cleanReImport() {
        var cp = ConfigParser()
        cp.parseConfig(StringReader(miniconfig + fakeCerts))
        val vp = cp.convertProfile()

        val outConfig = vp.getConfigFile(ApplicationProvider.getApplicationContext(), false)

        cp = ConfigParser()
        cp.parseConfig(StringReader(outConfig))
        val vp2 = cp.convertProfile()

        val outConfig2 = vp2.getConfigFile(ApplicationProvider.getApplicationContext(), false)

        Assert.assertEquals(outConfig, outConfig2)
        Assert.assertFalse(vp.mUseCustomConfig)
        Assert.assertFalse(vp2.mUseCustomConfig)

    }

    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun testCommonOptionsImport() {
        val config = ("client\n"
                + "tun-mtu 1234\n" +
                "<connection>\n" +
                "remote foo.bar\n" +
                "tun-mtu 1222\n" +
                "</connection>\n" +
                "route 8.8.8.8 255.255.255.255 net_gateway\n")

        val cp = ConfigParser()
        cp.parseConfig(StringReader(config))
        val vp = cp.convertProfile()

        Assert.assertEquals(1234, vp.mTunMtu.toLong())
        Assert.assertTrue(vp.mConnections[0].mCustomConfiguration.contains("tun-mtu 1222"))
        Assert.assertTrue(vp.mConnections[0].mUseCustomConfig)
        Assert.assertEquals(vp.mExcludedRoutes.trim(), "8.8.8.8/32");
    }

    @Test
    fun testOneDNSImport()
    {
        val config = "client\n" +
                "tun-mtu 1234\n" +
                "<connection>\n" +
                "remote foo.bar\n" +
                "tun-mtu 1222\n" +
                "</connection>\n" +
                "route 8.8.8.8 255.255.255.255 net_gateway\n" +
                "dhcp-option DNS 1.2.3.4\n"

        val cp = ConfigParser()
        cp.parseConfig(StringReader(config))
        val vp = cp.convertProfile()

        Assert.assertEquals("1.2.3.4", vp.mDNS1)
        Assert.assertEquals("" , vp.mDNS2)
    }

    @Test
    fun testCipherImport() {
        val config = ("client\n"
                + "tun-mtu 1234\n" +
                "<connection>\n" +
                "remote foo.bar\n" +
                "tun-mtu 1222\n" +
                "</connection>\n" +
                "route 8.8.8.8 255.255.255.255 net_gateway\n")


        val config1 = config + "cipher AES-128-GCM\n"

        val cp = ConfigParser()
        cp.parseConfig(StringReader(config1))
        val vp = cp.convertProfile()

        Assert.assertEquals("", vp.mDataCiphers)
        Assert.assertEquals("AES-128-GCM", vp.mCipher)

        val config2 = config + "cipher AES-128-GCM\ndata-ciphers AES-128-GCM:AES-256-GCM:BF-CBC\n"

        cp.parseConfig(StringReader(config2))
        val vp2 = cp.convertProfile()

        Assert.assertEquals("AES-128-GCM:AES-256-GCM:BF-CBC", vp2.mDataCiphers)

        val config3 = config + "cipher AES-128-GCM\n"

        cp.parseConfig(StringReader(config3))
        val vp3 = cp.convertProfile()

        Assert.assertEquals(vp3.mDataCiphers, "")

        val config4 = config + "cipher BF-CBC\nncp-ciphers AES-128-GCM:AES-256-GCM:CHACHA20-POLY1305\n"
        cp.parseConfig(StringReader(config4))
        val vp4 = cp.convertProfile()

        Assert.assertEquals("AES-128-GCM:AES-256-GCM:CHACHA20-POLY1305", vp4.mDataCiphers)



    }


    @Test
    fun testCompatmodeImport() {
        val config = ("client\n"
                + "tun-mtu 1234\n" +
                "<connection>\n" +
                "remote foo.bar\n" +
                "tun-mtu 1222\n" +
                "</connection>\n" +
                "<cert>\nfakecert\n</cert>\n" +
                "<key>\nfakekey\n</key>\n" +
                "route 8.8.8.8 255.255.255.255 net_gateway\n")
        val c:Context = ApplicationProvider.getApplicationContext()

        val config1 = config + "compat-mode 2.7.7\n"

        val cp = ConfigParser()
        cp.parseConfig(StringReader(config1))
        val vp = cp.convertProfile()

        Assert.assertEquals(20707, vp.mCompatMode)


        val config2 = config + "compat-mode 2.4.0\n"


        cp.parseConfig(StringReader(config2))
        val vp2 = cp.convertProfile()
        Assert.assertEquals(20400, vp2.mCompatMode)
        val conf2 = vp2.getConfigFile(c, false)
        Assert.assertTrue(conf2.contains("compat-mode 2.4.0"));

        val config3 = config + "compat-mode 1.17.23\n";
        cp.parseConfig(StringReader(config3))
        val vp3 = cp.convertProfile()
        Assert.assertEquals(11723, vp3.mCompatMode)

        val conf = vp3.getConfigFile(c, false)
        Assert.assertTrue(conf.contains("compat-mode 1.17.23"))
    }

    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun testSockProxyImport() {
        val proxy = "ca baz\n" +
                "key foo\n" +
                "cert bar\n" +
                "client\n" +
                "<connection>\n" +
                "socks-proxy 13.23.3.2\n" +
                "remote foo.bar\n" +
                "</connection>\n" +
                "\n" +
                "<connection>\n" +
                "socks-proxy 1.2.3.4 1234\n" +
                "remote foo.bar\n" +
                "</connection>\n" +
                "\n" +
                "<connection>\n" +
                "http-proxy 1.2.3.7 8080\n" +
                "remote foo.bar\n" +
                "</connection>"

        val cp = ConfigParser()
        cp.parseConfig(StringReader(proxy))
        val vp = cp.convertProfile()
        Assert.assertEquals(3, vp.mConnections.size.toLong())

        Assert.assertEquals("13.23.3.2", vp.mConnections[0].mProxyName)
        Assert.assertEquals("1080", vp.mConnections[0].mProxyPort)
        Assert.assertEquals(Connection.ProxyType.SOCKS5, vp.mConnections[0].mProxyType)

        Assert.assertEquals("1.2.3.4", vp.mConnections[1].mProxyName)
        Assert.assertEquals("1234", vp.mConnections[1].mProxyPort)
        Assert.assertEquals(Connection.ProxyType.SOCKS5, vp.mConnections[0].mProxyType)

        Assert.assertEquals("1.2.3.7", vp.mConnections[2].mProxyName)
        Assert.assertEquals("8080", vp.mConnections[2].mProxyPort)
        Assert.assertEquals(Connection.ProxyType.HTTP, vp.mConnections[2].mProxyType)

        val c:Context = ApplicationProvider.getApplicationContext()
        val err = vp.checkProfile(c, false)
        Assert.assertTrue("Failed with " + c.getString(err), err == R.string.no_error_found)
    }

    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun testHttpUserPassAuth() {
        val proxy = "client\n" +
                "dev tun\n" +
                "proto tcp\n" +
                "remote 1.2.3.4 443\n" +
                "resolv-retry infinite\n" +
                "nobind\n" +
                "persist-key\n" +
                "persist-tun\n" +
                "auth-user-pass\n" +
                "verb 3\n" +
                "cipher AES-128-CBC\n" +
                "pull\n" +
                "route-delay 2\n" +
                "redirect-gateway\n" +
                "remote-cert-tls server\n" +
                "ns-cert-type server\n" +
                "comp-lzo no\n" +
                "http-proxy 1.2.3.4 1234\n" +
                "<http-proxy-user-pass>\n" +
                "username12\n" +
                "password34\n" +
                "</http-proxy-user-pass>\n" +
                "<ca>\n" +
                "foo\n" +
                "</ca>\n" +
                "<cert>\n" +
                "bar\n" +
                "</cert>\n" +
                "<key>\n" +
                "baz\n" +
                "</key>\n"
        val cp = ConfigParser()
        cp.parseConfig(StringReader(proxy))
        val vp = cp.convertProfile()
        var config = vp.getConfigFile(ApplicationProvider.getApplicationContext(), true)
        Assert.assertTrue(config.contains("username12"))
        Assert.assertTrue(config.contains("http-proxy 1.2.3.4"))

        config = vp.getConfigFile(ApplicationProvider.getApplicationContext(), false)

        Assert.assertFalse(config.contains("username12"))
        Assert.assertFalse(config.contains("http-proxy 1.2.3.4"))

        Assert.assertTrue(vp.mConnections[0].mUseProxyAuth)
        Assert.assertEquals(vp.mConnections[0].mProxyAuthUser, "username12")
        Assert.assertEquals(vp.mConnections[0].mProxyAuthPassword, "password34")
    }

    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun testConfigWithHttpProxyOptions() {
        val proxyconf = "pull\n" +
                "dev tun\n" +
                "proto tcp-client\n" +
                "cipher AES-128-CBC\n" +
                "auth SHA1\n" +
                "reneg-sec 0\n" +
                "remote-cert-tls server\n" +
                "tls-version-min 1.2 or-highest\n" +
                "persist-tun\n" +
                "nobind\n" +
                "connect-retry 2 2\n" +
                "dhcp-option DNS 1.1.1.1\n" +
                "dhcp-option DNS 84.200.69.80\n" +
                "auth-user-pass\n" +
                "\n" +
                "remote xx.xx.xx.xx 1194\n" +
                "http-proxy 1.2.3.4 8080\n" +
                "http-proxy-option VERSION 1.1\n" +
                "http-proxy-option CUSTOM-HEADER \"Connection: Upgrade\"\n" +
                "http-proxy-option CUSTOM-HEADER \"X-Forwarded-Proto: https\"\n" +
                "http-proxy-option CUSTOM-HEADER \"Upgrade-Insecure-Requests: 1\"\n" +
                "http-proxy-option CUSTOM-HEADER \"DNT: 1\"\n" +
                "http-proxy-option CUSTOM-HEADER \"Tk: N\"\n" +
                "\n" +
                fakeCerts

        val cp = ConfigParser()
        cp.parseConfig(StringReader(proxyconf))
        val vp = cp.convertProfile()

        Assert.assertEquals(vp.checkProfile(ApplicationProvider.getApplicationContext(), true).toLong(), R.string.no_error_found.toLong())
        Assert.assertEquals(vp.checkProfile(ApplicationProvider.getApplicationContext(), false).toLong(), R.string.no_error_found.toLong())

        val config = vp.getConfigFile(ApplicationProvider.getApplicationContext(), false)

        Assert.assertTrue(config.contains("http-proxy 1.2.3.4"))
        Assert.assertFalse(config.contains("management-query-proxy"))


        Assert.assertTrue(config.contains("http-proxy-option CUSTOM-HEADER"))

        vp.mConnections = Arrays.copyOf(vp.mConnections, vp.mConnections.size + 1)
        vp.mConnections[vp.mConnections.size - 1] = Connection()

        vp.mConnections[vp.mConnections.size - 1].mProxyType = Connection.ProxyType.ORBOT

        Assert.assertEquals(vp.checkProfile(ApplicationProvider.getApplicationContext(), false).toLong(), R.string.error_orbot_and_proxy_options.toLong())

    }


    @Test
    fun testTlscryptV2Import()
    {
        val conf = """<ca>
Here
</ca>

<cert>
no
</cert>

cipher AES-256-GCM
client
compress
dev-type tun
explicit-exit-notify
<key>
useful
</key>
nobind
persist-key
persist-tun
remote home.evil.cloud 65443 udp
remote home.evil.cloud 65443 tcp-client
remote-cert-tls server
<tls-crypt-v2>
content
</tls-crypt-v2>
topology subnet
verb 4
verify-x509-name homevpn.evil.cloud name

""";
        val cp = ConfigParser()
        cp.parseConfig(StringReader(conf))

        val vp = cp.convertProfile()

        val config = vp.getConfigFile(ApplicationProvider.getApplicationContext(), true)

        Assert.assertEquals("tls-crypt-v2", vp.mTLSAuthDirection)

        Assert.assertFalse(config.contains("key-direction"))
    }

    @Test
    @Throws(IOException::class, ConfigParser.ConfigParseError::class)
    fun testPeerFingerprint() {
        val conf = """
<cert>
dummy
</cert>
cipher AES-256-GCM
client
dev-type tun
<key>
dummykey
</key>
remote home.evil.cloud 65443 udp
""";
        val fps = """
    28:45:c7:ad:6a:c4:83:c7:a0:0a:0a:91:4b:43:e3:09:79:05:a2:ce:c2:e2:5e:c9:70:5a:2b:a4:e1:0f:97:e3
    F8:FA:6D:CF:58:65:98:5F:E0:E7:2A:B4:25:ED:2C:DD:45:7B:21:C1:B7:46:1D:46:C3:2B:1D:1D:F7:0E:43:51
    ef:5c:fc:a4:d5:59:78:14:e0:87:66:0b:53:df:e5:1e:a1:39:e0:1f:7a:ca:ca:87:4e:78:8b:45:c7:3d:af:c7
    """.trimIndent()
        val fpBlock = "<peer-fingerprint>\n${fps}\n</peer-fingerprint>"

        val fpSingle = "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff"
        val fpSingleCmd = "peer-fingerprint ${fpSingle}\n"

        val cp = ConfigParser()
        cp.parseConfig(StringReader(conf + fpBlock))
        val vp = cp.convertProfile()

        Assert.assertTrue(vp.mCheckPeerFingerprint)
        Assert.assertEquals(fps.trim(), vp.mPeerFingerPrints.trim())

        cp.parseConfig(StringReader(conf + fpBlock + "\n" + fpSingleCmd))
        val vp2 = cp.convertProfile()
        Assert.assertTrue(vp2.mCheckPeerFingerprint)
        Assert.assertEquals((fps + "\n" + fpSingle).trim(), vp2.mPeerFingerPrints.trim())


    }

}