Example #1
0
    def test_get_interface_2_ap_monitor_is_managed_by_nm_1_ap_mon_interface(
            self):
        """
        Test get_interface method get the correct interface when 1
        card is managed and the other card is unmanaged by NetworkManager
        """

        interface_name_0 = "wlan0"
        interface_name_1 = "wlan1"
        interface_object = "Card Object"
        adapter_0 = interfaces.NetworkAdapter(interface_name_0,
                                              interface_object,
                                              self.mac_address)
        adapter_1 = interfaces.NetworkAdapter(interface_name_1,
                                              interface_object,
                                              self.mac_address)
        self.network_manager._name_to_object[interface_name_0] = adapter_0
        self.network_manager._name_to_object[interface_name_1] = adapter_1
        self.network_manager.internet_access_enable = True
        adapter_0.has_monitor_mode = True
        adapter_1.has_monitor_mode = True
        adapter_0.has_ap_mode = True
        adapter_1.has_ap_mode = True
        adapter_0.is_managed_by_nm = True
        adapter_1.is_managed_by_nm = False

        expected = interface_name_1
        actual = self.network_manager.get_interface(True, True)
        self.assertEqual(expected, actual)
Example #2
0
    def test_get_interface_2_ap_monitor_is_managed_by_nm_error(self):
        """
        Tests get_interface method when 2 interfaces with both AP and
        monitor mode are given as input but the adapters are both managed
        by NetworkManager
        """

        interface_name_0 = "wlan0"
        interface_name_1 = "wlan1"
        interface_object = "Card Object"
        adapter_0 = interfaces.NetworkAdapter(interface_name_0,
                                              interface_object,
                                              self.mac_address)
        adapter_1 = interfaces.NetworkAdapter(interface_name_1,
                                              interface_object,
                                              self.mac_address)
        self.network_manager._name_to_object[interface_name_0] = adapter_0
        self.network_manager._name_to_object[interface_name_1] = adapter_1
        self.network_manager.internet_access_enable = True
        adapter_0.has_monitor_mode = True
        adapter_1.has_monitor_mode = True
        adapter_0.has_ap_mode = True
        adapter_1.has_ap_mode = True
        adapter_0.is_managed_by_nm = True
        adapter_1.is_managed_by_nm = True

        self.assertRaises(interfaces.InterfaceManagedByNetworkManagerError,
                          self.network_manager.get_interface, True, True)
Example #3
0
    def test_get_interface_automatically_1_ap_1_mon_ap_interfaces(self):
        """
        Tests get_interface_automatically method when 1 AP and 1
        monitor and AP interface are given as inputs
        """

        interface_name_0 = "wlan0"
        interface_name_1 = "wlan1"
        interface_object = "Card Object"
        adapter_0 = interfaces.NetworkAdapter(interface_name_0,
                                              interface_object,
                                              self.mac_address)
        adapter_1 = interfaces.NetworkAdapter(interface_name_1,
                                              interface_object,
                                              self.mac_address)
        adapter_0.has_monitor_mode = True
        adapter_1.has_ap_mode = True
        adapter_1.has_monitor_mode = True
        self.network_manager._name_to_object[interface_name_0] = adapter_0
        self.network_manager._name_to_object[interface_name_1] = adapter_1

        expected = (interface_name_0, interface_name_1)
        actual = self.network_manager.get_interface_automatically()

        self.assertEqual(expected, actual)
Example #4
0
    def setUp(self):
        """ Set up the tests """

        self.adapter_name = "wlan0"
        self.card = "CARD"
        self.mac_address = "00:00:00:00:00:00"
        self.adapter = interfaces.NetworkAdapter(self.adapter_name, self.card, self.mac_address)
Example #5
0
    def setUp(self):
        """ Set up the tests """

        # setup fake card
        card = "Card"
        mac_address = "00:00:00:00:00:00"
        self.adapter = interfaces.NetworkAdapter("wlan0", card, mac_address)
