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