1#
2#   Copyright 2016 - 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 pprint
17import random
18import time
19
20from acts import asserts
21from acts import signals
22from acts.test_decorators import test_tracker_info
23from acts.test_utils.wifi import wifi_test_utils as wutils
24from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
25
26WifiEnums = wutils.WifiEnums
27
28# EAP Macros
29EAP = WifiEnums.Eap
30EapPhase2 = WifiEnums.EapPhase2
31
32# Enterprise Config Macros
33Ent = WifiEnums.Enterprise
34
35
36class WifiEnterpriseRoamingTest(WifiBaseTest):
37    def setup_class(self):
38        super().setup_class()
39
40        self.dut = self.android_devices[0]
41        wutils.wifi_test_device_init(self.dut)
42        req_params = (
43            "attn_vals",
44            # Expected time within which roaming should finish, in seconds.
45            "roam_interval",
46            "ca_cert",
47            "client_cert",
48            "client_key",
49            "eap_identity",
50            "eap_password",
51            "device_password",
52            "radius_conf_2g",
53            "radius_conf_5g")
54        self.unpack_userparams(req_params)
55        if "AccessPoint" in self.user_params:
56            self.legacy_configure_ap_and_start(
57                mirror_ap=True,
58                ent_network=True,
59                ap_count=2,
60                radius_conf_2g=self.radius_conf_2g,
61                radius_conf_5g=self.radius_conf_5g,)
62        self.ent_network_2g_a = self.ent_networks[0]["2g"]
63        self.ent_network_2g_b = self.ent_networks[1]["2g"]
64        self.bssid_2g_a = self.ent_network_2g_a[WifiEnums.BSSID_KEY.lower()]
65        self.bssid_2g_b = self.ent_network_2g_b[WifiEnums.BSSID_KEY.lower()]
66        self.ent_roaming_ssid = self.ent_network_2g_a[WifiEnums.SSID_KEY]
67        self.bssid_a = self.bssid_2g_a
68        self.bssid_b = self.bssid_2g_b
69
70        self.config_peap = {
71            Ent.EAP: int(EAP.PEAP),
72            Ent.CA_CERT: self.ca_cert,
73            Ent.IDENTITY: self.eap_identity,
74            Ent.PASSWORD: self.eap_password,
75            Ent.PHASE2: int(EapPhase2.MSCHAPV2),
76            WifiEnums.SSID_KEY: self.ent_roaming_ssid
77        }
78        self.config_tls = {
79            Ent.EAP: int(EAP.TLS),
80            Ent.CA_CERT: self.ca_cert,
81            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
82            Ent.CLIENT_CERT: self.client_cert,
83            Ent.PRIVATE_KEY_ID: self.client_key,
84            Ent.IDENTITY: self.eap_identity,
85        }
86        self.config_ttls = {
87            Ent.EAP: int(EAP.TTLS),
88            Ent.CA_CERT: self.ca_cert,
89            Ent.IDENTITY: self.eap_identity,
90            Ent.PASSWORD: self.eap_password,
91            Ent.PHASE2: int(EapPhase2.MSCHAPV2),
92            WifiEnums.SSID_KEY: self.ent_roaming_ssid
93        }
94        self.config_sim = {
95            Ent.EAP: int(EAP.SIM),
96            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
97        }
98        self.attn_a = self.attenuators[0]
99        self.attn_b = self.attenuators[2]
100        # Set screen lock password so ConfigStore is unlocked.
101        self.dut.droid.setDevicePassword(self.device_password)
102        self.set_attns("default")
103
104    def teardown_class(self):
105        wutils.reset_wifi(self.dut)
106        self.dut.droid.disableDevicePassword(self.device_password)
107        self.dut.ed.clear_all_events()
108        self.set_attns("default")
109
110    def setup_test(self):
111        self.dut.droid.wifiStartTrackingStateChange()
112        self.dut.droid.wakeLockAcquireBright()
113        self.dut.droid.wakeUpNow()
114        wutils.reset_wifi(self.dut)
115        self.dut.ed.clear_all_events()
116
117    def teardown_test(self):
118        self.dut.droid.wakeLockRelease()
119        self.dut.droid.goToSleepNow()
120        self.dut.droid.wifiStopTrackingStateChange()
121        self.set_attns("default")
122
123    def on_fail(self, test_name, begin_time):
124        self.dut.take_bug_report(test_name, begin_time)
125        self.dut.cat_adb_log(test_name, begin_time)
126
127    def set_attns(self, attn_val_name):
128        """Sets attenuation values on attenuators used in this test.
129
130        Args:
131            attn_val_name: Name of the attenuation value pair to use.
132        """
133        self.log.info("Set attenuation values to %s",
134                      self.attn_vals[attn_val_name])
135        try:
136            self.attn_a.set_atten(self.attn_vals[attn_val_name][0])
137            self.attn_b.set_atten(self.attn_vals[attn_val_name][1])
138        except:
139            self.log.exception("Failed to set attenuation values %s.",
140                           attn_val_name)
141            raise
142
143    def trigger_roaming_and_validate(self, attn_val_name, expected_con):
144        """Sets attenuators to trigger roaming and validate the DUT connected
145        to the BSSID expected.
146
147        Args:
148            attn_val_name: Name of the attenuation value pair to use.
149            expected_con: The expected info of the network to we expect the DUT
150                to roam to.
151        """
152        self.set_attns(attn_val_name)
153        self.log.info("Wait %ss for roaming to finish.", self.roam_interval)
154        time.sleep(self.roam_interval)
155        try:
156            self.dut.droid.wakeLockAcquireBright()
157            self.dut.droid.wakeUpNow()
158            wutils.verify_wifi_connection_info(self.dut, expected_con)
159            expected_bssid = expected_con[WifiEnums.BSSID_KEY]
160            self.log.info("Roamed to %s successfully", expected_bssid)
161        finally:
162            self.dut.droid.wifiLockRelease()
163            self.dut.droid.goToSleepNow()
164
165    def roaming_between_a_and_b_logic(self, config):
166        """Test roaming between two enterprise APs.
167
168        Steps:
169        1. Make bssid_a visible, bssid_b not visible.
170        2. Connect to ent_roaming_ssid. Expect DUT to connect to bssid_a.
171        3. Make bssid_a not visible, bssid_b visible.
172        4. Expect DUT to roam to bssid_b.
173        5. Make bssid_a visible, bssid_b not visible.
174        6. Expect DUT to roam back to bssid_a.
175        """
176        expected_con_to_a = {
177            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
178            WifiEnums.BSSID_KEY: self.bssid_a,
179        }
180        expected_con_to_b = {
181            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
182            WifiEnums.BSSID_KEY: self.bssid_b,
183        }
184        self.set_attns("a_on_b_off")
185        wutils.wifi_connect(self.dut, config)
186        wutils.verify_wifi_connection_info(self.dut, expected_con_to_a)
187        self.log.info("Roaming from %s to %s", self.bssid_a, self.bssid_b)
188        self.trigger_roaming_and_validate("b_on_a_off", expected_con_to_b)
189        self.log.info("Roaming from %s to %s", self.bssid_b, self.bssid_a)
190        self.trigger_roaming_and_validate("a_on_b_off", expected_con_to_a)
191
192    """ Tests Begin """
193
194    @test_tracker_info(uuid="b15e4b3f-841d-428d-87ac-272f29f06e14")
195    def test_roaming_with_config_tls(self):
196        self.roaming_between_a_and_b_logic(self.config_tls)
197
198    @test_tracker_info(uuid="d349cfec-b4af-48b2-88b7-744f5de25d43")
199    def test_roaming_with_config_ttls_none(self):
200        config = dict(self.config_ttls)
201        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.NONE.value
202        self.roaming_between_a_and_b_logic(config)
203
204    @test_tracker_info(uuid="89b8161c-754e-4138-831d-5fe40f521ce4")
205    def test_roaming_with_config_ttls_pap(self):
206        config = dict(self.config_ttls)
207        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.PAP.value
208        self.roaming_between_a_and_b_logic(config)
209
210    @test_tracker_info(uuid="d4925470-924b-4d03-8437-83e26b5f2df3")
211    def test_roaming_with_config_ttls_mschap(self):
212        config = dict(self.config_ttls)
213        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.MSCHAP.value
214        self.roaming_between_a_and_b_logic(config)
215
216    @test_tracker_info(uuid="206b1327-dd9c-4742-8717-e7bf2a04eed6")
217    def test_roaming_with_config_ttls_mschapv2(self):
218        config = dict(self.config_ttls)
219        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.MSCHAPV2.value
220        self.roaming_between_a_and_b_logic(config)
221
222    @test_tracker_info(uuid="c2c0168b-2933-4954-af62-fb41f42dc45a")
223    def test_roaming_with_config_ttls_gtc(self):
224        config = dict(self.config_ttls)
225        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.GTC.value
226        self.roaming_between_a_and_b_logic(config)
227
228    @test_tracker_info(uuid="481c4102-8f5b-4fcd-95cc-5e3285f47985")
229    def test_roaming_with_config_peap_mschapv2(self):
230        config = dict(self.config_peap)
231        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.MSCHAPV2.value
232        self.roaming_between_a_and_b_logic(config)
233
234    @test_tracker_info(uuid="404155d4-33a7-42b3-b369-5f2d63d19f16")
235    def test_roaming_with_config_peap_gtc(self):
236        config = dict(self.config_peap)
237        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.GTC.value
238        self.roaming_between_a_and_b_logic(config)
239
240    """ Tests End """
241