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
Example #2
0
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_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
Example #4
0
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
Example #5
0
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
Example #6
0
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
Example #7
0
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)
Example #8
0
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
Example #9
0
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)
Example #10
0
def test_peer_invalid_dns(dns, exception_message):
    address = '192.168.0.2'

    with pytest.raises(ValueError) as exc:
        peer = Peer(
            'test-peer',
            address=address,
            dns=dns,
        )

    assert exception_message in str(exc.value)
Example #11
0
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_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