Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 - 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 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 16 | import threading |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 17 | import time |
| 18 | |
| 19 | from acts import asserts |
| 20 | from acts import base_test |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 21 | from acts import utils |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 22 | from acts.controllers import adb |
David Goldfarb | 78a3b67 | 2020-03-31 12:44:01 -0700 | [diff] [blame] | 23 | from acts.controllers.adb_lib.error import AdbError |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 24 | from acts.controllers.ap_lib import hostapd_constants |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 25 | from acts.test_decorators import test_tracker_info |
Xianyuan Jia | a4913cd | 2020-11-16 11:58:29 -0800 | [diff] [blame] | 26 | from acts.test_utils.net import connectivity_const as cconst |
| 27 | from acts.test_utils.net import connectivity_test_utils as cutils |
| 28 | from acts.test_utils.net import net_test_utils as nutils |
| 29 | from acts.test_utils.net.net_test_utils import start_tcpdump |
| 30 | from acts.test_utils.net.net_test_utils import stop_tcpdump |
| 31 | from acts.test_utils.tel import tel_test_utils as ttutils |
| 32 | from acts.test_utils.tel.tel_test_utils import get_operator_name |
| 33 | from acts.test_utils.tel.tel_test_utils import http_file_download_by_chrome |
| 34 | from acts.test_utils.wifi import wifi_test_utils as wutils |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 35 | import queue |
| 36 | from queue import Empty |
| 37 | |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 38 | |
| 39 | conn_test_class = "com.android.tests.connectivity.uid.ConnectivityTestActivity" |
| 40 | android_os_class = "com.quicinc.cne.CNEService.CNEServiceApp" |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 41 | instr_cmd = ("am instrument -w -e command grant-all " |
| 42 | "com.android.permissionutils/.PermissionInstrumentation") |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 43 | |
| 44 | HOUR_IN_MILLIS = 1000 * 60 * 60 |
| 45 | BYTE_TO_MB_ANDROID = 1000.0 * 1000.0 |
| 46 | BYTE_TO_MB = 1024.0 * 1024.0 |
| 47 | DOWNLOAD_PATH = "/sdcard/download/" |
| 48 | DATA_USG_ERR = 2.2 |
| 49 | DATA_ERR = 0.2 |
| 50 | TIMEOUT = 2 * 60 |
| 51 | INC_DATA = 10 |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 52 | AP_PASSPHRASE_LENGTH_2G = hostapd_constants.AP_PASSPHRASE_LENGTH_2G |
| 53 | AP_SSID_LENGTH_2G = hostapd_constants.AP_SSID_LENGTH_2G |
| 54 | RETRY_SLEEP = 3 |
| 55 | WIFI_SLEEP = 180 |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 56 | |
| 57 | |
| 58 | class DataUsageTest(base_test.BaseTestClass): |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 59 | """Data usage tests. |
| 60 | |
| 61 | Requirements: |
| 62 | Two Pixel devices - one device with TMO and other with VZW SIM |
| 63 | WiFi network - Wifi network to test wifi data usage |
| 64 | """ |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 65 | |
| 66 | def __init__(self, controllers): |
| 67 | base_test.BaseTestClass.__init__(self, controllers) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 68 | |
| 69 | def setup_class(self): |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 70 | """Setup devices for tests and unpack params.""" |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 71 | |
| 72 | # unpack user params |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 73 | req_params = ("wifi_network", |
| 74 | "file_size", |
| 75 | "download_file_ipv4", |
| 76 | "download_file_ipv6", |
| 77 | "dbs_supported_models") |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 78 | self.unpack_userparams(req_params) |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 79 | self.file_path = DOWNLOAD_PATH + self.download_file_ipv4.split("/")[-1] |
| 80 | self.file_size = float(self.file_size) |
| 81 | |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 82 | for ad in self.android_devices: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 83 | self.log.info("Operator on %s is %s" % \ |
| 84 | (ad.serial, get_operator_name(self.log, ad))) |
| 85 | ad.reboot() |
| 86 | nutils.verify_lte_data_and_tethering_supported(ad) |
| 87 | nutils.set_chrome_browser_permissions(ad) |
| 88 | cutils.set_private_dns(ad, cconst.PRIVATE_DNS_MODE_OFF) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 89 | try: |
| 90 | ad.adb.shell(instr_cmd) |
David Goldfarb | 78a3b67 | 2020-03-31 12:44:01 -0700 | [diff] [blame] | 91 | except AdbError: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 92 | self.log.warn("cmd %s failed on %s" % (instr_cmd, ad.serial)) |
| 93 | self.tcpdumps = [] |
| 94 | self.hs_enabled = [] |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 95 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 96 | def setup_test(self): |
| 97 | for ad in self.android_devices: |
| 98 | self.tcpdumps.append(start_tcpdump(ad, self.test_name)) |
| 99 | ad.droid.wakeLockAcquireBright() |
| 100 | ad.droid.wakeUpNow() |
| 101 | |
| 102 | def teardown_test(self): |
| 103 | for ad in self.hs_enabled: |
| 104 | try: |
| 105 | wutils.stop_wifi_tethering(ad) |
| 106 | except Exception as e: |
| 107 | self.log.warn("Failed to stop wifi tethering: %s" % e) |
| 108 | self.hs_enabled = [] |
| 109 | for i in range(len(self.android_devices)): |
| 110 | ad = self.android_devices[i] |
| 111 | stop_tcpdump(ad, self.tcpdumps[i], self.test_name) |
| 112 | wutils.reset_wifi(ad) |
| 113 | sub_id = str(ad.droid.telephonyGetSubscriberId()) |
| 114 | ad.droid.connectivityFactoryResetNetworkPolicies(sub_id) |
| 115 | ad.droid.telephonyToggleDataConnection(True) |
| 116 | ad.droid.wakeLockRelease() |
| 117 | ad.droid.goToSleepNow() |
| 118 | self.tcpdumps = [] |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 119 | |
| 120 | def teardown_class(self): |
Girish Moturu | b9d573e | 2018-07-17 22:42:18 -0700 | [diff] [blame] | 121 | for ad in self.android_devices: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 122 | cutils.set_private_dns(ad, cconst.PRIVATE_DNS_MODE_OPPORTUNISTIC) |
Girish Moturu | b9d573e | 2018-07-17 22:42:18 -0700 | [diff] [blame] | 123 | |
| 124 | def on_fail(self, test_name, begin_time): |
| 125 | for ad in self.android_devices: |
| 126 | ad.take_bug_report(test_name, begin_time) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 127 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 128 | ### Helper functions |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 129 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 130 | def _download_data_through_app(self, ad, file_link): |
| 131 | """Download data through app on DUT. |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 132 | |
| 133 | Args: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 134 | ad: DUT to download the file on |
| 135 | file_link: http link of the file to download |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 136 | |
| 137 | Returns: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 138 | True if file download is successful, False if not |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 139 | """ |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 140 | intent = ad.droid.createIntentForClassName(conn_test_class) |
| 141 | json_obj = {"url": file_link} |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 142 | ad.droid.launchForResultWithIntent(intent, json_obj) |
| 143 | download_status = False |
| 144 | end_time = time.time() + TIMEOUT |
| 145 | while time.time() < end_time: |
| 146 | download_status = ttutils._check_file_existance( |
| 147 | ad, self.file_path, self.file_size * BYTE_TO_MB) |
| 148 | if download_status: |
| 149 | self.log.info("Delete file: %s", self.file_path) |
| 150 | ad.adb.shell("rm %s" % self.file_path, ignore_status=True) |
| 151 | break |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 152 | time.sleep(RETRY_SLEEP*2) # wait to check if download is complete |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 153 | return download_status |
| 154 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 155 | def _get_total_data_usage_for_device(self, ad, conn_type): |
| 156 | """Get total data usage in MB for device. |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 157 | |
| 158 | Args: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 159 | ad: android device object |
| 160 | conn_type: MOBILE/WIFI data usage |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 161 | |
| 162 | Returns: |
| 163 | Data usage in MB |
| 164 | """ |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 165 | sub_id = str(ad.droid.telephonyGetSubscriberId()) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 166 | end_time = int(time.time() * 1000) + 2 * HOUR_IN_MILLIS |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 167 | data_usage = ad.droid.connectivityQuerySummaryForDevice( |
| 168 | conn_type, sub_id, 0, end_time) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 169 | data_usage /= BYTE_TO_MB_ANDROID |
| 170 | self.log.info("Total data usage is: %s" % data_usage) |
| 171 | return data_usage |
| 172 | |
| 173 | def _get_data_usage_for_uid_rx(self, ad, conn_type, uid): |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 174 | """Get data usage for UID in Rx Bytes. |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 175 | |
| 176 | Args: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 177 | ad: DUT to get data usage from |
| 178 | conn_type: MOBILE/WIFI data usage |
| 179 | uid: UID of the app |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 180 | |
| 181 | Returns: |
| 182 | Data usage in MB |
| 183 | """ |
| 184 | subscriber_id = ad.droid.telephonyGetSubscriberId() |
| 185 | end_time = int(time.time() * 1000) + 2 * HOUR_IN_MILLIS |
| 186 | data_usage = ad.droid.connectivityQueryDetailsForUidRxBytes( |
| 187 | conn_type, subscriber_id, 0, end_time, uid) |
| 188 | return data_usage/BYTE_TO_MB_ANDROID |
| 189 | |
| 190 | def _get_data_usage_for_device_rx(self, ad, conn_type): |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 191 | """Get total data usage in rx bytes for device. |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 192 | |
| 193 | Args: |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 194 | ad: DUT to get data usage from |
| 195 | conn_type: MOBILE/WIFI data usage |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 196 | |
| 197 | Returns: |
| 198 | Data usage in MB |
| 199 | """ |
| 200 | subscriber_id = ad.droid.telephonyGetSubscriberId() |
| 201 | end_time = int(time.time() * 1000) + 2 * HOUR_IN_MILLIS |
| 202 | data_usage = ad.droid.connectivityQuerySummaryForDeviceRxBytes( |
| 203 | conn_type, subscriber_id, 0, end_time) |
| 204 | return data_usage/BYTE_TO_MB_ANDROID |
| 205 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 206 | def _get_data_usage(self, ad, conn_type): |
| 207 | """Get data usage. |
| 208 | |
| 209 | Args: |
| 210 | ad: DUT to get data usage from |
| 211 | conn_type: MOBILE/WIFI data usage |
| 212 | |
| 213 | Returns: |
| 214 | Tuple of Android Os app, Conn UID app, Total data usages |
| 215 | """ |
| 216 | android_os_uid = ad.droid.getUidForPackage(android_os_class) |
| 217 | conn_test_uid = ad.droid.getUidForPackage(conn_test_class) |
| 218 | aos = self._get_data_usage_for_uid_rx(ad, conn_type, android_os_uid) |
| 219 | app = self._get_data_usage_for_uid_rx(ad, conn_type, conn_test_uid) |
| 220 | tot = self._get_data_usage_for_device_rx(ad, conn_type) |
| 221 | self.log.info("Android Os data usage: %s" % aos) |
| 222 | self.log.info("Conn UID Test data usage: %s" % app) |
| 223 | self.log.info("Total data usage: %s" % tot) |
| 224 | return (aos, app, tot) |
| 225 | |
| 226 | def _listen_for_network_callback(self, ad, event, q): |
| 227 | """Verify network callback for data usage. |
| 228 | |
| 229 | Args: |
| 230 | ad: DUT to get the network callback for |
| 231 | event: Network callback event |
| 232 | q: queue object |
| 233 | |
| 234 | Returns: |
| 235 | True: if the expected network callback found, False if not |
| 236 | """ |
| 237 | key = ad.droid.connectivityRegisterDefaultNetworkCallback() |
| 238 | ad.droid.connectivityNetworkCallbackStartListeningForEvent(key, event) |
| 239 | |
| 240 | curr_time = time.time() |
| 241 | status = False |
| 242 | while time.time() < curr_time + TIMEOUT: |
| 243 | try: |
| 244 | nc_event = ad.ed.pop_event("NetworkCallback") |
| 245 | self.log.info("Received: %s" % |
| 246 | nc_event["data"]["networkCallbackEvent"]) |
| 247 | if nc_event["data"]["networkCallbackEvent"] == event: |
| 248 | status = True |
| 249 | break |
| 250 | except Empty: |
| 251 | pass |
| 252 | |
| 253 | ad.droid.connectivityNetworkCallbackStopListeningForEvent(key, event) |
| 254 | ad.droid.connectivityUnregisterNetworkCallback(key) |
| 255 | q.put(status) |
| 256 | |
| 257 | def _test_data_usage_downlink(self, |
| 258 | dut_a, |
| 259 | dut_b, |
| 260 | file_link, |
| 261 | data_usage, |
| 262 | tethering=False, |
| 263 | connect_wifi=False, |
| 264 | app_inc=True): |
| 265 | """Verify data usage. |
| 266 | |
| 267 | Steps: |
| 268 | 1. Get the current data usage of ConnUIDTest and Android OS apps |
| 269 | 2. DUT is on LTE data (on wifi if connect_wifi is True) |
| 270 | 3. Download file of size xMB through ConnUIDTest app |
| 271 | 4. Verify that data usage of Android OS app did not change |
| 272 | 5. Verify that data usage of ConnUIDTest app increased by ~xMB |
| 273 | 6. Verify that data usage of device also increased by ~xMB |
| 274 | |
| 275 | Args: |
| 276 | dut_a: DUT on which data usage need to be verified |
| 277 | dut_b: DUT on which data is downloaded |
| 278 | file_link: Link used to download data |
| 279 | data_usage: specifies if MOBILE or WIFI data is calculated |
| 280 | tethering: if True, enable hotspot on dut_a and connect dut_b to it |
| 281 | connect_wifi: if True, connect dut_a to a wifi network |
| 282 | app_inc: if True, verify app data usage on dut_a is increased |
| 283 | """ |
| 284 | if connect_wifi: |
| 285 | dut_a.droid.telephonyToggleDataConnection(False) |
| 286 | wutils.start_wifi_connection_scan_and_ensure_network_found( |
| 287 | dut_a, self.wifi_network["SSID"]) |
| 288 | wutils.wifi_connect(dut_a, self.wifi_network) |
| 289 | # sleep for some time after connecting to wifi. some apps use wifi |
| 290 | # data when connected initially. waiting after connecting to wifi |
| 291 | # ensures correct wifi data usage calculation |
| 292 | time.sleep(WIFI_SLEEP) |
| 293 | |
| 294 | if tethering: |
| 295 | ssid = "hs_%s" % utils.rand_ascii_str(AP_SSID_LENGTH_2G) |
| 296 | pwd = utils.rand_ascii_str(AP_PASSPHRASE_LENGTH_2G) |
| 297 | network = {wutils.WifiEnums.SSID_KEY: ssid, |
| 298 | wutils.WifiEnums.PWD_KEY: pwd} |
| 299 | dut_b.droid.telephonyToggleDataConnection(False) |
| 300 | wutils.start_wifi_tethering(dut_a, |
| 301 | network[wutils.WifiEnums.SSID_KEY], |
| 302 | network[wutils.WifiEnums.PWD_KEY]) |
| 303 | self.hs_enabled.append(dut_a) |
| 304 | wutils.start_wifi_connection_scan_and_ensure_network_found( |
| 305 | dut_b, network[wutils.WifiEnums.SSID_KEY]) |
| 306 | wutils.wifi_connect(dut_b, network) |
| 307 | # sleep for some time after connecting to wifi. though wifi hotspot |
| 308 | # is metered, some apps would still use wifi data when connected |
| 309 | # initially. this ensures correct wifi data usage calculation |
| 310 | time.sleep(WIFI_SLEEP) |
| 311 | |
| 312 | # get pre data usage |
| 313 | (aos_pre, app_pre, total_pre) = self._get_data_usage(dut_a, data_usage) |
| 314 | |
| 315 | # download file through app |
| 316 | status = self._download_data_through_app(dut_b, file_link) |
| 317 | asserts.assert_true(status, "Failed to download file: %s" % file_link) |
| 318 | |
| 319 | # get new data usage |
| 320 | aos_exp = DATA_ERR |
| 321 | app_exp = self.file_size + DATA_USG_ERR |
| 322 | file_size = self.file_size |
| 323 | if not app_inc: |
| 324 | app_exp = DATA_ERR |
| 325 | file_size = 0 |
| 326 | total_exp = self.file_size + DATA_USG_ERR |
| 327 | |
| 328 | # somtimes data usage is not increased immediately. |
| 329 | # re-tries until the data usage is closed to expected value. |
| 330 | curr_time = time.time() |
| 331 | while time.time() < curr_time + TIMEOUT: |
| 332 | (aos_pst, app_pst, total_pst) = self._get_data_usage(dut_a, |
| 333 | data_usage) |
| 334 | if total_pst - total_pre >= self.file_size and \ |
| 335 | app_pst - app_pre >= file_size: |
| 336 | # wait for some time to verify that data doesn't increase |
| 337 | time.sleep(RETRY_SLEEP*2) |
| 338 | break |
| 339 | time.sleep(RETRY_SLEEP) # wait before retry |
| 340 | (aos_pst, app_pst, total_pst) = self._get_data_usage(dut_a, data_usage) |
| 341 | |
| 342 | # verify data usage |
| 343 | aos_diff = aos_pst - aos_pre |
| 344 | app_diff = app_pst - app_pre |
| 345 | total_diff = total_pst - total_pre |
| 346 | self.log.info("Usage of Android OS increased: %sMB, expected: %sMB" % \ |
| 347 | (aos_diff, aos_exp)) |
| 348 | self.log.info("Usage of ConnUID app increased: %sMB, expected: %sMB" % \ |
| 349 | (app_diff, app_exp)) |
| 350 | self.log.info("Usage on the device increased: %sMB, expected: %sMB" % \ |
| 351 | (total_diff, total_exp)) |
| 352 | |
| 353 | asserts.assert_true( |
| 354 | (aos_diff < aos_exp) and (file_size <= app_diff < app_exp) and \ |
| 355 | (self.file_size <= total_diff < total_exp), |
| 356 | "Incorrect data usage count") |
| 357 | |
| 358 | def _test_data_usage_limit_downlink(self, |
| 359 | dut_a, |
| 360 | dut_b, |
| 361 | file_link, |
| 362 | data_usage, |
| 363 | tethering=False): |
| 364 | """Verify data usage limit reached. |
| 365 | |
| 366 | Steps: |
| 367 | 1. Set the data usage limit to current data usage + 10MB |
| 368 | 2. If tested for tethered device, start wifi hotspot and |
| 369 | connect DUT to it. |
| 370 | 3. If tested for tethered device, download 20MB data from |
| 371 | tethered device else download on the same DUT |
| 372 | 4. Verify file download stops and data limit reached |
| 373 | |
| 374 | Args: |
| 375 | dut_a: DUT on which data usage limit needs to be verified |
| 376 | dut_b: DUT on which data needs to be downloaded |
| 377 | file_link: specifies if the link is IPv4 or IPv6 |
| 378 | data_usage: specifies if MOBILE or WIFI data usage |
| 379 | tethering: if wifi hotspot should be enabled on dut_a |
| 380 | """ |
| 381 | sub_id = str(dut_a.droid.telephonyGetSubscriberId()) |
| 382 | |
| 383 | # enable hotspot and connect dut_b to it. |
| 384 | if tethering: |
| 385 | ssid = "hs_%s" % utils.rand_ascii_str(AP_SSID_LENGTH_2G) |
| 386 | pwd = utils.rand_ascii_str(AP_PASSPHRASE_LENGTH_2G) |
| 387 | network = {wutils.WifiEnums.SSID_KEY: ssid, |
| 388 | wutils.WifiEnums.PWD_KEY: pwd} |
| 389 | dut_b.droid.telephonyToggleDataConnection(False) |
| 390 | wutils.start_wifi_tethering(dut_a, |
| 391 | network[wutils.WifiEnums.SSID_KEY], |
| 392 | network[wutils.WifiEnums.PWD_KEY]) |
| 393 | self.hs_enabled.append(dut_a) |
| 394 | wutils.start_wifi_connection_scan_and_ensure_network_found( |
| 395 | dut_b, network[wutils.WifiEnums.SSID_KEY]) |
| 396 | wutils.wifi_connect(dut_b, network) |
| 397 | |
| 398 | # get pre mobile data usage |
| 399 | total_pre = self._get_total_data_usage_for_device(dut_a, data_usage) |
| 400 | |
| 401 | # set data usage limit to current usage limit + 10MB |
| 402 | self.log.info("Set data usage limit to %sMB" % (total_pre + INC_DATA)) |
| 403 | dut_a.droid.connectivitySetDataUsageLimit( |
| 404 | sub_id, int((total_pre + INC_DATA) * BYTE_TO_MB_ANDROID)) |
| 405 | |
| 406 | # download file on dut_b and look for BlockedStatusChanged callback |
| 407 | q = queue.Queue() |
| 408 | t = threading.Thread(target=self._listen_for_network_callback, |
| 409 | args=(dut_a, "BlockedStatusChanged", q)) |
| 410 | t.daemon = True |
| 411 | t.start() |
| 412 | status = http_file_download_by_chrome( |
| 413 | dut_b, file_link, self.file_size, True, TIMEOUT) |
| 414 | t.join() |
| 415 | cb_res = q.get() |
| 416 | |
| 417 | # verify file download fails and expected callback is recevied |
| 418 | asserts.assert_true(cb_res, |
| 419 | "Failed to verify blocked status network callback") |
| 420 | asserts.assert_true(not status, |
| 421 | "File download successful. Expected to fail") |
| 422 | |
| 423 | ### Test Cases |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 424 | |
| 425 | @test_tracker_info(uuid="b2d9b36c-3a1c-47ca-a9c1-755450abb20c") |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 426 | def test_mobile_data_usage_downlink_ipv4_tmo(self): |
| 427 | """Verify mobile data usage over IPv4 and TMO carrier.""" |
| 428 | self._test_data_usage_downlink(self.android_devices[0], |
| 429 | self.android_devices[0], |
| 430 | self.download_file_ipv4, |
| 431 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 432 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 433 | @test_tracker_info(uuid="fbbb58ed-d573-4bbd-bd5d-0bc540507896") |
| 434 | def test_mobile_data_usage_downlink_ipv6_tmo(self): |
| 435 | """Verify mobile data usage over IPv6 and TMO carrier.""" |
| 436 | self._test_data_usage_downlink(self.android_devices[0], |
| 437 | self.android_devices[0], |
| 438 | self.download_file_ipv6, |
| 439 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 440 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 441 | @test_tracker_info(uuid="6562d626-2271-4d93-96c0-f33138635330") |
| 442 | def test_mobile_data_usage_downlink_ipv4_vzw(self): |
| 443 | """Verify mobile data usage over IPv4 and VZW carrier.""" |
| 444 | self._test_data_usage_downlink(self.android_devices[1], |
| 445 | self.android_devices[1], |
| 446 | self.download_file_ipv4, |
| 447 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 448 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 449 | @test_tracker_info(uuid="2edf94cf-d937-459a-a7e4-2c679803c4d3") |
| 450 | def test_mobile_data_usage_downlink_ipv6_vzw(self): |
| 451 | """Verify mobile data usage over IPv6 and VZW carrier.""" |
| 452 | self._test_data_usage_downlink(self.android_devices[1], |
| 453 | self.android_devices[1], |
| 454 | self.download_file_ipv6, |
| 455 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 456 | |
| 457 | @test_tracker_info(uuid="fe1390e5-635c-49a9-b050-032e66f52f40") |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 458 | def test_wifi_tethering_mobile_data_usage_downlink_ipv4_tmo(self): |
| 459 | """Verify mobile data usage with tethered device over IPv4 and TMO.""" |
| 460 | self._test_data_usage_downlink(self.android_devices[0], |
| 461 | self.android_devices[1], |
| 462 | self.download_file_ipv4, |
| 463 | cconst.TYPE_MOBILE, |
| 464 | True, |
| 465 | False, |
| 466 | False) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 467 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 468 | @test_tracker_info(uuid="d7fde6b2-6672-484a-a2fd-47858f5a163f") |
| 469 | def test_wifi_tethering_mobile_data_usage_downlink_ipv6_tmo(self): |
| 470 | """Verify mobile data usage with tethered device over IPv6 and TMO.""" |
| 471 | self._test_data_usage_downlink(self.android_devices[0], |
| 472 | self.android_devices[1], |
| 473 | self.download_file_ipv6, |
| 474 | cconst.TYPE_MOBILE, |
| 475 | True, |
| 476 | False, |
| 477 | False) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 478 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 479 | @test_tracker_info(uuid="6285ef69-37a8-4069-8cb2-21dc266a57c3") |
| 480 | def test_wifi_tethering_mobile_data_usage_downlink_ipv4_vzw(self): |
| 481 | """Verify mobile data usage with tethered device over IPv4 and VZW.""" |
| 482 | self._test_data_usage_downlink(self.android_devices[1], |
| 483 | self.android_devices[0], |
| 484 | self.download_file_ipv4, |
| 485 | cconst.TYPE_MOBILE, |
| 486 | True, |
| 487 | False, |
| 488 | False) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 489 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 490 | @test_tracker_info(uuid="565fc7e3-d7cf-43cc-8982-4f17999cf579") |
| 491 | def test_wifi_tethering_mobile_data_usage_downlink_ipv6_vzw(self): |
| 492 | """Verify mobile data usage with tethered device over IPv6 and VZW.""" |
| 493 | self._test_data_usage_downlink(self.android_devices[1], |
| 494 | self.android_devices[0], |
| 495 | self.download_file_ipv6, |
| 496 | cconst.TYPE_MOBILE, |
| 497 | True, |
| 498 | False, |
| 499 | False) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 500 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 501 | @test_tracker_info(uuid="72ddb42a-5942-4a6a-8b20-2181c41b2765") |
| 502 | def test_wifi_data_usage_downlink_ipv4(self): |
| 503 | """Verify wifi data usage over IPv4.""" |
| 504 | self._test_data_usage_downlink(self.android_devices[0], |
| 505 | self.android_devices[0], |
| 506 | self.download_file_ipv4, |
| 507 | cconst.TYPE_WIFI, |
| 508 | False, |
| 509 | True, |
| 510 | True) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 511 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 512 | @test_tracker_info(uuid="76b316f5-2946-4757-b5d6-62a8b1fd627e") |
| 513 | def test_wifi_data_usage_downlink_ipv6(self): |
| 514 | """Verify wifi data usage over IPv6.""" |
| 515 | self._test_data_usage_downlink(self.android_devices[0], |
| 516 | self.android_devices[0], |
| 517 | self.download_file_ipv6, |
| 518 | cconst.TYPE_WIFI, |
| 519 | False, |
| 520 | True, |
| 521 | True) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 522 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 523 | @test_tracker_info(uuid="4be5f2d4-9bc6-4190-813e-044f00d94aa6") |
| 524 | def test_wifi_tethering_wifi_data_usage_downlink_ipv4(self): |
| 525 | """Verify wifi data usage with tethered device over IPv4.""" |
| 526 | asserts.skip_if( |
| 527 | self.android_devices[0].model not in self.dbs_supported_models, |
| 528 | "Device %s does not support dual interfaces." % |
| 529 | self.android_devices[0].model) |
| 530 | self._test_data_usage_downlink(self.android_devices[0], |
| 531 | self.android_devices[1], |
| 532 | self.download_file_ipv4, |
| 533 | cconst.TYPE_WIFI, |
| 534 | True, |
| 535 | True, |
| 536 | False) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 537 | |
| 538 | @test_tracker_info(uuid="ac4750fd-20d9-451d-a85b-79fdbaa7da97") |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 539 | def test_data_usage_limit_downlink_ipv4_tmo(self): |
| 540 | """Verify data limit for TMO with IPv4 link.""" |
| 541 | self._test_data_usage_limit_downlink(self.android_devices[0], |
| 542 | self.android_devices[0], |
| 543 | self.download_file_ipv4, |
| 544 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 545 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 546 | @test_tracker_info(uuid="6598732e-f4d3-40c7-a73b-e89bb3d196c5") |
| 547 | def test_data_usage_limit_downlink_ipv6_tmo(self): |
| 548 | """Verify data limit for TMO with IPv6 link.""" |
| 549 | self._test_data_usage_limit_downlink(self.android_devices[0], |
| 550 | self.android_devices[0], |
| 551 | self.download_file_ipv6, |
| 552 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 553 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 554 | @test_tracker_info(uuid="d1ef0405-cf58-4141-94c7-cfa0c9d14438") |
| 555 | def test_data_usage_limit_downlink_ipv4_vzw(self): |
| 556 | """Verify data limit for VZW with IPv4 link.""" |
| 557 | self._test_data_usage_limit_downlink(self.android_devices[1], |
| 558 | self.android_devices[1], |
| 559 | self.download_file_ipv4, |
| 560 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 561 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 562 | @test_tracker_info(uuid="18d169d9-fc4f-4782-8f5f-fc96b8976d03") |
| 563 | def test_data_usage_limit_downlink_ipv6_vzw(self): |
| 564 | """Verify data limit for VZW with IPv6 link.""" |
| 565 | self._test_data_usage_limit_downlink(self.android_devices[1], |
| 566 | self.android_devices[1], |
| 567 | self.download_file_ipv6, |
| 568 | cconst.TYPE_MOBILE) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 569 | |
| 570 | @test_tracker_info(uuid="7c9ab330-9645-4030-bb1e-dcce126944a2") |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 571 | def test_wifi_tethering_data_usage_limit_downlink_ipv4_tmo(self): |
| 572 | """Verify data limit for TMO from tethered device with IPv4 link.""" |
| 573 | self._test_data_usage_limit_downlink(self.android_devices[0], |
| 574 | self.android_devices[1], |
| 575 | self.download_file_ipv4, |
| 576 | cconst.TYPE_MOBILE, |
| 577 | True) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 578 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 579 | @test_tracker_info(uuid="1cafdaf7-a41e-4d19-b08a-ae094d796426") |
| 580 | def test_wifi_tethering_data_usage_limit_downlink_ipv6_tmo(self): |
| 581 | """Verify data limit for TMO from tethered device with IPv6 link.""" |
| 582 | self._test_data_usage_limit_downlink(self.android_devices[0], |
| 583 | self.android_devices[1], |
| 584 | self.download_file_ipv6, |
| 585 | cconst.TYPE_MOBILE, |
| 586 | True) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 587 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 588 | @test_tracker_info(uuid="bd5a8f93-4e2f-474d-a01d-4bb5117d5350") |
| 589 | def test_wifi_tethering_data_usage_limit_downlink_ipv4_vzw(self): |
| 590 | """Verify data limit for VZW from tethered device with IPv4 link.""" |
| 591 | self._test_data_usage_limit_downlink(self.android_devices[1], |
| 592 | self.android_devices[0], |
| 593 | self.download_file_ipv4, |
| 594 | cconst.TYPE_MOBILE, |
| 595 | True) |
Girish Moturu | 383e41a | 2018-01-02 13:51:15 -0800 | [diff] [blame] | 596 | |
Girish Moturu | 74ff565 | 2019-11-17 13:19:26 -0800 | [diff] [blame] | 597 | @test_tracker_info(uuid="ec082fe5-6af1-425e-ace6-4726930bf62f") |
| 598 | def test_wifi_tethering_data_usage_limit_downlink_ipv6_vzw(self): |
| 599 | """Verify data limit for VZW from tethered device with IPv6 link.""" |
| 600 | self._test_data_usage_limit_downlink(self.android_devices[1], |
| 601 | self.android_devices[0], |
| 602 | self.download_file_ipv6, |
| 603 | cconst.TYPE_MOBILE, |
| 604 | True) |