unit testcases for mqtt
authorkang <pilseob.kang@samsung.com>
Fri, 8 Sep 2017 04:59:51 +0000 (13:59 +0900)
committerkang <pilseob.kang@samsung.com>
Mon, 11 Sep 2017 04:51:39 +0000 (13:51 +0900)
apps/examples/testcase/Kconfig
apps/examples/testcase/Makefile
apps/examples/testcase/README.txt
apps/examples/testcase/ta_tc/mqtt/utc/Kconfig [new file with mode: 0644]
apps/examples/testcase/ta_tc/mqtt/utc/Make.defs [new file with mode: 0644]
apps/examples/testcase/ta_tc/mqtt/utc/utc_mqtt_main.c [new file with mode: 0644]
apps/examples/testcase/tc_main.c

index 5da847b..08be0a4 100644 (file)
@@ -20,6 +20,7 @@ source "$APPSDIR/examples/testcase/ta_tc/systemio/utc/Kconfig"
 source "$APPSDIR/examples/testcase/ta_tc/systemio/itc/Kconfig"
 source "$APPSDIR/examples/testcase/ta_tc/device_management/utc/Kconfig"
 source "$APPSDIR/examples/testcase/ta_tc/device_management/itc/Kconfig"
+source "$APPSDIR/examples/testcase/ta_tc/mqtt/utc/Kconfig"
 endif #EXAMPLES_TESTCASE
 
 config USER_ENTRYPOINT
index 8af6105..4b7a6cf 100644 (file)
@@ -86,6 +86,7 @@ include ta_tc/arastorage/utc/Make.defs
 include ta_tc/arastorage/itc/Make.defs
 include ta_tc/device_management/utc/Make.defs
 include ta_tc/device_management/itc/Make.defs
+include ta_tc/mqtt/utc/Make.defs
 
 endif
 
index 4845a00..8467591 100644 (file)
@@ -18,4 +18,5 @@ examples/testcase
   * CONFIG_EXAMPLES_TESTCASE_SYSTEMIO_ITC
   * CONFIG_EXAMPLES_TESTCASE_DM_UTC
   * CONFIG_EXAMPLES_TESTCASE_DM_ITC
+  * CONFIG_EXAMPLES_TESTCASE_MQTT_UTC
 
