WifiMonitorTest.java revision ef027b4240eb994976dc6173fa396fe10b45ae52
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 17package com.android.server.wifi; 18 19import static org.junit.Assert.assertEquals; 20import static org.mockito.Mockito.spy; 21import static org.mockito.Mockito.verify; 22 23import android.hardware.wifi.supplicant.V1_0.ISupplicantStaIfaceCallback.WpsConfigError; 24import android.hardware.wifi.supplicant.V1_0.ISupplicantStaIfaceCallback.WpsErrorIndication; 25import android.net.wifi.WifiManager; 26import android.os.Handler; 27import android.os.Message; 28import android.os.test.TestLooper; 29import android.test.suitebuilder.annotation.SmallTest; 30 31import org.junit.Before; 32import org.junit.Test; 33import org.mockito.ArgumentCaptor; 34 35import java.lang.reflect.Constructor; 36 37/** 38 * Unit tests for {@link com.android.server.wifi.WifiMonitor}. 39 */ 40@SmallTest 41public class WifiMonitorTest { 42 private static final String WLAN_IFACE_NAME = "wlan0"; 43 private WifiMonitor mWifiMonitor; 44 private TestLooper mLooper; 45 private Handler mHandlerSpy; 46 47 @Before 48 public void setUp() throws Exception { 49 final Constructor<WifiMonitor> wifiMonitorConstructor = 50 WifiMonitor.class.getDeclaredConstructor(); 51 wifiMonitorConstructor.setAccessible(true); 52 mWifiMonitor = spy(wifiMonitorConstructor.newInstance()); 53 mLooper = new TestLooper(); 54 mHandlerSpy = spy(new Handler(mLooper.getLooper())); 55 mWifiMonitor.setMonitoring(WLAN_IFACE_NAME, true); 56 } 57 58 /** 59 * Broadcast WPS failure event test. 60 */ 61 @Test 62 public void testBroadcastWpsEventFailDueToErrorTkipOnlyProhibhited() { 63 mWifiMonitor.registerHandler( 64 WLAN_IFACE_NAME, WifiMonitor.WPS_FAIL_EVENT, mHandlerSpy); 65 mWifiMonitor.broadcastWpsFailEvent( 66 WLAN_IFACE_NAME, WpsConfigError.NO_ERROR, 67 WpsErrorIndication.SECURITY_TKIP_ONLY_PROHIBITED); 68 mLooper.dispatchAll(); 69 70 ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); 71 verify(mHandlerSpy).handleMessage(messageCaptor.capture()); 72 assertEquals(WifiMonitor.WPS_FAIL_EVENT, messageCaptor.getValue().what); 73 assertEquals(WifiManager.WPS_TKIP_ONLY_PROHIBITED, messageCaptor.getValue().arg1); 74 } 75 76 /** 77 * Broadcast WPS failure event test. 78 */ 79 @Test 80 public void testBroadcastWpsEventFailDueToErrorWepProhibhited() { 81 mWifiMonitor.registerHandler( 82 WLAN_IFACE_NAME, WifiMonitor.WPS_FAIL_EVENT, mHandlerSpy); 83 mWifiMonitor.broadcastWpsFailEvent( 84 WLAN_IFACE_NAME, WpsConfigError.NO_ERROR, 85 WpsErrorIndication.SECURITY_WEP_PROHIBITED); 86 mLooper.dispatchAll(); 87 88 ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); 89 verify(mHandlerSpy).handleMessage(messageCaptor.capture()); 90 assertEquals(WifiMonitor.WPS_FAIL_EVENT, messageCaptor.getValue().what); 91 assertEquals(WifiManager.WPS_WEP_PROHIBITED, messageCaptor.getValue().arg1); 92 } 93 94 /** 95 * Broadcast WPS failure event test. 96 */ 97 @Test 98 public void testBroadcastWpsEventFailDueToConfigAuthError() { 99 mWifiMonitor.registerHandler( 100 WLAN_IFACE_NAME, WifiMonitor.WPS_FAIL_EVENT, mHandlerSpy); 101 mWifiMonitor.broadcastWpsFailEvent( 102 WLAN_IFACE_NAME, WpsConfigError.DEV_PASSWORD_AUTH_FAILURE, 103 WpsErrorIndication.NO_ERROR); 104 105 mLooper.dispatchAll(); 106 ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); 107 verify(mHandlerSpy).handleMessage(messageCaptor.capture()); 108 assertEquals(WifiMonitor.WPS_FAIL_EVENT, messageCaptor.getValue().what); 109 assertEquals(WifiManager.WPS_AUTH_FAILURE, messageCaptor.getValue().arg1); 110 } 111 112 /** 113 * Broadcast WPS failure event test. 114 */ 115 @Test 116 public void testBroadcastWpsEventFailDueToConfigPbcOverlapError() { 117 mWifiMonitor.registerHandler( 118 WLAN_IFACE_NAME, WifiMonitor.WPS_FAIL_EVENT, mHandlerSpy); 119 mWifiMonitor.broadcastWpsFailEvent( 120 WLAN_IFACE_NAME, WpsConfigError.MULTIPLE_PBC_DETECTED, 121 WpsErrorIndication.NO_ERROR); 122 mLooper.dispatchAll(); 123 124 ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); 125 verify(mHandlerSpy).handleMessage(messageCaptor.capture()); 126 assertEquals(WifiMonitor.WPS_FAIL_EVENT, messageCaptor.getValue().what); 127 assertEquals(WifiManager.WPS_OVERLAP_ERROR, messageCaptor.getValue().arg1); 128 } 129 130 /** 131 * Broadcast WPS failure event test. 132 */ 133 @Test 134 public void testBroadcastWpsEventFailDueToConfigError() { 135 mWifiMonitor.registerHandler( 136 WLAN_IFACE_NAME, WifiMonitor.WPS_FAIL_EVENT, mHandlerSpy); 137 mWifiMonitor.broadcastWpsFailEvent( 138 WLAN_IFACE_NAME, WpsConfigError.MSG_TIMEOUT, 139 WpsErrorIndication.NO_ERROR); 140 mLooper.dispatchAll(); 141 142 ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); 143 verify(mHandlerSpy).handleMessage(messageCaptor.capture()); 144 assertEquals(WifiMonitor.WPS_FAIL_EVENT, messageCaptor.getValue().what); 145 assertEquals(WifiManager.ERROR, messageCaptor.getValue().arg1); 146 assertEquals(WpsConfigError.MSG_TIMEOUT, messageCaptor.getValue().arg2); 147 } 148} 149