blob: 8805edd818db314f7b6a387887b2098ea2343577 [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 Lai36e76262023-12-27 18:17:30 +080024import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED
25import android.net.NetworkCapabilities.TRANSPORT_WIFI
26import android.net.NetworkRequest
27import android.net.cts.util.CtsNetUtils
28import android.net.cts.util.CtsTetheringUtils
29import android.net.wifi.ScanResult
30import android.net.wifi.SoftApConfiguration
31import android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK
32import android.net.wifi.WifiConfiguration
Junyu Laib99caad2024-04-22 16:22:36 +080033import android.net.wifi.WifiInfo
Junyu Lai36e76262023-12-27 18:17:30 +080034import android.net.wifi.WifiManager
35import android.net.wifi.WifiNetworkSpecifier
36import android.net.wifi.WifiSsid
37import androidx.test.platform.app.InstrumentationRegistry
Junyu Laib99caad2024-04-22 16:22:36 +080038import com.android.testutils.AutoReleaseNetworkCallbackRule
Junyu Lai36e76262023-12-27 18:17:30 +080039import com.android.testutils.ConnectUtil
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090040import com.android.testutils.NetworkCallbackHelper
Junyu Lai36e76262023-12-27 18:17:30 +080041import com.android.testutils.RecorderCallback.CallbackEntry.CapabilitiesChanged
42import com.android.testutils.TestableNetworkCallback
43import com.android.testutils.runAsShell
44import com.google.android.mobly.snippet.Snippet
45import com.google.android.mobly.snippet.rpc.Rpc
Junyu Laib99caad2024-04-22 16:22:36 +080046import org.junit.Rule
Junyu Lai36e76262023-12-27 18:17:30 +080047
48class ConnectivityMultiDevicesSnippet : Snippet {
Junyu Laib99caad2024-04-22 16:22:36 +080049 @get:Rule
50 val networkCallbackRule = AutoReleaseNetworkCallbackRule()
Junyu Lai36e76262023-12-27 18:17:30 +080051 private val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
52 private val wifiManager = context.getSystemService(WifiManager::class.java)!!
53 private val cm = context.getSystemService(ConnectivityManager::class.java)!!
54 private val pm = context.packageManager
55 private val ctsNetUtils = CtsNetUtils(context)
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090056 private val cbHelper = NetworkCallbackHelper()
Junyu Lai36e76262023-12-27 18:17:30 +080057 private val ctsTetheringUtils = CtsTetheringUtils(context)
58 private var oldSoftApConfig: SoftApConfiguration? = null
59
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090060 override fun shutdown() {
61 cbHelper.unregisterAll()
62 }
63
Junyu Lai36e76262023-12-27 18:17:30 +080064 @Rpc(description = "Check whether the device has wifi feature.")
65 fun hasWifiFeature() = pm.hasSystemFeature(FEATURE_WIFI)
66
67 @Rpc(description = "Check whether the device has telephony feature.")
68 fun hasTelephonyFeature() = pm.hasSystemFeature(FEATURE_TELEPHONY)
69
70 @Rpc(description = "Check whether the device supporters AP + STA concurrency.")
Paul Hu2ab79272024-04-24 05:49:42 +000071 fun isStaApConcurrencySupported() = wifiManager.isStaApConcurrencySupported()
Junyu Lai36e76262023-12-27 18:17:30 +080072
73 @Rpc(description = "Request cellular connection and ensure it is the default network.")
74 fun requestCellularAndEnsureDefault() {
75 ctsNetUtils.disableWifi()
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090076 val network = cbHelper.requestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080077 ctsNetUtils.expectNetworkIsSystemDefault(network)
78 }
79
80 @Rpc(description = "Unrequest cellular connection.")
81 fun unrequestCellular() {
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090082 cbHelper.unrequestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080083 }
84
85 @Rpc(description = "Ensure any wifi is connected and is the default network.")
86 fun ensureWifiIsDefault() {
87 val network = ctsNetUtils.ensureWifiConnected()
88 ctsNetUtils.expectNetworkIsSystemDefault(network)
89 }
90
91 @Rpc(description = "Connect to specified wifi network.")
92 // Suppress warning because WifiManager methods to connect to a config are
93 // documented not to be deprecated for privileged users.
94 @Suppress("DEPRECATION")
Junyu Laicea928f2024-04-24 16:15:30 +080095 fun connectToWifi(ssid: String, passphrase: String): Long {
Junyu Lai36e76262023-12-27 18:17:30 +080096 val specifier = WifiNetworkSpecifier.Builder()
Junyu Lai36e76262023-12-27 18:17:30 +080097 .setBand(ScanResult.WIFI_BAND_24_GHZ)
98 .build()
99 val wifiConfig = WifiConfiguration()
100 wifiConfig.SSID = "\"" + ssid + "\""
101 wifiConfig.preSharedKey = "\"" + passphrase + "\""
102 wifiConfig.hiddenSSID = true
103 wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK)
104 wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
105 wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
106
Junyu Laib99caad2024-04-22 16:22:36 +0800107 // Add the test configuration and connect to it.
108 val connectUtil = ConnectUtil(context)
109 connectUtil.connectToWifiConfig(wifiConfig)
110
111 // Implement manual SSID matching. Specifying the SSID in
112 // NetworkSpecifier is ineffective
113 // (see WifiNetworkAgentSpecifier#canBeSatisfiedBy for details).
114 // Note that holding permission is necessary when waiting for
115 // the callbacks. The handler thread checks permission; if
116 // it's not present, the SSID will be redacted.
Junyu Lai36e76262023-12-27 18:17:30 +0800117 val networkCallback = TestableNetworkCallback()
Junyu Laib99caad2024-04-22 16:22:36 +0800118 val wifiRequest = NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build()
119 return runAsShell(NETWORK_SETTINGS) {
120 // Register the network callback is needed here.
121 // This is to avoid the race condition where callback is fired before
122 // acquiring permission.
123 networkCallbackRule.registerNetworkCallback(wifiRequest, networkCallback)
124 return@runAsShell networkCallback.eventuallyExpect<CapabilitiesChanged> {
125 // Remove double quotes.
126 val ssidFromCaps = (WifiInfo::sanitizeSsid)(it.caps.ssid)
127 ssidFromCaps == ssid && it.caps.hasCapability(NET_CAPABILITY_VALIDATED)
Junyu Laicea928f2024-04-24 16:15:30 +0800128 }.network.networkHandle
Junyu Lai36e76262023-12-27 18:17:30 +0800129 }
130 }
131
132 @Rpc(description = "Check whether the device supports hotspot feature.")
133 fun hasHotspotFeature(): Boolean {
134 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
135 try {
136 return tetheringCallback.isWifiTetheringSupported(context)
137 } finally {
138 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
139 }
140 }
141
142 @Rpc(description = "Start a hotspot with given SSID and passphrase.")
143 fun startHotspot(ssid: String, passphrase: String) {
144 // Store old config.
145 runAsShell(OVERRIDE_WIFI_CONFIG) {
146 oldSoftApConfig = wifiManager.getSoftApConfiguration()
147 }
148
149 val softApConfig = SoftApConfiguration.Builder()
150 .setWifiSsid(WifiSsid.fromBytes(ssid.toByteArray()))
151 .setPassphrase(passphrase, SECURITY_TYPE_WPA2_PSK)
152 .setBand(SoftApConfiguration.BAND_2GHZ)
153 .build()
154 runAsShell(OVERRIDE_WIFI_CONFIG) {
155 wifiManager.setSoftApConfiguration(softApConfig)
156 }
157 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
158 try {
159 tetheringCallback.expectNoTetheringActive()
160 ctsTetheringUtils.startWifiTethering(tetheringCallback)
161 } finally {
162 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
163 }
164 }
165
166 @Rpc(description = "Stop all tethering.")
167 fun stopAllTethering() {
168 ctsTetheringUtils.stopAllTethering()
169
170 // Restore old config.
171 oldSoftApConfig?.let {
172 runAsShell(OVERRIDE_WIFI_CONFIG) {
173 wifiManager.setSoftApConfiguration(it)
174 }
175 }
176 }
177}