summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDave Tharp <dtharp@codeaurora.org>2015-04-02 13:27:52 -0700
committerWebTech Code Review <code-review@localhost>2015-04-08 10:12:34 -0700
commit2abd631dfab16347c3c1b3a9cc7905310136d969 (patch)
tree906b5c7d941583dd20f2d17b055ee53e76921a8f /src
parentcbbeab5e3f2a9d41ffa7a2be4cb9eddfc392fcd6 (diff)
downloadandroid_packages_apps_Gello-2abd631dfab16347c3c1b3a9cc7905310136d969.tar.gz
android_packages_apps_Gello-2abd631dfab16347c3c1b3a9cc7905310136d969.tar.bz2
android_packages_apps_Gello-2abd631dfab16347c3c1b3a9cc7905310136d969.zip
Adding unit tests for MDM Proxy Restriction
Unit tests to test various proxy modes' interaction with ProxyChangeListener. Also including fixes to bugs revealed by unit testing. These tests use the Bundle communications mechanism defined by MDM, but do not actually send data through the RestrictionsManager. This is enough for unit testing since involving the RestrictionsManager would be more of an integration/systems test. Change-Id: Ia5e12ccb1997594673d954dfbe878c0f4846b14a
Diffstat (limited to 'src')
-rw-r--r--src/com/android/browser/mdm/ProxyRestriction.java12
-rw-r--r--src/com/android/browser/mdm/tests/ProxyRestrictionsTest.java286
2 files changed, 292 insertions, 6 deletions
diff --git a/src/com/android/browser/mdm/ProxyRestriction.java b/src/com/android/browser/mdm/ProxyRestriction.java
index be082543..a252b3d3 100644
--- a/src/com/android/browser/mdm/ProxyRestriction.java
+++ b/src/com/android/browser/mdm/ProxyRestriction.java
@@ -89,10 +89,10 @@ public class ProxyRestriction extends Restriction implements PreferenceKeys {
// If you choose to use system proxy settings or auto detect the proxy server,
// all other options are ignored.
else if (proxyMode.equals(ProxyChangeListener.MODE_SYSTEM) ||
- proxyMode.equals(ProxyChangeListener.MODE_AUTO_DETECT)) {
- //TODO: Disable for now. Needs more investigation.
- Log.v(TAG, "enforce: proxyMode is [" + proxyMode.toString() + "]. Not supported. disabling.");
- enable(false);
+ proxyMode.equals(ProxyChangeListener.MODE_AUTO_DETECT)) {
+ // TODO We will go ahead and configure here, but will throttle the enable in ProxyModeListener
+ enable(true);
+ ProxyChangeListener.setMdmProxy(proxyMode, null);
}
// If you choose fixed server proxy mode, you can specify further options in 'Address or URL
@@ -120,7 +120,7 @@ public class ProxyRestriction extends Restriction implements PreferenceKeys {
Log.v(TAG,"enforce: saving MODE_FIXED_SERVERS proxy config: ");
Log.v(TAG," - host : " + host.toString());
Log.v(TAG," - port : " + port);
-// Log.v(TAG," - bypassList : " + proxyBypassList != null ? proxyBypassList.toString() : "NULL");
+ Log.v(TAG," - bypassList : " + (proxyBypassList != null ? proxyBypassList.toString() : "NULL"));
saveProxyConfig(proxyMode, host, port, null, proxyBypassList);
}
@@ -137,7 +137,7 @@ public class ProxyRestriction extends Restriction implements PreferenceKeys {
} else {
Log.v(TAG, "enforce: MODE_PAC_SCRIPT. proxyPacUrl ["+proxyPacUrl.toString() +
"]. sending and enabling");
- saveProxyConfig(proxyMode, null, -1, null, proxyPacUrl);
+ saveProxyConfig(proxyMode, null, -1, proxyPacUrl, null);
}
}
}
diff --git a/src/com/android/browser/mdm/tests/ProxyRestrictionsTest.java b/src/com/android/browser/mdm/tests/ProxyRestrictionsTest.java
new file mode 100644
index 00000000..211c6b42
--- /dev/null
+++ b/src/com/android/browser/mdm/tests/ProxyRestrictionsTest.java
@@ -0,0 +1,286 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+package com.android.browser.mdm.tests;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.os.Bundle;
+import android.test.ActivityInstrumentationTestCase2;
+import android.util.Log;
+
+import com.android.browser.BrowserActivity;
+import com.android.browser.PreferenceKeys;
+import com.android.browser.mdm.ManagedProfileManager;
+import com.android.browser.mdm.ProxyRestriction;
+
+import org.chromium.net.ProxyChangeListener;
+
+public class ProxyRestrictionsTest extends ActivityInstrumentationTestCase2<BrowserActivity>
+ implements PreferenceKeys {
+
+ private final static String TAG = "ProxyRestrictionsTest";
+
+ private Instrumentation mInstrumentation;
+ private Context mContext;
+ private BrowserActivity mActivity;
+ private ProxyRestriction mProxyRestriction;
+
+
+ public ProxyRestrictionsTest() {
+ super(BrowserActivity.class);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mInstrumentation = getInstrumentation();
+ mContext = getInstrumentation().getTargetContext();
+ mActivity = getActivity();
+ mProxyRestriction = ProxyRestriction.getInstance();
+ }
+
+ /*
+ * Proxy Restrictions Tests
+ */
+ // Properties we can query ProxyChangeListener for:
+ // http.proxyHost , https.proxyHost
+ // http.proxyPort , https.proxyPort
+ // http.nonProxyHosts , https.nonProxyHosts
+ // ProxyServer (ProxyChangeListener.PROXY_SERVER)
+ // ProxyPacUrl (ProxyChangeListener.PROXY_PAC_URL)
+ // ProxyBypassList (ProxyChangeListener.PROXY_BYPASS_LIST)
+
+ // TODO: refactor this into a toString() on ProxyConfig
+ public void logProxyConfig (ProxyChangeListener.ProxyConfig pc) {
+ if (pc != null) {
+ Log.v(TAG, "ProxyConfig Dump:");
+ Log.v(TAG, " mHost = " + (pc.mHost != null ? pc.mHost : "NULL"));
+ Log.v(TAG, " mPort = " + pc.mPort);
+ Log.v(TAG, " mPacUrl = " + (pc.mPacUrl != null ? pc.mPacUrl : "NULL"));
+ //Log.v(TAG, " mExclusionList = " + pc.mExclusionList);
+ } else {
+ Log.v(TAG, "ProxyConfig Dump: pc was NULL");
+ }
+ }
+
+ // Ensure we start mode and proxyConfig as null
+ public void testPreconditions() throws Throwable {
+ Log.v(TAG, "== Init Conditions ==");
+
+ // check configured proxy mode
+ assertNull("Init: mode should be null", ProxyChangeListener.getmMdmProxyMode());
+
+ // get the proxy config from ProxyChangeListener
+ assertNull("Init: proxyConfig should be null", ProxyChangeListener.getMdmProxyConfig());
+ }
+
+ // If you choose to never use a proxy server and always connect directly,
+ // all other options are ignored.
+ public void testProxy_ModeDirect() throws Throwable {
+ String mode = ProxyChangeListener.MODE_DIRECT;
+ Log.v(TAG, "== Testing " + mode + " ==");
+
+ // set the restrictions
+ setProxyRestrictions(mode, null, null, null);
+
+ // check configured proxy mode
+ String configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertEquals(mode + ": configuration", mode, configuredMode);
+
+ // get the proxy config from ProxyChangeListener
+ ProxyChangeListener.ProxyConfig pc = ProxyChangeListener.getMdmProxyConfig();
+ assertNull(mode +": proxyConfig should be null", pc);
+ }
+
+ // If you choose to use system proxy settings or auto detect the proxy server,
+ // all other options are ignored.
+ public void testProxy_ModeSystem() throws Throwable {
+ String mode = ProxyChangeListener.MODE_SYSTEM;
+ Log.v(TAG, "== Testing " + mode + " ==");
+
+ // Clear any restrictions
+ setProxyRestrictions(ProxyChangeListener.MODE_DIRECT, null, null, null);
+
+ // set the restrictions
+ setProxyRestrictions(mode, null, null, null);
+
+ // check configured proxy mode
+ String configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertNotNull(configuredMode);
+ assertEquals(mode + ": configuration",mode,configuredMode);
+
+ // get the proxy config from ProxyChangeListener
+ ProxyChangeListener.ProxyConfig pc = ProxyChangeListener.getMdmProxyConfig();
+ assertNull(mode +": proxyConfig should be null", pc);
+ }
+
+ // If you choose to use system proxy settings or auto detect the proxy server,
+ // all other options are ignored.
+ public void testProxy_ModeAutoDetect() throws Throwable {
+ String mode = ProxyChangeListener.MODE_AUTO_DETECT;
+ Log.v(TAG, "== Testing " + mode + " ==");
+
+ // Clear any restrictions
+ setProxyRestrictions(ProxyChangeListener.MODE_DIRECT, null, null, null);
+
+ // set the restrictions
+ setProxyRestrictions(mode, null, null, null);
+
+ // check configured proxy mode
+ String configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertNotNull(configuredMode);
+ assertEquals(mode + ": configuration",mode,configuredMode);
+
+ // get the proxy config from ProxyChangeListener
+ ProxyChangeListener.ProxyConfig pc = ProxyChangeListener.getMdmProxyConfig();
+ assertNull(mode +": proxyConfig should be null", pc);
+ }
+
+ // If you choose fixed server proxy mode, you can specify further options in
+ // 'Address or URL of proxy server' and 'Comma-separated list of proxy bypass rules'.
+ public void testProxy_ModeFixedServers() throws Throwable {
+ String mode = ProxyChangeListener.MODE_FIXED_SERVERS;
+ Log.v(TAG, "== Testing " + mode + " ==");
+
+ String proxyHost = "192.241.207.220";
+ String proxyPort = "9090";
+ String proxyServer = "http://" + proxyHost + ":" + proxyPort;
+ String configuredMode;
+
+ // Clear any restrictions
+ setProxyRestrictions(ProxyChangeListener.MODE_DIRECT, null, null, null);
+
+ // Test that mode didn't get set if no proxy server is set
+ setProxyRestrictions(mode, null, null, null);
+ configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertNull(configuredMode);
+
+ //
+ // set the restrictions without Exclusion List
+ //
+ setProxyRestrictions(mode, proxyServer, null, null);
+
+ // check configured proxy mode
+ configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertNotNull(configuredMode);
+ assertEquals(mode + ": configuration",mode,configuredMode);
+
+ logProxyConfig(ProxyChangeListener.getMdmProxyConfig());
+
+ // check properties
+ assertEquals(ProxyChangeListener.getProperty("http.proxyHost"), proxyHost);
+ assertEquals(ProxyChangeListener.getProperty("http.proxyPort"), proxyPort);
+
+ assertNull(ProxyChangeListener.getProperty("http.nonProxyHosts"));
+
+ //
+ // set the restrictions with Exclusion list
+ //
+ setProxyRestrictions(mode, proxyServer, "*.google.com, *foo.com, 127.0.0.1:8080", null);
+
+ // check configured proxy mode
+ configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertNotNull(configuredMode);
+ assertEquals(mode + ": configuration",mode,configuredMode);
+
+ logProxyConfig(ProxyChangeListener.getMdmProxyConfig());
+
+ // check properties
+ assertEquals(ProxyChangeListener.getProperty("http.proxyHost"), proxyHost);
+ assertEquals(ProxyChangeListener.getProperty("http.proxyPort"), proxyPort);
+
+ String expected = "*.google.com|*foo.com|127.0.0.1:8080";
+ assertEquals(ProxyChangeListener.getProperty("http.nonProxyHosts"), expected);
+ }
+
+ // If you choose to use a .pac proxy script, you must specify the URL to the
+ // script in 'URL to a proxy .pac file'.
+ public void testProxy_ModePacScript() throws Throwable {
+ String mode = ProxyChangeListener.MODE_PAC_SCRIPT;
+ Log.v(TAG, "== Testing " + mode + " ==");
+
+ // Clear any restrictions
+ setProxyRestrictions(ProxyChangeListener.MODE_DIRECT, null, null, null);
+
+ // set the restrictions without pac url
+ setProxyRestrictions(mode, null, null, null);
+ assertNull(ProxyChangeListener.getmMdmProxyMode()); // registered mode should be null
+
+ // set the restrictions
+ String pacUrl = "http://internal.site:8888/example.pac";
+ setProxyRestrictions(mode, null, null, pacUrl);
+
+ // check configured proxy mode
+ String configuredMode = ProxyChangeListener.getmMdmProxyMode();
+ assertNotNull(configuredMode);
+ assertEquals(mode + ": configuration",mode,configuredMode);
+
+ logProxyConfig(ProxyChangeListener.getMdmProxyConfig());
+ assertEquals(ProxyChangeListener.getProperty(ProxyChangeListener.PROXY_PAC_URL), pacUrl);
+ }
+
+
+ /**
+ * Activate Proxy restriction
+ * @param mode Required. The Proxy mode we are to configure.
+ * @param proxyServer Required for MODE_FIXED_SERVERS, otherwise optional.
+ * @param nonProxyList Optional for MODE_FIXED_SERVERS, otherwise optional.
+ * This is a comma separated list of host patterns..
+ * @param pacScriptUri Required for MODE_PAC_SCRIPT, otherwise optional.
+ */
+ private void setProxyRestrictions(String mode, String proxyServer,
+ String nonProxyList, String pacScriptUri) {
+ // Construct restriction bundle
+ final Bundle restrictions = new Bundle();
+ restrictions.putString(ProxyChangeListener.PROXY_MODE, mode);
+
+ if (proxyServer != null) {
+ restrictions.putString(ProxyChangeListener.PROXY_SERVER, proxyServer);
+ }
+ if (nonProxyList != null) {
+ restrictions.putString(ProxyChangeListener.PROXY_BYPASS_LIST, nonProxyList);
+ }
+ if (pacScriptUri != null) {
+ restrictions.putString(ProxyChangeListener.PROXY_PAC_URL, pacScriptUri);
+ }
+
+ // Deliver restriction on UI thread
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ManagedProfileManager.getInstance().setMdmRestrictions(restrictions);
+ }
+ });
+
+ // Wait to ensure restriction is set
+ mInstrumentation.waitForIdleSync();
+ }
+}