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