Skip to content

Commit

Permalink
Remove also app restriction profiles on missing vpn profile list and …
Browse files Browse the repository at this point in the history
…add tests
  • Loading branch information
schwabe committed Oct 9, 2023
1 parent 173f36f commit 2c66e70
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 4 deletions.
12 changes: 8 additions & 4 deletions main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -81,6 +81,10 @@ private void applyRestrictions(Context c) {
if (restrictionsMgr == null)
return;
Bundle restrictions = restrictionsMgr.getApplicationRestrictions();
parseRestrictionsBundle(c, restrictions);
}
public void parseRestrictionsBundle(Context c, Bundle restrictions)
{
if (restrictions == null)
return;

Expand All @@ -97,8 +101,8 @@ private void applyRestrictions(Context c) {
}
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);
Expand Down
146 changes: 146 additions & 0 deletions main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt
Original file line number Diff line number Diff line change
@@ -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"
)

}
}

0 comments on commit 2c66e70

Please sign in to comment.