Example #6
0
    def test_get_interface_automatically_2_ap_error(self):
        """
        Tests get_interface_automatically method when two interfaces
        are available but only support AP mode
        """

        interface_name_0 = "wlan0"
        interface_name_1 = "wlan1"
        interface_object = "Card Object"
        adapter_0 = interfaces.NetworkAdapter(interface_name_0, interface_object, self.mac_address)
        adapter_1 = interfaces.NetworkAdapter(interface_name_0, interface_object, self.mac_address)
        adapter_0.has_ap_mode = True
        adapter_1.has_ap_mode = True
        self.network_manager._name_to_object[interface_name_0] = adapter_0
        self.network_manager._name_to_object[interface_name_1] = adapter_1

        with self.assertRaises(interfaces.InterfaceCantBeFoundError):
            self.network_manager.get_interface_automatically()
    def test_get_interface_1_mon_interface(self):
        """
        Tests get_interface method when one interface supports AP
        and monitor and the other supports only Monitor
        """

        interface_name_0 = "wlan0"
        interface_name_1 = "wlan1"
        interface_object = "Card Object"
        adapter_0 = interfaces.NetworkAdapter(interface_name_0, interface_object, self.mac_address)
        adapter_1 = interfaces.NetworkAdapter(interface_name_1, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name_0] = adapter_0
        self.network_manager._name_to_object[interface_name_1] = adapter_1
        adapter_0.has_monitor_mode = True
        adapter_0.has_ap_mode = True
        adapter_1.has_monitor_mode = True

        expected = interface_name_1
        actual = self.network_manager.get_interface(False, True)
        self.assertEqual(expected, actual)
Example #8
0
    def test_get_interface_mac_address(self):
        """
        Test get_interface_mac under normal conditions
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        self.assertEqual(self.network_manager.get_interface_mac(interface_name), self.mac_address)
    def test_is_interface_valid_valid_true(self):
        """ Tests is_interface_valid method when interface is valid """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)

        self.network_manager._name_to_object[interface_name] = adapter

        actual = self.network_manager.is_interface_valid(interface_name)

        message = "Failed to validate a valid interface"
        self.assertTrue(actual, message)
Example #10
0
    def test_is_interface_wired_is_wireless_error(self):
        """
        Tests is_interface_wired when interface is wireless
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        adapter.is_wireless = True
        self.network_manager._name_to_object[interface_name] = adapter

        with self.assertRaises(interfaces.InvalidInternetInterfaceError):
            self.network_manager.is_interface_wired(interface_name)
Example #11
0
    def test_is_interface_valid_mode_monitor_is_managed_by_nm_error(self):
        """
        Tests is_interface_valid when the adapter is required as monitor but
        is managed by NetworkManager
        """

        interface_name = "wlan0"
        adapter = interfaces.NetworkAdapter(interface_name, "CARD", "00:00:00:00:00:00")
        adapter.is_managed_by_nm = True
        adapter.has_monitor_mode = True
        self.network_manager._name_to_object[interface_name] = adapter
        with self.assertRaises(interfaces.InterfaceManagedByNetworkManagerError):
            self.network_manager.is_interface_valid(interface_name, "monitor")
Example #12
0
    def test_get_interface_active_interface_error(self):
        """
        Tests get_interface method when no interface can be found
        because interface is active
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        with self.assertRaises(interfaces.InterfaceCantBeFoundError):
            self.network_manager.get_interface(has_monitor_mode=True)
Example #13
0
    def test_is_interface_valid_no_monitor_error(self):
        """
        Tests is_interface_valid method when interface has no monitor
        mode support and it is required
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        adapter.has_monitor_mode = False
        self.network_manager._name_to_object[interface_name] = adapter

        with self.assertRaises(interfaces.InvalidInterfaceError):
            self.network_manager.is_interface_valid(interface_name, "monitor")
Example #14
0
    def test_is_interface_valid_mode_internet_is_managed_by_nm_true(self):
        """
        Tests is_interface_valid when the adapter is internet mode
        """

        interface_name = "wlan0"
        self.network_manager = interfaces.NetworkManager()
        adapter = interfaces.NetworkAdapter(interface_name, "CARD", "00:00:00:00:00:00")
        adapter.is_managed_by_nm = True
        self.network_manager._name_to_object[interface_name] = adapter

        actual = self.network_manager.is_interface_valid(interface_name, "internet")
        message = "Failed to validate an interface with internet mode"
        self.assertTrue(actual, message)
Example #15
0
    def test_set_interface_channel_normal_none(self, pyric):
        """
        Tests set_interface_channel method when setting a channel
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        channel = 4
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter

        self.network_manager.set_interface_channel(interface_name, channel)

        pyric.chset.assert_called_once_with(interface_object, channel)
Example #16
0
    def test_is_interface_wired_is_wired_true(self):
        """
        Tests is_interface_wired when interface is wired
        """

        interface_name = "lan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        adapter.is_wireless = False
        self.network_manager._name_to_object[interface_name] = adapter

        actual = self.network_manager.is_interface_wired(interface_name)
        message = "Failed to identify interface as wired when interface was wired"
        self.assertTrue(actual, message)
Example #17
0
    def test_set_interface_mode_interface_none(self, pyric):
        """ Test set_interface_mode method under normal conditions """

        interface_name = "wlan0"
        interface_object = "Card Object"
        mode = "monitor"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter

        self.network_manager.set_interface_mode(interface_name, mode)

        pyric.down.assert_called_once_with(interface_object)
        pyric.modeset.assert_called_once_with(interface_object, mode)
        pyric.up.assert_called_once_with(interface_object)
Example #18
0
    def test_is_interface_valid_invalid_interface_error(self):
        """
        Test is_interface_valid method when interface is already been chosen
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object,
                                            self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        # mimic the card has been chosen
        self.network_manager._active.add(interface_name)

        with self.assertRaises(interfaces.InvalidInterfaceError):
            self.network_manager.is_interface_valid(interface_name)
Example #19
0
    def test_get_interface_no_ap_available_error(self):
        """
        Tests get_interface method when interface with specified mode
        can't be found
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        adapter.has_ap_mode = False
        adapter.has_monitor_mode = False
        self.network_manager._name_to_object[interface_name] = adapter

        with self.assertRaises(interfaces.InterfaceCantBeFoundError):
            self.network_manager.get_interface(True)
Example #20
0
    def test_is_interface_valid_has_monitor_true(self):
        """
        Tests is_interface_valid method when interface has monitor
        mode support and it is required
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        adapter.has_monitor_mode = True
        self.network_manager._name_to_object[interface_name] = adapter

        actual = self.network_manager.is_interface_valid(interface_name, "monitor")
        message = "Failed to validate an interface with monitor mode available and requested"
        self.assertTrue(actual, message)
Example #21
0
    def test_is_interface_valid_mode_ap_is_managed_by_nm_true(self):
        """
        Tests is_interface_valid when the adapter is required as monitor and is not managed by
        NetworkManager
        """

        interface_name = "wlan0"
        adapter = interfaces.NetworkAdapter(interface_name, "CARD", "00:00:00:00:00:00")
        adapter.is_managed_by_nm = False
        adapter.has_ap_mode = True
        self.network_manager._name_to_object[interface_name] = adapter
        actual = self.network_manager.is_interface_valid(interface_name, "AP")

        message = "Failed to validate an interface with AP mode"
        self.assertTrue(actual, message)
Example #22
0
    def test_on_exit_has_active_none(self, pyric):
        """
        Tests on_exit method when there are active interfaces
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        mode = "managed"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        self.network_manager.on_exit()

        pyric.modeset.assert_called_once_with(interface_object, mode)
Example #23
0
    def test_unblock_interface_not_blocked_none(self, pyric):
        """
        Tests unblock_interface when the interface is blocked
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter

        pyric.isblocked.return_value = False

        self.network_manager.unblock_interface(interface_name)

        pyric.unblock.assert_not_called()
