mqtt: example tests refactored to provide descriptive failures, addapted to python23, removed workarround for launching without mqtt-paho package

This commit is contained in:
David Cermak 2018-09-27 11:46:13 +02:00
parent 12b2268ee5
commit 8ce1f55b59
5 changed files with 130 additions and 200 deletions

View file

@ -5,66 +5,29 @@ import time
import socket import socket
import imp import imp
import ssl import ssl
import paho.mqtt.client as mqtt
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
g_recv_data="" g_recv_data=""
g_recv_topic=""
# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface g_broker_connected=0
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()
# The callback for when the client receives a CONNACK response from the server. # The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
global g_broker_connected
print("Connected with result code "+str(rc)) print("Connected with result code "+str(rc))
g_broker_connected = 1
client.subscribe("/topic/qos0") client.subscribe("/topic/qos0")
# The callback for when a PUBLISH message is received from the server. # The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
global g_recv_topic global g_recv_topic
global g_recv_data 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") client.publish("/topic/qos0", "data_to_esp32")
g_recv_topic = msg.topic g_recv_topic = msg.topic
g_recv_data = msg.payload g_recv_data = payload
print(msg.topic+" "+str(msg.payload)) print(msg.topic+" "+str(payload))
# this is a test case write with tiny-test-fw. # this is a test case write with tiny-test-fw.
# to run test cases outside 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 TinyFW
import IDF import IDF
import DUT
@ -84,6 +47,8 @@ import IDF
def test_examples_protocol_mqtt_ssl(env, extra_data): def test_examples_protocol_mqtt_ssl(env, extra_data):
global g_recv_topic global g_recv_topic
global g_recv_data global g_recv_data
global g_broker_connected
broker_url="iot.eclipse.org"
""" """
steps: | steps: |
1. join AP and connects to ssl broker 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) bin_size = os.path.getsize(binary_file)
IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size//1024)) IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("mqtt_ssl_size", 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() dut1.start_app()
client = None
# 2. Test connects to a broker # 2. Test connects to a broker
if use_mqtt_client_sketch: try:
mqtt_client_sketch() ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
else: print("Connected to AP with IP: {}".format(ip_address))
client = mqtt.Client() client = mqtt.Client()
client.on_connect = on_connect client.on_connect = on_connect
client.on_message = on_message client.on_message = on_message
@ -110,15 +76,21 @@ def test_examples_protocol_mqtt_ssl(env, extra_data):
None, None,
None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
client.tls_insecure_set(True) client.tls_insecure_set(True)
print("Connecting...")
print "Connecting..." client.connect(broker_url, 8883, 60)
client.connect("iot.eclipse.org", 8883, 60) print("...done")
print "...done" except DUT.ExpectTimeout:
print "Start Looping..." raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
start = time.time() except:
while (time.time() - start) <= 20: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
client.loop() raise
print "...done" 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 # 3. check the message received back from the server
if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : if g_recv_topic == "/topic/qos0" and g_recv_data == "data" :
print("PASS: Received correct message") print("PASS: Received correct message")

View file

@ -3,9 +3,10 @@ import os
import sys import sys
from socket import * from socket import *
from threading import Thread from threading import Thread
import struct
import time import time
global msgid msgid=-1
def get_my_ip(): def get_my_ip():
s1 = socket(AF_INET, SOCK_DGRAM) s1 = socket(AF_INET, SOCK_DGRAM)
@ -17,14 +18,20 @@ def get_my_ip():
def mqqt_server_sketch(my_ip, port): def mqqt_server_sketch(my_ip, port):
global msgid global msgid
print("Starting the server on {}".format(my_ip)) print("Starting the server on {}".format(my_ip))
s=socket(AF_INET, SOCK_STREAM) s = None
s.settimeout(60) try:
s.bind((my_ip, port)) s=socket(AF_INET, SOCK_STREAM)
s.listen(1) s.settimeout(60)
q,addr=s.accept() s.bind((my_ip, port))
q.settimeout(30) s.listen(1)
print("connection accepted") q,addr=s.accept()
# q.send(g_msg_to_client) 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) data = q.recv(1024)
# check if received initial empty message # check if received initial empty message
print("received from client {}".format(data)) print("received from client {}".format(data))
@ -32,7 +39,7 @@ def mqqt_server_sketch(my_ip, port):
q.send(data) q.send(data)
# try to receive qos1 # try to receive qos1
data = q.recv(1024) 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)) print("received from client {}, msgid: {}".format(data, msgid))
data = bytearray([0x40, 0x02, data[15], data[16]]) data = bytearray([0x40, 0x02, data[15], data[16]])
q.send(data) q.send(data)
@ -50,6 +57,7 @@ if test_fw_path and test_fw_path not in sys.path:
import TinyFW import TinyFW
import IDF 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 = Thread(target = mqqt_server_sketch, args = (host_ip,1883))
thread1.start() thread1.start()
# 2. start the dut test and wait till client gets IP address # 2. start the dut test and wait till client gets IP address
dut1.start_app() dut1.start_app()
# waiting for getting the IP address # waiting for getting the IP address
data = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) try:
# time.sleep(15) 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")) print ("writing to device: {}".format("mqtt://" + host_ip + "\n"))
dut1.write("mqtt://" + host_ip + "\n") dut1.write("mqtt://" + host_ip + "\n")
thread1.join() thread1.join()
print ("Message id received from server: {}".format(msgid)) print ("Message id received from server: {}".format(msgid))
# 3. check the message id was enqueued and then deleted # 3. check the message id was enqueued and then deleted
msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30) 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) 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 # 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)): if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)):
print("PASS: Received correct msg id") print("PASS: Received correct msg id")

