1 /* 2 * Copyright (C) 2020 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 android.net.wifi.nl80211.cts; 18 19 import static android.net.wifi.nl80211.WifiNl80211Manager.OemSecurityType; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertNotNull; 25 import static org.junit.Assert.fail; 26 import static org.junit.Assume.assumeTrue; 27 28 import android.content.Context; 29 import android.net.wifi.ScanResult; 30 import android.net.wifi.WifiManager; 31 import android.net.wifi.WifiScanner; 32 import android.net.wifi.cts.WifiFeature; 33 import android.net.wifi.nl80211.WifiNl80211Manager; 34 import android.os.Binder; 35 import android.os.Build; 36 import android.os.IBinder; 37 import android.platform.test.annotations.AppModeFull; 38 39 import androidx.test.ext.junit.runners.AndroidJUnit4; 40 import androidx.test.filters.SdkSuppress; 41 import androidx.test.filters.SmallTest; 42 import androidx.test.platform.app.InstrumentationRegistry; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 48 import java.util.Arrays; 49 import java.util.concurrent.ConcurrentLinkedQueue; 50 import java.util.concurrent.Executor; 51 52 53 /** CTS tests for {@link WifiNl80211Manager}. */ 54 @SmallTest 55 @RunWith(AndroidJUnit4.class) 56 @AppModeFull(reason = "Cannot get WifiManager/WifiNl80211Manager in instant app mode") 57 public class WifiNl80211ManagerTest { 58 59 private Context mContext; 60 61 private static class TestExecutor implements Executor { 62 private ConcurrentLinkedQueue<Runnable> tasks = new ConcurrentLinkedQueue<>(); 63 64 @Override execute(Runnable task)65 public void execute(Runnable task) { 66 tasks.add(task); 67 } 68 runAll()69 private void runAll() { 70 Runnable task = tasks.poll(); 71 while (task != null) { 72 task.run(); 73 task = tasks.poll(); 74 } 75 } 76 } 77 78 private class TestCountryCodeChangeListener implements 79 WifiNl80211Manager.CountryCodeChangedListener { 80 private String mCurrentCountryCode; 81 getCurrentCountryCode()82 public String getCurrentCountryCode() { 83 return mCurrentCountryCode; 84 } 85 86 @Override onCountryCodeChanged(String country)87 public void onCountryCodeChanged(String country) { 88 mCurrentCountryCode = country; 89 } 90 } 91 92 private class NormalScanEventCallback implements WifiNl80211Manager.ScanEventCallback { 93 private String mIfaceName; 94 NormalScanEventCallback(String ifaceName)95 NormalScanEventCallback(String ifaceName) { 96 mIfaceName = ifaceName; 97 } 98 99 @Override onScanResultReady()100 public void onScanResultReady() { 101 } 102 103 @Override onScanFailed()104 public void onScanFailed() { 105 } 106 107 @Override onScanFailed(int errorCode)108 public void onScanFailed(int errorCode) { 109 } 110 } 111 112 private class PnoScanEventCallback implements WifiNl80211Manager.ScanEventCallback { 113 private String mIfaceName; 114 PnoScanEventCallback(String ifaceName)115 PnoScanEventCallback(String ifaceName) { 116 mIfaceName = ifaceName; 117 } 118 119 @Override onScanResultReady()120 public void onScanResultReady() { 121 } 122 123 @Override onScanFailed()124 public void onScanFailed() { 125 } 126 } 127 128 @Before setUp()129 public void setUp() { 130 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 131 // skip tests if Wifi is not supported 132 assumeTrue(WifiFeature.isWifiSupported(mContext)); 133 } 134 135 @Test testOemSecurityTypeConstructor()136 public void testOemSecurityTypeConstructor() { 137 OemSecurityType securityType = new OemSecurityType( 138 ScanResult.PROTOCOL_WPA, 139 Arrays.asList(ScanResult.KEY_MGMT_PSK, ScanResult.KEY_MGMT_SAE), 140 Arrays.asList(ScanResult.CIPHER_NONE, ScanResult.CIPHER_TKIP), 141 ScanResult.CIPHER_CCMP); 142 143 assertThat(securityType.protocol).isEqualTo(ScanResult.PROTOCOL_WPA); 144 assertThat(securityType.keyManagement) 145 .isEqualTo(Arrays.asList(ScanResult.KEY_MGMT_PSK, ScanResult.KEY_MGMT_SAE)); 146 assertThat(securityType.pairwiseCipher) 147 .isEqualTo(Arrays.asList(ScanResult.CIPHER_NONE, ScanResult.CIPHER_TKIP)); 148 assertThat(securityType.groupCipher).isEqualTo(ScanResult.CIPHER_CCMP); 149 } 150 151 @Test testSendMgmtFrame()152 public void testSendMgmtFrame() { 153 try { 154 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 155 manager.sendMgmtFrame("wlan0", new byte[]{}, -1, Runnable::run, 156 new WifiNl80211Manager.SendMgmtFrameCallback() { 157 @Override 158 public void onAck(int elapsedTimeMs) {} 159 160 @Override 161 public void onFailure(int reason) {} 162 }); 163 } catch (Exception ignore) {} 164 } 165 166 @Test testGetTxPacketCounters()167 public void testGetTxPacketCounters() { 168 try { 169 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 170 manager.getTxPacketCounters("wlan0"); 171 } catch (Exception ignore) {} 172 } 173 174 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU) 175 @Test testGetMaxSsidsPerScan()176 public void testGetMaxSsidsPerScan() { 177 try { 178 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 179 manager.getMaxSsidsPerScan("wlan0"); 180 } catch (Exception ignore) { } 181 } 182 183 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 184 @Test testStartScan2()185 public void testStartScan2() { 186 try { 187 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 188 manager.startScan2("wlan0", WifiScanner.SCAN_TYPE_HIGH_ACCURACY, 189 null, null, null); 190 } catch (Exception ignore) { } 191 } 192 193 @Test testSetOnServiceDeadCallback()194 public void testSetOnServiceDeadCallback() { 195 try { 196 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 197 manager.setOnServiceDeadCallback(() -> {}); 198 } catch (Exception ignore) {} 199 } 200 201 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.S) 202 @Test testCountryCodeChangeListener()203 public void testCountryCodeChangeListener() { 204 TestCountryCodeChangeListener testCountryCodeChangeListener = 205 new TestCountryCodeChangeListener(); 206 TestExecutor executor = new TestExecutor(); 207 WifiManager wifiManager = mContext.getSystemService(WifiManager.class); 208 // Enable wifi to trigger country code change 209 wifiManager.setWifiEnabled(true); 210 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 211 // Register listener and unregister listener for API coverage only. 212 // Since current cts don't have sufficient permission to call WifiNl80211Manager API. 213 // Assert register fail because the CTS don't have sufficient permission to call 214 // WifiNl80211Manager API which is guarded by selinux. 215 assertFalse(manager.registerCountryCodeChangedListener(executor, 216 testCountryCodeChangeListener)); 217 manager.unregisterCountryCodeChangedListener(testCountryCodeChangeListener); 218 } 219 220 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU) 221 @Test testNotifyCountryCodeChanged()222 public void testNotifyCountryCodeChanged() { 223 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 224 // Assert fail because the CTS don't have sufficient permission to call 225 // WifiNl80211Manager API which is guarded by selinux. 226 try { 227 manager.notifyCountryCodeChanged("US"); 228 fail("notifyCountryCodeChanged doesn't throws RuntimeException"); 229 } catch (RuntimeException re) { 230 } 231 } 232 233 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 234 @Test testWifiNl80211ManagerConstructor()235 public void testWifiNl80211ManagerConstructor() { 236 IBinder testBinder = new Binder(); 237 WifiNl80211Manager manager = new WifiNl80211Manager(mContext, testBinder); 238 assertNotNull(manager); 239 } 240 241 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 242 @Test testScanEventCallback()243 public void testScanEventCallback() { 244 try { 245 WifiNl80211Manager manager = mContext.getSystemService(WifiNl80211Manager.class); 246 manager.setupInterfaceForClientMode("wlan0", Runnable::run, 247 new NormalScanEventCallback("wlan0"), 248 new PnoScanEventCallback("wlan0")); 249 } catch (Exception ignore) { } 250 } 251 } 252