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