diff options
author | xshu <xshu@google.com> | 2019-11-04 15:36:22 -0800 |
---|---|---|
committer | xshu <xshu@google.com> | 2019-12-02 16:38:24 -0800 |
commit | fe3912fd156bc82e501dac14db9a07aa18a82acc (patch) | |
tree | ab1caab980f5bd7e51694509b25bbea9f370e1be /tests | |
parent | 9328b5ed7bcda1cf5d73cdaa9c70284b58462547 (diff) |
[MAC rand] Fix unit test slowness
Removing usage use spyStatic to speed up unit test.
Bug: 143712485
Test: atest WifiConfigManagerTest is back to normal speed now
Change-Id: I39bb0916bf3a88f73a1ca869af347e3442f2384b
Merged-In: I39bb0916bf3a88f73a1ca869af347e3442f2384b
(cherry picked from: 316dd9f75f489c8e4c26efaae56f1cdefe9a56b9)
Diffstat (limited to 'tests')
3 files changed, 77 insertions, 33 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/MacAddressUtilTest.java b/tests/wifitests/src/com/android/server/wifi/MacAddressUtilTest.java new file mode 100644 index 000000000..253310840 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/MacAddressUtilTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.net.MacAddress; +import android.net.wifi.WifiConfiguration; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Random; + +import javax.crypto.Mac; + +/** + * Unit tests for {@link com.android.server.wifi.MacAddressUtil}. + */ +@SmallTest +public class MacAddressUtilTest { + private MacAddressUtil mMacAddressUtil; + + @Mock private Mac mMac; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mMacAddressUtil = new MacAddressUtil(); + } + + /** + * Verifies that calculatePersistentMacForConfiguration valid randomized MACs. + */ + @Test + public void testCalculatePersistentMacForConfiguration() { + // verify null inputs + assertNull(mMacAddressUtil.calculatePersistentMacForConfiguration(null, null)); + + Random rand = new Random(); + // Verify that a the MAC address calculated is valid + for (int i = 0; i < 10; i++) { + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); + + byte[] bytes = new byte[32]; + rand.nextBytes(bytes); + when(mMac.doFinal(any())).thenReturn(bytes); + MacAddress macAddress = mMacAddressUtil.calculatePersistentMacForConfiguration( + config, mMac); + assertTrue(WifiConfiguration.isValidMacAddressForRandomization(macAddress)); + } + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index c3b90d36c..95a1e84e0 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -135,6 +135,7 @@ public class WifiConfigManagerTest { @Mock private WifiConfigManager.OnSavedNetworkUpdateListener mWcmListener; @Mock private FrameworkFacade mFrameworkFacade; @Mock private CarrierNetworkConfig mCarrierNetworkConfig; + @Mock private MacAddressUtil mMacAddressUtil; private MockResources mResources; private InOrder mContextConfigStoreMockOrder; @@ -218,6 +219,10 @@ public class WifiConfigManagerTest { when(mWifiInjector.getWifiLastResortWatchdog().shouldIgnoreSsidUpdate()) .thenReturn(false); when(mWifiInjector.getCarrierNetworkConfig()).thenReturn(mCarrierNetworkConfig); + when(mWifiInjector.getMacAddressUtil()).thenReturn(mMacAddressUtil); + when(mMacAddressUtil.calculatePersistentMacForConfiguration(any(), any())) + .thenReturn(TEST_RANDOMIZED_MAC); + createWifiConfigManager(); mWifiConfigManager.setOnSavedNetworkUpdateListener(mWcmListener); ArgumentCaptor<ContentObserver> observerCaptor = @@ -233,13 +238,10 @@ public class WifiConfigManagerTest { // static mocking mSession = ExtendedMockito.mockitoSession() .mockStatic(WifiConfigStore.class, withSettings().lenient()) - .spyStatic(WifiConfigurationUtil.class) .strictness(Strictness.LENIENT) .startMocking(); when(WifiConfigStore.createUserFiles(anyInt(), anyBoolean())).thenReturn(mock(List.class)); when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mDataTelephonyManager); - when(WifiConfigurationUtil.calculatePersistentMacForConfiguration(any(), any())) - .thenReturn(TEST_RANDOMIZED_MAC); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java index 7173dae5b..c1640ce91 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java @@ -25,7 +25,6 @@ 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; @@ -40,8 +39,6 @@ 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}. */ @@ -964,33 +961,6 @@ 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; |