View file

@ -1,70 +1,35 @@
from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
import re import re
import os import os
import sys import sys
import time import time
import socket import socket
import imp import imp
import paho.mqtt.client as mqtt
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
g_recv_data="" g_recv_data=""
g_recv_topic=""
# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface g_broker_connected=0
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()
# The callback for when the client receives a CONNACK response from the server. # The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
global g_broker_connected
print("Connected with result code "+str(rc)) print("Connected with result code "+str(rc))
g_broker_connected = 1
client.subscribe("/topic/qos0") client.subscribe("/topic/qos0")
# The callback for when a PUBLISH message is received from the server. # The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
global g_recv_topic global g_recv_topic
global g_recv_data 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") client.publish("/topic/qos0", "data_to_esp32")
g_recv_topic = msg.topic g_recv_topic = msg.topic
g_recv_data = msg.payload g_recv_data = payload
print(msg.topic+" "+str(msg.payload)) print(msg.topic+" "+payload)
# this is a test case write with tiny-test-fw. # this is a test case write with tiny-test-fw.
# to run test cases outside 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 TinyFW
import IDF import IDF
import DUT
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_mqtt_ws(env, extra_data): def test_examples_protocol_mqtt_ws(env, extra_data):
global g_recv_topic global g_recv_topic
global g_recv_data global g_recv_data
global g_broker_connected
broker_url="iot.eclipse.org"
""" """
steps: | steps: |
1. join AP and connects to ws broker 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) bin_size = os.path.getsize(binary_file)
IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size//1024)) IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("mqtt_websocket_size", 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() dut1.start_app()
client = None
# 2. Test connects to a broker # 2. Test connects to a broker
if use_mqtt_client_sketch: try:
mqtt_client_sketch() ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
else: print("Connected to AP with IP: {}".format(ip_address))
client = mqtt.Client(transport="websockets") client = mqtt.Client(transport="websockets")
client.on_connect = on_connect client.on_connect = on_connect
client.on_message = on_message client.on_message = on_message
client.ws_set_options(path="/ws", headers=None) client.ws_set_options(path="/ws", headers=None)
print "Connecting..." print("Connecting...")
client.connect("iot.eclipse.org", 80, 60) client.connect(broker_url, 80, 60)
print "...done" print("...done")
print "Start Looping..." except DUT.ExpectTimeout:
start = time.time() raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
while (time.time() - start) <= 20: except:
client.loop() print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
print "...done" 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 # 3. check the message received back from the server
if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : if g_recv_topic == "/topic/qos0" and g_recv_data == "data" :
print("PASS: Received correct message") print("PASS: Received correct message")

View file

@ -1,3 +1,4 @@
from __future__ import unicode_literals
import re import re
import os import os
import sys import sys
@ -5,68 +6,29 @@ import time
import socket import socket
import imp import imp
import ssl import ssl
import paho.mqtt.client as mqtt
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
g_recv_data="" g_recv_data=""
g_recv_topic=""
# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface g_broker_connected=0
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()
# The callback for when the client receives a CONNACK response from the server. # The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
global g_broker_connected
print("Connected with result code "+str(rc)) print("Connected with result code "+str(rc))
g_broker_connected = 1
client.subscribe("/topic/qos0") client.subscribe("/topic/qos0")
# The callback for when a PUBLISH message is received from the server. # The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
global g_recv_topic global g_recv_topic
global g_recv_data 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") client.publish("/topic/qos0", "data_to_esp32")
g_recv_topic = msg.topic g_recv_topic = msg.topic
g_recv_data = msg.payload g_recv_data = payload
print(msg.topic+" "+str(msg.payload)) print(msg.topic+" "+str(payload))
# this is a test case write with tiny-test-fw. # this is a test case write with tiny-test-fw.
# to run test cases outside 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 TinyFW
import IDF import IDF
import DUT
@ -86,6 +48,8 @@ import IDF
def test_examples_protocol_mqtt_wss(env, extra_data): def test_examples_protocol_mqtt_wss(env, extra_data):
global g_recv_topic global g_recv_topic
global g_recv_data global g_recv_data
global g_broker_connected
broker_url="iot.eclipse.org"
""" """
steps: | steps: |
1. join AP and connects to wss broker 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) bin_size = os.path.getsize(binary_file)
IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size//1024)) IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("mqtt_websocket_secure_size", 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() dut1.start_app()
client = None
# 2. Test connects to a broker # 2. Test connects to a broker
if use_mqtt_client_sketch: try:
mqtt_client_sketch() ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30)
else: print("Connected to AP with IP: {}".format(ip_address))
client = mqtt.Client(transport="websockets") client = mqtt.Client(transport="websockets")
client.on_connect = on_connect client.on_connect = on_connect
client.on_message = on_message client.on_message = on_message
client.tls_set(None, client.tls_set(None,
None, None,
None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
print "Connecting..." print("Connecting...")
client.connect("iot.eclipse.org", 443, 60) client.connect(broker_url, 443, 60)
print "...done" print("...done")
print "Start Looping..." except DUT.ExpectTimeout:
start = time.time() raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
while (time.time() - start) <= 20: except:
client.loop() print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0]))
print "...done" 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 # 3. check the message received back from the server
if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : if g_recv_topic == "/topic/qos0" and g_recv_data == "data" :
print("PASS: Received correct message") print("PASS: Received correct message")

View file

@ -174,7 +174,11 @@ class IDFDUT(DUT.SerialDUT):
# Filter out invalid port by our own will be much simpler. # 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)] 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 $ESPPORT is a valid port, make it appear first in the list
if port_hint in ports: if port_hint in ports: