1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - Google
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16"""
17    Test Script for Telephony Settings
18"""
19
20import os
21import time
22
23from acts import signals
24from acts.keys import Config
25from acts.utils import unzip_maintain_permissions
26from acts.test_decorators import test_tracker_info
27from acts_contrib.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
28from acts_contrib.test_utils.tel.tel_defines import MAX_WAIT_TIME_FOR_STATE_CHANGE
29from acts_contrib.test_utils.tel.tel_test_utils import dumpsys_carrier_config
30from acts_contrib.test_utils.tel.tel_test_utils import ensure_phone_subscription
31from acts_contrib.test_utils.tel.tel_test_utils import flash_radio
32from acts_contrib.test_utils.tel.tel_test_utils import get_outgoing_voice_sub_id
33from acts_contrib.test_utils.tel.tel_test_utils import get_slot_index_from_subid
34from acts_contrib.test_utils.tel.tel_test_utils import is_sim_locked
35from acts_contrib.test_utils.tel.tel_test_utils import multithread_func
36from acts_contrib.test_utils.tel.tel_test_utils import power_off_sim
37from acts_contrib.test_utils.tel.tel_test_utils import power_on_sim
38from acts_contrib.test_utils.tel.tel_test_utils import print_radio_info
39from acts_contrib.test_utils.tel.tel_test_utils import revert_default_telephony_setting
40from acts_contrib.test_utils.tel.tel_test_utils import set_qxdm_logger_command
41from acts_contrib.test_utils.tel.tel_test_utils import system_file_push
42from acts_contrib.test_utils.tel.tel_test_utils import unlock_sim
43from acts_contrib.test_utils.tel.tel_test_utils import verify_default_telephony_setting
44from acts.utils import set_mobile_data_always_on
45
46
47class TelLiveSettingsTest(TelephonyBaseTest):
48    def setup_class(self):
49        TelephonyBaseTest.setup_class(self)
50        self.dut = self.android_devices[0]
51        self.number_of_devices = 1
52        self.stress_test_number = self.get_stress_test_number()
53        self.carrier_configs = dumpsys_carrier_config(self.dut)
54        self.dut_subID = get_outgoing_voice_sub_id(self.dut)
55        self.dut_capabilities = self.dut.telephony["subscription"][self.dut_subID].get("capabilities", [])
56
57    @test_tracker_info(uuid="c6149bd6-7080-453d-af37-1f9bd350a764")
58    @TelephonyBaseTest.tel_test_wrap
59    def test_telephony_factory_reset(self):
60        """Test VOLTE is enabled WFC is disabled after telephony factory reset.
61
62        Steps:
63        1. Setup DUT with various dataroaming, mobiledata, and default_network.
64        2. Call telephony factory reset.
65        3. Verify DUT back to factory default.
66
67        Expected Results: dataroaming is off, mobiledata is on, network
68                          preference is back to default.
69        """
70        revert_default_telephony_setting(self.dut)
71        self.dut.log.info("Call telephony factory reset")
72        self.dut.droid.telephonyFactoryReset()
73        time.sleep(MAX_WAIT_TIME_FOR_STATE_CHANGE)
74        return verify_default_telephony_setting(self.dut)
75
76    @test_tracker_info(uuid="3afa2070-564f-4e6c-b08d-12dd4381abb9")
77    @TelephonyBaseTest.tel_test_wrap
78    def test_check_carrier_config(self):
79        """Check the carrier_config and network setting for different carrier.
80
81        Steps:
82        1. Device loaded with different SIM cards.
83        2. Check the carrier_configs are expected value.
84
85        """
86        pass
87
88    @test_tracker_info(uuid="64deba57-c1c2-422f-b771-639c95edfbc0")
89    @TelephonyBaseTest.tel_test_wrap
90    def test_disable_mobile_data_always_on(self):
91        """Verify mobile_data_always_on can be disabled.
92
93        Steps:
94        1. Disable mobile_data_always_on by adb.
95        2. Verify the mobile data_always_on state.
96
97        Expected Results: mobile_data_always_on return 0.
98        """
99        self.dut.log.info("Disable mobile_data_always_on")
100        set_mobile_data_always_on(self.dut, False)
101        time.sleep(1)
102        return self.dut.adb.shell(
103            "settings get global mobile_data_always_on") == "0"
104
105    @test_tracker_info(uuid="56ddcd5a-92b0-46c7-9c2b-d743794efb7c")
106    @TelephonyBaseTest.tel_test_wrap
107    def test_enable_mobile_data_always_on(self):
108        """Verify mobile_data_always_on can be enabled.
109
110        Steps:
111        1. Enable mobile_data_always_on by adb.
112        2. Verify the mobile data_always_on state.
113
114        Expected Results: mobile_data_always_on return 1.
115        """
116        self.dut.log.info("Enable mobile_data_always_on")
117        set_mobile_data_always_on(self.dut, True)
118        time.sleep(1)
119        return "1" in self.dut.adb.shell(
120            "settings get global mobile_data_always_on")
121
122    @test_tracker_info(uuid="c2cc5b66-40af-4ba6-81cb-6c44ae34cbbb")
123    @TelephonyBaseTest.tel_test_wrap
124    def test_push_new_radio_or_mbn(self):
125        """Verify new mdn and radio can be push to device.
126
127        Steps:
128        1. If new radio path is given, flash new radio on the device.
129        2. Verify the radio version.
130        3. If new mbn path is given, push new mbn to device.
131        4. Verify the installed mbn version.
132
133        Expected Results:
134        radio and mbn can be pushed to device and mbn.ver is available.
135        """
136        result = True
137        paths = {}
138        for path_key, dst_name in zip(["radio_image", "mbn_path"],
139                                      ["radio.img", "mcfg_sw"]):
140            path = self.user_params.get(path_key)
141            if not path:
142                continue
143            elif isinstance(path, list):
144                if not path[0]:
145                    continue
146                path = path[0]
147            if "dev/null" in path:
148                continue
149            if not os.path.exists(path):
150                self.log.error("path %s does not exist", path)
151                self.log.info(self.user_params)
152                path = os.path.join(
153                    self.user_params[Config.key_config_path.value], path)
154                if not os.path.exists(path):
155                    self.log.error("path %s does not exist", path)
156                    continue
157
158            self.log.info("%s path = %s", path_key, path)
159            if "zip" in path:
160                self.log.info("Unzip %s", path)
161                file_path, file_name = os.path.split(path)
162                dest_path = os.path.join(file_path, dst_name)
163                os.system("rm -rf %s" % dest_path)
164                unzip_maintain_permissions(path, file_path)
165                path = dest_path
166            os.system("chmod -R 777 %s" % path)
167            paths[path_key] = path
168        if not paths:
169            self.log.info("No radio_path or mbn_path is provided")
170            raise signals.TestSkip("No radio_path or mbn_path is provided")
171        self.log.info("paths = %s", paths)
172        for ad in self.android_devices:
173            if paths.get("radio_image"):
174                print_radio_info(ad, "Before flash radio, ")
175                flash_radio(ad, paths["radio_image"])
176                print_radio_info(ad, "After flash radio, ")
177            if not paths.get("mbn_path") or "mbn" not in ad.adb.shell(
178                    "ls /vendor"):
179                ad.log.info("No need to push mbn files")
180                continue
181            push_result = True
182            try:
183                mbn_ver = ad.adb.shell(
184                    "cat /vendor/mbn/mcfg/configs/mcfg_sw/mbn.ver")
185                if mbn_ver:
186                    ad.log.info("Before push mbn, mbn.ver = %s", mbn_ver)
187                else:
188                    ad.log.info(
189                        "There is no mbn.ver before push, unmatching device")
190                    continue
191            except:
192                ad.log.info(
193                    "There is no mbn.ver before push, unmatching device")
194                continue
195            print_radio_info(ad, "Before push mbn, ")
196            for i in range(2):
197                if not system_file_push(ad, paths["mbn_path"],
198                                        "/vendor/mbn/mcfg/configs/"):
199                    if i == 1:
200                        ad.log.error("Failed to push mbn file")
201                        push_result = False
202                else:
203                    ad.log.info("The mbn file is pushed to device")
204                    break
205            if not push_result:
206                result = False
207                continue
208            print_radio_info(ad, "After push mbn, ")
209            try:
210                new_mbn_ver = ad.adb.shell(
211                    "cat /vendor/mbn/mcfg/configs/mcfg_sw/mbn.ver")
212                if new_mbn_ver:
213                    ad.log.info("new mcfg_sw mbn.ver = %s", new_mbn_ver)
214                    if new_mbn_ver == mbn_ver:
215                        ad.log.error(
216                            "mbn.ver is the same before and after push")
217                        result = False
218                else:
219                    ad.log.error("Unable to get new mbn.ver")
220                    result = False
221            except Exception as e:
222                ad.log.error("cat mbn.ver with error %s", e)
223                result = False
224        return result
225
226    @TelephonyBaseTest.tel_test_wrap
227    def test_set_qxdm_log_mask_ims(self):
228        """Set the QXDM Log mask to IMS_DS_CNE_LnX_Golden.cfg"""
229        tasks = [(set_qxdm_logger_command, [ad, "IMS_DS_CNE_LnX_Golden.cfg"])
230                 for ad in self.android_devices]
231        return multithread_func(self.log, tasks)
232
233    @TelephonyBaseTest.tel_test_wrap
234    def test_set_qxdm_log_mask_qc_default(self):
235        """Set the QXDM Log mask to QC_Default.cfg"""
236        tasks = [(set_qxdm_logger_command, [ad, " QC_Default.cfg"])
237                 for ad in self.android_devices]
238        return multithread_func(self.log, tasks)
239
240    @test_tracker_info(uuid="e2734d66-6111-4e76-aa7b-d3b4cbcde4f1")
241    @TelephonyBaseTest.tel_test_wrap
242    def test_check_carrier_id(self):
243        """Verify mobile_data_always_on can be enabled.
244
245        Steps:
246        1. Enable mobile_data_always_on by adb.
247        2. Verify the mobile data_always_on state.
248
249        Expected Results: mobile_data_always_on return 1.
250        """
251        result = True
252        if self.dut.adb.getprop("ro.build.version.release")[0] in ("8", "O",
253                                                                   "7", "N"):
254            raise signals.TestSkip("Not supported in this build")
255        old_carrier_id = self.dut.droid.telephonyGetSimCarrierId()
256        old_carrier_name = self.dut.droid.telephonyGetSimCarrierIdName()
257        self.result_detail = "carrier_id = %s, carrier_name = %s" % (
258            old_carrier_id, old_carrier_name)
259        self.dut.log.info(self.result_detail)
260        sub_id = get_outgoing_voice_sub_id(self.dut)
261        slot_index = get_slot_index_from_subid(self.log, self.dut, sub_id)
262
263        if self.dut.model in ("angler", "bullhead", "marlin", "sailfish"):
264            msg = "Power off SIM slot is not supported"
265            self.dut.log.warning("%s, test finished", msg)
266            self.result_detail = "%s, %s" % (self.result_detail, msg)
267            return result
268
269        if power_off_sim(self.dut, slot_index):
270            for i in range(3):
271                carrier_id = self.dut.droid.telephonyGetSimCarrierId()
272                carrier_name = self.dut.droid.telephonyGetSimCarrierIdName()
273                msg = "After SIM power down, carrier_id = %s(expecting -1), " \
274                      "carrier_name = %s(expecting None)" % (carrier_id, carrier_name)
275                if carrier_id != -1 or carrier_name:
276                    if i == 2:
277                        self.dut.log.error(msg)
278                        self.result_detail = "%s, %s" % (self.result_detail,
279                                                         msg)
280                        result = False
281                    else:
282                        time.sleep(5)
283                else:
284                    self.dut.log.info(msg)
285                    break
286        else:
287            msg = "Power off SIM slot is not working"
288            self.dut.log.error(msg)
289            result = False
290            self.result_detail = "%s, %s" % (self.result_detail, msg)
291
292        if not power_on_sim(self.dut, slot_index):
293            self.dut.log.error("Fail to power up SIM")
294            result = False
295            setattr(self.dut, "reboot_to_recover", True)
296        else:
297            if is_sim_locked(self.dut):
298                self.dut.log.info("Sim is locked")
299                carrier_id = self.dut.droid.telephonyGetSimCarrierId()
300                carrier_name = self.dut.droid.telephonyGetSimCarrierIdName()
301                msg = "In locked SIM, carrier_id = %s(expecting -1), " \
302                      "carrier_name = %s(expecting None)" % (carrier_id, carrier_name)
303                if carrier_id != -1 or carrier_name:
304                    self.dut.log.error(msg)
305                    self.result_detail = "%s, %s" % (self.result_detail, msg)
306                    result = False
307                else:
308                    self.dut.log.info(msg)
309                unlock_sim(self.dut)
310            elif getattr(self.dut, "is_sim_locked", False):
311                self.dut.log.error(
312                    "After SIM slot power cycle, SIM in not in locked state")
313                return False
314
315            if not ensure_phone_subscription(self.log, self.dut):
316                self.dut.log.error("Unable to find a valid subscription!")
317                result = False
318            time.sleep(15)
319            new_carrier_id = self.dut.droid.telephonyGetSimCarrierId()
320            new_carrier_name = self.dut.droid.telephonyGetSimCarrierIdName()
321            msg = "After SIM power up, new_carrier_id = %s, " \
322                  "new_carrier_name = %s" % (new_carrier_id, new_carrier_name)
323            if old_carrier_id != new_carrier_id or (old_carrier_name !=
324                                                    new_carrier_name):
325                self.dut.log.error(msg)
326                self.result_detail = "%s, %s" % (self.result_detail, msg)
327                result = False
328            else:
329                self.dut.log.info(msg)
330        return result
331