]> granicus.if.org Git - esp-idf/commitdiff
mqtt: example tests refactored to provide descriptive failures, addapted to python23...
authorDavid Cermak <cermak@espressif.com>
Thu, 27 Sep 2018 09:46:13 +0000 (11:46 +0200)
committerDavid Cermak <cermak@espressif.com>
Thu, 1 Nov 2018 08:04:09 +0000 (09:04 +0100)
examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py
examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py
examples/protocols/mqtt/ws/mqtt_ws_example_test.py
examples/protocols/mqtt/wss/mqtt_wss_example_test.py
tools/tiny-test-fw/IDF/IDFDUT.py

index c5ae31af2adcbaa30008daca227e518710e6f617..be6d1e302c8fa16f273677ddc7c543d1020a6bee 100644 (file)
@@ -5,66 +5,29 @@ import time
 import socket
 import imp
 import ssl
-
-use_mqtt_client_sketch = False
-
-try:
-    imp.find_module('paho')
-    import paho.mqtt.client as mqtt
-    # Make things with supposed existing module
-except ImportError:
-    use_mqtt_client_sketch = True
-    pass
-
-global g_recv_topic
-global g_recv_data
+import paho.mqtt.client as mqtt
 
 g_recv_data=""
-
-# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface
-def mqtt_client_sketch():
-    global g_recv_topic
-    global g_recv_data
-    connect_msg = bytearray([0x10, 0x0c, 00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x02, 00, 0x3c, 00, 00])
-    send_qos0_msg = bytearray([ 0x30, 0x1a, 0x00, 0x0b, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x2f, 0x71, 0x6f, 0x73, 0x30, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x74, 0x6f, 0x5f, 0x65, 0x73, 0x70, 0x33, 0x32])
-    subscribe_qos0 = bytearray([ 0x82, 0x10, 0x00, 0x01, 0x00, 0x0b, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x2f, 0x71, 0x6f, 0x73, 0x30, 0x00] )
-    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    client.settimeout(30)
-    cli = ssl.wrap_socket(client)
-    cli.connect(("iot.eclipse.org", 8883))
-    cli.send(connect_msg)
-    data = cli.recv(1024)
-    print("Connect ack received {}".format(data))
-    cli.send(subscribe_qos0)
-    data = cli.recv(1024)
-    print("Subscibe ack received {}".format(data))
-    start = time.time()
-    while (time.time() - start) <= 20:
-        data = cli.recv(1024)
-        print("Data received {}".format(data[-17:]))
-        if data[-15:] == "/topic/qos0data":
-            g_recv_topic = data[-15:][:11]
-            g_recv_data = data[-4:]
-            cli.send(send_qos0_msg)
-            data = cli.recv(1024)
-            print("data ack received {}".format(data))
-            break
-    cli.close()
+g_recv_topic=""
+g_broker_connected=0
 
 # The callback for when the client receives a CONNACK response from the server.
 def on_connect(client, userdata, flags, rc):
+    global g_broker_connected
     print("Connected with result code "+str(rc))
+    g_broker_connected = 1
     client.subscribe("/topic/qos0")
 
 # The callback for when a PUBLISH message is received from the server.
 def on_message(client, userdata, msg):
     global g_recv_topic
     global g_recv_data
-    if g_recv_data == "" and  msg.payload == "data":
+    payload = msg.payload.decode()
+    if g_recv_data == "" and  payload == "data":
         client.publish("/topic/qos0", "data_to_esp32")
         g_recv_topic = msg.topic
-        g_recv_data = msg.payload
-    print(msg.topic+" "+str(msg.payload))
+        g_recv_data = payload
+    print(msg.topic+" "+str(payload))
 
 # this is a test case write with tiny-test-fw.
 # to run test cases outside tiny-test-fw,
@@ -76,7 +39,7 @@ if test_fw_path and test_fw_path not in sys.path:
 
 import TinyFW
 import IDF
-
+import DUT
 
 
 
@@ -84,6 +47,8 @@ import IDF
 def test_examples_protocol_mqtt_ssl(env, extra_data):
     global g_recv_topic
     global g_recv_data
