Merge "Add test for proxy spec building" am: 68dcdfa29b

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1554103

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I33167ab926d43b8921a30e190bc44136fc0547d2
This commit is contained in:
Remi NGUYEN VAN
2021-01-25 02:26:09 +00:00
committed by Automerger Merge Worker
2 changed files with 90 additions and 27 deletions

View File

@@ -87,6 +87,7 @@ import android.telephony.TelephonyManager;
import android.telephony.data.ApnSetting;
import android.util.ArraySet;
import android.util.Log;
import android.util.Pair;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.NetworkUtilsInternal;
@@ -4524,30 +4525,10 @@ public class DevicePolicyManager {
if (!proxySpec.type().equals(Proxy.Type.HTTP)) {
throw new IllegalArgumentException();
}
InetSocketAddress sa = (InetSocketAddress)proxySpec.address();
String hostName = sa.getHostName();
int port = sa.getPort();
StringBuilder hostBuilder = new StringBuilder();
hostSpec = hostBuilder.append(hostName)
.append(":").append(Integer.toString(port)).toString();
if (exclusionList == null) {
exclSpec = "";
} else {
StringBuilder listBuilder = new StringBuilder();
boolean firstDomain = true;
for (String exclDomain : exclusionList) {
if (!firstDomain) {
listBuilder = listBuilder.append(",");
} else {
firstDomain = false;
}
listBuilder = listBuilder.append(exclDomain.trim());
}
exclSpec = listBuilder.toString();
}
if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
!= android.net.Proxy.PROXY_VALID)
throw new IllegalArgumentException();
final Pair<String, String> proxyParams =
getProxyParameters(proxySpec, exclusionList);
hostSpec = proxyParams.first;
exclSpec = proxyParams.second;
}
return mService.setGlobalProxy(admin, hostSpec, exclSpec);
} catch (RemoteException e) {
@@ -4557,6 +4538,41 @@ public class DevicePolicyManager {
return null;
}
/**
* Build HTTP proxy parameters for {@link IDevicePolicyManager#setGlobalProxy}.
* @throws IllegalArgumentException Invalid proxySpec
* @hide
*/
@VisibleForTesting
public Pair<String, String> getProxyParameters(Proxy proxySpec, List<String> exclusionList) {
InetSocketAddress sa = (InetSocketAddress) proxySpec.address();
String hostName = sa.getHostName();
int port = sa.getPort();
StringBuilder hostBuilder = new StringBuilder();
final String hostSpec = hostBuilder.append(hostName)
.append(":").append(Integer.toString(port)).toString();
final String exclSpec;
if (exclusionList == null) {
exclSpec = "";
} else {
StringBuilder listBuilder = new StringBuilder();
boolean firstDomain = true;
for (String exclDomain : exclusionList) {
if (!firstDomain) {
listBuilder = listBuilder.append(",");
} else {
firstDomain = false;
}
listBuilder = listBuilder.append(exclDomain.trim());
}
exclSpec = listBuilder.toString();
}
if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
!= android.net.Proxy.PROXY_VALID) throw new IllegalArgumentException();
return new Pair<>(hostSpec, exclSpec);
}
/**
* Set a network-independent global HTTP proxy. This is not normally what you want for typical
* HTTP proxies - they are generally network dependent. However if you're doing something

View File

@@ -30,6 +30,7 @@ import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_NONE;
import static android.app.admin.DevicePolicyManager.WIPE_EUICC;
import static android.app.admin.PasswordMetrics.computeForPassword;
import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_DIRECT_BOOT_AWARE;
import static android.net.InetAddresses.parseNumericAddress;
import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_NONE;
import static com.android.internal.widget.LockPatternUtils.EscrowTokenStateChangeCallback;
@@ -65,6 +66,8 @@ import static org.mockito.Mockito.when;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
import static org.testng.Assert.assertThrows;
import static java.util.Collections.emptyList;
import android.Manifest.permission;
import android.app.Activity;
import android.app.AppOpsManager;
@@ -118,6 +121,8 @@ import org.mockito.internal.util.collections.Sets;
import org.mockito.stubbing.Answer;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -2246,6 +2251,48 @@ public class DevicePolicyManagerTest extends DpmTestBase {
assertThat(actualAccounts).containsExactlyElementsIn(expectedAccounts);
}
public void testGetProxyParameters() throws Exception {
assertThat(dpm.getProxyParameters(inetAddrProxy("192.0.2.1", 1234), emptyList()))
.isEqualTo(new Pair<>("192.0.2.1:1234", ""));
assertThat(dpm.getProxyParameters(inetAddrProxy("192.0.2.1", 1234),
listOf("one.example.com ", " two.example.com ")))
.isEqualTo(new Pair<>("192.0.2.1:1234", "one.example.com,two.example.com"));
assertThat(dpm.getProxyParameters(hostnameProxy("proxy.example.com", 1234), emptyList()))
.isEqualTo(new Pair<>("proxy.example.com:1234", ""));
assertThat(dpm.getProxyParameters(hostnameProxy("proxy.example.com", 1234),
listOf("excluded.example.com")))
.isEqualTo(new Pair<>("proxy.example.com:1234", "excluded.example.com"));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
inetAddrProxy("192.0.2.1", 0), emptyList()));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
hostnameProxy("", 1234), emptyList()));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
hostnameProxy("", 0), emptyList()));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
hostnameProxy("invalid! hostname", 1234), emptyList()));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
hostnameProxy("proxy.example.com", 1234), listOf("invalid exclusion")));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
hostnameProxy("proxy.example.com", -1), emptyList()));
assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters(
hostnameProxy("proxy.example.com", 0xFFFF + 1), emptyList()));
}
private static Proxy inetAddrProxy(String inetAddr, int port) {
return new Proxy(
Proxy.Type.HTTP, new InetSocketAddress(parseNumericAddress(inetAddr), port));
}
private static Proxy hostnameProxy(String hostname, int port) {
return new Proxy(
Proxy.Type.HTTP, InetSocketAddress.createUnresolved(hostname, port));
}
private static List<String> listOf(String... args) {
return Arrays.asList(args);
}
public void testSetKeyguardDisabledFeaturesWithDO() throws Exception {
mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
setupDeviceOwner();
@@ -5156,7 +5203,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
// Attempt to set to empty list (which means no listener is allowlisted)
mContext.binder.callingUid = adminUid;
assertFalse(dpms.setPermittedCrossProfileNotificationListeners(
admin1, Collections.emptyList()));
admin1, emptyList()));
assertNull(dpms.getPermittedCrossProfileNotificationListeners(admin1));
mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
@@ -5248,7 +5295,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
// Setting an empty allowlist - only system listeners allowed
mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID;
assertTrue(dpms.setPermittedCrossProfileNotificationListeners(
admin1, Collections.emptyList()));
admin1, emptyList()));
assertEquals(0, dpms.getPermittedCrossProfileNotificationListeners(admin1).size());
mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
@@ -5312,7 +5359,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
// all allowed in primary profile
mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID;
assertTrue(dpms.setPermittedCrossProfileNotificationListeners(
admin1, Collections.emptyList()));
admin1, emptyList()));
assertEquals(0, dpms.getPermittedCrossProfileNotificationListeners(admin1).size());
mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;