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