From e544282169db8b8e90867bbb087133621bde35ca Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 21 Aug 2022 17:21:46 +0200 Subject: Log exit reason on start in log This should help finding out why the process suddenly dies. --- .../de/blinkt/openvpn/core/TestLogFileHandler.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'main/src/test/java/de/blinkt/openvpn/core') diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java b/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java index b32e5efa..e024bf90 100644 --- a/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java +++ b/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java @@ -80,6 +80,27 @@ public class TestLogFileHandler { Assert.assertEquals(li, li2); } + @Test + public void testLogInsertByTime() + { + VpnStatus vpnStatus = new VpnStatus(); + /* Add the generic information message */ + VpnStatus.clearLog(); + + long[] testTimes = {1000, 20000, 1500, 500, 6000, 70000, System.currentTimeMillis()+5000}; + for (long time: testTimes) { + LogItem li = new LogItem(VpnStatus.LogLevel.INFO, "unit test", time); + VpnStatus.newLogItemIfUnique(li); + } + + long lastlogTime = 0; + for(LogItem li:VpnStatus.getlogbuffer()) + { + org.junit.Assert.assertTrue(li.getLogtime() >= lastlogTime); + lastlogTime = li.getLogtime(); + } + } + private void testEquals(LogItem li, LogItem li2) { Assert.assertEquals(li.getLogLevel(), li2.getLogLevel()); @@ -120,5 +141,4 @@ public class TestLogFileHandler { } } - } \ No newline at end of file -- cgit v1.2.3 From 42b2c8ee062318bffd059a810033b615164cfb48 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sat, 26 Aug 2023 18:23:02 +0200 Subject: Fix unit test building and running --- main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'main/src/test/java/de/blinkt/openvpn/core') diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.kt b/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.kt index 53bfd00a..fcc27878 100644 --- a/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.kt +++ b/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.kt @@ -252,7 +252,7 @@ class TestConfigParser { val c:Context = ApplicationProvider.getApplicationContext() val err = vp.checkProfile(c, false) - Assert.assertTrue("Failed with " + c.getString(err), err == R.string.no_error_found) + Assert.assertTrue("Failed with wrong error code: $err", err == R.string.no_error_found) } @Test -- cgit v1.2.3 From 2c66e7086c50d78caadc080313d3d6e4605de95e Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 9 Oct 2023 19:32:09 +0200 Subject: Remove also app restriction profiles on missing vpn profile list and add tests --- .../de/blinkt/openvpn/core/TestRestrictions.kt | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt (limited to 'main/src/test/java/de/blinkt/openvpn/core') 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 = 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 = 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" + ) + + } +} -- cgit v1.2.3