• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.wifi.SupplicantState;
23 import android.net.wifi.WifiConfiguration;
24 import android.net.wifi.WifiInfo;
25 import android.net.wifi.WifiManager;
26 import android.util.Log;
27 
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 import java.net.InetSocketAddress;
38 import java.net.Socket;
39 import java.net.SocketTimeoutException;
40 import java.util.List;
41 
42 /**
43  * Tests used to validate E2E WIFI functionality.
44  */
45 @RunWith(JUnit4.class)
46 public class WifiE2eTests {
47     private static final String TAG = "WifiE2eTests";
48     private Context mContext;
49     private WifiManager mWifiManager;
50     private ConnectivityManager mConnManager;
51 
52     @Before
setUp()53     public void setUp() throws Exception {
54         mContext = ApplicationProvider.getApplicationContext();
55         mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
56         mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
57     }
58 
59 
60     @SuppressWarnings("unused")
enableWifi()61     private void enableWifi() throws InterruptedException {
62         Log.i(TAG, "Enabling WIFI...");
63         mWifiManager.setWifiEnabled(true);
64         while (!mWifiManager.isWifiEnabled()) {
65             Log.i(TAG, "Waiting for WIFI to be enabled...");
66             Thread.sleep(1000);
67         }
68     }
69 
70 
71     @SuppressWarnings("unused")
disableWifi()72     private void disableWifi() throws InterruptedException {
73         Log.i(TAG, "Disabling WIFI...");
74 
75         mWifiManager.setWifiEnabled(false);
76         while (mWifiManager.isWifiEnabled()) {
77             Log.i(TAG, "Waiting for WIFI to be disabled...");
78         }
79     }
80 
81 
waitForSupplicantState(SupplicantState... expectedStates)82     private void waitForSupplicantState(SupplicantState... expectedStates)
83             throws InterruptedException {
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             Thread.sleep(1000);
97         }
98     }
99 
100 
enableNetwork(String SSID)101     private void enableNetwork(String SSID) {
102         WifiConfiguration conf = new WifiConfiguration();
103         conf.SSID = SSID;
104         conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
105         int networkId = mWifiManager.addNetwork(conf);
106         Assert.assertTrue(networkId >= 0);
107         mWifiManager.enableNetwork(networkId, false);
108     }
109 
110 
111     /**
112      * Initialize wifi, erase all settings.
113      */
114     @Test(timeout = 10 * 1000)
testWifiInitialization()115     public void testWifiInitialization() throws Exception {
116         enableWifi();
117 
118         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
119         Assert.assertNotNull(configs);
120         for (WifiConfiguration config : configs) {
121             Log.i(TAG, "Removing network " + config.networkId + ": " + config.SSID);
122             mWifiManager.disableNetwork(config.networkId);
123             mWifiManager.removeNetwork(config.networkId);
124         }
125         configs = mWifiManager.getConfiguredNetworks();
126         Assert.assertEquals(0, configs.size());
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             Thread.sleep(1000);
172         }
173 
174         // 6. Bind process to WIFI network. This should allow us to verify network is functional.
175         Network net = mConnManager.getActiveNetwork();
176         Assert.assertNotNull(net);
177         Assert.assertTrue(mConnManager.bindProcessToNetwork(net));
178 
179         // 7. Open connection to Google public DNS server
180         InetSocketAddress addr = new InetSocketAddress("8.8.8.8", 53);
181         while (true) {
182             try (Socket s = new Socket()) {
183                 Log.d(TAG, "Testing socket connection to 8.8.8.8:53...");
184                 s.connect(addr, 5000); // use a socket connection timeout of 5s
185                 Assert.assertTrue(
186                         "Failed to make socket connection to 8.8.8.8:53", s.isConnected());
187                 return;
188             } catch (SocketTimeoutException e) {
189                 Log.d(TAG, "Socket connection to 8.8.8.8:53 timed out (5s), retry...");
190             }
191         }
192     }
193 }
194