blob: 7368669987dc1ee80454fe8b62459585cda8aa85 [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
Paul Hue7fd5a52024-07-01 03:22:59 +000039import com.android.modules.utils.build.SdkLevel
Junyu Laib99caad2024-04-22 16:22:36 +080040import com.android.testutils.AutoReleaseNetworkCallbackRule
Junyu Lai36e76262023-12-27 18:17:30 +080041import com.android.testutils.ConnectUtil
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090042import com.android.testutils.NetworkCallbackHelper
Junyu Lai36e76262023-12-27 18:17:30 +080043import com.android.testutils.RecorderCallback.CallbackEntry.CapabilitiesChanged
44import com.android.testutils.TestableNetworkCallback
45import com.android.testutils.runAsShell
46import com.google.android.mobly.snippet.Snippet
47import com.google.android.mobly.snippet.rpc.Rpc
Junyu Laib99caad2024-04-22 16:22:36 +080048import org.junit.Rule
Junyu Lai36e76262023-12-27 18:17:30 +080049
50class ConnectivityMultiDevicesSnippet : Snippet {
Junyu Laib99caad2024-04-22 16:22:36 +080051 @get:Rule
52 val networkCallbackRule = AutoReleaseNetworkCallbackRule()
Junyu Lai36e76262023-12-27 18:17:30 +080053 private val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
54 private val wifiManager = context.getSystemService(WifiManager::class.java)!!
55 private val cm = context.getSystemService(ConnectivityManager::class.java)!!
56 private val pm = context.packageManager
57 private val ctsNetUtils = CtsNetUtils(context)
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090058 private val cbHelper = NetworkCallbackHelper()
Junyu Lai36e76262023-12-27 18:17:30 +080059 private val ctsTetheringUtils = CtsTetheringUtils(context)
60 private var oldSoftApConfig: SoftApConfiguration? = null
61
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090062 override fun shutdown() {
63 cbHelper.unregisterAll()
64 }
65
Junyu Lai36e76262023-12-27 18:17:30 +080066 @Rpc(description = "Check whether the device has wifi feature.")
67 fun hasWifiFeature() = pm.hasSystemFeature(FEATURE_WIFI)
68
69 @Rpc(description = "Check whether the device has telephony feature.")
70 fun hasTelephonyFeature() = pm.hasSystemFeature(FEATURE_TELEPHONY)
71
72 @Rpc(description = "Check whether the device supporters AP + STA concurrency.")
Paul Hu2ab79272024-04-24 05:49:42 +000073 fun isStaApConcurrencySupported() = wifiManager.isStaApConcurrencySupported()
Junyu Lai36e76262023-12-27 18:17:30 +080074
Paul Hue7fd5a52024-07-01 03:22:59 +000075 @Rpc(description = "Check whether the device SDK is as least T")
76 fun isAtLeastT() = SdkLevel.isAtLeastT()
77
Junyu Lai36e76262023-12-27 18:17:30 +080078 @Rpc(description = "Request cellular connection and ensure it is the default network.")
79 fun requestCellularAndEnsureDefault() {
80 ctsNetUtils.disableWifi()
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090081 val network = cbHelper.requestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080082 ctsNetUtils.expectNetworkIsSystemDefault(network)
83 }
84
Junyu Lai26c81bd2024-06-12 10:45:46 +080085 @Rpc(description = "Unregister all connections.")
86 fun unregisterAll() {
87 cbHelper.unregisterAll()
Junyu Lai36e76262023-12-27 18:17:30 +080088 }
89
90 @Rpc(description = "Ensure any wifi is connected and is the default network.")
91 fun ensureWifiIsDefault() {
92 val network = ctsNetUtils.ensureWifiConnected()
93 ctsNetUtils.expectNetworkIsSystemDefault(network)
94 }
95
96 @Rpc(description = "Connect to specified wifi network.")
97 // Suppress warning because WifiManager methods to connect to a config are
98 // documented not to be deprecated for privileged users.
99 @Suppress("DEPRECATION")
Junyu Laicea928f2024-04-24 16:15:30 +0800100 fun connectToWifi(ssid: String, passphrase: String): Long {
Junyu Lai36e76262023-12-27 18:17:30 +0800101 val specifier = WifiNetworkSpecifier.Builder()
Junyu Lai36e76262023-12-27 18:17:30 +0800102 .setBand(ScanResult.WIFI_BAND_24_GHZ)
103 .build()
104 val wifiConfig = WifiConfiguration()
105 wifiConfig.SSID = "\"" + ssid + "\""
106 wifiConfig.preSharedKey = "\"" + passphrase + "\""
107 wifiConfig.hiddenSSID = true
108 wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK)
109 wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
110 wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
111
Junyu Laib99caad2024-04-22 16:22:36 +0800112 // Add the test configuration and connect to it.
113 val connectUtil = ConnectUtil(context)
114 connectUtil.connectToWifiConfig(wifiConfig)
115
116 // Implement manual SSID matching. Specifying the SSID in
117 // NetworkSpecifier is ineffective
118 // (see WifiNetworkAgentSpecifier#canBeSatisfiedBy for details).
119 // Note that holding permission is necessary when waiting for
120 // the callbacks. The handler thread checks permission; if
121 // it's not present, the SSID will be redacted.
Junyu Lai36e76262023-12-27 18:17:30 +0800122 val networkCallback = TestableNetworkCallback()
Junyu Laib99caad2024-04-22 16:22:36 +0800123 val wifiRequest = NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build()
124 return runAsShell(NETWORK_SETTINGS) {
125 // Register the network callback is needed here.
126 // This is to avoid the race condition where callback is fired before
127 // acquiring permission.
128 networkCallbackRule.registerNetworkCallback(wifiRequest, networkCallback)
129 return@runAsShell networkCallback.eventuallyExpect<CapabilitiesChanged> {
130 // Remove double quotes.
131 val ssidFromCaps = (WifiInfo::sanitizeSsid)(it.caps.ssid)
132 ssidFromCaps == ssid && it.caps.hasCapability(NET_CAPABILITY_VALIDATED)
Junyu Laicea928f2024-04-24 16:15:30 +0800133 }.network.networkHandle
Junyu Lai36e76262023-12-27 18:17:30 +0800134 }
135 }
136
Junyu Lai5d5a30e2024-04-09 13:42:20 +0800137 @Rpc(description = "Get interface name from NetworkHandle")
138 fun getInterfaceNameFromNetworkHandle(networkHandle: Long): String {
139 val network = Network.fromNetworkHandle(networkHandle)
140 return cm.getLinkProperties(network)!!.getInterfaceName()!!
141 }
142
Junyu Lai36e76262023-12-27 18:17:30 +0800143 @Rpc(description = "Check whether the device supports hotspot feature.")
144 fun hasHotspotFeature(): Boolean {
145 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
146 try {
147 return tetheringCallback.isWifiTetheringSupported(context)
148 } finally {
149 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
150 }
151 }
152
153 @Rpc(description = "Start a hotspot with given SSID and passphrase.")
Junyu Lai5d5a30e2024-04-09 13:42:20 +0800154 fun startHotspot(ssid: String, passphrase: String): String {
Junyu Lai36e76262023-12-27 18:17:30 +0800155 // Store old config.
156 runAsShell(OVERRIDE_WIFI_CONFIG) {
157 oldSoftApConfig = wifiManager.getSoftApConfiguration()
158 }
159
160 val softApConfig = SoftApConfiguration.Builder()
161 .setWifiSsid(WifiSsid.fromBytes(ssid.toByteArray()))
162 .setPassphrase(passphrase, SECURITY_TYPE_WPA2_PSK)
163 .setBand(SoftApConfiguration.BAND_2GHZ)
164 .build()
165 runAsShell(OVERRIDE_WIFI_CONFIG) {
166 wifiManager.setSoftApConfiguration(softApConfig)
167 }
168 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
169 try {
170 tetheringCallback.expectNoTetheringActive()
Junyu Lai5d5a30e2024-04-09 13:42:20 +0800171 return ctsTetheringUtils.startWifiTethering(tetheringCallback).getInterface()
Junyu Lai36e76262023-12-27 18:17:30 +0800172 } finally {
173 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
174 }
175 }
176
177 @Rpc(description = "Stop all tethering.")
178 fun stopAllTethering() {
179 ctsTetheringUtils.stopAllTethering()
180
181 // Restore old config.
182 oldSoftApConfig?.let {
183 runAsShell(OVERRIDE_WIFI_CONFIG) {
184 wifiManager.setSoftApConfiguration(it)
185 }
186 }
187 }
188}