+    global g_broker_connected
+    broker_url="iot.eclipse.org"
     """
     steps: |
       1. join AP and connects to ssl broker
@@ -97,12 +62,13 @@ def test_examples_protocol_mqtt_ssl(env, extra_data):
     bin_size = os.path.getsize(binary_file)
     IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size//1024))
     IDF.check_performance("mqtt_ssl_size", bin_size//1024)
-    # 1. start test
+    # 1. start test (and check the environment is healthy)
     dut1.start_app()
+    client = None
     # 2. Test connects to a broker
-    if use_mqtt_client_sketch:
-        mqtt_client_sketch()
-    else:
+    try:
+        ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
+        print("Connected to AP with IP: {}".format(ip_address))
         client = mqtt.Client()
         client.on_connect = on_connect
         client.on_message = on_message
@@ -110,15 +76,21 @@ def test_examples_protocol_mqtt_ssl(env, extra_data):
                     None,
                     None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
         client.tls_insecure_set(True)
-
-        print "Connecting..."
-        client.connect("iot.eclipse.org", 8883, 60)
-        print "...done"
-        print "Start Looping..."
-        start = time.time()
-        while (time.time() - start) <= 20:
-            client.loop()
-        print "...done"
+        print("Connecting...")
+        client.connect(broker_url, 8883, 60)
+        print("...done")
+    except DUT.ExpectTimeout:
+        raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+    except:
+        print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
+        raise
+    print("Start Looping...")
+    start = time.time()
+    while (time.time() - start) <= 20:
+        client.loop()
+    print("...done")
+    if g_broker_connected == 0:
+        raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
     # 3. check the message received back from the server
     if g_recv_topic == "/topic/qos0" and g_recv_data == "data" :
         print("PASS: Received correct message")
index c1bf42d9f26ea6b37655d0fefb60570095b489ef..e2568d527b31a4ed3ca23ba8c82919c0e1a4e485 100644 (file)
@@ -3,9 +3,10 @@ import os
 import sys
 from socket import *
 from threading import Thread
+import struct
 import time
 
-global msgid
+msgid=-1
 
 def get_my_ip():
     s1 = socket(AF_INET, SOCK_DGRAM)
@@ -17,14 +18,20 @@ def get_my_ip():
 def mqqt_server_sketch(my_ip, port):
     global msgid
     print("Starting the server on {}".format(my_ip))
-    s=socket(AF_INET, SOCK_STREAM)
-    s.settimeout(60)
-    s.bind((my_ip, port))
-    s.listen(1)
-    q,addr=s.accept()
-    q.settimeout(30)
-    print("connection accepted")
-    # q.send(g_msg_to_client)
+    s = None
+    try:
+        s=socket(AF_INET, SOCK_STREAM)
+        s.settimeout(60)
+        s.bind((my_ip, port))
+        s.listen(1)
+        q,addr=s.accept()
+        q.settimeout(30)
+        print("connection accepted")
+    except:
+        print("Local server on {}:{} listening/accepting failure: {}"
+                "Possibly check permissions or firewall settings"
+                "to accept connections on this address".format(my_ip, port, sys.exc_info()[0]))
+        raise
     data = q.recv(1024)
     # check if received initial empty message
     print("received from client {}".format(data))
@@ -32,7 +39,7 @@ def mqqt_server_sketch(my_ip, port):
     q.send(data)
     # try to receive qos1
     data = q.recv(1024)
-    msgid = ord(data[15])*256+ord(data[16])
+    msgid = struct.unpack(">H", data[15:17])[0]
     print("received from client {}, msgid: {}".format(data, msgid))
     data = bytearray([0x40, 0x02, data[15], data[16]])
     q.send(data)
@@ -50,6 +57,7 @@ if test_fw_path and test_fw_path not in sys.path:
 
 import TinyFW
 import IDF
+import DUT
 
 
 
@@ -75,21 +83,21 @@ def test_examples_protocol_mqtt_qos1(env, extra_data):
     thread1 = Thread(target = mqqt_server_sketch, args = (host_ip,1883))
     thread1.start()
     # 2. start the dut test and wait till client gets IP address
-    dut1.start_app()
+    dut1.start_app()    
     # waiting for getting the IP address
-    data = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
-    # time.sleep(15)
+    try:
+        ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
+        print("Connected to AP with IP: {}".format(ip_address))
+    except DUT.ExpectTimeout:
+        raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+
     print ("writing to device: {}".format("mqtt://" + host_ip + "\n"))
     dut1.write("mqtt://" + host_ip + "\n")
     thread1.join()
     print ("Message id received from server: {}".format(msgid))
     # 3. check the message id was enqueued and then deleted
     msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30)
-    # expect_txt="OUTBOX: ENQUEUE msgid=" + str(msgid)
-    # dut1.expect(re.compile(expect_txt), timeout=30)
     msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30)
-    # expect_txt="OUTBOX: DELETED msgid=" + str(msgid)
-    # dut1.expect(re.compile(expect_txt), timeout=30)
     # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox
     if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)):
         print("PASS: Received correct msg id")
index f00e4dd0be5bdb10de56658027d3f5083c6612fc..276f1d3f69df9355855761e9c40e88957e4314bb 100644 (file)
@@ -1,70 +1,35 @@
+from __future__ import print_function
+from __future__ import unicode_literals
+from builtins import str
 import re
 import os
 import sys
 import time
 import socket
 import imp
-
-use_mqtt_client_sketch = False
-
-try:
-    imp.find_module('paho')
-    import paho.mqtt.client as mqtt
-    # Make things with supposed existing module
-except ImportError:
-    use_mqtt_client_sketch = True
-    pass
-
-global g_recv_topic
-global g_recv_data
+import paho.mqtt.client as mqtt
 
 g_recv_data=""
-
-# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface
-def mqtt_client_sketch():
-    global g_recv_topic
-    global g_recv_data
-    http_connect = bytearray([ 0x47, 0x45, 0x54, 0x20, 0x2f, 0x77, 0x73, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x33, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20, 0x6d, 0x71, 0x74, 0x74, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x4b, 0x65, 0x79, 0x3a, 0x20, 0x6c, 0x35, 0x61, 0x50, 0x41, 0x64, 0x6d, 0x4a, 0x52, 0x65, 0x32, 0x79, 0x55, 0x42, 0x79, 0x68, 0x37, 0x35, 0x72, 0x58, 0x68, 0x51, 0x3d, 0x3d, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x0d, 0x0a])
-    connect_msg = bytearray([0x82, 0x8e, 0x82, 0x1a, 0xe6, 0x22, 0x92, 0x16, 0xe6, 0x26, 0xcf, 0x4b, 0xb2, 0x76, 0x86, 0x18, 0xe6, 0x1e, 0x82, 0x1a])
-    send_qos0_msg = bytearray([ 0x82, 0x9c, 0x44, 0x78, 0xdf, 0x8e, 0x74, 0x62, 0xdf, 0x85, 0x6b, 0x0c, 0xb0, 0xfe, 0x2d, 0x1b, 0xf0, 0xff, 0x2b, 0x0b, 0xef, 0xea, 0x25, 0x0c, 0xbe, 0xd1, 0x30, 0x17, 0x80, 0xeb, 0x37, 0x08, 0xec, 0xbc ])
-    subscribe_qos0 = bytearray([ 0x82, 0x92, 0x8e, 0x31, 0x8c, 0x4a, 0x0c, 0x21, 0x8c, 0x4b, 0x8e, 0x3a, 0xa3, 0x3e, 0xe1, 0x41, 0xe5, 0x29, 0xa1, 0x40, 0xe3, 0x39, 0xbe, 0x31] )
-    cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    cli.settimeout(30)
-    cli.connect(("iot.eclipse.org", 80))
-    cli.send(http_connect)
-    cli.send(connect_msg)
-    data = cli.recv(1024)
-    print("Connect ack received {}".format(data))
-    cli.send(subscribe_qos0)
-    data = cli.recv(1024)
-    print("Subscibe ack received {}".format(data))
-    start = time.time()
-    while (time.time() - start) <= 20:
-        data = cli.recv(1024)
-        print("Data received {}".format(data[-17:]))
-        if data[-15:] == "/topic/qos0data":
-            g_recv_topic = data[-15:][:11]
-            g_recv_data = data[-4:]
-            cli.send(send_qos0_msg)
-            data = cli.recv(1024)
-            print("data ack received {}".format(data))
-            break
-    cli.close()
+g_recv_topic=""
+g_broker_connected=0
 
 # The callback for when the client receives a CONNACK response from the server.
 def on_connect(client, userdata, flags, rc):
+    global g_broker_connected
     print("Connected with result code "+str(rc))
+    g_broker_connected = 1
     client.subscribe("/topic/qos0")
 
 # The callback for when a PUBLISH message is received from the server.
 def on_message(client, userdata, msg):
     global g_recv_topic
     global g_recv_data
-    if g_recv_data == "" and  msg.payload == "data":
+    payload = msg.payload.decode()
+    if g_recv_data == "" and  payload == "data":
         client.publish("/topic/qos0", "data_to_esp32")
         g_recv_topic = msg.topic
-        g_recv_data = msg.payload
-    print(msg.topic+" "+str(msg.payload))
+        g_recv_data = payload
+    print(msg.topic+" "+payload)
 
 # this is a test case write with tiny-test-fw.
 # to run test cases outside tiny-test-fw,
@@ -76,14 +41,15 @@ if test_fw_path and test_fw_path not in sys.path:
 
 import TinyFW
 import IDF
-
-
+import DUT
 
 
 @IDF.idf_example_test(env_tag="Example_WIFI")
 def test_examples_protocol_mqtt_ws(env, extra_data):
     global g_recv_topic
     global g_recv_data
+    global g_broker_connected
+    broker_url="iot.eclipse.org"
     """
     steps: |
       1. join AP and connects to ws broker
