예제 #1
0
def test():
    sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
    mosq_test.do_send_receive(sock, subscribe1_packet, suback1_packet, "suback 1")
    mosq_test.do_send_receive(sock, subscribe2_packet, suback2_packet, "suback 2")
    mosq_test.do_send_receive(sock, unsubscribe1_packet, unsuback1_packet, "unsuback")

    sock.send(disconnect_packet)
    sock.close()
예제 #2
0
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)

    broker = subprocess.Popen(['../../src/mosquitto', '-p', '1888'],
                              stderr=subprocess.PIPE)

    try:
        time.sleep(0.5)

        sock = mosq_test.do_client_connect(connect_packet,
                                           connack_packet,
                                           timeout=20)
        sock.send(subscribe_packet)

        if mosq_test.expect_packet(sock, "suback", suback_packet):
            pub = subprocess.Popen(
                ['./03-pattern-matching-helper.py', pub_topic])
            pub.wait()

            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()
        if rc:
            (stdo, stde) = broker.communicate()
            print(stde)
            raise

    return rc
예제 #3
0
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
예제 #4
0
def test():
    sock = mosq_test.do_client_connect(connect_packet,
                                       connack_packet,
                                       port=port)
    mosq_test.do_send_receive(sock, subscribe1_packet, suback1_packet,
                              "suback 1")
    mosq_test.do_send_receive(sock, subscribe2_packet, suback2_packet,
                              "suback 2")
    mosq_test.do_send_receive(sock, unsubscribe1_packet, unsuback1_packet,
                              "unsuback")

    sock.send(disconnect_packet)
    sock.close()
예제 #5
0
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 do_test(port, connack_rc, username, password):
    rc = 1
    keepalive = 60
    connect_packet = mosq_test.gen_connect("username-password-check",
                                           keepalive=keepalive,
                                           username=username,
                                           password=password)
    connack_packet = mosq_test.gen_connack(rc=connack_rc)

    try:
        sock = mosq_test.do_client_connect(connect_packet,
                                           connack_packet,
                                           port=port)
        rc = 0
        sock.close()
    finally:
        if rc:
            exit(rc)
예제 #7
0
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)
subscribe_packet = mosq_test.gen_subscribe(mid, "qos1/disconnect/test", 1)
suback_packet = mosq_test.gen_suback(mid, 1)

mid = 1
publish_packet = mosq_test.gen_publish("qos1/disconnect/test", qos=1, mid=mid, payload="disconnect-message")
publish_dup_packet = mosq_test.gen_publish("qos1/disconnect/test", qos=1, mid=mid, payload="disconnect-message", dup=True)
puback_packet = mosq_test.gen_puback(mid)

mid = 3266
publish2_packet = mosq_test.gen_publish("qos1/outgoing", qos=1, mid=mid, payload="outgoing-message")
puback2_packet = mosq_test.gen_puback(mid)

broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
    sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port)

    mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")

    pub = subprocess.Popen(['./03-publish-b2c-disconnect-qos1-helper.py', str(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    pub.wait()
    (stdo, stde) = pub.communicate()
    # Should have now received a publish command

    if mosq_test.expect_packet(sock, "publish", publish_packet):
        # Send our outgoing message. When we disconnect the broker
        # should get rid of it and assume we're going to retry.
        sock.send(publish2_packet)
        sock.close()

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
예제 #9
0
import mosq_test

rc = 1
mid = 53
keepalive = 60
connect_packet_ok = mosq_test.gen_connect("will-acl-test", keepalive=keepalive, will_topic="ok", will_payload="should be ok")
connack_packet_ok = mosq_test.gen_connack(rc=0)

connect_packet = mosq_test.gen_connect("will-acl-test", keepalive=keepalive, will_topic="will/acl/test", will_payload="should be denied")
connack_packet = mosq_test.gen_connack(rc=5)

broker = subprocess.Popen(['../../src/mosquitto', '-c', '07-will-acl-denied.conf'], stderr=subprocess.PIPE)

try:
    time.sleep(0.5)

    sock_ok = mosq_test.do_client_connect(connect_packet_ok, connack_packet_ok, timeout=5, connack_error="connack ok")
    sock = mosq_test.do_client_connect(connect_packet, connack_packet)
    rc = 0
    sock.close()
    sock_ok.close()
finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
        print(stde)

exit(rc)

예제 #10
0
connect_packet = mosq_test.gen_connect("will-acl-test",
                                       keepalive=keepalive,
                                       will_topic="will/acl/test",
                                       will_payload="should be denied")
connack_packet = mosq_test.gen_connack(rc=5)

broker = subprocess.Popen(
    ['../../src/mosquitto', '-c', '07-will-acl-denied.conf'],
    stderr=subprocess.PIPE)

try:
    time.sleep(0.5)

    sock_ok = mosq_test.do_client_connect(connect_packet_ok,
                                          connack_packet_ok,
                                          timeout=5,
                                          connack_error="connack ok")
    sock = mosq_test.do_client_connect(connect_packet, connack_packet)
    rc = 0
    sock.close()
    sock_ok.close()
finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
        print(stde)

exit(rc)
    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)

mosq_test.do_send_receive(sock, publish_packet, puback_packet, "helper puback")

rc = 0

sock.close()

exit(rc)
import time

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

rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect(None, keepalive=keepalive)

broker = subprocess.Popen(['../../src/mosquitto', '-p', '1888'], stderr=subprocess.PIPE)

try:
    time.sleep(0.5)

    sock = mosq_test.do_client_connect(connect_packet, "")
    sock.close()
    rc = 0
finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
        print(stde)

exit(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 = int(sys.argv[2])

rc = 1
keepalive = 60
connect_packet = mosq_test.gen_connect("test-helper", keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0)

publish_packet = mosq_test.gen_publish(sys.argv[1], qos=0, retain=True, payload="message")

sock = mosq_test.do_client_connect(connect_packet, connack_packet, connack_error="helper connack", port=port)
sock.send(publish_packet)
rc = 0
sock.close()

exit(rc)

예제 #14
0
connect_packet = mosq_test.gen_connect("no-psk-test-client", keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0)

mid = 1
subscribe_packet = mosq_test.gen_subscribe(mid, "psk/test", 0)
suback_packet = mosq_test.gen_suback(mid, 0)

publish_packet = mosq_test.gen_publish(topic="psk/test", payload="message", qos=0)

bridge_cmd = ['../../src/mosquitto', '-c', '08-tls-psk-bridge.conf2']
broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port1)
bridge = mosq_test.start_broker(filename=os.path.basename(__file__)+'_bridge', cmd=bridge_cmd, port=port3)

pub = None
try:
    sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=30, port=port1)
    sock.send(subscribe_packet)

    if mosq_test.expect_packet(sock, "suback", suback_packet):
        pub = subprocess.Popen(['./c/08-tls-psk-bridge.test', str(port3)], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if pub.wait():
            raise ValueError
        (stdo, stde) = pub.communicate()

        if mosq_test.expect_packet(sock, "publish", publish_packet):
            rc = 0
    sock.close()
finally:
    os.remove(conf_file1)
    os.remove(conf_file2)
    time.sleep(1)
        if not match:
            print("Fail on cases local_topic=%r, remote_topic=%r" % (
                local_topic,
                remote_topic,
            ))
            return 1
    return 0


try:
    (bridge, address) = ssock.accept()
    bridge.settimeout(2)

    sock = mosq_test.do_client_connect(
        client_connect_packet,
        client_connack_packet,
        port=port2,
    )

    rc = test(bridge, sock)

    sock.close()
    bridge.close()
finally:
    os.remove(conf_file)
    try:
        bridge.close()
    except NameError:
        pass

    broker.terminate()
예제 #16
0
                                         qos=2,
                                         mid=mid,
                                         payload="message")
pubrec1r_packet = mosq_test.gen_pubrec(mid)
pubrel1r_packet = mosq_test.gen_pubrel(mid)
puback1r_packet = mosq_test.gen_puback(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, pubrec1s_packet,
                              "pubrec 1s")
    mosq_test.do_send_receive(helper, pubrel1s_packet, pubcomp1s_packet,
                              "pubcomp 1s")
    helper.close()

    if mosq_test.expect_packet(sock, "publish 1r", publish1r_packet):
        mosq_test.do_send_receive(sock, pubrec1s_packet, pubrel1s_packet,
예제 #17
0
pubrel_packet = mosq_test.gen_pubrel(mid)
pubcomp_packet = mosq_test.gen_pubcomp(mid)

mid = 3266
publish2_packet = mosq_test.gen_publish("qos1/outgoing",
                                        qos=1,
                                        mid=mid,
                                        payload="outgoing-message")
puback2_packet = mosq_test.gen_puback(mid)

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,
                                       connack1_packet,
                                       port=port)

    mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")

    pub = subprocess.Popen(
        ['./03-publish-b2c-disconnect-qos2-helper.py',
         str(port)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    hrc = pub.wait()
    (stdo, stde) = pub.communicate()
    if hrc:
        exit(hrc)
    # Should have now received a publish command
예제 #18
0
            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 = 60
connect_packet = mosq_test.gen_connect("will-null-topic",
                                       keepalive=keepalive,
                                       will_topic="",
                                       will_payload=struct.pack(
                                           "!4sB7s", "will", 0, "message"))
connack_packet = mosq_test.gen_connack(rc=2)

cmd = ['../../src/mosquitto', '-p', '1888']
broker = mosq_test.start_broker(filename=os.path.basename(__file__), cmd=cmd)

try:
    sock = mosq_test.do_client_connect(connect_packet, "", timeout=30)
    rc = 0
    sock.close()
finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
        print(stde)

exit(rc)
예제 #19
0
                print("FAIL: Received data when nothing is expected")
                print("Fail on cases local_topic=%r, remote_topic=%r" % (
                    local_topic, remote_topic,
                ))
                return 1
            except socket.timeout:
                pass
            bridge.settimeout(20)
    return 0

try:
    (bridge, address) = ssock.accept()
    bridge.settimeout(2)

    sock = mosq_test.do_client_connect(
        client_connect_packet, client_connack_packet,
        port=port2,
    )

    rc = test(bridge, sock)

    sock.close()
    bridge.close()
finally:
    os.remove(conf_file)
    try:
        bridge.close()
    except NameError:
        pass

    broker.terminate()
    broker.wait()
mid=1
subscribe_packet = mosq_test.gen_subscribe(mid, "flipflop/test", 1)
suback_packet = mosq_test.gen_suback(mid, 1)

mid=1
publish_packet = mosq_test.gen_publish("flipflop/test", qos=1, mid=mid, payload="message")
puback_packet = mosq_test.gen_puback(mid)


port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
    # mosquitto_sub -i sub -t 'topic' -q 1 -d -c
    sub_sock = mosq_test.do_client_connect(connect_packet_sub_persistent, connack_packet_sub, port=port)
    mosq_test.do_send_receive(sub_sock, subscribe_packet, suback_packet, "subscribe persistent 1")
    # And disconnect
    sub_sock.close()

    # mosquitto_sub -i sub -t 'topic' -q 1 -d
    sub_sock = mosq_test.do_client_connect(connect_packet_sub_clean, connack_packet_sub, port=port)
    mosq_test.do_send_receive(sub_sock, subscribe_packet, suback_packet, "subscribe clean")
    # And disconnect
    sub_sock.close()

    # mosquitto_sub -i sub -t 'topic' -v -q 1 -d -c
    sub_sock = mosq_test.do_client_connect(connect_packet_sub_persistent, connack_packet_sub, port=port)
    mosq_test.do_send_receive(sub_sock, subscribe_packet, suback_packet, "subscribe persistent 2")

    # and in a separate shell
예제 #21
0
rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("no-psk-test-client", keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0)

mid = 1
subscribe_packet = mosq_test.gen_subscribe(mid, "psk/test", 0)
suback_packet = mosq_test.gen_suback(mid, 0)

publish_packet = mosq_test.gen_publish(topic="psk/test", payload="message", qos=0)

broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=1889)

try:
    sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=1889, timeout=20)
    sock.send(subscribe_packet)

    if mosq_test.expect_packet(sock, "suback", suback_packet):
        pub = subprocess.Popen(['./c/08-tls-psk-pub.test'], env=env)
        if pub.wait():
            raise ValueError

        if mosq_test.expect_packet(sock, "publish", publish_packet):
            rc = 0
    sock.close()
finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
예제 #22
0
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ssock.settimeout(40)
ssock.bind(('', port1))
ssock.listen(5)

broker = mosq_test.start_broker(filename=os.path.basename(__file__),
                                port=port2,
                                use_conf=True)

try:
    (bridge, address) = ssock.accept()
    bridge.settimeout(20)

    client = mosq_test.do_client_connect(c_connect_packet,
                                         c_connack_packet,
                                         timeout=20,
                                         port=port2)
    mosq_test.do_send_receive(client, publish_packet, puback_packet, "puback")
    client.close()
    # We've now sent a message to the broker that should be delivered to us via the bridge

    if mosq_test.expect_packet(bridge, "connect", connect_packet):
        bridge.send(connack_packet)

        if mosq_test.expect_packet(bridge, "publish", publish_packet):
            rc = 0

    bridge.close()
finally:
    os.remove(conf_file)
    try:
예제 #23
0
import struct
import mosq_test

rc = 1
keepalive = 60
connect_packet = mosq_test.gen_connect("connect-invalid-utf8",
                                       keepalive=keepalive)
b = list(struct.unpack("B" * len(connect_packet), connect_packet))
b[21] = 0  # Client id should never have a 0x0000
connect_packet = struct.pack("B" * len(b), *b)

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, "", port=port)
    # Exception occurs if connack packet returned
    rc = 0
    sock.close()
