diff --git a/.travis.yml b/.travis.yml index f3f0ae9..276edd6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ python: # command to install dependencies before_install: - sudo apt-get install wireless-tools + - sudo pip install -U pip wheel setuptools install: - sudo python setup.py -q install # command to run tests diff --git a/setup.py b/setup.py index f88dde6..96e4f21 100644 --- a/setup.py +++ b/setup.py @@ -13,8 +13,8 @@ def read(fname): install_requires = [ - 'setuptools', 'pbkdf2', + 'mock', ] try: import argparse diff --git a/tests/test_parsing.py b/tests/test_parsing.py index eaf04df..afbfea4 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -68,6 +68,7 @@ def test_absolute_quality(self): class ScanningTest(TestCase): def test_scanning(self): self.assertRaises(InterfaceError, Cell.all, 'fake-interface') + self.assertRaises(InterfaceError, Cell.all, 'fake-interface', True) IWLIST_SCAN_NO_ENCRYPTION = """Cell 02 - Address: 38:83:45:CC:58:74 @@ -278,3 +279,4 @@ def test_scanning(self): Encryption key:off Bit Rates:144 Mb/s """ + diff --git a/tests/test_scan.py b/tests/test_scan.py new file mode 100644 index 0000000..e21dd41 --- /dev/null +++ b/tests/test_scan.py @@ -0,0 +1,23 @@ +from unittest import TestCase + +from wifi.scan import Cell +from wifi import subprocess_compat as subprocess +from mock import patch +from test_parsing import IWLIST_SCAN_WEP, IWLIST_SCAN_WPA2 + + +expected_output = '\n'.join([IWLIST_SCAN_WEP, IWLIST_SCAN_WPA2]) + +class ScanTest(TestCase): + def test_all_calls_check_output_with_good_args(self): + args = ['/sbin/iwlist', 'interface', 'scan'] + kwargs = {'stderr':subprocess.STDOUT} + with patch.object(subprocess, 'check_output', + return_value=expected_output): + #subprocess.check_output = MagicMock(return_value=expected_output) + Cell.all('interface') + subprocess.check_output.assert_called_with(args, **kwargs) + args.insert(0, 'sudo') + Cell.all('interface', sudo=True) + subprocess.check_output.assert_called_with(args, **kwargs) + diff --git a/tests/test_schemes.py b/tests/test_schemes.py index 8e22d20..0a4d4f8 100644 --- a/tests/test_schemes.py +++ b/tests/test_schemes.py @@ -5,6 +5,8 @@ from wifi import Cell from wifi.scheme import extract_schemes, Scheme from wifi.exceptions import ConnectionError +from wifi import subprocess_compat as subprocess +from mock import patch NETWORK_INTERFACES_FILE = """ @@ -99,6 +101,18 @@ def test_failed_connection(self): scheme = Scheme('wlan0', 'test') self.assertRaises(ConnectionError, scheme.parse_ifup_output, FAILED_IFUP_OUTPUT) + def test_activate_is_called_with_good_args(self): + args = ['sudo', '/sbin/ifdown', 'wlan0'] + kwargs = {'stderr':subprocess.STDOUT} + scheme = Scheme('wlan0', 'test') + with patch.object(subprocess, 'check_output', + return_value=SUCCESSFUL_IFUP_OUTPUT): + scheme.activate(sudo=True) + subprocess.check_output.assert_any_call(args, **kwargs) + args = ['/sbin/ifdown', 'wlan0'] + scheme.activate() + subprocess.check_output.assert_any_call(args, **kwargs) + class TestForCell(TestCase): def test_unencrypted(self): @@ -218,3 +232,4 @@ def test_wpa(self): No DHCPOFFERS received. No working leases in persistent database - sleeping. """ + diff --git a/wifi/scan.py b/wifi/scan.py index e93ac4b..c9b6b39 100644 --- a/wifi/scan.py +++ b/wifi/scan.py @@ -20,12 +20,15 @@ def __repr__(self): return 'Cell(ssid={ssid})'.format(**vars(self)) @classmethod - def all(cls, interface): + def all(cls, interface, sudo=False): """ Returns a list of all cells extracted from the output of iwlist. """ + args = ['/sbin/iwlist', interface, 'scan'] + if sudo: + args.insert(0, 'sudo') try: - iwlist_scan = subprocess.check_output(['/sbin/iwlist', interface, 'scan'], + iwlist_scan = subprocess.check_output(args, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: raise InterfaceError(e.output.strip()) @@ -154,3 +157,4 @@ def normalize(cell_block): cell.encryption_type = 'wep' return cell + diff --git a/wifi/scheme.py b/wifi/scheme.py index 056dc05..234c410 100644 --- a/wifi/scheme.py +++ b/wifi/scheme.py @@ -164,13 +164,20 @@ def as_args(self): return [self.interface + '=' + self.iface] + args - def activate(self): + def activate(self, sudo=False): """ Connects to the network as configured in this scheme. """ - subprocess.check_output(['/sbin/ifdown', self.interface], stderr=subprocess.STDOUT) - ifup_output = subprocess.check_output(['/sbin/ifup'] + self.as_args(), stderr=subprocess.STDOUT) + args_ifdown = ['/sbin/ifdown', self.interface] + args_ifup = ['/sbin/ifup'] + if sudo: + args_ifdown.insert(0, 'sudo') + args_ifup.insert(0, 'sudo') + subprocess.check_output(args_ifdown, stderr=subprocess.STDOUT) + ifup_output = subprocess.check_output(args_ifup + + self.as_args(), + stderr=subprocess.STDOUT) ifup_output = ifup_output.decode('utf-8') return self.parse_ifup_output(ifup_output)