summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRoshan Pius <rpius@google.com>2017-02-09 16:35:16 -0800
committerRoshan Pius <rpius@google.com>2017-02-13 17:51:31 -0800
commitf180b0109b3fce79609b03ae2f7fbeff02d96b80 (patch)
treedffa175c498733e4e6ef4be00e4d8b0def9e75ff /tests
parent11d07764318e2491003346ce88047862fea45b4f (diff)
SupplicantStaIfaceHal: Load networks
Porting over the load networks from wpa_supplicant to HIDL interface. Bug: 33383725 Test: Unit tests Change-Id: Ica1fd02dd2d5fae823e17b61ffd12d9090ca8fd0
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java228
1 files changed, 221 insertions, 7 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java b/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
index 6645a8d5e..e80b81ef5 100644
--- a/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
@@ -15,29 +15,35 @@
*/
package com.android.server.wifi;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.test.MockAnswerUtil;
import android.hardware.wifi.supplicant.V1_0.ISupplicant;
import android.hardware.wifi.supplicant.V1_0.ISupplicantIface;
import android.hardware.wifi.supplicant.V1_0.ISupplicantStaIface;
+import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetwork;
import android.hardware.wifi.supplicant.V1_0.IfaceType;
import android.hardware.wifi.supplicant.V1_0.SupplicantStatus;
import android.hardware.wifi.supplicant.V1_0.SupplicantStatusCode;
import android.hidl.manager.V1_0.IServiceManager;
import android.hidl.manager.V1_0.IServiceNotification;
+import android.net.IpConfiguration;
+import android.net.wifi.WifiConfiguration;
import android.os.HandlerThread;
import android.os.IHwBinder;
import android.os.RemoteException;
import android.os.test.TestLooper;
+import android.util.SparseArray;
import org.junit.Before;
import org.junit.Test;
@@ -47,24 +53,31 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
/**
* Unit tests for SupplicantStaIfaceHal
*/
public class SupplicantStaIfaceHalTest {
private static final String TAG = "SupplicantStaIfaceHalTest";
- private SupplicantStaIfaceHal mDut;
+ private static final Map<Integer, String> NETWORK_ID_TO_SSID = new HashMap<Integer, String>() {{
+ put(1, "ssid1");
+ put(2, "ssid2");
+ put(3, "ssid3");
+ }};
@Mock IServiceManager mServiceManagerMock;
@Mock ISupplicant mISupplicantMock;
@Mock ISupplicantIface mISupplicantIfaceMock;
@Mock ISupplicantStaIface mISupplicantStaIfaceMock;
@Mock HandlerThread mHandlerThread;
-
+ @Mock SupplicantStaNetworkHal mSupplicantStaNetworkMock;
SupplicantStatus mStatusSuccess;
+ SupplicantStatus mStatusFailure;
ISupplicant.IfaceInfo mStaIface;
ISupplicant.IfaceInfo mP2pIface;
ArrayList<ISupplicant.IfaceInfo> mIfaceInfoList;
-
+ private SupplicantStaIfaceHal mDut;
private ArgumentCaptor<IHwBinder.DeathRecipient> mDeathRecipientCaptor =
ArgumentCaptor.forClass(IHwBinder.DeathRecipient.class);
private ArgumentCaptor<IServiceNotification.Stub> mServiceNotificationCaptor =
@@ -76,6 +89,7 @@ public class SupplicantStaIfaceHalTest {
SupplicantStaIfaceHalSpy(HandlerThread handlerThread) {
super(handlerThread);
}
+
@Override
protected IServiceManager getServiceManagerMockable() throws RemoteException {
return mServiceManagerMock;
@@ -90,6 +104,12 @@ public class SupplicantStaIfaceHalTest {
protected ISupplicantStaIface getStaIfaceMockable(ISupplicantIface iface) {
return mISupplicantStaIfaceMock;
}
+
+ @Override
+ protected SupplicantStaNetworkHal getStaNetworkMockable(
+ ISupplicantStaNetwork iSupplicantStaNetwork, HandlerThread handlerThread) {
+ return mSupplicantStaNetworkMock;
+ }
}
@Before
@@ -97,6 +117,7 @@ public class SupplicantStaIfaceHalTest {
MockitoAnnotations.initMocks(this);
mTestLooper = new TestLooper();
mStatusSuccess = createSupplicantStatus(SupplicantStatusCode.SUCCESS);
+ mStatusFailure = createSupplicantStatus(SupplicantStatusCode.FAILURE_UNKNOWN);
mStaIface = createIfaceInfo(IfaceType.STA, "wlan0");
mP2pIface = createIfaceInfo(IfaceType.P2P, "p2p0");
@@ -149,12 +170,206 @@ public class SupplicantStaIfaceHalTest {
}
/**
+ * Tests the loading of networks using {@link SupplicantStaNetworkHal}.
+ * Fills up only the SSID field of configs and uses it as a configKey as well.
+ */
+ @Test
+ public void testLoadNetworks() throws Exception {
+ executeAndValidateInitializationSequence(false, false, false);
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(ISupplicantStaIface.listNetworksCallback cb) {
+ cb.onValues(mStatusSuccess, new ArrayList<>(NETWORK_ID_TO_SSID.keySet()));
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .listNetworks(any(ISupplicantStaIface.listNetworksCallback.class));
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(final int networkId, ISupplicantStaIface.getNetworkCallback cb) {
+ // Reset the |mSupplicantStaNetwork| mock for each network.
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public boolean answer(
+ WifiConfiguration config, Map<String, String> networkExtra) {
+ config.SSID = NETWORK_ID_TO_SSID.get(networkId);
+ config.networkId = networkId;
+ networkExtra.put(
+ SupplicantStaNetworkHal.ID_STRING_KEY_CONFIG_KEY, config.SSID);
+ return true;
+ }
+ }).when(mSupplicantStaNetworkMock)
+ .loadWifiConfiguration(any(WifiConfiguration.class), any(Map.class));
+ cb.onValues(mStatusSuccess, mock(ISupplicantStaNetwork.class));
+ return;
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .getNetwork(anyInt(), any(ISupplicantStaIface.getNetworkCallback.class));
+
+ Map<String, WifiConfiguration> configs = new HashMap<>();
+ SparseArray<Map<String, String>> extras = new SparseArray<>();
+ assertTrue(mDut.loadNetworks(configs, extras));
+
+ assertEquals(3, configs.size());
+ assertEquals(3, extras.size());
+ for (Map.Entry<Integer, String> network : NETWORK_ID_TO_SSID.entrySet()) {
+ WifiConfiguration config = configs.get(network.getValue());
+ assertTrue(config != null);
+ assertEquals(network.getKey(), Integer.valueOf(config.networkId));
+ assertEquals(network.getValue(), config.SSID);
+ assertEquals(IpConfiguration.IpAssignment.DHCP, config.getIpAssignment());
+ assertEquals(IpConfiguration.ProxySettings.NONE, config.getProxySettings());
+ }
+ }
+
+ /**
+ * Tests the loading of networks using {@link SupplicantStaNetworkHal} removes any networks
+ * with duplicate config key.
+ * Fills up only the SSID field of configs and uses it as a configKey as well.
+ */
+ @Test
+ public void testLoadNetworksRemovesDuplicates() throws Exception {
+ // Network ID which will have the same config key as the previous one.
+ final int duplicateNetworkId = 2;
+ final int toRemoveNetworkId = duplicateNetworkId - 1;
+ executeAndValidateInitializationSequence(false, false, false);
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(ISupplicantStaIface.listNetworksCallback cb) {
+ cb.onValues(mStatusSuccess, new ArrayList<>(NETWORK_ID_TO_SSID.keySet()));
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .listNetworks(any(ISupplicantStaIface.listNetworksCallback.class));
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public SupplicantStatus answer(int id) {
+ return mStatusSuccess;
+ }
+ }).when(mISupplicantStaIfaceMock).removeNetwork(eq(toRemoveNetworkId));
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(final int networkId, ISupplicantStaIface.getNetworkCallback cb) {
+ // Reset the |mSupplicantStaNetwork| mock for each network.
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public boolean answer(
+ WifiConfiguration config, Map<String, String> networkExtra) {
+ config.SSID = NETWORK_ID_TO_SSID.get(networkId);
+ config.networkId = networkId;
+ // Duplicate network gets the same config key as the to removed one.
+ if (networkId == duplicateNetworkId) {
+ networkExtra.put(
+ SupplicantStaNetworkHal.ID_STRING_KEY_CONFIG_KEY,
+ NETWORK_ID_TO_SSID.get(toRemoveNetworkId));
+ } else {
+ networkExtra.put(
+ SupplicantStaNetworkHal.ID_STRING_KEY_CONFIG_KEY,
+ NETWORK_ID_TO_SSID.get(networkId));
+ }
+ return true;
+ }
+ }).when(mSupplicantStaNetworkMock)
+ .loadWifiConfiguration(any(WifiConfiguration.class), any(Map.class));
+ cb.onValues(mStatusSuccess, mock(ISupplicantStaNetwork.class));
+ return;
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .getNetwork(anyInt(), any(ISupplicantStaIface.getNetworkCallback.class));
+
+ Map<String, WifiConfiguration> configs = new HashMap<>();
+ SparseArray<Map<String, String>> extras = new SparseArray<>();
+ assertTrue(mDut.loadNetworks(configs, extras));
+
+ assertEquals(2, configs.size());
+ assertEquals(2, extras.size());
+ for (Map.Entry<Integer, String> network : NETWORK_ID_TO_SSID.entrySet()) {
+ if (network.getKey() == toRemoveNetworkId) {
+ continue;
+ }
+ WifiConfiguration config;
+ // Duplicate network gets the same config key as the to removed one. So, use that to
+ // lookup the map.
+ if (network.getKey() == duplicateNetworkId) {
+ config = configs.get(NETWORK_ID_TO_SSID.get(toRemoveNetworkId));
+ } else {
+ config = configs.get(network.getValue());
+ }
+ assertTrue(config != null);
+ assertEquals(network.getKey(), Integer.valueOf(config.networkId));
+ assertEquals(network.getValue(), config.SSID);
+ assertEquals(IpConfiguration.IpAssignment.DHCP, config.getIpAssignment());
+ assertEquals(IpConfiguration.ProxySettings.NONE, config.getProxySettings());
+ }
+ }
+
+ /**
+ * Tests the failure to load networks because of listNetworks failure.
+ */
+ @Test
+ public void testLoadNetworksFailedDueToListNetworks() throws Exception {
+ executeAndValidateInitializationSequence(false, false, false);
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(ISupplicantStaIface.listNetworksCallback cb) {
+ cb.onValues(mStatusFailure, null);
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .listNetworks(any(ISupplicantStaIface.listNetworksCallback.class));
+
+ Map<String, WifiConfiguration> configs = new HashMap<>();
+ SparseArray<Map<String, String>> extras = new SparseArray<>();
+ assertFalse(mDut.loadNetworks(configs, extras));
+ }
+
+ /**
+ * Tests the failure to load networks because of getNetwork failure.
+ */
+ @Test
+ public void testLoadNetworksFailedDueToGetNetwork() throws Exception {
+ executeAndValidateInitializationSequence(false, false, false);
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(ISupplicantStaIface.listNetworksCallback cb) {
+ cb.onValues(mStatusSuccess, new ArrayList<>(NETWORK_ID_TO_SSID.keySet()));
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .listNetworks(any(ISupplicantStaIface.listNetworksCallback.class));
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(final int networkId, ISupplicantStaIface.getNetworkCallback cb) {
+ cb.onValues(mStatusFailure, mock(ISupplicantStaNetwork.class));
+ return;
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .getNetwork(anyInt(), any(ISupplicantStaIface.getNetworkCallback.class));
+
+ Map<String, WifiConfiguration> configs = new HashMap<>();
+ SparseArray<Map<String, String>> extras = new SparseArray<>();
+ assertFalse(mDut.loadNetworks(configs, extras));
+ }
+
+ /**
+ * Tests the failure to load networks because of loadWifiConfiguration failure.
+ */
+ @Test
+ public void testLoadNetworksFailedDueToLoadWifiConfiguration() throws Exception {
+ executeAndValidateInitializationSequence(false, false, false);
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public void answer(ISupplicantStaIface.listNetworksCallback cb) {
+ cb.onValues(mStatusSuccess, new ArrayList<>(NETWORK_ID_TO_SSID.keySet()));
+ }
+ }).when(mISupplicantStaIfaceMock)
+ .listNetworks(any(ISupplicantStaIface.listNetworksCallback.class));
+ doAnswer(new MockAnswerUtil.AnswerWithArguments() {
+ public boolean answer(WifiConfiguration config, Map<String, String> networkExtra) {
+ return false;
+ }
+ }).when(mSupplicantStaNetworkMock)
+ .loadWifiConfiguration(any(WifiConfiguration.class), any(Map.class));
+
+ Map<String, WifiConfiguration> configs = new HashMap<>();
+ SparseArray<Map<String, String>> extras = new SparseArray<>();
+ assertFalse(mDut.loadNetworks(configs, extras));
+ }
+
+ /**
* Calls.initialize(), mocking various call back answers and verifying flow, asserting for the
* expected result. Verifies if ISupplicantStaIface manager is initialized or reset.
* Each of the arguments will cause a different failure mode when set true.
*/
private void executeAndValidateInitializationSequence(boolean causeRemoteException,
- boolean getZeroInterfaces, boolean getNullInterface) throws Exception {
+ boolean getZeroInterfaces,
+ boolean getNullInterface)
+ throws Exception {
boolean shouldSucceed = !causeRemoteException && !getZeroInterfaces && !getNullInterface;
// Setup callback mock answers
ArrayList<ISupplicant.IfaceInfo> interfaces;
@@ -193,11 +408,10 @@ public class SupplicantStaIfaceHalTest {
if (!getZeroInterfaces) {
mInOrder.verify(mISupplicantMock)
.getInterface(any(ISupplicant.IfaceInfo.class),
- any(ISupplicant.getInterfaceCallback.class));
+ any(ISupplicant.getInterfaceCallback.class));
}
}
-
private SupplicantStatus createSupplicantStatus(int code) {
SupplicantStatus status = new SupplicantStatus();
status.code = code;