finally:
    broker.terminate()
    broker.wait()
    (stdo, stde) = broker.communicate()
    if rc:
        print(stde)

exit(rc)
예제 #24
0
suback_packet = mosq_test.gen_suback(mid, 1)

mid = 1
publish_packet = mosq_test.gen_publish("flipflop/test",
                                       qos=1,
                                       mid=mid,
                                       payload="message")
puback_packet = mosq_test.gen_puback(mid)

port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
    # mosquitto_sub -i sub -t 'topic' -q 1 -d -c
    sub_sock = mosq_test.do_client_connect(connect_packet_sub_persistent,
                                           connack_packet_sub,
                                           port=port)
    mosq_test.do_send_receive(sub_sock, subscribe_packet, suback_packet,
                              "subscribe persistent 1")
    # And disconnect
    sub_sock.close()

    # mosquitto_sub -i sub -t 'topic' -q 1 -d
    sub_sock = mosq_test.do_client_connect(connect_packet_sub_clean,
                                           connack_packet_sub,
                                           port=port)
    mosq_test.do_send_receive(sub_sock, subscribe_packet, suback_packet,
                              "subscribe clean")
    # And disconnect
    sub_sock.close()
예제 #25
0
puback_packet = mosq_test.gen_puback(mid)

mid = 1
publish_packet2 = mosq_test.gen_publish("subpub/qos1",
                                        qos=1,
                                        mid=mid,
                                        payload="message")

