Unix socket connect test.

This commit is contained in:
Roger A. Light
2022-01-01 22:03:17 +00:00
parent aa7e09948b
commit 4cd2f85e77
4 changed files with 72 additions and 0 deletions
+58
View File
@@ -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)
+1
View File
@@ -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
+1
View File
@@ -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'),
+12
View File
@@ -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])