Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.server.wifi;
     17 
     18 import static org.junit.Assert.*;
     19 import static org.mockito.Matchers.eq;
     20 import static org.mockito.Mockito.any;
     21 import static org.mockito.Mockito.anyString;
     22 import static org.mockito.Mockito.doAnswer;
     23 import static org.mockito.Mockito.never;
     24 import static org.mockito.Mockito.verify;
     25 import static org.mockito.Mockito.when;
     26 
     27 import android.app.test.MockAnswerUtil.AnswerWithArguments;
     28 import android.content.Context;
     29 import android.hardware.wifi.supplicant.V1_0.ISupplicantNetwork;
     30 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetwork;
     31 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetworkCallback;
     32 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetworkCallback
     33         .NetworkRequestEapSimGsmAuthParams;
     34 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetworkCallback
     35         .NetworkRequestEapSimUmtsAuthParams;
     36 import android.hardware.wifi.supplicant.V1_0.SupplicantStatus;
     37 import android.hardware.wifi.supplicant.V1_0.SupplicantStatusCode;
     38 import android.net.wifi.WifiConfiguration;
     39 import android.net.wifi.WifiEnterpriseConfig;
     40 import android.os.RemoteException;
     41 import android.text.TextUtils;
     42 
     43 import com.android.internal.R;
     44 import com.android.server.wifi.util.NativeUtil;
     45 
     46 import org.junit.Before;
     47 import org.junit.Test;
     48 import org.mockito.Mock;
     49 import org.mockito.MockitoAnnotations;
     50 
     51 import java.util.ArrayList;
     52 import java.util.HashMap;
     53 import java.util.Map;
     54 import java.util.Random;
     55 
     56 /**
     57  * Unit tests for SupplicantStaNetworkHal
     58  */
     59 public class SupplicantStaNetworkHalTest {
     60     private static final String IFACE_NAME = "wlan0";
     61     private static final Map<String, String> NETWORK_EXTRAS_VALUES = new HashMap<>();
     62     static {
     63         NETWORK_EXTRAS_VALUES.put("key1", "value1");
     64         NETWORK_EXTRAS_VALUES.put("key2", "value2");
     65     }
     66     private static final String NETWORK_EXTRAS_SERIALIZED =
     67             "%7B%22key1%22%3A%22value1%22%2C%22key2%22%3A%22value2%22%7D";
     68     private static final String ANONYMOUS_IDENTITY = "aaa (at) bbb.cc.ddd";
     69 
     70     private SupplicantStaNetworkHal mSupplicantNetwork;
     71     private SupplicantStatus mStatusSuccess;
     72     private SupplicantStatus mStatusFailure;
     73     @Mock private ISupplicantStaNetwork mISupplicantStaNetworkMock;
     74     @Mock
     75     private android.hardware.wifi.supplicant.V1_1.ISupplicantStaNetwork mISupplicantStaNetworkV11;
     76     @Mock private Context mContext;
     77     @Mock private WifiMonitor mWifiMonitor;
     78 
     79     private SupplicantNetworkVariables mSupplicantVariables;
     80     private MockResources mResources;
     81     private ISupplicantStaNetworkCallback mISupplicantStaNetworkCallback;
     82 
     83     /**
     84      * Spy used to return the V1_1 ISupplicantStaNetwork mock object to simulate the 1.1 HAL running
     85      * on the device.
     86      */
     87     private class SupplicantStaNetworkHalSpyV1_1 extends SupplicantStaNetworkHal {
     88         SupplicantStaNetworkHalSpyV1_1(ISupplicantStaNetwork iSupplicantStaNetwork,
     89                 String ifaceName,
     90                 Context context, WifiMonitor monitor) {
     91             super(iSupplicantStaNetwork, ifaceName, context, monitor);
     92         }
     93         @Override
     94         protected android.hardware.wifi.supplicant.V1_1.ISupplicantStaNetwork
     95         getSupplicantStaNetworkForV1_1Mockable() {
     96             return mISupplicantStaNetworkV11;
     97         }
     98     }
     99 
    100     @Before
    101     public void setUp() throws Exception {
    102         MockitoAnnotations.initMocks(this);
    103         mStatusSuccess = createSupplicantStatus(SupplicantStatusCode.SUCCESS);
    104         mStatusFailure = createSupplicantStatus(SupplicantStatusCode.FAILURE_UNKNOWN);
    105         mSupplicantVariables = new SupplicantNetworkVariables();
    106         setupISupplicantNetworkMock();
    107 
    108         mResources = new MockResources();
    109         when(mContext.getResources()).thenReturn(mResources);
    110         createSupplicantStaNetwork();
    111     }
    112 
    113     /**
    114      * Tests the saving of WifiConfiguration to wpa_supplicant.
    115      */
    116     @Test
    117     public void testOpenNetworkWifiConfigurationSaveLoad() throws Exception {
    118         WifiConfiguration config = WifiConfigurationTestUtil.createOpenHiddenNetwork();
    119         config.updateIdentifier = "45";
    120         testWifiConfigurationSaveLoad(config);
    121     }
    122 
    123     /**
    124      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with psk passphrase.
    125      */
    126     @Test
    127     public void testPskPassphraseNetworkWifiConfigurationSaveLoad() throws Exception {
    128         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    129         config.requirePMF = true;
    130         testWifiConfigurationSaveLoad(config);
    131         verify(mISupplicantStaNetworkMock).setPskPassphrase(anyString());
    132         verify(mISupplicantStaNetworkMock)
    133                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
    134         verify(mISupplicantStaNetworkMock, never()).setPsk(any(byte[].class));
    135         verify(mISupplicantStaNetworkMock, never())
    136                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
    137     }
    138 
    139     /**
    140      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with raw psk.
    141      */
    142     @Test
    143     public void testPskNetworkWifiConfigurationSaveLoad() throws Exception {
    144         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    145         config.preSharedKey = "945ef00c463c2a7c2496376b13263d1531366b46377179a4b17b393687450779";
    146         testWifiConfigurationSaveLoad(config);
    147         verify(mISupplicantStaNetworkMock).setPsk(any(byte[].class));
    148         verify(mISupplicantStaNetworkMock)
    149                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
    150         verify(mISupplicantStaNetworkMock, never()).setPskPassphrase(anyString());
    151         verify(mISupplicantStaNetworkMock)
    152                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
    153     }
    154 
    155     /**
    156      * Tests the saving of WifiConfiguration to wpa_supplicant removes enclosing quotes of psk
    157      * passphrase
    158      */
    159     @Test
    160     public void testPskNetworkWifiConfigurationSaveRemovesPskQuotes() throws Exception {
    161         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    162         config.preSharedKey = "\"quoted_psd\"";
    163         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    164         assertEquals(mSupplicantVariables.pskPassphrase,
    165                 NativeUtil.removeEnclosingQuotes(config.preSharedKey));
    166     }
    167 
    168     /**
    169      * Tests the saving/loading of WifiConfiguration to wpa_supplicant.
    170      */
    171     @Test
    172     public void testWepNetworkWifiConfigurationSaveLoad() throws Exception {
    173         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    174         config.BSSID = "34:45:19:09:45";
    175         testWifiConfigurationSaveLoad(config);
    176     }
    177 
    178     /**
    179      * Tests the saving of WifiConfiguration to wpa_supplicant.
    180      */
    181     @Test
    182     public void testEapPeapGtcNetworkWifiConfigurationSaveLoad() throws Exception {
    183         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    184         config.enterpriseConfig =
    185                 WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
    186         testWifiConfigurationSaveLoad(config);
    187     }
    188 
    189     /**
    190      * Tests the saving of WifiConfiguration to wpa_supplicant.
    191      */
    192     @Test
    193     public void testEapTlsNoneNetworkWifiConfigurationSaveLoad() throws Exception {
    194         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    195         config.enterpriseConfig =
    196                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
    197         testWifiConfigurationSaveLoad(config);
    198     }
    199 
    200     /**
    201      * Tests the saving of WifiConfiguration to wpa_supplicant.
    202      */
    203     @Test
    204     public void testEapTlsNoneClientCertNetworkWifiConfigurationSaveLoad() throws Exception {
    205         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    206         config.enterpriseConfig =
    207                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
    208         config.enterpriseConfig.setClientCertificateAlias("test_alias");
    209         testWifiConfigurationSaveLoad(config);
    210     }
    211 
    212     /**
    213      * Tests the saving of WifiConfiguration to wpa_supplicant.
    214      */
    215     @Test
    216     public void testEapTlsAkaNetworkWifiConfigurationSaveLoad() throws Exception {
    217         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    218         config.enterpriseConfig =
    219                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithAkaPhase2();
    220         testWifiConfigurationSaveLoad(config);
    221     }
    222 
    223     /**
    224      * Tests the loading of network ID.
    225      */
    226     @Test
    227     public void testNetworkIdLoad() throws Exception {
    228         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    229         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    230 
    231         // Modify the supplicant variable directly.
    232         mSupplicantVariables.networkId = 5;
    233         WifiConfiguration loadConfig = new WifiConfiguration();
    234         Map<String, String> networkExtras = new HashMap<>();
    235         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    236         assertEquals(mSupplicantVariables.networkId, loadConfig.networkId);
    237     }
    238 
    239     /**
    240      * Tests the failure to load ssid aborts the loading of network variables.
    241      */
    242     @Test
    243     public void testSsidLoadFailure() throws Exception {
    244         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    245         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    246 
    247         doAnswer(new AnswerWithArguments() {
    248             public void answer(ISupplicantStaNetwork.getSsidCallback cb) throws RemoteException {
    249                 cb.onValues(mStatusFailure, mSupplicantVariables.ssid);
    250             }
    251         }).when(mISupplicantStaNetworkMock)
    252                 .getSsid(any(ISupplicantStaNetwork.getSsidCallback.class));
    253 
    254         WifiConfiguration loadConfig = new WifiConfiguration();
    255         Map<String, String> networkExtras = new HashMap<>();
    256         assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    257     }
    258 
    259     /**
    260      * Tests the failure to save ssid.
    261      */
    262     @Test
    263     public void testSsidSaveFailure() throws Exception {
    264         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    265 
    266         doAnswer(new AnswerWithArguments() {
    267             public SupplicantStatus answer(ArrayList<Byte> ssid) throws RemoteException {
    268                 return mStatusFailure;
    269             }
    270         }).when(mISupplicantStaNetworkMock).setSsid(any(ArrayList.class));
    271 
    272         assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
    273     }
    274 
    275     /**
    276      * Tests the failure to save invalid key mgmt (unknown bit set in the
    277      * {@link WifiConfiguration#allowedKeyManagement} being saved).
    278      */
    279     @Test
    280     public void testInvalidKeyMgmtSaveFailure() throws Exception {
    281         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    282         config.allowedKeyManagement.set(10);
    283         try {
    284             assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
    285         } catch (IllegalArgumentException e) {
    286             return;
    287         }
    288         assertTrue(false);
    289     }
    290 
    291     /**
    292      * Tests the failure to load invalid key mgmt (unkown bit set in the supplicant network key_mgmt
    293      * variable read).
    294      */
    295     @Test
    296     public void testInvalidKeyMgmtLoadFailure() throws Exception {
    297         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    298         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    299 
    300         // Modify the supplicant variable directly.
    301         mSupplicantVariables.keyMgmtMask = 0xFFFFF;
    302         WifiConfiguration loadConfig = new WifiConfiguration();
    303         Map<String, String> networkExtras = new HashMap<>();
    304         try {
    305             assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    306         } catch (IllegalArgumentException e) {
    307             return;
    308         }
    309         assertTrue(false);
    310     }
    311 
    312     /**
    313      * Tests the failure to save invalid bssid (less than 6 bytes in the
    314      * {@link WifiConfiguration#BSSID} being saved).
    315      */
    316     @Test
    317     public void testInvalidBssidSaveFailure() throws Exception {
    318         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    319         config.getNetworkSelectionStatus().setNetworkSelectionBSSID("45:34:23:12");
    320         try {
    321             assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
    322         } catch (IllegalArgumentException e) {
    323             return;
    324         }
    325         assertTrue(false);
    326     }
    327 
    328     /**
    329      * Tests the failure to load invalid bssid (less than 6 bytes in the supplicant bssid variable
    330      * read).
    331      */
    332     @Test
    333     public void testInvalidBssidLoadFailure() throws Exception {
    334         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    335         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    336 
    337         // Modify the supplicant variable directly.
    338         mSupplicantVariables.bssid = new byte[3];
    339         WifiConfiguration loadConfig = new WifiConfiguration();
    340         Map<String, String> networkExtras = new HashMap<>();
    341         try {
    342             assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    343         } catch (IllegalArgumentException e) {
    344             return;
    345         }
    346         assertTrue(false);
    347     }
    348 
    349     /**
    350      * Tests the loading of invalid ssid from wpa_supplicant.
    351      */
    352     @Test
    353     public void testInvalidSsidLoadFailure() throws Exception {
    354         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
    355         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    356 
    357         // Modify the supplicant variable directly.
    358         mSupplicantVariables.ssid = null;
    359 
    360         WifiConfiguration loadConfig = new WifiConfiguration();
    361         Map<String, String> networkExtras = new HashMap<>();
    362         assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    363     }
    364 
    365     /**
    366      * Tests the loading of invalid eap method from wpa_supplicant.
    367      * Invalid eap method is assumed to be a non enterprise network. So, the loading should
    368      * succeed as a non-enterprise network.
    369      */
    370     @Test
    371     public void testInvalidEapMethodLoadFailure() throws Exception {
    372         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    373         config.enterpriseConfig =
    374                 WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
    375         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    376 
    377         // Modify the supplicant variable directly.
    378         mSupplicantVariables.eapMethod = -1;
    379 
    380         WifiConfiguration loadConfig = new WifiConfiguration();
    381         Map<String, String> networkExtras = new HashMap<>();
    382         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    383     }
    384 
    385     /**
    386      * Tests the loading of invalid eap phase2 method from wpa_supplicant.
    387      */
    388     @Test
    389     public void testInvalidEapPhase2MethodLoadFailure() throws Exception {
    390         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    391         config.enterpriseConfig =
    392                 WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
    393         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    394 
    395         // Modify the supplicant variable directly.
    396         mSupplicantVariables.eapPhase2Method = -1;
    397 
    398         WifiConfiguration loadConfig = new WifiConfiguration();
    399         Map<String, String> networkExtras = new HashMap<>();
    400         assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    401     }
    402 
    403     /**
    404      * Tests the parsing of GSM auth response parameters.
    405      */
    406     @Test
    407     public void testSendNetworkEapSimGsmAuthResponseWith2KcSresPair() throws Exception {
    408         final byte[] kc = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
    409         final byte[] sres = new byte[]{0x12, 0x10, 0x32, 0x23};
    410         // Send 2 kc/sres pair for this request.
    411         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc)
    412                 + ":" + NativeUtil.hexStringFromByteArray(sres)
    413                 + ":" + NativeUtil.hexStringFromByteArray(kc)
    414                 + ":" + NativeUtil.hexStringFromByteArray(sres);
    415 
    416         doAnswer(new AnswerWithArguments() {
    417             public SupplicantStatus answer(
    418                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
    419                     throws RemoteException {
    420                 assertEquals(2, params.size());
    421                 assertArrayEquals(kc, params.get(0).kc);
    422                 assertArrayEquals(sres, params.get(0).sres);
    423                 assertArrayEquals(kc, params.get(1).kc);
    424                 assertArrayEquals(sres, params.get(1).sres);
    425                 return mStatusSuccess;
    426             }
    427         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
    428 
    429         assertTrue(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
    430     }
    431 
    432     /**
    433      * Tests the parsing of GSM auth response parameters.
    434      */
    435     @Test
    436     public void testSendNetworkEapSimGsmAuthResponseWith3KcSresPair() throws Exception {
    437         final byte[] kc1 = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
    438         final byte[] sres1 = new byte[]{0x12, 0x10, 0x32, 0x23};
    439         final byte[] kc2 = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
    440         final byte[] sres2 = new byte[]{0x12, 0x23, 0x12, 0x23};
    441         final byte[] kc3 = new byte[]{0x25, 0x34, 0x12, 0x14, 0x45, 0x10, 0x34, 0x12};
    442         final byte[] sres3 = new byte[]{0x42, 0x23, 0x22, 0x23};
    443         // Send 3 kc/sres pair for this request.
    444         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc1)
    445                 + ":" + NativeUtil.hexStringFromByteArray(sres1)
    446                 + ":" + NativeUtil.hexStringFromByteArray(kc2)
    447                 + ":" + NativeUtil.hexStringFromByteArray(sres2)
    448                 + ":" + NativeUtil.hexStringFromByteArray(kc3)
    449                 + ":" + NativeUtil.hexStringFromByteArray(sres3);
    450 
    451         doAnswer(new AnswerWithArguments() {
    452             public SupplicantStatus answer(
    453                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
    454                     throws RemoteException {
    455                 assertEquals(3, params.size());
    456                 assertArrayEquals(kc1, params.get(0).kc);
    457                 assertArrayEquals(sres1, params.get(0).sres);
    458                 assertArrayEquals(kc2, params.get(1).kc);
    459                 assertArrayEquals(sres2, params.get(1).sres);
    460                 assertArrayEquals(kc3, params.get(2).kc);
    461                 assertArrayEquals(sres3, params.get(2).sres);
    462                 return mStatusSuccess;
    463             }
    464         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
    465 
    466         assertTrue(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
    467     }
    468 
    469     /**
    470      * Tests the parsing of invalid GSM auth response parameters (invalid kc & sres lengths).
    471      */
    472     @Test
    473     public void testSendInvalidKcSresLenNetworkEapSimGsmAuthResponse() throws Exception {
    474         final byte[] kc1 = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34};
    475         final byte[] sres1 = new byte[]{0x12, 0x10, 0x23};
    476         final byte[] kc2 = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
    477         final byte[] sres2 = new byte[]{0x12, 0x23, 0x12, 0x23};
    478         // Send 2 kc/sres pair for this request.
    479         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc1)
    480                 + ":" + NativeUtil.hexStringFromByteArray(sres1)
    481                 + ":" + NativeUtil.hexStringFromByteArray(kc2)
    482                 + ":" + NativeUtil.hexStringFromByteArray(sres2);
    483 
    484         doAnswer(new AnswerWithArguments() {
    485             public SupplicantStatus answer(
    486                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
    487                     throws RemoteException {
    488                 return mStatusSuccess;
    489             }
    490         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
    491 
    492         assertFalse(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
    493     }
    494 
    495     /**
    496      * Tests the parsing of invalid GSM auth response parameters (invalid number of kc/sres pairs).
    497      */
    498     @Test
    499     public void testSendInvalidKcSresPairNumNetworkEapSimGsmAuthResponse() throws Exception {
    500         final byte[] kc = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
    501         final byte[] sres = new byte[]{0x12, 0x23, 0x12, 0x23};
    502         // Send 1 kc/sres pair for this request.
    503         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc)
    504                 + ":" + NativeUtil.hexStringFromByteArray(sres);
    505 
    506         doAnswer(new AnswerWithArguments() {
    507             public SupplicantStatus answer(
    508                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
    509                     throws RemoteException {
    510                 return mStatusSuccess;
    511             }
    512         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
    513 
    514         assertFalse(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
    515     }
    516 
    517     /**
    518      * Tests the parsing of UMTS auth response parameters.
    519      */
    520     @Test
    521     public void testSendNetworkEapSimUmtsAuthResponse() throws Exception {
    522         final byte[] ik = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
    523                 0x33, 0x23, 0x34, 0x10, 0x40, 0x34};
    524         final byte[] ck = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
    525                 0x33, 0x23, 0x34, 0x10, 0x40, 0x34};
    526         final byte[] res = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34};
    527         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(ik)
    528                 + ":" + NativeUtil.hexStringFromByteArray(ck)
    529                 + ":" + NativeUtil.hexStringFromByteArray(res);
    530 
    531         doAnswer(new AnswerWithArguments() {
    532             public SupplicantStatus answer(
    533                     ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams params)
    534                     throws RemoteException {
    535                 assertArrayEquals(ik, params.ik);
    536                 assertArrayEquals(ck, params.ck);
    537                 // Convert to arraylist before comparison.
    538                 ArrayList<Byte> resList = new ArrayList<>();
    539                 for (byte b : res) {
    540                     resList.add(b);
    541                 }
    542                 assertEquals(resList, params.res);
    543                 return mStatusSuccess;
    544             }
    545         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAuthResponse(
    546                 any(ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams.class));
    547 
    548         assertTrue(mSupplicantNetwork.sendNetworkEapSimUmtsAuthResponse(paramsStr));
    549     }
    550 
    551     /**
    552      * Tests the parsing of invalid UMTS auth response parameters (invalid ik, ck lengths).
    553      */
    554     @Test
    555     public void testSendInvalidNetworkEapSimUmtsAuthResponse() throws Exception {
    556         final byte[] ik = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
    557         final byte[] ck = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
    558                 0x33, 0x23, 0x34, 0x10, 0x40};
    559         final byte[] res = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34};
    560         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(ik)
    561                 + ":" + NativeUtil.hexStringFromByteArray(ck)
    562                 + ":" + NativeUtil.hexStringFromByteArray(res);
    563 
    564         doAnswer(new AnswerWithArguments() {
    565             public SupplicantStatus answer(
    566                     ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams params)
    567                     throws RemoteException {
    568                 return mStatusSuccess;
    569             }
    570         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAuthResponse(
    571                 any(ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams.class));
    572 
    573         assertFalse(mSupplicantNetwork.sendNetworkEapSimUmtsAuthResponse(paramsStr));
    574     }
    575 
    576     /**
    577      * Tests the parsing of UMTS auts response parameters.
    578      */
    579     @Test
    580     public void testSendNetworkEapSimUmtsAutsResponse() throws Exception {
    581         final byte[] auts = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
    582                 0x33, 0x23, 0x34, 0x10};
    583         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(auts);
    584 
    585         doAnswer(new AnswerWithArguments() {
    586             public SupplicantStatus answer(byte[] params)
    587                     throws RemoteException {
    588                 assertArrayEquals(auts, params);
    589                 return mStatusSuccess;
    590             }
    591         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAutsResponse(any(byte[].class));
    592 
    593         assertTrue(mSupplicantNetwork.sendNetworkEapSimUmtsAutsResponse(paramsStr));
    594     }
    595 
    596     /**
    597      * Tests the parsing of invalid UMTS auts response parameters (invalid auts length).
    598      */
    599     @Test
    600     public void testSendInvalidNetworkEapSimUmtsAutsResponse() throws Exception {
    601         final byte[] auts = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23};
    602         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(auts);
    603 
    604         doAnswer(new AnswerWithArguments() {
    605             public SupplicantStatus answer(byte[] params)
    606                     throws RemoteException {
    607                 assertArrayEquals(auts, params);
    608                 return mStatusSuccess;
    609             }
    610         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAutsResponse(any(byte[].class));
    611 
    612         assertFalse(mSupplicantNetwork.sendNetworkEapSimUmtsAutsResponse(paramsStr));
    613     }
    614 
    615     /**
    616      * Tests the parsing of identity string.
    617      */
    618     @Test
    619     public void testSendNetworkEapIdentityResponse() throws Exception {
    620         final String identityStr = "test (at) test.com";
    621         final String encryptedIdentityStr = "test2 (at) test.com";
    622         doAnswer(new AnswerWithArguments() {
    623             public SupplicantStatus answer(ArrayList<Byte> identity)
    624                     throws RemoteException {
    625                 assertEquals(identityStr, NativeUtil.stringFromByteArrayList(identity));
    626                 return mStatusSuccess;
    627             }
    628         }).when(mISupplicantStaNetworkMock).sendNetworkEapIdentityResponse(any(ArrayList.class));
    629 
    630         assertTrue(mSupplicantNetwork.sendNetworkEapIdentityResponse(identityStr,
    631                 encryptedIdentityStr));
    632         verify(mISupplicantStaNetworkV11, never()).sendNetworkEapIdentityResponse_1_1(
    633                 any(ArrayList.class), any(ArrayList.class));
    634 
    635         // Now expose the V1.1 ISupplicantStaNetwork
    636         mSupplicantNetwork = new SupplicantStaNetworkHalSpyV1_1(mISupplicantStaNetworkMock,
    637                 IFACE_NAME, mContext, mWifiMonitor);
    638         doAnswer(new AnswerWithArguments() {
    639             public SupplicantStatus answer(ArrayList<Byte> identity,
    640                     ArrayList<Byte> encryptedIdentity)
    641                     throws RemoteException {
    642                 assertEquals(identityStr, NativeUtil.stringFromByteArrayList(identity));
    643                 assertEquals(encryptedIdentityStr,
    644                         NativeUtil.stringFromByteArrayList(encryptedIdentity));
    645                 return mStatusSuccess;
    646             }
    647         }).when(mISupplicantStaNetworkV11).sendNetworkEapIdentityResponse_1_1(any(ArrayList.class),
    648                 any(ArrayList.class));
    649         assertTrue(mSupplicantNetwork.sendNetworkEapIdentityResponse(identityStr,
    650                 encryptedIdentityStr));
    651     }
    652 
    653     /**
    654      * Tests the addition of FT flags when the device supports it.
    655      */
    656     @Test
    657     public void testAddFtPskFlags() throws Exception {
    658         mResources.setBoolean(R.bool.config_wifi_fast_bss_transition_enabled, true);
    659         createSupplicantStaNetwork();
    660 
    661         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    662         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    663 
    664         // Check the supplicant variables to ensure that we have added the FT flags.
    665         assertTrue((mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.FT_PSK)
    666                 == ISupplicantStaNetwork.KeyMgmtMask.FT_PSK);
    667 
    668         WifiConfiguration loadConfig = new WifiConfiguration();
    669         Map<String, String> networkExtras = new HashMap<>();
    670         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    671         // The FT flags should be stripped out when reading it back.
    672         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
    673     }
    674 
    675     /**
    676      * Tests the addition of FT flags when the device supports it.
    677      */
    678     @Test
    679     public void testAddFtEapFlags() throws Exception {
    680         mResources.setBoolean(R.bool.config_wifi_fast_bss_transition_enabled, true);
    681         createSupplicantStaNetwork();
    682 
    683         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    684         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    685 
    686         // Check the supplicant variables to ensure that we have added the FT flags.
    687         assertTrue((mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.FT_EAP)
    688                 == ISupplicantStaNetwork.KeyMgmtMask.FT_EAP);
    689 
    690         WifiConfiguration loadConfig = new WifiConfiguration();
    691         Map<String, String> networkExtras = new HashMap<>();
    692         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    693         // The FT flags should be stripped out when reading it back.
    694         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
    695     }
    696 
    697     /**
    698      * Tests the retrieval of WPS NFC token.
    699      */
    700     @Test
    701     public void testGetWpsNfcConfigurationToken() throws Exception {
    702         final ArrayList<Byte> token = new ArrayList<>();
    703         token.add(Byte.valueOf((byte) 0x45));
    704         token.add(Byte.valueOf((byte) 0x34));
    705 
    706         doAnswer(new AnswerWithArguments() {
    707             public void answer(ISupplicantStaNetwork.getWpsNfcConfigurationTokenCallback cb)
    708                     throws RemoteException {
    709                 cb.onValues(mStatusSuccess, token);
    710             }
    711         }).when(mISupplicantStaNetworkMock)
    712                 .getWpsNfcConfigurationToken(
    713                         any(ISupplicantStaNetwork.getWpsNfcConfigurationTokenCallback.class));
    714 
    715         assertEquals("4534", mSupplicantNetwork.getWpsNfcConfigurationToken());
    716     }
    717 
    718     /**
    719      * Tests that callback registration failure triggers a failure in saving network config.
    720      */
    721     @Test
    722     public void testSaveFailureDueToCallbackReg() throws Exception {
    723         when(mISupplicantStaNetworkMock.registerCallback(any(ISupplicantStaNetworkCallback.class)))
    724                 .thenReturn(mStatusFailure);
    725         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    726         assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
    727     }
    728 
    729     /**
    730      * Tests the network gsm auth callback.
    731      */
    732     @Test
    733     public void testNetworkEapGsmAuthCallback() throws Exception {
    734         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    735         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    736         assertNotNull(mISupplicantStaNetworkCallback);
    737 
    738         // Now trigger eap gsm callback and ensure that the event is broadcast via WifiMonitor.
    739         NetworkRequestEapSimGsmAuthParams params = new NetworkRequestEapSimGsmAuthParams();
    740         Random random = new Random();
    741         byte[] rand1 = new byte[16];
    742         byte[] rand2 = new byte[16];
    743         byte[] rand3 = new byte[16];
    744         random.nextBytes(rand1);
    745         random.nextBytes(rand2);
    746         random.nextBytes(rand3);
    747         params.rands.add(rand1);
    748         params.rands.add(rand2);
    749         params.rands.add(rand3);
    750 
    751         String[] expectedRands = {
    752                 NativeUtil.hexStringFromByteArray(rand1), NativeUtil.hexStringFromByteArray(rand2),
    753                 NativeUtil.hexStringFromByteArray(rand3)
    754         };
    755 
    756         mISupplicantStaNetworkCallback.onNetworkEapSimGsmAuthRequest(params);
    757         verify(mWifiMonitor).broadcastNetworkGsmAuthRequestEvent(
    758                 eq(IFACE_NAME), eq(config.networkId), eq(config.SSID), eq(expectedRands));
    759     }
    760 
    761     /**
    762      * Tests the network umts auth callback.
    763      */
    764     @Test
    765     public void testNetworkEapUmtsAuthCallback() throws Exception {
    766         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    767         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    768         assertNotNull(mISupplicantStaNetworkCallback);
    769 
    770         // Now trigger eap gsm callback and ensure that the event is broadcast via WifiMonitor.
    771         NetworkRequestEapSimUmtsAuthParams params = new NetworkRequestEapSimUmtsAuthParams();
    772         Random random = new Random();
    773         random.nextBytes(params.autn);
    774         random.nextBytes(params.rand);
    775 
    776         String[] expectedRands = {
    777                 NativeUtil.hexStringFromByteArray(params.rand),
    778                 NativeUtil.hexStringFromByteArray(params.autn)
    779         };
    780 
    781         mISupplicantStaNetworkCallback.onNetworkEapSimUmtsAuthRequest(params);
    782         verify(mWifiMonitor).broadcastNetworkUmtsAuthRequestEvent(
    783                 eq(IFACE_NAME), eq(config.networkId), eq(config.SSID), eq(expectedRands));
    784     }
    785 
    786     /**
    787      * Tests the network identity callback.
    788      */
    789     @Test
    790     public void testNetworkIdentityCallback() throws Exception {
    791         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
    792         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    793         assertNotNull(mISupplicantStaNetworkCallback);
    794 
    795         // Now trigger identity request callback and ensure that the event is broadcast via
    796         // WifiMonitor.
    797         mISupplicantStaNetworkCallback.onNetworkEapIdentityRequest();
    798         verify(mWifiMonitor).broadcastNetworkIdentityRequestEvent(
    799                 eq(IFACE_NAME), eq(config.networkId), eq(config.SSID));
    800     }
    801 
    802     private void testWifiConfigurationSaveLoad(WifiConfiguration config) {
    803         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    804         WifiConfiguration loadConfig = new WifiConfiguration();
    805         Map<String, String> networkExtras = new HashMap<>();
    806         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
    807         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
    808         assertEquals(config.configKey(),
    809                 networkExtras.get(SupplicantStaNetworkHal.ID_STRING_KEY_CONFIG_KEY));
    810         assertEquals(
    811                 config.creatorUid,
    812                 Integer.parseInt(networkExtras.get(
    813                         SupplicantStaNetworkHal.ID_STRING_KEY_CREATOR_UID)));
    814         // There is no getter for this one, so check the supplicant variable.
    815         if (!TextUtils.isEmpty(config.updateIdentifier)) {
    816             assertEquals(Integer.parseInt(config.updateIdentifier),
    817                     mSupplicantVariables.updateIdentifier);
    818         }
    819         // There is no getter for this one, so check the supplicant variable.
    820         String oppKeyCaching =
    821                 config.enterpriseConfig.getFieldValue(WifiEnterpriseConfig.OPP_KEY_CACHING);
    822         if (!TextUtils.isEmpty(oppKeyCaching)) {
    823             assertEquals(
    824                     Integer.parseInt(oppKeyCaching) == 1 ? true : false,
    825                     mSupplicantVariables.eapProactiveKeyCaching);
    826         }
    827     }
    828 
    829     /**
    830      * Verifies that createNetworkExtra() & parseNetworkExtra correctly writes a serialized and
    831      * URL-encoded JSON object.
    832      */
    833     @Test
    834     public void testNetworkExtra() {
    835         assertEquals(NETWORK_EXTRAS_SERIALIZED,
    836                 SupplicantStaNetworkHal.createNetworkExtra(NETWORK_EXTRAS_VALUES));
    837         assertEquals(NETWORK_EXTRAS_VALUES,
    838                 SupplicantStaNetworkHal.parseNetworkExtra(NETWORK_EXTRAS_SERIALIZED));
    839     }
    840 
    841     /**
    842      * Verifies that fetachEapAnonymousIdentity() can get the anonymous identity from supplicant.
    843      */
    844     @Test
    845     public void testFetchEapAnonymousIdentity() {
    846         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
    847         config.enterpriseConfig.setAnonymousIdentity(ANONYMOUS_IDENTITY);
    848         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
    849         assertEquals(ANONYMOUS_IDENTITY, mSupplicantNetwork.fetchEapAnonymousIdentity());
    850     }
    851 
    852     /**
    853      * Sets up the HIDL interface mock with all the setters/getter values.
    854      * Note: This only sets up the mock to return success on all methods.
    855      */
    856     private void setupISupplicantNetworkMock() throws Exception {
    857         /** SSID */
    858         doAnswer(new AnswerWithArguments() {
    859             public SupplicantStatus answer(ArrayList<Byte> ssid) throws RemoteException {
    860                 mSupplicantVariables.ssid = ssid;
    861                 return mStatusSuccess;
    862             }
    863         }).when(mISupplicantStaNetworkMock).setSsid(any(ArrayList.class));
    864         doAnswer(new AnswerWithArguments() {
    865             public void answer(ISupplicantStaNetwork.getSsidCallback cb) throws RemoteException {
    866                 cb.onValues(mStatusSuccess, mSupplicantVariables.ssid);
    867             }
    868         }).when(mISupplicantStaNetworkMock)
    869                 .getSsid(any(ISupplicantStaNetwork.getSsidCallback.class));
    870 
    871         /** Network Id */
    872         doAnswer(new AnswerWithArguments() {
    873             public void answer(ISupplicantNetwork.getIdCallback cb) throws RemoteException {
    874                 cb.onValues(mStatusSuccess, mSupplicantVariables.networkId);
    875             }
    876         }).when(mISupplicantStaNetworkMock).getId(any(ISupplicantNetwork.getIdCallback.class));
    877 
    878         /** BSSID */
    879         doAnswer(new AnswerWithArguments() {
    880             public SupplicantStatus answer(byte[] bssid) throws RemoteException {
    881                 mSupplicantVariables.bssid = bssid;
    882                 return mStatusSuccess;
    883             }
    884         }).when(mISupplicantStaNetworkMock).setBssid(any(byte[].class));
    885         doAnswer(new AnswerWithArguments() {
    886             public void answer(ISupplicantStaNetwork.getBssidCallback cb) throws RemoteException {
    887                 cb.onValues(mStatusSuccess, mSupplicantVariables.bssid);
    888             }
    889         }).when(mISupplicantStaNetworkMock)
    890                 .getBssid(any(ISupplicantStaNetwork.getBssidCallback.class));
    891 
    892         /** Scan SSID (Is Hidden Network?) */
    893         doAnswer(new AnswerWithArguments() {
    894             public SupplicantStatus answer(boolean enable) throws RemoteException {
    895                 mSupplicantVariables.scanSsid = enable;
    896                 return mStatusSuccess;
    897             }
    898         }).when(mISupplicantStaNetworkMock).setScanSsid(any(boolean.class));
    899         doAnswer(new AnswerWithArguments() {
    900             public void answer(ISupplicantStaNetwork.getScanSsidCallback cb)
    901                     throws RemoteException {
    902                 cb.onValues(mStatusSuccess, mSupplicantVariables.scanSsid);
    903             }
    904         }).when(mISupplicantStaNetworkMock)
    905                 .getScanSsid(any(ISupplicantStaNetwork.getScanSsidCallback.class));
    906 
    907         /** Require PMF*/
    908         doAnswer(new AnswerWithArguments() {
    909             public SupplicantStatus answer(boolean enable) throws RemoteException {
    910                 mSupplicantVariables.requirePmf = enable;
    911                 return mStatusSuccess;
    912             }
    913         }).when(mISupplicantStaNetworkMock).setRequirePmf(any(boolean.class));
    914         doAnswer(new AnswerWithArguments() {
    915             public void answer(ISupplicantStaNetwork.getRequirePmfCallback cb)
    916                     throws RemoteException {
    917                 cb.onValues(mStatusSuccess, mSupplicantVariables.requirePmf);
    918             }
    919         }).when(mISupplicantStaNetworkMock)
    920                 .getRequirePmf(any(ISupplicantStaNetwork.getRequirePmfCallback.class));
    921 
    922         /** PSK passphrase */
    923         doAnswer(new AnswerWithArguments() {
    924             public SupplicantStatus answer(String pskPassphrase) throws RemoteException {
    925                 mSupplicantVariables.pskPassphrase = pskPassphrase;
    926                 return mStatusSuccess;
    927             }
    928         }).when(mISupplicantStaNetworkMock).setPskPassphrase(any(String.class));
    929         doAnswer(new AnswerWithArguments() {
    930             public void answer(ISupplicantStaNetwork.getPskPassphraseCallback cb)
    931                     throws RemoteException {
    932                 cb.onValues(mStatusSuccess, mSupplicantVariables.pskPassphrase);
    933             }
    934         }).when(mISupplicantStaNetworkMock)
    935                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
    936 
    937         /** PSK */
    938         doAnswer(new AnswerWithArguments() {
    939             public SupplicantStatus answer(byte[] psk) throws RemoteException {
    940                 mSupplicantVariables.psk = psk;
    941                 return mStatusSuccess;
    942             }
    943         }).when(mISupplicantStaNetworkMock).setPsk(any(byte[].class));
    944         doAnswer(new AnswerWithArguments() {
    945             public void answer(ISupplicantStaNetwork.getPskCallback cb)
    946                     throws RemoteException {
    947                 cb.onValues(mStatusSuccess, mSupplicantVariables.psk);
    948             }
    949         }).when(mISupplicantStaNetworkMock)
    950                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
    951 
    952         /** WEP keys **/
    953         doAnswer(new AnswerWithArguments() {
    954             public SupplicantStatus answer(int keyIdx, ArrayList<Byte> key) throws RemoteException {
    955                 mSupplicantVariables.wepKey[keyIdx] = key;
    956                 return mStatusSuccess;
    957             }
    958         }).when(mISupplicantStaNetworkMock).setWepKey(any(int.class), any(ArrayList.class));
    959         doAnswer(new AnswerWithArguments() {
    960             public void answer(int keyIdx, ISupplicantStaNetwork.getWepKeyCallback cb)
    961                     throws RemoteException {
    962                 cb.onValues(mStatusSuccess, mSupplicantVariables.wepKey[keyIdx]);
    963             }
    964         }).when(mISupplicantStaNetworkMock)
    965                 .getWepKey(any(int.class), any(ISupplicantStaNetwork.getWepKeyCallback.class));
    966 
    967         doAnswer(new AnswerWithArguments() {
    968             public SupplicantStatus answer(int keyIdx) throws RemoteException {
    969                 mSupplicantVariables.wepTxKeyIdx = keyIdx;
    970                 return mStatusSuccess;
    971             }
    972         }).when(mISupplicantStaNetworkMock).setWepTxKeyIdx(any(int.class));
    973         doAnswer(new AnswerWithArguments() {
    974             public void answer(ISupplicantStaNetwork.getWepTxKeyIdxCallback cb)
    975                     throws RemoteException {
    976                 cb.onValues(mStatusSuccess, mSupplicantVariables.wepTxKeyIdx);
    977             }
    978         }).when(mISupplicantStaNetworkMock)
    979                 .getWepTxKeyIdx(any(ISupplicantStaNetwork.getWepTxKeyIdxCallback.class));
    980 
    981         /** allowedKeyManagement */
    982         doAnswer(new AnswerWithArguments() {
    983             public SupplicantStatus answer(int mask) throws RemoteException {
    984                 mSupplicantVariables.keyMgmtMask = mask;
    985                 return mStatusSuccess;
    986             }
    987         }).when(mISupplicantStaNetworkMock).setKeyMgmt(any(int.class));
    988         doAnswer(new AnswerWithArguments() {
    989             public void answer(ISupplicantStaNetwork.getKeyMgmtCallback cb) throws RemoteException {
    990                 cb.onValues(mStatusSuccess, mSupplicantVariables.keyMgmtMask);
    991             }
    992         }).when(mISupplicantStaNetworkMock)
    993                 .getKeyMgmt(any(ISupplicantStaNetwork.getKeyMgmtCallback.class));
    994 
    995         /** allowedProtocols */
    996         doAnswer(new AnswerWithArguments() {
    997             public SupplicantStatus answer(int mask) throws RemoteException {
    998                 mSupplicantVariables.protoMask = mask;
    999                 return mStatusSuccess;
   1000             }
   1001         }).when(mISupplicantStaNetworkMock).setProto(any(int.class));
   1002         doAnswer(new AnswerWithArguments() {
   1003             public void answer(ISupplicantStaNetwork.getProtoCallback cb) throws RemoteException {
   1004                 cb.onValues(mStatusSuccess, mSupplicantVariables.protoMask);
   1005             }
   1006         }).when(mISupplicantStaNetworkMock)
   1007                 .getProto(any(ISupplicantStaNetwork.getProtoCallback.class));
   1008 
   1009         /** allowedAuthAlgorithms */
   1010         doAnswer(new AnswerWithArguments() {
   1011             public SupplicantStatus answer(int mask) throws RemoteException {
   1012                 mSupplicantVariables.authAlgMask = mask;
   1013                 return mStatusSuccess;
   1014             }
   1015         }).when(mISupplicantStaNetworkMock).setAuthAlg(any(int.class));
   1016         doAnswer(new AnswerWithArguments() {
   1017             public void answer(ISupplicantStaNetwork.getAuthAlgCallback cb) throws RemoteException {
   1018                 cb.onValues(mStatusSuccess, mSupplicantVariables.authAlgMask);
   1019             }
   1020         }).when(mISupplicantStaNetworkMock)
   1021                 .getAuthAlg(any(ISupplicantStaNetwork.getAuthAlgCallback.class));
   1022 
   1023         /** allowedGroupCiphers */
   1024         doAnswer(new AnswerWithArguments() {
   1025             public SupplicantStatus answer(int mask) throws RemoteException {
   1026                 mSupplicantVariables.groupCipherMask = mask;
   1027                 return mStatusSuccess;
   1028             }
   1029         }).when(mISupplicantStaNetworkMock).setGroupCipher(any(int.class));
   1030         doAnswer(new AnswerWithArguments() {
   1031             public void answer(ISupplicantStaNetwork.getGroupCipherCallback cb)
   1032                     throws RemoteException {
   1033                 cb.onValues(mStatusSuccess, mSupplicantVariables.groupCipherMask);
   1034             }
   1035         }).when(mISupplicantStaNetworkMock)
   1036                 .getGroupCipher(any(ISupplicantStaNetwork.getGroupCipherCallback.class));
   1037 
   1038         /** allowedPairwiseCiphers */
   1039         doAnswer(new AnswerWithArguments() {
   1040             public SupplicantStatus answer(int mask) throws RemoteException {
   1041                 mSupplicantVariables.pairwiseCipherMask = mask;
   1042                 return mStatusSuccess;
   1043             }
   1044         }).when(mISupplicantStaNetworkMock).setPairwiseCipher(any(int.class));
   1045         doAnswer(new AnswerWithArguments() {
   1046             public void answer(ISupplicantStaNetwork.getPairwiseCipherCallback cb)
   1047                     throws RemoteException {
   1048                 cb.onValues(mStatusSuccess, mSupplicantVariables.pairwiseCipherMask);
   1049             }
   1050         }).when(mISupplicantStaNetworkMock)
   1051                 .getPairwiseCipher(any(ISupplicantStaNetwork.getPairwiseCipherCallback.class));
   1052 
   1053         /** metadata: idstr */
   1054         doAnswer(new AnswerWithArguments() {
   1055             public SupplicantStatus answer(String idStr) throws RemoteException {
   1056                 mSupplicantVariables.idStr = idStr;
   1057                 return mStatusSuccess;
   1058             }
   1059         }).when(mISupplicantStaNetworkMock).setIdStr(any(String.class));
   1060         doAnswer(new AnswerWithArguments() {
   1061             public void answer(ISupplicantStaNetwork.getIdStrCallback cb) throws RemoteException {
   1062                 cb.onValues(mStatusSuccess, mSupplicantVariables.idStr);
   1063             }
   1064         }).when(mISupplicantStaNetworkMock)
   1065                 .getIdStr(any(ISupplicantStaNetwork.getIdStrCallback.class));
   1066 
   1067         /** UpdateIdentifier */
   1068         doAnswer(new AnswerWithArguments() {
   1069             public SupplicantStatus answer(int identifier) throws RemoteException {
   1070                 mSupplicantVariables.updateIdentifier = identifier;
   1071                 return mStatusSuccess;
   1072             }
   1073         }).when(mISupplicantStaNetworkMock).setUpdateIdentifier(any(int.class));
   1074 
   1075         /** EAP method */
   1076         doAnswer(new AnswerWithArguments() {
   1077             public SupplicantStatus answer(int method) throws RemoteException {
   1078                 mSupplicantVariables.eapMethod = method;
   1079                 return mStatusSuccess;
   1080             }
   1081         }).when(mISupplicantStaNetworkMock).setEapMethod(any(int.class));
   1082         doAnswer(new AnswerWithArguments() {
   1083             public void answer(ISupplicantStaNetwork.getEapMethodCallback cb)
   1084                     throws RemoteException {
   1085                 // When not set, return failure.
   1086                 if (mSupplicantVariables.eapMethod == -1) {
   1087                     cb.onValues(mStatusFailure, mSupplicantVariables.eapMethod);
   1088                 } else {
   1089                     cb.onValues(mStatusSuccess, mSupplicantVariables.eapMethod);
   1090                 }
   1091             }
   1092         }).when(mISupplicantStaNetworkMock)
   1093                 .getEapMethod(any(ISupplicantStaNetwork.getEapMethodCallback.class));
   1094 
   1095         /** EAP Phase 2 method */
   1096         doAnswer(new AnswerWithArguments() {
   1097             public SupplicantStatus answer(int method) throws RemoteException {
   1098                 mSupplicantVariables.eapPhase2Method = method;
   1099                 return mStatusSuccess;
   1100             }
   1101         }).when(mISupplicantStaNetworkMock).setEapPhase2Method(any(int.class));
   1102         doAnswer(new AnswerWithArguments() {
   1103             public void answer(ISupplicantStaNetwork.getEapPhase2MethodCallback cb)
   1104                     throws RemoteException {
   1105                 // When not set, return failure.
   1106                 if (mSupplicantVariables.eapPhase2Method == -1) {
   1107                     cb.onValues(mStatusFailure, mSupplicantVariables.eapPhase2Method);
   1108                 } else {
   1109                     cb.onValues(mStatusSuccess, mSupplicantVariables.eapPhase2Method);
   1110                 }
   1111             }
   1112         }).when(mISupplicantStaNetworkMock)
   1113                 .getEapPhase2Method(any(ISupplicantStaNetwork.getEapPhase2MethodCallback.class));
   1114 
   1115         /** EAP Identity */
   1116         doAnswer(new AnswerWithArguments() {
   1117             public SupplicantStatus answer(ArrayList<Byte> identity) throws RemoteException {
   1118                 mSupplicantVariables.eapIdentity = identity;
   1119                 return mStatusSuccess;
   1120             }
   1121         }).when(mISupplicantStaNetworkMock).setEapIdentity(any(ArrayList.class));
   1122         doAnswer(new AnswerWithArguments() {
   1123             public void answer(ISupplicantStaNetwork.getEapIdentityCallback cb)
   1124                     throws RemoteException {
   1125                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapIdentity);
   1126             }
   1127         }).when(mISupplicantStaNetworkMock)
   1128                 .getEapIdentity(any(ISupplicantStaNetwork.getEapIdentityCallback.class));
   1129 
   1130         /** EAP Anonymous Identity */
   1131         doAnswer(new AnswerWithArguments() {
   1132             public SupplicantStatus answer(ArrayList<Byte> identity) throws RemoteException {
   1133                 mSupplicantVariables.eapAnonymousIdentity = identity;
   1134                 return mStatusSuccess;
   1135             }
   1136         }).when(mISupplicantStaNetworkMock).setEapAnonymousIdentity(any(ArrayList.class));
   1137         doAnswer(new AnswerWithArguments() {
   1138             public void answer(ISupplicantStaNetwork.getEapAnonymousIdentityCallback cb)
   1139                     throws RemoteException {
   1140                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapAnonymousIdentity);
   1141             }
   1142         }).when(mISupplicantStaNetworkMock)
   1143                 .getEapAnonymousIdentity(
   1144                         any(ISupplicantStaNetwork.getEapAnonymousIdentityCallback.class));
   1145 
   1146         /** EAP Password */
   1147         doAnswer(new AnswerWithArguments() {
   1148             public SupplicantStatus answer(ArrayList<Byte> password) throws RemoteException {
   1149                 mSupplicantVariables.eapPassword = password;
   1150                 return mStatusSuccess;
   1151             }
   1152         }).when(mISupplicantStaNetworkMock).setEapPassword(any(ArrayList.class));
   1153         doAnswer(new AnswerWithArguments() {
   1154             public void answer(ISupplicantStaNetwork.getEapPasswordCallback cb)
   1155                     throws RemoteException {
   1156                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapPassword);
   1157             }
   1158         }).when(mISupplicantStaNetworkMock)
   1159                 .getEapPassword(any(ISupplicantStaNetwork.getEapPasswordCallback.class));
   1160 
   1161         /** EAP Client Cert */
   1162         doAnswer(new AnswerWithArguments() {
   1163             public SupplicantStatus answer(String cert) throws RemoteException {
   1164                 mSupplicantVariables.eapClientCert = cert;
   1165                 return mStatusSuccess;
   1166             }
   1167         }).when(mISupplicantStaNetworkMock).setEapClientCert(any(String.class));
   1168         doAnswer(new AnswerWithArguments() {
   1169             public void answer(ISupplicantStaNetwork.getEapClientCertCallback cb)
   1170                     throws RemoteException {
   1171                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapClientCert);
   1172             }
   1173         }).when(mISupplicantStaNetworkMock)
   1174                 .getEapClientCert(any(ISupplicantStaNetwork.getEapClientCertCallback.class));
   1175 
   1176         /** EAP CA Cert */
   1177         doAnswer(new AnswerWithArguments() {
   1178             public SupplicantStatus answer(String cert) throws RemoteException {
   1179                 mSupplicantVariables.eapCACert = cert;
   1180                 return mStatusSuccess;
   1181             }
   1182         }).when(mISupplicantStaNetworkMock).setEapCACert(any(String.class));
   1183         doAnswer(new AnswerWithArguments() {
   1184             public void answer(ISupplicantStaNetwork.getEapCACertCallback cb)
   1185                     throws RemoteException {
   1186                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapCACert);
   1187             }
   1188         }).when(mISupplicantStaNetworkMock)
   1189                 .getEapCACert(any(ISupplicantStaNetwork.getEapCACertCallback.class));
   1190 
   1191         /** EAP Subject Match */
   1192         doAnswer(new AnswerWithArguments() {
   1193             public SupplicantStatus answer(String match) throws RemoteException {
   1194                 mSupplicantVariables.eapSubjectMatch = match;
   1195                 return mStatusSuccess;
   1196             }
   1197         }).when(mISupplicantStaNetworkMock).setEapSubjectMatch(any(String.class));
   1198         doAnswer(new AnswerWithArguments() {
   1199             public void answer(ISupplicantStaNetwork.getEapSubjectMatchCallback cb)
   1200                     throws RemoteException {
   1201                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapSubjectMatch);
   1202             }
   1203         }).when(mISupplicantStaNetworkMock)
   1204                 .getEapSubjectMatch(any(ISupplicantStaNetwork.getEapSubjectMatchCallback.class));
   1205 
   1206         /** EAP Engine */
   1207         doAnswer(new AnswerWithArguments() {
   1208             public SupplicantStatus answer(boolean enable) throws RemoteException {
   1209                 mSupplicantVariables.eapEngine = enable;
   1210                 return mStatusSuccess;
   1211             }
   1212         }).when(mISupplicantStaNetworkMock).setEapEngine(any(boolean.class));
   1213         doAnswer(new AnswerWithArguments() {
   1214             public void answer(ISupplicantStaNetwork.getEapEngineCallback cb)
   1215                     throws RemoteException {
   1216                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapEngine);
   1217             }
   1218         }).when(mISupplicantStaNetworkMock)
   1219                 .getEapEngine(any(ISupplicantStaNetwork.getEapEngineCallback.class));
   1220 
   1221         /** EAP Engine ID */
   1222         doAnswer(new AnswerWithArguments() {
   1223             public SupplicantStatus answer(String id) throws RemoteException {
   1224                 mSupplicantVariables.eapEngineID = id;
   1225                 return mStatusSuccess;
   1226             }
   1227         }).when(mISupplicantStaNetworkMock).setEapEngineID(any(String.class));
   1228         doAnswer(new AnswerWithArguments() {
   1229             public void answer(ISupplicantStaNetwork.getEapEngineIDCallback cb)
   1230                     throws RemoteException {
   1231                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapEngineID);
   1232             }
   1233         }).when(mISupplicantStaNetworkMock)
   1234                 .getEapEngineID(any(ISupplicantStaNetwork.getEapEngineIDCallback.class));
   1235 
   1236         /** EAP Private Key */
   1237         doAnswer(new AnswerWithArguments() {
   1238             public SupplicantStatus answer(String key) throws RemoteException {
   1239                 mSupplicantVariables.eapPrivateKeyId = key;
   1240                 return mStatusSuccess;
   1241             }
   1242         }).when(mISupplicantStaNetworkMock).setEapPrivateKeyId(any(String.class));
   1243         doAnswer(new AnswerWithArguments() {
   1244             public void answer(ISupplicantStaNetwork.getEapPrivateKeyIdCallback cb)
   1245                     throws RemoteException {
   1246                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapPrivateKeyId);
   1247             }
   1248         }).when(mISupplicantStaNetworkMock)
   1249                 .getEapPrivateKeyId(any(ISupplicantStaNetwork.getEapPrivateKeyIdCallback.class));
   1250 
   1251         /** EAP Alt Subject Match */
   1252         doAnswer(new AnswerWithArguments() {
   1253             public SupplicantStatus answer(String match) throws RemoteException {
   1254                 mSupplicantVariables.eapAltSubjectMatch = match;
   1255                 return mStatusSuccess;
   1256             }
   1257         }).when(mISupplicantStaNetworkMock).setEapAltSubjectMatch(any(String.class));
   1258         doAnswer(new AnswerWithArguments() {
   1259             public void answer(ISupplicantStaNetwork.getEapAltSubjectMatchCallback cb)
   1260                     throws RemoteException {
   1261                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapAltSubjectMatch);
   1262             }
   1263         }).when(mISupplicantStaNetworkMock)
   1264                 .getEapAltSubjectMatch(
   1265                         any(ISupplicantStaNetwork.getEapAltSubjectMatchCallback.class));
   1266 
   1267         /** EAP Domain Suffix Match */
   1268         doAnswer(new AnswerWithArguments() {
   1269             public SupplicantStatus answer(String match) throws RemoteException {
   1270                 mSupplicantVariables.eapDomainSuffixMatch = match;
   1271                 return mStatusSuccess;
   1272             }
   1273         }).when(mISupplicantStaNetworkMock).setEapDomainSuffixMatch(any(String.class));
   1274         doAnswer(new AnswerWithArguments() {
   1275             public void answer(ISupplicantStaNetwork.getEapDomainSuffixMatchCallback cb)
   1276                     throws RemoteException {
   1277                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapDomainSuffixMatch);
   1278             }
   1279         }).when(mISupplicantStaNetworkMock)
   1280                 .getEapDomainSuffixMatch(
   1281                         any(ISupplicantStaNetwork.getEapDomainSuffixMatchCallback.class));
   1282 
   1283         /** EAP CA Path*/
   1284         doAnswer(new AnswerWithArguments() {
   1285             public SupplicantStatus answer(String path) throws RemoteException {
   1286                 mSupplicantVariables.eapCAPath = path;
   1287                 return mStatusSuccess;
   1288             }
   1289         }).when(mISupplicantStaNetworkMock).setEapCAPath(any(String.class));
   1290         doAnswer(new AnswerWithArguments() {
   1291             public void answer(ISupplicantStaNetwork.getEapCAPathCallback cb)
   1292                     throws RemoteException {
   1293                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapCAPath);
   1294             }
   1295         }).when(mISupplicantStaNetworkMock)
   1296                 .getEapCAPath(any(ISupplicantStaNetwork.getEapCAPathCallback.class));
   1297 
   1298         /** EAP Proactive Key Caching */
   1299         doAnswer(new AnswerWithArguments() {
   1300             public SupplicantStatus answer(boolean enable) throws RemoteException {
   1301                 mSupplicantVariables.eapProactiveKeyCaching = enable;
   1302                 return mStatusSuccess;
   1303             }
   1304         }).when(mISupplicantStaNetworkMock).setProactiveKeyCaching(any(boolean.class));
   1305 
   1306         /** Callback registeration */
   1307         doAnswer(new AnswerWithArguments() {
   1308             public SupplicantStatus answer(ISupplicantStaNetworkCallback cb)
   1309                     throws RemoteException {
   1310                 mISupplicantStaNetworkCallback = cb;
   1311                 return mStatusSuccess;
   1312             }
   1313         }).when(mISupplicantStaNetworkMock)
   1314                 .registerCallback(any(ISupplicantStaNetworkCallback.class));
   1315     }
   1316 
   1317     private SupplicantStatus createSupplicantStatus(int code) {
   1318         SupplicantStatus status = new SupplicantStatus();
   1319         status.code = code;
   1320         return status;
   1321     }
   1322 
   1323     /**
   1324      * Need this for tests which wants to manipulate context before creating the instance.
   1325      */
   1326     private void createSupplicantStaNetwork() {
   1327         mSupplicantNetwork =
   1328                 new SupplicantStaNetworkHal(mISupplicantStaNetworkMock, IFACE_NAME, mContext,
   1329                         mWifiMonitor);
   1330     }
   1331 
   1332     // Private class to to store/inspect values set via the HIDL mock.
   1333     private class SupplicantNetworkVariables {
   1334         public ArrayList<Byte> ssid;
   1335         public int networkId;
   1336         public byte[/* 6 */] bssid;
   1337         public int keyMgmtMask;
   1338         public int protoMask;
   1339         public int authAlgMask;
   1340         public int groupCipherMask;
   1341         public int pairwiseCipherMask;
   1342         public boolean scanSsid;
   1343         public boolean requirePmf;
   1344         public String idStr;
   1345         public int updateIdentifier;
   1346         public String pskPassphrase;
   1347         public byte[] psk;
   1348         public ArrayList<Byte>[] wepKey = new ArrayList[4];
   1349         public int wepTxKeyIdx;
   1350         public int eapMethod = -1;
   1351         public int eapPhase2Method = -1;
   1352         public ArrayList<Byte> eapIdentity;
   1353         public ArrayList<Byte> eapAnonymousIdentity;
   1354         public ArrayList<Byte> eapPassword;
   1355         public String eapCACert;
   1356         public String eapCAPath;
   1357         public String eapClientCert;
   1358         public String eapPrivateKeyId;
   1359         public String eapSubjectMatch;
   1360         public String eapAltSubjectMatch;
   1361         public boolean eapEngine;
   1362         public String eapEngineID;
   1363         public String eapDomainSuffixMatch;
   1364         public boolean eapProactiveKeyCaching;
   1365     }
   1366 }
   1367