Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6fc985c3 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Add test for proxy spec building

In preparation for a refactor of the logic, add a test to verify proxy
spec building and validation in DevicePolicyManager#setGlobalProxy.

Bug: 174436414
Test: atest DevicePolicyManagerTest#testGetProxyParameters
Change-Id: Ic1c8bf6e8f5e79a7a1736af6cc1d50a7969fb9ad
parent 773d2c63
Loading
Loading
Loading
Loading
+40 −24
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ import android.telephony.data.ApnSetting;
import android.util.ArraySet;
import android.util.DebugUtils;
import android.util.Log;
import android.util.Pair;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.NetworkUtilsInternal;
@@ -5276,12 +5277,33 @@ public class DevicePolicyManager {
                    if (!proxySpec.type().equals(Proxy.Type.HTTP)) {
                        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) {
                throw e.rethrowFromSystemServer();
            }
        }
        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();
                    hostSpec = hostBuilder.append(hostName)
        final String hostSpec = hostBuilder.append(hostName)
                .append(":").append(Integer.toString(port)).toString();
        final String exclSpec;
        if (exclusionList == null) {
            exclSpec = "";
        } else {
@@ -5298,15 +5320,9 @@ public class DevicePolicyManager {
            exclSpec = listBuilder.toString();
        }
        if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
                            != android.net.Proxy.PROXY_VALID)
                        throw new IllegalArgumentException();
                }
                return mService.setGlobalProxy(admin, hostSpec, exclSpec);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
        return null;
                != android.net.Proxy.PROXY_VALID) throw new IllegalArgumentException();
        return new Pair<>(hostSpec, exclSpec);
    }
    /**
+51 −3
Original line number Diff line number Diff line
@@ -31,6 +31,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;
@@ -66,6 +67,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;
@@ -126,6 +129,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;
@@ -2359,6 +2364,49 @@ public class DevicePolicyManagerTest extends DpmTestBase {
                () -> parentDpm.setPermittedInputMethods(admin1, inputMethods));
    }

    @Test
    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);
    }

    @Test
    public void testSetKeyguardDisabledFeaturesWithDO() throws Exception {
        mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
@@ -5494,7 +5542,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        // Attempt to set to empty list (which means no listener is allowlisted)
        mContext.binder.callingUid = adminUid;
        assertThat(dpms.setPermittedCrossProfileNotificationListeners(
                admin1, Collections.emptyList())).isFalse();
                admin1, emptyList())).isFalse();
        assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1)).isNull();

        mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
@@ -5588,7 +5636,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        // Setting an empty allowlist - only system listeners allowed
        mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID;
        assertThat(dpms.setPermittedCrossProfileNotificationListeners(
                admin1, Collections.emptyList())).isTrue();
                admin1, emptyList())).isTrue();
        assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1).size()).isEqualTo(0);

        mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
@@ -5653,7 +5701,7 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        // all allowed in primary profile
        mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID;
        assertThat(dpms.setPermittedCrossProfileNotificationListeners(
                admin1, Collections.emptyList())).isTrue();
                admin1, emptyList())).isTrue();
        assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1).size()).isEqualTo(0);

        mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;