Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# coding formatting with black
ccabb89dc428fb1a9339baa878c519affb6d2f72

# lint + format tests
63ff1109f2404eddd4b0b95cd66cf1d7fc23b9ab
236 changes: 160 additions & 76 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

import pytest
from unittest.mock import (
call,
mock_open,
patch,
)
import pytest # pylint: disable=import-error

from wireguard import (
Config,
Expand All @@ -16,170 +15,252 @@


def test_basic_server():
subnet = '192.168.0.0/24'
address = '192.168.0.1'
subnet = "192.168.0.0/24"
address = "192.168.0.1"

server = Server(
'test-server',
"test-server",
subnet,
address=address,
)

config = ServerConfig(server)
wg_config = config.local_config
config_lines = wg_config.split('\n')
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]'
assert line == "[Interface]"
break

# Check that these are on a line alone in the config output
assert f'Address = {address}/24' in config_lines
assert '# test-server' 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
assert f"Address = {address}/24" in config_lines
assert (
"# test-server" 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

# Check that these don't appear anywhere at all because of how basic this config is
for option in ['DNS', 'PreUp', 'PostUp', 'PreDown', 'PostDown', 'SaveConfig', 'MTU', 'Table', 'AllowedIPs', 'Endpoint', 'PersistentKeepalive', 'PresharedKey', 'PublicKey']:
assert f'{option} =' not in wg_config
for option in [
"DNS",
"PreUp",
"PostUp",
"PreDown",
"PostDown",
"SaveConfig",
"MTU",
"Table",
"AllowedIPs",
"Endpoint",
"PersistentKeepalive",
"PresharedKey",
"PublicKey",
]:
assert f"{option} =" not in wg_config


def test_basic_peer():
address = '192.168.0.2'
address = "192.168.0.2"

peer = Peer(
'test-peer',
"test-peer",
address=address,
)

config = Config(peer)
wg_config = config.local_config
config_lines = wg_config.split('\n')
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]'
assert line == "[Interface]"
break

assert f'Address = {address}/32' in config_lines
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
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

# Check that these don't appear anywhere at all because of how basic this config is
for option in ['DNS', 'PreUp', 'PostUp', 'PreDown', 'PostDown', 'SaveConfig', 'MTU', 'Table', 'AllowedIPs', 'Endpoint', 'PersistentKeepalive', 'PresharedKey', 'PublicKey']:
assert f'{option} =' not in wg_config
for option in [
"DNS",
"PreUp",
"PostUp",
"PreDown",
"PostDown",
"SaveConfig",
"MTU",
"Table",
"AllowedIPs",
"Endpoint",
"PersistentKeepalive",
"PresharedKey",
"PublicKey",
]:
assert f"{option} =" not in wg_config


def test_inadmissible_non_peer():
class NonPeer():
class NonPeer: # pylint: disable=too-few-public-methods
attrib1 = IPAddressSet()
attrib2 = 'something'
attrib2 = "something"

with pytest.raises(ValueError) as exc:
config = Config(NonPeer())
Config(NonPeer())

assert 'provide a valid Peer' in str(exc.value)
assert "provide a valid Peer" in str(exc.value)


def test_admissible_non_peer():
class NonPeer():
class NonPeer: # pylint: disable=too-few-public-methods
allowed_ips = IPAddressSet()
public_key = 'something'
public_key = "something"

config = Config(NonPeer())
for line in config.local_config.split('\n'):
for line in config.local_config.split("\n"):
if line:
assert line == '[Interface]'
assert line == "[Interface]"

assert '[Peer]' in config.remote_config
assert 'PublicKey = something' in config.remote_config
assert "[Peer]" in config.remote_config
assert "PublicKey = something" in config.remote_config


def test_write_server_config_no_params():

subnet = '192.168.0.0/24'
address = '192.168.0.1'
subnet = "192.168.0.0/24"
address = "192.168.0.1"

server = Server(
'test-server',
"test-server",
subnet,
address=address,
)

with patch('builtins.open', mock_open()) as mo:
with patch("builtins.open", mock_open()) as mo:
server.config.write()

mo.assert_has_calls([
call('/etc/wireguard/wg0.conf', mode='w', encoding='utf-8'),
call('/etc/wireguard/wg0-peers.conf', mode='w', encoding='utf-8'),
], any_order=True)
mo.assert_has_calls(
[
call("/etc/wireguard/wg0.conf", mode="w", encoding="utf-8"),
call("/etc/wireguard/wg0-peers.conf", mode="w", encoding="utf-8"),
],
any_order=True,
)


@pytest.mark.parametrize(
('interface', 'path', 'full_path', 'peers_full_path'),
("interface", "path", "full_path", "peers_full_path"),
[
(None, None, '/etc/wireguard/wg0.conf', '/etc/wireguard/wg0-peers.conf',), # Default options
('wg3', None, '/etc/wireguard/wg3.conf', '/etc/wireguard/wg3-peers.conf',),
(None, '/opt/my-wg-dir', '/opt/my-wg-dir/wg0.conf', '/opt/my-wg-dir/wg0-peers.conf',),
('wg1', '/opt/my-other-wg-dir', '/opt/my-other-wg-dir/wg1.conf', '/opt/my-other-wg-dir/wg1-peers.conf',),
])
(
None,
None,
"/etc/wireguard/wg0.conf",
"/etc/wireguard/wg0-peers.conf",
), # Default options
(
"wg3",
None,
"/etc/wireguard/wg3.conf",
"/etc/wireguard/wg3-peers.conf",
),
(
None,
"/opt/my-wg-dir",
"/opt/my-wg-dir/wg0.conf",
"/opt/my-wg-dir/wg0-peers.conf",
),
(
"wg1",
"/opt/my-other-wg-dir",
"/opt/my-other-wg-dir/wg1.conf",
"/opt/my-other-wg-dir/wg1-peers.conf",
),
],
)
def test_write_server_config(interface, path, full_path, peers_full_path):
subnet = '192.168.0.0/24'
address = '192.168.0.1'
subnet = "192.168.0.0/24"
address = "192.168.0.1"

server = Server(
'test-server',
subnet,
address=address,
interface=interface
)
server = Server("test-server", subnet, address=address, interface=interface)

config = server.config
assert config.full_path(path) == full_path
assert config.peers_full_path(path) == peers_full_path

with patch('builtins.open', mock_open()) as mo:
with patch("builtins.open", mock_open()) as mo:
config.write(path)

mo.assert_has_calls([
call(full_path, mode='w', encoding='utf-8'),
call(peers_full_path, mode='w', encoding='utf-8'),
], any_order=True)
mo.assert_has_calls(
[
call(full_path, mode="w", encoding="utf-8"),
call(peers_full_path, mode="w", encoding="utf-8"),
],
any_order=True,
)


def test_write_peer_config_no_params():

address = '192.168.0.1'
address = "192.168.0.1"

peer = Peer(
'test-peer',
"test-peer",
address=address,
)

with patch('builtins.open', mock_open()) as mo:
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)
mo.assert_has_calls(
[
call("/etc/wireguard/wg0.conf", mode="w", encoding="utf-8"),
],
any_order=True,
)


@pytest.mark.parametrize(
('interface', 'path', 'full_path',),
(
"interface",
"path",
"full_path",
),
[
(None, None, '/etc/wireguard/wg0.conf',), # Default options
('wg3', None, '/etc/wireguard/wg3.conf',),
(None, '/opt/my-wg-dir', '/opt/my-wg-dir/wg0.conf',),
('wg1', '/opt/my-other-wg-dir', '/opt/my-other-wg-dir/wg1.conf',),
])
(
None,
None,
"/etc/wireguard/wg0.conf",
), # Default options
(
"wg3",
None,
"/etc/wireguard/wg3.conf",
),
(
None,
"/opt/my-wg-dir",
"/opt/my-wg-dir/wg0.conf",
),
(
"wg1",
"/opt/my-other-wg-dir",
"/opt/my-other-wg-dir/wg1.conf",
),
],
)
def test_write_peer_config(interface, path, full_path):
address = '192.168.0.2'
address = "192.168.0.2"

peer = Peer(
'test-peer',
"test-peer",
address=address,
interface=interface,
)
Expand All @@ -188,9 +269,12 @@ def test_write_peer_config(interface, path, full_path):

assert config.full_path(path) == full_path

with patch('builtins.open', mock_open()) as mo:
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)
mo.assert_has_calls(
[
call(full_path, mode="w", encoding="utf-8"),
],
any_order=True,
)
Loading