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
setUp()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 
enableWifi()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 
disableWifi()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 
waitForSupplicantState(SupplicantState... expectedStates)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 
enableNetwork(String SSID)103     private void enableNetwork(String SSID) {
104         WifiConfiguration conf = new WifiConfiguration();
105         conf.SSID = SSID;
106         conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
107         int networkId = mWifiManager.addNetwork(conf);
108         Assert.assertTrue(networkId >= 0);
109         mWifiManager.enableNetwork(networkId, false);
110     }
111 
112 
113     /**
114      * Initialize wifi, erase all settings.
115      */
116     @Test(timeout = 10 * 1000)
testWifiInitialization()117     public void testWifiInitialization() {
118         enableWifi();
119 
120         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
121         Assert.assertNotNull(configs);
122         for (WifiConfiguration config : configs) {
123             Log.i(TAG, "Removing network " + config.networkId + ": " + config.SSID);
124             Assert.assertTrue(mWifiManager.disableNetwork(config.networkId));
125             Assert.assertTrue(mWifiManager.removeNetwork(config.networkId));
126         }
127 
128         waitForSupplicantState(
129                 SupplicantState.INACTIVE,
130                 SupplicantState.DISCONNECTED,
131                 SupplicantState.SCANNING);
132 
133         disableWifi();
134     }
135 
136 
137     /**
138      * Verify that WIFI stack is able to get up and connect to network in
139      * 60 seconds.
140      */
141     @Test(timeout = 60 * 1000)
testWifiConnects()142     public void testWifiConnects() throws Exception {
143         // 1. Make sure we start with WIFI disabled.
144         // It could be, that WIFI is currently disabled anyway.
145         // Let's make sure that's the case.
146         disableWifi();
147 
148         // 2. Wait until stack is up.
149         enableWifi();
150 
151         // 3. Configure WIFI:
152         //    - Add network,
153         //    - Enable network,
154         //    - Scan for network
155         Log.i(TAG, "Configuring WIFI...");
156         enableNetwork("\"VirtWifi\"");
157         enableNetwork("\"AndroidWifi\"");
158         mWifiManager.startScan();
159 
160         // 4. Wait until connected.
161         Log.i(TAG, "Waiting for connectivity...");
162         waitForSupplicantState(SupplicantState.COMPLETED);
163 
164         // 5. Wait until WIFI is current network.
165         while (true) {
166             NetworkInfo net = mConnManager.getActiveNetworkInfo();
167             if (net != null && net.getType() == ConnectivityManager.TYPE_WIFI) break;
168 
169             Log.i(TAG, "Waiting for WIFI to become primary network for DATA.");
170 
171             try {
172                 Thread.sleep(1000);
173             } catch (InterruptedException e) {}
174         }
175 
176         // 6. Bind process to WIFI network. This should allow us to verify network is functional.
177         Network net = mConnManager.getActiveNetwork();
178         Assert.assertNotNull(net);
179         Assert.assertTrue(mConnManager.bindProcessToNetwork(net));
180 
181         // 7. Open connection to google.com servers.
182         try (Socket s = new Socket("google.com", 80)) {
183             Assert.assertTrue(s.isConnected());
184         }
185     }
186 }
187