From af2709bcf95654b944cac2f94975a10f8037a53f Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 4 Apr 2018 14:32:18 +0200 Subject: Improve unit tests, fix orbot warning (closes #860) --- build.gradle | 3 +- gradle/wrapper/gradle-wrapper.properties | 2 +- main/src/main/cpp/opvpnutil/jniglue.c | 2 +- .../main/java/de/blinkt/openvpn/VpnProfile.java | 19 +++++-- .../blinkt/openvpn/core/ICSOpenVPNApplication.java | 4 +- .../java/de/blinkt/openvpn/core/NativeUtils.java | 22 ++++++-- .../blinkt/openvpn/core/TestConfigGenerator.java | 3 ++ .../de/blinkt/openvpn/core/TestConfigParser.java | 59 +++++++++++++++++++++- 8 files changed, 100 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index d170fb01..364d22e6 100644 --- a/build.gradle +++ b/build.gradle @@ -9,9 +9,10 @@ buildscript { url 'https://maven.google.com' } jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.1.0' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 68843020..733dc00a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-all.zip diff --git a/main/src/main/cpp/opvpnutil/jniglue.c b/main/src/main/cpp/opvpnutil/jniglue.c index bb5ec688..b0c6c704 100644 --- a/main/src/main/cpp/opvpnutil/jniglue.c +++ b/main/src/main/cpp/opvpnutil/jniglue.c @@ -26,7 +26,7 @@ void Java_de_blinkt_openvpn_core_NativeUtils_jniclose(JNIEnv *env,jclass jo, jin //! Hack to get the current installed ABI of the libraries. See also https://github.com/schwabe/ics-openvpn/issues/391 -jstring Java_de_blinkt_openvpn_core_NativeUtils_getNativeAPI(JNIEnv *env, jclass jo) +jstring Java_de_blinkt_openvpn_core_NativeUtils_getJNIAPI(JNIEnv *env, jclass jo) { return (*env)->NewStringUTF(env, TARGET_ARCH_ABI); diff --git a/main/src/main/java/de/blinkt/openvpn/VpnProfile.java b/main/src/main/java/de/blinkt/openvpn/VpnProfile.java index 838604e2..fcf3c20c 100644 --- a/main/src/main/java/de/blinkt/openvpn/VpnProfile.java +++ b/main/src/main/java/de/blinkt/openvpn/VpnProfile.java @@ -912,8 +912,13 @@ public class VpnProfile implements Serializable, Cloneable { } + public int checkProfile(Context c) + { + return checkProfile(c, doUseOpenVPN3(c)); + } + //! Return an error if something is wrong - public int checkProfile(Context context) { + public int checkProfile(Context context, boolean useOpenVPN3) { if (mAuthenticationType == TYPE_KEYSTORE || mAuthenticationType == TYPE_USERPASS_KEYSTORE) { if (mAlias == null) return R.string.no_keystore_cert_selected; @@ -951,14 +956,15 @@ public class VpnProfile implements Serializable, Cloneable { boolean noRemoteEnabled = true; - for (Connection c : mConnections) + for (Connection c : mConnections) { if (c.mEnabled) noRemoteEnabled = false; + } if (noRemoteEnabled) return R.string.remote_no_server_selected; - if (doUseOpenVPN3(context)) { + if (useOpenVPN3) { if (mAuthenticationType == TYPE_STATICKEYS) { return R.string.openvpn3_nostatickeys; } @@ -970,8 +976,11 @@ public class VpnProfile implements Serializable, Cloneable { return R.string.openvpn3_socksproxy; } } - if (!OrbotHelper.checkTorReceier(context)) - return R.string.no_orbotfound; + for (Connection c: mConnections) { + if (c.mProxyType == Connection.ProxyType.ORBOT) + if (!OrbotHelper.checkTorReceier(context)) + return R.string.no_orbotfound; + } // Everything okay diff --git a/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java b/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java index 461b2660..53847cbf 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ICSOpenVPNApplication.java @@ -26,11 +26,13 @@ public class ICSOpenVPNApplication extends Application { @Override public void onCreate() { + if("robolectric".equals(Build.FINGERPRINT)) + return; + super.onCreate(); PRNGFixes.apply(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - createNotificationChannels(); mStatus = new StatusListener(); mStatus.init(getApplicationContext()); diff --git a/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java b/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java index 70c7455a..6b633c34 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java +++ b/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java @@ -16,7 +16,15 @@ public class NativeUtils { static native void jniclose(int fdint); - public static native String getNativeAPI(); + public static String getNativeAPI() + { + if (isRoboUnitTest()) + return "ROBO"; + else + return getJNIAPI(); + } + + private static native String getJNIAPI(); public final static int[] openSSLlengths = { @@ -26,8 +34,14 @@ public class NativeUtils { public static native double[] getOpenSSLSpeed(String algorithm, int testnum); static { - System.loadLibrary("opvpnutil"); - if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) - System.loadLibrary("jbcrypto"); + if (!isRoboUnitTest()) { + System.loadLibrary("opvpnutil"); + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) + System.loadLibrary("jbcrypto"); + } + } + + public static boolean isRoboUnitTest() { + return "robolectric".equals(Build.FINGERPRINT); } } diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestConfigGenerator.java b/main/src/test/java/de/blinkt/openvpn/core/TestConfigGenerator.java index 66e69ca5..4fb14d2c 100644 --- a/main/src/test/java/de/blinkt/openvpn/core/TestConfigGenerator.java +++ b/main/src/test/java/de/blinkt/openvpn/core/TestConfigGenerator.java @@ -60,6 +60,9 @@ public class TestConfigGenerator { vp.mAuthRetry = AUTH_RETRY_NOINTERACT; String config = vp.getConfigFile(RuntimeEnvironment.application, false); Assert.assertTrue(config.contains("\nauth-retry nointeract\n")); + for (Connection connection: vp.mConnections) + Assert.assertTrue(connection.mProxyType == Connection.ProxyType.NONE); } + } diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java b/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java index 5793676f..6615a8f0 100644 --- a/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java +++ b/main/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java @@ -5,18 +5,27 @@ package de.blinkt.openvpn.core; +import android.app.Application; +import android.content.Context; +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.RuntimeEnvironment; import java.io.IOException; import java.io.StringReader; import de.blinkt.openvpn.VpnProfile; +import org.robolectric.annotation.Config; /** * Created by arne on 03.10.16. */ +@Config(manifest= "src/main/AndroidManifest.xml") +@RunWith(RobolectricTestRunner.class) public class TestConfigParser { String miniconfig = "client\nremote test.blinkt.de\n"; @@ -38,7 +47,11 @@ public class TestConfigParser { @Test public void testSockProxyImport() throws IOException, ConfigParser.ConfigParseError { String proxy = - "\n" + + "ca baz\n" + + "key foo\n" + + "cert bar\n" + + "client\n" + + "\n" + "socks-proxy 13.23.3.2\n" + "remote foo.bar\n" + "\n" + @@ -69,7 +82,51 @@ public class TestConfigParser { 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); + + Context c = RuntimeEnvironment.application; + int err = vp.checkProfile(c, false); + Assert.assertTrue("Failed with " + c.getString(err), err == R.string.no_error_found); } + @Test + public void testHttpUserPassAuth() throws IOException, ConfigParser.ConfigParseError { + String 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" + + "\n" + + "username12\n" + + "password34\n" + + "\n" + + "\n" + + "foo\n" + + "\n" + + "\n" + + "bar\n" + + "\n" + + "\n" + + "baz\n" + + "\n"; + ConfigParser cp = new ConfigParser(); + cp.parseConfig(new StringReader(proxy)); + VpnProfile vp = cp.convertProfile(); + String config = vp.getConfigFile(RuntimeEnvironment.application, false); + Assert.assertTrue(config.contains("username12")); + Assert.assertTrue(config.contains("http-proxy 1.2.3.4")); + } } -- cgit v1.2.3