blob: 49688cc98678779d56e257251f709879c3287d03 [file] [log] [blame]
Junyu Lai36e76262023-12-27 18:17:30 +08001/*
2 * Copyright (C) 2024 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
17package com.google.snippet.connectivity
18
Junyu Laib99caad2024-04-22 16:22:36 +080019import android.Manifest.permission.NETWORK_SETTINGS
Junyu Lai36e76262023-12-27 18:17:30 +080020import android.Manifest.permission.OVERRIDE_WIFI_CONFIG
21import android.content.pm.PackageManager.FEATURE_TELEPHONY
22import android.content.pm.PackageManager.FEATURE_WIFI
23import android.net.ConnectivityManager
Junyu Lai5d5a30e2024-04-09 13:42:20 +080024import android.net.Network
Junyu Lai36e76262023-12-27 18:17:30 +080025import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED
26import android.net.NetworkCapabilities.TRANSPORT_WIFI
27import android.net.NetworkRequest
28import android.net.cts.util.CtsNetUtils
29import android.net.cts.util.CtsTetheringUtils
30import android.net.wifi.ScanResult
31import android.net.wifi.SoftApConfiguration
32import android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK
33import android.net.wifi.WifiConfiguration
Junyu Laib99caad2024-04-22 16:22:36 +080034import android.net.wifi.WifiInfo
Junyu Lai36e76262023-12-27 18:17:30 +080035import android.net.wifi.WifiManager
36import android.net.wifi.WifiNetworkSpecifier
37import android.net.wifi.WifiSsid
38import androidx.test.platform.app.InstrumentationRegistry
Junyu Lai73711f72024-07-11 17:00:29 +080039import com.android.compatibility.common.util.PropertyUtil
Paul Hue7fd5a52024-07-01 03:22:59 +000040import com.android.modules.utils.build.SdkLevel
Junyu Laib99caad2024-04-22 16:22:36 +080041import com.android.testutils.AutoReleaseNetworkCallbackRule
Junyu Lai36e76262023-12-27 18:17:30 +080042import com.android.testutils.ConnectUtil
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090043import com.android.testutils.NetworkCallbackHelper
Junyu Lai36e76262023-12-27 18:17:30 +080044import com.android.testutils.RecorderCallback.CallbackEntry.CapabilitiesChanged
45import com.android.testutils.TestableNetworkCallback
46import com.android.testutils.runAsShell
47import com.google.android.mobly.snippet.Snippet
48import com.google.android.mobly.snippet.rpc.Rpc
Junyu Laib99caad2024-04-22 16:22:36 +080049import org.junit.Rule
Junyu Lai36e76262023-12-27 18:17:30 +080050
51class ConnectivityMultiDevicesSnippet : Snippet {
Junyu Laib99caad2024-04-22 16:22:36 +080052 @get:Rule
53 val networkCallbackRule = AutoReleaseNetworkCallbackRule()
Junyu Lai36e76262023-12-27 18:17:30 +080054 private val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
55 private val wifiManager = context.getSystemService(WifiManager::class.java)!!
56 private val cm = context.getSystemService(ConnectivityManager::class.java)!!
57 private val pm = context.packageManager
58 private val ctsNetUtils = CtsNetUtils(context)
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090059 private val cbHelper = NetworkCallbackHelper()
Junyu Lai36e76262023-12-27 18:17:30 +080060 private val ctsTetheringUtils = CtsTetheringUtils(context)
61 private var oldSoftApConfig: SoftApConfiguration? = null
62
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090063 override fun shutdown() {
64 cbHelper.unregisterAll()
65 }
66
Junyu Lai36e76262023-12-27 18:17:30 +080067 @Rpc(description = "Check whether the device has wifi feature.")
68 fun hasWifiFeature() = pm.hasSystemFeature(FEATURE_WIFI)
69
70 @Rpc(description = "Check whether the device has telephony feature.")
71 fun hasTelephonyFeature() = pm.hasSystemFeature(FEATURE_TELEPHONY)
72
73 @Rpc(description = "Check whether the device supporters AP + STA concurrency.")
Paul Hu2ab79272024-04-24 05:49:42 +000074 fun isStaApConcurrencySupported() = wifiManager.isStaApConcurrencySupported()
Junyu Lai36e76262023-12-27 18:17:30 +080075
Paul Hue7fd5a52024-07-01 03:22:59 +000076 @Rpc(description = "Check whether the device SDK is as least T")
77 fun isAtLeastT() = SdkLevel.isAtLeastT()
78
Junyu Lai73711f72024-07-11 17:00:29 +080079 @Rpc(description = "Return whether the Sdk level is at least V.")
80 fun isAtLeastV() = SdkLevel.isAtLeastV()
81
82 @Rpc(description = "Return the API level that the VSR requirement must be fulfilled.")
83 fun getVsrApiLevel() = PropertyUtil.getVsrApiLevel()
84
Junyu Lai36e76262023-12-27 18:17:30 +080085 @Rpc(description = "Request cellular connection and ensure it is the default network.")
86 fun requestCellularAndEnsureDefault() {
87 ctsNetUtils.disableWifi()
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090088 val network = cbHelper.requestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080089 ctsNetUtils.expectNetworkIsSystemDefault(network)
90 }
91
Junyu Lai26c81bd2024-06-12 10:45:46 +080092 @Rpc(description = "Unregister all connections.")
93 fun unregisterAll() {
94 cbHelper.unregisterAll()
Junyu Lai36e76262023-12-27 18:17:30 +080095 }
96
97 @Rpc(description = "Ensure any wifi is connected and is the default network.")
98 fun ensureWifiIsDefault() {
99 val network = ctsNetUtils.ensureWifiConnected()
100 ctsNetUtils.expectNetworkIsSystemDefault(network)
101 }
102
103 @Rpc(description = "Connect to specified wifi network.")
104 // Suppress warning because WifiManager methods to connect to a config are
105 // documented not to be deprecated for privileged users.
106 @Suppress("DEPRECATION")
Junyu Laicea928f2024-04-24 16:15:30 +0800107 fun connectToWifi(ssid: String, passphrase: String): Long {
Junyu Lai36e76262023-12-27 18:17:30 +0800108 val specifier = WifiNetworkSpecifier.Builder()
Junyu Lai36e76262023-12-27 18:17:30 +0800109 .setBand(ScanResult.WIFI_BAND_24_GHZ)
110 .build()
111 val wifiConfig = WifiConfiguration()
112 wifiConfig.SSID = "\"" + ssid + "\""
113 wifiConfig.preSharedKey = "\"" + passphrase + "\""
114 wifiConfig.hiddenSSID = true
115 wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK)
116 wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
117 wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
118
Junyu Laib99caad2024-04-22 16:22:36 +0800119 // Add the test configuration and connect to it.
120 val connectUtil = ConnectUtil(context)
121 connectUtil.connectToWifiConfig(wifiConfig)
122
123 // Implement manual SSID matching. Specifying the SSID in
124 // NetworkSpecifier is ineffective
125 // (see WifiNetworkAgentSpecifier#canBeSatisfiedBy for details).
126 // Note that holding permission is necessary when waiting for
127 // the callbacks. The handler thread checks permission; if
128 // it's not present, the SSID will be redacted.
Junyu Lai36e76262023-12-27 18:17:30 +0800129 val networkCallback = TestableNetworkCallback()
Junyu Laib99caad2024-04-22 16:22:36 +0800130 val wifiRequest = NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build()
131 return runAsShell(NETWORK_SETTINGS) {
132 // Register the network callback is needed here.
133 // This is to avoid the race condition where callback is fired before
134 // acquiring permission.
135 networkCallbackRule.registerNetworkCallback(wifiRequest, networkCallback)
136 return@runAsShell networkCallback.eventuallyExpect<CapabilitiesChanged> {
137 // Remove double quotes.
138 val ssidFromCaps = (WifiInfo::sanitizeSsid)(it.caps.ssid)
139 ssidFromCaps == ssid && it.caps.hasCapability(NET_CAPABILITY_VALIDATED)
Junyu Laicea928f2024-04-24 16:15:30 +0800140 }.network.networkHandle
Junyu Lai36e76262023-12-27 18:17:30 +0800141 }
142 }
143
Junyu Lai5d5a30e2024-04-09 13:42:20 +0800144 @Rpc(description = "Get interface name from NetworkHandle")
145 fun getInterfaceNameFromNetworkHandle(networkHandle: Long): String {
146 val network = Network.fromNetworkHandle(networkHandle)
147 return cm.getLinkProperties(network)!!.getInterfaceName()!!
148 }
149
Junyu Lai36e76262023-12-27 18:17:30 +0800150 @Rpc(description = "Check whether the device supports hotspot feature.")
151 fun hasHotspotFeature(): Boolean {
152 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
153 try {
154 return tetheringCallback.isWifiTetheringSupported(context)
155 } finally {
156 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
157 }
158 }
159
160 @Rpc(description = "Start a hotspot with given SSID and passphrase.")
Junyu Lai5d5a30e2024-04-09 13:42:20 +0800161 fun startHotspot(ssid: String, passphrase: String): String {
Junyu Lai36e76262023-12-27 18:17:30 +0800162 // Store old config.
163 runAsShell(OVERRIDE_WIFI_CONFIG) {
164 oldSoftApConfig = wifiManager.getSoftApConfiguration()
165 }
166
167 val softApConfig = SoftApConfiguration.Builder()
168 .setWifiSsid(WifiSsid.fromBytes(ssid.toByteArray()))
169 .setPassphrase(passphrase, SECURITY_TYPE_WPA2_PSK)
170 .setBand(SoftApConfiguration.BAND_2GHZ)
171 .build()
172 runAsShell(OVERRIDE_WIFI_CONFIG) {
173 wifiManager.setSoftApConfiguration(softApConfig)
174 }
175 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
176 try {
177 tetheringCallback.expectNoTetheringActive()
Junyu Lai5d5a30e2024-04-09 13:42:20 +0800178 return ctsTetheringUtils.startWifiTethering(tetheringCallback).getInterface()
Junyu Lai36e76262023-12-27 18:17:30 +0800179 } finally {
180 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
181 }
182 }
183
184 @Rpc(description = "Stop all tethering.")
185 fun stopAllTethering() {
186 ctsTetheringUtils.stopAllTethering()
187
188 // Restore old config.
189 oldSoftApConfig?.let {
190 runAsShell(OVERRIDE_WIFI_CONFIG) {
191 wifiManager.setSoftApConfiguration(it)
192 }
193 }
194 }
195}