Fix memory leak on unittest and add more testcases 44/186244/2
authorsinikang <sinikang@samsung.com>
Wed, 8 Aug 2018 08:40:24 +0000 (17:40 +0900)
committersinikang <sinikang@samsung.com>
Wed, 8 Aug 2018 08:45:33 +0000 (17:45 +0900)
Change-Id: Ia3b9f24d1581b0bf37d37a48f15d6360e1d1e6d1

gtest/unittest.cpp
include/at.h
src/at.c

index 9fb3491..89043f0 100644 (file)
@@ -652,7 +652,6 @@ TEST(LIBTCORE_AT, test_at_reqeust)
        tcore_at_free(at);
 }
 
-#if 0
 static gboolean __on_atcmd_cmti(TcoreAT *at, const GSList *lines, void *user_data)
 {
        int *checked = (int *)user_data;
@@ -662,7 +661,7 @@ static gboolean __on_atcmd_cmti(TcoreAT *at, const GSList *lines, void *user_dat
        std::cout << std::endl;
        std::cout <<"   data = " << (char *)lines->data << std::endl;
 
-       return FALSE;
+       return TRUE;
 }
 
 static gboolean __on_atcmd_cmt(TcoreAT *at, const GSList *lines, void *user_data)
@@ -683,13 +682,22 @@ static gboolean __on_atcmd_cmt(TcoreAT *at, const GSList *lines, void *user_data
        return TRUE;
 }
 
+static gboolean __on_atcmd_test(TcoreAT *at, const GSList *lines, void *user_data)
+{
+       std::cout << std::endl;
+       std::cout <<"   __on_atcmd_test() data = " << (char *)lines->data << std::endl;
+
+       return TRUE;
+}
+
+
 TEST(LIBTCORE_AT, test_at_notificatoin)
 {
        TcoreAT *at;
        TReturn ret;
        int checked = 0;
        char *msg = NULL;
-       gboolean bSuccess;
+       gboolean bRet;
 
        at = tcore_at_new(NULL);
        ASSERT_TRUE(at);
@@ -702,17 +710,16 @@ TEST(LIBTCORE_AT, test_at_notificatoin)
 
        /* Non type PDU message */
        msg = (char *)"+CMTI: \"SM\", 1\r\n";
-       bSuccess = tcore_at_process(at, strlen(msg), msg);
-       EXPECT_EQ(bSuccess, true);
+       bRet = tcore_at_process(at, strlen(msg), msg);
+       EXPECT_EQ(bRet, FALSE);
 
        /* callback check */
        EXPECT_EQ(checked, 1);
 
-
        /* Completed PDU message */
        msg = (char *)"+CMT: 19\r\n038121F3100481214301112170137192410404F17B590E\r\n";
-       bSuccess = tcore_at_process(at, strlen(msg), msg);
-       EXPECT_EQ(bSuccess, true);
+       bRet = tcore_at_process(at, strlen(msg), msg);
+       EXPECT_EQ(bRet, FALSE);
 
        /* callback check */
        EXPECT_EQ(checked, 2);
@@ -721,29 +728,39 @@ TEST(LIBTCORE_AT, test_at_notificatoin)
        /* Split PDU message */
        checked = 0;
        msg = (char *)"+CMT: 19\r\n";
-       bSuccess = tcore_at_process(at, strlen(msg), msg);
-       EXPECT_EQ(bSuccess, true);
+       bRet = tcore_at_process(at, strlen(msg), msg);
+       EXPECT_EQ(bRet, FALSE);
 
        /* callback check */
        EXPECT_EQ(checked, 0);
 
        msg = (char *)"038121F310";
-       bSuccess = tcore_at_process(at, strlen(msg), msg);
-       EXPECT_EQ(bSuccess, true);
+       bRet = tcore_at_process(at, strlen(msg), msg);
+       EXPECT_EQ(bRet, FALSE);
 
        /* callback check */
        EXPECT_EQ(checked, 0);
 
        msg = (char *)"0481214301112170137192410404F17B590E\r\n";
-       bSuccess = tcore_at_process(at, strlen(msg), msg);
-       EXPECT_EQ(bSuccess, true);
+       bRet = tcore_at_process(at, strlen(msg), msg);
+       EXPECT_EQ(bRet, FALSE);
+
+       ret = tcore_at_add_notification(at, "+test:", FALSE, __on_atcmd_test, NULL);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* Non type PDU message */
+       msg = (char *)"+test: \"TEST\", 1\r\n";
+       bRet = tcore_at_process(at, strlen(msg), msg);
+       EXPECT_EQ(bRet, FALSE);
+
+       ret = tcore_at_remove_notification(at, "+test:", __on_atcmd_test);
+
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
 
        /* callback check */
        EXPECT_EQ(checked, 2);
 
 }
-#endif
-
 
 static enum tcore_hook_return __on_send_hook(TcoreHal *hal, guint data_len, void *data, void *user_data)
 {
@@ -894,16 +911,26 @@ TEST(LIBTCORE_HAL, test_hal_pending)
        /* first item */
        pending1 = tcore_pending_new(NULL, 1);
        ASSERT_TRUE(pending1);
+
+       /* set priority */
        tcore_pending_set_priority(pending1, TCORE_PENDING_PRIORITY_IMMEDIATELY);
 
+       /* get priority */
        enum tcore_pending_priority result_priority;
        ret = tcore_pending_get_priority(pending1,  &result_priority);
        EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
        EXPECT_EQ(result_priority, TCORE_PENDING_PRIORITY_IMMEDIATELY);
 
+       /* get pending id */
        unsigned int id = tcore_pending_get_id(pending1);
        std::cout << std::endl << "     pending1 id = " << id << std::endl;
 
+       /* get pending send status */
+       gboolean send_status;
+       ret = tcore_pending_get_send_status(pending1, &send_status);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+       std::cout << std::endl << "     pending1 send_status = " << send_status << std::endl;
+
        /* second item */
        pending2 = tcore_pending_new(NULL, 1);
        ASSERT_TRUE(pending2);
@@ -913,6 +940,27 @@ TEST(LIBTCORE_HAL, test_hal_pending)
        std::cout << std::endl << "     pending2 audo_free_status = " << auto_free_status << std::endl;
        EXPECT_EQ(auto_free_status, TRUE);
 
+       /* set pending data */
+       char pending_data1[] = "pending_data1";
+       ret = tcore_pending_set_request_data(pending2, strlen(pending_data1), pending_data1);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* get pending data */
+       unsigned int data_len;
+       char *ref_data = (char *)tcore_pending_ref_request_data(pending2, &data_len);
+       EXPECT_EQ(strcmp(pending_data1, ref_data), 0);
+       std::cout << std::endl << "     pending2 data = " << ref_data << std::endl;
+
+       /* set pending data */
+       char pending_data2[] = "pending_data2";
+       ret = tcore_pending_set_request_data(pending2, strlen(pending_data2), pending_data2);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* get pending data */
+       ref_data = (char *)tcore_pending_ref_request_data(pending2, &data_len);
+       EXPECT_EQ(strcmp(pending_data2, ref_data), 0);
+       std::cout << std::endl << "     pending2 data = " << ref_data << std::endl;
+
        /* third item */
        pending3 = tcore_pending_new(NULL, 1);
        ASSERT_TRUE(pending3);
@@ -991,16 +1039,26 @@ TEST(LIBTCORE_HAL, test_hal_pending)
 
 TEST(LIBTCORE_QUEUE, test_queue_push)
 {
+       TcoreHal *h;
        TcoreQueue *q;
        TcorePending *pending1;
        TcorePending *pending2;
        TcorePending *pending3;
+       TcorePending *pending4;
+       TcorePending *pending5;
        TcorePending *pending = NULL;
        TReturn ret;
 
-       q = tcore_queue_new(NULL);
+       h = tcore_hal_new(NULL, "test_hal_for_queue", NULL, TCORE_HAL_MODE_CUSTOM);
+       ASSERT_TRUE(h);
+
+       q = tcore_queue_new(h);
        ASSERT_TRUE(q);
 
+       TcoreHal *ref_hal = tcore_queue_ref_hal(q);
+       EXPECT_EQ(ref_hal, h);
+       std::cout << std::endl << "     q's reference hal is matched with h " << std::endl;
+
        /* first item */
        pending1 = tcore_pending_new(NULL, 1);
        ASSERT_TRUE(pending1);
@@ -1027,6 +1085,10 @@ TEST(LIBTCORE_QUEUE, test_queue_push)
        EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
        std::cout << std::endl << "     pending3 is pushed into queue successufully " << std::endl;
 
+       pending = tcore_queue_ref_next_pending(q);
+       EXPECT_EQ(pending, pending1);
+       std::cout << std::endl << "     tcore_queue_ref_next_pending is pending1" << std::endl;
+
        TcorePending *head = tcore_queue_ref_head(q);
        EXPECT_EQ(head, pending1);
        std::cout << std::endl << "     head is pending1" << std::endl;
@@ -1045,23 +1107,147 @@ TEST(LIBTCORE_QUEUE, test_queue_push)
        pending = tcore_queue_pop_by_id(q, 1);
        EXPECT_EQ(pending, pending1);
        std::cout << std::endl << "     pending1 is poped from queue successufully " << std::endl;
+       tcore_pending_free(pending);
 
        pending = tcore_queue_pop_by_id(q, 1);
        EXPECT_EQ(pending, pending3);
        std::cout << std::endl << "     pending3 is poped from queue successufully " << std::endl;
+       tcore_pending_free(pending);
 
        pending = tcore_queue_pop_by_id(q, 1);
        EXPECT_EQ(pending, pending2);
        std::cout << std::endl << "     pending2 is poped from queue successufully " << std::endl;
+       tcore_pending_free(pending);
+
+       /* 4th item push */
+       pending4 = tcore_pending_new(NULL, 1);
+       ASSERT_TRUE(pending4);
+
+       ret = tcore_queue_push(q, pending4);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+       std::cout << std::endl << "     pending4 is pushed into queue successufully " << std::endl;
+       TcorePlugin *ref_plugin = tcore_pending_ref_plugin(pending4);
+       EXPECT_TRUE(!ref_plugin);
+       CoreObject *ref_co = tcore_pending_ref_core_object(pending4);
+       EXPECT_TRUE(!ref_co);
+
+       /* 5th item push */
+       pending5 = tcore_pending_new(NULL, 1);
+       ASSERT_TRUE(pending5);
+
+       ret = tcore_queue_push(q, pending5);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+       std::cout << std::endl << "     pending5 is pushed into queue successufully " << std::endl;
+
+       /* pop by pending */
+       pending = tcore_queue_pop_by_pending(q, pending4);
+       EXPECT_EQ(pending, pending4);
+       std::cout << std::endl << "     pending4 is poped from queue successufully " << std::endl;
+       tcore_pending_free(pending);
+
+       /* pop head pending */
+       pending = tcore_queue_pop(q);
+       EXPECT_EQ(pending, pending5);
+       std::cout << std::endl << "     pending5 is poped from queue successufully " << std::endl;
+       tcore_pending_free(pending);
 
        tcore_queue_free(q);
+       tcore_hal_free(h);
+}
+
+TEST(LIBTCORE_QUEUE, test_user_request_new)
+{
+       UserRequest *ur;
+       TReturn ret;
+
+       ur = tcore_user_request_new(NULL, "test_user_request");
+       ASSERT_TRUE(ur);
+
+       /* ref user request */
+       tcore_user_request_ref(ur);
+
+       /* unref user request */
+       tcore_user_request_unref(ur);
+
+       /* get modem name of user request */
+       char *modem_name = tcore_user_request_get_modem_name(ur);
+       std::cout << std::endl << "     ur modem name = " << modem_name << std::endl;
+
+       /* set userinfo of user request */
+       char user_info[] = "test_userinfo";
+       ret = tcore_user_request_set_user_info(ur, user_info);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* get userinfo */
+       char *ref_user_info = (char *)tcore_user_request_ref_user_info(ur);
+       std::cout << std::endl << "     ur user_info = " << ref_user_info << std::endl;
+
+       /* set data of user request */
+       char data[] = "test_data";
+       ret = tcore_user_request_set_data(ur, strlen(data), data);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* get data */
+       unsigned int data_len;
+       char *ref_data = (char *)tcore_user_request_ref_data(ur, &data_len);
+       std::cout << std::endl << "     ur data_length = " << data_len << " data = " << ref_data << std::endl;
+
+       /* set metainfo of user request */
+       char metainfo[] = "test_metainfo";
+       ret = tcore_user_request_set_metainfo(ur, strlen(metainfo), metainfo);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* get metainfo */
+       unsigned int metainfo_len;
+       char *ref_metainfo = (char *)tcore_user_request_ref_metainfo(ur, &metainfo_len);
+       std::cout << std::endl << "     ur metainfo_len = " << metainfo_len << " metainfo = " << ref_metainfo << std::endl;
+
+       tcore_user_request_free(ur);
+
+}
+
+static void __on_pending_send_callback(TcorePending *pending, gboolean result, void *user_data)
+{
+       std::cout << std::endl << "     __on_pending_send_callback() result =" << result << std::endl;
 }
 
 int g_response_flag = 0;
 static void __on_pending_response_callback(TcorePending *pending, int data_len, const void *data, void *user_data)
 {
-       std::cout << std::endl << "     __on_pending_response_callback() !!\n" << std::endl;
+       std::cout << std::endl << "     __on_pending_response_callback()" << std::endl;
        g_response_flag++;
+
+       if (data_len > 0 && data) {
+               char *resp_data = (char *)data;
+               std::cout << "  response data = " << resp_data << std::endl;
+       }
+}
+
+static void __on_pending_timeout_callback(TcorePending *p, void *user_data)
+{
+       std::cout << std::endl << "     __on_pending_timeout_callback()" << std::endl;
+}
+
+static void __on_ur_free_hook(UserRequest *ur)
+{
+       struct dbus_request_info *user_info;
+
+       std::cout << std::endl << "     _on_ur_free_hook()" << std::endl;
+
+       user_info = (struct dbus_request_info *)tcore_user_request_ref_user_info(ur);
+       if (user_info)
+               free(user_info);
+}
+
+static void __on_ur_response_hook(UserRequest *ur, enum tcore_response_command command,
+                                 unsigned int data_len, const void *data, void *user_data)
+{
+       std::cout << std::endl << "     __on_ur_response_hook()" << std::endl;
+
+       if (data_len > 0 && data) {
+               char *resp_data = (char *)data;
+               std::cout << "  response data = " << resp_data << std::endl;
+       }
 }
 
 TEST(LIBTCORE_QUEUE, test_hal_queue_callback)
@@ -1097,20 +1283,60 @@ TEST(LIBTCORE_QUEUE, test_hal_queue_callback)
        ret = tcore_hal_send_request(h, pending1);
        EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
 
+       /* set send callback */
+       ret = tcore_pending_set_send_callback(pending1, __on_pending_send_callback, NULL);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
        /* force update send state */
-       tcore_pending_emit_send_callback(pending1, TRUE);
+       ret = tcore_pending_emit_send_callback(pending1, TRUE);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* set timeout callback */
+       ret = tcore_pending_set_timeout_callback(pending1, __on_pending_timeout_callback, NULL);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* emit timeout callback */
+       ret = tcore_pending_emit_timeout_callback(pending1);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
 
        /* second item */
        ur2 = tcore_user_request_new(NULL, NULL);
        ASSERT_TRUE(ur2);
 
-       tcore_user_request_set_command(ur2, TREQ_NETWORK_SEARCH);
+       /* set command on user_request */
+       ret = tcore_user_request_set_command(ur2, TREQ_NETWORK_SEARCH);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* get command */
+       EXPECT_EQ(tcore_user_request_get_command(ur2), TREQ_NETWORK_SEARCH);
+       std::cout << std::endl << "     ur2 command  = TREQ_NETWORK_SEARCH" << std::endl;
 
        pending2 = tcore_pending_new(NULL, 1);
        ASSERT_TRUE(pending2);
 
-       tcore_pending_set_response_callback(pending2, __on_pending_response_callback, NULL);
-       tcore_pending_link_user_request(pending2, ur2);
+       ret = tcore_pending_set_response_callback(pending2, __on_pending_response_callback, NULL);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       char test_emit_resp[] = "test_emit_response";
+       ret = tcore_pending_emit_response_callback(pending2, strlen(test_emit_resp), test_emit_resp);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       ret = tcore_pending_link_user_request(pending2, ur2);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       UserRequest *ref_ur = tcore_pending_ref_user_request(pending2);
+       EXPECT_EQ(ref_ur, ur2);
+
+       /* set free hook callback */
+       ret = tcore_user_request_set_free_hook(ur2, __on_ur_free_hook);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* set response hook callback */
+       ret = tcore_user_request_set_response_hook(ur2, __on_ur_response_hook, NULL);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
+       /* ref communicator */
+       EXPECT_TRUE(!tcore_user_request_ref_communicator(ur2));
 
        ret = tcore_hal_send_request(h, pending2);
        EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
@@ -1130,6 +1356,10 @@ TEST(LIBTCORE_QUEUE, test_hal_queue_callback)
        p = tcore_queue_search_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH, FALSE);
        EXPECT_EQ(p, pending2);
 
+       char resp_custom[] = "test_resp";
+       ret = tcore_user_request_send_response(ur2, TRESP_CUSTOM, strlen(resp_custom), resp_custom);
+       EXPECT_EQ(ret, TCORE_RETURN_SUCCESS);
+
        /* cancel */
        g_response_flag = 0;
        ret = tcore_queue_cancel_pending_by_command(tcore_hal_ref_queue(h), TREQ_NETWORK_SEARCH);
@@ -1141,6 +1371,7 @@ TEST(LIBTCORE_QUEUE, test_hal_queue_callback)
 
 }
 
+
 int main(int argc, char **argv)
 {
        int ret = -1;
index 2111a48..bba0f2b 100644 (file)
@@ -128,7 +128,6 @@ TReturn tcore_at_remove_notification_full(TcoreAT *at, const char *prefix,
 TcoreATRequest *tcore_at_request_new(const char *cmd, const char *prefix,
        enum tcore_at_command_type type);
 void tcore_at_request_free(TcoreATRequest *req);
-void tcore_at_response_free(TcoreATResponse* rsp);
 
 gboolean tcore_at_process(TcoreAT *at,
        unsigned int data_len, const char *data);
index ddb3622..881498e 100644 (file)
--- a/src/at.c
+++ b/src/at.c
@@ -583,11 +583,6 @@ void tcore_at_request_free(TcoreATRequest *req)
        g_free(req);
 }
 
-void tcore_at_response_free(TcoreATResponse *rsp)
-{
-       _response_free(rsp);
-}
-
 /* To get the length value from little-endian bytes */
 static int __sum_4_bytes(const char *posn)
 {
@@ -1051,7 +1046,7 @@ void tcore_free_pending_timeout_at_request(TcoreAT *at)
        tcore_at_request_free(at->req);
        at->req = NULL;
 
-       tcore_at_response_free(at->resp);
+       _response_free(at->resp);
        at->resp = NULL;
 }