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.ril.tests; 17 18 import static org.hamcrest.Matchers.greaterThan; 19 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.net.Network; 23 import android.net.NetworkCapabilities; 24 import android.net.wifi.WifiManager; 25 import android.os.Build; 26 import android.telephony.CellInfo; 27 import android.telephony.CellInfoLte; 28 import android.telephony.CellSignalStrengthLte; 29 import android.telephony.TelephonyManager; 30 import android.util.Log; 31 32 import androidx.annotation.NonNull; 33 import androidx.test.core.app.ApplicationProvider; 34 35 import com.android.compatibility.common.util.PropertyUtil; 36 37 import org.hamcrest.MatcherAssert; 38 import org.junit.Assert; 39 import org.junit.Assume; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.junit.runners.JUnit4; 44 45 import java.net.InetSocketAddress; 46 import java.net.Socket; 47 import java.net.SocketTimeoutException; 48 import java.util.ArrayList; 49 import java.util.List; 50 import java.util.concurrent.CountDownLatch; 51 52 /** 53 * Tests used to validate E2E RIL functionality. 54 */ 55 @RunWith(JUnit4.class) 56 public class RilE2eTests { 57 private static final String TAG = "RilE2eTests"; 58 private static final int MAX_POLL_DISABLED_WIFI_COUNT = 10; 59 private Context mContext; 60 private WifiManager mWifiManager; 61 private ConnectivityManager mConnManager; 62 private TelephonyManager mTeleManager; 63 64 @Before setUp()65 public void setUp() throws Exception { 66 // Ideally this should be done in the @BeforeClass hook, but that would 67 // make tradefed unhappy with a bunch "test did not run due to 68 // instrumentation issue. See run level error for reason." errors. 69 Assume.assumeFalse( 70 "Skip testing deprecated radio HAL from Q or earlier vendor", 71 PropertyUtil.getFirstApiLevel() <= Build.VERSION_CODES.Q); 72 73 mContext = ApplicationProvider.getApplicationContext(); 74 mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE); 75 mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 76 mTeleManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE); 77 // There must not be an active wifi connection while running the test or else 78 // getActiveNetworkInfo() will return that instead of the telephony network. 79 // Turning wifi off should do the trick. 80 disableWifi(); 81 } 82 83 @SuppressWarnings("deprecation") // setWifiEnabled not deprecated for system uid disableWifi()84 private void disableWifi() throws Exception { 85 Log.i(TAG, "Disabling WIFI..."); 86 87 mWifiManager.setWifiEnabled(false); 88 int count = MAX_POLL_DISABLED_WIFI_COUNT; 89 while (mWifiManager.isWifiEnabled() && count-- > 0) { 90 Log.i(TAG, "Waiting for WIFI to be disabled..."); 91 Thread.sleep(1000); 92 } 93 if (count < 0) { 94 Log.e(TAG, "Reached max number of polls while waiting to disable wifi"); 95 throw new Exception("Timed out waiting for wifi to be disabled"); 96 } 97 } 98 99 100 /** 101 * Verify that RIL stack is able to get up and connect to network in 102 * 20 seconds. 103 */ 104 @Test(timeout = 20 * 1000) testRilConnects()105 public void testRilConnects() throws Exception { 106 while (true) { 107 Network nw = mConnManager.getActiveNetwork(); 108 if (nw == null) { 109 continue; 110 } 111 NetworkCapabilities cap = mConnManager.getNetworkCapabilities(nw); 112 if (cap != null && cap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { 113 break; 114 } 115 116 Log.i(TAG, "Waiting for MOBILE to become primary network for DATA."); 117 118 Thread.sleep(1000); 119 } 120 121 // Bind process to MOBILE network. This should allow us to verify network is functional. 122 Network net = mConnManager.getActiveNetwork(); 123 Assert.assertNotNull(net); 124 Assert.assertTrue(mConnManager.bindProcessToNetwork(net)); 125 126 // Open connection to Google public DNS server 127 InetSocketAddress addr = new InetSocketAddress("8.8.8.8", 53); 128 while (true) { 129 try (Socket s = new Socket()) { 130 Log.d(TAG, "Testing socket connection to 8.8.8.8:53..."); 131 s.connect(addr, 5000); // use a socket connection timeout of 5s 132 Assert.assertTrue( 133 "Failed to make socket connection to 8.8.8.8:53", s.isConnected()); 134 return; 135 } catch (SocketTimeoutException e) { 136 Log.d(TAG, "Socket connection to 8.8.8.8:53 timed out, retry..."); 137 } 138 } 139 } 140 141 142 /** 143 * Verify that AVD is connected to our virtual network operator and is 144 * phone-, sms- and data capable. 145 */ 146 @Test testBasicPhoneAttributes()147 public void testBasicPhoneAttributes() throws Exception { 148 Assert.assertEquals("Android Virtual Operator", mTeleManager.getNetworkOperatorName()); 149 Assert.assertFalse(mTeleManager.isNetworkRoaming()); 150 Assert.assertTrue(mTeleManager.isSmsCapable()); 151 Assert.assertSame(TelephonyManager.NETWORK_TYPE_LTE, mTeleManager.getVoiceNetworkType()); 152 Assert.assertSame(TelephonyManager.SIM_STATE_READY, mTeleManager.getSimState()); 153 Assert.assertSame(TelephonyManager.PHONE_TYPE_GSM, mTeleManager.getPhoneType()); 154 Assert.assertSame(mTeleManager.getActiveModemCount(), 1); 155 // See SIM FS response for 178 28480 (Cuttlefish RIL). 156 Assert.assertEquals("+15551234567", mTeleManager.getLine1Number()); 157 // See SIM FS response for 178 28615 (Cuttlefish RIL). 158 Assert.assertEquals("+15557654321", mTeleManager.getVoiceMailNumber()); 159 Assert.assertSame(TelephonyManager.DATA_CONNECTED, mTeleManager.getDataState()); 160 } 161 162 @Test testSignalLevels()163 public void testSignalLevels() throws Exception { 164 List<CellInfo> cellInfos = new ArrayList<>(); 165 CountDownLatch cdl = new CountDownLatch(1); 166 mTeleManager.requestCellInfoUpdate(mContext.getMainExecutor(), 167 new TelephonyManager.CellInfoCallback() { 168 @Override 169 public void onCellInfo(@NonNull List<CellInfo> cellInfo) { 170 if (cellInfo != null) { 171 cellInfos.addAll(cellInfo); 172 } 173 cdl.countDown(); 174 } 175 }); 176 cdl.await(); 177 MatcherAssert.assertThat("Size of list of cell info", cellInfos.size(), greaterThan(0)); 178 CellInfoLte cellInfo = (CellInfoLte) cellInfos.get(0); 179 CellSignalStrengthLte signalStrength = cellInfo.getCellSignalStrength(); 180 int bars = signalStrength.getLevel(); 181 MatcherAssert.assertThat("Signal Bars", bars, greaterThan(1)); 182 } 183 } 184