Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.google.snippet.connectivity |
| 18 | |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 19 | import android.Manifest.permission.NETWORK_SETTINGS |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 20 | import android.Manifest.permission.OVERRIDE_WIFI_CONFIG |
| 21 | import android.content.pm.PackageManager.FEATURE_TELEPHONY |
| 22 | import android.content.pm.PackageManager.FEATURE_WIFI |
| 23 | import android.net.ConnectivityManager |
Junyu Lai | 5d5a30e | 2024-04-09 13:42:20 +0800 | [diff] [blame^] | 24 | import android.net.Network |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 25 | import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED |
| 26 | import android.net.NetworkCapabilities.TRANSPORT_WIFI |
| 27 | import android.net.NetworkRequest |
| 28 | import android.net.cts.util.CtsNetUtils |
| 29 | import android.net.cts.util.CtsTetheringUtils |
| 30 | import android.net.wifi.ScanResult |
| 31 | import android.net.wifi.SoftApConfiguration |
| 32 | import android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK |
| 33 | import android.net.wifi.WifiConfiguration |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 34 | import android.net.wifi.WifiInfo |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 35 | import android.net.wifi.WifiManager |
| 36 | import android.net.wifi.WifiNetworkSpecifier |
| 37 | import android.net.wifi.WifiSsid |
| 38 | import androidx.test.platform.app.InstrumentationRegistry |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 39 | import com.android.testutils.AutoReleaseNetworkCallbackRule |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 40 | import com.android.testutils.ConnectUtil |
Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 41 | import com.android.testutils.NetworkCallbackHelper |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 42 | import com.android.testutils.RecorderCallback.CallbackEntry.CapabilitiesChanged |
| 43 | import com.android.testutils.TestableNetworkCallback |
| 44 | import com.android.testutils.runAsShell |
| 45 | import com.google.android.mobly.snippet.Snippet |
| 46 | import com.google.android.mobly.snippet.rpc.Rpc |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 47 | import org.junit.Rule |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 48 | |
| 49 | class ConnectivityMultiDevicesSnippet : Snippet { |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 50 | @get:Rule |
| 51 | val networkCallbackRule = AutoReleaseNetworkCallbackRule() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 52 | private val context = InstrumentationRegistry.getInstrumentation().getTargetContext() |
| 53 | private val wifiManager = context.getSystemService(WifiManager::class.java)!! |
| 54 | private val cm = context.getSystemService(ConnectivityManager::class.java)!! |
| 55 | private val pm = context.packageManager |
| 56 | private val ctsNetUtils = CtsNetUtils(context) |
Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 57 | private val cbHelper = NetworkCallbackHelper() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 58 | private val ctsTetheringUtils = CtsTetheringUtils(context) |
| 59 | private var oldSoftApConfig: SoftApConfiguration? = null |
| 60 | |
Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 61 | override fun shutdown() { |
| 62 | cbHelper.unregisterAll() |
| 63 | } |
| 64 | |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 65 | @Rpc(description = "Check whether the device has wifi feature.") |
| 66 | fun hasWifiFeature() = pm.hasSystemFeature(FEATURE_WIFI) |
| 67 | |
| 68 | @Rpc(description = "Check whether the device has telephony feature.") |
| 69 | fun hasTelephonyFeature() = pm.hasSystemFeature(FEATURE_TELEPHONY) |
| 70 | |
| 71 | @Rpc(description = "Check whether the device supporters AP + STA concurrency.") |
Paul Hu | 2ab7927 | 2024-04-24 05:49:42 +0000 | [diff] [blame] | 72 | fun isStaApConcurrencySupported() = wifiManager.isStaApConcurrencySupported() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 73 | |
| 74 | @Rpc(description = "Request cellular connection and ensure it is the default network.") |
| 75 | fun requestCellularAndEnsureDefault() { |
| 76 | ctsNetUtils.disableWifi() |
Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 77 | val network = cbHelper.requestCell() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 78 | ctsNetUtils.expectNetworkIsSystemDefault(network) |
| 79 | } |
| 80 | |
Junyu Lai | 26c81bd | 2024-06-12 10:45:46 +0800 | [diff] [blame] | 81 | @Rpc(description = "Unregister all connections.") |
| 82 | fun unregisterAll() { |
| 83 | cbHelper.unregisterAll() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Rpc(description = "Ensure any wifi is connected and is the default network.") |
| 87 | fun ensureWifiIsDefault() { |
| 88 | val network = ctsNetUtils.ensureWifiConnected() |
| 89 | ctsNetUtils.expectNetworkIsSystemDefault(network) |
| 90 | } |
| 91 | |
| 92 | @Rpc(description = "Connect to specified wifi network.") |
| 93 | // Suppress warning because WifiManager methods to connect to a config are |
| 94 | // documented not to be deprecated for privileged users. |
| 95 | @Suppress("DEPRECATION") |
Junyu Lai | cea928f | 2024-04-24 16:15:30 +0800 | [diff] [blame] | 96 | fun connectToWifi(ssid: String, passphrase: String): Long { |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 97 | val specifier = WifiNetworkSpecifier.Builder() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 98 | .setBand(ScanResult.WIFI_BAND_24_GHZ) |
| 99 | .build() |
| 100 | val wifiConfig = WifiConfiguration() |
| 101 | wifiConfig.SSID = "\"" + ssid + "\"" |
| 102 | wifiConfig.preSharedKey = "\"" + passphrase + "\"" |
| 103 | wifiConfig.hiddenSSID = true |
| 104 | wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK) |
| 105 | wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP) |
| 106 | wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP) |
| 107 | |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 108 | // Add the test configuration and connect to it. |
| 109 | val connectUtil = ConnectUtil(context) |
| 110 | connectUtil.connectToWifiConfig(wifiConfig) |
| 111 | |
| 112 | // Implement manual SSID matching. Specifying the SSID in |
| 113 | // NetworkSpecifier is ineffective |
| 114 | // (see WifiNetworkAgentSpecifier#canBeSatisfiedBy for details). |
| 115 | // Note that holding permission is necessary when waiting for |
| 116 | // the callbacks. The handler thread checks permission; if |
| 117 | // it's not present, the SSID will be redacted. |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 118 | val networkCallback = TestableNetworkCallback() |
Junyu Lai | b99caad | 2024-04-22 16:22:36 +0800 | [diff] [blame] | 119 | val wifiRequest = NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build() |
| 120 | return runAsShell(NETWORK_SETTINGS) { |
| 121 | // Register the network callback is needed here. |
| 122 | // This is to avoid the race condition where callback is fired before |
| 123 | // acquiring permission. |
| 124 | networkCallbackRule.registerNetworkCallback(wifiRequest, networkCallback) |
| 125 | return@runAsShell networkCallback.eventuallyExpect<CapabilitiesChanged> { |
| 126 | // Remove double quotes. |
| 127 | val ssidFromCaps = (WifiInfo::sanitizeSsid)(it.caps.ssid) |
| 128 | ssidFromCaps == ssid && it.caps.hasCapability(NET_CAPABILITY_VALIDATED) |
Junyu Lai | cea928f | 2024-04-24 16:15:30 +0800 | [diff] [blame] | 129 | }.network.networkHandle |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Junyu Lai | 5d5a30e | 2024-04-09 13:42:20 +0800 | [diff] [blame^] | 133 | @Rpc(description = "Get interface name from NetworkHandle") |
| 134 | fun getInterfaceNameFromNetworkHandle(networkHandle: Long): String { |
| 135 | val network = Network.fromNetworkHandle(networkHandle) |
| 136 | return cm.getLinkProperties(network)!!.getInterfaceName()!! |
| 137 | } |
| 138 | |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 139 | @Rpc(description = "Check whether the device supports hotspot feature.") |
| 140 | fun hasHotspotFeature(): Boolean { |
| 141 | val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback() |
| 142 | try { |
| 143 | return tetheringCallback.isWifiTetheringSupported(context) |
| 144 | } finally { |
| 145 | ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Rpc(description = "Start a hotspot with given SSID and passphrase.") |
Junyu Lai | 5d5a30e | 2024-04-09 13:42:20 +0800 | [diff] [blame^] | 150 | fun startHotspot(ssid: String, passphrase: String): String { |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 151 | // Store old config. |
| 152 | runAsShell(OVERRIDE_WIFI_CONFIG) { |
| 153 | oldSoftApConfig = wifiManager.getSoftApConfiguration() |
| 154 | } |
| 155 | |
| 156 | val softApConfig = SoftApConfiguration.Builder() |
| 157 | .setWifiSsid(WifiSsid.fromBytes(ssid.toByteArray())) |
| 158 | .setPassphrase(passphrase, SECURITY_TYPE_WPA2_PSK) |
| 159 | .setBand(SoftApConfiguration.BAND_2GHZ) |
| 160 | .build() |
| 161 | runAsShell(OVERRIDE_WIFI_CONFIG) { |
| 162 | wifiManager.setSoftApConfiguration(softApConfig) |
| 163 | } |
| 164 | val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback() |
| 165 | try { |
| 166 | tetheringCallback.expectNoTetheringActive() |
Junyu Lai | 5d5a30e | 2024-04-09 13:42:20 +0800 | [diff] [blame^] | 167 | return ctsTetheringUtils.startWifiTethering(tetheringCallback).getInterface() |
Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 168 | } finally { |
| 169 | ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Rpc(description = "Stop all tethering.") |
| 174 | fun stopAllTethering() { |
| 175 | ctsTetheringUtils.stopAllTethering() |
| 176 | |
| 177 | // Restore old config. |
| 178 | oldSoftApConfig?.let { |
| 179 | runAsShell(OVERRIDE_WIFI_CONFIG) { |
| 180 | wifiManager.setSoftApConfiguration(it) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |