From 5c806c924f862031aeca2d1075409366273ea795 Mon Sep 17 00:00:00 2001 From: Bogdan Kolendovskyy Date: Tue, 9 Dec 2025 13:43:53 +0100 Subject: [PATCH] ci(mqtt): use QoS=1 for control messages in SSL test SSL test relies on the device receiving a control message during the test, which was being sent with QoS=0. Send the control message with QoS=1 to avoid failures due to poor network connection. --- examples/protocols/mqtt/ssl/main/app_main.c | 7 +++-- .../protocols/mqtt/ssl/pytest_mqtt_ssl.py | 29 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 20cf1808f4..01a6ac3404 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -46,7 +46,7 @@ static void send_binary(esp_mqtt_client_handle_t client) esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle); // sending only the configured portion of the partition (if it's less than the partition size) int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND, partition->size); - int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, binary_size, 0, 0); + int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, binary_size, 1, 0); ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id); } @@ -69,6 +69,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ switch ((esp_mqtt_event_id_t)event_id) { case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + esp_mqtt_client_subscribe(client, "/topic/binary", 1); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); @@ -126,7 +128,8 @@ static void mqtt_app_start(void) const esp_mqtt_client_config_t mqtt_cfg = { .broker = { .address.uri = CONFIG_BROKER_URI, - .verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start + .verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start, + .verification.skip_cert_common_name_check = true }, }; diff --git a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py index d27cc65b78..36de017ccb 100644 --- a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py +++ b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py @@ -15,6 +15,7 @@ from pytest_embedded import Dut event_client_connected = Event() event_stop_client = Event() +event_client_connected_to_topic_binary = Event() event_client_received_correct = Event() event_client_received_binary = Event() message_log = '' @@ -36,14 +37,15 @@ def mqtt_client_task(client): # type: (mqtt.Client) -> None # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client.MQTTMessage) -> None global message_log + global event_client_connected_to_topic_binary global event_client_received_correct global event_client_received_binary if msg.topic == '/topic/binary': binary, bin_size = userdata print('Receiving binary from esp and comparing with {}, size {}...'.format(binary, bin_size)) with open(binary, 'rb') as f: - bin = f.read() - if bin[:bin_size] == msg.payload[:bin_size]: + binary_reference = f.read() + if binary_reference[:bin_size] == msg.payload[:bin_size]: print('...matches!') event_client_received_binary.set() return @@ -53,9 +55,13 @@ def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) payload = msg.payload.decode() - if not event_client_received_correct.is_set() and payload == 'data': - client.subscribe('/topic/binary') - client.publish('/topic/qos0', 'send binary please') + if ( + event_client_connected_to_topic_binary.is_set() + and not event_client_received_correct.is_set() + and payload == 'data' + ): + client.publish('/topic/binary', 'send binary please', qos=1) + client.subscribe('/topic/binary', qos=1) if msg.topic == '/topic/qos0' and payload == 'data': event_client_received_correct.set() message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' @@ -117,6 +123,19 @@ def test_examples_protocol_mqtt_ssl(dut): # type: (Dut) -> None except pexpect.TIMEOUT: print('ENV_TEST_FAILURE: Cannot connect to AP') raise + try: + dut.expect(r'MQTT_EVENT_CONNECTED', timeout=120) + # SSL example subscribes to topics /topic/binary, /topic/qos0, and /topic/qos1 + # We do not want to introduce any logs related to /topic/binary, so we will + # wait for all three MQTT_EVENT_SUBSCRIBED to be processed + for i in range(3): + dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)') + + event_client_connected_to_topic_binary.set() + + except pexpect.TIMEOUT: + print('ENV_TEST_FAILURE: Client cannot connect to the broker {}:{}.'.format(broker_url, broker_port)) + raise print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log))