blob: 32ebd7f6320149caeb27e1c2ab777ffadd13e7cc [file] [log] [blame]
Girish Moturu383e41a2018-01-02 13:51:15 -08001#
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 time
17
18from acts import asserts
19from acts import base_test
20from acts.controllers import adb
21from acts.test_decorators import test_tracker_info
22from acts.test_utils.net import connectivity_const as cconst
23from acts.test_utils.tel.tel_data_utils import wait_for_cell_data_connection
24from acts.test_utils.tel.tel_test_utils import http_file_download_by_chrome
25from acts.test_utils.tel.tel_test_utils import verify_http_connection
Girish Moturub9d573e2018-07-17 22:42:18 -070026import acts.test_utils.net.net_test_utils as nutils
Girish Moturu383e41a2018-01-02 13:51:15 -080027from acts.test_utils.tel import tel_test_utils as ttutils
28from acts.test_utils.wifi import wifi_test_utils as wutils
29
30conn_test_class = "com.android.tests.connectivity.uid.ConnectivityTestActivity"
31android_os_class = "com.quicinc.cne.CNEService.CNEServiceApp"
32instr_cmd = "am instrument -w -e command grant-all \
33 com.android.permissionutils/.PermissionInstrumentation"
34
35HOUR_IN_MILLIS = 1000 * 60 * 60
36BYTE_TO_MB_ANDROID = 1000.0 * 1000.0
37BYTE_TO_MB = 1024.0 * 1024.0
38DOWNLOAD_PATH = "/sdcard/download/"
39DATA_USG_ERR = 2.2
40DATA_ERR = 0.2
41TIMEOUT = 2 * 60
42INC_DATA = 10
43
44
45class DataUsageTest(base_test.BaseTestClass):
46 """ Data usage tests """
47
48 def __init__(self, controllers):
49 base_test.BaseTestClass.__init__(self, controllers)
50 self.tests = ("test_mobile_data_usage_downlink",
51 "test_wifi_data_usage_downlink",
52 "test_wifi_tethering_mobile_data_usage_downlink",
53 "test_data_usage_limit_downlink",
54 "test_wifi_tethering_data_usage_limit_downlink",)
55
56 def setup_class(self):
57 """ Setup devices for tests and unpack params """
58 self.dut = self.android_devices[0]
59 self.tethered_devices = self.android_devices[1:]
Girish Moturub9d573e2018-07-17 22:42:18 -070060 nutils.verify_lte_data_and_tethering_supported(self.dut)
Girish Moturu383e41a2018-01-02 13:51:15 -080061
62 # unpack user params
63 req_params = ("wifi_network", "download_file", "file_size", "network")
64 self.unpack_userparams(req_params)
65 self.file_path = DOWNLOAD_PATH + self.download_file.split('/')[-1]
66 self.file_size = int(self.file_size)
67 self.sub_id = str(self.dut.droid.telephonyGetSubscriberId())
68 self.android_os_uid = self.dut.droid.getUidForPackage(android_os_class)
69 self.conn_test_uid = self.dut.droid.getUidForPackage(conn_test_class)
70 for ad in self.android_devices:
71 try:
72 ad.adb.shell(instr_cmd)
73 except adb.AdbError:
74 self.log.warn("adb cmd %s failed on %s" % (instr_cmd, ad.serial))
75
76 # Set chrome browser start with no-first-run verification
77 # Give permission to read from and write to storage
Girish Moturub9d573e2018-07-17 22:42:18 -070078 nutils.set_chrome_browser_permissions(self.dut)
Girish Moturu383e41a2018-01-02 13:51:15 -080079
80 def teardown_class(self):
81 """ Reset devices """
Girish Moturub9d573e2018-07-17 22:42:18 -070082 for ad in self.android_devices:
83 wutils.reset_wifi(ad)
84
85 def on_fail(self, test_name, begin_time):
86 for ad in self.android_devices:
87 ad.take_bug_report(test_name, begin_time)
Girish Moturu383e41a2018-01-02 13:51:15 -080088
89 """ Helper functions """
90
91 def _download_data_through_app(self, ad):
92 """ Download data through app on DUT
93
94 Args:
95 1. ad - DUT to download the file on
96
97 Returns:
98 True - if file download is successful
99 False - if file download is not successful
100 """
101 intent = self.dut.droid.createIntentForClassName(conn_test_class)
102 json_obj = {"url": self.download_file}
103 ad.droid.launchForResultWithIntent(intent, json_obj)
104 download_status = False
105 end_time = time.time() + TIMEOUT
106 while time.time() < end_time:
107 download_status = ttutils._check_file_existance(
108 ad, self.file_path, self.file_size * BYTE_TO_MB)
109 if download_status:
110 self.log.info("Delete file: %s", self.file_path)
111 ad.adb.shell("rm %s" % self.file_path, ignore_status=True)
112 break
113 time.sleep(8) # wait to check again if download is complete
114 return download_status
115
116 def _get_data_usage(self, ad, conn_type):
117 """ Get data usage
118
119 Args:
120 1. ad - DUT to get data usage from
121 2. conn_type - MOBILE/WIFI data usage
122
123 Returns:
124 Tuple of Android Os app, Conn UID app, Total data usages
125 """
126 aos = self._get_data_usage_for_uid_rx(ad, conn_type, self.android_os_uid)
127 app = self._get_data_usage_for_uid_rx(ad, conn_type, self.conn_test_uid)
128 tot = self._get_data_usage_for_device_rx(ad, conn_type)
129 self.log.info("Android Os data usage: %s" % aos)
130 self.log.info("Conn UID Test data usage: %s" % app)
131 self.log.info("Total data usage: %s" % tot)
132 return (aos, app, tot)
133
134 def _get_total_data_usage_for_device(self, conn_type):
135 """ Get total data usage in MB for device
136
137 Args:
138 1. conn_type - MOBILE/WIFI data usage
139
140 Returns:
141 Data usage in MB
142 """
143 end_time = int(time.time() * 1000) + 2 * HOUR_IN_MILLIS
144 data_usage = self.dut.droid.connectivityQuerySummaryForDevice(
145 conn_type, self.sub_id, 0, end_time)
146 data_usage /= BYTE_TO_MB_ANDROID
147 self.log.info("Total data usage is: %s" % data_usage)
148 return data_usage
149
150 def _get_data_usage_for_uid_rx(self, ad, conn_type, uid):
151 """ Get data usage for UID in Rx Bytes
152
153 Args:
154 1. ad - DUT to get data usage from
155 2. conn_type - MOBILE/WIFI data usage
156 3. uid - UID of the app
157
158 Returns:
159 Data usage in MB
160 """
161 subscriber_id = ad.droid.telephonyGetSubscriberId()
162 end_time = int(time.time() * 1000) + 2 * HOUR_IN_MILLIS
163 data_usage = ad.droid.connectivityQueryDetailsForUidRxBytes(
164 conn_type, subscriber_id, 0, end_time, uid)
165 return data_usage/BYTE_TO_MB_ANDROID
166
167 def _get_data_usage_for_device_rx(self, ad, conn_type):
168 """ Get total data usage in rx bytes for device
169
170 Args:
171 1. ad - DUT to get data usage from
172 2. conn_type - MOBILE/WIFI data usage
173
174 Returns:
175 Data usage in MB
176 """
177 subscriber_id = ad.droid.telephonyGetSubscriberId()
178 end_time = int(time.time() * 1000) + 2 * HOUR_IN_MILLIS
179 data_usage = ad.droid.connectivityQuerySummaryForDeviceRxBytes(
180 conn_type, subscriber_id, 0, end_time)
181 return data_usage/BYTE_TO_MB_ANDROID
182
183 """ Test Cases """
184
185 @test_tracker_info(uuid="b2d9b36c-3a1c-47ca-a9c1-755450abb20c")
186 def test_mobile_data_usage_downlink(self):
187 """ Verify mobile data usage
188
189 Steps:
190 1. Get the current data usage of ConnUIDTest and Android OS apps
191 2. DUT is on LTE data
192 3. Download file of size xMB through ConnUIDTest app
193 4. Verify that data usage of Android OS app did not change
194 5. Verify that data usage of ConnUIDTest app increased by ~xMB
195 6. Verify that data usage of device also increased by ~xMB
196 """
Girish Moturu383e41a2018-01-02 13:51:15 -0800197 # get pre mobile data usage
198 (aos_pre, app_pre, total_pre) = self._get_data_usage(self.dut,
199 cconst.TYPE_MOBILE)
200
201 # download file through app
202 self._download_data_through_app(self.dut)
203
204 # get new mobile data usage
205 (aos_pst, app_pst, total_pst) = self._get_data_usage(self.dut,
206 cconst.TYPE_MOBILE)
207
208 # verify data usage
209 aos_diff = aos_pst - aos_pre
210 app_diff = app_pst - app_pre
211 total_diff = total_pst - total_pre
212 self.log.info("Data usage of Android os increased by %s" % aos_diff)
213 self.log.info("Data usage of ConnUID app increased by %s" % app_diff)
214 self.log.info("Data usage on the device increased by %s" % total_diff)
215 return (aos_diff < DATA_ERR) and \
216 (self.file_size < app_diff < self.file_size + DATA_USG_ERR) and \
217 (self.file_size < total_diff < self.file_size + DATA_USG_ERR)
218
219 @test_tracker_info(uuid="72ddb42a-5942-4a6a-8b20-2181c41b2765")
220 def test_wifi_data_usage_downlink(self):
221 """ Verify wifi data usage
222
223 Steps:
224 1. Get the current data usage of ConnUIDTest and Android OS apps
225 2. DUT is on LTE data
226 3. Download file of size xMB through ConnUIDTest app
227 4. Verify that data usage of Android OS app did not change
228 5. Verify that data usage of ConnUIDTest app increased by ~xMB
229 6. Verify that data usage of device also increased by ~xMB
230 """
231 # connect to wifi network
232 wutils.wifi_connect(self.dut, self.wifi_network)
233
234 # get pre wifi data usage
235 (aos_pre, app_pre, total_pre) = self._get_data_usage(self.dut,
236 cconst.TYPE_WIFI)
237
238 # download file through app
239 self._download_data_through_app(self.dut)
240
241 # get new mobile data usage
242 (aos_pst, app_pst, total_pst) = self._get_data_usage(self.dut,
243 cconst.TYPE_WIFI)
244
245 # verify data usage
246 aos_diff = aos_pst - aos_pre
247 app_diff = app_pst - app_pre
248 total_diff = total_pst - total_pre
249 self.log.info("Data usage of Android os increased by %s" % aos_diff)
250 self.log.info("Data usage of ConnUID app increased by %s" % app_diff)
251 self.log.info("Data usage on the device increased by %s" % total_diff)
Girish Moturub9d573e2018-07-17 22:42:18 -0700252
253 # forget network
254 wutils.wifi_forget_network(self.dut, self.wifi_network['SSID'])
255
Girish Moturu383e41a2018-01-02 13:51:15 -0800256 return (aos_diff < DATA_ERR) and \
257 (self.file_size < app_diff < self.file_size + DATA_USG_ERR) and \
258 (self.file_size < total_diff < self.file_size + DATA_USG_ERR)
259
260 @test_tracker_info(uuid="fe1390e5-635c-49a9-b050-032e66f52f40")
261 def test_wifi_tethering_mobile_data_usage_downlink(self):
262 """ Verify mobile data usage with tethered device
263
264 Steps:
265 1. Start wifi hotspot and connect tethered device to it
266 2. Get the data usage on hotspot device
267 3. Download data on tethered device
268 4. Get the new data usage on hotspot device
269 5. Verify that hotspot device's data usage increased by downloaded file size
270 """
271 # connect device to wifi hotspot
272 ad = self.tethered_devices[0]
273 wutils.start_wifi_tethering(self.dut,
274 self.network[wutils.WifiEnums.SSID_KEY],
275 self.network[wutils.WifiEnums.PWD_KEY],
276 ttutils.WIFI_CONFIG_APBAND_2G)
277 wutils.wifi_connect(ad, self.network)
278
279 # get pre mobile data usage
280 (aos_pre, app_pre, total_pre) = self._get_data_usage(self.dut,
281 cconst.TYPE_MOBILE)
282
283 # download file through app
284 self._download_data_through_app(ad)
285
286 # get new mobile data usage
287 (aos_pst, app_pst, total_pst) = self._get_data_usage(self.dut,
288 cconst.TYPE_MOBILE)
289
290 # stop wifi hotspot
291 wutils.stop_wifi_tethering(self.dut)
292
293 # verify data usage
294 aos_diff = aos_pst - aos_pre
295 app_diff = app_pst - app_pre
296 total_diff = total_pst - total_pre
297 self.log.info("Data usage of Android os increased by %s" % aos_diff)
298 self.log.info("Data usage of ConnUID app increased by %s" % app_diff)
299 self.log.info("Data usage on the device increased by %s" % total_diff)
300 return (aos_diff < DATA_ERR) and (app_diff < DATA_ERR) and \
301 (self.file_size < total_diff < self.file_size + DATA_USG_ERR)
302
303 @test_tracker_info(uuid="ac4750fd-20d9-451d-a85b-79fdbaa7da97")
304 def test_data_usage_limit_downlink(self):
305 """ Verify connectivity when data usage limit reached
306
307 Steps:
308 1. Set the data usage limit to current data usage + 10MB
309 2. Download 20MB data
310 3. File download stops and data limit reached
311 4. Device should lose internet connectivity
312 5. Verify data usage limit
313 """
314 # get pre mobile data usage
315 total_pre = self._get_total_data_usage_for_device(cconst.TYPE_MOBILE)
316
317 # set data usage limit to current usage limit + 10MB
318 self.log.info("Setting data usage limit to %sMB" % (total_pre + INC_DATA))
319 self.dut.droid.connectivitySetDataUsageLimit(
320 self.sub_id, str(int((total_pre + INC_DATA) * BYTE_TO_MB_ANDROID)))
321
322 # download file through app
323 http_file_download_by_chrome(
324 self.dut, self.download_file, self.file_size, timeout=120)
325 total_pst = self._get_total_data_usage_for_device(cconst.TYPE_MOBILE)
326
327 # verify data usage
328 connectivity_status = wutils.validate_connection(self.dut)
329 self.dut.droid.connectivityFactoryResetNetworkPolicies(self.sub_id)
330 self.log.info("Expected data usage: %s" % (total_pre + INC_DATA))
331 self.log.info("Actual data usage: %s" % total_pst)
332 asserts.assert_true(
333 not connectivity_status,
334 "Device has internet connectivity after reaching data limit")
335 return total_pst - total_pre - INC_DATA < DATA_USG_ERR
336
337 @test_tracker_info(uuid="7c9ab330-9645-4030-bb1e-dcce126944a2")
338 def test_wifi_tethering_data_usage_limit_downlink(self):
339 """ Verify connectivity when data usage limit reached
340
341 Steps:
342 1. Set the data usage limit to current data usage + 10MB
343 2. Start wifi tethering and connect a dut to the SSID
344 3. Download 20MB data on tethered device
345 4. File download stops and data limit reached
346 5. Verify data usage limit
347 """
348 # connect device to wifi hotspot
349 ad = self.tethered_devices[0]
350 wutils.toggle_wifi_off_and_on(self.dut)
351 wutils.start_wifi_tethering(self.dut,
352 self.network[wutils.WifiEnums.SSID_KEY],
353 self.network[wutils.WifiEnums.PWD_KEY],
354 ttutils.WIFI_CONFIG_APBAND_2G)
355 wutils.wifi_connect(ad, self.network)
356
357 # get pre mobile data usage
358 total_pre = self._get_total_data_usage_for_device(cconst.TYPE_MOBILE)
359
360 # set data usage limit to current usage limit + 10MB
361 self.log.info("Setting data usage limit to %sMB" % (total_pre + INC_DATA))
362 self.dut.droid.connectivitySetDataUsageLimit(
363 self.sub_id, str(int((total_pre + INC_DATA) * BYTE_TO_MB_ANDROID)))
364
365 # download file from tethered device
366 http_file_download_by_chrome(
367 ad, self.download_file, self.file_size, timeout=120)
368 total_pst = self._get_total_data_usage_for_device(cconst.TYPE_MOBILE)
369
370 # verify data usage
371 connectivity_status = wutils.validate_connection(ad)
372 self.dut.droid.connectivityFactoryResetNetworkPolicies(self.sub_id)
373 wutils.stop_wifi_tethering(self.dut)
374 self.log.info("Expected data usage: %s" % (total_pre + INC_DATA))
375 self.log.info("Actual data usage: %s" % total_pst)
376 asserts.assert_true(
377 not connectivity_status,
378 "Device has internet connectivity after reaching data limit")
379 return total_pst - total_pre - INC_DATA < DATA_USG_ERR