| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from concurrent.futures import Future |
| 5 |
from awscrt import mqtt5, io, http, exceptions |
| 6 |
from test import test_retry_wrapper, NativeResourceTest |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
import uuid |
| 11 |
import time |
| 12 |
|
| 13 |
""" |
| 14 |
# Environmental variables used by mqtt5 bindings tests |
| 15 |
|
| 16 |
# AWS_TEST_MQTT5_DIRECT_MQTT_HOST - host to connect to in direct mqtt tests |
| 17 |
# AWS_TEST_MQTT5_DIRECT_MQTT_PORT - port to connect to in direct mqtt tests |
| 18 |
# AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST - host to connect to in direct mqtt basic authentication tests |
| 19 |
# AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT - port to connect to in direct mqtt basic authentication tests |
| 20 |
# AWS_TEST_MQTT5_DIRECT_MQTT_TLS_HOST - host to connect to in direct mqtt tls tests |
| 21 |
# AWS_TEST_MQTT5_DIRECT_MQTT_TLS_PORT - port to connect to in direct mqtt tls tests |
| 22 |
# AWS_TEST_MQTT5_WS_MQTT_HOST - host to connect to in websocket tests |
| 23 |
# AWS_TEST_MQTT5_WS_MQTT_PORT - port to connect to in websocket tests |
| 24 |
# AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_HOST - host to connect to in websocket basic authentication tests |
| 25 |
# AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_PORT - port to connect to in websocket basic authentication tests |
| 26 |
# AWS_TEST_MQTT5_WS_MQTT_TLS_HOST - host to connect to in websocket tls tests |
| 27 |
# AWS_TEST_MQTT5_WS_MQTT_TLS_PORT - port to connect to in websocket tls tests |
| 28 |
# AWS_TEST_MQTT5_IOT_CORE_HOST - host to connect to in MTLS tests |
| 29 |
# AWS_TEST_MQTT5_BASIC_AUTH_USERNAME - username to use in basic authentication tests |
| 30 |
# AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD - password to use in basic authentication tests |
| 31 |
# AWS_TEST_MQTT5_PROXY_HOST - name of http proxy host to use in proxy-based tests |
| 32 |
# AWS_TEST_MQTT5_PROXY_PORT - port of http proxy host to use in proxy-based tests |
| 33 |
# AWS_TEST_MQTT5_CERTIFICATE_FILE - certificate file path |
| 34 |
# AWS_TEST_MQTT5_KEY_FILE - private key file path |
| 35 |
# AWS_TEST_MQTT5_IOT_KEY_PATH - private key file path for MTLS tests |
| 36 |
# AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH - certificate file path for MTLS tests |
| 37 |
|
| 38 |
""" |
| 39 |
|
| 40 |
TIMEOUT = 100.0 |
| 41 |
|
| 42 |
|
| 43 |
def _get_env_variable(env_name): |
| 44 |
env_data = os.environ.get(env_name) |
| 45 |
if not env_data: |
| 46 |
raise unittest.SkipTest(f"test requires env var: {env_name}") |
| 47 |
return env_data |
| 48 |
|
| 49 |
|
| 50 |
def create_client_id(): |
| 51 |
return f"aws-crt-python-unit-test-{uuid.uuid4()}" |
| 52 |
|
| 53 |
|
| 54 |
class Mqtt5TestCallbacks(): |
| 55 |
def __init__(self): |
| 56 |
self.on_publish_receive_expected = 0 |
| 57 |
self.on_publish_received_counter = 0 |
| 58 |
self.last_exception = None |
| 59 |
|
| 60 |
self.negotiated_settings = None |
| 61 |
self.connack_packet = None |
| 62 |
self.publish_packet = None |
| 63 |
|
| 64 |
self.future_connection_success = Future() |
| 65 |
self.future_stopped = Future() |
| 66 |
self.future_connection_failure = Future() |
| 67 |
self.future_disconnection = Future() |
| 68 |
self.future_publish_received = Future() |
| 69 |
self.future_expected_publishes_received = Future() |
| 70 |
|
| 71 |
def ws_handshake_transform(self, transform_args): |
| 72 |
transform_args.set_done() |
| 73 |
|
| 74 |
def on_publish_received(self, publish_received_data: mqtt5.PublishReceivedData): |
| 75 |
self.on_publish_received_counter += 1 |
| 76 |
if self.future_publish_received and not self.future_publish_received.done(): |
| 77 |
self.future_publish_received.set_result(publish_received_data.publish_packet) |
| 78 |
|
| 79 |
if self.on_publish_receive_expected > 0 and self.on_publish_received_counter == self.on_publish_receive_expected and not self.future_expected_publishes_received.done(): |
| 80 |
self.future_expected_publishes_received.set_result(True) |
| 81 |
|
| 82 |
def on_lifecycle_stopped(self, lifecycle_stopped: mqtt5.LifecycleStoppedData): |
| 83 |
if self.future_stopped: |
| 84 |
self.future_stopped.set_result(None) |
| 85 |
|
| 86 |
def on_lifecycle_attempting_connect(self, lifecycle_attempting_connect: mqtt5.LifecycleAttemptingConnectData): |
| 87 |
pass |
| 88 |
|
| 89 |
def on_lifecycle_connection_success(self, lifecycle_connection_success: mqtt5.LifecycleConnectSuccessData): |
| 90 |
self.negotiated_settings = lifecycle_connection_success.negotiated_settings |
| 91 |
self.connack_packet = lifecycle_connection_success.connack_packet |
| 92 |
if self.future_connection_success: |
| 93 |
self.future_connection_success.set_result(lifecycle_connection_success) |
| 94 |
|
| 95 |
def on_lifecycle_connection_failure(self, lifecycle_connection_failure: mqtt5.LifecycleConnectFailureData): |
| 96 |
self.last_exception = lifecycle_connection_failure.exception |
| 97 |
if self.future_connection_failure: |
| 98 |
if self.future_connection_failure.done(): |
| 99 |
pass |
| 100 |
else: |
| 101 |
self.future_connection_failure.set_result(lifecycle_connection_failure) |
| 102 |
|
| 103 |
def on_lifecycle_disconnection(self, lifecycle_disconnect_data: mqtt5.LifecycleDisconnectData): |
| 104 |
if self.future_disconnection: |
| 105 |
self.future_disconnection.set_result(lifecycle_disconnect_data) |
| 106 |
|
| 107 |
|
| 108 |
MAX_RETRIES = 5 |
| 109 |
|
| 110 |
|
| 111 |
class Mqtt5ClientTest(NativeResourceTest): |
| 112 |
|
| 113 |
def _create_tls_context(self): |
| 114 |
cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 115 |
key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 116 |
return io.ClientTlsContext( |
| 117 |
io.TlsContextOptions.create_client_with_mtls_from_path(cert, key)) |
| 118 |
|
| 119 |
def _create_client( |
| 120 |
self, |
| 121 |
client_options: mqtt5.ClientOptions = None, |
| 122 |
callbacks: Mqtt5TestCallbacks = None): |
| 123 |
|
| 124 |
default_host = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 125 |
|
| 126 |
if client_options is None: |
| 127 |
client_options = mqtt5.ClientOptions( |
| 128 |
host_name=default_host, |
| 129 |
port=8883, |
| 130 |
tls_ctx=self._create_tls_context()) |
| 131 |
|
| 132 |
if (client_options.host_name == _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") and |
| 133 |
client_options.tls_ctx is None): |
| 134 |
client_options.tls_ctx = self._create_tls_context() |
| 135 |
|
| 136 |
if client_options.port is None: |
| 137 |
client_options.port = 8883 |
| 138 |
|
| 139 |
if client_options.connect_options is None: |
| 140 |
client_options.connect_options = mqtt5.ConnectPacket() |
| 141 |
client_options.connect_options.client_id = create_client_id() |
| 142 |
|
| 143 |
if callbacks is not None: |
| 144 |
client_options.on_publish_callback_fn = callbacks.on_publish_received |
| 145 |
client_options.on_lifecycle_event_stopped_fn = callbacks.on_lifecycle_stopped |
| 146 |
client_options.on_lifecycle_event_attempting_connect_fn = callbacks.on_lifecycle_attempting_connect |
| 147 |
client_options.on_lifecycle_event_connection_success_fn = callbacks.on_lifecycle_connection_success |
| 148 |
client_options.on_lifecycle_event_connection_failure_fn = callbacks.on_lifecycle_connection_failure |
| 149 |
client_options.on_lifecycle_event_disconnection_fn = callbacks.on_lifecycle_disconnection |
| 150 |
|
| 151 |
client = mqtt5.Client(client_options) |
| 152 |
return client |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
def test_client_creation_minimum(self): |
| 159 |
client = self._create_client() |
| 160 |
|
| 161 |
def test_client_creation_maximum(self): |
| 162 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 163 |
|
| 164 |
user_properties = [] |
| 165 |
user_properties.append(mqtt5.UserProperty(name="name1", value="value1")) |
| 166 |
user_properties.append(mqtt5.UserProperty(name="name2", value="value2")) |
| 167 |
|
| 168 |
publish_packet = mqtt5.PublishPacket( |
| 169 |
payload="TEST_PAYLOAD", |
| 170 |
qos=mqtt5.QoS.AT_LEAST_ONCE, |
| 171 |
retain=False, |
| 172 |
topic="TEST_TOPIC", |
| 173 |
payload_format_indicator=mqtt5.PayloadFormatIndicator.AWS_MQTT5_PFI_UTF8, |
| 174 |
message_expiry_interval_sec=10, |
| 175 |
topic_alias=1, |
| 176 |
response_topic="TEST_RESPONSE_TOPIC", |
| 177 |
correlation_data="TEST_CORRELATION_DATA", |
| 178 |
content_type="TEST_CONTENT_TYPE", |
| 179 |
user_properties=user_properties |
| 180 |
) |
| 181 |
|
| 182 |
connect_options = mqtt5.ConnectPacket( |
| 183 |
keep_alive_interval_sec=10, |
| 184 |
client_id="TEST_CLIENT", |
| 185 |
username="USERNAME", |
| 186 |
password="PASSWORD", |
| 187 |
session_expiry_interval_sec=100, |
| 188 |
request_response_information=1, |
| 189 |
request_problem_information=1, |
| 190 |
receive_maximum=1000, |
| 191 |
maximum_packet_size=10000, |
| 192 |
will_delay_interval_sec=1000, |
| 193 |
will=publish_packet, |
| 194 |
user_properties=user_properties |
| 195 |
) |
| 196 |
client_options = mqtt5.ClientOptions( |
| 197 |
host_name=input_host_name, |
| 198 |
port=8883, |
| 199 |
connect_options=connect_options, |
| 200 |
session_behavior=mqtt5.ClientSessionBehaviorType.CLEAN, |
| 201 |
extended_validation_and_flow_control_options=mqtt5.ExtendedValidationAndFlowControlOptions.AWS_IOT_CORE_DEFAULTS, |
| 202 |
offline_queue_behavior=mqtt5.ClientOperationQueueBehaviorType.FAIL_ALL_ON_DISCONNECT, |
| 203 |
retry_jitter_mode=mqtt5.ExponentialBackoffJitterMode.DECORRELATED, |
| 204 |
min_reconnect_delay_ms=100, |
| 205 |
max_reconnect_delay_ms=50000, |
| 206 |
min_connected_time_to_reset_reconnect_delay_ms=1000, |
| 207 |
ping_timeout_ms=1000, |
| 208 |
connack_timeout_ms=1000, |
| 209 |
ack_timeout_sec=100) |
| 210 |
client = self._create_client(client_options=client_options) |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
def _test_direct_connect_minimum(self): |
| 217 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 218 |
|
| 219 |
client_options = mqtt5.ClientOptions( |
| 220 |
host_name=input_host_name, |
| 221 |
disable_metrics=True |
| 222 |
) |
| 223 |
callbacks = Mqtt5TestCallbacks() |
| 224 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 225 |
client.start() |
| 226 |
callbacks.future_connection_success.result(TIMEOUT) |
| 227 |
client.stop() |
| 228 |
callbacks.future_stopped.result(TIMEOUT) |
| 229 |
|
| 230 |
def test_direct_connect_minimum(self): |
| 231 |
test_retry_wrapper(self._test_direct_connect_minimum) |
| 232 |
|
| 233 |
def _test_direct_connect_basic_auth(self): |
| 234 |
input_username = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_USERNAME") |
| 235 |
input_password = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD") |
| 236 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST") |
| 237 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT")) |
| 238 |
|
| 239 |
connect_options = mqtt5.ConnectPacket( |
| 240 |
client_id=create_client_id(), |
| 241 |
username=input_username, |
| 242 |
password=input_password |
| 243 |
) |
| 244 |
client_options = mqtt5.ClientOptions( |
| 245 |
host_name=input_host_name, |
| 246 |
port=input_port, |
| 247 |
connect_options=connect_options, |
| 248 |
disable_metrics=True |
| 249 |
) |
| 250 |
callbacks = Mqtt5TestCallbacks() |
| 251 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 252 |
client.start() |
| 253 |
callbacks.future_connection_success.result(TIMEOUT) |
| 254 |
client.stop() |
| 255 |
callbacks.future_stopped.result(TIMEOUT) |
| 256 |
|
| 257 |
def test_direct_connect_basic_auth(self): |
| 258 |
test_retry_wrapper(self._test_direct_connect_basic_auth) |
| 259 |
|
| 260 |
def _test_direct_connect_tls(self): |
| 261 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_TLS_HOST") |
| 262 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_TLS_PORT")) |
| 263 |
|
| 264 |
client_options = mqtt5.ClientOptions( |
| 265 |
host_name=input_host_name, |
| 266 |
port=input_port |
| 267 |
) |
| 268 |
tls_ctx_options = io.TlsContextOptions() |
| 269 |
tls_ctx_options.verify_peer = False |
| 270 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 271 |
|
| 272 |
callbacks = Mqtt5TestCallbacks() |
| 273 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 274 |
client.start() |
| 275 |
callbacks.future_connection_success.result(TIMEOUT) |
| 276 |
client.stop() |
| 277 |
callbacks.future_stopped.result(TIMEOUT) |
| 278 |
|
| 279 |
def test_direct_connect_tls(self): |
| 280 |
test_retry_wrapper(self._test_direct_connect_tls) |
| 281 |
|
| 282 |
def _test_direct_connect_mutual_tls(self): |
| 283 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 284 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 285 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 286 |
|
| 287 |
client_options = mqtt5.ClientOptions( |
| 288 |
host_name=input_host_name, |
| 289 |
port=8883 |
| 290 |
) |
| 291 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 292 |
input_cert, |
| 293 |
input_key |
| 294 |
) |
| 295 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 296 |
|
| 297 |
callbacks = Mqtt5TestCallbacks() |
| 298 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 299 |
client.start() |
| 300 |
callbacks.future_connection_success.result(TIMEOUT) |
| 301 |
client.stop() |
| 302 |
callbacks.future_stopped.result(TIMEOUT) |
| 303 |
|
| 304 |
def test_direct_connect_mutual_tls(self): |
| 305 |
test_retry_wrapper(self._test_direct_connect_mutual_tls) |
| 306 |
|
| 307 |
def _test_direct_connect_mutual_tls13(self): |
| 308 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_TLS13_HOST") |
| 309 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 310 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 311 |
|
| 312 |
client_options = mqtt5.ClientOptions( |
| 313 |
host_name=input_host_name, |
| 314 |
port=8883 |
| 315 |
) |
| 316 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 317 |
input_cert, |
| 318 |
input_key |
| 319 |
) |
| 320 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 321 |
|
| 322 |
callbacks = Mqtt5TestCallbacks() |
| 323 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 324 |
client.start() |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if sys.platform == 'darwin' and not os.environ.get('AWS_CRT_USE_NON_FIPS_TLS_13'): |
| 329 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 330 |
else: |
| 331 |
callbacks.future_connection_success.result(TIMEOUT) |
| 332 |
|
| 333 |
client.stop() |
| 334 |
callbacks.future_stopped.result(TIMEOUT) |
| 335 |
|
| 336 |
def test_direct_connect_mutual_tls13(self): |
| 337 |
test_retry_wrapper(self._test_direct_connect_mutual_tls13) |
| 338 |
|
| 339 |
def _test_direct_connect_http_proxy_tls(self): |
| 340 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_TLS_HOST") |
| 341 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_TLS_PORT")) |
| 342 |
input_proxy_host = _get_env_variable("AWS_TEST_MQTT5_PROXY_HOST") |
| 343 |
input_proxy_port = int(_get_env_variable("AWS_TEST_MQTT5_PROXY_PORT")) |
| 344 |
|
| 345 |
client_options = mqtt5.ClientOptions( |
| 346 |
host_name=input_host_name, |
| 347 |
port=input_port |
| 348 |
) |
| 349 |
tls_ctx_options = io.TlsContextOptions() |
| 350 |
tls_ctx_options.verify_peer = False |
| 351 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 352 |
|
| 353 |
http_proxy_options = http.HttpProxyOptions( |
| 354 |
host_name=input_proxy_host, |
| 355 |
port=input_proxy_port |
| 356 |
) |
| 357 |
http_proxy_options.connection_type = http.HttpProxyConnectionType.Tunneling |
| 358 |
http_proxy_options.auth_type = http.HttpProxyAuthenticationType.Nothing |
| 359 |
client_options.http_proxy_options = http_proxy_options |
| 360 |
|
| 361 |
callbacks = Mqtt5TestCallbacks() |
| 362 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 363 |
client.start() |
| 364 |
callbacks.future_connection_success.result(TIMEOUT) |
| 365 |
client.stop() |
| 366 |
callbacks.future_stopped.result(TIMEOUT) |
| 367 |
|
| 368 |
def test_direct_connect_http_proxy_tls(self): |
| 369 |
test_retry_wrapper(self._test_direct_connect_http_proxy_tls) |
| 370 |
|
| 371 |
def _test_direct_connect_maximum(self): |
| 372 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 373 |
|
| 374 |
user_properties = [] |
| 375 |
user_properties.append(mqtt5.UserProperty(name="name1", value="value1")) |
| 376 |
user_properties.append(mqtt5.UserProperty(name="name2", value="value2")) |
| 377 |
|
| 378 |
publish_packet = mqtt5.PublishPacket( |
| 379 |
payload="TEST_PAYLOAD", |
| 380 |
qos=mqtt5.QoS.AT_LEAST_ONCE, |
| 381 |
retain=False, |
| 382 |
topic="TEST_TOPIC", |
| 383 |
payload_format_indicator=mqtt5.PayloadFormatIndicator.AWS_MQTT5_PFI_UTF8, |
| 384 |
message_expiry_interval_sec=10, |
| 385 |
topic_alias=1, |
| 386 |
response_topic="TEST_RESPONSE_TOPIC", |
| 387 |
correlation_data="TEST_CORRELATION_DATA", |
| 388 |
content_type="TEST_CONTENT_TYPE", |
| 389 |
user_properties=user_properties |
| 390 |
) |
| 391 |
connect_options = mqtt5.ConnectPacket( |
| 392 |
keep_alive_interval_sec=10, |
| 393 |
client_id=create_client_id(), |
| 394 |
session_expiry_interval_sec=100, |
| 395 |
request_response_information=1, |
| 396 |
request_problem_information=1, |
| 397 |
receive_maximum=1000, |
| 398 |
maximum_packet_size=10000, |
| 399 |
will_delay_interval_sec=1000, |
| 400 |
will=publish_packet, |
| 401 |
user_properties=user_properties |
| 402 |
) |
| 403 |
client_options = mqtt5.ClientOptions( |
| 404 |
host_name=input_host_name, |
| 405 |
port=8883, |
| 406 |
connect_options=connect_options, |
| 407 |
session_behavior=mqtt5.ClientSessionBehaviorType.CLEAN, |
| 408 |
extended_validation_and_flow_control_options=mqtt5.ExtendedValidationAndFlowControlOptions.AWS_IOT_CORE_DEFAULTS, |
| 409 |
offline_queue_behavior=mqtt5.ClientOperationQueueBehaviorType.FAIL_ALL_ON_DISCONNECT, |
| 410 |
retry_jitter_mode=mqtt5.ExponentialBackoffJitterMode.DECORRELATED, |
| 411 |
min_reconnect_delay_ms=100, |
| 412 |
max_reconnect_delay_ms=50000, |
| 413 |
min_connected_time_to_reset_reconnect_delay_ms=1000, |
| 414 |
ping_timeout_ms=1000, |
| 415 |
connack_timeout_ms=1000, |
| 416 |
ack_timeout_sec=100) |
| 417 |
|
| 418 |
callbacks = Mqtt5TestCallbacks() |
| 419 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 420 |
client.start() |
| 421 |
callbacks.future_connection_success.result(TIMEOUT) |
| 422 |
client.stop() |
| 423 |
callbacks.future_stopped.result(TIMEOUT) |
| 424 |
|
| 425 |
def test_direct_connect_maximum(self): |
| 426 |
test_retry_wrapper(self._test_direct_connect_maximum) |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
def _test_websocket_connect_minimum(self): |
| 433 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_HOST") |
| 434 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_PORT")) |
| 435 |
|
| 436 |
client_options = mqtt5.ClientOptions( |
| 437 |
host_name=input_host_name, |
| 438 |
port=input_port |
| 439 |
) |
| 440 |
callbacks = Mqtt5TestCallbacks() |
| 441 |
client_options.websocket_handshake_transform = callbacks.ws_handshake_transform |
| 442 |
|
| 443 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 444 |
client.start() |
| 445 |
callbacks.future_connection_success.result(TIMEOUT) |
| 446 |
client.stop() |
| 447 |
callbacks.future_stopped.result(TIMEOUT) |
| 448 |
|
| 449 |
def test_websocket_connect_minimum(self): |
| 450 |
test_retry_wrapper(self._test_websocket_connect_minimum) |
| 451 |
|
| 452 |
def _test_websocket_connect_basic_auth(self): |
| 453 |
input_username = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_USERNAME") |
| 454 |
input_password = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD") |
| 455 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_HOST") |
| 456 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_PORT")) |
| 457 |
|
| 458 |
connect_options = mqtt5.ConnectPacket( |
| 459 |
client_id=create_client_id(), |
| 460 |
username=input_username, |
| 461 |
password=input_password |
| 462 |
) |
| 463 |
client_options = mqtt5.ClientOptions( |
| 464 |
host_name=input_host_name, |
| 465 |
port=input_port, |
| 466 |
connect_options=connect_options, |
| 467 |
disable_metrics=True |
| 468 |
) |
| 469 |
callbacks = Mqtt5TestCallbacks() |
| 470 |
client_options.websocket_handshake_transform = callbacks.ws_handshake_transform |
| 471 |
|
| 472 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 473 |
client.start() |
| 474 |
callbacks.future_connection_success.result(TIMEOUT) |
| 475 |
client.stop() |
| 476 |
callbacks.future_stopped.result(TIMEOUT) |
| 477 |
|
| 478 |
def test_websocket_connect_basic_auth(self): |
| 479 |
test_retry_wrapper(self._test_websocket_connect_basic_auth) |
| 480 |
|
| 481 |
def _test_websocket_connect_tls(self): |
| 482 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_TLS_HOST") |
| 483 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_TLS_PORT")) |
| 484 |
|
| 485 |
client_options = mqtt5.ClientOptions( |
| 486 |
host_name=input_host_name, |
| 487 |
port=input_port |
| 488 |
) |
| 489 |
tls_ctx_options = io.TlsContextOptions() |
| 490 |
tls_ctx_options.verify_peer = False |
| 491 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 492 |
callbacks = Mqtt5TestCallbacks() |
| 493 |
client_options.websocket_handshake_transform = callbacks.ws_handshake_transform |
| 494 |
|
| 495 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 496 |
client.start() |
| 497 |
callbacks.future_connection_success.result(TIMEOUT) |
| 498 |
client.stop() |
| 499 |
callbacks.future_stopped.result(TIMEOUT) |
| 500 |
|
| 501 |
def test_websocket_connect_tls(self): |
| 502 |
test_retry_wrapper(self._test_websocket_connect_tls) |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
def _test_websocket_connect_http_proxy_tls(self): |
| 507 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_TLS_HOST") |
| 508 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_TLS_PORT")) |
| 509 |
input_proxy_host = _get_env_variable("AWS_TEST_MQTT5_PROXY_HOST") |
| 510 |
input_proxy_port = int(_get_env_variable("AWS_TEST_MQTT5_PROXY_PORT")) |
| 511 |
|
| 512 |
client_options = mqtt5.ClientOptions( |
| 513 |
host_name=input_host_name, |
| 514 |
port=input_port |
| 515 |
) |
| 516 |
tls_ctx_options = io.TlsContextOptions() |
| 517 |
tls_ctx_options.verify_peer = False |
| 518 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 519 |
|
| 520 |
http_proxy_options = http.HttpProxyOptions( |
| 521 |
host_name=input_proxy_host, |
| 522 |
port=input_proxy_port |
| 523 |
) |
| 524 |
http_proxy_options.connection_type = http.HttpProxyConnectionType.Tunneling |
| 525 |
http_proxy_options.auth_type = http.HttpProxyAuthenticationType.Nothing |
| 526 |
client_options.http_proxy_options = http_proxy_options |
| 527 |
|
| 528 |
callbacks = Mqtt5TestCallbacks() |
| 529 |
client_options.websocket_handshake_transform = callbacks.ws_handshake_transform |
| 530 |
|
| 531 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 532 |
client.start() |
| 533 |
callbacks.future_connection_success.result(TIMEOUT) |
| 534 |
client.stop() |
| 535 |
callbacks.future_stopped.result(TIMEOUT) |
| 536 |
|
| 537 |
def test_websocket_connect_http_proxy_tls(self): |
| 538 |
test_retry_wrapper(self._test_websocket_connect_http_proxy_tls) |
| 539 |
|
| 540 |
def _test_websocket_connect_maximum(self): |
| 541 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_HOST") |
| 542 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_PORT")) |
| 543 |
|
| 544 |
user_properties = [] |
| 545 |
user_properties.append(mqtt5.UserProperty(name="name1", value="value1")) |
| 546 |
user_properties.append(mqtt5.UserProperty(name="name2", value="value2")) |
| 547 |
|
| 548 |
publish_packet = mqtt5.PublishPacket( |
| 549 |
payload="TEST_PAYLOAD", |
| 550 |
qos=mqtt5.QoS.AT_LEAST_ONCE, |
| 551 |
retain=False, |
| 552 |
topic="TEST_TOPIC", |
| 553 |
payload_format_indicator=mqtt5.PayloadFormatIndicator.AWS_MQTT5_PFI_UTF8, |
| 554 |
message_expiry_interval_sec=10, |
| 555 |
topic_alias=1, |
| 556 |
response_topic="TEST_RESPONSE_TOPIC", |
| 557 |
correlation_data="TEST_CORRELATION_DATA", |
| 558 |
content_type="TEST_CONTENT_TYPE", |
| 559 |
user_properties=user_properties |
| 560 |
) |
| 561 |
connect_options = mqtt5.ConnectPacket( |
| 562 |
keep_alive_interval_sec=10, |
| 563 |
client_id=create_client_id(), |
| 564 |
session_expiry_interval_sec=100, |
| 565 |
request_response_information=1, |
| 566 |
request_problem_information=1, |
| 567 |
receive_maximum=1000, |
| 568 |
maximum_packet_size=10000, |
| 569 |
will_delay_interval_sec=1000, |
| 570 |
will=publish_packet, |
| 571 |
user_properties=user_properties |
| 572 |
) |
| 573 |
client_options = mqtt5.ClientOptions( |
| 574 |
host_name=input_host_name, |
| 575 |
port=input_port, |
| 576 |
connect_options=connect_options, |
| 577 |
session_behavior=mqtt5.ClientSessionBehaviorType.CLEAN, |
| 578 |
extended_validation_and_flow_control_options=mqtt5.ExtendedValidationAndFlowControlOptions.AWS_IOT_CORE_DEFAULTS, |
| 579 |
offline_queue_behavior=mqtt5.ClientOperationQueueBehaviorType.FAIL_ALL_ON_DISCONNECT, |
| 580 |
retry_jitter_mode=mqtt5.ExponentialBackoffJitterMode.DECORRELATED, |
| 581 |
min_reconnect_delay_ms=100, |
| 582 |
max_reconnect_delay_ms=50000, |
| 583 |
min_connected_time_to_reset_reconnect_delay_ms=1000, |
| 584 |
ping_timeout_ms=1000, |
| 585 |
connack_timeout_ms=1000, |
| 586 |
ack_timeout_sec=100) |
| 587 |
callbacks = Mqtt5TestCallbacks() |
| 588 |
client_options.websocket_handshake_transform = callbacks.ws_handshake_transform |
| 589 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 590 |
client.start() |
| 591 |
callbacks.future_connection_success.result(TIMEOUT) |
| 592 |
client.stop() |
| 593 |
callbacks.future_stopped.result(TIMEOUT) |
| 594 |
|
| 595 |
def test_websocket_connect_maximum(self): |
| 596 |
test_retry_wrapper(self._test_websocket_connect_maximum) |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
def test_connect_with_invalid_host_name(self): |
| 603 |
client_options = mqtt5.ClientOptions( |
| 604 |
host_name="badhost", |
| 605 |
port=1883 |
| 606 |
) |
| 607 |
callbacks = Mqtt5TestCallbacks() |
| 608 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 609 |
client.start() |
| 610 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 611 |
client.stop() |
| 612 |
callbacks.future_stopped.result(TIMEOUT) |
| 613 |
|
| 614 |
def test_connect_with_invalid_port(self): |
| 615 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 616 |
client_options = mqtt5.ClientOptions( |
| 617 |
host_name=input_host_name, |
| 618 |
port=444 |
| 619 |
) |
| 620 |
callbacks = Mqtt5TestCallbacks() |
| 621 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 622 |
client.start() |
| 623 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 624 |
client.stop() |
| 625 |
callbacks.future_stopped.result(TIMEOUT) |
| 626 |
|
| 627 |
def test_connect_with_invalid_port_for_websocket_connection(self): |
| 628 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 629 |
client_options = mqtt5.ClientOptions( |
| 630 |
host_name=input_host_name, |
| 631 |
port=1883 |
| 632 |
) |
| 633 |
callbacks = Mqtt5TestCallbacks() |
| 634 |
client_options.websocket_handshake_transform = callbacks.ws_handshake_transform |
| 635 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 636 |
client.start() |
| 637 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 638 |
client.stop() |
| 639 |
callbacks.future_stopped.result(TIMEOUT) |
| 640 |
|
| 641 |
def test_connect_with_socket_timeout(self): |
| 642 |
client_options = mqtt5.ClientOptions( |
| 643 |
host_name="www.example.com", |
| 644 |
port=81 |
| 645 |
) |
| 646 |
client_options.connack_timeout_ms = 200 |
| 647 |
callbacks = Mqtt5TestCallbacks() |
| 648 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 649 |
client.start() |
| 650 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 651 |
client.stop() |
| 652 |
callbacks.future_stopped.result(TIMEOUT) |
| 653 |
|
| 654 |
def test_connect_with_incorrect_basic_authentication_credentials(self): |
| 655 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST") |
| 656 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT")) |
| 657 |
|
| 658 |
connect_options = mqtt5.ConnectPacket( |
| 659 |
client_id=create_client_id(), |
| 660 |
username="bad username", |
| 661 |
password="bad password" |
| 662 |
) |
| 663 |
client_options = mqtt5.ClientOptions( |
| 664 |
host_name=input_host_name, |
| 665 |
port=input_port, |
| 666 |
connect_options=connect_options |
| 667 |
) |
| 668 |
callbacks = Mqtt5TestCallbacks() |
| 669 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 670 |
client.start() |
| 671 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 672 |
self.assertEqual(str(callbacks.last_exception), str(exceptions.from_code(5150))) |
| 673 |
client.stop() |
| 674 |
callbacks.future_stopped.result(TIMEOUT) |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
def test_double_client_id_failure(self): |
| 679 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 680 |
shared_client_id = create_client_id() |
| 681 |
|
| 682 |
connect_options = mqtt5.ConnectPacket(client_id=shared_client_id) |
| 683 |
client_options = mqtt5.ClientOptions( |
| 684 |
host_name=input_host_name, |
| 685 |
connect_options=connect_options |
| 686 |
) |
| 687 |
callbacks = Mqtt5TestCallbacks() |
| 688 |
client1 = self._create_client(client_options=client_options, callbacks=callbacks) |
| 689 |
|
| 690 |
callbacks2 = Mqtt5TestCallbacks() |
| 691 |
client2 = self._create_client(client_options=client_options, callbacks=callbacks2) |
| 692 |
|
| 693 |
client1.start() |
| 694 |
callbacks.future_connection_success.result(TIMEOUT) |
| 695 |
|
| 696 |
time.sleep(5) |
| 697 |
|
| 698 |
client2.start() |
| 699 |
|
| 700 |
callbacks.future_disconnection.result(TIMEOUT) |
| 701 |
|
| 702 |
client1.stop() |
| 703 |
callbacks.future_stopped.result(TIMEOUT) |
| 704 |
client2.stop() |
| 705 |
callbacks2.future_stopped.result(TIMEOUT) |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
def test_negative_connect_packet_properties(self): |
| 712 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 713 |
|
| 714 |
connect_options = mqtt5.ConnectPacket( |
| 715 |
keep_alive_interval_sec=-10, |
| 716 |
) |
| 717 |
|
| 718 |
client_options = mqtt5.ClientOptions( |
| 719 |
host_name=input_host_name, |
| 720 |
port=8883, |
| 721 |
connect_options=connect_options, |
| 722 |
) |
| 723 |
|
| 724 |
with self.assertRaises(OverflowError) as cm: |
| 725 |
self._create_client(client_options=client_options) |
| 726 |
|
| 727 |
connect_options = mqtt5.ConnectPacket( |
| 728 |
session_expiry_interval_sec=-100, |
| 729 |
) |
| 730 |
|
| 731 |
client_options = mqtt5.ClientOptions( |
| 732 |
host_name=input_host_name, |
| 733 |
port=8883, |
| 734 |
connect_options=connect_options, |
| 735 |
) |
| 736 |
|
| 737 |
with self.assertRaises(OverflowError) as cm: |
| 738 |
self._create_client(client_options=client_options) |
| 739 |
|
| 740 |
connect_options = mqtt5.ConnectPacket( |
| 741 |
receive_maximum=-1000, |
| 742 |
) |
| 743 |
|
| 744 |
client_options = mqtt5.ClientOptions( |
| 745 |
host_name=input_host_name, |
| 746 |
port=8883, |
| 747 |
connect_options=connect_options, |
| 748 |
) |
| 749 |
|
| 750 |
with self.assertRaises(OverflowError) as cm: |
| 751 |
self._create_client(client_options=client_options) |
| 752 |
|
| 753 |
connect_options = mqtt5.ConnectPacket( |
| 754 |
maximum_packet_size=-10000, |
| 755 |
) |
| 756 |
|
| 757 |
client_options = mqtt5.ClientOptions( |
| 758 |
host_name=input_host_name, |
| 759 |
port=8883, |
| 760 |
connect_options=connect_options, |
| 761 |
) |
| 762 |
|
| 763 |
with self.assertRaises(OverflowError) as cm: |
| 764 |
self._create_client(client_options=client_options) |
| 765 |
|
| 766 |
connect_options = mqtt5.ConnectPacket( |
| 767 |
will_delay_interval_sec=-1000, |
| 768 |
) |
| 769 |
|
| 770 |
client_options = mqtt5.ClientOptions( |
| 771 |
host_name=input_host_name, |
| 772 |
port=8883, |
| 773 |
connect_options=connect_options, |
| 774 |
) |
| 775 |
|
| 776 |
with self.assertRaises(OverflowError) as cm: |
| 777 |
self._create_client(client_options=client_options) |
| 778 |
|
| 779 |
def test_overflow_connect_packet_properties(self): |
| 780 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 781 |
connect_options = mqtt5.ConnectPacket( |
| 782 |
keep_alive_interval_sec=65536 |
| 783 |
) |
| 784 |
|
| 785 |
client_options = mqtt5.ClientOptions( |
| 786 |
host_name=input_host_name, |
| 787 |
port=8883, |
| 788 |
connect_options=connect_options, |
| 789 |
) |
| 790 |
|
| 791 |
with self.assertRaises(OverflowError) as cm: |
| 792 |
self._create_client(client_options=client_options) |
| 793 |
|
| 794 |
connect_options = mqtt5.ConnectPacket( |
| 795 |
session_expiry_interval_sec=4294967296 |
| 796 |
) |
| 797 |
|
| 798 |
client_options = mqtt5.ClientOptions( |
| 799 |
host_name=input_host_name, |
| 800 |
port=8883, |
| 801 |
connect_options=connect_options, |
| 802 |
) |
| 803 |
|
| 804 |
with self.assertRaises(OverflowError) as cm: |
| 805 |
self._create_client(client_options=client_options) |
| 806 |
|
| 807 |
connect_options = mqtt5.ConnectPacket( |
| 808 |
receive_maximum=65536 |
| 809 |
) |
| 810 |
|
| 811 |
client_options = mqtt5.ClientOptions( |
| 812 |
host_name=input_host_name, |
| 813 |
port=8883, |
| 814 |
connect_options=connect_options, |
| 815 |
) |
| 816 |
|
| 817 |
with self.assertRaises(OverflowError) as cm: |
| 818 |
self._create_client(client_options=client_options) |
| 819 |
|
| 820 |
connect_options = mqtt5.ConnectPacket( |
| 821 |
maximum_packet_size=4294967296 |
| 822 |
) |
| 823 |
|
| 824 |
client_options = mqtt5.ClientOptions( |
| 825 |
host_name=input_host_name, |
| 826 |
port=8883, |
| 827 |
connect_options=connect_options, |
| 828 |
) |
| 829 |
|
| 830 |
with self.assertRaises(OverflowError) as cm: |
| 831 |
self._create_client(client_options=client_options) |
| 832 |
|
| 833 |
connect_options = mqtt5.ConnectPacket( |
| 834 |
will_delay_interval_sec=4294967296 |
| 835 |
) |
| 836 |
|
| 837 |
client_options = mqtt5.ClientOptions( |
| 838 |
host_name=input_host_name, |
| 839 |
port=8883, |
| 840 |
connect_options=connect_options, |
| 841 |
) |
| 842 |
|
| 843 |
with self.assertRaises(OverflowError) as cm: |
| 844 |
self._create_client(client_options=client_options) |
| 845 |
|
| 846 |
def _test_negative_disconnect_packet_properties(self): |
| 847 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 848 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 849 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 850 |
|
| 851 |
client_options = mqtt5.ClientOptions( |
| 852 |
host_name=input_host_name, |
| 853 |
port=8883 |
| 854 |
) |
| 855 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 856 |
input_cert, |
| 857 |
input_key |
| 858 |
) |
| 859 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 860 |
callbacks = Mqtt5TestCallbacks() |
| 861 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 862 |
client.start() |
| 863 |
callbacks.future_connection_success.result(TIMEOUT) |
| 864 |
|
| 865 |
disconnect_packet = mqtt5.DisconnectPacket(session_expiry_interval_sec=-1) |
| 866 |
|
| 867 |
with self.assertRaises(OverflowError) as cm: |
| 868 |
client.stop(disconnect_packet=disconnect_packet) |
| 869 |
|
| 870 |
client.stop() |
| 871 |
callbacks.future_stopped.result(TIMEOUT) |
| 872 |
|
| 873 |
def test_negative_disconnect_packet_properties(self): |
| 874 |
test_retry_wrapper(self._test_negative_disconnect_packet_properties) |
| 875 |
|
| 876 |
def _test_negative_publish_packet_properties(self): |
| 877 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 878 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 879 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 880 |
|
| 881 |
client_options = mqtt5.ClientOptions( |
| 882 |
host_name=input_host_name, |
| 883 |
port=8883 |
| 884 |
) |
| 885 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 886 |
input_cert, |
| 887 |
input_key |
| 888 |
) |
| 889 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 890 |
callbacks = Mqtt5TestCallbacks() |
| 891 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 892 |
client.start() |
| 893 |
callbacks.future_connection_success.result(TIMEOUT) |
| 894 |
|
| 895 |
publish_packet = mqtt5.PublishPacket(message_expiry_interval_sec=-1) |
| 896 |
with self.assertRaises(OverflowError) as cm: |
| 897 |
client.publish(publish_packet=publish_packet) |
| 898 |
|
| 899 |
client.stop() |
| 900 |
callbacks.future_stopped.result(TIMEOUT) |
| 901 |
|
| 902 |
def test_negative_publish_packet_properties(self): |
| 903 |
test_retry_wrapper(self._test_negative_publish_packet_properties) |
| 904 |
|
| 905 |
def _test_negative_subscribe_packet_properties(self): |
| 906 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 907 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 908 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 909 |
|
| 910 |
client_options = mqtt5.ClientOptions( |
| 911 |
host_name=input_host_name, |
| 912 |
port=8883 |
| 913 |
) |
| 914 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 915 |
input_cert, |
| 916 |
input_key |
| 917 |
) |
| 918 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 919 |
callbacks = Mqtt5TestCallbacks() |
| 920 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 921 |
client.start() |
| 922 |
callbacks.future_connection_success.result(TIMEOUT) |
| 923 |
|
| 924 |
subscriptions = [] |
| 925 |
subscriptions.append(mqtt5.Subscription(topic_filter="test1", qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 926 |
subscribe_packet = mqtt5.SubscribePacket(subscription_identifier=-1, subscriptions=subscriptions) |
| 927 |
|
| 928 |
with self.assertRaises(OverflowError) as cm: |
| 929 |
client.subscribe(subscribe_packet=subscribe_packet) |
| 930 |
|
| 931 |
client.stop() |
| 932 |
callbacks.future_stopped.result(TIMEOUT) |
| 933 |
|
| 934 |
def test_negative_subscribe_packet_properties(self): |
| 935 |
test_retry_wrapper(self._test_negative_subscribe_packet_properties) |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
def _test_negotiated_settings_minimal_settings(self): |
| 942 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 943 |
|
| 944 |
connect_options = mqtt5.ConnectPacket( |
| 945 |
session_expiry_interval_sec=600000 |
| 946 |
) |
| 947 |
client_options = mqtt5.ClientOptions( |
| 948 |
host_name=input_host_name, |
| 949 |
connect_options=connect_options |
| 950 |
) |
| 951 |
callbacks = Mqtt5TestCallbacks() |
| 952 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 953 |
client.start() |
| 954 |
callbacks.future_connection_success.result(TIMEOUT) |
| 955 |
|
| 956 |
self.assertIsNotNone(callbacks.negotiated_settings) |
| 957 |
self.assertEqual(callbacks.negotiated_settings.session_expiry_interval_sec, 3600) |
| 958 |
|
| 959 |
client.stop() |
| 960 |
callbacks.future_stopped.result(TIMEOUT) |
| 961 |
|
| 962 |
def test_negotiated_settings_minimal_settings(self): |
| 963 |
test_retry_wrapper(self._test_negotiated_settings_minimal_settings) |
| 964 |
|
| 965 |
def _test_negotiated_settings_maximum_settings(self): |
| 966 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 967 |
|
| 968 |
client_id = create_client_id() |
| 969 |
connect_options = mqtt5.ConnectPacket( |
| 970 |
client_id=client_id, |
| 971 |
session_expiry_interval_sec=600, |
| 972 |
keep_alive_interval_sec=1000 |
| 973 |
) |
| 974 |
|
| 975 |
client_options = mqtt5.ClientOptions( |
| 976 |
host_name=input_host_name, |
| 977 |
connect_options=connect_options |
| 978 |
) |
| 979 |
callbacks = Mqtt5TestCallbacks() |
| 980 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 981 |
client.start() |
| 982 |
callbacks.future_connection_success.result(TIMEOUT) |
| 983 |
|
| 984 |
self.assertIsNotNone(callbacks.negotiated_settings) |
| 985 |
self.assertIsNotNone(callbacks.connack_packet) |
| 986 |
self.assertIsNone(callbacks.connack_packet.assigned_client_identifier) |
| 987 |
self.assertEqual(callbacks.negotiated_settings.session_expiry_interval_sec, 600) |
| 988 |
self.assertEqual(callbacks.negotiated_settings.server_keep_alive_sec, 1000) |
| 989 |
self.assertEqual(callbacks.negotiated_settings.maximum_qos, mqtt5.QoS.AT_LEAST_ONCE) |
| 990 |
self.assertEqual(callbacks.negotiated_settings.receive_maximum_from_server, 100) |
| 991 |
self.assertEqual(callbacks.negotiated_settings.maximum_packet_size_to_server, 149504) |
| 992 |
self.assertEqual(callbacks.negotiated_settings.topic_alias_maximum_to_server, 8) |
| 993 |
self.assertEqual(callbacks.negotiated_settings.topic_alias_maximum_to_client, 0) |
| 994 |
self.assertTrue(callbacks.negotiated_settings.retain_available) |
| 995 |
self.assertTrue(callbacks.negotiated_settings.wildcard_subscriptions_available) |
| 996 |
self.assertFalse(callbacks.negotiated_settings.subscription_identifiers_available) |
| 997 |
self.assertTrue(callbacks.negotiated_settings.shared_subscriptions_available) |
| 998 |
self.assertFalse(callbacks.negotiated_settings.rejoined_session) |
| 999 |
self.assertEqual(callbacks.negotiated_settings.client_id, client_id) |
| 1000 |
|
| 1001 |
client.stop() |
| 1002 |
callbacks.future_stopped.result(TIMEOUT) |
| 1003 |
|
| 1004 |
def test_negotiated_settings_maximum_settings(self): |
| 1005 |
test_retry_wrapper(self._test_negotiated_settings_maximum_settings) |
| 1006 |
|
| 1007 |
def _test_negotiated_settings_server_limit(self): |
| 1008 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1009 |
|
| 1010 |
uint32_max = 4294967295 |
| 1011 |
uint16_max = 65535 |
| 1012 |
|
| 1013 |
connect_options = mqtt5.ConnectPacket( |
| 1014 |
session_expiry_interval_sec=uint32_max, |
| 1015 |
receive_maximum=uint16_max, |
| 1016 |
keep_alive_interval_sec=uint16_max, |
| 1017 |
maximum_packet_size=uint32_max |
| 1018 |
) |
| 1019 |
|
| 1020 |
client_options = mqtt5.ClientOptions( |
| 1021 |
host_name=input_host_name, |
| 1022 |
connect_options=connect_options |
| 1023 |
) |
| 1024 |
callbacks = Mqtt5TestCallbacks() |
| 1025 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1026 |
client.start() |
| 1027 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1028 |
|
| 1029 |
self.assertIsNotNone(callbacks.negotiated_settings) |
| 1030 |
self.assertNotEqual(callbacks.negotiated_settings.receive_maximum_from_server, uint16_max) |
| 1031 |
self.assertNotEqual(callbacks.negotiated_settings.maximum_packet_size_to_server, uint32_max) |
| 1032 |
self.assertEqual(callbacks.negotiated_settings.server_keep_alive_sec, 1200) |
| 1033 |
self.assertEqual(callbacks.negotiated_settings.session_expiry_interval_sec, 3600) |
| 1034 |
|
| 1035 |
client.stop() |
| 1036 |
callbacks.future_stopped.result(TIMEOUT) |
| 1037 |
|
| 1038 |
def test_negotiated_settings_server_limit(self): |
| 1039 |
test_retry_wrapper(self._test_negotiated_settings_server_limit) |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
def _test_operation_sub_unsub(self): |
| 1046 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1047 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1048 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1049 |
|
| 1050 |
client_id = create_client_id() |
| 1051 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 1052 |
payload = "Hello World" |
| 1053 |
|
| 1054 |
client_options = mqtt5.ClientOptions( |
| 1055 |
host_name=input_host_name, |
| 1056 |
port=8883 |
| 1057 |
) |
| 1058 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1059 |
input_cert, |
| 1060 |
input_key |
| 1061 |
) |
| 1062 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1063 |
callbacks = Mqtt5TestCallbacks() |
| 1064 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1065 |
client.start() |
| 1066 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1067 |
|
| 1068 |
callbacks.on_publish_receive_expected = 1 |
| 1069 |
|
| 1070 |
subscriptions = [] |
| 1071 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1072 |
subscribe_packet = mqtt5.SubscribePacket( |
| 1073 |
subscriptions=subscriptions) |
| 1074 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 1075 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1076 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1077 |
|
| 1078 |
publish_packet = mqtt5.PublishPacket( |
| 1079 |
payload=payload, |
| 1080 |
topic=topic_filter, |
| 1081 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 1082 |
|
| 1083 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1084 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1085 |
puback_packet = publish_completion_data.puback |
| 1086 |
self.assertIsInstance(puback_packet, mqtt5.PubackPacket) |
| 1087 |
|
| 1088 |
callbacks.future_expected_publishes_received.result(TIMEOUT) |
| 1089 |
|
| 1090 |
topic_filters = [] |
| 1091 |
topic_filters.append(topic_filter) |
| 1092 |
unsubscribe_packet = mqtt5.UnsubscribePacket(topic_filters=topic_filters) |
| 1093 |
unsubscribe_future = client.unsubscribe(unsubscribe_packet) |
| 1094 |
unsuback_packet = unsubscribe_future.result(TIMEOUT) |
| 1095 |
self.assertIsInstance(unsuback_packet, mqtt5.UnsubackPacket) |
| 1096 |
|
| 1097 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1098 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1099 |
puback_packet = publish_completion_data.puback |
| 1100 |
self.assertIsInstance(puback_packet, mqtt5.PubackPacket) |
| 1101 |
|
| 1102 |
self.assertEqual(callbacks.on_publish_received_counter, 1) |
| 1103 |
|
| 1104 |
client.stop() |
| 1105 |
callbacks.future_stopped.result(TIMEOUT) |
| 1106 |
|
| 1107 |
def test_operation_sub_unsub(self): |
| 1108 |
test_retry_wrapper(self._test_operation_sub_unsub) |
| 1109 |
|
| 1110 |
def _test_operation_will(self): |
| 1111 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1112 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1113 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1114 |
|
| 1115 |
client_id_publisher = create_client_id() |
| 1116 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id_publisher |
| 1117 |
|
| 1118 |
will_packet = mqtt5.PublishPacket( |
| 1119 |
payload="TEST WILL", |
| 1120 |
qos=mqtt5.QoS.AT_LEAST_ONCE, |
| 1121 |
topic=topic_filter |
| 1122 |
) |
| 1123 |
|
| 1124 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1125 |
input_cert, |
| 1126 |
input_key |
| 1127 |
) |
| 1128 |
|
| 1129 |
client_options1 = mqtt5.ClientOptions( |
| 1130 |
host_name=input_host_name, |
| 1131 |
port=8883 |
| 1132 |
) |
| 1133 |
client_options1.connect_options = mqtt5.ConnectPacket(client_id=client_id_publisher, |
| 1134 |
will_delay_interval_sec=0, |
| 1135 |
will=will_packet) |
| 1136 |
client_options1.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1137 |
callbacks1 = Mqtt5TestCallbacks() |
| 1138 |
client1 = self._create_client(client_options=client_options1, callbacks=callbacks1) |
| 1139 |
client1.start() |
| 1140 |
callbacks1.future_connection_success.result(TIMEOUT) |
| 1141 |
|
| 1142 |
client_options2 = mqtt5.ClientOptions( |
| 1143 |
host_name=input_host_name, |
| 1144 |
port=8883 |
| 1145 |
) |
| 1146 |
client_options2.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 1147 |
client_options2.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1148 |
callbacks2 = Mqtt5TestCallbacks() |
| 1149 |
client2 = self._create_client(client_options=client_options2, callbacks=callbacks2) |
| 1150 |
client2.start() |
| 1151 |
callbacks2.future_connection_success.result(TIMEOUT) |
| 1152 |
|
| 1153 |
subscriptions = [] |
| 1154 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1155 |
subscribe_packet = mqtt5.SubscribePacket( |
| 1156 |
subscriptions=subscriptions) |
| 1157 |
subscribe_future = client2.subscribe(subscribe_packet=subscribe_packet) |
| 1158 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1159 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1160 |
|
| 1161 |
|
| 1162 |
time.sleep(2) |
| 1163 |
|
| 1164 |
disconnect_packet = mqtt5.DisconnectPacket(reason_code=mqtt5.DisconnectReasonCode.DISCONNECT_WITH_WILL_MESSAGE) |
| 1165 |
client1.stop(disconnect_packet=disconnect_packet) |
| 1166 |
callbacks1.future_stopped.result(TIMEOUT) |
| 1167 |
|
| 1168 |
received_will = callbacks2.future_publish_received.result(TIMEOUT) |
| 1169 |
self.assertIsInstance(received_will, mqtt5.PublishPacket) |
| 1170 |
client2.stop() |
| 1171 |
callbacks2.future_stopped.result(TIMEOUT) |
| 1172 |
|
| 1173 |
def test_operation_will(self): |
| 1174 |
test_retry_wrapper(self._test_operation_will) |
| 1175 |
|
| 1176 |
def _do_will_correlation_data_test(self, outbound_correlation_data_bytes, outbound_correlation_data, |
| 1177 |
expected_correlation_data_bytes, expected_correlation_data): |
| 1178 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1179 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1180 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1181 |
|
| 1182 |
client_id_publisher = create_client_id() |
| 1183 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id_publisher |
| 1184 |
|
| 1185 |
payload = "TEST WILL" |
| 1186 |
payload_bytes = payload.encode("utf-8") |
| 1187 |
|
| 1188 |
will_packet = mqtt5.PublishPacket( |
| 1189 |
payload="TEST WILL", |
| 1190 |
qos=mqtt5.QoS.AT_LEAST_ONCE, |
| 1191 |
topic=topic_filter, |
| 1192 |
correlation_data_bytes=outbound_correlation_data_bytes, |
| 1193 |
correlation_data=outbound_correlation_data |
| 1194 |
) |
| 1195 |
|
| 1196 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1197 |
input_cert, |
| 1198 |
input_key |
| 1199 |
) |
| 1200 |
|
| 1201 |
client_options1 = mqtt5.ClientOptions( |
| 1202 |
host_name=input_host_name, |
| 1203 |
port=8883 |
| 1204 |
) |
| 1205 |
client_options1.connect_options = mqtt5.ConnectPacket(client_id=client_id_publisher, |
| 1206 |
will_delay_interval_sec=0, |
| 1207 |
will=will_packet) |
| 1208 |
client_options1.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1209 |
callbacks1 = Mqtt5TestCallbacks() |
| 1210 |
client1 = self._create_client(client_options=client_options1, callbacks=callbacks1) |
| 1211 |
client1.start() |
| 1212 |
callbacks1.future_connection_success.result(TIMEOUT) |
| 1213 |
|
| 1214 |
client_options2 = mqtt5.ClientOptions( |
| 1215 |
host_name=input_host_name, |
| 1216 |
port=8883 |
| 1217 |
) |
| 1218 |
client_options2.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 1219 |
client_options2.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1220 |
callbacks2 = Mqtt5TestCallbacks() |
| 1221 |
client2 = self._create_client(client_options=client_options2, callbacks=callbacks2) |
| 1222 |
client2.start() |
| 1223 |
callbacks2.future_connection_success.result(TIMEOUT) |
| 1224 |
|
| 1225 |
subscriptions = [] |
| 1226 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1227 |
subscribe_packet = mqtt5.SubscribePacket( |
| 1228 |
subscriptions=subscriptions) |
| 1229 |
subscribe_future = client2.subscribe(subscribe_packet=subscribe_packet) |
| 1230 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1231 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1232 |
|
| 1233 |
disconnect_packet = mqtt5.DisconnectPacket(reason_code=mqtt5.DisconnectReasonCode.DISCONNECT_WITH_WILL_MESSAGE) |
| 1234 |
client1.stop(disconnect_packet=disconnect_packet) |
| 1235 |
callbacks1.future_stopped.result(TIMEOUT) |
| 1236 |
|
| 1237 |
received_will = callbacks2.future_publish_received.result(TIMEOUT) |
| 1238 |
self.assertIsInstance(received_will, mqtt5.PublishPacket) |
| 1239 |
self.assertEqual(received_will.payload, payload_bytes) |
| 1240 |
self.assertEqual(received_will.correlation_data_bytes, expected_correlation_data_bytes) |
| 1241 |
self.assertEqual(received_will.correlation_data, expected_correlation_data) |
| 1242 |
|
| 1243 |
client2.stop() |
| 1244 |
callbacks2.future_stopped.result(TIMEOUT) |
| 1245 |
|
| 1246 |
def _test_will_correlation_data_bytes_binary(self): |
| 1247 |
correlation_data = bytearray(os.urandom(64)) |
| 1248 |
self._do_will_correlation_data_test(correlation_data, None, correlation_data, None) |
| 1249 |
|
| 1250 |
def test_will_correlation_data_bytes_binary(self): |
| 1251 |
test_retry_wrapper(self._test_will_correlation_data_bytes_binary) |
| 1252 |
|
| 1253 |
def _test_will_correlation_data_bytes_string(self): |
| 1254 |
correlation_data = "CorrelationData" |
| 1255 |
correlation_data_as_bytes = correlation_data.encode('utf-8') |
| 1256 |
self._do_will_correlation_data_test(correlation_data, None, correlation_data_as_bytes, correlation_data) |
| 1257 |
|
| 1258 |
def test_will_correlation_data_bytes_string(self): |
| 1259 |
test_retry_wrapper(self._test_will_correlation_data_bytes_string) |
| 1260 |
|
| 1261 |
def _test_will_correlation_data_binary(self): |
| 1262 |
correlation_data = bytearray(os.urandom(64)) |
| 1263 |
self._do_will_correlation_data_test(None, correlation_data, correlation_data, None) |
| 1264 |
|
| 1265 |
def test_will_correlation_data_binary(self): |
| 1266 |
test_retry_wrapper(self._test_will_correlation_data_binary) |
| 1267 |
|
| 1268 |
def _test_will_correlation_data_string(self): |
| 1269 |
correlation_data = "CorrelationData" |
| 1270 |
correlation_data_as_bytes = correlation_data.encode('utf-8') |
| 1271 |
self._do_will_correlation_data_test(None, correlation_data, correlation_data_as_bytes, correlation_data) |
| 1272 |
|
| 1273 |
def test_will_correlation_data_string(self): |
| 1274 |
test_retry_wrapper(self._test_will_correlation_data_string) |
| 1275 |
|
| 1276 |
def _test_will_correlation_data_bytes_binary_precedence(self): |
| 1277 |
correlation_data = bytearray(os.urandom(64)) |
| 1278 |
self._do_will_correlation_data_test(correlation_data, "Ignored", correlation_data, None) |
| 1279 |
|
| 1280 |
def test_will_correlation_data_bytes_binary_precedence(self): |
| 1281 |
test_retry_wrapper(self._test_will_correlation_data_bytes_binary_precedence) |
| 1282 |
|
| 1283 |
def _test_operation_binary_publish(self): |
| 1284 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1285 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1286 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1287 |
|
| 1288 |
client_id = create_client_id() |
| 1289 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 1290 |
payload = bytearray(os.urandom(256)) |
| 1291 |
|
| 1292 |
client_options = mqtt5.ClientOptions( |
| 1293 |
host_name=input_host_name, |
| 1294 |
port=8883 |
| 1295 |
) |
| 1296 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1297 |
input_cert, |
| 1298 |
input_key |
| 1299 |
) |
| 1300 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1301 |
callbacks = Mqtt5TestCallbacks() |
| 1302 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1303 |
client.start() |
| 1304 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1305 |
|
| 1306 |
subscriptions = [] |
| 1307 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1308 |
subscribe_packet = mqtt5.SubscribePacket( |
| 1309 |
subscriptions=subscriptions) |
| 1310 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 1311 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1312 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1313 |
|
| 1314 |
publish_packet = mqtt5.PublishPacket( |
| 1315 |
payload=payload, |
| 1316 |
topic=topic_filter, |
| 1317 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 1318 |
|
| 1319 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1320 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1321 |
puback_packet = publish_completion_data.puback |
| 1322 |
self.assertIsInstance(puback_packet, mqtt5.PubackPacket) |
| 1323 |
|
| 1324 |
received_publish = callbacks.future_publish_received.result(TIMEOUT) |
| 1325 |
self.assertIsInstance(received_publish, mqtt5.PublishPacket) |
| 1326 |
self.assertEqual(received_publish.payload, payload) |
| 1327 |
|
| 1328 |
topic_filters = [] |
| 1329 |
topic_filters.append(topic_filter) |
| 1330 |
unsubscribe_packet = mqtt5.UnsubscribePacket(topic_filters=topic_filters) |
| 1331 |
unsubscribe_future = client.unsubscribe(unsubscribe_packet) |
| 1332 |
unsuback_packet = unsubscribe_future.result(TIMEOUT) |
| 1333 |
self.assertIsInstance(unsuback_packet, mqtt5.UnsubackPacket) |
| 1334 |
|
| 1335 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1336 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1337 |
puback_packet = publish_completion_data.puback |
| 1338 |
self.assertIsInstance(puback_packet, mqtt5.PubackPacket) |
| 1339 |
|
| 1340 |
self.assertEqual(callbacks.on_publish_received_counter, 1) |
| 1341 |
|
| 1342 |
client.stop() |
| 1343 |
callbacks.future_stopped.result(TIMEOUT) |
| 1344 |
|
| 1345 |
def test_operation_binary_publish(self): |
| 1346 |
test_retry_wrapper(self._test_operation_binary_publish) |
| 1347 |
|
| 1348 |
def _do_correlation_data_test(self, outbound_correlation_data_bytes, outbound_correlation_data, |
| 1349 |
expected_correlation_data_bytes, expected_correlation_data): |
| 1350 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1351 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1352 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1353 |
|
| 1354 |
client_id = create_client_id() |
| 1355 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 1356 |
payload = bytearray(os.urandom(256)) |
| 1357 |
|
| 1358 |
client_options = mqtt5.ClientOptions( |
| 1359 |
host_name=input_host_name, |
| 1360 |
port=8883 |
| 1361 |
) |
| 1362 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1363 |
input_cert, |
| 1364 |
input_key |
| 1365 |
) |
| 1366 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1367 |
callbacks = Mqtt5TestCallbacks() |
| 1368 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1369 |
client.start() |
| 1370 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1371 |
|
| 1372 |
subscriptions = [] |
| 1373 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1374 |
subscribe_packet = mqtt5.SubscribePacket( |
| 1375 |
subscriptions=subscriptions) |
| 1376 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 1377 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1378 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1379 |
|
| 1380 |
publish_packet = mqtt5.PublishPacket( |
| 1381 |
payload=payload, |
| 1382 |
topic=topic_filter, |
| 1383 |
qos=mqtt5.QoS.AT_LEAST_ONCE, |
| 1384 |
correlation_data_bytes=outbound_correlation_data_bytes, |
| 1385 |
correlation_data=outbound_correlation_data) |
| 1386 |
|
| 1387 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1388 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1389 |
puback_packet = publish_completion_data.puback |
| 1390 |
self.assertIsInstance(puback_packet, mqtt5.PubackPacket) |
| 1391 |
|
| 1392 |
received_publish = callbacks.future_publish_received.result(TIMEOUT) |
| 1393 |
self.assertIsInstance(received_publish, mqtt5.PublishPacket) |
| 1394 |
self.assertEqual(received_publish.payload, payload) |
| 1395 |
self.assertEqual(received_publish.correlation_data_bytes, expected_correlation_data_bytes) |
| 1396 |
self.assertEqual(received_publish.correlation_data, expected_correlation_data) |
| 1397 |
|
| 1398 |
client.stop() |
| 1399 |
callbacks.future_stopped.result(TIMEOUT) |
| 1400 |
|
| 1401 |
def _test_operation_publish_correlation_data_bytes_binary(self): |
| 1402 |
correlation_data = bytearray(os.urandom(64)) |
| 1403 |
self._do_correlation_data_test(correlation_data, None, correlation_data, None) |
| 1404 |
|
| 1405 |
def test_operation_publish_correlation_data_bytes_binary(self): |
| 1406 |
test_retry_wrapper(self._test_operation_publish_correlation_data_bytes_binary) |
| 1407 |
|
| 1408 |
def _test_operation_publish_correlation_data_bytes_string(self): |
| 1409 |
correlation_data = "CorrelationData" |
| 1410 |
correlation_data_as_bytes = correlation_data.encode('utf-8') |
| 1411 |
self._do_correlation_data_test(correlation_data, None, correlation_data_as_bytes, correlation_data) |
| 1412 |
|
| 1413 |
def test_operation_publish_correlation_data_bytes_string(self): |
| 1414 |
test_retry_wrapper(self._test_operation_publish_correlation_data_bytes_string) |
| 1415 |
|
| 1416 |
def _test_operation_publish_correlation_data_binary(self): |
| 1417 |
correlation_data = bytearray(os.urandom(64)) |
| 1418 |
self._do_correlation_data_test(None, correlation_data, correlation_data, None) |
| 1419 |
|
| 1420 |
def test_operation_publish_correlation_data_binary(self): |
| 1421 |
test_retry_wrapper(self._test_operation_publish_correlation_data_binary) |
| 1422 |
|
| 1423 |
def _test_operation_publish_correlation_data_string(self): |
| 1424 |
correlation_data = "CorrelationData" |
| 1425 |
correlation_data_as_bytes = correlation_data.encode('utf-8') |
| 1426 |
self._do_correlation_data_test(None, correlation_data, correlation_data_as_bytes, correlation_data) |
| 1427 |
|
| 1428 |
def test_operation_publish_correlation_data_string(self): |
| 1429 |
test_retry_wrapper(self._test_operation_publish_correlation_data_string) |
| 1430 |
|
| 1431 |
def _test_operation_publish_correlation_data_bytes_binary_precedence(self): |
| 1432 |
correlation_data = bytearray(os.urandom(64)) |
| 1433 |
self._do_correlation_data_test(correlation_data, "Ignored", correlation_data, None) |
| 1434 |
|
| 1435 |
def test_operation_publish_correlation_data_bytes_binary_precedence(self): |
| 1436 |
test_retry_wrapper(self._test_operation_publish_correlation_data_bytes_binary_precedence) |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
def _test_operation_error_null_publish(self): |
| 1443 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1444 |
|
| 1445 |
client_options = mqtt5.ClientOptions( |
| 1446 |
host_name=input_host_name |
| 1447 |
) |
| 1448 |
callbacks = Mqtt5TestCallbacks() |
| 1449 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1450 |
client.start() |
| 1451 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1452 |
|
| 1453 |
with self.assertRaises(Exception) as cm: |
| 1454 |
client.publish(None) |
| 1455 |
|
| 1456 |
client.stop() |
| 1457 |
callbacks.future_stopped.result(TIMEOUT) |
| 1458 |
|
| 1459 |
def test_operation_error_null_publish(self): |
| 1460 |
test_retry_wrapper(self._test_operation_error_null_publish) |
| 1461 |
|
| 1462 |
def _test_operation_error_null_subscribe(self): |
| 1463 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1464 |
|
| 1465 |
client_options = mqtt5.ClientOptions( |
| 1466 |
host_name=input_host_name |
| 1467 |
) |
| 1468 |
callbacks = Mqtt5TestCallbacks() |
| 1469 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1470 |
client.start() |
| 1471 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1472 |
|
| 1473 |
with self.assertRaises(Exception) as cm: |
| 1474 |
client.subscribe(None) |
| 1475 |
|
| 1476 |
client.stop() |
| 1477 |
callbacks.future_stopped.result(TIMEOUT) |
| 1478 |
|
| 1479 |
def test_operation_error_null_subscribe(self): |
| 1480 |
test_retry_wrapper(self._test_operation_error_null_subscribe) |
| 1481 |
|
| 1482 |
def _test_operation_error_null_unsubscribe(self): |
| 1483 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1484 |
|
| 1485 |
client_options = mqtt5.ClientOptions( |
| 1486 |
host_name=input_host_name |
| 1487 |
) |
| 1488 |
callbacks = Mqtt5TestCallbacks() |
| 1489 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1490 |
client.start() |
| 1491 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1492 |
|
| 1493 |
with self.assertRaises(Exception) as cm: |
| 1494 |
client.unsubscribe(None) |
| 1495 |
|
| 1496 |
client.stop() |
| 1497 |
callbacks.future_stopped.result(TIMEOUT) |
| 1498 |
|
| 1499 |
def test_operation_error_null_unsubscribe(self): |
| 1500 |
test_retry_wrapper(self._test_operation_error_null_unsubscribe) |
| 1501 |
|
| 1502 |
def _test_operation_rejoin_always(self): |
| 1503 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1504 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1505 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1506 |
|
| 1507 |
client_id = create_client_id() |
| 1508 |
|
| 1509 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1510 |
input_cert, |
| 1511 |
input_key |
| 1512 |
) |
| 1513 |
|
| 1514 |
client_options = mqtt5.ClientOptions( |
| 1515 |
host_name=input_host_name, |
| 1516 |
port=8883, |
| 1517 |
session_behavior=mqtt5.ClientSessionBehaviorType.REJOIN_ALWAYS |
| 1518 |
) |
| 1519 |
client_options.connect_options = mqtt5.ConnectPacket( |
| 1520 |
client_id=client_id, session_expiry_interval_sec=3600, keep_alive_interval_sec=360) |
| 1521 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1522 |
callbacks1 = Mqtt5TestCallbacks() |
| 1523 |
client1 = self._create_client(client_options=client_options, callbacks=callbacks1) |
| 1524 |
client1.start() |
| 1525 |
callbacks1.future_connection_success.result(TIMEOUT) |
| 1526 |
client1.stop() |
| 1527 |
callbacks1.future_stopped.result(TIMEOUT) |
| 1528 |
|
| 1529 |
callbacks2 = Mqtt5TestCallbacks() |
| 1530 |
client2 = self._create_client(client_options=client_options, callbacks=callbacks2) |
| 1531 |
client2.start() |
| 1532 |
connection_success_data = callbacks2.future_connection_success.result(TIMEOUT) |
| 1533 |
connack_packet = connection_success_data.connack_packet |
| 1534 |
self.assertTrue(connack_packet.session_present) |
| 1535 |
client2.stop() |
| 1536 |
callbacks2.future_stopped.result(TIMEOUT) |
| 1537 |
|
| 1538 |
def test_operation_rejoin_always(self): |
| 1539 |
test_retry_wrapper(self._test_operation_rejoin_always) |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
def _test_qos1_happy_path(self): |
| 1546 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1547 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1548 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1549 |
|
| 1550 |
client_id_publisher = create_client_id() |
| 1551 |
payload = "HELLO WORLD" |
| 1552 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id_publisher |
| 1553 |
|
| 1554 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1555 |
input_cert, |
| 1556 |
input_key |
| 1557 |
) |
| 1558 |
|
| 1559 |
client_options = mqtt5.ClientOptions( |
| 1560 |
host_name=input_host_name, |
| 1561 |
port=8883 |
| 1562 |
) |
| 1563 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 1564 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1565 |
callbacks1 = Mqtt5TestCallbacks() |
| 1566 |
client1 = self._create_client(client_options=client_options, callbacks=callbacks1) |
| 1567 |
client1.start() |
| 1568 |
callbacks1.future_connection_success.result(TIMEOUT) |
| 1569 |
|
| 1570 |
client_options2 = mqtt5.ClientOptions( |
| 1571 |
host_name=input_host_name, |
| 1572 |
port=8883 |
| 1573 |
) |
| 1574 |
client_options2.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 1575 |
client_options2.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1576 |
callbacks2 = Mqtt5TestCallbacks() |
| 1577 |
client2 = self._create_client(client_options=client_options2, callbacks=callbacks2) |
| 1578 |
client2.start() |
| 1579 |
callbacks2.future_connection_success.result(TIMEOUT) |
| 1580 |
callbacks2.on_publish_receive_expected = 10 |
| 1581 |
|
| 1582 |
subscriptions = [] |
| 1583 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1584 |
subscribe_packet = mqtt5.SubscribePacket( |
| 1585 |
subscriptions=subscriptions) |
| 1586 |
subscribe_future = client2.subscribe(subscribe_packet=subscribe_packet) |
| 1587 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1588 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1589 |
|
| 1590 |
publishes = 10 |
| 1591 |
|
| 1592 |
publish_packet = mqtt5.PublishPacket( |
| 1593 |
payload=payload, |
| 1594 |
topic=topic_filter, |
| 1595 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 1596 |
|
| 1597 |
for x in range(publishes): |
| 1598 |
publish_future = client1.publish(publish_packet) |
| 1599 |
publish_future.result(TIMEOUT) |
| 1600 |
|
| 1601 |
client1.stop() |
| 1602 |
callbacks1.future_stopped.result(TIMEOUT) |
| 1603 |
|
| 1604 |
callbacks2.future_expected_publishes_received.result(TIMEOUT) |
| 1605 |
self.assertEqual(callbacks2.on_publish_received_counter, publishes) |
| 1606 |
|
| 1607 |
client2.stop() |
| 1608 |
callbacks2.future_stopped.result(TIMEOUT) |
| 1609 |
|
| 1610 |
def test_qos1_happy_path(self): |
| 1611 |
test_retry_wrapper(self._test_qos1_happy_path) |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
def _test_manual_publish_acknowledgement_hold(self): |
| 1619 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1620 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1621 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1622 |
|
| 1623 |
client_id = create_client_id() |
| 1624 |
topic_filter = "test/MQTT5_ManualPuback_Python_" + client_id |
| 1625 |
payload = str(uuid.uuid4()) |
| 1626 |
payload_bytes = payload.encode("utf-8") |
| 1627 |
|
| 1628 |
PUBACK_HOLD_TIMEOUT = 60.0 |
| 1629 |
|
| 1630 |
future_first_delivery = Future() |
| 1631 |
future_redelivery = Future() |
| 1632 |
puback_handle_holder = [None] |
| 1633 |
|
| 1634 |
def on_publish_received(publish_received_data: mqtt5.PublishReceivedData): |
| 1635 |
received_payload = publish_received_data.publish_packet.payload |
| 1636 |
if not future_first_delivery.done(): |
| 1637 |
|
| 1638 |
puback_handle_holder[0] = publish_received_data.acquire_publish_acknowledgement_control() |
| 1639 |
future_first_delivery.set_result(received_payload) |
| 1640 |
elif not future_redelivery.done(): |
| 1641 |
|
| 1642 |
future_redelivery.set_result(received_payload) |
| 1643 |
|
| 1644 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1645 |
input_cert, |
| 1646 |
input_key |
| 1647 |
) |
| 1648 |
client_options = mqtt5.ClientOptions( |
| 1649 |
host_name=input_host_name, |
| 1650 |
port=8883 |
| 1651 |
) |
| 1652 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=client_id) |
| 1653 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1654 |
|
| 1655 |
callbacks = Mqtt5TestCallbacks() |
| 1656 |
callbacks.on_publish_received = on_publish_received |
| 1657 |
|
| 1658 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1659 |
client.start() |
| 1660 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1661 |
|
| 1662 |
|
| 1663 |
subscriptions = [mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 1664 |
subscribe_packet = mqtt5.SubscribePacket(subscriptions=subscriptions) |
| 1665 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 1666 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1667 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1668 |
|
| 1669 |
|
| 1670 |
publish_packet = mqtt5.PublishPacket( |
| 1671 |
payload=payload, |
| 1672 |
topic=topic_filter, |
| 1673 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 1674 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1675 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1676 |
self.assertIsInstance(publish_completion_data.puback, mqtt5.PubackPacket) |
| 1677 |
|
| 1678 |
|
| 1679 |
first_payload = future_first_delivery.result(TIMEOUT) |
| 1680 |
self.assertEqual(first_payload, payload_bytes) |
| 1681 |
self.assertIsNotNone( |
| 1682 |
puback_handle_holder[0], |
| 1683 |
"acquire_publish_acknowledgement_control() should have returned a handle") |
| 1684 |
|
| 1685 |
|
| 1686 |
redelivered_payload = future_redelivery.result(PUBACK_HOLD_TIMEOUT) |
| 1687 |
self.assertEqual(redelivered_payload, payload_bytes, |
| 1688 |
"Re-delivered payload should match the original UUID payload") |
| 1689 |
|
| 1690 |
|
| 1691 |
client.invoke_publish_acknowledgement(puback_handle_holder[0]) |
| 1692 |
|
| 1693 |
client.stop() |
| 1694 |
callbacks.future_stopped.result(TIMEOUT) |
| 1695 |
|
| 1696 |
def test_manual_publish_acknowledgement_hold(self): |
| 1697 |
test_retry_wrapper(self._test_manual_publish_acknowledgement_hold) |
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
def _test_manual_publish_acknowledgement_invoke(self): |
| 1702 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1703 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1704 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1705 |
|
| 1706 |
client_id = create_client_id() |
| 1707 |
topic_filter = "test/MQTT5_ManualPuback_Python_" + client_id |
| 1708 |
payload = str(uuid.uuid4()) |
| 1709 |
payload_bytes = payload.encode("utf-8") |
| 1710 |
|
| 1711 |
NO_REDELIVERY_WAIT = 35.0 |
| 1712 |
|
| 1713 |
future_first_delivery = Future() |
| 1714 |
future_unexpected_redelivery = Future() |
| 1715 |
puback_handle_holder = [None] |
| 1716 |
|
| 1717 |
def on_publish_received(publish_received_data: mqtt5.PublishReceivedData): |
| 1718 |
received_payload = publish_received_data.publish_packet.payload |
| 1719 |
if not future_first_delivery.done(): |
| 1720 |
|
| 1721 |
puback_handle_holder[0] = publish_received_data.acquire_publish_acknowledgement_control() |
| 1722 |
future_first_delivery.set_result(received_payload) |
| 1723 |
elif received_payload == payload_bytes and not future_unexpected_redelivery.done(): |
| 1724 |
|
| 1725 |
future_unexpected_redelivery.set_result(received_payload) |
| 1726 |
|
| 1727 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1728 |
input_cert, |
| 1729 |
input_key |
| 1730 |
) |
| 1731 |
client_options = mqtt5.ClientOptions( |
| 1732 |
host_name=input_host_name, |
| 1733 |
port=8883 |
| 1734 |
) |
| 1735 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=client_id) |
| 1736 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1737 |
|
| 1738 |
callbacks = Mqtt5TestCallbacks() |
| 1739 |
callbacks.on_publish_received = on_publish_received |
| 1740 |
|
| 1741 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1742 |
client.start() |
| 1743 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1744 |
|
| 1745 |
|
| 1746 |
subscriptions = [mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 1747 |
subscribe_packet = mqtt5.SubscribePacket(subscriptions=subscriptions) |
| 1748 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 1749 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 1750 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 1751 |
|
| 1752 |
|
| 1753 |
publish_packet = mqtt5.PublishPacket( |
| 1754 |
payload=payload, |
| 1755 |
topic=topic_filter, |
| 1756 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 1757 |
publish_future = client.publish(publish_packet=publish_packet) |
| 1758 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 1759 |
self.assertIsInstance(publish_completion_data.puback, mqtt5.PubackPacket) |
| 1760 |
|
| 1761 |
|
| 1762 |
first_payload = future_first_delivery.result(TIMEOUT) |
| 1763 |
self.assertEqual(first_payload, payload_bytes) |
| 1764 |
self.assertIsNotNone(puback_handle_holder[0], |
| 1765 |
"acquire_publish_acknowledgement_control() should have returned a handle") |
| 1766 |
|
| 1767 |
|
| 1768 |
client.invoke_publish_acknowledgement(puback_handle_holder[0]) |
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
redelivered = future_unexpected_redelivery.done() or \ |
| 1773 |
(not future_unexpected_redelivery.done() and |
| 1774 |
self._wait_for_future_timeout(future_unexpected_redelivery, NO_REDELIVERY_WAIT)) |
| 1775 |
self.assertFalse(redelivered, |
| 1776 |
"Broker should NOT re-deliver the message after invoke_publish_acknowledgement() was called") |
| 1777 |
|
| 1778 |
client.stop() |
| 1779 |
callbacks.future_stopped.result(TIMEOUT) |
| 1780 |
|
| 1781 |
def _wait_for_future_timeout(self, future, timeout_sec): |
| 1782 |
"""Returns True if the future completed within timeout_sec, False if it timed out.""" |
| 1783 |
try: |
| 1784 |
future.result(timeout_sec) |
| 1785 |
return True |
| 1786 |
except Exception: |
| 1787 |
return False |
| 1788 |
|
| 1789 |
def test_manual_publish_acknowledgement_invoke(self): |
| 1790 |
test_retry_wrapper(self._test_manual_publish_acknowledgement_invoke) |
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
def _test_manual_publish_acknowledgement_acquire_double_call_raises(self): |
| 1795 |
"""Verify that calling acquire_publish_acknowledgement_control() twice on the same QoS 1 PUBLISH raises RuntimeError.""" |
| 1796 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1797 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1798 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1799 |
|
| 1800 |
client_id = create_client_id() |
| 1801 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 1802 |
payload = str(uuid.uuid4()) |
| 1803 |
|
| 1804 |
future_result = Future() |
| 1805 |
|
| 1806 |
def on_publish_received(publish_received_data: mqtt5.PublishReceivedData): |
| 1807 |
try: |
| 1808 |
|
| 1809 |
handle = publish_received_data.acquire_publish_acknowledgement_control() |
| 1810 |
|
| 1811 |
try: |
| 1812 |
publish_received_data.acquire_publish_acknowledgement_control() |
| 1813 |
future_result.set_result("no_error") |
| 1814 |
except RuntimeError: |
| 1815 |
future_result.set_result("double_call_raised") |
| 1816 |
except Exception as e: |
| 1817 |
future_result.set_result(f"unexpected_error: {e}") |
| 1818 |
|
| 1819 |
|
| 1820 |
except Exception as e: |
| 1821 |
future_result.set_result(f"first_call_failed: {e}") |
| 1822 |
|
| 1823 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(input_cert, input_key) |
| 1824 |
client_options = mqtt5.ClientOptions(host_name=input_host_name, port=8883) |
| 1825 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=client_id) |
| 1826 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1827 |
|
| 1828 |
callbacks = Mqtt5TestCallbacks() |
| 1829 |
callbacks.on_publish_received = on_publish_received |
| 1830 |
|
| 1831 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1832 |
client.start() |
| 1833 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1834 |
|
| 1835 |
subscriptions = [mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 1836 |
subscribe_future = client.subscribe(mqtt5.SubscribePacket(subscriptions=subscriptions)) |
| 1837 |
subscribe_future.result(TIMEOUT) |
| 1838 |
|
| 1839 |
publish_future = client.publish(mqtt5.PublishPacket( |
| 1840 |
payload=payload, topic=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1841 |
publish_future.result(TIMEOUT) |
| 1842 |
|
| 1843 |
result = future_result.result(TIMEOUT) |
| 1844 |
self.assertEqual(result, "double_call_raised", |
| 1845 |
f"Expected RuntimeError on double-call, got: {result}") |
| 1846 |
|
| 1847 |
client.stop() |
| 1848 |
callbacks.future_stopped.result(TIMEOUT) |
| 1849 |
|
| 1850 |
def test_manual_publish_acknowledgement_acquire_double_call_raises(self): |
| 1851 |
test_retry_wrapper(self._test_manual_publish_acknowledgement_acquire_double_call_raises) |
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
def _test_manual_publish_acknowledgement_acquire_post_callback_raises(self): |
| 1857 |
"""Verify that calling acquire_publish_acknowledgement_control() after the callback has returned raises RuntimeError.""" |
| 1858 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1859 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1860 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1861 |
|
| 1862 |
client_id = create_client_id() |
| 1863 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 1864 |
payload = str(uuid.uuid4()) |
| 1865 |
|
| 1866 |
future_callback_done = Future() |
| 1867 |
saved_acquire_fn_holder = [None] |
| 1868 |
|
| 1869 |
def on_publish_received(publish_received_data: mqtt5.PublishReceivedData): |
| 1870 |
|
| 1871 |
saved_acquire_fn_holder[0] = publish_received_data.acquire_publish_acknowledgement_control |
| 1872 |
future_callback_done.set_result(True) |
| 1873 |
|
| 1874 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(input_cert, input_key) |
| 1875 |
client_options = mqtt5.ClientOptions(host_name=input_host_name, port=8883) |
| 1876 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=client_id) |
| 1877 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1878 |
|
| 1879 |
callbacks = Mqtt5TestCallbacks() |
| 1880 |
callbacks.on_publish_received = on_publish_received |
| 1881 |
|
| 1882 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1883 |
client.start() |
| 1884 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1885 |
|
| 1886 |
subscriptions = [mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 1887 |
subscribe_future = client.subscribe(mqtt5.SubscribePacket(subscriptions=subscriptions)) |
| 1888 |
subscribe_future.result(TIMEOUT) |
| 1889 |
|
| 1890 |
publish_future = client.publish(mqtt5.PublishPacket( |
| 1891 |
payload=payload, topic=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 1892 |
publish_future.result(TIMEOUT) |
| 1893 |
|
| 1894 |
|
| 1895 |
future_callback_done.result(TIMEOUT) |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
acquire_fn = saved_acquire_fn_holder[0] |
| 1900 |
self.assertIsNotNone(acquire_fn, "acquire_publish_acknowledgement_control should have been saved") |
| 1901 |
with self.assertRaises(RuntimeError): |
| 1902 |
acquire_fn() |
| 1903 |
|
| 1904 |
client.stop() |
| 1905 |
callbacks.future_stopped.result(TIMEOUT) |
| 1906 |
|
| 1907 |
def test_manual_publish_acknowledgement_acquire_post_callback_raises(self): |
| 1908 |
test_retry_wrapper(self._test_manual_publish_acknowledgement_acquire_post_callback_raises) |
| 1909 |
|
| 1910 |
|
| 1911 |
def _test_manual_publish_acknowledgement_qos0_acquire_is_none(self): |
| 1912 |
"""Verify that acquire_publish_acknowledgement_control is None for QoS 0 messages.""" |
| 1913 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1914 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1915 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1916 |
|
| 1917 |
client_id = create_client_id() |
| 1918 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 1919 |
payload = str(uuid.uuid4()) |
| 1920 |
|
| 1921 |
future_acquire_value = Future() |
| 1922 |
|
| 1923 |
def on_publish_received(publish_received_data: mqtt5.PublishReceivedData): |
| 1924 |
|
| 1925 |
future_acquire_value.set_result(publish_received_data.acquire_publish_acknowledgement_control) |
| 1926 |
|
| 1927 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(input_cert, input_key) |
| 1928 |
client_options = mqtt5.ClientOptions(host_name=input_host_name, port=8883) |
| 1929 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=client_id) |
| 1930 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1931 |
|
| 1932 |
callbacks = Mqtt5TestCallbacks() |
| 1933 |
callbacks.on_publish_received = on_publish_received |
| 1934 |
|
| 1935 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 1936 |
client.start() |
| 1937 |
callbacks.future_connection_success.result(TIMEOUT) |
| 1938 |
|
| 1939 |
|
| 1940 |
subscriptions = [mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 1941 |
subscribe_future = client.subscribe(mqtt5.SubscribePacket(subscriptions=subscriptions)) |
| 1942 |
subscribe_future.result(TIMEOUT) |
| 1943 |
|
| 1944 |
|
| 1945 |
publish_future = client.publish(mqtt5.PublishPacket( |
| 1946 |
payload=payload, topic=topic_filter, qos=mqtt5.QoS.AT_MOST_ONCE)) |
| 1947 |
publish_future.result(TIMEOUT) |
| 1948 |
|
| 1949 |
acquire_value = future_acquire_value.result(TIMEOUT) |
| 1950 |
self.assertIsNone(acquire_value, |
| 1951 |
"acquire_publish_acknowledgement_control should be None for QoS 0 messages") |
| 1952 |
|
| 1953 |
client.stop() |
| 1954 |
callbacks.future_stopped.result(TIMEOUT) |
| 1955 |
|
| 1956 |
def test_manual_publish_acknowledgement_qos0_acquire_is_none(self): |
| 1957 |
test_retry_wrapper(self._test_manual_publish_acknowledgement_qos0_acquire_is_none) |
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
def _test_auto_puback_no_duplicate(self): |
| 1963 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 1964 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 1965 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 1966 |
|
| 1967 |
client_id = create_client_id() |
| 1968 |
topic_filter = "test/MQTT5_AutoPuback_Python_" + client_id |
| 1969 |
payload = "hello" |
| 1970 |
payload_bytes = payload.encode("utf-8") |
| 1971 |
|
| 1972 |
NO_REDELIVERY_WAIT = 35.0 |
| 1973 |
|
| 1974 |
future_first_delivery = Future() |
| 1975 |
future_unexpected_redelivery = Future() |
| 1976 |
|
| 1977 |
def on_publish_received(publish_received_data: mqtt5.PublishReceivedData): |
| 1978 |
received_payload = publish_received_data.publish_packet.payload |
| 1979 |
if not future_first_delivery.done(): |
| 1980 |
|
| 1981 |
|
| 1982 |
future_first_delivery.set_result(received_payload) |
| 1983 |
elif received_payload == payload_bytes and not future_unexpected_redelivery.done(): |
| 1984 |
|
| 1985 |
future_unexpected_redelivery.set_result(received_payload) |
| 1986 |
|
| 1987 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 1988 |
input_cert, |
| 1989 |
input_key |
| 1990 |
) |
| 1991 |
client_options = mqtt5.ClientOptions( |
| 1992 |
host_name=input_host_name, |
| 1993 |
port=8883 |
| 1994 |
) |
| 1995 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=client_id) |
| 1996 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 1997 |
|
| 1998 |
callbacks = Mqtt5TestCallbacks() |
| 1999 |
callbacks.on_publish_received = on_publish_received |
| 2000 |
|
| 2001 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 2002 |
client.start() |
| 2003 |
callbacks.future_connection_success.result(TIMEOUT) |
| 2004 |
|
| 2005 |
|
| 2006 |
subscriptions = [mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 2007 |
subscribe_packet = mqtt5.SubscribePacket(subscriptions=subscriptions) |
| 2008 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 2009 |
suback_packet = subscribe_future.result(TIMEOUT) |
| 2010 |
self.assertIsInstance(suback_packet, mqtt5.SubackPacket) |
| 2011 |
|
| 2012 |
|
| 2013 |
publish_packet = mqtt5.PublishPacket( |
| 2014 |
payload=payload, |
| 2015 |
topic=topic_filter, |
| 2016 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 2017 |
publish_future = client.publish(publish_packet=publish_packet) |
| 2018 |
publish_completion_data = publish_future.result(TIMEOUT) |
| 2019 |
self.assertIsInstance(publish_completion_data.puback, mqtt5.PubackPacket) |
| 2020 |
|
| 2021 |
|
| 2022 |
first_payload = future_first_delivery.result(TIMEOUT) |
| 2023 |
self.assertEqual(first_payload, payload_bytes) |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
redelivered = self._wait_for_future_timeout(future_unexpected_redelivery, NO_REDELIVERY_WAIT) |
| 2028 |
self.assertFalse(redelivered, |
| 2029 |
"Auto-PUBACK should have been sent " |
| 2030 |
"(acquire_publish_acknowledgement_control() was not called), " |
| 2031 |
"verified by absence of re-delivery") |
| 2032 |
|
| 2033 |
client.stop() |
| 2034 |
callbacks.future_stopped.result(TIMEOUT) |
| 2035 |
|
| 2036 |
def test_auto_puback_no_duplicate(self): |
| 2037 |
test_retry_wrapper(self._test_auto_puback_no_duplicate) |
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
def _test_retain_set_and_clear(self): |
| 2044 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 2045 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 2046 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 2047 |
|
| 2048 |
client_id_publisher = create_client_id() |
| 2049 |
payload = "HELLO WORLD" |
| 2050 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id_publisher |
| 2051 |
|
| 2052 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 2053 |
input_cert, |
| 2054 |
input_key |
| 2055 |
) |
| 2056 |
|
| 2057 |
client_options = mqtt5.ClientOptions( |
| 2058 |
host_name=input_host_name, |
| 2059 |
port=8883 |
| 2060 |
) |
| 2061 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2062 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2063 |
callbacks1 = Mqtt5TestCallbacks() |
| 2064 |
client1 = self._create_client(client_options=client_options, callbacks=callbacks1) |
| 2065 |
client1.start() |
| 2066 |
callbacks1.future_connection_success.result(TIMEOUT) |
| 2067 |
|
| 2068 |
publish_packet = mqtt5.PublishPacket( |
| 2069 |
payload=payload, |
| 2070 |
topic=topic_filter, |
| 2071 |
retain=True, |
| 2072 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 2073 |
puback_future1 = client1.publish(publish_packet) |
| 2074 |
puback_future1.result(TIMEOUT) |
| 2075 |
|
| 2076 |
client_options2 = mqtt5.ClientOptions( |
| 2077 |
host_name=input_host_name, |
| 2078 |
port=8883 |
| 2079 |
) |
| 2080 |
client_options2.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2081 |
client_options2.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2082 |
callbacks2 = Mqtt5TestCallbacks() |
| 2083 |
client2 = self._create_client(client_options=client_options2, callbacks=callbacks2) |
| 2084 |
client2.start() |
| 2085 |
callbacks2.future_connection_success.result(TIMEOUT) |
| 2086 |
|
| 2087 |
subscriptions = [] |
| 2088 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 2089 |
subscribe_packet = mqtt5.SubscribePacket( |
| 2090 |
subscriptions=subscriptions) |
| 2091 |
subscribe_future1 = client2.subscribe(subscribe_packet=subscribe_packet) |
| 2092 |
suback_packet1 = subscribe_future1.result(TIMEOUT) |
| 2093 |
self.assertIsInstance(suback_packet1, mqtt5.SubackPacket) |
| 2094 |
|
| 2095 |
received_retained_publish1 = callbacks2.future_publish_received.result(TIMEOUT) |
| 2096 |
self.assertIsInstance(received_retained_publish1, mqtt5.PublishPacket) |
| 2097 |
client2.stop() |
| 2098 |
callbacks2.future_stopped.result(TIMEOUT) |
| 2099 |
|
| 2100 |
publish_packet.payload = None |
| 2101 |
puback_future2 = client1.publish(publish_packet) |
| 2102 |
puback_future2.result(TIMEOUT) |
| 2103 |
|
| 2104 |
client1.stop() |
| 2105 |
callbacks1.future_stopped.result(TIMEOUT) |
| 2106 |
|
| 2107 |
client_options3 = mqtt5.ClientOptions( |
| 2108 |
host_name=input_host_name, |
| 2109 |
port=8883 |
| 2110 |
) |
| 2111 |
client_options3.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2112 |
client_options3.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2113 |
callbacks3 = Mqtt5TestCallbacks() |
| 2114 |
client3 = self._create_client(client_options=client_options3, callbacks=callbacks3) |
| 2115 |
client3.start() |
| 2116 |
callbacks3.future_connection_success.result(TIMEOUT) |
| 2117 |
|
| 2118 |
subscribe_future2 = client3.subscribe(subscribe_packet=subscribe_packet) |
| 2119 |
suback_packet2 = subscribe_future2.result(TIMEOUT) |
| 2120 |
self.assertIsInstance(suback_packet2, mqtt5.SubackPacket) |
| 2121 |
|
| 2122 |
time.sleep(1) |
| 2123 |
self.assertEqual(callbacks3.on_publish_received_counter, 0) |
| 2124 |
|
| 2125 |
client3.stop() |
| 2126 |
callbacks3.future_stopped.result(TIMEOUT) |
| 2127 |
|
| 2128 |
def test_retain_set_and_clear(self): |
| 2129 |
test_retry_wrapper(self._test_retain_set_and_clear) |
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
def _test_interruption_sub(self): |
| 2136 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 2137 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 2138 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 2139 |
|
| 2140 |
client_id = create_client_id() |
| 2141 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 2142 |
|
| 2143 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 2144 |
input_cert, |
| 2145 |
input_key |
| 2146 |
) |
| 2147 |
client_options = mqtt5.ClientOptions( |
| 2148 |
host_name=input_host_name, |
| 2149 |
port=8883 |
| 2150 |
) |
| 2151 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2152 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2153 |
callbacks = Mqtt5TestCallbacks() |
| 2154 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 2155 |
client.start() |
| 2156 |
callbacks.future_connection_success.result(TIMEOUT) |
| 2157 |
|
| 2158 |
subscriptions = [] |
| 2159 |
subscriptions.append(mqtt5.Subscription(topic_filter=topic_filter, qos=mqtt5.QoS.AT_LEAST_ONCE)) |
| 2160 |
subscribe_packet = mqtt5.SubscribePacket( |
| 2161 |
subscriptions=subscriptions) |
| 2162 |
subscribe_future = client.subscribe(subscribe_packet=subscribe_packet) |
| 2163 |
client.stop() |
| 2164 |
|
| 2165 |
with self.assertRaises(Exception): |
| 2166 |
subscribe_future.result(TIMEOUT) |
| 2167 |
|
| 2168 |
callbacks.future_stopped.result(TIMEOUT) |
| 2169 |
|
| 2170 |
def test_interruption_sub(self): |
| 2171 |
test_retry_wrapper(self._test_interruption_sub) |
| 2172 |
|
| 2173 |
def _test_interruption_unsub(self): |
| 2174 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 2175 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 2176 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 2177 |
|
| 2178 |
client_id = create_client_id() |
| 2179 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 2180 |
|
| 2181 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 2182 |
input_cert, |
| 2183 |
input_key |
| 2184 |
) |
| 2185 |
client_options = mqtt5.ClientOptions( |
| 2186 |
host_name=input_host_name, |
| 2187 |
port=8883 |
| 2188 |
) |
| 2189 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2190 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2191 |
callbacks = Mqtt5TestCallbacks() |
| 2192 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 2193 |
client.start() |
| 2194 |
callbacks.future_connection_success.result(TIMEOUT) |
| 2195 |
|
| 2196 |
topic_filters = [] |
| 2197 |
topic_filters.append(topic_filter) |
| 2198 |
unsubscribe_packet = mqtt5.UnsubscribePacket(topic_filters=topic_filters) |
| 2199 |
unsubscribe_future = client.unsubscribe(unsubscribe_packet) |
| 2200 |
client.stop() |
| 2201 |
|
| 2202 |
with self.assertRaises(Exception): |
| 2203 |
unsubscribe_future.result(TIMEOUT) |
| 2204 |
|
| 2205 |
callbacks.future_stopped.result(TIMEOUT) |
| 2206 |
|
| 2207 |
def test_interruption_unsub(self): |
| 2208 |
test_retry_wrapper(self._test_interruption_unsub) |
| 2209 |
|
| 2210 |
def _test_interruption_qos1_publish(self): |
| 2211 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 2212 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 2213 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 2214 |
|
| 2215 |
client_id = create_client_id() |
| 2216 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id |
| 2217 |
payload = "test payload" |
| 2218 |
|
| 2219 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 2220 |
input_cert, |
| 2221 |
input_key |
| 2222 |
) |
| 2223 |
client_options = mqtt5.ClientOptions( |
| 2224 |
host_name=input_host_name, |
| 2225 |
port=8883 |
| 2226 |
) |
| 2227 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2228 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2229 |
callbacks = Mqtt5TestCallbacks() |
| 2230 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 2231 |
client.start() |
| 2232 |
callbacks.future_connection_success.result(TIMEOUT) |
| 2233 |
|
| 2234 |
publish_packet = mqtt5.PublishPacket( |
| 2235 |
payload=payload, |
| 2236 |
topic=topic_filter, |
| 2237 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 2238 |
|
| 2239 |
publish_future = client.publish(publish_packet=publish_packet) |
| 2240 |
client.stop() |
| 2241 |
|
| 2242 |
with self.assertRaises(Exception): |
| 2243 |
publish_future.result(TIMEOUT) |
| 2244 |
|
| 2245 |
callbacks.future_stopped.result(TIMEOUT) |
| 2246 |
|
| 2247 |
def test_interruption_qos1_publish(self): |
| 2248 |
test_retry_wrapper(self._test_interruption_qos1_publish) |
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
def _test_operation_statistics_uc1(self): |
| 2255 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST") |
| 2256 |
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT") |
| 2257 |
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY") |
| 2258 |
|
| 2259 |
client_id_publisher = create_client_id() |
| 2260 |
payload = "HELLO WORLD" |
| 2261 |
topic_filter = "test/MQTT5_Binding_Python_" + client_id_publisher |
| 2262 |
|
| 2263 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path( |
| 2264 |
input_cert, |
| 2265 |
input_key |
| 2266 |
) |
| 2267 |
client_options = mqtt5.ClientOptions( |
| 2268 |
host_name=input_host_name, |
| 2269 |
port=8883 |
| 2270 |
) |
| 2271 |
client_options.connect_options = mqtt5.ConnectPacket(client_id=create_client_id()) |
| 2272 |
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 2273 |
callbacks = Mqtt5TestCallbacks() |
| 2274 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 2275 |
client.start() |
| 2276 |
callbacks.future_connection_success.result(TIMEOUT) |
| 2277 |
|
| 2278 |
|
| 2279 |
statistics = client.get_stats() |
| 2280 |
self.assertEqual(statistics.incomplete_operation_count, 0) |
| 2281 |
self.assertEqual(statistics.incomplete_operation_size, 0) |
| 2282 |
self.assertEqual(statistics.unacked_operation_count, 0) |
| 2283 |
self.assertEqual(statistics.unacked_operation_size, 0) |
| 2284 |
|
| 2285 |
publish_packet = mqtt5.PublishPacket( |
| 2286 |
payload=payload, |
| 2287 |
topic=topic_filter, |
| 2288 |
qos=mqtt5.QoS.AT_LEAST_ONCE) |
| 2289 |
|
| 2290 |
publishes = 10 |
| 2291 |
for x in range(publishes): |
| 2292 |
publish_future = client.publish(publish_packet) |
| 2293 |
publish_future.result(TIMEOUT) |
| 2294 |
|
| 2295 |
|
| 2296 |
statistics = client.get_stats() |
| 2297 |
self.assertEqual(statistics.incomplete_operation_count, 0) |
| 2298 |
self.assertEqual(statistics.incomplete_operation_size, 0) |
| 2299 |
self.assertEqual(statistics.unacked_operation_count, 0) |
| 2300 |
self.assertEqual(statistics.unacked_operation_size, 0) |
| 2301 |
|
| 2302 |
client.stop() |
| 2303 |
callbacks.future_stopped.result(TIMEOUT) |
| 2304 |
|
| 2305 |
def test_operation_statistics_uc1(self): |
| 2306 |
test_retry_wrapper(self._test_operation_statistics_uc1) |
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
def _test_direct_connect_basic_auth_metrics_enabled(self): |
| 2313 |
"""Test that connection fails with basic auth when metrics are enabled. |
| 2314 |
|
| 2315 |
When metrics are enabled, the SDK appends metrics information to the username field, |
| 2316 |
which corrupts the basic authentication and causes the connection to fail. |
| 2317 |
""" |
| 2318 |
input_username = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_USERNAME") |
| 2319 |
input_password = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD") |
| 2320 |
input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST") |
| 2321 |
input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT")) |
| 2322 |
|
| 2323 |
connect_options = mqtt5.ConnectPacket( |
| 2324 |
client_id=create_client_id(), |
| 2325 |
username=input_username, |
| 2326 |
password=input_password |
| 2327 |
) |
| 2328 |
client_options = mqtt5.ClientOptions( |
| 2329 |
host_name=input_host_name, |
| 2330 |
port=input_port, |
| 2331 |
connect_options=connect_options, |
| 2332 |
disable_metrics=False |
| 2333 |
) |
| 2334 |
callbacks = Mqtt5TestCallbacks() |
| 2335 |
client = self._create_client(client_options=client_options, callbacks=callbacks) |
| 2336 |
|
| 2337 |
|
| 2338 |
self.assertFalse(client_options.disable_metrics) |
| 2339 |
|
| 2340 |
client.start() |
| 2341 |
|
| 2342 |
callbacks.future_connection_failure.result(TIMEOUT) |
| 2343 |
client.stop() |
| 2344 |
callbacks.future_stopped.result(TIMEOUT) |
| 2345 |
|
| 2346 |
def test_direct_connect_basic_auth_metrics_enabled(self): |
| 2347 |
test_retry_wrapper(self._test_direct_connect_basic_auth_metrics_enabled) |
| 2348 |
|
| 2349 |
|
| 2350 |
if __name__ == 'main': |
| 2351 |
unittest.main() |