summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorxshu <xshu@google.com>2019-11-04 15:36:22 -0800
committerxshu <xshu@google.com>2019-11-04 16:12:36 -0800
commit316dd9f75f489c8e4c26efaae56f1cdefe9a56b9 (patch)
tree3385ec85cc2466c3b38336c2555948e7fdb41155 /tests
parent2028120248de6500c24a96cc52f21b1fceb7ad4d (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
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/MacAddressUtilTest.java72
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java8
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java29
3 files changed, 77 insertions, 32 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..331725c14
--- /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 extends WifiBaseTest {
+ 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 62eef1ac8..74f102652 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
@@ -134,6 +134,7 @@ public class WifiConfigManagerTest extends WifiBaseTest {
@Mock private FrameworkFacade mFrameworkFacade;
@Mock private DeviceConfigFacade mDeviceConfigFacade;
@Mock private CarrierNetworkConfig mCarrierNetworkConfig;
+ @Mock private MacAddressUtil mMacAddressUtil;
private MockResources mResources;
private InOrder mContextConfigStoreMockOrder;
@@ -215,6 +216,10 @@ public class WifiConfigManagerTest extends WifiBaseTest {
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.addOnNetworkUpdateListener(mWcmListener);
ArgumentCaptor<ContentObserver> observerCaptor =
@@ -230,13 +235,10 @@ public class WifiConfigManagerTest extends WifiBaseTest {
// static mocking
mSession = ExtendedMockito.mockitoSession()
.mockStatic(WifiConfigStore.class, withSettings().lenient())
- .spyStatic(WifiConfigurationUtil.class)
.strictness(Strictness.LENIENT)
.startMocking();
when(WifiConfigStore.createUserFiles(anyInt())).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 a241a1c42..567a2b7ad 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.util.Pair;
@@ -39,8 +38,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}.
*/
@@ -940,32 +937,6 @@ public class WifiConfigurationUtilTest extends WifiBaseTest {
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));
-
- // Verify that a the MAC address calculated is valid
- int uid = Binder.getCallingUid();
- for (int i = 0; i < 10; i++) {
- 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;