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 16import logging 17import os 18import random 19import socket 20import threading 21import time 22 23from acts import asserts 24from acts import base_test 25from acts import test_runner 26from acts.controllers import adb 27from acts.test_decorators import test_tracker_info 28from acts.test_utils.net import net_test_utils as nutils 29from acts.test_utils.tel.tel_test_utils import _check_file_existance 30from acts.test_utils.tel.tel_test_utils import _generate_file_directory_and_file_name 31from acts.test_utils.wifi import wifi_test_utils as wutils 32from acts.test_utils.net.connectivity_const import MULTIPATH_PREFERENCE_NONE as NONE 33from acts.test_utils.net.connectivity_const import MULTIPATH_PREFERENCE_HANDOVER as HANDOVER 34from acts.test_utils.net.connectivity_const import MULTIPATH_PREFERENCE_RELIABILITY as RELIABILITY 35from acts.test_utils.net.connectivity_const import MULTIPATH_PREFERENCE_PERFORMANCE as PERFORMANCE 36 37DOWNLOAD_PATH = "/sdcard/Download/" 38RELIABLE = RELIABILITY | HANDOVER 39 40class DataCostTest(base_test.BaseTestClass): 41 """ Tests for Wifi Tethering """ 42 43 def setup_class(self): 44 """ Setup devices for tethering and unpack params """ 45 46 req_params = ("wifi_network", "download_file") 47 self.unpack_userparams(req_params) 48 49 for ad in self.android_devices: 50 nutils.verify_lte_data_and_tethering_supported(ad) 51 52 def teardown_class(self): 53 """ Reset settings to default """ 54 for ad in self.android_devices: 55 sub_id = str(ad.droid.telephonyGetSubscriberId()) 56 ad.droid.connectivityFactoryResetNetworkPolicies(sub_id) 57 ad.droid.connectivitySetDataWarningLimit(sub_id, -1) 58 wutils.reset_wifi(ad) 59 60 def on_fail(self, test_name, begin_time): 61 for ad in self.android_devices: 62 ad.take_bug_report(test_name, begin_time) 63 64 """ Helper functions """ 65 66 def _get_total_data_usage_for_device(self, ad, conn_type, sub_id): 67 """ Get total data usage in MB for device 68 69 Args: 70 ad: Android device object 71 conn_type: MOBILE/WIFI data usage 72 sub_id: subscription id 73 74 Returns: 75 Data usage in MB 76 """ 77 # end time should be in milli seconds and at least 2 hours more than the 78 # actual end time. NetStats:bucket is of size 2 hours and to ensure to 79 # get the most recent data usage, end_time should be +2hours 80 end_time = int(time.time() * 1000) + 2 * 1000 * 60 * 60 81 data_usage = ad.droid.connectivityQuerySummaryForDevice( 82 conn_type, sub_id, 0, end_time) 83 data_usage /= 1000.0 * 1000.0 # convert data_usage to MB 84 self.log.info("Total data usage is: %s" % data_usage) 85 return data_usage 86 87 def _check_if_multipath_preference_valid(self, val, exp): 88 """ Check if multipath value is same as expected 89 90 Args: 91 val: multipath preference for the network 92 exp: expected multipath preference value 93 """ 94 if exp == NONE: 95 asserts.assert_true(val == exp, "Multipath value should be 0") 96 else: 97 asserts.assert_true(val >= exp, 98 "Multipath value should be at least %s" % exp) 99 100 def _verify_multipath_preferences(self, 101 ad, 102 wifi_pref, 103 cell_pref, 104 wifi_network, 105 cell_network): 106 """ Verify mutlipath preferences for wifi and cell networks 107 108 Args: 109 ad: Android device object 110 wifi_pref: Expected multipath value for wifi network 111 cell_pref: Expected multipath value for cell network 112 wifi_network: Wifi network id on the device 113 cell_network: Cell network id on the device 114 """ 115 wifi_multipath = \ 116 ad.droid.connectivityGetMultipathPreferenceForNetwork(wifi_network) 117 cell_multipath = \ 118 ad.droid.connectivityGetMultipathPreferenceForNetwork(cell_network) 119 self.log.info("WiFi multipath preference: %s" % wifi_multipath) 120 self.log.info("Cell multipath preference: %s" % cell_multipath) 121 self.log.info("Checking multipath preference for wifi") 122 self._check_if_multipath_preference_valid(wifi_multipath, wifi_pref) 123 self.log.info("Checking multipath preference for cell") 124 self._check_if_multipath_preference_valid(cell_multipath, cell_pref) 125 126 """ Test Cases """ 127 128 @test_tracker_info(uuid="e86c8108-3e84-4668-bae4-e5d2c8c27910") 129 def test_multipath_preference_low_data_limit(self): 130 """ Verify multipath preference when mobile data limit is low 131 132 Steps: 133 1. DUT has WiFi and LTE data 134 2. Set mobile data usage limit to low value 135 3. Verify that multipath preference is 0 for cell network 136 """ 137 # set vars 138 ad = self.android_devices[0] 139 sub_id = str(ad.droid.telephonyGetSubscriberId()) 140 cell_network = ad.droid.connectivityGetActiveNetwork() 141 self.log.info("cell network %s" % cell_network) 142 wutils.wifi_connect(ad, self.wifi_network) 143 wifi_network = ad.droid.connectivityGetActiveNetwork() 144 self.log.info("wifi network %s" % wifi_network) 145 146 # verify mulipath preference values 147 self._verify_multipath_preferences( 148 ad, RELIABLE, RELIABLE, wifi_network, cell_network) 149 150 # set low data limit on mobile data 151 total_pre = self._get_total_data_usage_for_device(ad, 0, sub_id) 152 self.log.info("Setting data usage limit to %sMB" % (total_pre + 5)) 153 ad.droid.connectivitySetDataUsageLimit( 154 sub_id, int((total_pre + 5) * 1000.0 * 1000.0)) 155 self.log.info("Setting data warning limit to %sMB" % (total_pre + 5)) 156 ad.droid.connectivitySetDataWarningLimit( 157 sub_id, int((total_pre + 5) * 1000.0 * 1000.0)) 158 159 # verify multipath preference values 160 self._verify_multipath_preferences( 161 ad, RELIABLE, NONE, wifi_network, cell_network) 162 163 @test_tracker_info(uuid="a2781411-d880-476a-9f40-2c67e0f97db9") 164 def test_multipath_preference_data_download(self): 165 """ Verify multipath preference when large file is downloaded 166 167 Steps: 168 1. DUT has WiFi and LTE data 169 2. WiFi is active network 170 3. Download large file over cell network 171 4. Verify multipath preference on cell network is 0 172 """ 173 # set vars 174 ad = self.android_devices[1] 175 cell_network = ad.droid.connectivityGetActiveNetwork() 176 self.log.info("cell network %s" % cell_network) 177 wutils.wifi_connect(ad, self.wifi_network) 178 wifi_network = ad.droid.connectivityGetActiveNetwork() 179 self.log.info("wifi network %s" % wifi_network) 180 181 # verify multipath preference for wifi and cell networks 182 self._verify_multipath_preferences( 183 ad, RELIABLE, RELIABLE, wifi_network, cell_network) 184 185 # download file with cell network 186 ad.droid.connectivityNetworkOpenConnection(cell_network, 187 self.download_file) 188 file_folder, file_name = _generate_file_directory_and_file_name( 189 self.download_file, DOWNLOAD_PATH) 190 file_path = os.path.join(file_folder, file_name) 191 self.log.info("File path: %s" % file_path) 192 if _check_file_existance(ad, file_path): 193 self.log.info("File exists. Removing file %s" % file_name) 194 ad.adb.shell("rm -rf %s%s" % (DOWNLOAD_PATH, file_name)) 195 196 # verify multipath preference values 197 self._verify_multipath_preferences( 198 ad, RELIABLE, NONE, wifi_network, cell_network) 199 200 # TODO gmoturu@: Need to add tests that use the mobility rig and test when 201 # the WiFi signal is poor and data signal is good. 202