def normalize(cell_block): # The cell blocks come in with every line except the first indented at # least 20 spaces. This removes the first 20 spaces off of those lines. lines = textwrap.dedent(' ' * 20 + cell_block).splitlines() cell = Cell() while lines: line = lines.pop(0) if line.startswith('Quality'): for re_name, quality_re in quality_re_dict.items(): match_result = quality_re.search(line) if match_result is not None: cell.quality, signal = match_result.groups() if re_name == 'relative': actual, total = map(int, signal.split('/')) cell.signal = db2dbm(int((actual / total) * 100)) else: cell.signal = int(signal) break elif line.startswith('Bit Rates'): values = split_on_colon(line)[1].split('; ') # consume next line of bit rates, because they are split on # different lines, sometimes... while lines[0].startswith(' ' * 10): values += lines.pop(0).strip().split('; ') cell.bitrates.extend(values) elif ':' in line: key, value = split_on_colon(line) key = normalize_key(key) if key == 'ie': if 'Unknown' in value: continue # consume remaining block values = [value] while lines and lines[0].startswith(' ' * 4): values.append(lines.pop(0).strip()) if 'WPA2' in value: cell.encryption_type = 'wpa2' elif 'WPA' in value: cell.encryption_type = 'wpa' if key == 'frequency': frequency, channel = frequency_re.search(value).groups() cell.frequency = frequency cell.channel = int(channel) elif key in normalize_value: setattr(cell, key, normalize_value[key](value)) # It seems that encryption types other than WEP need to specify their # existence. if cell.encrypted and not hasattr(cell, 'encryption_type'): cell.encryption_type = 'wep' return cell
def test_unencrypted(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = False scheme = Scheme.for_cell('wlan0', 'test', cell) self.assertEqual(scheme.options, { 'wireless-essid': 'SSID', 'wireless-channel': 'auto', })
def test_wep(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = True cell.encryption_type = 'wep' scheme = Scheme.for_cell('wlan0', 'test', cell, 'passkey') self.assertEqual(scheme.options, { 'wireless-essid': 'SSID', 'wireless-key': 'passkey', })
def test_wpa(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = True cell.encryption_type = 'wpa' scheme = Scheme.for_cell('wlan0', 'test', cell, 'passkey') self.assertEqual( scheme.options, { 'wpa-ssid': 'SSID', 'wpa-psk': 'ea1548d4e8850c8d94c5ef9ed6fe483981b64c1436952cb1bf80c08a68cdc763', 'wireless-channel': 'auto', })
def test_empty_init(self): tcell = Cell() for field, value in self.fields.iteritems(): self.assertEqual(value, getattr(tcell, field))