| 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 |  | 
|  | 19 | import android.Manifest.permission.OVERRIDE_WIFI_CONFIG | 
|  | 20 | import android.content.pm.PackageManager.FEATURE_TELEPHONY | 
|  | 21 | import android.content.pm.PackageManager.FEATURE_WIFI | 
|  | 22 | import android.net.ConnectivityManager | 
|  | 23 | import android.net.Network | 
|  | 24 | import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED | 
|  | 25 | import android.net.NetworkCapabilities.TRANSPORT_WIFI | 
|  | 26 | import android.net.NetworkRequest | 
|  | 27 | import android.net.cts.util.CtsNetUtils | 
|  | 28 | import android.net.cts.util.CtsTetheringUtils | 
|  | 29 | import android.net.wifi.ScanResult | 
|  | 30 | import android.net.wifi.SoftApConfiguration | 
|  | 31 | import android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK | 
|  | 32 | import android.net.wifi.WifiConfiguration | 
|  | 33 | import android.net.wifi.WifiManager | 
|  | 34 | import android.net.wifi.WifiNetworkSpecifier | 
|  | 35 | import android.net.wifi.WifiSsid | 
|  | 36 | import androidx.test.platform.app.InstrumentationRegistry | 
|  | 37 | import com.android.testutils.ConnectUtil | 
| Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 38 | import com.android.testutils.NetworkCallbackHelper | 
| Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 39 | import com.android.testutils.RecorderCallback.CallbackEntry.Available | 
|  | 40 | import com.android.testutils.RecorderCallback.CallbackEntry.CapabilitiesChanged | 
|  | 41 | import com.android.testutils.TestableNetworkCallback | 
|  | 42 | import com.android.testutils.runAsShell | 
|  | 43 | import com.google.android.mobly.snippet.Snippet | 
|  | 44 | import com.google.android.mobly.snippet.rpc.Rpc | 
|  | 45 |  | 
|  | 46 | class ConnectivityMultiDevicesSnippet : Snippet { | 
|  | 47 | private val context = InstrumentationRegistry.getInstrumentation().getTargetContext() | 
|  | 48 | private val wifiManager = context.getSystemService(WifiManager::class.java)!! | 
|  | 49 | private val cm = context.getSystemService(ConnectivityManager::class.java)!! | 
|  | 50 | private val pm = context.packageManager | 
|  | 51 | private val ctsNetUtils = CtsNetUtils(context) | 
| Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 52 | private val cbHelper = NetworkCallbackHelper() | 
| Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 53 | private val ctsTetheringUtils = CtsTetheringUtils(context) | 
|  | 54 | private var oldSoftApConfig: SoftApConfiguration? = null | 
|  | 55 |  | 
| Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 56 | override fun shutdown() { | 
|  | 57 | cbHelper.unregisterAll() | 
|  | 58 | } | 
|  | 59 |  | 
| Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 60 | @Rpc(description = "Check whether the device has wifi feature.") | 
|  | 61 | fun hasWifiFeature() = pm.hasSystemFeature(FEATURE_WIFI) | 
|  | 62 |  | 
|  | 63 | @Rpc(description = "Check whether the device has telephony feature.") | 
|  | 64 | fun hasTelephonyFeature() = pm.hasSystemFeature(FEATURE_TELEPHONY) | 
|  | 65 |  | 
|  | 66 | @Rpc(description = "Check whether the device supporters AP + STA concurrency.") | 
| Paul Hu | 2ab7927 | 2024-04-24 05:49:42 +0000 | [diff] [blame] | 67 | fun isStaApConcurrencySupported() = wifiManager.isStaApConcurrencySupported() | 
| Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 68 |  | 
|  | 69 | @Rpc(description = "Request cellular connection and ensure it is the default network.") | 
|  | 70 | fun requestCellularAndEnsureDefault() { | 
|  | 71 | ctsNetUtils.disableWifi() | 
| Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 72 | val network = cbHelper.requestCell() | 
| Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 73 | ctsNetUtils.expectNetworkIsSystemDefault(network) | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | @Rpc(description = "Unrequest cellular connection.") | 
|  | 77 | fun unrequestCellular() { | 
| Remi NGUYEN VAN | e629bb7 | 2024-03-26 18:12:46 +0900 | [diff] [blame] | 78 | cbHelper.unrequestCell() | 
| Junyu Lai | 36e7626 | 2023-12-27 18:17:30 +0800 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
|  | 81 | @Rpc(description = "Ensure any wifi is connected and is the default network.") | 
|  | 82 | fun ensureWifiIsDefault() { | 
|  | 83 | val network = ctsNetUtils.ensureWifiConnected() | 
|  | 84 | ctsNetUtils.expectNetworkIsSystemDefault(network) | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | @Rpc(description = "Connect to specified wifi network.") | 
|  | 88 | // Suppress warning because WifiManager methods to connect to a config are | 
|  | 89 | // documented not to be deprecated for privileged users. | 
|  | 90 | @Suppress("DEPRECATION") | 
|  | 91 | fun connectToWifi(ssid: String, passphrase: String, requireValidation: Boolean): Network { | 
|  | 92 | val specifier = WifiNetworkSpecifier.Builder() | 
|  | 93 | .setSsid(ssid) | 
|  | 94 | .setWpa2Passphrase(passphrase) | 
|  | 95 | .setBand(ScanResult.WIFI_BAND_24_GHZ) | 
|  | 96 | .build() | 
|  | 97 | val wifiConfig = WifiConfiguration() | 
|  | 98 | wifiConfig.SSID = "\"" + ssid + "\"" | 
|  | 99 | wifiConfig.preSharedKey = "\"" + passphrase + "\"" | 
|  | 100 | wifiConfig.hiddenSSID = true | 
|  | 101 | wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK) | 
|  | 102 | wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP) | 
|  | 103 | wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP) | 
|  | 104 |  | 
|  | 105 | // Register network callback for the specific wifi. | 
|  | 106 | val networkCallback = TestableNetworkCallback() | 
|  | 107 | val wifiRequest = NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI) | 
|  | 108 | .setNetworkSpecifier(specifier) | 
|  | 109 | .build() | 
|  | 110 | cm.registerNetworkCallback(wifiRequest, networkCallback) | 
|  | 111 |  | 
|  | 112 | try { | 
|  | 113 | // Add the test configuration and connect to it. | 
|  | 114 | val connectUtil = ConnectUtil(context) | 
|  | 115 | connectUtil.connectToWifiConfig(wifiConfig) | 
|  | 116 |  | 
|  | 117 | val event = networkCallback.expect<Available>() | 
|  | 118 | if (requireValidation) { | 
|  | 119 | networkCallback.eventuallyExpect<CapabilitiesChanged> { | 
|  | 120 | it.caps.hasCapability(NET_CAPABILITY_VALIDATED) | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 | return event.network | 
|  | 124 | } finally { | 
|  | 125 | cm.unregisterNetworkCallback(networkCallback) | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | @Rpc(description = "Check whether the device supports hotspot feature.") | 
|  | 130 | fun hasHotspotFeature(): Boolean { | 
|  | 131 | val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback() | 
|  | 132 | try { | 
|  | 133 | return tetheringCallback.isWifiTetheringSupported(context) | 
|  | 134 | } finally { | 
|  | 135 | ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback) | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | @Rpc(description = "Start a hotspot with given SSID and passphrase.") | 
|  | 140 | fun startHotspot(ssid: String, passphrase: String) { | 
|  | 141 | // Store old config. | 
|  | 142 | runAsShell(OVERRIDE_WIFI_CONFIG) { | 
|  | 143 | oldSoftApConfig = wifiManager.getSoftApConfiguration() | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | val softApConfig = SoftApConfiguration.Builder() | 
|  | 147 | .setWifiSsid(WifiSsid.fromBytes(ssid.toByteArray())) | 
|  | 148 | .setPassphrase(passphrase, SECURITY_TYPE_WPA2_PSK) | 
|  | 149 | .setBand(SoftApConfiguration.BAND_2GHZ) | 
|  | 150 | .build() | 
|  | 151 | runAsShell(OVERRIDE_WIFI_CONFIG) { | 
|  | 152 | wifiManager.setSoftApConfiguration(softApConfig) | 
|  | 153 | } | 
|  | 154 | val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback() | 
|  | 155 | try { | 
|  | 156 | tetheringCallback.expectNoTetheringActive() | 
|  | 157 | ctsTetheringUtils.startWifiTethering(tetheringCallback) | 
|  | 158 | } finally { | 
|  | 159 | ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback) | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | @Rpc(description = "Stop all tethering.") | 
|  | 164 | fun stopAllTethering() { | 
|  | 165 | ctsTetheringUtils.stopAllTethering() | 
|  | 166 |  | 
|  | 167 | // Restore old config. | 
|  | 168 | oldSoftApConfig?.let { | 
|  | 169 | runAsShell(OVERRIDE_WIFI_CONFIG) { | 
|  | 170 | wifiManager.setSoftApConfiguration(it) | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 | } |