blob: 258648f6b1b60f469dfd95759ad0fa8dd84f41bd [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.")
Paul Hu2ab79272024-04-24 05:49:42 +000067 fun isStaApConcurrencySupported() = wifiManager.isStaApConcurrencySupported()
Junyu Lai36e76262023-12-27 18:17:30 +080068
69 @Rpc(description = "Request cellular connection and ensure it is the default network.")
70 fun requestCellularAndEnsureDefault() {
71 ctsNetUtils.disableWifi()
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090072 val network = cbHelper.requestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080073 ctsNetUtils.expectNetworkIsSystemDefault(network)
74 }
75
76 @Rpc(description = "Unrequest cellular connection.")
77 fun unrequestCellular() {
Remi NGUYEN VANe629bb72024-03-26 18:12:46 +090078 cbHelper.unrequestCell()
Junyu Lai36e76262023-12-27 18:17:30 +080079 }
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}