2015-09-27 19:17:42 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
import asyncio
|
2015-09-27 19:39:43 +00:00
|
|
|
import os
|
2015-09-27 19:17:42 +00:00
|
|
|
import logging
|
2021-01-10 15:07:02 +00:00
|
|
|
import urllib.request
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
2021-03-06 17:59:03 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2015-10-07 20:42:04 +00:00
|
|
|
from hbmqtt.client import MQTTClient, ConnectException
|
2016-05-31 19:40:10 +00:00
|
|
|
from hbmqtt.broker import Broker
|
2017-08-06 22:46:15 +00:00
|
|
|
from hbmqtt.mqtt.constants import QOS_0, QOS_1, QOS_2
|
2015-09-27 19:17:42 +00:00
|
|
|
|
|
|
|
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
2017-06-02 19:17:05 +00:00
|
|
|
logging.basicConfig(level=logging.ERROR, format=formatter)
|
2015-09-27 19:17:42 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-05-31 19:40:10 +00:00
|
|
|
broker_config = {
|
|
|
|
'listeners': {
|
|
|
|
'default': {
|
|
|
|
'type': 'tcp',
|
2017-11-17 09:11:26 +00:00
|
|
|
'bind': '127.0.0.1:1883',
|
2016-05-31 19:40:10 +00:00
|
|
|
'max_connections': 10
|
|
|
|
},
|
|
|
|
'ws': {
|
|
|
|
'type': 'ws',
|
2017-11-17 09:11:26 +00:00
|
|
|
'bind': '127.0.0.1:8080',
|
2016-05-31 19:40:10 +00:00
|
|
|
'max_connections': 10
|
|
|
|
},
|
2017-06-02 14:53:58 +00:00
|
|
|
'wss': {
|
|
|
|
'type': 'ws',
|
2017-11-17 09:11:26 +00:00
|
|
|
'bind': '127.0.0.1:8081',
|
2017-06-02 14:53:58 +00:00
|
|
|
'max_connections': 10
|
|
|
|
},
|
2016-05-31 19:40:10 +00:00
|
|
|
},
|
|
|
|
'sys_interval': 0,
|
|
|
|
'auth': {
|
|
|
|
'allow-anonymous': True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:59:03 +00:00
|
|
|
ca_file: str = ""
|
|
|
|
temp_dir: str = ""
|
|
|
|
|
|
|
|
|
|
|
|
def setup_module():
|
|
|
|
global ca_file, temp_dir
|
|
|
|
|
|
|
|
temp_dir = tempfile.mkdtemp(prefix='hbmqtt-test-')
|
|
|
|
url = "http://test.mosquitto.org/ssl/mosquitto.org.crt"
|
|
|
|
ca_file = os.path.join(temp_dir, 'mosquitto.org.crt')
|
|
|
|
urllib.request.urlretrieve(url, ca_file)
|
|
|
|
log.info("stored mosquitto cert at %s" % ca_file)
|
|
|
|
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
shutil.rmtree(temp_dir)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_connect_tcp():
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('mqtt://test.mosquitto.org/')
|
|
|
|
assert client.session is not None
|
|
|
|
await client.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_connect_tcp_secure():
|
|
|
|
client = MQTTClient(config={'check_hostname': False})
|
|
|
|
await client.connect('mqtts://test.mosquitto.org/', cafile=ca_file)
|
|
|
|
assert client.session is not None
|
|
|
|
await client.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_connect_tcp_failure():
|
|
|
|
config = {'auto_reconnect': False}
|
|
|
|
client = MQTTClient(config=config)
|
|
|
|
with pytest.raises(ConnectException):
|
|
|
|
await client.connect('mqtt://127.0.0.1/')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_connect_ws():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('ws://127.0.0.1:8080/')
|
|
|
|
assert client.session is not None
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_reconnect_ws_retain_username_password():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('ws://fred:password@127.0.0.1:8080/')
|
|
|
|
assert client.session is not None
|
|
|
|
await client.disconnect()
|
|
|
|
await client.reconnect()
|
|
|
|
|
|
|
|
assert client.session.username is not None
|
|
|
|
assert client.session.password is not None
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_connect_ws_secure():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('ws://127.0.0.1:8081/', cafile=ca_file)
|
|
|
|
assert client.session is not None
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_ping():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('mqtt://127.0.0.1/')
|
|
|
|
assert client.session is not None
|
|
|
|
await client.ping()
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_subscribe():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('mqtt://127.0.0.1/')
|
|
|
|
assert client.session is not None
|
|
|
|
ret = await client.subscribe([
|
|
|
|
('$SYS/broker/uptime', QOS_0),
|
|
|
|
('$SYS/broker/uptime', QOS_1),
|
|
|
|
('$SYS/broker/uptime', QOS_2),
|
|
|
|
])
|
|
|
|
assert ret[0] == QOS_0
|
|
|
|
assert ret[1] == QOS_1
|
|
|
|
assert ret[2] == QOS_2
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_unsubscribe():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('mqtt://127.0.0.1/')
|
|
|
|
assert client.session is not None
|
|
|
|
ret = await client.subscribe([
|
|
|
|
('$SYS/broker/uptime', QOS_0),
|
|
|
|
])
|
|
|
|
assert ret[0] == QOS_0
|
|
|
|
await client.unsubscribe(['$SYS/broker/uptime'])
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_deliver():
|
|
|
|
data = b'data'
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('mqtt://127.0.0.1/')
|
|
|
|
assert client.session is not None
|
|
|
|
ret = await client.subscribe([
|
|
|
|
('test_topic', QOS_0),
|
|
|
|
])
|
|
|
|
assert ret[0] == QOS_0
|
|
|
|
client_pub = MQTTClient()
|
|
|
|
await client_pub.connect('mqtt://127.0.0.1/')
|
|
|
|
await client_pub.publish('test_topic', data, QOS_0)
|
|
|
|
await client_pub.disconnect()
|
|
|
|
message = await client.deliver_message()
|
|
|
|
assert message is not None
|
|
|
|
assert message.publish_packet is not None
|
|
|
|
assert message.data == data
|
|
|
|
await client.unsubscribe(['$SYS/broker/uptime'])
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_deliver_timeout():
|
|
|
|
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
await broker.start()
|
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect('mqtt://127.0.0.1/')
|
|
|
|
assert client.session is not None
|
|
|
|
ret = await client.subscribe([
|
|
|
|
('test_topic', QOS_0),
|
|
|
|
])
|
|
|
|
assert ret[0] == QOS_0
|
|
|
|
with pytest.raises(asyncio.TimeoutError):
|
|
|
|
await client.deliver_message(timeout=2)
|
|
|
|
await client.unsubscribe(['$SYS/broker/uptime'])
|
|
|
|
await client.disconnect()
|
|
|
|
await broker.shutdown()
|
2015-09-27 19:17:42 +00:00
|
|
|
|