broker = subprocess.Popen(['../../src/mosquitto', '-p', '1888'],
                          stderr=subprocess.PIPE)

try:
    time.sleep(0.5)

    sock = mosq_test.do_client_connect(connect_packet,
                                       connack_packet,
                                       timeout=20)
    sock.send(subscribe_packet)

    if mosq_test.expect_packet(sock, "suback", suback_packet):
        sock.send(publish_packet)

        if mosq_test.expect_packet(sock, "puback", puback_packet):

            if mosq_test.expect_packet(sock, "publish2", publish_packet2):
                rc = 0

    sock.close()
finally:
    broker.terminate()
    broker.wait()
예제 #26
0
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

rc = 1
keepalive = 60
connect_packet = mosq_test.gen_connect("will-null-topic", keepalive=keepalive, will_topic="", will_payload=struct.pack("!4sB7s", "will", 0, "message"))
connack_packet = mosq_test.gen_connack(rc=2)

cmd = ['../../src/mosquitto', '-p', '1888']
broker = mosq_test.start_broker(filename=os.path.basename(__file__), cmd=cmd)

try:
    sock = mosq_test.do_client_connect(connect_packet, "", timeout=30)
    rc = 0
    sock.close()
finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
        print(stde)

exit(rc)

mid = 1
publish_packet = mosq_test.gen_publish("qos2/disconnect/test", qos=2, mid=mid, payload="disconnect-message")
publish_dup_packet = mosq_test.gen_publish("qos2/disconnect/test", qos=2, mid=mid, payload="disconnect-message", dup=True)
pubrec_packet = mosq_test.gen_pubrec(mid)
pubrel_packet = mosq_test.gen_pubrel(mid)
pubrel_dup_packet = mosq_test.gen_pubrel(mid, dup=True)
pubcomp_packet = mosq_test.gen_pubcomp(mid)

