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.cuttlefish.wifi.tests; 17 18 import android.content.Context; 19 import android.net.ConnectivityManager; 20 import android.net.Network; 21 import android.net.NetworkInfo; 22 import android.net.ConnectivityManager; 23 import android.net.wifi.SupplicantState; 24 import android.net.wifi.WifiConfiguration; 25 import android.net.wifi.WifiInfo; 26 import android.net.wifi.WifiManager; 27 import android.support.test.InstrumentationRegistry; 28 import android.util.Log; 29 30 import org.junit.Assert; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 import java.net.Socket; 37 import java.util.List; 38 39 /** 40 * Tests used to validate E2E WIFI functionality. 41 */ 42 @RunWith(JUnit4.class) 43 public class WifiE2eTests { 44 private static final String TAG = "WifiE2eTests"; 45 private Context mContext; 46 private WifiManager mWifiManager; 47 private ConnectivityManager mConnManager; 48 49 @Before 50 public void setUp() throws Exception { 51 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 52 mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE); 53 mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 54 } 55 56 57 private void enableWifi() { 58 Log.i(TAG, "Enabling WIFI..."); 59 mWifiManager.setWifiEnabled(true); 60 while (!(mWifiManager.isWifiEnabled() && mWifiManager.pingSupplicant())) { 61 Log.i(TAG, "Waiting for WIFI (Enabled: " + mWifiManager.isWifiEnabled() + 62 ", Ready: " + mWifiManager.pingSupplicant() + ")"); 63 try { 64 Thread.sleep(1000); 65 } catch (InterruptedException e) {} 66 } 67 } 68 69 70 private void disableWifi() { 71 Log.i(TAG, "Disabling WIFI..."); 72 73 mWifiManager.setWifiEnabled(false); 74 while (mWifiManager.isWifiEnabled()) { 75 Log.i(TAG, "Waiting for WIFI to be disabled..."); 76 try { 77 Thread.sleep(1000); 78 } catch (InterruptedException e) {} 79 } 80 } 81 82 83 private void waitForSupplicantState(SupplicantState... expectedStates) { 84 while (true) { 85 WifiInfo info = mWifiManager.getConnectionInfo(); 86 SupplicantState currentState = info.getSupplicantState(); 87 88 Log.i(TAG, "WIFI State: " + currentState); 89 for (SupplicantState state : expectedStates) { 90 if (currentState == state) { 91 Log.i(TAG, "WIFI is now in expected state."); 92 return; 93 } 94 } 95 96 try { 97 Thread.sleep(1000); 98 } catch (InterruptedException e) {} 99 } 100 } 101 102 103 /** 104 * Initialize wifi, erase all settings. 105 */ 106 @Test(timeout = 10 * 1000) 107 public void testWifiInitialization() { 108 enableWifi(); 109 110 List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); 111 Assert.assertNotNull(configs); 112 for (WifiConfiguration config : configs) { 113 Log.i(TAG, "Removing network " + config.networkId + ": " + config.SSID); 114 Assert.assertTrue(mWifiManager.disableNetwork(config.networkId)); 115 Assert.assertTrue(mWifiManager.removeNetwork(config.networkId)); 116 } 117 118 waitForSupplicantState( 119 SupplicantState.INACTIVE, 120 SupplicantState.DISCONNECTED, 121 SupplicantState.SCANNING); 122 123 disableWifi(); 124 } 125 126 127 /** 128 * Verify that WIFI stack is able to get up and connect to network in 129 * 60 seconds. 130 */ 131 @Test(timeout = 60 * 1000) 132 public void testWifiConnects() throws Exception { 133 // 1. Make sure we start with WIFI disabled. 134 // It could be, that WIFI is currently disabled anyway. 135 // Let's make sure that's the case. 136 disableWifi(); 137 138 // 2. Wait until stack is up. 139 enableWifi(); 140 141 // 3. Configure WIFI: 142 // - Add network, 143 // - Enable network, 144 // - Scan for network 145 Log.i(TAG, "Configuring WIFI..."); 146 WifiConfiguration conf = new WifiConfiguration(); 147 conf.SSID = "\"AndroidWifi\""; 148 conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 149 int networkId = mWifiManager.addNetwork(conf); 150 Assert.assertTrue(networkId >= 0); 151 mWifiManager.enableNetwork(networkId, false); 152 mWifiManager.startScan(); 153 154 // 4. Wait until connected. 155 Log.i(TAG, "Waiting for connectivity..."); 156 waitForSupplicantState(SupplicantState.COMPLETED); 157 158 // 5. Wait until WIFI is current network. 159 while (true) { 160 NetworkInfo net = mConnManager.getActiveNetworkInfo(); 161 if (net != null && net.getType() == ConnectivityManager.TYPE_WIFI) break; 162 163 Log.i(TAG, "Waiting for WIFI to become primary network for DATA."); 164 165 try { 166 Thread.sleep(1000); 167 } catch (InterruptedException e) {} 168 } 169 170 // 6. Bind process to WIFI network. This should allow us to verify network is functional. 171 Network net = mConnManager.getActiveNetwork(); 172 Assert.assertNotNull(net); 173 Assert.assertTrue(mConnManager.bindProcessToNetwork(net)); 174 175 // 7. Open connection to google.com servers. 176 try (Socket s = new Socket("google.com", 80)) { 177 Assert.assertTrue(s.isConnected()); 178 } 179 } 180 } 181