• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
37    def _enable_all_interfaces(self):
38        interfaces = self.dut.netstack_lib.netstackListInterfaces()
39        for item in interfaces.get("result"):
40            identifier = item.get('id')
41            self.dut.netstack_lib.enableInterface(identifier)
42
43    def setup_test(self):
44        # Always make sure all interfaces listed are in an up state.
45        self._enable_all_interfaces()
46
47    def teardown_test(self):
48        # Always make sure all interfaces listed are in an up state.
49        self._enable_all_interfaces()
50
51    def test_list_interfaces(self):
52        """Test listing all interfaces.
53
54        Steps:
55        1. Call ListInterfaces FIDL api.
56        2. Verify there is at least one interface returned.
57
58        Expected Result:
59        There were no errors in retrieving the list of interfaces.
60        There was at least one interface in the list.
61
62        Returns:
63          signals.TestPass if no errors
64          signals.TestFailure if there are any errors during the test.
65
66        TAGS: Netstack
67        Priority: 1
68        """
69        interfaces = self.dut.netstack_lib.netstackListInterfaces()
70        if interfaces.get('error') is not None:
71            raise signals.TestFailure("Failed with {}".format(
72                interfaces.get('error')))
73        if len(interfaces.get('result')) < 1:
74            raise signals.TestFailure("No interfaces found.")
75        self.log.info("Interfaces found: {}".format(interfaces.get('result')))
76        raise signals.TestPass("Success")
77
78    def test_toggle_wlan_interface(self):
79        """Test toggling the wlan interface if it exists.
80
81        Steps:
82        1. Call ListInterfaces FIDL api.
83        2. Find the wlan interface.
84        3. Disable the interface.
85        4. Verify interface attributes in a down state.
86        5. Enable the interface.
87        6. Verify interface attributes in an up state.
88
89        Expected Result:
90        WLAN interface was successfully brought down and up again.
91
92        Returns:
93          signals.TestPass if no errors
94          signals.TestFailure if there are any errors during the test.
95          signals.TestSkip if there are no wlan interfaces.
96
97        TAGS: Netstack
98        Priority: 1
99        """
100
101        def get_wlan_interfaces():
102            result = self.dut.netstack_lib.netstackListInterfaces()
103            if (error := result.get('error')):
104                raise signals.TestFailure(
105                    f'unable to list interfaces: {error}')
106            return [
107                interface for interface in result.get('result')
108                if 'wlan' in interface.get('name')
109            ]
110
111        def get_ids(interfaces):
112            return [get_id(interface) for interface in interfaces]
113
114        wlan_interfaces = get_wlan_interfaces()
115        if not wlan_interfaces:
116            raise signals.TestSkip('no wlan interface found')
117        interface_ids = get_ids(wlan_interfaces)
118
119        # Disable the interfaces.
120        for identifier in interface_ids:
121            result = self.dut.netstack_lib.disableInterface(identifier)
122            if (error := result.get('error')):
123                raise signals.TestFailure(
124                    f'failed to disable wlan interface {identifier}: {error}')
125
126        # Retrieve the interfaces again.
127        disabled_wlan_interfaces = get_wlan_interfaces()
128        disabled_interface_ids = get_ids(wlan_interfaces)
129
130        if not disabled_interface_ids == interface_ids:
131            raise signals.TestFailure(
132                f'disabled interface IDs do not match original interface IDs: original={interface_ids} disabled={disabled_interface_ids}'
133            )
134
135        # Check the current state of the interfaces.
136        for interface in disabled_interfaces:
137            if len(interface_info.get('ipv4_addresses')) > 0:
138                raise signals.TestFailure(
139                    f'no Ipv4 Address should be present: {interface}')
140
141            # TODO (35981): Verify other values when interface down.
142
143        # Re-enable the interfaces.
144        for identifier in disabled_interface_ids:
145            result = self.dut.netstack_lib.enableInterface(identifier)
146            if (error := result.get('error')):
147                raise signals.TestFailure(
148                    f'failed to enable wlan interface {identifier}: {error}')
149
150        # TODO (35981): Verify other values when interface up.
151        raise signals.TestPass("Success")
152