summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOscar Shu <xshu@google.com>2019-12-03 18:34:56 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2019-12-03 18:34:56 +0000
commit85cf0ca195b932e5f35b1938e7b711ab73b8e677 (patch)
treedfa9c5fc67a97f392202c82d17b445b4be060528 /tests
parentdafd07c544a9c17d0afb965184411a5644a77421 (diff)
parent151e7bb7b5ac4633bdac673bba401e5b80edf03c (diff)
Merge changes I478f1d55,I39bb0916 into qt-qpr1-dev
* changes: Fix boot regression from KeyStore being slow [MAC rand] Fix unit test slowness
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.java18
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java30
3 files changed, 87 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 c4cbc6e50..7dd675c3c 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;
@@ -216,6 +217,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 =
@@ -231,13 +236,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);
}
/**
@@ -292,6 +294,16 @@ public class WifiConfigManagerTest {
}
/**
+ * Verifies that the Mac randomization secret hashfunction is obtained after |loadFromStore|.
+ */
+ @Test
+ public void testMacHashIsObtainedAfterLoadFromStore() {
+ verify(mMacAddressUtil, never()).obtainMacRandHashFunction(anyInt());
+ assertTrue(mWifiConfigManager.loadFromStore());
+ verify(mMacAddressUtil).obtainMacRandHashFunction(anyInt());
+ }
+
+ /**
* Verifies the addition of a single network using
* {@link WifiConfigManager#addOrUpdateNetwork(WifiConfiguration, int)}
*/
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;