diff --git a/test/broker/01-connect-unix-socket.py b/test/broker/01-connect-unix-socket.py new file mode 100755 index 00000000..1517f33a --- /dev/null +++ b/test/broker/01-connect-unix-socket.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +# Test whether connections to a unix socket work + +from mosq_test_helper import * + +def start_broker(filename): + cmd = ['../../src/mosquitto', '-v', '-c', filename] + + if os.environ.get('MOSQ_USE_VALGRIND') is not None: + logfile = filename+'.'+str(vg_index)+'.vglog' + if os.environ.get('MOSQ_USE_VALGRIND') == 'callgrind': + cmd = ['valgrind', '-q', '--tool=callgrind', '--log-file='+logfile] + cmd + elif os.environ.get('MOSQ_USE_VALGRIND') == 'massif': + cmd = ['valgrind', '-q', '--tool=massif', '--log-file='+logfile] + cmd + else: + cmd = ['valgrind', '-q', '--trace-children=yes', '--leak-check=full', '--show-leak-kinds=all', '--log-file='+logfile] + cmd + + return subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) + + +def write_config(filename, port): + with open(filename, 'w') as f: + f.write("listener 0 %d.sock\n" % (port)) + f.write("allow_anonymous true\n") + +def do_test(): + rc = 1 + + connect_packet = mosq_test.gen_connect("unix-socket") + connack_packet = mosq_test.gen_connack(rc=0) + + port = mosq_test.get_port() + conf_file = os.path.basename(__file__).replace('.py', '.conf') + write_config(conf_file, port) + broker = start_broker(filename=os.path.basename(__file__)) + + try: + sock = mosq_test.do_client_connect_unix(connect_packet, connack_packet, path=f"{port}.sock") + sock.close() + + rc = 0 + except mosq_test.TestError: + pass + except Exception as err: + print(err) + finally: + broker.terminate() + broker.wait() + os.remove(conf_file) + os.remove(f"{port}.sock") + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + exit(rc) + +do_test() +exit(0) diff --git a/test/broker/Makefile b/test/broker/Makefile index 2cb450c8..4de6e6e5 100644 --- a/test/broker/Makefile +++ b/test/broker/Makefile @@ -36,6 +36,7 @@ msg_sequence_test: ./01-connect-uname-or-anon.py ./01-connect-uname-password-denied-no-will.py ./01-connect-uname-password-denied.py + ./01-connect-unix-socket.py ./01-connect-windows-line-endings.py ./01-connect-zero-length-id.py diff --git a/test/broker/test.py b/test/broker/test.py index b4479ab6..1323c912 100755 --- a/test/broker/test.py +++ b/test/broker/test.py @@ -18,6 +18,7 @@ tests = [ (1, './01-connect-uname-or-anon.py'), (1, './01-connect-uname-password-denied-no-will.py'), (1, './01-connect-uname-password-denied.py'), + (1, './01-connect-unix-socket.py'), (1, './01-connect-windows-line-endings.py'), (2, './01-connect-zero-length-id.py'), diff --git a/test/mosq_test.py b/test/mosq_test.py index f79c180b..e0734cb5 100644 --- a/test/mosq_test.py +++ b/test/mosq_test.py @@ -181,12 +181,24 @@ def client_connect_only(hostname="localhost", port=1888, timeout=10): sock.connect((hostname, port)) return sock +def client_connect_only_unix(path, timeout=10): + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.settimeout(timeout) + sock.connect(path) + return sock + def do_client_connect(connect_packet, connack_packet, hostname="localhost", port=1888, timeout=10, connack_error="connack"): sock = client_connect_only(hostname, port, timeout) return do_send_receive(sock, connect_packet, connack_packet, connack_error) +def do_client_connect_unix(connect_packet, connack_packet, path, timeout=10, connack_error="connack"): + sock = client_connect_only_unix(path, timeout) + + return do_send_receive(sock, connect_packet, connack_packet, connack_error) + + def remaining_length(packet): l = min(5, len(packet)) all_bytes = struct.unpack("!"+"B"*l, packet[:l])