diff --git a/apps/examples/testcase/ta_tc/mqtt/utc/Kconfig b/apps/examples/testcase/ta_tc/mqtt/utc/Kconfig
new file mode 100644 (file)
index 0000000..a05a28d
--- /dev/null
@@ -0,0 +1,27 @@
+#
+# For a description of the syntax of this configuration file,
+# see kconfig-language at https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
+#
+
+config EXAMPLES_TESTCASE_MQTT_UTC
+       bool "MQTT UTC TestCase Example"
+       depends on NETUTILS_MQTT
+       default n
+       ---help---
+               Enable the MQTT TestCase example
+
+if EXAMPLES_TESTCASE_MQTT_UTC
+
+config EXAMPLES_TESTCASE_MQTT_BROKER_ADDR
+       string "MQTT broker IP address for UTC"
+       default "127.0.0.1"
+       ---help---
+               MQTT broker's ip address to connect
+
+config EXAMPLES_TESTCASE_MQTT_BROKER_PORT
+       int "MQTT broker port for UTC"
+       default 1883
+       ---help---
+               MQTT broker's port to connect
+
+endif
diff --git a/apps/examples/testcase/ta_tc/mqtt/utc/Make.defs b/apps/examples/testcase/ta_tc/mqtt/utc/Make.defs
new file mode 100644 (file)
index 0000000..97daaa5
--- /dev/null
@@ -0,0 +1,26 @@
+###########################################################################
+#
+# Copyright 2017 Samsung Electronics All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+# either express or implied. See the License for the specific
+# language governing permissions and limitations under the License.
+#
+###########################################################################
+
+ifeq ($(CONFIG_EXAMPLES_TESTCASE_MQTT_UTC),y)
+CSRCS += utc_mqtt_main.c
+
+# include mqtt build support
+
+DEPPATH += --dep-path ta_tc/mqtt/utc
+VPATH += :ta_tc/mqtt/utc
+endif
diff --git a/apps/examples/testcase/ta_tc/mqtt/utc/utc_mqtt_main.c b/apps/examples/testcase/ta_tc/mqtt/utc/utc_mqtt_main.c
new file mode 100644 (file)
index 0000000..5e7bf4f
--- /dev/null
@@ -0,0 +1,483 @@
+/****************************************************************************
+ *
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+#include <tinyara/config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <apps/shell/tash.h>
+#include <mqtt/mqtt_api.h>
+#include "tc_common.h"
+
+#define UTC_MQTT_TOPIC "topic_utc"
+#define UTC_PUB_CLIENT_ID "utc_mqtt_pub/02"
+#define UTC_SUB_CLIENT_ID "utc_mqtt_sub/01"
+#define UTC_MQTT_MESSAGE "utc hello mqtt"
+#define UTC_MQTT_LOGE printf("%s:%d\n", __FUNCTION__, __LINE__)
+#define UTC_MQTT_DEBUG
+#ifdef UTC_MQTT_DEBUG
+#define UTC_MQTT_LOGD printf
+#else
+#define UTC_MQTT_LOGD
+#endif
+#define UTC_MQTT_WAIT_SIGNAL \
+       do {                                                                            \
+               pthread_mutex_lock(&g_ocfmutex);                        \
+               pthread_cond_wait(&g_ocfcond, &g_ocfmutex);     \
+               pthread_mutex_unlock(&g_ocfmutex);                      \
+       } while (0)
+
+#define UTC_MQTT_SEND_SIGNAL                                   \
+       do {                                                                            \
+               pthread_mutex_lock(&g_ocfmutex);                        \
+               pthread_cond_signal(&g_ocfcond);                        \
+               pthread_mutex_unlock(&g_ocfmutex);                      \
+       } while (0)
+
+static char g_mqtt_msg[] = UTC_MQTT_MESSAGE;
+static pthread_mutex_t g_ocfmutex = PTHREAD_MUTEX_INITIALIZER;;
+static pthread_cond_t g_ocfcond;
+static mqtt_client_t *g_mqtt_pub_handle = NULL;
+static mqtt_client_t *g_mqtt_sub_handle = NULL;
+
+extern sem_t tc_sem;
+extern int working_tc;
+/**
+ * Private Functions
+ */
+static int _utc_mqtt_init(void)
+{
+       g_mqtt_sub_handle = g_mqtt_pub_handle = NULL;
+
+       int res = pthread_mutex_init(&g_ocfmutex, NULL);
+       if (res != 0) {
+               UTC_MQTT_LOGE;
+               return -1;
+       }
+       res = pthread_cond_init(&g_ocfcond, NULL);
+       if (res != 0) {
+               UTC_MQTT_LOGE;
+               return -1;
+       }
+
+       return 0;
+}
+
+static void _utc_mqtt_deinit(void)
+{
+       pthread_mutex_destroy(&g_ocfmutex);
+       pthread_cond_destroy(&g_ocfcond);
+
+       return ;
+}
+
+static void on_connect(void *client, int result)
+{
+       mqtt_client_t *id = (mqtt_client_t *)client;
+       if (!id || !id->config) {
+               goto send_signal;
+       }
+
+       UTC_MQTT_LOGD("%s: %s, %d\n", __FUNCTION__, id->config->client_id, result);
+send_signal:
+       UTC_MQTT_SEND_SIGNAL;
+}
+
+static void on_disconnect(void *client, int result)
+{
+       mqtt_client_t *id = (mqtt_client_t *)client;
+       if (!id || !id->config) {
+               goto send_signal;
+       }
+       UTC_MQTT_LOGD("%s: %s, %d\n", __FUNCTION__, id->config->client_id, result);
+send_signal:
+       UTC_MQTT_SEND_SIGNAL;
+}
+
+static void on_publish(void *client, int msg_id)
+{
+       mqtt_client_t *id = (mqtt_client_t *)client;
+       if (!id || !id->config) {
+               goto send_signal;
+       }
+       UTC_MQTT_LOGD("%s: %s, %d\n", __FUNCTION__, id->config->client_id, msg_id);
+send_signal:
+       UTC_MQTT_SEND_SIGNAL;
+}
+
+static void on_message(void *client, mqtt_msg_t *msg)
+{
+       mqtt_client_t *id = (mqtt_client_t *)client;
+       if (!id || !id->config) {
+               return;
+       }
+       UTC_MQTT_LOGD("%s: %s, %p\n", __FUNCTION__, id->config->client_id, msg);
+}
+
+static void on_subscribe(void *client, int msg_id, int qos_count, const int *granted_qos)
+{
+       mqtt_client_t *id = (mqtt_client_t *)client;
+       if (!id || !id->config) {
+               return;
+       }
+       UTC_MQTT_LOGD("%s: %s, %d\n", __FUNCTION__, id->config->client_id, msg_id);
+}
+
+static void on_unsubscribe(void *client, int msg_id)
+{
+       mqtt_client_t *id = (mqtt_client_t *)client;
+       if (!id || !id->config) {
+               return;
+       }
+       UTC_MQTT_LOGD("%s: %s, %d\n", __FUNCTION__, id->config->client_id, msg_id);
+}
+
+
+static mqtt_client_config_t g_mqtt_pub_config = {
+       UTC_PUB_CLIENT_ID,
+       "", "", 1, 3, 0, 0,
+       on_connect,
+       on_disconnect,
+       on_publish,
+       on_message,
+       on_subscribe,
+       on_unsubscribe};
+
+static mqtt_client_config_t g_mqtt_sub_config = {
+       UTC_SUB_CLIENT_ID,
+       "", "", 1, 3, 0, 0,
+       on_connect,
+       on_disconnect,
+       on_publish,
+       on_message,
+       on_subscribe,
+       on_unsubscribe};
+
+
+/**
+ * @testcase         utc_mqtt_init_client_n
+ * @brief            Initialize mqtt stack
+ * @scenario         Initialize mqtt stack to connect broker
+ * @apicovered       mqtt_init_client
+ * @precondition     none
+ * @postcondition    none
+ */
+static void utc_mqtt_init_client_n(void)
+{
+       mqtt_client_t *res = mqtt_init_client(NULL);
+       TC_ASSERT_EQ("mqtt_init_client", res, NULL);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_init_client_p
+ * @brief            Initialize mqtt stack
+ * @scenario         Initialize mqtt stack to connect broker
+ * @apicovered       mqtt_init_client
+ * @precondition     none
+ * @postcondition    none
+ */
+static void utc_mqtt_init_client_p(void)
+{
+       g_mqtt_sub_handle = mqtt_init_client(&g_mqtt_sub_config);
+       TC_ASSERT_NEQ("utc_mqtt_init_client_p", g_mqtt_sub_handle, NULL);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_connect_n
+ * @brief            connect to broker
+ * @scenario         connect to broker not exist, it should return error
+ * @apicovered       mqtt_connect
+ * @precondition     mqtt_init_client
+ * @postcondition    none
+ */
+static void utc_mqtt_connect_n(void)
+{
+       int res = mqtt_connect(g_mqtt_sub_handle, "", -1, 0);
+       TC_ASSERT_LT("mqtt_connect", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_connect_p
+ * @brief            connect to broker
+ * @scenario         connect to broker not exist, it should return error
+ * @apicovered       mqtt_connect
+ * @precondition     mqtt_init_client
+ * @postcondition    none
+ */
+static void utc_mqtt_connect_p(void)
+{
+       int res = mqtt_connect(g_mqtt_sub_handle, CONFIG_EXAMPLES_TESTCASE_MQTT_BROKER_ADDR,
+                                                  CONFIG_EXAMPLES_TESTCASE_MQTT_BROKER_PORT, 0);
+       TC_ASSERT_EQ("mqtt_connect", res, 0);
+
+       UTC_MQTT_WAIT_SIGNAL;
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_subscribe_n
+ * @brief            subscribe a message from broker with an invalid input
+ * @scenario         subscribe a message
+ * @apicovered       mqtt_subscribe
+ * @precondition     mqtt_connect
+ * @postcondition    none
+ */
+static void utc_mqtt_subscribe_n(void)
+{
+       int res = mqtt_subscribe(NULL, UTC_MQTT_TOPIC, 0);
+       TC_ASSERT_LT("mqtt_subscribe", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_subscribe_p
+ * @brief            subscribe a message from broker with a valid input
+ * @scenario         subscribe a message
+ * @apicovered       mqtt_subscribe
+ * @precondition     mqtt_connect
+ * @postcondition    none
+ */
+static void utc_mqtt_subscribe_p(void)
+{
+       int res = mqtt_subscribe(g_mqtt_sub_handle, UTC_MQTT_TOPIC, 0);
+       TC_ASSERT_EQ("mqtt_subscribe", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_publish_n
+ * @brief            publish a message with an invalid input
+ * @scenario         publish a message
+ * @apicovered       mqtt_publish
+ * @precondition     mqtt_connect
+ * @postcondition    none
+ */
+static void utc_mqtt_publish_n(void)
+{
+       int res = mqtt_publish(NULL, UTC_MQTT_TOPIC, "", 0, 0, 0);
+       TC_ASSERT_LT("mqtt_publish", res, 0);
+
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_publish_p
+ * @brief            publish a message
+ * @scenario         publish a message
+ * @apicovered       mqtt_publish
+ * @precondition     mqtt_connect
+ * @postcondition    none
+ */
+static void utc_mqtt_publish_p(void)
+{
+       /* Prepare the publisher*/
+       g_mqtt_pub_handle = mqtt_init_client(&g_mqtt_pub_config);
+       if (!g_mqtt_pub_handle) {
+               UTC_MQTT_LOGE;
+               return ;
+       }
+
+       int res = mqtt_connect(g_mqtt_pub_handle, CONFIG_EXAMPLES_TESTCASE_MQTT_BROKER_ADDR,
+                                                  CONFIG_EXAMPLES_TESTCASE_MQTT_BROKER_PORT, 0);
+       if (res < 0) {
+               UTC_MQTT_LOGE;
+               return ;
+       }
+
+       UTC_MQTT_WAIT_SIGNAL;
+       res = mqtt_publish(g_mqtt_pub_handle, UTC_MQTT_TOPIC, g_mqtt_msg, sizeof(g_mqtt_msg), 0, 0);
+       TC_ASSERT_EQ("mqtt_publish", res, 0);
+       UTC_MQTT_WAIT_SIGNAL;
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_unsubscribe_n
+ * @brief            stop subscribing from a broker
+ * @scenario         call mqtt_unsubscribe with an invalid input
+ * @apicovered       mqtt_unsubscribe
+ * @precondition     mqtt_subscribe
+ * @postcondition    none
+ */
+static void utc_mqtt_unsubscribe_n(void)
+{
+       int res = mqtt_unsubscribe(NULL, NULL);
+       TC_ASSERT_LT("mqtt_unsubscribe", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_unsubscribe_p
+ * @brief            stop subscribing from a broker
+ * @scenario         call mqtt_unsubscribe
+ * @apicovered       mqtt_unsubscribe
+ * @precondition     mqtt_subscribe
+ * @postcondition    none
+ */
+static void utc_mqtt_unsubscribe_p(void)
+{
+       int res = mqtt_unsubscribe(g_mqtt_sub_handle, UTC_MQTT_TOPIC);
+       TC_ASSERT_EQ("mqtt_unsbscribe", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_disconnect_n
+ * @brief            disconnect to broker
+ * @scenario         disconnect to broker using an invalid handle
+ * @apicovered       mqtt_disconnect
+ * @precondition     mqtt_connect
+ * @postcondition    none
+ */
+static void utc_mqtt_disconnect_n(void)
+{
+       int res = mqtt_disconnect(NULL);
+       TC_ASSERT_LT("mqtt_disconnect", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_disconnect_p
+ * @brief            disconnect to broker
+ * @scenario         disconnect to broker using a valid handle
+ * @apicovered       mqtt_disconnect
+ * @precondition     mqtt_connect
+ * @postcondition    none
+ */
+static void utc_mqtt_disconnect_p(void)
+{
+       int res = mqtt_disconnect(g_mqtt_sub_handle);
+       TC_ASSERT_EQ("mqtt_disconnect", res, 0);
+
+       UTC_MQTT_WAIT_SIGNAL;
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_deinit_client_n
+ * @brief            free all resources used to mqtt stack
+ * @scenario         call mqtt_deinit with an invalid input
+ * @apicovered       mqtt_deinit_client
+ * @precondition     mqtt_init
+ * @postcondition    none
+ */
+static void utc_mqtt_deinit_client_n(void)
+{
+       int res = mqtt_deinit_client(NULL);
+       TC_ASSERT_LT("mqtt_deinit_client", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+/**
+ * @testcase         utc_mqtt_deinit_client_p
+ * @brief            free all resources used to mqtt stack
+ * @scenario         call mqtt_deinit with an invalid input
+ * @apicovered       mqtt_deinit_client
+ * @precondition     mqtt_init
+ * @postcondition    none
+ */
+static void utc_mqtt_deinit_client_p(void)
+{p
+       int res = mqtt_deinit_client(g_mqtt_pub_handle);
+       if (res < 0) {
+               UTC_MQTT_LOGE;
+       }
+       res = mqtt_deinit_client(g_mqtt_sub_handle);
+       TC_ASSERT_EQ("mqtt_deinit_client", res, 0);
+       TC_SUCCESS_RESULT();
+}
+
+
+static int mqtt_utc(int argc, FAR char *argv[])
+{
+       sem_wait(&tc_sem);
+       working_tc++;
+
+       total_fail = total_pass = 0;
+
+       printf("=== TIZENRT MQTT TC START! ===\n");
+       int res = _utc_mqtt_init();
+       if (res < 0) {
+               UTC_MQTT_LOGE;
+               goto exit;
+       }
+       utc_mqtt_init_client_n();
+       utc_mqtt_init_client_p();
+       utc_mqtt_connect_n();
+       utc_mqtt_connect_p();
+       utc_mqtt_subscribe_n();
+       utc_mqtt_subscribe_p();
+       utc_mqtt_publish_n();
+       utc_mqtt_publish_p();
+       utc_mqtt_unsubscribe_n();
+       utc_mqtt_unsubscribe_p();
+       utc_mqtt_disconnect_n();
+       utc_mqtt_disconnect_p();
+       utc_mqtt_deinit_client_n();
+       utc_mqtt_deinit_client_p();
+
+       _utc_mqtt_deinit();
+exit:
+       printf("\n=== TIZENRT MQTT TC COMPLETE ===\n");
+       printf("\t\tTotal pass : %d\n\t\tTotal fail : %d\n", total_pass, total_fail);
+
+       working_tc--;
+       sem_post(&tc_sem);
+
+       return 0;
+}
+
+
+#ifdef CONFIG_BUILD_KERNEL
+int main(int argc, FAR char *argv[])
+#else
+int utc_mqtt_main(int argc, char *argv[])
+#endif
+{
+
+#ifdef CONFIG_TASH
+       tash_cmd_install("mqtt_utc", mqtt_utc, TASH_EXECMD_SYNC);
+#else
+       mqtt_utc(argc, argv);
+#endif
+       return 0;
+}
+
index dd8cddb..3b9a13e 100644 (file)
@@ -50,6 +50,9 @@
 #if defined(CONFIG_EXAMPLES_TESTCASE_DM_UTC) || defined(CONFIG_EXAMPLES_TESTCASE_DM_ITC)
 #define TC_DM_STACK  20480
 #endif
+#if defined(CONFIG_EXAMPLES_TESTCASE_MQTT_UTC)
+#define TC_MQTT_STACK 4096
+#endif
 
 sem_t tc_sem;
 int working_tc;
@@ -67,6 +70,7 @@ extern int utc_sysio_main(int argc, char *argv[]);
 extern int itc_sysio_main(int argc, char *argv[]);
 extern int utc_dm_main(int argc, char *argv[]);
 extern int itc_dm_main(int argc, char *argv[]);
+extern int utc_mqtt_main(int argc, char *argv[]);
 
 /* Not yet */
 extern int mpu_tc_main(int argc, char *argv[]);
@@ -149,6 +153,13 @@ int tc_main(int argc, char *argv[])
                printf("System IO itc is not started, err = %d\n", pid);
        }
 #endif
+
+#ifdef CONFIG_EXAMPLES_TESTCASE_MQTT_UTC
+       pid = task_create("mqttutc", SCHED_PRIORITY_DEFAULT, TC_MQTT_STACK, utc_mqtt_main, argv);
+       if (pid < 0) {
+               printf("MQTT utc is not started, err = %d\n", pid);
+       }
+#endif
        do {
                sleep(5);
        } while (working_tc > 0);