def test_basic_peer(): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, ) config = Config(peer) 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 # 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
def test_multiple_dns(dns, dummy): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, dns=dns, ) config = Config(peer) wg_config = config.local_config config_lines = wg_config.split('\n') # Because the set of DNS entries could return in any order, check that at least one is present assert ('DNS = 1.1.1.1,2.2.2.2,3.3.3.3' in config_lines or 'DNS = 1.1.1.1,3.3.3.3,2.2.2.2' in config_lines or 'DNS = 2.2.2.2,1.1.1.1,3.3.3.3' in config_lines or 'DNS = 2.2.2.2,3.3.3.3,1.1.1.1' in config_lines or 'DNS = 3.3.3.3,1.1.1.1,2.2.2.2' in config_lines or 'DNS = 3.3.3.3,2.2.2.2,1.1.1.1' in config_lines) # Check that these don't appear anywhere at all because of how basic this config is for option in [ 'PreUp', 'PostUp', 'PreDown', 'PostDown', 'SaveConfig', 'MTU', 'Table', 'AllowedIPs', 'Endpoint', 'PersistentKeepalive', 'PresharedKey', 'PublicKey' ]: assert f'{option} =' not in wg_config peer.dns = None assert config.dns is None
def test_table(): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, table='off', ) config = Config(peer) wg_config = config.local_config config_lines = wg_config.split('\n') assert 'Table = off' in config_lines # 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', 'AllowedIPs', 'Endpoint', 'PersistentKeepalive', 'PresharedKey', 'PublicKey' ]: assert f'{option} =' not in wg_config peer.table = None assert config.table is None
def test_inadmissible_non_peer(): class NonPeer(): attrib1 = IPAddressSet() attrib2 = 'something' with pytest.raises(ValueError) as exc: config = Config(NonPeer()) assert 'provide a valid Peer' in str(exc.value)
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_admissible_non_peer(): class NonPeer(): allowed_ips = IPAddressSet() public_key = 'something' config = Config(NonPeer()) for line in config.local_config.split('\n'): if line: assert line == '[Interface]' assert '[Peer]' in config.remote_config assert 'PublicKey = something' in config.remote_config
def test_description(): address = '192.168.0.2' peer = Peer( 'test-peer', address=address, ) config = Config(peer) wg_config = config.local_config assert config.description == '# test-peer' # 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 peer.description = None assert config.description is None