199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao/* 299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * Copyright (C) 2016 The Android Open Source Project 399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * 499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * Licensed under the Apache License, Version 2.0 (the "License"); 599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * you may not use this file except in compliance with the License. 699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * You may obtain a copy of the License at 799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * 899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * http://www.apache.org/licenses/LICENSE-2.0 999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * 1099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * Unless required by applicable law or agreed to in writing, software 1199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * distributed under the License is distributed on an "AS IS" BASIS, 1299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * See the License for the specific language governing permissions and 1499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * limitations under the License. 1599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao */ 1699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raopackage com.android.server.wifi; 1799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 1899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport static org.junit.Assert.assertEquals; 1999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport static org.junit.Assert.assertTrue; 2099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport static org.mockito.Mockito.*; 2199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 2299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.content.BroadcastReceiver; 2399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.content.Context; 2499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.content.Intent; 2599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.content.IntentFilter; 2699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.net.wifi.SupplicantState; 2799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.net.wifi.WifiManager; 2899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.net.wifi.WifiSsid; 2999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.os.Handler; 3099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.os.Message; 3199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport android.os.test.TestLooper; 3299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 3399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport com.android.internal.app.IBatteryStats; 3499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 3599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport org.junit.Before; 3699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport org.junit.Test; 3799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport org.mockito.Mock; 3899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raoimport org.mockito.MockitoAnnotations; 3999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao/** 4099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * Unit tests for {@link android.net.wifi.SupplicantStateTracker}. 4199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao */ 4299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Raopublic class SupplicantStateTrackerTest { 4399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 4499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private static final String TAG = "SupplicantStateTrackerTest"; 4599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private static final String sSSID = "\"GoogleGuest\""; 4699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private static final WifiSsid sWifiSsid = WifiSsid.createFromAsciiEncoded(sSSID); 4799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private static final String sBSSID = "01:02:03:04:05:06"; 4899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 4999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private @Mock WifiConfigManager mWcm; 5099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private @Mock Context mContext; 5199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private Handler mHandler; 5299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private SupplicantStateTracker mSupplicantStateTracker; 5399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private TestLooper mLooper; 5499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private FrameworkFacade mFacade; 5599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 5699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private FrameworkFacade getFrameworkFacade() { 5799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao FrameworkFacade facade = mock(FrameworkFacade.class); 5899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao IBatteryStats batteryStatsService = mock(IBatteryStats.class); 5999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao when(facade.getBatteryService()).thenReturn(batteryStatsService); 6099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao return facade; 6199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao } 6299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 6399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao private Message getSupplicantStateChangeMessage(int networkId, WifiSsid wifiSsid, 6499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao String bssid, SupplicantState newSupplicantState) { 6599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao return Message.obtain(null, WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT, 0, 0, 6699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao new StateChangeResult(networkId, wifiSsid, bssid, newSupplicantState)); 6799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 6899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao } 6999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 7099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao @Before 7199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao public void setUp() { 7299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao mLooper = new TestLooper(); 7399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao mHandler = new Handler(mLooper.getLooper()); 7499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao MockitoAnnotations.initMocks(this); 7599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao mFacade = getFrameworkFacade(); 7699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao mSupplicantStateTracker = new SupplicantStateTracker(mContext, mWcm, mFacade, mHandler); 7799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao } 7899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao 7999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao /** 8099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * This test verifies that the SupplicantStateTracker sends a broadcast intent upon receiving 8199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao * a message when supplicant state changes 8299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao */ 8399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao @Test 8499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao public void testSupplicantStateChangeIntent() { 856459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver() { 8699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao @Override 8799f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao public void onReceive(Context context, Intent intent) { 8899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao String action = intent.getAction(); 8999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao assertTrue(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)); 9099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao SupplicantState recvdState = 9199f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao (SupplicantState) intent.getExtra(WifiManager.EXTRA_NEW_STATE, -1); 9299f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao assertEquals(SupplicantState.SCANNING, recvdState); 9399f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao } 9499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao }; 9599f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao IntentFilter mIntentFilter = new IntentFilter(); 9699f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 976459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mContext.registerReceiver(wifiBroadcastReceiver, mIntentFilter); 9899f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao mSupplicantStateTracker.sendMessage(getSupplicantStateChangeMessage(0, sWifiSsid, 9999f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao sBSSID, SupplicantState.SCANNING)); 10099f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao } 1016459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao 1026459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao /** 1036459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao * This test verifies that the current auth status is sent in the Broadcast intent 1046459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao */ 1056459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao @Test 1066459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao public void testAuthPassInSupplicantStateChangeIntent() { 1076459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver() { 1086459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao @Override 1096459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao public void onReceive(Context context, Intent intent) { 1106459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao String action = intent.getAction(); 1116459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertTrue(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)); 1126459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao SupplicantState recvdState = 1136459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao (SupplicantState) intent.getExtra(WifiManager.EXTRA_NEW_STATE, -1); 1146459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(SupplicantState.AUTHENTICATING, recvdState); 1156459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao boolean authStatus = 1166459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao (boolean) intent.getExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1); 1176459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(authStatus, true); 1186459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao } 1196459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao }; 1206459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao IntentFilter mIntentFilter = new IntentFilter(); 1216459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 1226459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mContext.registerReceiver(wifiBroadcastReceiver, mIntentFilter); 1236459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mSupplicantStateTracker.sendMessage(getSupplicantStateChangeMessage(0, sWifiSsid, 1246459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao sBSSID, SupplicantState.AUTHENTICATING)); 1256459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mSupplicantStateTracker.sendMessage(WifiMonitor.AUTHENTICATION_FAILURE_EVENT); 1266459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao } 1276459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao 1286459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao /** 1296459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao * This test verifies that the current auth status is sent in the Broadcast intent 1306459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao */ 1316459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao @Test 1326459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao public void testAuthFailedInSupplicantStateChangeIntent() { 1336459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver() { 1346459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao @Override 1356459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao public void onReceive(Context context, Intent intent) { 1366459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao String action = intent.getAction(); 1376459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertTrue(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)); 1386459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao SupplicantState recvdState = 1396459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao (SupplicantState) intent.getExtra(WifiManager.EXTRA_NEW_STATE, -1); 1406459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(SupplicantState.AUTHENTICATING, recvdState); 1416459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao boolean authStatus = 1426459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao (boolean) intent.getExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1); 1436459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(authStatus, false); 1446459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao } 1456459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao }; 1466459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao IntentFilter mIntentFilter = new IntentFilter(); 1476459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 1486459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mContext.registerReceiver(wifiBroadcastReceiver, mIntentFilter); 1496459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mSupplicantStateTracker.sendMessage(WifiMonitor.AUTHENTICATION_FAILURE_EVENT); 1506459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mSupplicantStateTracker.sendMessage(getSupplicantStateChangeMessage(0, sWifiSsid, 1516459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao sBSSID, SupplicantState.AUTHENTICATING)); 1526459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao } 1536459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao 1546459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao /** 1556459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao * This test verifies the correct reasonCode for auth failure is sent in Broadcast 1566459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao * intent. 1576459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao */ 1586459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao @Test 1596459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao public void testReasonCodeInSupplicantStateChangeIntent() { 1606459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao BroadcastReceiver wifiBroadcastReceiver = new BroadcastReceiver() { 1616459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao @Override 1626459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao public void onReceive(Context context, Intent intent) { 1636459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao String action = intent.getAction(); 1646459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertTrue(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)); 1656459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao SupplicantState recvdState = 1666459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao (SupplicantState) intent.getExtra(WifiManager.EXTRA_NEW_STATE, -1); 1676459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(SupplicantState.AUTHENTICATING, recvdState); 1686459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao boolean authStatus = 1696459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao (boolean) intent.getExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1); 1706459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(authStatus, false); 1716459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao int reasonCode = (int) 1726459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao intent.getExtra(WifiManager.EXTRA_SUPPLICANT_ERROR_REASON, -1); 1736459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao assertEquals(reasonCode, WifiManager.ERROR_AUTH_FAILURE_WRONG_PSWD); 1746459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao } 1756459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao }; 1766459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao IntentFilter mIntentFilter = new IntentFilter(); 1776459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 1786459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mContext.registerReceiver(wifiBroadcastReceiver, mIntentFilter); 1796459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mSupplicantStateTracker.sendMessage(WifiMonitor.AUTHENTICATION_FAILURE_EVENT, 0, 1806459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao WifiManager.ERROR_AUTH_FAILURE_WRONG_PSWD); 1816459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao mSupplicantStateTracker.sendMessage(getSupplicantStateChangeMessage(0, sWifiSsid, 1826459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao sBSSID, SupplicantState.AUTHENTICATING)); 1836459709af0e494f81c0b792566d9e8bee4b19d95Sohani Rao } 18499f90f5cb7637ece0358003d3d3355036e6d68f7Sohani Rao} 185