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
16from acts.test_decorators import test_tracker_info
17from acts_contrib.test_utils.wifi import wifi_test_utils as wutils
18from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
19
20WifiEnums = wutils.WifiEnums
21
22# EAP Macros
23EAP = WifiEnums.Eap
24EapPhase2 = WifiEnums.EapPhase2
25
26# Enterprise Config Macros
27Ent = WifiEnums.Enterprise
28
29
30class WifiEnterpriseRoamingTest(WifiBaseTest):
31
32    def __init__(self, configs):
33        super().__init__(configs)
34        self.enable_packet_log = True
35
36    def setup_class(self):
37        super().setup_class()
38
39        self.dut = self.android_devices[0]
40        req_params = (
41            "roaming_attn",
42            # Expected time within which roaming should finish, in seconds.
43            "roam_interval",
44            "ca_cert",
45            "client_cert",
46            "client_key",
47            "eap_identity",
48            "eap_password",
49            "device_password",
50            "wifi6_models",
51            "radius_conf_2g",
52            "radius_conf_5g")
53        self.unpack_userparams(req_params)
54        if "AccessPoint" in self.user_params:
55            self.legacy_configure_ap_and_start(
56                mirror_ap=True,
57                ent_network=True,
58                ap_count=2,
59                radius_conf_2g=self.radius_conf_2g,
60                radius_conf_5g=self.radius_conf_5g,)
61        elif "OpenWrtAP" in self.user_params:
62            self.configure_openwrt_ap_and_start(
63                mirror_ap=True,
64                ent_network=True,
65                ap_count=2,
66                radius_conf_2g=self.radius_conf_2g,
67                radius_conf_5g=self.radius_conf_5g,)
68        self.ent_network_2g_a = self.ent_networks[0]["2g"]
69        self.ent_network_2g_b = self.ent_networks[1]["2g"]
70        self.ent_roaming_ssid = self.ent_network_2g_a[WifiEnums.SSID_KEY]
71        if "AccessPoint" in self.user_params:
72            self.bssid_a = self.ent_network_2g_a[WifiEnums.BSSID_KEY.lower()]
73            self.bssid_b = self.ent_network_2g_b[WifiEnums.BSSID_KEY.lower()]
74        elif "OpenWrtAP" in self.user_params:
75            self.bssid_a = self.bssid_map[0]["2g"][self.ent_roaming_ssid]
76            self.bssid_b = self.bssid_map[1]["2g"][self.ent_roaming_ssid]
77
78        self.config_peap = {
79            Ent.EAP: int(EAP.PEAP),
80            Ent.CA_CERT: self.ca_cert,
81            Ent.IDENTITY: self.eap_identity,
82            Ent.PASSWORD: self.eap_password,
83            Ent.PHASE2: int(EapPhase2.MSCHAPV2),
84            WifiEnums.SSID_KEY: self.ent_roaming_ssid
85        }
86        self.config_tls = {
87            Ent.EAP: int(EAP.TLS),
88            Ent.CA_CERT: self.ca_cert,
89            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
90            Ent.CLIENT_CERT: self.client_cert,
91            Ent.PRIVATE_KEY_ID: self.client_key,
92            Ent.IDENTITY: self.eap_identity,
93        }
94        self.config_ttls = {
95            Ent.EAP: int(EAP.TTLS),
96            Ent.CA_CERT: self.ca_cert,
97            Ent.IDENTITY: self.eap_identity,
98            Ent.PASSWORD: self.eap_password,
99            Ent.PHASE2: int(EapPhase2.MSCHAPV2),
100            WifiEnums.SSID_KEY: self.ent_roaming_ssid
101        }
102        self.config_sim = {
103            Ent.EAP: int(EAP.SIM),
104            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
105        }
106        self.config_aka = {
107            Ent.EAP: int(EAP.AKA),
108            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
109        }
110        self.config_aka_prime = {
111            Ent.EAP: int(EAP.AKA_PRIME),
112            WifiEnums.SSID_KEY: self.ent_roaming_ssid,
113        }
114        self.attn_a = self.attenuators[0]
115        self.attn_b = self.attenuators[2]
116        if "OpenWrtAP" in self.user_params:
117            self.attn_b = self.attenuators[1]
118        # Set screen lock password so ConfigStore is unlocked.
119        self.dut.droid.setDevicePassword(self.device_password)
120        wutils.set_attns(self.attenuators, "default")
121
122    def teardown_class(self):
123        wutils.reset_wifi(self.dut)
124        self.dut.droid.disableDevicePassword(self.device_password)
125        self.dut.ed.clear_all_events()
126        wutils.set_attns(self.attenuators, "default")
127
128    def setup_test(self):
129        super().setup_test()
130        self.dut.droid.wifiStartTrackingStateChange()
131        self.dut.droid.wakeLockAcquireBright()
132        self.dut.droid.wakeUpNow()
133        wutils.reset_wifi(self.dut)
134        self.dut.ed.clear_all_events()
135
136    def teardown_test(self):
137        super().teardown_test()
138        self.dut.droid.wakeLockRelease()
139        self.dut.droid.goToSleepNow()
140        self.dut.droid.wifiStopTrackingStateChange()
141        wutils.set_attns(self.attenuators, "default")
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        wutils.set_attns_steps(
153            self.attenuators, attn_val_name, self.roaming_attn)
154        self.log.info("Wait %ss for roaming to finish.", 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        wutils.set_attns_steps(
185            self.attenuators, "AP1_on_AP2_off", self.roaming_attn)
186        wutils.connect_to_wifi_network(self.dut, config)
187        wutils.verify_11ax_wifi_connection(
188            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
189        wutils.verify_wifi_connection_info(self.dut, expected_con_to_a)
190        self.log.info("Roaming from %s to %s", self.bssid_a, self.bssid_b)
191        self.trigger_roaming_and_validate("AP1_off_AP2_on", expected_con_to_b)
192        wutils.verify_11ax_wifi_connection(
193            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
194        self.log.info("Roaming from %s to %s", self.bssid_b, self.bssid_a)
195        self.trigger_roaming_and_validate("AP1_on_AP2_off", expected_con_to_a)
196        wutils.verify_11ax_wifi_connection(
197            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
198
199    """ Tests Begin """
200
201    @test_tracker_info(uuid="b15e4b3f-841d-428d-87ac-272f29f06e14")
202    def test_roaming_with_config_tls(self):
203        self.roaming_between_a_and_b_logic(self.config_tls)
204
205    @test_tracker_info(uuid="d349cfec-b4af-48b2-88b7-744f5de25d43")
206    def test_roaming_with_config_ttls_none(self):
207        config = dict(self.config_ttls)
208        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.NONE.value
209        self.roaming_between_a_and_b_logic(config)
210
211    @test_tracker_info(uuid="89b8161c-754e-4138-831d-5fe40f521ce4")
212    def test_roaming_with_config_ttls_pap(self):
213        config = dict(self.config_ttls)
214        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.PAP.value
215        self.roaming_between_a_and_b_logic(config)
216
217    @test_tracker_info(uuid="d4925470-924b-4d03-8437-83e26b5f2df3")
218    def test_roaming_with_config_ttls_mschap(self):
219        config = dict(self.config_ttls)
220        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.MSCHAP.value
221        self.roaming_between_a_and_b_logic(config)
222
223    @test_tracker_info(uuid="206b1327-dd9c-4742-8717-e7bf2a04eed6")
224    def test_roaming_with_config_ttls_mschapv2(self):
225        config = dict(self.config_ttls)
226        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.MSCHAPV2.value
227        self.roaming_between_a_and_b_logic(config)
228
229    @test_tracker_info(uuid="c2c0168b-2933-4954-af62-fb41f42dc45a")
230    def test_roaming_with_config_ttls_gtc(self):
231        config = dict(self.config_ttls)
232        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.GTC.value
233        self.roaming_between_a_and_b_logic(config)
234
235    @test_tracker_info(uuid="481c4102-8f5b-4fcd-95cc-5e3285f47985")
236    def test_roaming_with_config_peap_mschapv2(self):
237        config = dict(self.config_peap)
238        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.MSCHAPV2.value
239        self.roaming_between_a_and_b_logic(config)
240
241    @test_tracker_info(uuid="404155d4-33a7-42b3-b369-5f2d63d19f16")
242    def test_roaming_with_config_peap_gtc(self):
243        config = dict(self.config_peap)
244        config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.GTC.value
245        self.roaming_between_a_and_b_logic(config)
246
247    @test_tracker_info(uuid="e014fc94-a547-4aa7-953e-cff3bfe3f20c")
248    def test_roaming_with_config_sim(self):
249        self.roaming_between_a_and_b_logic(self.config_sim)
250
251    @test_tracker_info(uuid="2e45a59e-ec22-4bf4-811f-1a3a4b9bb330")
252    def test_roaming_with_config_aka(self):
253        self.roaming_between_a_and_b_logic(self.config_aka)
254
255    @test_tracker_info(uuid="63ebfdf5-e765-47ff-9754-f60e3f6e7409")
256    def test_roaming_with_config_aka_prime(self):
257        self.roaming_between_a_and_b_logic(self.config_aka_prime)
258
259    """ Tests End """