blob: c883b7803b37fdee52abb3d5083b322b5057b5c8 [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
19import android.Manifest.permission.OVERRIDE_WIFI_CONFIG
20import android.content.pm.PackageManager.FEATURE_TELEPHONY
21import android.content.pm.PackageManager.FEATURE_WIFI
22import android.net.ConnectivityManager
23import android.net.Network
24import 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
33import android.net.wifi.WifiManager
34import android.net.wifi.WifiNetworkSpecifier
35import android.net.wifi.WifiSsid
36import androidx.test.platform.app.InstrumentationRegistry
37import com.android.testutils.ConnectUtil
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090038import com.android.testutils.NetworkCallbackHelper
Junyu Lai36e76262023-12-27 18:17:30 +080039import com.android.testutils.RecorderCallback.CallbackEntry.Available
40import com.android.testutils.RecorderCallback.CallbackEntry.CapabilitiesChanged
41import com.android.testutils.TestableNetworkCallback
42import com.android.testutils.runAsShell
43import com.google.android.mobly.snippet.Snippet
44import com.google.android.mobly.snippet.rpc.Rpc
45
46class 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 VANe629bb72024-03-26 18:12:46 +090052 private val cbHelper = NetworkCallbackHelper()
Junyu Lai36e76262023-12-27 18:17:30 +080053 private val ctsTetheringUtils = CtsTetheringUtils(context)
54 private var oldSoftApConfig: SoftApConfiguration? = null
55
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090056 override fun shutdown() {
57 cbHelper.unregisterAll()
58 }
59
Junyu Lai36e76262023-12-27 18:17:30 +080060 @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.")
67 fun isStaApConcurrencySupported() {
68 wifiManager.isStaApConcurrencySupported()
69 }
70
71 @Rpc(description = "Request cellular connection and ensure it is the default network.")
72 fun requestCellularAndEnsureDefault() {
73 ctsNetUtils.disableWifi()
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090074 val network = cbHelper.requestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080075 ctsNetUtils.expectNetworkIsSystemDefault(network)
76 }
77
78 @Rpc(description = "Unrequest cellular connection.")
79 fun unrequestCellular() {
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090080 cbHelper.unrequestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080081 }
82
83 @Rpc(description = "Ensure any wifi is connected and is the default network.")
84 fun ensureWifiIsDefault() {
85 val network = ctsNetUtils.ensureWifiConnected()
86 ctsNetUtils.expectNetworkIsSystemDefault(network)
87 }
88
89 @Rpc(description = "Connect to specified wifi network.")
90 // Suppress warning because WifiManager methods to connect to a config are
91 // documented not to be deprecated for privileged users.
92 @Suppress("DEPRECATION")
93 fun connectToWifi(ssid: String, passphrase: String, requireValidation: Boolean): Network {
94 val specifier = WifiNetworkSpecifier.Builder()
95 .setSsid(ssid)
96 .setWpa2Passphrase(passphrase)
97 .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
107 // Register network callback for the specific wifi.
108 val networkCallback = TestableNetworkCallback()
109 val wifiRequest = NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI)
110 .setNetworkSpecifier(specifier)
111 .build()
112 cm.registerNetworkCallback(wifiRequest, networkCallback)
113
114 try {
115 // Add the test configuration and connect to it.
116 val connectUtil = ConnectUtil(context)
117 connectUtil.connectToWifiConfig(wifiConfig)
118
119 val event = networkCallback.expect<Available>()
120 if (requireValidation) {
121 networkCallback.eventuallyExpect<CapabilitiesChanged> {
122 it.caps.hasCapability(NET_CAPABILITY_VALIDATED)
123 }
124 }
125 return event.network
126 } finally {
127 cm.unregisterNetworkCallback(networkCallback)
128 }
129 }
130
131 @Rpc(description = "Check whether the device supports hotspot feature.")
132 fun hasHotspotFeature(): Boolean {
133 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
134 try {
135 return tetheringCallback.isWifiTetheringSupported(context)
136 } finally {
137 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
138 }
139 }
140
141 @Rpc(description = "Start a hotspot with given SSID and passphrase.")
142 fun startHotspot(ssid: String, passphrase: String) {
143 // Store old config.
144 runAsShell(OVERRIDE_WIFI_CONFIG) {
145 oldSoftApConfig = wifiManager.getSoftApConfiguration()
146 }
147
148 val softApConfig = SoftApConfiguration.Builder()
149 .setWifiSsid(WifiSsid.fromBytes(ssid.toByteArray()))
150 .setPassphrase(passphrase, SECURITY_TYPE_WPA2_PSK)
151 .setBand(SoftApConfiguration.BAND_2GHZ)
152 .build()
153 runAsShell(OVERRIDE_WIFI_CONFIG) {
154 wifiManager.setSoftApConfiguration(softApConfig)
155 }
156 val tetheringCallback = ctsTetheringUtils.registerTetheringEventCallback()
157 try {
158 tetheringCallback.expectNoTetheringActive()
159 ctsTetheringUtils.startWifiTethering(tetheringCallback)
160 } finally {
161 ctsTetheringUtils.unregisterTetheringEventCallback(tetheringCallback)
162 }
163 }
164
165 @Rpc(description = "Stop all tethering.")
166 fun stopAllTethering() {
167 ctsTetheringUtils.stopAllTethering()
168
169 // Restore old config.
170 oldSoftApConfig?.let {
171 runAsShell(OVERRIDE_WIFI_CONFIG) {
172 wifiManager.setSoftApConfiguration(it)
173 }
174 }
175 }
176}