def test_write_peer_config_no_params(): address = '192.168.0.1' peer = Peer( 'test-peer', address=address, ) with patch('builtins.open', mock_open()) as mo: peer.config().write() mo.assert_has_calls([ call('/etc/wireguard/wg0.conf', mode='w', encoding='utf-8'), ], any_order=True)
def test_peer_mtu(mtu): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, mtu=mtu, ) assert isinstance(peer.address, IPv4Address) assert str(peer.address) == address assert peer.port == PORT assert peer.interface == INTERFACE assert peer.private_key is not None assert peer.public_key is not None assert peer.public_key == public_key(peer.private_key) assert peer.mtu == mtu # Ensure nothing else got set assert not peer.peers assert not peer.dns assert not peer.table assert not peer.pre_up assert not peer.post_up assert not peer.pre_down assert not peer.post_down assert not peer.keepalive assert not peer.preshared_key config = peer.config() config_lines = config.local_config.split('\n') assert f'MTU = {mtu}' in config_lines
def test_write_peer_config(interface, path, full_path): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, interface=interface, ) config = Config(peer) assert config.full_path(path) == full_path with patch('builtins.open', mock_open()) as mo: peer.config().write(path) mo.assert_has_calls([ call(full_path, mode='w', encoding='utf-8'), ], any_order=True)
def test_basic_peer(): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, ) assert isinstance(peer.address, IPv4Address) assert str(peer.address) == address assert peer.port == PORT assert peer.interface == INTERFACE assert peer.private_key is not None assert peer.public_key is not None assert peer.public_key == public_key(peer.private_key) assert not peer.peers assert not peer.dns assert not peer.mtu assert not peer.table assert not peer.pre_up assert not peer.post_up assert not peer.pre_down assert not peer.post_down assert not peer.keepalive assert not peer.preshared_key config = peer.config() assert isinstance(config, Config) wg_config = config.local_config config_lines = wg_config.split('\n') # Ensure that [Interface] is first in the config, allowing for blank lines before for line in config_lines: if line: assert line == '[Interface]' break assert f'Address = {address}/32' in config_lines assert '# test-peer' not in config_lines # Should only be present in Peer section on remote assert '[Peer]' not in config_lines # We haven't configured any peers, so this shouldn't exist # None of these have been set for this peer. They should not be in the config file assert 'DNS =' not in wg_config assert 'MTU =' not in wg_config assert 'Table =' not in wg_config assert 'PreUp =' not in wg_config assert 'PostUp =' not in wg_config assert 'PreDown =' not in wg_config assert 'PostDown =' not in wg_config assert 'PresharedKey =' not in wg_config assert 'PersistentKeepalive =' not in wg_config
def test_peer_qrcode_not_present(): try: import qrcode pytest.skip('QRCode is available') except ImportError: pass address = '192.168.0.1' peer = Peer( 'test-peer', address=address, ) # If qrcode is not present in the venv, test it fails appropriately. with pytest.raises(AttributeError) as exc: peer.config().qrcode assert 'add the qrcode' in str(exc.value)
def test_peer_qrcode(): # If qrcode is present in the venv, test it works. pytest.importorskip('qrcode', reason='QRCode is NOT available') address = '192.168.0.1' peer = Peer( 'test-peer', address=address, ) assert peer.config().qrcode
def test_peer_dns(): address = '192.168.0.2' dns = '1.1.1.1' peer = Peer( 'test-peer', address=address, dns=ip_address(dns), ) assert isinstance(peer.address, IPv4Address) assert str(peer.address) == address assert peer.port == PORT assert peer.interface == INTERFACE assert peer.private_key is not None assert peer.public_key is not None assert peer.public_key == public_key(peer.private_key) assert peer.dns is not None dns_found = False for entry in peer.dns: if str(entry) == dns: dns_found = True break assert dns_found # Ensure nothing else got set assert not peer.peers assert not peer.mtu assert not peer.table assert not peer.pre_up assert not peer.post_up assert not peer.pre_down assert not peer.post_down assert not peer.keepalive assert not peer.preshared_key config = peer.config() config_lines = config.local_config.split('\n') assert f'DNS = {dns}' in config_lines