Example #24
0
    def test_is_interface_valid_mode_ap_is_managed_by_nm_error(self):
        """
        Tests is_interface_valid when the adapter is required as AP but
        is managed by NetworkManager
        """

        interface_name = "wlan0"
        adapter = interfaces.NetworkAdapter(interface_name, "CARD",
                                            "00:00:00:00:00:00")
        adapter.is_managed_by_nm = True
        adapter.has_ap_mode = True
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager.internet_access_enable = True
        self.assertRaises(interfaces.InterfaceManagedByNetworkManagerError,
                          self.network_manager.is_interface_valid,
                          interface_name, "AP")
Example #25
0
    def test_set_interface_mac_valid_mac_none(self, pyw):
        """
        Test set_interface_mac with an valid MAC address to simulate
        normal operation
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        mac_address = "11:22:33:44:55:66"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        operation = self.network_manager.set_interface_mac(interface_name, mac_address)
        message = "Failed when a valid mac address was provided"
        self.assertIsNone(operation, message)
Example #26
0
    def test_set_interface_mac_invalid_mac_error(self, pyw):
        """
        Test set_interface_mac with an invalid MAC address to raise an
        error
        """

        pyw.macset.side_effect = pyric.error(22, "Invalid mac address")

        interface_name = "wlan0"
        interface_object = "Card Object"
        mac_address = "1"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        with self.assertRaises(interfaces.InvalidMacAddressError):
            self.network_manager.set_interface_mac(interface_name, mac_address)
Example #27
0
    def test_get_interface_1_ap_monitor_interface(self):
        """
        Tests get_interface method when interface with both AP and
        monitor mode are given as input
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        adapter.has_ap_mode = True
        adapter.has_monitor_mode = True
        self.network_manager._name_to_object[interface_name] = adapter

        expected = interface_name
        actual = self.network_manager.get_interface(True, True)

        self.assertEqual(expected, actual)
Example #28
0
    def test_set_interface_mac_random_none(self, pyw):
        """
        Test set_interface_mac_random under normal conditions
        """

        new_mac_address = "00:11:22:33:44:55"

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        with mock.patch("wifiphisher.common.interfaces.generate_random_address") as generator:
            generator.return_value = new_mac_address
            self.network_manager.set_interface_mac_random(interface_name)

            pyw.macset.assert_called_once_with(interface_object, new_mac_address)
Example #29
0
    def test_set_interface_unexpected_error(self, pyw):
        """
        Test set_interface_mac when an unexpected error occurs
        """

        pyw.macset.side_effect = pyric.error(5534, "Unexpected error")

        interface_name = "wlan0"
        interface_object = "Card Object"
        mac_address = "11:22:33:44:55:66"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object, self.mac_address)
        self.network_manager._name_to_object[interface_name] = adapter
        self.network_manager._active.add(interface_name)

        with self.assertRaises(pyric.error) as error:
            self.network_manager.set_interface_mac(interface_name, mac_address)

        self.assertEqual(error.exception[0], 5534, "Unexpected error")
Example #30
0
    def test_get_interface_1_ap_monitor_is_managed_by_nm_error(self):
        """
        Tests get_interface method when interface with both AP and
        monitor mode are given as input but the adapter is managed
        by NetworkManager
        """

        interface_name = "wlan0"
        interface_object = "Card Object"
        adapter = interfaces.NetworkAdapter(interface_name, interface_object,
                                            self.mac_address)
        adapter.has_ap_mode = True
        adapter.has_monitor_mode = True
        adapter.is_managed_by_nm = True
        self.network_manager._name_to_object[interface_name] = adapter

        self.assertRaises(interfaces.InterfaceManagedByNetworkManagerError,
                          self.network_manager.get_interface, True, True)