summaryrefslogtreecommitdiff
path: root/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt
blob: 75505655e4795aff1c6e0031a93cc25faa3b7dcb (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
/*
 * Copyright (c) 2012-2023 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.Bundle
import androidx.test.core.app.ApplicationProvider
import de.blinkt.openvpn.VpnProfile
import de.blinkt.openvpn.api.AppRestrictions
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

interface createTestBundle

@RunWith(RobolectricTestRunner::class)
class TestRestrictions : createTestBundle {
    @Test
    fun testImportRestrictions() {

        val context: Context = ApplicationProvider.getApplicationContext();

        val appr = AppRestrictions.getInstance(context);
        val b: Bundle = createTestBundle()


        appr.parseRestrictionsBundle(context, b);

        val pm = ProfileManager.getInstance(context);

        Assert.assertEquals(pm.profiles.size, 1)

        val firstProfile: VpnProfile = pm.profiles.first()

        Assert.assertEquals(
            firstProfile.uuidString,
            "F8AEE125-2D7A-44E9-B9EB-82FB619D51CC".lowercase()
        )
        Assert.assertEquals(
            firstProfile.importedProfileHash,
            "4098294f8a8d25bb6e85cef5672dfba13ed63719"
        )

        /* Try to remove the imported profiles again */
    }

    private fun createTestBundle(): Bundle {
        val b: Bundle = Bundle();

        val miniconfig = "client\nremote test.blinkt.de\n";

        val testVPN = Bundle();
        testVPN.putString("name", "Unit Test VPN");
        testVPN.putString("ovpn", miniconfig)
        testVPN.putString("uuid", "F8AEE125-2D7A-44E9-B9EB-82FB619D51CC");

        val ba: Array<Bundle> = arrayOf(testVPN)

        b.putParcelableArray("vpn_configuration_list", ba)
        b.putString("defaultprofile", "F8AEE125-2D7A-44E9-B9EB-82FB619D51CC")
        b.putString("version", "1")
        b.putString("allowed_remote_access", "some.random.app")
        return b
    }

    private fun createTestBundleEmptyVPN(): Bundle {
        val b: Bundle = Bundle();
        val ba: Array<Bundle> = arrayOf()

        b.putParcelableArray("vpn_configuration_list", ba)
        b.putString("version", "1")
        b.putString("allowed_remote_access", "some.random.app");
        return b
    }

    @Test
    fun testImportRestrictionsDelete() {

        val context: Context = ApplicationProvider.getApplicationContext();

        val appr = AppRestrictions.getInstance(context);
        val b: Bundle = createTestBundle()

        appr.parseRestrictionsBundle(context, b)

        /* add another not restriction managed profile */
        val otherVP: VpnProfile = VpnProfile("another")

        val pm = ProfileManager.getInstance(context)
        pm.addProfile(otherVP)

        Assert.assertEquals(pm.profiles.size, 2)

        val bEmpty: Bundle = createTestBundleEmptyVPN()
        appr.parseRestrictionsBundle(context, bEmpty)

        Assert.assertEquals(pm.profiles.size, 1)

        val firstProfile: VpnProfile = pm.profiles.first()

        Assert.assertEquals(
            firstProfile.name,
            "another"
        )

    }

    @Test
    fun testImportRestrictionsDeleteEmptyProfileList() {

        val context: Context = ApplicationProvider.getApplicationContext();

        val appr = AppRestrictions.getInstance(context);
        val b: Bundle = createTestBundle()

        appr.parseRestrictionsBundle(context, b)

        /* add another not restriction managed profile */
        val otherVP: VpnProfile = VpnProfile("another")

        val pm = ProfileManager.getInstance(context)
        pm.addProfile(otherVP)

        Assert.assertEquals(pm.profiles.size, 2)

        val bNoVPNConfigList: Bundle = Bundle();

        bNoVPNConfigList.putString("version", "1")


        appr.parseRestrictionsBundle(context, bNoVPNConfigList)

        Assert.assertEquals(pm.profiles.size, 1)

        val firstProfile: VpnProfile = pm.profiles.first()

        Assert.assertEquals(
            firstProfile.name,
            "another"
        )

    }
}