def do_test(port, per_listener): conf_file = os.path.basename(__file__).replace('.py', '.conf') write_config(conf_file, port, per_listener) acl_file = os.path.basename(__file__).replace('.py', '.acl') write_acl(acl_file) rc = 1 keepalive = 60 connect_packet = mosq_test.gen_connect("acl-check", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) mid = 1 publish_packet = mosq_test.gen_publish("test/topic", qos=0, payload="message") subscribe_packet = mosq_test.gen_subscribe(mid, "test/topic", 0) suback_packet = mosq_test.gen_suback(mid, 0) pingreq_packet = mosq_test.gen_pingreq() pingresp_packet = mosq_test.gen_pingresp() broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback") sock.send(publish_packet) # If we receive the message, this will fail. mosq_test.do_send_receive(sock, pingreq_packet, pingresp_packet, "pingresp") rc = 0 sock.close() finally: os.remove(conf_file) os.remove(acl_file) broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde) exit(rc)
def single_test(port, per_listener, username, topic, expect_deny): rc = 1 conf_file = os.path.basename(__file__).replace('.py', '.conf') write_config(conf_file, port, per_listener) broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) try: keepalive = 60 connect_packet = mosq_test.gen_connect("acl-check", keepalive=keepalive, username=username) connack_packet = mosq_test.gen_connack(rc=0) mid = 1 subscribe_packet = mosq_test.gen_subscribe(mid=mid, topic=topic, qos=1) suback_packet = mosq_test.gen_suback(mid=mid, qos=1) mid = 2 publish1s_packet = mosq_test.gen_publish(topic=topic, mid=mid, qos=1, payload="message") puback1s_packet = mosq_test.gen_puback(mid) mid=1 publish1r_packet = mosq_test.gen_publish(topic=topic, mid=mid, qos=1, payload="message") pingreq_packet = mosq_test.gen_pingreq() pingresp_packet = mosq_test.gen_pingresp() sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback") mosq_test.do_send_receive(sock, publish1s_packet, puback1s_packet, "puback") if expect_deny: mosq_test.do_send_receive(sock, pingreq_packet, pingresp_packet, "pingresp") else: mosq_test.expect_packet(sock, "publish1r", publish1r_packet) sock.close() rc = 0 finally: os.remove(conf_file) broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde) exit(rc)
import time # 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 rc = 1 keepalive = 4 connect_packet = mosq_test.gen_connect("01-keepalive-pingreq", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) pingreq_packet = mosq_test.gen_pingreq() pingresp_packet = mosq_test.gen_pingresp() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.settimeout(10) sock.bind(('', 1888)) sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' env['PYTHONPATH'] = '../../lib/python:'+pp
subscribe_packet = mosq_test.gen_subscribe(mid, "subpub/qos1", 1) suback_packet = mosq_test.gen_suback(mid, 1) helper_connect = mosq_test.gen_connect("helper", keepalive=keepalive) helper_connack = mosq_test.gen_connack(rc=0) mid = 1 publish1s_packet = mosq_test.gen_publish("subpub/qos1", qos=1, mid=mid, payload="message") puback1s_packet = mosq_test.gen_puback(mid) mid = 1 publish1r_packet = mosq_test.gen_publish("subpub/qos1", qos=1, mid=mid, payload="message") pubrec1r_packet = mosq_test.gen_pubrec(mid) pingreq_packet = mosq_test.gen_pingreq() pingresp_packet = mosq_test.gen_pingresp() port = mosq_test.get_port() broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port) mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback") helper = mosq_test.do_client_connect(helper_connect, helper_connack, timeout=20, port=port) mosq_test.do_send_receive(helper, publish1s_packet, puback1s_packet, "puback 1s") helper.close() if mosq_test.expect_packet(sock, "publish 1r", publish1r_packet): sock.send(pubrec1r_packet) sock.send(pingreq_packet)
def do_test(per_listener): conf_file = os.path.basename(__file__).replace('.py', '.conf') write_config(conf_file, port, per_listener) acl_file = os.path.basename(__file__).replace('.py', '.acl') write_acl_1(acl_file) rc = 1 keepalive = 60 connect_packet = mosq_test.gen_connect("retain-check", keepalive=keepalive) connack_packet = mosq_test.gen_connack(rc=0) mid = 1 publish_packet = mosq_test.gen_publish("test/topic", qos=0, payload="retained message", retain=True) subscribe_packet = mosq_test.gen_subscribe(mid, "test/topic", 0) suback_packet = mosq_test.gen_suback(mid, 0) pingreq_packet = mosq_test.gen_pingreq() pingresp_packet = mosq_test.gen_pingresp() broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) sock.send(publish_packet) sock.close() sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback 1") if mosq_test.expect_packet(sock, "publish", publish_packet): sock.close() # Remove "write" ability write_acl_2(acl_file) broker.send_signal(signal.SIGHUP) sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback 2") # If we receive the retained message here, it is a failure. mosq_test.do_send_receive(sock, pingreq_packet, pingresp_packet, "pingresp") rc = 0 sock.close() finally: os.remove(conf_file) os.remove(acl_file) broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde) exit(rc)