From 23a37100f121edd0c1291c4a78901662eae5d58b Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Wed, 5 Jun 2024 13:35:20 -0700 Subject: Integration tests and "\r\n" DOS-style line feeds - Parse \r\n line feeds - Add simple integration tests to ensure regressions do not occur - Add test "wg1" for \r\n parsing --- test/integration_test.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 test/integration_test.py (limited to 'test/integration_test.py') diff --git a/test/integration_test.py b/test/integration_test.py new file mode 100644 index 0000000..2616986 --- /dev/null +++ b/test/integration_test.py @@ -0,0 +1,61 @@ +from pathlib import Path +from enum import Enum +import subprocess +import sys + +WG2ND_EXECUTABLE = "./wg2nd" +TEST_DIRECTORY = './test/example_config' +TESTS = [ + 'wg0', + 'wg1', # same as wg0 except with \r\n +] + +def die(*args, code: int = 1, **kwargs): + print(*args, **kwargs, file=sys.stderr) + sys.exit(code) + +class Wg2ndFileType( Enum ): + NETWORK = 'network' + NETDEV = 'netdev' + KEYFILE = 'keyfile' + NFT = 'nft' + +def wg2nd_generate(filetype: Wg2ndFileType, path: Path): + + try: + result = subprocess.run([ + WG2ND_EXECUTABLE, 'generate', '-t', filetype.value, str(path) + ], capture_output=True, check=True) + except FileNotFoundError: + die(f'Failed to find executable "{WG2ND_EXECUTABLE}"') + except subprocess.CalledProcessError as e: + die(f'Failed to generate config with wg2nd: {e}') + + return str(result.stdout, encoding='utf-8') + +def read_config(path: str) -> str: + + with open(path, 'r') as f: + return f.read() + +for test in TESTS: + + test_directory = Path(TEST_DIRECTORY) / test + + wg_config = test_directory / f'{test}.conf' + + expected_netdev = read_config(test_directory / f'{test}.netdev') + expected_network = read_config(test_directory / f'{test}.network') + expected_nftables = read_config(test_directory / 'nftables.conf') + + network = wg2nd_generate(Wg2ndFileType.NETWORK, wg_config) + netdev = wg2nd_generate(Wg2ndFileType.NETDEV, wg_config) + nftables = wg2nd_generate(Wg2ndFileType.NFT, wg_config) + + print(f'testing {test}') + + assert network == expected_network + assert netdev == expected_netdev + assert nftables == expected_nftables + + print('pass') -- cgit v1.2.3