mid = 3266
publish2_packet = mosq_test.gen_publish("qos1/outgoing", qos=1, mid=mid, payload="outgoing-message")
puback2_packet = mosq_test.gen_puback(mid)

broker = mosq_test.start_broker(filename=os.path.basename(__file__))

try:
    sock = mosq_test.do_client_connect(connect_packet, connack_packet)

    sock.send(publish_packet)
    if mosq_test.expect_packet(sock, "pubrec", pubrec_packet):
        # We're now going to disconnect and pretend we didn't receive the pubrec.
        sock.close()

        sock = mosq_test.do_client_connect(connect_packet, connack_packet)
        sock.send(publish_dup_packet)

        if mosq_test.expect_packet(sock, "pubrec", pubrec_packet):
            sock.send(pubrel_packet)

            if mosq_test.expect_packet(sock, "pubcomp", pubcomp_packet):
                # Again, pretend we didn't receive this pubcomp
                sock.close()
예제 #28
0
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)
예제 #29
0
publish_packet = mosq_test.gen_publish("retain/clear/test", qos=0, payload="retained message", retain=True)
retain_clear_packet = mosq_test.gen_publish("retain/clear/test", qos=0, payload=None, retain=True)
mid_sub = 592
subscribe_packet = mosq_test.gen_subscribe(mid_sub, "retain/clear/test", 0)
suback_packet = mosq_test.gen_suback(mid_sub, 0)

mid_unsub = 593
unsubscribe_packet = mosq_test.gen_unsubscribe(mid_unsub, "retain/clear/test")
unsuback_packet = mosq_test.gen_unsuback(mid_unsub)

cmd = ['../../src/mosquitto', '-p', '1888']
broker = mosq_test.start_broker(filename=os.path.basename(__file__), cmd=cmd)

try:
    sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=4)
    # Send retained message
    sock.send(publish_packet)
    # Subscribe to topic, we should get the retained message back.
    sock.send(subscribe_packet)

    if mosq_test.expect_packet(sock, "suback", suback_packet):
        if mosq_test.expect_packet(sock, "publish", publish_packet):
            # Now unsubscribe from the topic before we clear the retained
            # message.
            sock.send(unsubscribe_packet)

            if mosq_test.expect_packet(sock, "unsuback", unsuback_packet):
                # Now clear the retained message.
                sock.send(retain_clear_packet)
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)
        p = sock.recv(len(pingresp_packet))
        if len(p) == 0:
            rc = 0

    sock.close()
finally:
예제 #31
0
            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 = 10
connect_packet = mosq_test.gen_connect("connect-invalid-id-test-",
                                       keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=2)

broker = subprocess.Popen(['../../src/mosquitto', '-p', '1888'],
                          stderr=subprocess.PIPE)

try:
    time.sleep(0.5)

    sock = mosq_test.do_client_connect(connect_packet, connack_packet)
    sock.close()
    rc = 0

finally:
    broker.terminate()
    broker.wait()
    if rc:
        (stdo, stde) = broker.communicate()
        print(stde)

exit(rc)
connack2_packet = mosq_test.gen_connack(resv=1, rc=0)

disconnect_packet = mosq_test.gen_disconnect()

subscribe_packet = mosq_test.gen_subscribe(mid, "qos1/clean_session/test", 1)
suback_packet = mosq_test.gen_suback(mid, 1)

mid = 1
publish_packet = mosq_test.gen_publish("qos1/clean_session/test", qos=1, mid=mid, payload="clean-session-message")
puback_packet = mosq_test.gen_puback(mid)

cmd = ['../../src/mosquitto', '-p', '1888']
broker = mosq_test.start_broker(filename=os.path.basename(__file__), cmd=cmd)

try:
    sock = mosq_test.do_client_connect(connect_packet, connack1_packet)
    sock.send(subscribe_packet)

    if mosq_test.expect_packet(sock, "suback", suback_packet):
        sock.send(disconnect_packet)
        sock.close()

        pub = subprocess.Popen(['./05-clean-session-qos1-helper.py'])
        pub.wait()

        # Now reconnect and expect a publish message.
        sock = mosq_test.do_client_connect(connect_packet, connack2_packet, timeout=30)
        if mosq_test.expect_packet(sock, "publish", publish_packet):
            sock.send(puback_packet)
            rc = 0