diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java | 18 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java | 30 |
2 files changed, 47 insertions, 1 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index 9d5ed04c7..dad88f3c0 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -63,6 +63,7 @@ import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoSession; +import org.mockito.quality.Strictness; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -111,6 +112,8 @@ public class WifiConfigManagerTest { private static final int TEST_FREQUENCY_1 = 2412; private static final int TEST_FREQUENCY_2 = 5180; private static final int TEST_FREQUENCY_3 = 5240; + private static final MacAddress TEST_RANDOMIZED_MAC = + MacAddress.fromString("d2:11:19:34:a5:20"); @Mock private Context mContext; @Mock private Clock mClock; @@ -227,10 +230,14 @@ public class WifiConfigManagerTest { // static mocking mSession = ExtendedMockito.mockitoSession() .mockStatic(WifiConfigStore.class, withSettings().lenient()) + .spyStatic(WifiConfigurationUtil.class) + .strictness(Strictness.LENIENT) .startMocking(); when(WifiConfigStore.createUserFiles(anyInt(), any(UserManager.class))) .thenReturn(mock(List.class)); when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mDataTelephonyManager); + when(WifiConfigurationUtil.calculatePersistentMacForConfiguration(any(), any())) + .thenReturn(TEST_RANDOMIZED_MAC); } /** @@ -239,7 +246,9 @@ public class WifiConfigManagerTest { @After public void cleanup() { validateMockitoUsage(); - mSession.finishMocking(); + if (mSession != null) { + mSession.finishMocking(); + } } /** @@ -368,6 +377,7 @@ public class WifiConfigManagerTest { */ @Test public void testAddingNetworkWithMatchingMacAddressOverridesField() { + int prevMappingSize = mWifiConfigManager.getRandomizedMacAddressMappingSize(); WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); Map<String, String> randomizedMacAddressMapping = new HashMap<>(); final String randMac = "12:23:34:45:56:67"; @@ -382,6 +392,9 @@ public class WifiConfigManagerTest { List<WifiConfiguration> retrievedNetworks = mWifiConfigManager.getConfiguredNetworksWithPasswords(); assertEquals(randMac, retrievedNetworks.get(0).getRandomizedMacAddress().toString()); + // Verify that for networks that we already have randomizedMacAddressMapping saved + // we are still correctly writing into the WifiConfigStore. + assertEquals(prevMappingSize + 1, mWifiConfigManager.getRandomizedMacAddressMappingSize()); } /** @@ -391,6 +404,7 @@ public class WifiConfigManagerTest { */ @Test public void testRandomizedMacAddressIsPersistedOverForgetNetwork() { + int prevMappingSize = mWifiConfigManager.getRandomizedMacAddressMappingSize(); // Create and add an open network WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); verifyAddNetworkToWifiConfigManager(openNetwork); @@ -410,6 +424,8 @@ public class WifiConfigManagerTest { verifyAddNetworkToWifiConfigManager(openNetwork); retrievedNetworks = mWifiConfigManager.getConfiguredNetworksWithPasswords(); assertEquals(randMac, retrievedNetworks.get(0).getRandomizedMacAddress().toString()); + // Verify that we are no longer persisting the randomized MAC address with WifiConfigStore. + assertEquals(prevMappingSize, mWifiConfigManager.getRandomizedMacAddressMappingSize()); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java index c1640ce91..7173dae5b 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java @@ -25,6 +25,7 @@ import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiEnterpriseConfig; import android.net.wifi.WifiNetworkSpecifier; import android.net.wifi.WifiScanner; +import android.os.Binder; import android.os.PatternMatcher; import android.os.UserHandle; import android.util.Pair; @@ -39,6 +40,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import javax.crypto.Mac; + /** * Unit tests for {@link com.android.server.wifi.WifiConfigurationUtil}. */ @@ -961,6 +964,33 @@ public class WifiConfigurationUtilTest { existingConfig, newConfig)); } + /** + * Verifies that calculatePersistentMacForConfiguration produces persistent, locally generated + * MAC addresses that are valid for MAC randomization. + */ + @Test + public void testCalculatePersistentMacForConfiguration() { + // verify null inputs + assertNull(WifiConfigurationUtil.calculatePersistentMacForConfiguration(null, null)); + + // test multiple times since there is some randomness involved with hashing + int uid = Binder.getCallingUid(); + for (int i = 0; i < 10; i++) { + // Verify that a the MAC address calculated is valid + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); + Mac hashFunction = WifiConfigurationUtil.obtainMacRandHashFunction(uid); + MacAddress macAddress = WifiConfigurationUtil.calculatePersistentMacForConfiguration( + config, hashFunction); + assertTrue(WifiConfiguration.isValidMacAddressForRandomization(macAddress)); + + // Verify that the secret used to generate MAC address is persistent + Mac hashFunction2 = WifiConfigurationUtil.obtainMacRandHashFunction(uid); + MacAddress macAddress2 = WifiConfigurationUtil.calculatePersistentMacForConfiguration( + config, hashFunction2); + assertEquals(macAddress, macAddress2); + } + } + private static class EnterpriseConfig { public String eap; public String phase2; |