Merge changes I5d3e448e,Id69ac1d3 am: 02eea987c0 am: b675eba4c1

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1711429

Change-Id: Iede15400f50e5e0f37a95f59b924d8b7dd698509
This commit is contained in:
Chiachang Wang
2021-06-03 09:22:36 +00:00
committed by Automerger Merge Worker
3 changed files with 108 additions and 18 deletions

View File

@@ -15,6 +15,7 @@
*/ */
package android.tethering.mts; package android.tethering.mts;
import static android.Manifest.permission.ACCESS_WIFI_STATE;
import static android.Manifest.permission.MANAGE_TEST_NETWORKS; import static android.Manifest.permission.MANAGE_TEST_NETWORKS;
import static android.Manifest.permission.NETWORK_SETTINGS; import static android.Manifest.permission.NETWORK_SETTINGS;
import static android.Manifest.permission.READ_DEVICE_CONFIG; import static android.Manifest.permission.READ_DEVICE_CONFIG;
@@ -70,7 +71,7 @@ public class TetheringModuleTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mUiAutomation.adoptShellPermissionIdentity(MANAGE_TEST_NETWORKS, NETWORK_SETTINGS, mUiAutomation.adoptShellPermissionIdentity(MANAGE_TEST_NETWORKS, NETWORK_SETTINGS,
WRITE_SETTINGS, READ_DEVICE_CONFIG, TETHER_PRIVILEGED); WRITE_SETTINGS, READ_DEVICE_CONFIG, TETHER_PRIVILEGED, ACCESS_WIFI_STATE);
mContext = InstrumentationRegistry.getContext(); mContext = InstrumentationRegistry.getContext();
mTm = mContext.getSystemService(TetheringManager.class); mTm = mContext.getSystemService(TetheringManager.class);
mCtsTetheringUtils = new CtsTetheringUtils(mContext); mCtsTetheringUtils = new CtsTetheringUtils(mContext);
@@ -102,7 +103,7 @@ public class TetheringModuleTest {
TestNetworkTracker tnt = null; TestNetworkTracker tnt = null;
try { try {
tetherEventCallback.assumeTetheringSupported(); tetherEventCallback.assumeTetheringSupported();
assumeTrue(isWifiTetheringSupported(tetherEventCallback)); assumeTrue(isWifiTetheringSupported(mContext, tetherEventCallback));
tetherEventCallback.expectNoTetheringActive(); tetherEventCallback.expectNoTetheringActive();
final TetheringInterface tetheredIface = final TetheringInterface tetheredIface =

View File

@@ -49,11 +49,16 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
import static android.net.NetworkCapabilities.TRANSPORT_TEST; import static android.net.NetworkCapabilities.TRANSPORT_TEST;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.net.TetheringManager.TETHERING_WIFI;
import static android.net.TetheringManager.TetheringRequest;
import static android.net.cts.util.CtsNetUtils.ConnectivityActionReceiver; import static android.net.cts.util.CtsNetUtils.ConnectivityActionReceiver;
import static android.net.cts.util.CtsNetUtils.HTTP_PORT; import static android.net.cts.util.CtsNetUtils.HTTP_PORT;
import static android.net.cts.util.CtsNetUtils.NETWORK_CALLBACK_ACTION; import static android.net.cts.util.CtsNetUtils.NETWORK_CALLBACK_ACTION;
import static android.net.cts.util.CtsNetUtils.TEST_HOST; import static android.net.cts.util.CtsNetUtils.TEST_HOST;
import static android.net.cts.util.CtsNetUtils.TestNetworkCallback; import static android.net.cts.util.CtsNetUtils.TestNetworkCallback;
import static android.net.cts.util.CtsTetheringUtils.StartTetheringCallback;
import static android.net.cts.util.CtsTetheringUtils.TestTetheringEventCallback;
import static android.net.cts.util.CtsTetheringUtils.isWifiTetheringSupported;
import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT; import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT;
import static android.provider.Settings.Global.NETWORK_METERED_MULTIPATH_PREFERENCE; import static android.provider.Settings.Global.NETWORK_METERED_MULTIPATH_PREFERENCE;
import static android.system.OsConstants.AF_INET; import static android.system.OsConstants.AF_INET;
@@ -92,6 +97,7 @@ import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback; import android.net.ConnectivityManager.NetworkCallback;
import android.net.ConnectivitySettingsManager;
import android.net.InetAddresses; import android.net.InetAddresses;
import android.net.IpSecManager; import android.net.IpSecManager;
import android.net.IpSecManager.UdpEncapsulationSocket; import android.net.IpSecManager.UdpEncapsulationSocket;
@@ -111,6 +117,7 @@ import android.net.SocketKeepalive;
import android.net.TelephonyNetworkSpecifier; import android.net.TelephonyNetworkSpecifier;
import android.net.TestNetworkInterface; import android.net.TestNetworkInterface;
import android.net.TestNetworkManager; import android.net.TestNetworkManager;
import android.net.TetheringManager;
import android.net.cts.util.CtsNetUtils; import android.net.cts.util.CtsNetUtils;
import android.net.util.KeepaliveUtils; import android.net.util.KeepaliveUtils;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
@@ -228,6 +235,9 @@ public class ConnectivityManagerTest {
private static final LinkAddress TEST_LINKADDR = new LinkAddress( private static final LinkAddress TEST_LINKADDR = new LinkAddress(
InetAddresses.parseNumericAddress("2001:db8::8"), 64); InetAddresses.parseNumericAddress("2001:db8::8"), 64);
private static final int AIRPLANE_MODE_OFF = 0;
private static final int AIRPLANE_MODE_ON = 1;
private Context mContext; private Context mContext;
private Instrumentation mInstrumentation; private Instrumentation mInstrumentation;
private ConnectivityManager mCm; private ConnectivityManager mCm;
@@ -237,6 +247,7 @@ public class ConnectivityManagerTest {
private final ArraySet<Integer> mNetworkTypes = new ArraySet<>(); private final ArraySet<Integer> mNetworkTypes = new ArraySet<>();
private UiAutomation mUiAutomation; private UiAutomation mUiAutomation;
private CtsNetUtils mCtsNetUtils; private CtsNetUtils mCtsNetUtils;
private TetheringManager mTm;
// Used for cleanup purposes. // Used for cleanup purposes.
private final List<Range<Integer>> mVpnRequiredUidRanges = new ArrayList<>(); private final List<Range<Integer>> mVpnRequiredUidRanges = new ArrayList<>();
@@ -250,6 +261,7 @@ public class ConnectivityManagerTest {
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mPackageManager = mContext.getPackageManager(); mPackageManager = mContext.getPackageManager();
mCtsNetUtils = new CtsNetUtils(mContext); mCtsNetUtils = new CtsNetUtils(mContext);
mTm = mContext.getSystemService(TetheringManager.class);
if (DevSdkIgnoreRuleKt.isDevSdkInRange(null /* minExclusive */, if (DevSdkIgnoreRuleKt.isDevSdkInRange(null /* minExclusive */,
Build.VERSION_CODES.R /* maxInclusive */)) { Build.VERSION_CODES.R /* maxInclusive */)) {
@@ -1995,4 +2007,76 @@ public class ConnectivityManagerTest {
assertThrows(SecurityException.class, () -> mCm.setGlobalProxy( assertThrows(SecurityException.class, () -> mCm.setGlobalProxy(
ProxyInfo.buildDirectProxy("example.com" /* host */, 8080 /* port */))); ProxyInfo.buildDirectProxy("example.com" /* host */, 8080 /* port */)));
} }
@Test
public void testFactoryResetWithoutPermission() {
assumeTrue(TestUtils.shouldTestSApis());
assertThrows(SecurityException.class, () -> mCm.factoryReset());
}
@Test
public void testFactoryReset() throws Exception {
assumeTrue(TestUtils.shouldTestSApis());
// Store current settings.
final int curAvoidBadWifi =
ConnectivitySettingsManager.getNetworkAvoidBadWifi(mContext);
final int curPrivateDnsMode = ConnectivitySettingsManager.getPrivateDnsMode(mContext);
final TestTetheringEventCallback tetherEventCallback = new TestTetheringEventCallback();
try {
mTm.registerTetheringEventCallback(c -> c.run() /* executor */, tetherEventCallback);
// Adopt for NETWORK_SETTINGS permission.
mUiAutomation.adoptShellPermissionIdentity();
// start tethering
tetherEventCallback.assumeWifiTetheringSupported(mContext);
startWifiTethering(tetherEventCallback);
// Update setting to verify the behavior.
mCm.setAirplaneMode(true);
ConnectivitySettingsManager.setPrivateDnsMode(mContext,
ConnectivitySettingsManager.PRIVATE_DNS_MODE_OFF);
ConnectivitySettingsManager.setNetworkAvoidBadWifi(mContext,
ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI_IGNORE);
assertEquals(AIRPLANE_MODE_ON, Settings.Global.getInt(
mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON));
// Verify factoryReset
mCm.factoryReset();
verifySettings(AIRPLANE_MODE_OFF,
ConnectivitySettingsManager.PRIVATE_DNS_MODE_OPPORTUNISTIC,
ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI_PROMPT);
tetherEventCallback.expectNoTetheringActive();
} finally {
// Restore settings.
mCm.setAirplaneMode(false);
ConnectivitySettingsManager.setNetworkAvoidBadWifi(mContext, curAvoidBadWifi);
ConnectivitySettingsManager.setPrivateDnsMode(mContext, curPrivateDnsMode);
mTm.unregisterTetheringEventCallback(tetherEventCallback);
mTm.stopAllTethering();
mUiAutomation.dropShellPermissionIdentity();
}
}
private void verifySettings(int expectedAirplaneMode, int expectedPrivateDnsMode,
int expectedAvoidBadWifi) throws Exception {
assertEquals(expectedAirplaneMode, Settings.Global.getInt(
mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON));
assertEquals(expectedPrivateDnsMode,
ConnectivitySettingsManager.getPrivateDnsMode(mContext));
assertEquals(expectedAvoidBadWifi,
ConnectivitySettingsManager.getNetworkAvoidBadWifi(mContext));
}
private void startWifiTethering(final TestTetheringEventCallback callback) throws Exception {
if (!isWifiTetheringSupported(mContext, callback)) return;
final List<String> wifiRegexs =
callback.getTetheringInterfaceRegexps().getTetherableWifiRegexs();
final StartTetheringCallback startTetheringCallback = new StartTetheringCallback();
final TetheringRequest request = new TetheringRequest.Builder(TETHERING_WIFI)
.setShouldShowEntitlementUi(false).build();
mTm.startTethering(request, c -> c.run() /* executor */, startTetheringCallback);
startTetheringCallback.verifyTetheringStarted();
callback.expectTetheredInterfacesChanged(wifiRegexs, TETHERING_WIFI);
}
} }

View File

@@ -388,20 +388,7 @@ public final class CtsTetheringUtils {
assumeTetheringSupported(); assumeTetheringSupported();
assumeTrue(!getTetheringInterfaceRegexps().getTetherableWifiRegexs().isEmpty()); assumeTrue(!getTetheringInterfaceRegexps().getTetherableWifiRegexs().isEmpty());
assumeTrue(isPortableHotspotSupported(ctx));
final PackageManager pm = ctx.getPackageManager();
assumeTrue(pm.hasSystemFeature(PackageManager.FEATURE_WIFI));
WifiManager wm = ctx.getSystemService(WifiManager.class);
// Wifi feature flags only work when wifi is on.
final boolean previousWifiEnabledState = wm.isWifiEnabled();
try {
if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi enable");
waitForWifiEnabled(ctx);
assumeTrue(wm.isPortableHotspotSupported());
} finally {
if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi disable");
}
} }
public TetheringInterfaceRegexps getTetheringInterfaceRegexps() { public TetheringInterfaceRegexps getTetheringInterfaceRegexps() {
@@ -453,8 +440,26 @@ public final class CtsTetheringUtils {
return callback.getTetheringInterfaceRegexps().getTetherableWifiRegexs(); return callback.getTetheringInterfaceRegexps().getTetherableWifiRegexs();
} }
public static boolean isWifiTetheringSupported(final TestTetheringEventCallback callback) { public static boolean isWifiTetheringSupported(final Context ctx,
return !getWifiTetherableInterfaceRegexps(callback).isEmpty(); final TestTetheringEventCallback callback) throws Exception {
return !getWifiTetherableInterfaceRegexps(callback).isEmpty()
&& isPortableHotspotSupported(ctx);
}
/* Returns if wifi supports hotspot. */
private static boolean isPortableHotspotSupported(final Context ctx) throws Exception {
final PackageManager pm = ctx.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_WIFI)) return false;
final WifiManager wm = ctx.getSystemService(WifiManager.class);
// Wifi feature flags only work when wifi is on.
final boolean previousWifiEnabledState = wm.isWifiEnabled();
try {
if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi enable");
waitForWifiEnabled(ctx);
return wm.isPortableHotspotSupported();
} finally {
if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi disable");
}
} }
public TetheringInterface startWifiTethering(final TestTetheringEventCallback callback) public TetheringInterface startWifiTethering(final TestTetheringEventCallback callback)