diff options
| author | Arne Schwabe <arne@rfc2549.org> | 2023-10-09 19:32:09 +0200 | 
|---|---|---|
| committer | Arne Schwabe <arne@rfc2549.org> | 2023-10-09 19:32:09 +0200 | 
| commit | 2c66e7086c50d78caadc080313d3d6e4605de95e (patch) | |
| tree | fffdb65804b00509ec1ef08bb334e080de568804 | |
| parent | 173f36fe513b644a71ed16f5778d190eb9ddd905 (diff) | |
Remove also app restriction profiles on missing vpn profile list and add tests
| -rw-r--r-- | main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java | 12 | ||||
| -rw-r--r-- | main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt | 146 | 
2 files changed, 154 insertions, 4 deletions
| diff --git a/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java b/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java index caa2de32..53919216 100644 --- a/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java +++ b/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java @@ -36,13 +36,13 @@ public class AppRestrictions {      private static AppRestrictions mInstance;      private BroadcastReceiver mRestrictionsReceiver; -    private AppRestrictions(Context c) { +    private AppRestrictions() {      }      public static AppRestrictions getInstance(Context c) {          if (mInstance == null) -            mInstance = new AppRestrictions(c); +            mInstance = new AppRestrictions();          return mInstance;      } @@ -81,6 +81,10 @@ public class AppRestrictions {          if (restrictionsMgr == null)              return;          Bundle restrictions = restrictionsMgr.getApplicationRestrictions(); +        parseRestrictionsBundle(c, restrictions); +    } +    public void parseRestrictionsBundle(Context c, Bundle restrictions) +    {          if (restrictions == null)              return; @@ -97,8 +101,8 @@ public class AppRestrictions {          }          Parcelable[] profileList = restrictions.getParcelableArray("vpn_configuration_list");          if (profileList == null) { -            VpnStatus.logError("App restriction does not contain a profile list (vpn_configuration_list)"); -            return; +            VpnStatus.logInfo("App restriction does not contain a profile list. Removing previously added profiles. (vpn_configuration_list)"); +            profileList = new Parcelable[]{};          }          importVPNProfiles(c, restrictions, profileList); diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt b/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt new file mode 100644 index 00000000..75505655 --- /dev/null +++ b/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt @@ -0,0 +1,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" +        ) + +    } +} | 
