Sunil Mohan Adapa 4b24fda3f5
wireguard: Accept/use netmask with IP address for server connection
- Currently, the value is hard-coded as /24. Instead take this as input and use
that value.

Tests:

- Entering invalid IPv4 address results in 'Enter a valid IPv4 address' error
message during form submission.

- Entering invalid prefix such as /33 results in 'Enter a valid network prefix
or net mask.' error during form submission.

- Both /32 and /255.255.255.255 formats are accepted.

- The description text for the form field 'IP address' is as expected.

- Changing the value of default route and IP address + netmask reflects in the
status page. Correct values is shown in the edit server and server status page.

- Not providing a netmask results in /32 being assigned.

- Unit and functional tests for wireguard pass. There are some intermittent
failures with functional tests that are unrelated to the patch.

- Setting the /32 prefix results in correct routing table as shown by 'ip route
show table all'. No default routes are network routes are present. 'traceroute
1.1.1.1' shows route taken via regular network.

- Setting the /24 prefix results in correct routing table. No default routes are
present. However, for the /24 network a route is present with device wg1.
'traceroute 1.1.1.1' shows route taken via regular network.

- Enabling the default route results in correct routing table. Default route is
shown for device wg1 with high priority. 'traceroute 1.1.1.1' shows route taken
via WireGuard network.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2026-03-02 15:22:36 -05:00

95 lines
2.5 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Tests for wireguard module forms.
"""
import pytest
from django.core.exceptions import ValidationError
from plinth.modules.wireguard.forms import (validate_endpoint,
validate_ipv4_address_with_network,
validate_key)
@pytest.mark.parametrize('key', [
'gKQhVGla4UtdqeY1dQ21G5lqrnX5NFcSEAqzM5iSdl0=',
'uHWSYIjPnS9fYFhZ0mf22IkOMyrWXDlfpXs6ve4QGHk=',
])
def test_validate_key_valid_patterns(key):
"""Test that valid wireguard key patterns as accepted."""
validate_key(key)
@pytest.mark.parametrize(
'key',
[
# Invalid padding
'gKQhVGla4UtdqeY1dQ21G5lqrnX5NFcSEAqzM5iSdl0',
'invalid-base64',
'',
'aW52YWxpZC1sZW5ndGg=', # Incorrect length
])
def test_validate_key_invalid_patterns(key):
"""Test that invalid wireguard key patterns are rejected."""
with pytest.raises(ValidationError):
validate_key(key)
@pytest.mark.parametrize('endpoint', [
'[1::2]:1234',
'1.2.3.4:1234',
'example.com:1234',
])
def test_validate_endpoint_valid_patterns(endpoint):
"""Test that valid wireguard endpoint patterns are accepted."""
validate_endpoint(endpoint)
@pytest.mark.parametrize(
'endpoint',
[
'',
# Invalid port
'1.2.3.4',
'1.2.3.4:',
'1.2.3.4:0',
'1.2.3.4:65536',
'1.2.3.4:1234invalid',
'1.2.3.4:invalid',
# Invalid IPv6
'[]:1234',
'[:1234',
])
def test_validate_endpoint_invalid_patterns(endpoint):
"""Test that invalid wireguard endpoint patterns are rejected."""
with pytest.raises(ValidationError):
validate_endpoint(endpoint)
@pytest.mark.parametrize('value', [
'1.2.3.4',
'1.2.3.4/0',
'1.2.3.4/32',
'1.2.3.4/24',
'1.2.3.4/255.255.255.0',
'1.2.3.4/0.0.0.255',
])
def test_validate_ipv4_address_with_network_valid_patterns(value):
"""Test validating IPv4 address with network works for valid values."""
validate_ipv4_address_with_network(value)
@pytest.mark.parametrize('value', [
'::1',
'1.2.3.4/',
'invalid-ip/24',
'1.2.3.4/x',
'1.2.3.4/-1',
'1.2.3.4/33',
'1.2.3.4/9.8.7.6',
])
def test_validate_ipv4_address_with_network_invalid_patterns(value):
"""Test validating IPv4 address with network works for invalid values."""
with pytest.raises(ValidationError):
validate_ipv4_address_with_network(value)