Sfoglia il codice sorgente

[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.
Euripedes Rocha 4 anni fa
parent
commit
307eb5f17f

+ 1 - 1
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)

+ 76 - 66
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,58 +240,7 @@ class TlsServer:
             self.shutdown.set()
 
 
-@ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps')
-def test_app_protocol_mqtt_publish_connect(env, extra_data):
-    """
-    steps:
-      1. join AP
-      2. connect to uri specified in the config
-      3. send and receive data
-    """
-    dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test')
-    # check and log bin size
-    binary_file = os.path.join(dut1.app.binary_path, 'mqtt_publish_connect_test.bin')
-    bin_size = os.path.getsize(binary_file)
-    ttfw_idf.log_performance('mqtt_publish_connect_test_bin_size', '{}KB'.format(bin_size // 1024))
-
-    # Look for test case symbolic names and publish configs
-    cases = {}
-    publish_cfg = {}
-    try:
-        def get_host_port_from_dut(dut1, config_option):
-            value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option])
-            if value is None:
-                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')
-        publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI')
-        publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WS_URI')
-        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')
-        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
+def connection_tests(dut, cases):
     ip = get_my_ip()
     set_server_cert_cn(ip)
     server_port = 2222
@@ -298,23 +248,23 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data):
     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))
+        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')
-            dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30)
+            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')
-            dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30)
-            dut1.expect('MQTT ERROR: 0x5')  # expecting 0x5 ... connection not authorized error
+            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')
-            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)
+            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()))
 
@@ -322,28 +272,28 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data):
         # 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)
+            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')
-        dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30)
-        dut1.expect('ESP-TLS ERROR: 0x8010')  # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA)
+        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')
-        dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30)
-        dut1.expect('ESP-TLS ERROR: 0x8010')  # expect ... handshake error (CERTIFICATE_VERIFY_FAILED)
+        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')
-            dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30)
+            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':
@@ -351,11 +301,71 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data):
             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):
+    """
+    steps:
+      1. join AP
+      2. connect to uri specified in the config
+      3. send and receive data
+    """
+    dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test')
+    # check and log bin size
+    binary_file = os.path.join(dut1.app.binary_path, 'mqtt_publish_connect_test.bin')
+    bin_size = os.path.getsize(binary_file)
+    ttfw_idf.log_performance('mqtt_publish_connect_test_bin_size', '{}KB'.format(bin_size // 1024))
+
+    # 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])
+            if value is None:
+                return None, None
+            return value.group(1), int(value.group(2))
+
+        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')
+        publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI')
+        publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WS_URI')
+        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 PUBLISH test case not found in sdkconfig')
+        raise
+
     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)

+ 67 - 67
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);
 }

+ 0 - 27
tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem

@@ -1,27 +0,0 @@
------BEGIN CERTIFICATE-----
-MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
-MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
-DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow
-SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT
-GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC
-AQ8AMIIBCgKCAQEAnNMM8FrlL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tt/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)
 {

+ 84 - 42
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();
     }
+
+
+    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);
 
+    xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
     for (int i = 0; i < expected_published; i++) {
         int msg_id;
         if (enqueue) {