1 /* 2 * Copyright (C) 2016 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 17 package com.android.server.wifi.hotspot2; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Matchers.eq; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 import static org.mockito.MockitoAnnotations.initMocks; 26 27 import android.support.test.filters.SmallTest; 28 29 import com.android.server.wifi.WifiNative; 30 import com.android.server.wifi.hotspot2.anqp.Constants; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.mockito.Mock; 35 36 import java.util.Arrays; 37 import java.util.HashSet; 38 import java.util.List; 39 40 /** 41 * Unit tests for {@link com.android.server.wifi.hotspot2.PasspointEventHandler}. 42 * TODO(zqiu): add more test when switch over to use wificond. 43 */ 44 @SmallTest 45 public class PasspointEventHandlerTest { 46 47 private static final String TAG = "PasspointEventHandlerTest"; 48 49 private static final long BSSID = 0x112233445566L; 50 private static final String BSSID_STR = "11:22:33:44:55:66"; 51 private static final String ICON_FILENAME = "icon.test"; 52 53 @Mock WifiNative mWifiNative; 54 @Mock PasspointEventHandler.Callbacks mCallbacks; 55 PasspointEventHandler mHandler; 56 57 /** Sets up test. */ 58 @Before 59 public void setUp() throws Exception { 60 initMocks(this); 61 mHandler = new PasspointEventHandler(mWifiNative, mCallbacks); 62 } 63 64 /** 65 * Test for requesting Hotspot 2.0 R1 ANQP element. 66 */ 67 @Test 68 public void requestR1AnqpElement() { 69 List<Constants.ANQPElementType> elementToRequest = 70 Arrays.asList(Constants.ANQPElementType.ANQPRoamingConsortium); 71 HashSet<Integer> expAnqpIds = 72 new HashSet<>(Arrays.asList(Constants.getANQPElementID( 73 Constants.ANQPElementType.ANQPRoamingConsortium))); 74 HashSet<Integer> expHs20Subtypes = new HashSet<>(); 75 76 // wpa_supplicant succeeded the request. 77 when(mWifiNative.requestAnqp(any(), eq(BSSID_STR), eq(expAnqpIds), eq(expHs20Subtypes))) 78 .thenReturn(true); 79 assertTrue(mHandler.requestANQP(BSSID, elementToRequest)); 80 81 // wpa_supplicant failed the request. 82 when(mWifiNative.requestAnqp(any(), eq(BSSID_STR), eq(expAnqpIds), eq(expHs20Subtypes))) 83 .thenReturn(false); 84 assertFalse(mHandler.requestANQP(BSSID, elementToRequest)); 85 } 86 87 /** 88 * Test for requesting Hotspot 2.0 R2 ANQP element. 89 */ 90 @Test 91 public void requestR2AnqpElement() { 92 List<Constants.ANQPElementType> elementToRequest = 93 Arrays.asList(Constants.ANQPElementType.HSFriendlyName); 94 HashSet<Integer> expAnqpIds = new HashSet<>(); 95 HashSet<Integer> expHs20Subtypes = 96 new HashSet<>(Arrays.asList(Constants.getHS20ElementID( 97 Constants.ANQPElementType.HSFriendlyName))); 98 99 // wpa_supplicant succeeded the request. 100 when(mWifiNative.requestAnqp(any(), eq(BSSID_STR), eq(expAnqpIds), eq(expHs20Subtypes))) 101 .thenReturn(true); 102 assertTrue(mHandler.requestANQP(BSSID, elementToRequest)); 103 104 // wpa_supplicant failed the request. 105 when(mWifiNative.requestAnqp(any(), eq(BSSID_STR), eq(expAnqpIds), eq(expHs20Subtypes))) 106 .thenReturn(false); 107 assertFalse(mHandler.requestANQP(BSSID, elementToRequest)); 108 } 109 110 /** 111 * Test for requesting both Hotspot 2.0 R1 and R2 ANQP elements. 112 */ 113 @Test 114 public void requestMixAnqpElements() { 115 List<Constants.ANQPElementType> elementToRequest = 116 Arrays.asList(Constants.ANQPElementType.ANQPRoamingConsortium, 117 Constants.ANQPElementType.HSFriendlyName); 118 HashSet<Integer> expAnqpIds = 119 new HashSet<>(Arrays.asList(Constants.getANQPElementID( 120 Constants.ANQPElementType.ANQPRoamingConsortium))); 121 HashSet<Integer> expHs20Subtypes = 122 new HashSet<>(Arrays.asList(Constants.getHS20ElementID( 123 Constants.ANQPElementType.HSFriendlyName))); 124 125 // wpa_supplicant succeeded the request. 126 when(mWifiNative.requestAnqp(any(), eq(BSSID_STR), eq(expAnqpIds), eq(expHs20Subtypes))) 127 .thenReturn(true); 128 assertTrue(mHandler.requestANQP(BSSID, elementToRequest)); 129 130 // wpa_supplicant failed the request. 131 when(mWifiNative.requestAnqp(any(), eq(BSSID_STR), eq(expAnqpIds), eq(expHs20Subtypes))) 132 .thenReturn(false); 133 assertFalse(mHandler.requestANQP(BSSID, elementToRequest)); 134 } 135 136 /** 137 * Test for requesting both Hotspot 2.0 R2 icon file. 138 */ 139 @Test 140 public void requestIconFile() { 141 // wpa_supplicant succeeded the request. 142 when(mWifiNative.requestIcon(any(), eq(BSSID_STR), eq(ICON_FILENAME))).thenReturn(true); 143 assertTrue(mHandler.requestIcon(BSSID, ICON_FILENAME)); 144 145 // wpa_supplicant failed the request. 146 when(mWifiNative.requestIcon(any(), eq(BSSID_STR), eq(ICON_FILENAME))).thenReturn(false); 147 assertFalse(mHandler.requestIcon(BSSID, ICON_FILENAME)); 148 } 149 150 /** 151 * Test for ANQP request completed with error. 152 */ 153 @Test 154 public void anqpRequestCompletedWithError() { 155 mHandler.notifyANQPDone(new AnqpEvent(BSSID, null)); 156 verify(mCallbacks).onANQPResponse(BSSID, null); 157 } 158 } 159