1#!/usr/bin/env python3
2#
3# Copyright (C) 2019 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 signals
18
19from acts.base_test import BaseTestClass
20from acts import asserts
21
22
23class NetstackIfaceTest(BaseTestClass):
24    default_timeout = 10
25    active_scan_callback_list = []
26    active_adv_callback_list = []
27    droid = None
28
29    def setup_class(self):
30        super().setup_class()
31        if (len(self.fuchsia_devices) < 1):
32            self.log.error(
33                "NetstackFuchsiaTest Init: Not enough fuchsia devices.")
34        self.log.info("Running testbed setup with one fuchsia devices")
35        self.dut = self.fuchsia_devices[0]
36        self.dut.netstack_lib.init()
37
38    def _enable_all_interfaces(self):
39        interfaces = self.dut.netstack_lib.netstackListInterfaces()
40        for item in interfaces.get("result"):
41            identifier = item.get('id')
42            self.dut.netstack_lib.enableInterface(identifier)
43
44    def setup_test(self):
45        # Always make sure all interfaces listed are in an up state.
46        self._enable_all_interfaces()
47
48    def teardown_test(self):
49        # Always make sure all interfaces listed are in an up state.
50        self._enable_all_interfaces()
51
52    def test_list_interfaces(self):
53        """Test listing all interfaces.
54
55        Steps:
56        1. Call ListInterfaces FIDL api.
57        2. Verify there is at least one interface returned.
58
59        Expected Result:
60        There were no errors in retrieving the list of interfaces.
61        There was at least one interface in the list.
62
63        Returns:
64          signals.TestPass if no errors
65          signals.TestFailure if there are any errors during the test.
66
67        TAGS: Netstack
68        Priority: 1
69        """
70        interfaces = self.dut.netstack_lib.netstackListInterfaces()
71        if interfaces.get('error') is not None:
72            raise signals.TestFailure("Failed with {}".format(
73                interfaces.get('error')))
74        if len(interfaces.get('result')) < 1:
75            raise signals.TestFailure("No interfaces found.")
76        self.log.info("Interfaces found: {}".format(interfaces.get('result')))
77        raise signals.TestPass("Success")
78
79    def test_get_interface_by_id(self):
80        """Tests getting interface information by id on all interfaces.
81
82        Steps:
83        1. Call ListInterfaces FIDL api.
84        2. For each interface in the list, call GetInterfaceInfo FIDL api.
85
86        Expected Result:
87        There were no errors in each GetInterfaceInfo call.
88
89        Returns:
90          signals.TestPass if no errors
91          signals.TestFailure if there are any errors during the test.
92
93        TAGS: Netstack
94        Priority: 1
95        """
96        interfaces = self.dut.netstack_lib.netstackListInterfaces()
97        if interfaces.get('error') is not None:
98            raise signals.TestFailure("Failed with {}".format(
99                interfaces.get('error')))
100        for item in interfaces.get("result"):
101            identifier = item.get('id')
102            interface_info_result = self.dut.netstack_lib.getInterfaceInfo(
103                identifier)
104            if interface_info_result.get('error') is not None:
105                raise signals.TestFailure(
106                    "Get interfaces info failed with {}".format(
107                        interface_info_result.get('error')))
108            else:
109                result = interface_info_result.get('result')
110                if result is None:
111                    raise signals.TestFailure(
112                        "Interface info returned None: {}".format(result))
113                self.log.info("Interface {} info: {}".format(
114                    identifier, result))
115        raise signals.TestPass("Success")
116
117    def test_toggle_wlan_interface(self):
118        """Test toggling the wlan interface if it exists.
119
120        Steps:
121        1. Call ListInterfaces FIDL api.
122        2. Find the wlan interface.
123        3. Disable the interface.
124        4. Verify interface attributes in a down state.
125        5. Enable the interface.
126        6. Verify interface attributes in an up state.
127
128        Expected Result:
129        WLAN interface was successfully brought down and up again.
130
131        Returns:
132          signals.TestPass if no errors
133          signals.TestFailure if there are any errors during the test.
134
135        TAGS: Netstack
136        Priority: 1
137        """
138        interfaces = self.dut.netstack_lib.netstackListInterfaces()
139        for item in interfaces.get('result'):
140            # Find the WLAN interface
141            if "wlan" in item.get('name'):
142                identifier = item.get('id')
143                # Disable the interface by ID.
144                result = self.dut.netstack_lib.disableInterface(identifier)
145                if result.get('error') is not None:
146                    raise signals.TestFailure(
147                        "Unable to disable wlan interface: {}".format(
148                            result.get('error')))
149
150                # Check the current state of the interface.
151                interface_info_result = self.dut.netstack_lib.getInterfaceInfo(
152                    identifier)
153                interface_info = interface_info_result.get('result')
154
155                if len(interface_info.get('ipv4_addresses')) > 0:
156                    raise signals.TestFailure(
157                        "No Ipv4 Address should be present: {}".format(
158                            interface_info))
159
160                # TODO (35981): Verify other values when interface down.
161
162                # Re-enable the interface
163                result = self.dut.netstack_lib.enableInterface(identifier)
164                if result.get('error') is not None:
165                    raise signals.TestFailure(
166                        "Unable to enable wlan interface: {}".format(
167                            result.get('error')))
168
169                # TODO (35981): Verify other values when interface up.
170
171                raise signals.TestPass("Success")
172
173        raise signals.TestSkip("No WLAN interface found.")
174