Archived
31 lines
1.5 KiB
Python
31 lines
1.5 KiB
Python
from pathlib import Path
|
|
import runpy
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def helper(monkeypatch):
|
|
pytest.importorskip('fcntl')
|
|
mod = runpy.run_path(str(Path(__file__).resolve().parents[1] / 'scripts/wgtools-helper'))
|
|
gl = mod['extended_snat_apply'].__globals__
|
|
config = '[Interface]\nAddress = 10.2.0.1/24\nPrivateKey = key\n\n[Peer]\nPublicKey = peer\n'
|
|
monkeypatch.setitem(gl, 'read_config_text', lambda iface: config)
|
|
monkeypatch.setitem(gl, 'parse_config', lambda text, iface: {'detected': {'vpn_network': '10.2.0.0/24', 'uplink_interface': 'eth0'}})
|
|
monkeypatch.setitem(gl, 'run', lambda cmd: (0, '[{"addr_info":[{"family":"inet","local":"192.0.2.10"}]}]', ''))
|
|
monkeypatch.setitem(gl, 'raw_config_apply', lambda iface, payload: {'ok': True, 'content': payload['content']})
|
|
return mod
|
|
|
|
|
|
def test_snat_uses_assigned_address_and_preserves_peers(helper):
|
|
result = helper['extended_snat_apply']('wg1', {'address': '192.0.2.10', 'uplink': 'eth0'})
|
|
assert 'iptables -t nat -I POSTROUTING 1 -s 10.2.0.0/24 -o eth0' in result['content']
|
|
assert '--to-source 192.0.2.10' in result['content']
|
|
assert result['content'].index('PostDown') < result['content'].index('[Peer]')
|
|
|
|
|
|
def test_snat_rejects_unassigned_and_invalid_address(helper):
|
|
for address, uplink in [('192.0.2.11', 'eth0'), ('192.0.2.10', 'eth0;id'), ('bad', 'eth0')]:
|
|
with pytest.raises(ValueError):
|
|
helper['extended_snat_apply']('wg1', {'address': address, 'uplink': uplink})
|