From 307eb5f17f6ed05dddee8cdad3aac2f73cc8e786 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 20 Jul 2021 11:11:54 -0300 Subject: [PATCH 1/3] [MQTT/test] Fix publish connect test - Mqtt client will return error on wrong parameters. This fix the test code to setup the client for each test case. --- .../mqtt/publish_connect_test/CMakeLists.txt | 2 +- .../mqtt/publish_connect_test/app_test.py | 178 +++++++++--------- .../publish_connect_test/main/connect_test.c | 134 ++++++------- .../main/mqtt_eclipse_org.pem | 27 --- .../main/mqtt_eclipseprojects_io.pem | 30 +++ .../main/publish_connect_test.c | 4 +- .../publish_connect_test/main/publish_test.c | 132 ++++++++----- 7 files changed, 281 insertions(+), 226 deletions(-) delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt index 6d1ef5af8f..6fcb74b52f 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt @@ -10,7 +10,7 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish_connect_test) -target_add_binary_data(mqtt_publish_connect_test.elf "main/mqtt_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "main/mqtt_eclipseprojects_io.pem" TEXT) target_add_binary_data(mqtt_publish_connect_test.elf "ca.crt" TEXT) target_add_binary_data(mqtt_publish_connect_test.elf "ca.der" TEXT) target_add_binary_data(mqtt_publish_connect_test.elf "client_pwd.key" TEXT) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 8f0bd95c26..580e28568b 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -8,6 +8,7 @@ import socket import ssl import string import subprocess +import sys from threading import Event, Thread import paho.mqtt.client as mqtt @@ -239,6 +240,68 @@ class TlsServer: self.shutdown.set() +def connection_tests(dut, cases): + ip = get_my_ip() + set_server_cert_cn(ip) + server_port = 2222 + + def start_connection_case(case, desc): + print('Starting {}: {}'.format(case, desc)) + case_id = cases[case] + dut.write('conn {} {} {}'.format(ip, server_port, case_id)) + dut.expect('Test case:{} started'.format(case_id)) + return case_id + + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: + # All these cases connect to the server with no server verification or with server only verification + with TlsServer(server_port): + test_nr = start_connection_case(case, 'default server - expect to connect normally') + dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + with TlsServer(server_port, refuse_connection=True): + test_nr = start_connection_case(case, 'ssl shall connect, but mqtt sends connect refusal') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error + with TlsServer(server_port, client_cert=True) as s: + test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): + raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: + # These cases connect to server with both server and client verification (client key might be password protected) + with TlsServer(server_port, client_cert=True): + test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') + dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + + case = 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' + with TlsServer(server_port) as s: + test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + if 'alert unknown ca' not in s.get_last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + + case = 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' + with TlsServer(server_port, client_cert=True) as s: + test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + with TlsServer(server_port, use_alpn=True) as s: + test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') + dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + if case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: + print(' - client with alpn off, no negotiated protocol: OK') + elif case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': + print(' - client with alpn on, negotiated protocol resolved: OK') + else: + raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) + + @ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): """ @@ -256,6 +319,35 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): # Look for test case symbolic names and publish configs cases = {} publish_cfg = {} + try: + + # Get connection test cases configuration: symbolic names for test cases + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', + 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', + 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + cases[case] = dut1.app.get_sdkconfig()[case] + except Exception: + print('ENV_TEST_FAILURE: Some mandatory CONNECTION test case not found in sdkconfig') + raise + + dut1.start_app() + esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) + print('Got IP={}'.format(esp_ip[0])) + + if not os.getenv('MQTT_SKIP_CONNECT_TEST'): + connection_tests(dut1,cases) + + # + # start publish tests only if enabled in the environment (for weekend tests only) + if not os.getenv('MQTT_PUBLISH_TEST'): + return + + # Get publish test configuration try: def get_host_port_from_dut(dut1, config_option): value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option]) @@ -263,17 +355,6 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): return None, None return value.group(1), int(value.group(2)) - # Get connection test cases configuration: symbolic names for test cases - for i in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', - 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', - 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: - cases[i] = dut1.app.get_sdkconfig()[i] - # Get publish test configuration publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','') publish_cfg['subscribe_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') @@ -282,80 +363,9 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WSS_URI') except Exception: - print('ENV_TEST_FAILURE: Some mandatory test case not found in sdkconfig') + print('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') raise - dut1.start_app() - esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) - print('Got IP={}'.format(esp_ip[0])) - - # - # start connection test - ip = get_my_ip() - set_server_cert_cn(ip) - server_port = 2222 - - def start_connection_case(case, desc): - print('Starting {}: {}'.format(case, desc)) - case_id = cases[case] - dut1.write('conn {} {} {}'.format(ip, server_port, case_id)) - dut1.expect('Test case:{} started'.format(case_id)) - return case_id - - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: - # All these cases connect to the server with no server verification or with server only verification - with TlsServer(server_port): - test_nr = start_connection_case(case, 'default server - expect to connect normally') - dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - with TlsServer(server_port, refuse_connection=True): - test_nr = start_connection_case(case, 'ssl shall connect, but mqtt sends connect refusal') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error - with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) - if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): - raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: - # These cases connect to server with both server and client verification (client key might be password protected) - with TlsServer(server_port, client_cert=True): - test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') - dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - - case = 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' - with TlsServer(server_port) as s: - test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) - if 'alert unknown ca' not in s.get_last_ssl_error(): - raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - case = 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' - with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) - if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): - raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: - with TlsServer(server_port, use_alpn=True) as s: - test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') - dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - if case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: - print(' - client with alpn off, no negotiated protocol: OK') - elif case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': - print(' - client with alpn on, negotiated protocol resolved: OK') - else: - raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) - - # - # start publish tests only if enabled in the environment (for weekend tests only) - if not os.getenv('MQTT_PUBLISH_TEST'): - return - def start_publish_case(transport, qos, repeat, published, queue): print('Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}' .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) @@ -375,4 +385,4 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): if __name__ == '__main__': - test_app_protocol_mqtt_publish_connect() + test_app_protocol_mqtt_publish_connect(dut=ttfw_idf.ESP32QEMUDUT if sys.argv[1:] == ['qemu'] else ttfw_idf.ESP32DUT) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index a5817eb25b..112c000f59 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -42,29 +42,29 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ esp_mqtt_event_handle_t event = event_data; ESP_LOGD(TAG, "Event: %d, Test case: %d", event->event_id, running_test_case); switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED: Test=%d", running_test_case); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); - if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { - ESP_LOGI(TAG, "ESP-TLS ERROR: 0x%x", event->error_handle->esp_tls_last_esp_err); - } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { - ESP_LOGI(TAG, "MQTT ERROR: 0x%x", event->error_handle->connect_return_code); - } else { - ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); - } - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED: Test=%d", running_test_case); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { + ESP_LOGI(TAG, "ESP-TLS ERROR: 0x%x", event->error_handle->esp_tls_last_esp_err); + } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { + ESP_LOGI(TAG, "MQTT ERROR: 0x%x", event->error_handle->connect_return_code); + } else { + ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } } static void create_client(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://127.0.0.1:1234" + .uri = "mqtts://127.0.0.1:1234" }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); @@ -77,7 +77,7 @@ static void connect_no_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri + .uri = uri }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -89,12 +89,12 @@ static void connect_with_client_key_password(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_pwd_crt, - .client_key_pem = (const char *)client_pwd_key, - .clientkey_password = "esp32", - .clientkey_password_len = 5 + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_pwd_crt, + .client_key_pem = (const char *)client_pwd_key, + .clientkey_password = "esp32", + .clientkey_password_len = 5 }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -106,11 +106,11 @@ static void connect_with_server_der_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_der_start, - .cert_len = ca_der_end - ca_der_start, - .client_cert_pem = "NULL", - .client_key_pem = "NULL" + .uri = uri, + .cert_pem = (const char *)ca_der_start, + .cert_len = ca_der_end - ca_der_start, + .client_cert_pem = "NULL", + .client_key_pem = "NULL" }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -122,10 +122,10 @@ static void connect_with_wrong_server_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)client_pwd_crt, - .client_cert_pem = "NULL", - .client_key_pem = "NULL" + .uri = uri, + .cert_pem = (const char *)client_pwd_crt, + .client_cert_pem = "NULL", + .client_key_pem = "NULL" }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -165,10 +165,10 @@ static void connect_with_invalid_client_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_inv_crt, - .client_key_pem = (const char *)client_no_pwd_key + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_inv_crt, + .client_key_pem = (const char *)client_no_pwd_key }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -181,15 +181,15 @@ static void connect_with_alpn(const char *host, const int port) const char *alpn_protos[] = { "mymqtt", NULL }; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .alpn_protos = alpn_protos + .uri = uri, + .alpn_protos = alpn_protos }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); esp_mqtt_client_reconnect(mqtt_client); } -void connection_test(const char* line) +void connection_test(const char *line) { char test_type[32]; char host[32]; @@ -203,33 +203,33 @@ void connection_test(const char* line) ESP_LOGI(TAG, "CASE:%d, connecting to mqtts://%s:%d ", test_case, host, port); running_test_case = test_case; switch (test_case) { - case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: - connect_no_certs(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT: - connect_with_server_cert(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH: - connect_with_server_client_certs(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT: - connect_with_wrong_server_cert(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT: - connect_with_server_der_cert(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD: - connect_with_client_key_password(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT: - connect_with_invalid_client_certs(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN: - connect_with_alpn(host, port); - break; - default: - ESP_LOGE(TAG, "Unknown test case %d ", test_case); - break; + case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: + connect_no_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT: + connect_with_server_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH: + connect_with_server_client_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT: + connect_with_wrong_server_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT: + connect_with_server_der_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD: + connect_with_client_key_password(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT: + connect_with_invalid_client_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN: + connect_with_alpn(host, port); + break; + default: + ESP_LOGE(TAG, "Unknown test case %d ", test_case); + break; } ESP_LOGI(TAG, "Test case:%d started", test_case); } diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem deleted file mode 100644 index 0002462ce8..0000000000 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ -MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT -DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow -SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT -GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC -AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF -q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 -SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 -Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA -a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj -/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T -AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG -CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv -bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k -c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw -VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC -ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz -MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu -Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF -AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo -uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ -wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu -X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG -PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 -KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem new file mode 100644 index 0000000000..43b222a60a --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw +WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg +RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK +AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP +R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx +sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm +NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg +Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG +/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC +AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB +Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA +FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw +AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw +Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB +gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W +PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl +ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz +CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm +lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4 +avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2 +yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O +yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids +hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+ +HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv +MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX +nLRbwHOoq7hHwg== +-----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index a112834231..84da251963 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -18,8 +18,8 @@ static const char *TAG = "PUBLISH_CONNECT_TEST"; -void connection_test(const char* line); -void publish_test(const char* line); +void connection_test(const char *line); +void publish_test(const char *line); static void get_string(char *line, size_t size) { diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index d4863e59b7..8801a4983f 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -17,6 +17,7 @@ #include "esp_log.h" #include "mqtt_client.h" +#include "sdkconfig.h" static const char *TAG = "PUBLISH_TEST"; @@ -32,25 +33,27 @@ static size_t expected_published = 0; static size_t actual_published = 0; static int qos_test = 0; - #if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 - static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; - #else - extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); - #endif - extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); +#if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 +static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +#else +extern const uint8_t mqtt_eclipseprojects_io_pem_start[] asm("_binary_mqtt_eclipseprojects_io_pem_start"); +#endif +extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclipseprojects_io_pem_end"); -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; static int msg_id = 0; static int actual_len = 0; - // your_context_t *context = event->context; switch (event->event_id) { + case MQTT_EVENT_BEFORE_CONNECT: + break; case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCIBE_TOPIC, msg_id); break; case MQTT_EVENT_DISCONNECTED: @@ -76,7 +79,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) msg_id = event->msg_id; } else { actual_len += event->data_len; - // check consisency with msg_id across multiple data events for single msg + // check consistency with msg_id across multiple data events for single msg if (msg_id != event->msg_id) { ESP_LOGI(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); abort(); @@ -105,36 +108,24 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) ESP_LOGI(TAG, "Other event id:%d", event->event_id); break; } - return ESP_OK; } +typedef enum {NONE, TCP, SSL, WS, WSS} transport_t; +static transport_t current_transport; -static void mqtt_app_start(void) + +void test_init(void) { mqtt_event_group = xEventGroupCreate(); - const esp_mqtt_client_config_t mqtt_cfg = { - .event_handle = mqtt_event_handler, - .cert_pem = (const char *)mqtt_eclipse_org_pem_start, - }; - + esp_mqtt_client_config_t config = {0}; + mqtt_client = esp_mqtt_client_init(&config); + current_transport = NONE; + esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); - mqtt_client = esp_mqtt_client_init(&mqtt_cfg); } -void publish_test(const char* line) +void pattern_setup(char *pattern, int repeat) { - char pattern[32]; - char transport[32]; - int repeat = 0; - int enqueue = 0; - static bool is_mqtt_init = false; - - if (!is_mqtt_init) { - mqtt_app_start(); - is_mqtt_init = true; - } - sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); - ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); int pattern_size = strlen(pattern); free(expected_data); free(actual_data); @@ -146,29 +137,80 @@ void publish_test(const char* line) memcpy(expected_data + i * pattern_size, pattern, pattern_size); } printf("EXPECTED STRING %.*s, SIZE:%d\n", expected_size, expected_data, expected_size); - esp_mqtt_client_stop(mqtt_client); +} +static void configure_client(char *transport) +{ + ESP_LOGI(TAG, "Configuration"); + transport_t selected_transport; if (0 == strcmp(transport, "tcp")) { - ESP_LOGI(TAG, "[TCP transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + selected_transport = TCP; } else if (0 == strcmp(transport, "ssl")) { - ESP_LOGI(TAG, "[SSL transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_SSL_URI); + selected_transport = SSL; } else if (0 == strcmp(transport, "ws")) { - ESP_LOGI(TAG, "[WS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + selected_transport = WS; } else if (0 == strcmp(transport, "wss")) { - ESP_LOGI(TAG, "[WSS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + selected_transport = WSS; } else { - ESP_LOGE(TAG, "Unexpected transport"); + ESP_LOGE(TAG, "Unexpected transport %s", transport); abort(); } - xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); - esp_mqtt_client_start(mqtt_client); - ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); - xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); + + if (selected_transport != current_transport) { + esp_mqtt_client_config_t config = {0}; + if (selected_transport == SSL || selected_transport == WSS) { + ESP_LOGI(TAG, "Set certificate"); + config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; + esp_mqtt_set_config(mqtt_client, &config); + } + switch (selected_transport) { + case NONE: + break; + case TCP: + ESP_LOGI(TAG, "[TCP transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + break; + case SSL: + ESP_LOGI(TAG, "[SSL transport] Startup.."); + config.uri = CONFIG_EXAMPLE_BROKER_SSL_URI; + break; + case WS: + ESP_LOGI(TAG, "[WS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + break; + case WSS: + ESP_LOGI(TAG, "[WSS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + break; + } + } +} +void publish_test(const char *line) +{ + char pattern[32]; + char transport[32]; + int repeat = 0; + int enqueue = 0; + + esp_mqtt_client_stop(mqtt_client); + + static bool is_test_init = false; + if (!is_test_init) { + test_init(); + is_test_init = true; + } + + sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); + ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); + pattern_setup(pattern, repeat); + xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); + configure_client(transport); + esp_mqtt_client_start(mqtt_client); + + ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); + + xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); for (int i = 0; i < expected_published; i++) { int msg_id; if (enqueue) { From 999710fd6abece4c563a9e7eddcfb1dff4ea6412 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 23 Aug 2021 09:46:56 -0300 Subject: [PATCH 2/3] Docs: Fix Typo in license version - Typo in Apache License version for ESP_MQTT Merges https://github.com/espressif/esp-idf/issues/7311 --- docs/en/COPYRIGHT.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/COPYRIGHT.rst b/docs/en/COPYRIGHT.rst index a79251bc59..ea02eb0a4b 100644 --- a/docs/en/COPYRIGHT.rst +++ b/docs/en/COPYRIGHT.rst @@ -57,7 +57,7 @@ These third party libraries can be included into the application (firmware) prod * :component:`Asio `, Copyright (c) 2003-2018 Christopher M. Kohlhoff is licensed under the Boost Software License as described in :component_file:`COPYING file`. -* :component:`ESP-MQTT ` MQTT Package (contiki-mqtt) - Copyright (c) 2014, Stephen Robinson, MQTT-ESP - Tuan PM is licensed under Apache License 2,0 as described in :component_file:`LICENSE file `. +* :component:`ESP-MQTT ` MQTT Package (contiki-mqtt) - Copyright (c) 2014, Stephen Robinson, MQTT-ESP - Tuan PM is licensed under Apache License 2.0 as described in :component_file:`LICENSE file `. * :component:`BLE Mesh ` is adapted from Zephyr Project, Copyright (c) 2017-2018 Intel Corporation and licensed under Apache License 2.0 From 00b117058875857ade05328f24b3ae8797c0a504 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 7 Sep 2021 11:26:59 +0200 Subject: [PATCH 3/3] CI: Update mqtt test to cleanup connect-publish-URI interaction --- .../mqtt/publish_connect_test/app_test.py | 5 +++++ .../publish_connect_test/main/connect_test.c | 14 ++++++++++++ .../publish_connect_test/main/publish_test.c | 22 ++++++++++--------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 580e28568b..738951bd0d 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -245,6 +245,9 @@ def connection_tests(dut, cases): set_server_cert_cn(ip) server_port = 2222 + def teardown_connection_suite(): + dut.write('conn teardown 0 0') + def start_connection_case(case, desc): print('Starting {}: {}'.format(case, desc)) case_id = cases[case] @@ -301,6 +304,8 @@ def connection_tests(dut, cases): else: raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) + teardown_connection_suite() + @ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index 112c000f59..aa03f1988c 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -70,6 +70,17 @@ static void create_client(void) esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); mqtt_client = client; esp_mqtt_client_start(client); + ESP_LOGI(TAG, "mqtt client created for connection tests"); +} + +static void destroy_client(void) +{ + if (mqtt_client) { + esp_mqtt_client_stop(mqtt_client); + esp_mqtt_client_destroy(mqtt_client); + mqtt_client = NULL; + ESP_LOGI(TAG, "mqtt client for connection tests destroyed"); + } } static void connect_no_certs(const char *host, const int port) @@ -200,6 +211,9 @@ void connection_test(const char *line) if (mqtt_client == NULL) { create_client(); } + if (strcmp(host, "teardown") == 0) { + destroy_client();; + } ESP_LOGI(TAG, "CASE:%d, connecting to mqtts://%s:%d ", test_case, host, port); running_test_case = test_case; switch (test_case) { diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 8801a4983f..38cd136273 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -159,17 +159,12 @@ static void configure_client(char *transport) if (selected_transport != current_transport) { esp_mqtt_client_config_t config = {0}; - if (selected_transport == SSL || selected_transport == WSS) { - ESP_LOGI(TAG, "Set certificate"); - config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; - esp_mqtt_set_config(mqtt_client, &config); - } switch (selected_transport) { case NONE: break; case TCP: ESP_LOGI(TAG, "[TCP transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + config.uri = CONFIG_EXAMPLE_BROKER_TCP_URI; break; case SSL: ESP_LOGI(TAG, "[SSL transport] Startup.."); @@ -177,14 +172,21 @@ static void configure_client(char *transport) break; case WS: ESP_LOGI(TAG, "[WS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + config.uri = CONFIG_EXAMPLE_BROKER_WS_URI; break; case WSS: ESP_LOGI(TAG, "[WSS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + config.uri = CONFIG_EXAMPLE_BROKER_WSS_URI; break; } + if (selected_transport == SSL || selected_transport == WSS) { + ESP_LOGI(TAG, "Set certificate"); + config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; + } + esp_mqtt_set_config(mqtt_client, &config); + } + } void publish_test(const char *line) { @@ -193,12 +195,12 @@ void publish_test(const char *line) int repeat = 0; int enqueue = 0; - esp_mqtt_client_stop(mqtt_client); - static bool is_test_init = false; if (!is_test_init) { test_init(); is_test_init = true; + } else { + esp_mqtt_client_stop(mqtt_client); } sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue);