iw_runner_unittest.py revision 6b8a9553d74d5ff4310e0dc7b0625d6b2349a40d
1# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from autotest_lib.client.common_lib.cros.network import iw_runner
8
9class IwRunnerTest(unittest.TestCase):
10    """Unit test for the IWRunner object."""
11
12
13    class host_cmd(object):
14        """Mock host command class."""
15
16        def __init__(self, stdout, stderr, exit_status):
17            self._stdout = stdout
18            self._stderr = stderr
19            self._exit_status = exit_status
20
21
22        @property
23        def stdout(self):
24            """Returns stdout."""
25            return self._stdout
26
27
28        @property
29        def stderr(self):
30            """Returns stderr."""
31            return self._stderr
32
33
34        @property
35        def exit_status(self):
36            """Returns the exit status."""
37            return self._exit_status
38
39
40    class host(object):
41        """Mock host class."""
42
43        def __init__(self, host_cmd):
44            self._host_cmd = IwRunnerTest.host_cmd(host_cmd, 1.0, 0)
45
46
47        def run(self, cmd, ignore_status=False):
48            """Returns the mocked output.
49
50            @param cmd: a stub input ignore
51            @param ignore_status: a stub input ignore
52
53            """
54            return self._host_cmd
55
56
57    HT20 = str('BSS aa:aa:aa:aa:aa:aa (on wlan0)\n'
58        '    freq: 2412\n'
59        '    signal: -50.00 dBm\n'
60        '    SSID: support_ht20\n'
61        '    HT operation:\n'
62        '         * secondary channel offset: no secondary\n')
63
64    HT20_IW_BSS = iw_runner.IwBss('aa:aa:aa:aa:aa:aa', 2412,
65                                  'support_ht20', iw_runner.SECURITY_OPEN,
66                                  iw_runner.HT20, -50.00)
67
68    HT20_2 = str('BSS 11:11:11:11:11:11 (on wlan0)\n'
69        '     freq: 2462\n'
70        '     signal: -42.00 dBm\n'
71        '     SSID: support_ht20\n'
72        '     WPA:          * Version: 1\n'
73        '     HT operation:\n'
74        '          * secondary channel offset: below\n')
75
76    HT20_2_IW_BSS = iw_runner.IwBss('11:11:11:11:11:11', 2462,
77                                    'support_ht20', iw_runner.SECURITY_WPA,
78                                    iw_runner.HT40_BELOW, -42.00)
79
80    HT40_ABOVE = str('BSS bb:bb:bb:bb:bb:bb (on wlan0)\n'
81        '    freq: 5180\n'
82        '    signal: -55.00 dBm\n'
83        '    SSID: support_ht40_above\n'
84        '    RSN:          * Version: 1\n'
85        '    HT operation:\n'
86        '         * secondary channel offset: above\n')
87
88    HT40_ABOVE_IW_BSS = iw_runner.IwBss('bb:bb:bb:bb:bb:bb', 5180,
89                                        'support_ht40_above',
90                                        iw_runner.SECURITY_WPA2,
91                                        iw_runner.HT40_ABOVE, -55.00)
92
93    HT40_BELOW = str('BSS cc:cc:cc:cc:cc:cc (on wlan0)\n'
94        '    freq: 2462\n'
95        '    signal: -44.00 dBm\n'
96        '    SSID: support_ht40_below\n'
97        '    RSN:          * Version: 1\n'
98        '    WPA:          * Version: 1\n'
99        '    HT operation:\n'
100        '        * secondary channel offset: below\n')
101
102    HT40_BELOW_IW_BSS = iw_runner.IwBss('cc:cc:cc:cc:cc:cc', 2462,
103                                        'support_ht40_below',
104                                        iw_runner.SECURITY_MIXED,
105                                        iw_runner.HT40_BELOW, -44.00)
106
107    NO_HT = str('BSS dd:dd:dd:dd:dd:dd (on wlan0)\n'
108        '    freq: 2412\n'
109        '    signal: -45.00 dBm\n'
110        '    SSID: no_ht_support\n')
111
112    NO_HT_IW_BSS = iw_runner.IwBss('dd:dd:dd:dd:dd:dd', 2412,
113                                   'no_ht_support', iw_runner.SECURITY_OPEN,
114                                   None, -45.00)
115
116    HIDDEN_SSID = str('BSS ee:ee:ee:ee:ee:ee (on wlan0)\n'
117        '    freq: 2462\n'
118        '    signal: -70.00 dBm\n'
119        '    SSID: \n'
120        '    HT operation:\n'
121        '         * secondary channel offset: no secondary\n')
122
123    HIDDEN_SSID_IW_BSS = iw_runner.IwBss('ee:ee:ee:ee:ee:ee', 2462,
124                                         None, iw_runner.SECURITY_OPEN,
125                                         iw_runner.HT20, -70.00)
126
127
128    def verify_values(self, iw_bss_1, iw_bss_2):
129        """Checks all of the IWBss values
130
131        @param iw_bss_1: an IWBss object
132        @param iw_bss_2: an IWBss object
133
134        """
135        self.assertEquals(iw_bss_1.bss, iw_bss_2[0].bss)
136        self.assertEquals(iw_bss_1.ssid, iw_bss_2[0].ssid)
137        self.assertEquals(iw_bss_1.frequency, iw_bss_2[0].frequency)
138        self.assertEquals(iw_bss_1.security, iw_bss_2[0].security)
139        self.assertEquals(iw_bss_1.ht, iw_bss_2[0].ht)
140        self.assertEquals(iw_bss_1.signal, iw_bss_2[0].signal)
141
142
143    def search_by_bss(self, scan_output, test_iw_bss):
144        """
145
146        @param scan_output: the output of the scan as a string
147        @param test_iw_bss: an IWBss object
148
149        Uses the runner to search for a network by bss.
150        """
151        host = self.host(scan_output)
152        runner = iw_runner.IwRunner(remote_host=host)
153        network = runner.wait_for_scan_result('wlan0', bsses=[test_iw_bss.bss])
154        self.verify_values(test_iw_bss, network)
155
156
157    def test_find_first(self):
158        """Test with the first item in the list."""
159        scan_output = self.HT20 + self.HT40_ABOVE
160        self.search_by_bss(scan_output, self.HT20_IW_BSS)
161
162
163    def test_find_last(self):
164        """Test with the last item in the list."""
165        scan_output = self.HT40_ABOVE + self.HT20
166        self.search_by_bss(scan_output, self.HT20_IW_BSS)
167
168
169    def test_find_middle(self):
170        """Test with the middle item in the list."""
171        scan_output = self.HT40_ABOVE + self.HT20 + self.NO_HT
172        self.search_by_bss(scan_output, self.HT20_IW_BSS)
173
174
175    def test_ht40_above(self):
176        """Test with a HT40+ network."""
177        scan_output = self.HT20 + self.HT40_ABOVE + self.NO_HT
178        self.search_by_bss(scan_output, self.HT40_ABOVE_IW_BSS)
179
180
181    def test_ht40_below(self):
182        """Test with a HT40- network."""
183        scan_output = self.HT20 + self.HT40_BELOW + self.NO_HT
184        self.search_by_bss(scan_output, self.HT40_BELOW_IW_BSS)
185
186
187    def test_no_ht(self):
188        """Test with a network that doesn't have ht."""
189        scan_output = self.HT20 + self.NO_HT + self.HT40_ABOVE
190        self.search_by_bss(scan_output, self.NO_HT_IW_BSS)
191
192
193    def test_hidden_ssid(self):
194        """Test with a network with a hidden ssid."""
195        scan_output = self.HT20 + self.HIDDEN_SSID + self.NO_HT
196        self.search_by_bss(scan_output, self.HIDDEN_SSID_IW_BSS)
197
198
199    def test_multiple_ssids(self):
200        """Test with multiple networks with the same ssids."""
201        return
202        scan_output = self.HT40_ABOVE + self.HT20 + self.NO_HT + self.HT20_2
203        host = self.host(scan_output)
204        runner = iw_runner.IwRunner(remote_host=host)
205        networks = runner.wait_for_scan_result('wlan 0',
206                                               ssids=[self.HT20_2_IW_BSS.ssid])
207        for iw_bss_1, iw_bss_2 in zip([self.HT20_IW_BSS, self.HT20_2_IW_BSS],
208                                      networks):
209            self.verify_values(iw_bss_1, iw_bss_2)
210