diff options
author | Oscar Shu <xshu@google.com> | 2019-08-02 17:37:02 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-08-02 17:37:02 +0000 |
commit | 5cd8ccf301ade70aaf1f5b9fb7a2da165bd71567 (patch) | |
tree | b0f91c9cd73cd5a3be1fc6360fa0c75c881e9f01 /tests | |
parent | 2633b084a64d18066dc7bdb2c3660c7bc41ed11b (diff) | |
parent | 5440ad32bf407b7eb159b8b30576df9c71321b95 (diff) |
Merge changes Ie177e36d,I3e80a75b,I1db57159
* changes:
[MAC rand] cleanup obsolete code
[MAC rand] using aggressive mac in ClientModeImpl
[MAC rand] maintaining an aggressive MAC
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java | 77 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java | 43 |
2 files changed, 71 insertions, 49 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java index 55d178cfc..a800aa976 100644 --- a/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java @@ -90,6 +90,7 @@ import android.util.Pair; import androidx.test.filters.SmallTest; +import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.internal.R; import com.android.internal.app.IBatteryStats; import com.android.internal.util.AsyncChannel; @@ -114,6 +115,8 @@ import org.mockito.ArgumentMatcher; import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.mockito.MockitoSession; +import org.mockito.quality.Strictness; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; @@ -153,6 +156,10 @@ public class ClientModeImplTest { MacAddress.fromString("10:22:34:56:78:92"); private static final MacAddress TEST_LOCAL_MAC_ADDRESS = MacAddress.fromString("2a:53:43:c3:56:21"); + private static final MacAddress TEST_DEFAULT_MAC_ADDRESS = + MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); + private static final MacAddress TEST_AGGRESSIVE_MAC_ADDRESS = + MacAddress.fromString("3a:53:43:c3:56:21"); // NetworkAgent creates threshold ranges with Integers private static final int RSSI_THRESHOLD_MAX = -30; @@ -162,6 +169,7 @@ public class ClientModeImplTest { private static final byte RSSI_THRESHOLD_BREACH_MAX = -20; private long mBinderToken; + private MockitoSession mSession; private static <T> T mockWithInterfaces(Class<T> class1, Class<?>... interfaces) { return mock(class1, withSettings().extraInterfaces(interfaces)); @@ -403,7 +411,6 @@ public class ClientModeImplTest { /** uncomment this to enable logs from ClientModeImpls */ // enableDebugLogs(); - mWifiMonitor = new MockWifiMonitor(); when(mWifiInjector.getWifiMetrics()).thenReturn(mWifiMetrics); when(mWifiInjector.getClock()).thenReturn(new Clock()); @@ -496,6 +503,11 @@ public class ClientModeImplTest { mIpClientCallback.onQuit(); return null; }).when(mIpClient).shutdown(); + + // static mocking + mSession = ExtendedMockito.mockitoSession().strictness(Strictness.LENIENT) + .spyStatic(MacAddress.class) + .startMocking(); initializeCmi(); mOsuProvider = PasspointProvisioningTestUtil.generateOsuProvider(true); @@ -582,6 +594,7 @@ public class ClientModeImplTest { mNetworkAgentAsyncChannel = null; mNetworkAgentHandler = null; mCmi = null; + mSession.finishMocking(); } @Test @@ -935,7 +948,7 @@ public class ClientModeImplTest { loadComponentsInStaMode(); WifiConfiguration config = mConnectedNetwork; config.networkId = FRAMEWORK_NETWORK_ID; - when(config.getOrCreateRandomizedMacAddress()).thenReturn(TEST_LOCAL_MAC_ADDRESS); + config.setRandomizedMacAddress(TEST_LOCAL_MAC_ADDRESS); config.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_PERSISTENT; setupAndStartConnectSequence(config); validateSuccessfulConnectSequence(config); @@ -2559,6 +2572,56 @@ public class ClientModeImplTest { } /** + * Verifies that + * 1. aggressive MAC is generated when ClientModeImpl is created. + * 2. aggressive MAC is generated again during connection when the appropriate amount of time + * have passed. + * @throws Exception + */ + @Test + public void testAggressiveMacUpdatedDuringConnection() throws Exception { + ExtendedMockito.verify(() -> MacAddress.createRandomUnicastAddress()); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + ClientModeImpl.AGGRESSIVE_MAC_REFRESH_MS + 1); + connect(); + ExtendedMockito.verify(() -> MacAddress.createRandomUnicastAddress(), times(2)); + } + + /** + * Verifies that aggressive MAC is not updated due to time constraint. + * @throws Exception + */ + @Test + public void testAggressiveMacNotUpdatedDueToTimeConstraint() throws Exception { + ExtendedMockito.verify(() -> MacAddress.createRandomUnicastAddress()); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + ClientModeImpl.AGGRESSIVE_MAC_REFRESH_MS); + connect(); + ExtendedMockito.verify(() -> MacAddress.createRandomUnicastAddress()); + } + + /** + * Verifies that + * 1. connected MAC randomization is on and + * 2. macRandomizationSetting of the WifiConfiguration is RANDOMIZATION_PERSISTENT and + * 3. the WifiConfiguration should use "aggressive mode" + * 4. ClientmodeImpl programs the aggressive MAC when connecting the network. + */ + @Test + public void testMacRandomizationAggressiveMacIsUsed() throws Exception { + when(MacAddress.createRandomUnicastAddress()).thenReturn(TEST_AGGRESSIVE_MAC_ADDRESS); + when(mWifiConfigManager.shouldUseAggressiveMode(any())).thenReturn(true); + initializeAndAddNetworkAndVerifySuccess(); + assertEquals(ClientModeImpl.CONNECT_MODE, mCmi.getOperationalModeForTest()); + assertEquals(WifiManager.WIFI_STATE_ENABLED, mCmi.syncGetWifiState()); + + when(mClock.getElapsedSinceBootMillis()).thenReturn( + ClientModeImpl.AGGRESSIVE_MAC_REFRESH_MS + 1); + connect(); + verify(mWifiNative).setMacAddress(WIFI_IFACE_NAME, TEST_AGGRESSIVE_MAC_ADDRESS); + } + + /** * Verifies that when * 1. Global feature support flag is set to false * 2. connected MAC randomization is on and @@ -2576,8 +2639,6 @@ public class ClientModeImplTest { assertEquals(WifiManager.WIFI_STATE_ENABLED, mCmi.syncGetWifiState()); connect(); - verify(mWifiConfigManager, never()).setNetworkRandomizedMacAddress(0, - TEST_LOCAL_MAC_ADDRESS); verify(mWifiNative, never()).setMacAddress(WIFI_IFACE_NAME, TEST_LOCAL_MAC_ADDRESS); verify(mWifiMetrics, never()) .logStaEvent(eq(StaEvent.TYPE_MAC_CHANGE), any(WifiConfiguration.class)); @@ -2601,7 +2662,6 @@ public class ClientModeImplTest { assertEquals(WifiManager.WIFI_STATE_ENABLED, mCmi.syncGetWifiState()); connect(); - verify(mWifiConfigManager).setNetworkRandomizedMacAddress(0, TEST_LOCAL_MAC_ADDRESS); verify(mWifiNative).setMacAddress(WIFI_IFACE_NAME, TEST_LOCAL_MAC_ADDRESS); verify(mWifiMetrics) .logStaEvent(eq(StaEvent.TYPE_MAC_CHANGE), any(WifiConfiguration.class)); @@ -2626,7 +2686,6 @@ public class ClientModeImplTest { .thenReturn(TEST_LOCAL_MAC_ADDRESS.toString()); connect(); - verify(mWifiConfigManager).setNetworkRandomizedMacAddress(0, TEST_LOCAL_MAC_ADDRESS); verify(mWifiNative, never()).setMacAddress(WIFI_IFACE_NAME, TEST_LOCAL_MAC_ADDRESS); verify(mWifiMetrics, never()) .logStaEvent(eq(StaEvent.TYPE_MAC_CHANGE), any(WifiConfiguration.class)); @@ -2660,7 +2719,6 @@ public class ClientModeImplTest { mCmi.sendMessage(ClientModeImpl.CMD_START_CONNECT, 0, 0, sBSSID); mLooper.dispatchAll(); - verify(mWifiConfigManager, never()).setNetworkRandomizedMacAddress(anyInt(), any()); verify(mWifiNative).setMacAddress(WIFI_IFACE_NAME, TEST_GLOBAL_MAC_ADDRESS); verify(mWifiMetrics) .logStaEvent(eq(StaEvent.TYPE_MAC_CHANGE), any(WifiConfiguration.class)); @@ -2692,7 +2750,6 @@ public class ClientModeImplTest { mCmi.sendMessage(ClientModeImpl.CMD_START_CONNECT, 0, 0, sBSSID); mLooper.dispatchAll(); - verify(mWifiConfigManager, never()).setNetworkRandomizedMacAddress(anyInt(), any()); verify(mWifiNative, never()).setMacAddress(WIFI_IFACE_NAME, TEST_GLOBAL_MAC_ADDRESS); verify(mWifiMetrics, never()) .logStaEvent(eq(StaEvent.TYPE_MAC_CHANGE), any(WifiConfiguration.class)); @@ -2734,8 +2791,7 @@ public class ClientModeImplTest { WifiConfiguration config = mock(WifiConfiguration.class); config.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_PERSISTENT; - when(config.getOrCreateRandomizedMacAddress()) - .thenReturn(MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS)); + config.setRandomizedMacAddress(MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS)); when(config.getNetworkSelectionStatus()) .thenReturn(new WifiConfiguration.NetworkSelectionStatus()); when(mWifiConfigManager.getConfiguredNetworkWithoutMasking(0)).thenReturn(config); @@ -2743,7 +2799,6 @@ public class ClientModeImplTest { mCmi.sendMessage(ClientModeImpl.CMD_START_CONNECT, 0, 0, sBSSID); mLooper.dispatchAll(); - verify(config).getOrCreateRandomizedMacAddress(); verify(mWifiNative, never()).setMacAddress(eq(WIFI_IFACE_NAME), any(MacAddress.class)); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index 28964b109..cd7aee301 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -1902,9 +1902,6 @@ public class WifiConfigManagerTest { WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(config); - MacAddress testMac = MacAddress.createRandomUnicastAddress(); - mWifiConfigManager.setNetworkRandomizedMacAddress(result.getNetworkId(), testMac); - // Verify that randomized MAC address is masked when obtaining saved networks from // invalid UID List<WifiConfiguration> configs = mWifiConfigManager.getSavedNetworks(Process.INVALID_UID); @@ -1915,7 +1912,8 @@ public class WifiConfigManagerTest { // system UID configs = mWifiConfigManager.getSavedNetworks(Process.WIFI_UID); assertEquals(1, configs.size()); - assertEquals(testMac, configs.get(0).getRandomizedMacAddress()); + String macAddress = configs.get(0).getRandomizedMacAddress().toString(); + assertNotEquals(WifiInfo.DEFAULT_MAC_ADDRESS, macAddress); // Verify that randomized MAC address is masked when obtaining saved networks from // (carrier app) non-creator of the config @@ -1927,18 +1925,18 @@ public class WifiConfigManagerTest { // (carrier app) creator of the config configs = mWifiConfigManager.getSavedNetworks(TEST_CREATOR_UID); assertEquals(1, configs.size()); - assertEquals(testMac, configs.get(0).getRandomizedMacAddress()); + assertEquals(macAddress, configs.get(0).getRandomizedMacAddress().toString()); // Verify that randomized MAC address is unmasked when getting list of privileged (with // password) configurations WifiConfiguration configWithRandomizedMac = mWifiConfigManager .getConfiguredNetworkWithPassword(result.getNetworkId()); - assertEquals(testMac, configWithRandomizedMac.getRandomizedMacAddress()); + assertEquals(macAddress, configs.get(0).getRandomizedMacAddress().toString()); // Ensure that the MAC address is present when asked for config with MAC address. configWithRandomizedMac = mWifiConfigManager .getConfiguredNetworkWithoutMasking(result.getNetworkId()); - assertEquals(testMac, configWithRandomizedMac.getRandomizedMacAddress()); + assertEquals(macAddress, configs.get(0).getRandomizedMacAddress().toString()); } /** @@ -1949,9 +1947,6 @@ public class WifiConfigManagerTest { WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(config); - MacAddress testMac = MacAddress.createRandomUnicastAddress(); - mWifiConfigManager.setNetworkRandomizedMacAddress(result.getNetworkId(), testMac); - // Verify macRandomizationSetting is not masked out when feature is supported. List<WifiConfiguration> configs = mWifiConfigManager.getSavedNetworks(Process.WIFI_UID); assertEquals(1, configs.size()); @@ -1970,9 +1965,6 @@ public class WifiConfigManagerTest { WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(config); - MacAddress testMac = MacAddress.createRandomUnicastAddress(); - mWifiConfigManager.setNetworkRandomizedMacAddress(result.getNetworkId(), testMac); - // Verify macRandomizationSetting is masked out when feature is unsupported. List<WifiConfiguration> configs = mWifiConfigManager.getSavedNetworks(Process.WIFI_UID); assertEquals(1, configs.size()); @@ -4299,31 +4291,6 @@ public class WifiConfigManagerTest { } /** - * Verifies that the method setNetworkRandomizedMacAddress changes the randomized MAC - * address variable in the internal configuration. - */ - @Test - public void testSetNetworkRandomizedMacAddressUpdatesInternalMacAddress() { - WifiConfiguration originalConfig = WifiConfigurationTestUtil.createOpenNetwork(); - NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(originalConfig); - - // Verify that internal randomized MAC address does not change from - // from setting external randomized MAC address - MacAddress originalMac = originalConfig.getOrCreateRandomizedMacAddress(); - WifiConfiguration retrievedConfig = mWifiConfigManager - .getConfiguredNetworkWithoutMasking(result.getNetworkId()); - assertNotEquals(originalMac, retrievedConfig.getRandomizedMacAddress()); - - // Verify that changing randomized MAC address through setNetworkRandomizedMacAddress - // changes the internal randomized MAC address - MacAddress newMac = MacAddress.createRandomUnicastAddress(); - mWifiConfigManager.setNetworkRandomizedMacAddress(result.getNetworkId(), newMac); - retrievedConfig = mWifiConfigManager - .getConfiguredNetworkWithoutMasking(result.getNetworkId()); - assertEquals(newMac, retrievedConfig.getRandomizedMacAddress()); - } - - /** * Verifies that the method resetSimNetworks updates SIM presence status and SIM configs. */ @Test |