def test_find_by_public_key_fail(self): """Tests find_by_public_key, returns empty list if no matching peer found.""" cm = netlink.ConfigManager("some_wg_interface", "some_vx_interface") reference_peer = netlink.WireGuardPeer("WGPEER_A_PUBLIC_KEY") cm.all_peers.append(reference_peer) self.assertListEqual(cm.find_by_public_key("WGPEER_AN_UNKNOWN_PUBLIC_KEY"), [])
def test_find_by_public_key_duplicete(self): """Tests find_by_public_key, raises AssertionError if searched pubkey is not unique.""" cm = netlink.ConfigManager("some_wg_interface", "some_vx_interface") reference_peer = netlink.WireGuardPeer("WGPEER_A_PUBLIC_KEY") cm.all_peers.append(reference_peer) cm.all_peers.append(reference_peer) with self.assertRaises(AssertionError): cm.find_by_public_key("WGPEER_A_PUBLIC_KEY")
def test_find_by_public_key_success(self): """Tests find_by_public_key, returns the correct peer.""" reference_peer = netlink.WireGuardPeer("WGPEER_A_PUBLIC_KEY") self.cm.all_peers.append(reference_peer) self.assertListEqual(self.cm.find_by_public_key("WGPEER_A_PUBLIC_KEY"), [reference_peer])
def test_needs_config_to_install_neg(self, mock_wgp): mock_wgp.__get__ = mock.PropertyMock(return_value=True) established = netlink.WireGuardPeer("doesnotmatter", is_installed=False) self.assertTrue(established.needs_config, "Not installed yet established peer does need configuration.")
def test_is_established_stalled(self): stalled = netlink.WireGuardPeer("doesnotmatter", latest_handshake=datetime.now()-(timedelta(seconds=1)+netlink.TIMEOUT)) assert False is stalled.is_established self.assertFalse(stalled.is_established, "Wrongly marked a stalled peer as established.")
def test_is_established_recent(self): recent = netlink.WireGuardPeer("doesnotmatter", latest_handshake=datetime.now()) self.assertTrue(recent.is_established, "Wrongly marked a recent peer as not established.")
def test_lladdr(self): dummy = netlink.WireGuardPeer("E2GuBSnyrKKG2mPIFc8tkEymTOTJcNH1WvF6N9KoWgs=") self.assertEqual("fe80::213:18ff:fe6e:f314/128", dummy.lladdr, "Calculated lladdr is wrong.")
from datetime import timedelta from datetime import datetime # pyroute2 decides imports based on platform. WireGuard is specific to Linux only. Mock pyroute2.WireGuard so that # any testing platform can execute tests. import sys sys.modules["pyroute2"] = mock.MagicMock() sys.modules["pyroute2.WireGuard"] = mock.MagicMock() sys.modules["pyroute2.IPRoute"] = mock.MagicMock() from pyroute2 import WireGuard from pyroute2 import IPRoute import netlink _WG_CLIENT_ADD = netlink.WireGuardPeer( public_key="public_key", latest_handshake=datetime.now(), is_installed=False ) _WG_CLIENT_DEL = netlink.WireGuardPeer( public_key="public_key", latest_handshake=(datetime.now() - (netlink.TIMEOUT+timedelta(seconds=1))), is_installed=True ) _WG_PEER_STALE = mock.Mock() _WG_PEER_STALE.WGPEER_A_PUBLIC_KEY = {"value": b"WGPEER_A_PUBLIC_KEY_STALE"} _WG_PEER_STALE.WGPEER_A_LAST_HANDSHAKE_TIME = { "tv_sec": int((datetime.now() - (netlink.TIMEOUT+timedelta(seconds=1))).timestamp()) } _WG_PEER = mock.Mock() _WG_PEER.WGPEER_A_PUBLIC_KEY = {"value": b"WGPEER_A_PUBLIC_KEY"} _WG_PEER.WGPEER_A_LAST_HANDSHAKE_TIME = {