diff options
author | Andrei Zeliankou <zelenkov@nginx.com> | 2022-08-25 15:50:49 +0100 |
---|---|---|
committer | Andrei Zeliankou <zelenkov@nginx.com> | 2022-08-25 15:50:49 +0100 |
commit | d1cb8ab2bb27a864ec6cb21c5e64af315266100d (patch) | |
tree | 455d15d3fabde9f027081ec487d834a04add3d73 | |
parent | 7e4a8a54221adf00cd3eb45a24b633ce61400570 (diff) | |
download | unit-d1cb8ab2bb27a864ec6cb21c5e64af315266100d.tar.gz unit-d1cb8ab2bb27a864ec6cb21c5e64af315266100d.tar.bz2 |
Tests: added tests with abstract UNIX sockets.
-rw-r--r-- | test/conftest.py | 2 | ||||
-rw-r--r-- | test/test_asgi_application_unix_abstract.py | 23 | ||||
-rw-r--r-- | test/test_configuration.py | 16 | ||||
-rw-r--r-- | test/test_unix_abstract.py | 109 | ||||
-rw-r--r-- | test/unit/check/unix_abstract.py | 25 | ||||
-rw-r--r-- | test/unit/http.py | 2 |
6 files changed, 176 insertions, 1 deletions
diff --git a/test/conftest.py b/test/conftest.py index 904abc32..ed19838c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -20,6 +20,7 @@ from unit.check.isolation import check_isolation from unit.check.node import check_node from unit.check.regex import check_regex from unit.check.tls import check_openssl +from unit.check.unix_abstract import check_unix_abstract from unit.http import TestHTTP from unit.log import Log from unit.option import option @@ -213,6 +214,7 @@ def pytest_sessionstart(session): check_chroot() check_isolation() + check_unix_abstract() _clear_conf(unit['temp_dir'] + '/control.unit.sock') diff --git a/test/test_asgi_application_unix_abstract.py b/test/test_asgi_application_unix_abstract.py new file mode 100644 index 00000000..c4ec812f --- /dev/null +++ b/test/test_asgi_application_unix_abstract.py @@ -0,0 +1,23 @@ +from packaging import version +from unit.applications.lang.python import TestApplicationPython + + +class TestASGIApplicationUnixAbstract(TestApplicationPython): + prerequisites = { + 'modules': { + 'python': lambda v: version.parse(v) >= version.parse('3.5') + }, + 'features': ['unix_abstract'], + } + load_module = 'asgi' + + def test_asgi_application_unix_abstract(self): + self.load('empty') + + addr = '\0sock' + assert 'success' in self.conf( + {"unix:@" + addr[1:]: {"pass": "applications/empty"}}, + 'listeners', + ) + + assert self.get(sock_type='unix', addr=addr)['status'] == 200 diff --git a/test/test_configuration.py b/test/test_configuration.py index 4a9d9840..75fbb845 100644 --- a/test/test_configuration.py +++ b/test/test_configuration.py @@ -2,6 +2,7 @@ import socket import pytest from unit.control import TestControl +from unit.option import option class TestConfiguration(TestControl): @@ -226,6 +227,21 @@ class TestConfiguration(TestControl): {"*:7080": {"pass": "applications/app"}}, 'listeners' ), 'listeners no app' + def test_listeners_unix_abstract(self): + if option.system != 'Linux': + assert 'error' in self.conf( + {"unix:@sock": {"pass": "routes"}}, 'listeners' + ), 'unix abstract at' + + pytest.skip('not yet') + + assert 'error' in self.conf( + {"unix:\0sock": {"pass": "routes"}}, 'listeners' + ), 'unix abstract zero' + assert 'error' in self.conf( + {"unix:\u0000sock": {"pass": "routes"}}, 'listeners' + ), 'unix abstract zero unicode' + def test_listeners_addr(self): assert 'success' in self.try_addr("*:7080"), 'wildcard' assert 'success' in self.try_addr("127.0.0.1:7081"), 'explicit' diff --git a/test/test_unix_abstract.py b/test/test_unix_abstract.py new file mode 100644 index 00000000..195b0aa7 --- /dev/null +++ b/test/test_unix_abstract.py @@ -0,0 +1,109 @@ +from unit.applications.lang.python import TestApplicationPython +from unit.option import option + + +class TestUnixAbstract(TestApplicationPython): + prerequisites = { + 'modules': {'python': 'any'}, + 'features': ['unix_abstract'], + } + + def test_unix_abstract_source(self): + addr = '\0sock' + + def source(source): + assert 'success' in self.conf( + '"' + source + '"', 'routes/0/match/source' + ) + + assert 'success' in self.conf( + { + "listeners": { + "127.0.0.1:7080": {"pass": "routes"}, + "unix:@" + addr[1:]: {"pass": "routes"}, + }, + "routes": [ + { + "match": {"source": "!0.0.0.0/0"}, + "action": {"return": 200}, + } + ], + "applications": {}, + } + ) + + assert ( + self.get(sock_type='unix', addr=addr)['status'] == 200 + ), 'neg ipv4' + + source("!::/0") + assert ( + self.get(sock_type='unix', addr=addr)['status'] == 200 + ), 'neg ipv6' + + source("unix") + assert self.get()['status'] == 404, 'ipv4' + assert self.get(sock_type='unix', addr=addr)['status'] == 200, 'unix' + + def test_unix_abstract_client_ip(self): + def get_xff(xff, sock_type='ipv4'): + address = { + 'ipv4': ('127.0.0.1', 7080), + 'ipv6': ('::1', 7081), + 'unix': ('\0sock', None), + } + (addr, port) = address[sock_type] + + return self.get( + sock_type=sock_type, + addr=addr, + port=port, + headers={'Connection': 'close', 'X-Forwarded-For': xff}, + )['body'] + + assert 'success' in self.conf( + { + "listeners": { + "127.0.0.1:7080": { + "client_ip": { + "header": "X-Forwarded-For", + "source": "unix", + }, + "pass": "applications/client_ip", + }, + "[::1]:7081": { + "client_ip": { + "header": "X-Forwarded-For", + "source": "unix", + }, + "pass": "applications/client_ip", + }, + "unix:@sock": { + "client_ip": { + "header": "X-Forwarded-For", + "source": "unix", + }, + "pass": "applications/client_ip", + }, + }, + "applications": { + "client_ip": { + "type": self.get_application_type(), + "processes": {"spare": 0}, + "path": option.test_dir + "/python/client_ip", + "working_directory": option.test_dir + + "/python/client_ip", + "module": "wsgi", + } + }, + } + ) + + assert get_xff('1.1.1.1') == '127.0.0.1', 'bad source ipv4' + assert get_xff('1.1.1.1', 'ipv6') == '::1', 'bad source ipv6' + + for ip in [ + '1.1.1.1', + '::11.22.33.44', + ]: + assert get_xff(ip, 'unix') == ip, 'replace' diff --git a/test/unit/check/unix_abstract.py b/test/unit/check/unix_abstract.py new file mode 100644 index 00000000..5d1f629e --- /dev/null +++ b/test/unit/check/unix_abstract.py @@ -0,0 +1,25 @@ +import json + +from unit.http import TestHTTP +from unit.option import option + +http = TestHTTP() + + +def check_unix_abstract(): + available = option.available + + resp = http.put( + url='/config', + sock_type='unix', + addr=option.temp_dir + '/control.unit.sock', + body=json.dumps( + { + "listeners": {"unix:@sock": {"pass": "routes"}}, + "routes": [], + } + ), + ) + + if 'success' in resp['body']: + available['features']['unix_abstract'] = True diff --git a/test/unit/http.py b/test/unit/http.py index 522d2ea6..b29667c9 100644 --- a/test/unit/http.py +++ b/test/unit/http.py @@ -51,7 +51,7 @@ class TestHTTP: connect_args = addr if sock_type == 'unix' else (addr, port) try: sock.connect(connect_args) - except ConnectionRefusedError: + except (ConnectionRefusedError, FileNotFoundError): sock.close() pytest.fail('Client can\'t connect to the server.') |