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

Commit 46c67f78 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Automerger Merge Worker
Browse files

Merge "Add test for proxy spec building" am: 68dcdfa2 am: e723d84c

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

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I1f570a5527c14d8b031537270108b0db8e2eebbc
parents 8df01f4a e723d84c
Loading
Loading
Loading
Loading
+40 −24
Original line number Diff line number Diff line
@@ -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;
@@ -4530,12 +4531,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 {
@@ -4552,15 +4574,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);
    }
    /**
+50 −3
Original line number Diff line number Diff line
@@ -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;
@@ -2247,6 +2252,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();
@@ -5160,7 +5207,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;
@@ -5252,7 +5299,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;
@@ -5316,7 +5363,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;