@@ -97,24 +63,32 @@ def test_examples_protocol_mqtt_ws(env, extra_data):
     bin_size = os.path.getsize(binary_file)
     IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size//1024))
     IDF.check_performance("mqtt_websocket_size", bin_size//1024)
-    # 1. start test
+    # 1. start test (and check the environment is healthy)
     dut1.start_app()
+    client = None
     # 2. Test connects to a broker
-    if use_mqtt_client_sketch:
-        mqtt_client_sketch()
-    else:
+    try:
+        ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
+        print("Connected to AP with IP: {}".format(ip_address))
         client = mqtt.Client(transport="websockets")
         client.on_connect = on_connect
         client.on_message = on_message
         client.ws_set_options(path="/ws", headers=None)
-        print "Connecting..."
-        client.connect("iot.eclipse.org", 80, 60)
-        print "...done"
-        print "Start Looping..."
-        start = time.time()
-        while (time.time() - start) <= 20:
-            client.loop()
-        print "...done"
+        print("Connecting...")
+        client.connect(broker_url, 80, 60)
+        print("...done")
+    except DUT.ExpectTimeout:
+        raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+    except:
+        print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
+        raise
+    print("Start Looping...")
+    start = time.time()
+    while (time.time() - start) <= 20:
+        client.loop()
+    print("...done")
+    if g_broker_connected == 0:
+        raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
     # 3. check the message received back from the server
     if g_recv_topic == "/topic/qos0" and g_recv_data == "data" :
         print("PASS: Received correct message")
index 9c153cf4b64a88d8f7441f8877a229da23943339..ade43c1d7e657c2dba26fb341c0cf7fb4a71a206 100644 (file)
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals
 import re
 import os
 import sys
@@ -5,68 +6,29 @@ import time
 import socket
 import imp
 import ssl
-
-use_mqtt_client_sketch = False
-
-try:
-    imp.find_module('paho')
-    import paho.mqtt.client as mqtt
-    # Make things with supposed existing module
-except ImportError:
-    use_mqtt_client_sketch = True
-    pass
-
-global g_recv_topic
-global g_recv_data
+import paho.mqtt.client as mqtt
 
 g_recv_data=""
-
-# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface
-def mqtt_client_sketch():
-    global g_recv_topic
-    global g_recv_data
-    http_connect = bytearray([ 0x47, 0x45, 0x54, 0x20, 0x2f, 0x77, 0x73, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x33, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20, 0x6d, 0x71, 0x74, 0x74, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x4b, 0x65, 0x79, 0x3a, 0x20, 0x6c, 0x35, 0x61, 0x50, 0x41, 0x64, 0x6d, 0x4a, 0x52, 0x65, 0x32, 0x79, 0x55, 0x42, 0x79, 0x68, 0x37, 0x35, 0x72, 0x58, 0x68, 0x51, 0x3d, 0x3d, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x0d, 0x0a])
-    connect_msg = bytearray([0x82, 0x8e, 0x82, 0x1a, 0xe6, 0x22, 0x92, 0x16, 0xe6, 0x26, 0xcf, 0x4b, 0xb2, 0x76, 0x86, 0x18, 0xe6, 0x1e, 0x82, 0x1a])
-    send_qos0_msg = bytearray([ 0x82, 0x9c, 0x44, 0x78, 0xdf, 0x8e, 0x74, 0x62, 0xdf, 0x85, 0x6b, 0x0c, 0xb0, 0xfe, 0x2d, 0x1b, 0xf0, 0xff, 0x2b, 0x0b, 0xef, 0xea, 0x25, 0x0c, 0xbe, 0xd1, 0x30, 0x17, 0x80, 0xeb, 0x37, 0x08, 0xec, 0xbc ])
-    subscribe_qos0 = bytearray([ 0x82, 0x92, 0x8e, 0x31, 0x8c, 0x4a, 0x0c, 0x21, 0x8c, 0x4b, 0x8e, 0x3a, 0xa3, 0x3e, 0xe1, 0x41, 0xe5, 0x29, 0xa1, 0x40, 0xe3, 0x39, 0xbe, 0x31] )
-    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    client.settimeout(30)
-    cli = ssl.wrap_socket(client)
-    cli.connect(("iot.eclipse.org", 443))
-    cli.send(http_connect)
-    cli.send(connect_msg)
-    data = cli.recv(1024)
-    print("Connect ack received {}".format(data))
-    cli.send(subscribe_qos0)
-    data = cli.recv(1024)
-    print("Subscibe ack received {}".format(data))
-    start = time.time()
-    while (time.time() - start) <= 20:
-        data = cli.recv(1024)
-        print("Data received {}".format(data[-17:]))
-        if data[-15:] == "/topic/qos0data":
-            g_recv_topic = data[-15:][:11]
-            g_recv_data = data[-4:]
-            cli.send(send_qos0_msg)
-            data = cli.recv(1024)
-            print("data ack received {}".format(data))
-            break
-    cli.close()
+g_recv_topic=""
+g_broker_connected=0
 
 # The callback for when the client receives a CONNACK response from the server.
 def on_connect(client, userdata, flags, rc):
+    global g_broker_connected
     print("Connected with result code "+str(rc))
+    g_broker_connected = 1
     client.subscribe("/topic/qos0")
 
 # The callback for when a PUBLISH message is received from the server.
 def on_message(client, userdata, msg):
     global g_recv_topic
     global g_recv_data
-    if g_recv_data == "" and  msg.payload == "data":
+    payload = msg.payload.decode()
+    if g_recv_data == "" and  payload == "data":
         client.publish("/topic/qos0", "data_to_esp32")
         g_recv_topic = msg.topic
-        g_recv_data = msg.payload
-    print(msg.topic+" "+str(msg.payload))
+        g_recv_data = payload
+    print(msg.topic+" "+str(payload))
 
 # this is a test case write with tiny-test-fw.
 # to run test cases outside tiny-test-fw,
@@ -78,7 +40,7 @@ if test_fw_path and test_fw_path not in sys.path:
 
 import TinyFW
 import IDF
-
+import DUT
 
 
 
@@ -86,6 +48,8 @@ import IDF
 def test_examples_protocol_mqtt_wss(env, extra_data):
     global g_recv_topic
     global g_recv_data
+    global g_broker_connected
+    broker_url="iot.eclipse.org"
     """
     steps: |
       1. join AP and connects to wss broker
@@ -99,26 +63,34 @@ def test_examples_protocol_mqtt_wss(env, extra_data):
     bin_size = os.path.getsize(binary_file)
     IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size//1024))
     IDF.check_performance("mqtt_websocket_secure_size", bin_size//1024)
-    # 1. start test
+    # 1. start test (and check the environment is healthy)
     dut1.start_app()
+    client = None
     # 2. Test connects to a broker
-    if use_mqtt_client_sketch:
-        mqtt_client_sketch()
-    else:
+    try:
+        ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
+        print("Connected to AP with IP: {}".format(ip_address))
         client = mqtt.Client(transport="websockets")
         client.on_connect = on_connect
         client.on_message = on_message
         client.tls_set(None,
                     None,
                     None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
-        print "Connecting..."
-        client.connect("iot.eclipse.org", 443, 60)
-        print "...done"
-        print "Start Looping..."
-        start = time.time()
-        while (time.time() - start) <= 20:
-            client.loop()
-        print "...done"
+        print("Connecting...")
+        client.connect(broker_url, 443, 60)
+        print("...done")
+    except DUT.ExpectTimeout:
+        raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
+    except:
+        print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
+        raise
+    print("Start Looping...")
+    start = time.time()
+    while (time.time() - start) <= 20:
+        client.loop()
+    print("...done")
+    if g_broker_connected == 0:
+        raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
     # 3. check the message received back from the server
     if g_recv_topic == "/topic/qos0" and g_recv_data == "data" :
         print("PASS: Received correct message")
index 58d5c6e525127918d82746c11c39aa3dd8ae0f2d..1a660931b58f60282994635bcb9b1ff7df810337 100644 (file)
@@ -174,7 +174,11 @@ class IDFDUT(DUT.SerialDUT):
             # Filter out invalid port by our own will be much simpler.
             return [x for x in ports if not cls.INVALID_PORT_PATTERN.search(x)]
 
-        port_hint = espport.decode('utf8')
+        # On MacOs with python3.6: type of espport is already utf8
+        if type(espport) is type(u''):
+            port_hint = espport
+        else:
+            port_hint = espport.decode('utf8')
 
         # If $ESPPORT is a valid port, make it appear first in the list
         if port_hint in ports: