def pattern_test(sub_topic, pub_topic): rc = 1 keepalive = 60 connect_packet = mosq_test.gen_connect("pattern-sub-test", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) publish_packet = mosq_test.gen_publish(pub_topic, qos=0, payload="message") publish_retained_packet = mosq_test.gen_publish(pub_topic, qos=0, retain=True, payload="message") mid = 312 subscribe_packet = mosq_test.gen_subscribe(mid, sub_topic, 0) suback_packet = mosq_test.gen_suback(mid, 0) mid = 234; unsubscribe_packet = mosq_test.gen_unsubscribe(mid, sub_topic) unsuback_packet = mosq_test.gen_unsuback(mid) port = mosq_test.get_port() broker = subprocess.Popen(['../../src/mosquitto', '-p', str(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: time.sleep(0.5) sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port) sock.send(subscribe_packet) if mosq_test.expect_packet(sock, "suback", suback_packet): pub = subprocess.Popen(['./03-pattern-matching-helper.py', pub_topic, str(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE) pub.wait() (stdo, stde) = pub.communicate() if mosq_test.expect_packet(sock, "publish", publish_packet): sock.send(unsubscribe_packet) if mosq_test.expect_packet(sock, "unsuback", unsuback_packet): sock.send(subscribe_packet) if mosq_test.expect_packet(sock, "suback", suback_packet): if mosq_test.expect_packet(sock, "publish retained", publish_retained_packet): rc = 0 sock.close() finally: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde) print(stdo) raise return rc
#!/usr/bin/env python import inspect, os, sys # From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder cmd_subfolder = os.path.realpath( os.path.abspath( os.path.join( os.path.split(inspect.getfile(inspect.currentframe()))[0], ".."))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import mosq_test port = mosq_test.get_port() rc = 1 keepalive = 60 connect_packet = mosq_test.gen_connect("test-helper", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) mid = 128 publish_packet = mosq_test.gen_publish("qos1/disconnect/test", qos=1, mid=mid, payload="disconnect-message") puback_packet = mosq_test.gen_puback(mid) sock = mosq_test.do_client_connect(connect_packet, connack_packet, connack_error="helper connack", port=port)
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],".."))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import mosq_test def write_config(filename, port1, port2): with open(filename, 'w') as f: f.write("port %d\n" % (port2)) f.write("listener %d\n" % (port1)) f.write("cafile ../ssl/all-ca.crt\n") f.write("certfile ../ssl/server.crt\n") f.write("keyfile ../ssl/server.key\n") f.write("require_certificate true\n") (port1, port2) = mosq_test.get_port(2) conf_file = os.path.basename(__file__).replace('.py', '.conf') write_config(conf_file, port1, port2) rc = 1 keepalive = 10 connect_packet = mosq_test.gen_connect("connect-success-test", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, use_conf=True) try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client.crt", keyfile="../ssl/client.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1))
f.write("port %d\n" % (port1)) f.write("\n") f.write("listener %d\n" % (port2)) f.write("psk_hint hint\n") def write_config2(filename, port2, port3): with open(filename, 'w') as f: f.write("port %d\n" % (port3)) f.write("\n") f.write("connection bridge-psk\n") f.write("address localhost:%d\n" % (port2)) f.write("topic psk/test out\n") f.write("bridge_identity psk-test\n") f.write("bridge_psk deadbeef\n") (port1, port2, port3) = mosq_test.get_port(3) conf_file1 = "08-tls-psk-bridge.conf" conf_file2 = "08-tls-psk-bridge.conf2" write_config1(conf_file1, port1, port2) write_config2(conf_file2, port2, port3) env = dict(os.environ) env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' env['PYTHONPATH'] = '../../lib/python:'+pp rc = 1
import mosq_test rc = 1 mid = 53 keepalive = 60 connect_packet = mosq_test.gen_connect("subscribe-invalid-utf8", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) subscribe_packet = mosq_test.gen_subscribe(mid, "invalid/utf8", 0) b = list(struct.unpack("B"*len(subscribe_packet), subscribe_packet)) b[13] = 0 # Topic should never have a 0x0000 subscribe_packet = struct.pack("B"*len(b), *b) suback_packet = mosq_test.gen_suback(mid, 0) port = mosq_test.get_port() broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: time.sleep(0.5) sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) sock.send(subscribe_packet) if mosq_test.expect_packet(sock, "suback", ""): rc = 0 sock.close() finally: broker.terminate() broker.wait()
import mosq_test def write_config(filename, port1, port2): with open(filename, 'w') as f: f.write("port %d\n" % (port2)) f.write("\n") f.write("listener %d\n" %(port1)) f.write("cafile ../ssl/all-ca.crt\n") f.write("certfile ../ssl/server.crt\n") f.write("keyfile ../ssl/server.key\n") f.write("\n") f.write("use_identity_as_username true\n") f.write("require_certificate true\n") (port1, port2) = mosq_test.get_port(2) conf_file = os.path.basename(__file__).replace('.py', '.conf') write_config(conf_file, port1, port2) rc = 1 keepalive = 10 connect_packet = mosq_test.gen_connect("connect-identity-test", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, use_conf=True) try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client.crt", keyfile="../ssl/client.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1))