1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17from acts.base_test import BaseTestClass
18from acts import asserts
19from acts import utils
20
21AP_ROLE = 'Ap'
22DEFAULT_SSID = 'testssid'
23DEFAULT_SECURITY = 'none'
24DEFAULT_PASSWORD = ''
25DEFAULT_CONNECTIVITY_MODE = 'local_only'
26DEFAULT_OPERATING_BAND = 'any'
27TEST_MAC_ADDR = '12:34:56:78:9a:bc'
28TEST_MAC_ADDR_SECONDARY = 'bc:9a:78:56:34:12'
29
30
31class WlanDeprecatedConfigurationTest(BaseTestClass):
32    """Tests for WlanDeprecatedConfigurationFacade"""
33    def setup_class(self):
34        super().setup_class()
35        self.dut = self.fuchsia_devices[0]
36
37    def setup_test(self):
38        self._stop_soft_aps()
39
40    def teardown_test(self):
41        self._stop_soft_aps()
42
43    def on_fail(self, test_name, begin_time):
44        for fd in self.fuchsia_devices:
45            try:
46                fd.take_bug_report(test_name, begin_time)
47                fd.get_log(test_name, begin_time)
48            except Exception:
49                pass
50
51            try:
52                if fd.device.hard_reboot_on_fail:
53                    fd.hard_power_cycle(self.pdu_devices)
54            except AttributeError:
55                pass
56
57    def _get_ap_interface_mac_address(self):
58        """Retrieves mac address from wlan interface with role ap
59
60        Returns:
61            string, the mac address of the AP interface
62
63        Raises:
64            ConnectionError, if SL4F calls fail
65            AttributeError, if no interface has role 'Ap'
66        """
67        wlan_ifaces = self.dut.wlan_lib.wlanGetIfaceIdList()
68        if wlan_ifaces.get('error'):
69            raise ConnectionError('Failed to get wlan interface IDs: %s' %
70                                  wlan_ifaces['error'])
71
72        for wlan_iface in wlan_ifaces['result']:
73            iface_info = self.dut.wlan_lib.wlanQueryInterface(wlan_iface)
74            if iface_info.get('error'):
75                raise ConnectionError('Failed to query wlan iface: %s' %
76                                      iface_info['error'])
77
78            if iface_info['result']['role'] == AP_ROLE:
79                return utils.mac_address_list_to_str(
80                    iface_info['result']['mac_addr'])
81        raise AttributeError(
82            'Failed to get ap interface mac address. No AP interface found.')
83
84    def _start_soft_ap(self):
85        """Starts SoftAP on DUT.
86
87        Raises:
88            ConnectionError, if SL4F call fails.
89        """
90        self.log.info('Starting SoftAP on Fuchsia device (%s).' % self.dut.ip)
91        response = self.dut.wlan_ap_policy_lib.wlanStartAccessPoint(
92            DEFAULT_SSID, DEFAULT_SECURITY, DEFAULT_PASSWORD,
93            DEFAULT_CONNECTIVITY_MODE, DEFAULT_OPERATING_BAND)
94        if response.get('error'):
95            raise ConnectionError('Failed to setup SoftAP: %s' %
96                                  response['error'])
97
98    def _stop_soft_aps(self):
99        """Stops SoftAP on DUT.
100
101        Raises:
102            ConnectionError, if SL4F call fails.
103        """
104        self.log.info('Stopping SoftAP.')
105        response = self.dut.wlan_ap_policy_lib.wlanStopAllAccessPoint()
106        if response.get('error'):
107            raise ConnectionError('Failed to stop SoftAP: %s' %
108                                  response['error'])
109
110    def _suggest_ap_mac_addr(self, mac_addr):
111        """Suggests mac address for AP interface.
112        Args:
113            mac_addr: string, mac address to suggest.
114
115        Raises:
116            TestFailure, if SL4F call fails.
117        """
118        self.log.info(
119            'Suggesting AP mac addr (%s) via wlan_deprecated_configuration_lib.'
120            % mac_addr)
121        response = self.dut.wlan_deprecated_configuration_lib.wlanSuggestAccessPointMacAddress(
122            mac_addr)
123        if response.get('error'):
124            asserts.fail('Failed to suggest AP mac address (%s): %s' %
125                         (mac_addr, response['error']))
126
127    def _verify_mac_addr(self, expected_addr):
128        """ Verifies mac address of ap interface is set to expected mac address.
129
130        Args:
131            Args:
132                expected_addr: string, expected mac address
133
134            Raises:
135                TestFailure, if actual mac address is not expected mac address.
136        """
137        set_mac_addr = self._get_ap_interface_mac_address()
138        if set_mac_addr != expected_addr:
139            asserts.fail(
140                'Failed to set AP mac address '
141                'via wlan_deprecated_configuration_lib. Expected mac addr: %s,'
142                ' Actual mac addr: %s' % (expected_addr, set_mac_addr))
143        else:
144            self.log.info('AP mac address successfully set to %s' %
145                          expected_addr)
146
147    def test_suggest_ap_mac_address(self):
148        """Tests suggest ap mac address SL4F call
149
150        1. Get initial mac address
151        2. Suggest new mac address
152        3. Verify new mac address is set successfully
153        4. Reset to initial mac address
154        5. Verify initial mac address is reset successfully
155
156
157        Raises:
158            TestFailure, if wlanSuggestAccessPointMacAddress call fails or
159                of mac address is not the suggest value
160            ConnectionError, if other SL4F calls fail
161        """
162        # Retrieve initial ap mac address
163        self._start_soft_ap()
164
165        self.log.info('Getting initial mac address.')
166        initial_mac_addr = self._get_ap_interface_mac_address()
167        self.log.info('Initial mac address: %s' % initial_mac_addr)
168
169        if initial_mac_addr != TEST_MAC_ADDR:
170            suggested_mac_addr = TEST_MAC_ADDR
171        else:
172            suggested_mac_addr = TEST_MAC_ADDR_SECONDARY
173
174        self._stop_soft_aps()
175
176        # Suggest and verify new mac address
177        self._suggest_ap_mac_addr(suggested_mac_addr)
178
179        self._start_soft_ap()
180
181        self._verify_mac_addr(suggested_mac_addr)
182
183        self._stop_soft_aps()
184
185        # Reset to initial mac address and verify
186        self.log.info('Resetting to initial mac address (%s).' %
187                      initial_mac_addr)
188        self._suggest_ap_mac_addr(initial_mac_addr)
189
190        self._start_soft_ap()
191
192        self._verify_mac_addr(initial_mac_addr)
193