Add unit-test for orchestration-server. 88/203188/1
authorheewon Park <h_w.park@samsung.com>
Wed, 10 Apr 2019 10:48:43 +0000 (19:48 +0900)
committerheewon Park <h_w.park@samsung.com>
Wed, 10 Apr 2019 10:48:43 +0000 (19:48 +0900)
   - Update Makefile for unittest
   - Add unit-test file.

Change-Id: Idc6b4d70d21e45d705c869ca5ae5f94634ecb844
Signed-off-by: heewon Park <h_w.park@samsung.com>
Makefile
src/CMain/inc/gdbus_interface.h [new file with mode: 0644]
src/CMain/src/orchestration_server.c
src/CMain/unittest/Makefile [new file with mode: 0644]
src/CMain/unittest/test_orchestration_server.cpp [new file with mode: 0644]

index 019505720a1a992554eeec0757234e43d3a15342..b25175d408cc61df075b379962107ffc92c44e8a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -31,6 +31,7 @@ CMAIN_DIR                     := $(BASE_DIR)/src/CMain
 CMAIN_INC_DIR          := $(CMAIN_DIR)/inc
 CMAIN_BIN_DIR          := $(CMAIN_DIR)/bin
 CMAIN_LIB_DIR          := $(CMAIN_DIR)/lib
+CMAIN_TEST_DIR         := $(CMAIN_DIR)/unittest
 
 # Library package
 LIBPKG_LIB_FILE                := liborchestration-client.so
@@ -94,6 +95,7 @@ test:
        $(GOCOVER) report coverage.out
        $(GOCOVER)-html coverage.out > coverage.html
        firefox coverage.html &
+       $(MAKE) -C $(CMAIN_TEST_DIR)
 
 ## build clean
 clean:
diff --git a/src/CMain/inc/gdbus_interface.h b/src/CMain/inc/gdbus_interface.h
new file mode 100644 (file)
index 0000000..54f5379
--- /dev/null
@@ -0,0 +1,65 @@
+#ifndef __ORCHESTRATION_GDBUS_INTERFACE__
+#define __ORCHESTRATION_GDBUS_INTERFACE__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <gio/gio.h>
+
+typedef GDBusConnection *(*_g_bus_get_sync)(GBusType bus_type, GCancellable *cancellable, GError **error);
+typedef guint (*_g_bus_own_name)(GBusType bus_type,
+                                 const gchar *name,
+                                 GBusNameOwnerFlags flags,
+                                 GBusAcquiredCallback bus_acquired_handler,
+                                 GBusNameAcquiredCallback name_acquired_handler,
+                                 GBusNameLostCallback name_lost_handler,
+                                 gpointer user_data,
+                                 GDestroyNotify user_data_free_func);
+typedef void (*_g_bus_unown_name)(guint owner_id);
+typedef GDBusNodeInfo *(*_g_dbus_node_info_new_for_xml)(const gchar *xml_data,
+                                                        GError **error);
+typedef void (*_g_dbus_node_info_unref)(GDBusNodeInfo *info);
+typedef guint (*_g_dbus_connection_register_object)(GDBusConnection *connection,
+                                                    const gchar *object_path,
+                                                    GDBusInterfaceInfo *interface_info,
+                                                    const GDBusInterfaceVTable *vtable,
+                                                    gpointer user_data,
+                                                    GDestroyNotify user_data_free_func,
+                                                    GError **error);
+typedef void (*_g_dbus_error_register_error_domain)(const gchar *error_domain_quark_name,
+                                                    volatile gsize *quark_volatile,
+                                                    const GDBusErrorEntry *entries,
+                                                    guint num_entries);
+typedef GError *(*_g_error_new)(GQuark domain,
+                                gint code,
+                                const gchar *format,
+                                ...);
+typedef void (*_g_error_free)(GError *error);
+typedef void (*_g_dbus_method_invocation_return_value)(GDBusMethodInvocation *invocation,
+                                                       GVariant *parameters);
+typedef void (*_g_dbus_method_invocation_return_gerror)(GDBusMethodInvocation *invocation,
+                                                        const GError *error);
+/* ---------------------------------------------------------------------------------------------------- */
+
+typedef struct
+{
+    _g_bus_get_sync get_sync;
+    _g_bus_own_name own_name;
+    _g_bus_unown_name unown_name;
+    _g_dbus_node_info_new_for_xml node_info_new_for_xml;
+    _g_dbus_node_info_unref node_info_unref;
+    _g_dbus_connection_register_object connection_register_object;
+    _g_dbus_error_register_error_domain register_error_domain;
+    _g_error_new error_new;
+    _g_error_free error_free;
+    _g_dbus_method_invocation_return_value invocation_return_value;
+    _g_dbus_method_invocation_return_gerror invocation_return_gerror;
+} _gdbus_interface;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ORCHESTRATION_GDBUS_INTERFACE__ */
\ No newline at end of file
index 6bc1dc00db3c6a7619aeee1d0d9b3fc76ffb3bbe..08ae27f80c34bbfcff64483c0ae3ba399cda7ceb 100755 (executable)
@@ -1,55 +1,8 @@
-#include <gio/gio.h>
 #include <stdio.h>
 #include <stdlib.h>
 
-#include <orchestration.h>
-#include <orchestration_server.h>
-
-/* ---------------------------------------------------------------------------------------------------- */
-typedef GDBusConnection *(*_g_bus_get_sync)(GBusType bus_type, GCancellable *cancellable, GError **error);
-typedef guint (*_g_bus_own_name)(GBusType bus_type,
-                                 const gchar *name,
-                                 GBusNameOwnerFlags flags,
-                                 GBusAcquiredCallback bus_acquired_handler,
-                                 GBusNameAcquiredCallback name_acquired_handler,
-                                 GBusNameLostCallback name_lost_handler,
-                                 gpointer user_data,
-                                 GDestroyNotify user_data_free_func);
-typedef void (*_g_bus_unown_name)(guint owner_id);
-typedef GDBusNodeInfo *(*_g_dbus_node_info_new_for_xml)(const gchar *xml_data,
-                                                        GError **error);
-typedef void (*_g_dbus_node_info_unref)(GDBusNodeInfo *info);
-typedef guint (*_g_dbus_connection_register_object)(GDBusConnection *connection,
-                                                    const gchar *object_path,
-                                                    GDBusInterfaceInfo *interface_info,
-                                                    const GDBusInterfaceVTable *vtable,
-                                                    gpointer user_data,
-                                                    GDestroyNotify user_data_free_func,
-                                                    GError **error);
-typedef GError *(*_g_error_new)(GQuark domain,
-                                gint code,
-                                const gchar *format,
-                                ...);
-typedef void (*_g_error_free)(GError *error);
-typedef void (*_g_dbus_method_invocation_return_value)(GDBusMethodInvocation *invocation,
-                                                       GVariant *parameters);
-typedef void (*_g_dbus_method_invocation_return_gerror)(GDBusMethodInvocation *invocation,
-                                                        const GError *error);
-/* ---------------------------------------------------------------------------------------------------- */
-
-typedef struct
-{
-    _g_bus_get_sync get_sync;
-    _g_bus_own_name own_name;
-    _g_bus_unown_name unown_name;
-    _g_dbus_node_info_new_for_xml node_info_new_for_xml;
-    _g_dbus_node_info_unref node_info_unref;
-    _g_dbus_connection_register_object connection_register_object;
-    _g_error_new error_new;
-    _g_error_free error_free;
-    _g_dbus_method_invocation_return_value invocation_return_value;
-    _g_dbus_method_invocation_return_gerror invocation_return_gerror;
-} _gdbus_interface;
+#include "gdbus_interface.h"
+#include "orchestration_server.h"
 
 static _gdbus_interface _gdbus;
 
@@ -90,6 +43,7 @@ void set_default_dbus_interface(void)
     _gdbus.node_info_new_for_xml = g_dbus_node_info_new_for_xml;
     _gdbus.node_info_unref = g_dbus_node_info_unref;
     _gdbus.connection_register_object = g_dbus_connection_register_object;
+    _gdbus.register_error_domain = g_dbus_error_register_error_domain;
     _gdbus.error_new = g_error_new;
     _gdbus.error_free = g_error_free;
     _gdbus.invocation_return_value = g_dbus_method_invocation_return_value;
@@ -99,10 +53,10 @@ void set_default_dbus_interface(void)
 static GQuark _ORCHESTRATION_ERROR_quark(void)
 {
     static volatile gsize quark_volatile = 0;
-    g_dbus_error_register_error_domain(_ORCHESTRATION_SVC_ERROR_DOMAIN,
-                                       &quark_volatile,
-                                       _orchestration_svc_errors,
-                                       G_N_ELEMENTS(_orchestration_svc_errors));
+    _gdbus.register_error_domain(_ORCHESTRATION_SVC_ERROR_DOMAIN,
+                                 &quark_volatile,
+                                 _orchestration_svc_errors,
+                                 G_N_ELEMENTS(_orchestration_svc_errors));
     return (GQuark)quark_volatile;
 }
 
@@ -110,13 +64,23 @@ static int _request_service(GVariant *parameters, GVariant **reply_body)
 {
     char *app_name, *service_info;
 
+    if (!parameters)
+    {
+        return ORCH_ERROR_INVALID_PARAMETER;
+    }
     g_variant_get(parameters, "(&ss)", &app_name, &service_info);
-
     if (app_name == NULL)
         return ORCH_ERROR_INVALID_PARAMETER;
 
     _request_service_cb(app_name, service_info);
 
+    *reply_body = g_variant_new("()");
+    if (*reply_body == NULL)
+    {
+        printf("Failed to make reply_body\n");
+        return ORCH_ERROR_FAULT;
+    }
+
     return ORCH_ERROR_NONE;
 }
 
@@ -140,16 +104,16 @@ static void _handle_method_call(
 
     if (ret == ORCH_ERROR_NONE)
     {
-        DEBUG("Orchestration service Success, method name : %s", method_name);
+        printf("Orchestration service Success, method name : %s\n", method_name);
         g_dbus_method_invocation_return_value(invocation, reply_body);
     }
     else
     {
-        DEBUG("Orchestration service fail, method name : %s", method_name);
+        printf("Orchestration service fail, method name : %s\n", method_name);
 
         GError *error = _gdbus.error_new(_ORCHESTRATION_ERROR_quark(), ret, "orchestration failed");
         _gdbus.invocation_return_gerror(invocation, error);
-        g_error_free(error);
+        _gdbus.error_free(error);
     }
 }
 
@@ -169,7 +133,7 @@ static int _get_sync(void)
         {
             if (error != NULL)
             {
-                DEBUG("Failed to get dbus [%s]", error->message);
+                printf("Failed to get dbus [%s]\n", error->message);
                 _gdbus.error_free(error);
             }
             return ORCH_ERROR_DBUS_FAILURE;
@@ -191,7 +155,7 @@ static int _own_name(void)
                                NULL, NULL);
     if (!owner_id)
     {
-        DEBUG("Failed to own name");
+        printf("Failed to own name\n");
         result = ORCH_ERROR_DBUS_FAILURE;
     }
     return result;
@@ -205,10 +169,10 @@ static int _set_node_info_new_for_xml(void)
     introspection_data = _gdbus.node_info_new_for_xml(introspection_xml, &error);
     if (!introspection_data)
     {
-        DEBUG("g_dbus_node_info_new_for_xml is failed");
+        printf("g_dbus_node_info_new_for_xml is failed\n");
         if (error != NULL)
         {
-            DEBUG("g_dbus_node_info_new_for_xml err [%s]", error->message);
+            printf("g_dbus_node_info_new_for_xml err [%s]\n", error->message);
             _gdbus.error_free(error);
         }
         result = ORCH_ERROR_DBUS_FAILURE;
@@ -227,7 +191,7 @@ static int _connection_register_object(void)
 
     if (orch_registration_id == 0)
     {
-        DEBUG("Failed to g_dbus_connection_register_object");
+        printf("Failed to g_dbus_connection_register_object\n");
         result = ORCH_ERROR_DBUS_FAILURE;
     }
     return result;
@@ -240,28 +204,27 @@ static int orch_dbus_initialize(void)
     result = _get_sync();
     if (result != ORCH_ERROR_NONE)
     {
-        DEBUG("Failed to _dbus_init");
+        printf("Failed to _dbus_init\n");
         goto out;
     }
 
     result = _own_name();
     if (result != ORCH_ERROR_NONE)
     {
-        DEBUG("Failed to _dbus_own_name");
+        printf("Failed to _dbus_own_name\n");
         goto out;
     }
 
     result = _set_node_info_new_for_xml();
     if (result != ORCH_ERROR_NONE)
     {
-        DEBUG("Failed to _set_node_info_new_for_xml");
+        printf("Failed to _set_node_info_new_for_xml\n");
         goto out;
     }
-
     result = _connection_register_object();
     if (result != ORCH_ERROR_NONE)
     {
-        DEBUG("Failed to _connection_register_object");
+        printf("Failed to _connection_register_object\n");
         goto out;
     }
 
@@ -276,7 +239,7 @@ int orchestration_server_initialize(request_service_cb cb)
     if (cb == NULL)
     {
         result = ORCH_ERROR_INVALID_PARAMETER;
-        DEBUG("request_service_cb is null");
+        printf("request_service_cb is null\n");
         goto out;
     }
     _request_service_cb = cb;
@@ -286,7 +249,7 @@ int orchestration_server_initialize(request_service_cb cb)
         result = orch_dbus_initialize();
         if (result != ORCH_ERROR_NONE)
         {
-            DEBUG("orch_dbus_initialize is failed");
+            printf("orch_dbus_initialize is failed\n");
             goto out;
         }
     }
diff --git a/src/CMain/unittest/Makefile b/src/CMain/unittest/Makefile
new file mode 100644 (file)
index 0000000..730f2ad
--- /dev/null
@@ -0,0 +1,39 @@
+#
+# Makefile
+#
+
+# Target parameter
+INC_DIR                := ../inc
+SRC_DIR                := ../src
+TEST_FILE      := test_orchestration_server.cpp
+EXEC_FILE      := test_orchestration_server
+OBJ_FILES      := *.o
+
+# Build parameter
+CC             := g++
+CFLAGS := -g -Wall -Werror
+LIBS    := -lgtest -lgtest_main -lpthread -fprofile-arcs -ftest-coverage
+# BUILDFLAGS := -lgtest -lgtest_main -lpthread -fprofile-arcs -ftest-coverage
+
+all: clean build execute lcov
+
+build:
+       $(CC) $(TEST_FILE) -o $(EXEC_FILE) $(CFLAGS) $(LIBS) -I $(INC_DIR) -I $(SRC_DIR) `pkg-config --libs --cflags gio-2.0 gio-unix-2.0 glib-2.0`
+
+execute:
+       ./$(EXEC_FILE)
+
+lcov:
+       # lcov -c --directory . --output-file orchestration_server.info --no-external
+       lcov -c --directory . --output-file orchestration_server.info
+       lcov --remove orchestration_server.info '*/test_orchestration_server.cpp' '/usr/include/*' -o orchestration_server.info
+       genhtml orchestration_server.info --output-directory out
+
+clean:
+       -rm -f *.gcno
+       -rm -f *.gcda
+       -rm -f *.info 
+       -rm -f $(EXEC_FILE)
+       -rm -rf out
+
+.PHONY: build clean
\ No newline at end of file
diff --git a/src/CMain/unittest/test_orchestration_server.cpp b/src/CMain/unittest/test_orchestration_server.cpp
new file mode 100644 (file)
index 0000000..8e5c4da
--- /dev/null
@@ -0,0 +1,253 @@
+extern "C"
+{
+#include "orchestration_server.c"
+#include <gio/gio.h>
+}
+
+#include <gtest/gtest.h>
+
+const char *APP_NAME = "APP_NAME";
+const char *SERVICE_INFO = "SERVICE_INFO";
+
+GDBusConnection *valid_GDBusConnection;
+GDBusNodeInfo *valid_GDBusNodeInfo;
+GDBusInterfaceInfo* valid_GDBusinterface;
+gboolean requestCbResult;
+
+void fake_request_cb(char *app_name, char *service_info)
+{
+    if (app_name == APP_NAME && service_info == SERVICE_INFO)
+    {
+        requestCbResult = true;
+    }
+    else{
+        requestCbResult = false;
+    }
+}
+
+GDBusConnection *fake_get_sync_return_NULL(GBusType bus_type, GCancellable *cancellable, GError **error)
+{
+    *error = NULL;
+    return NULL;
+}
+
+GDBusConnection *fake_get_sync_success(GBusType bus_type, GCancellable *cancellable, GError **error)
+{
+    *error = NULL;
+    valid_GDBusConnection = (GDBusConnection *)malloc(sizeof(int));
+    return valid_GDBusConnection;
+}
+
+guint fake_own_name_return_error(GBusType bus_type,
+                                 const gchar *name,
+                                 GBusNameOwnerFlags flags,
+                                 GBusAcquiredCallback bus_acquired_handler,
+                                 GBusNameAcquiredCallback name_acquired_handler,
+                                 GBusNameLostCallback name_lost_handler,
+                                 gpointer user_data,
+                                 GDestroyNotify user_data_free_func)
+{
+    return 0;
+}
+
+guint fake_own_name_success(GBusType bus_type,
+                            const gchar *name,
+                            GBusNameOwnerFlags flags,
+                            GBusAcquiredCallback bus_acquired_handler,
+                            GBusNameAcquiredCallback name_acquired_handler,
+                            GBusNameLostCallback name_lost_handler,
+                            gpointer user_data,
+                            GDestroyNotify user_data_free_func)
+{
+    return 1;
+}
+
+GDBusNodeInfo *fake_node_info_new_for_xml_return_NULL(const gchar *xml_data,
+                                                      GError **error)
+{
+    *error = NULL;
+    return NULL;
+}
+
+GDBusNodeInfo *fake_node_info_new_for_xml_success(const gchar *xml_data,
+                                                  GError **error)
+{
+    *error = NULL;
+    valid_GDBusNodeInfo = (GDBusNodeInfo *)malloc(sizeof(_GDBusNodeInfo));
+    GDBusInterfaceInfo* _interfaces[2];    
+    valid_GDBusNodeInfo->interfaces = _interfaces;
+    valid_GDBusinterface = (GDBusInterfaceInfo*)malloc(sizeof(_GDBusInterfaceInfo));
+    valid_GDBusNodeInfo->interfaces[0] = valid_GDBusinterface;
+    return valid_GDBusNodeInfo;
+}
+
+guint fake_connection_register_object_return_error(GDBusConnection *connection,
+                                                   const gchar *object_path,
+                                                   GDBusInterfaceInfo *interface_info,
+                                                   const GDBusInterfaceVTable *vtable,
+                                                   gpointer user_data,
+                                                   GDestroyNotify user_data_free_func,
+                                                   GError **error)
+{
+    return 0;
+}
+
+guint fake_connection_register_object_success(GDBusConnection *connection,
+                                              const gchar *object_path,
+                                              GDBusInterfaceInfo *interface_info,
+                                              const GDBusInterfaceVTable *vtable,
+                                              gpointer user_data,
+                                              GDestroyNotify user_data_free_func,
+                                              GError **error)
+{
+    return 1;
+}
+
+void fake_invocation_return_value(GDBusMethodInvocation *invocation,
+                                  GVariant *parameters)
+{
+}
+
+void fake_invocation_return_gerror(GDBusMethodInvocation *invocation,
+                                   const GError *error)
+{
+}
+
+GError *fake_error_new(GQuark domain,
+                       gint code,
+                       const gchar *format,
+                       ...)
+{
+    return NULL;
+}
+
+void fake_error_free(GError *error)
+{
+}
+
+
+void fake_error_register_error_domain(const gchar *error_domain_quark_name,
+                                      volatile gsize *quark_volatile,
+                                      const GDBusErrorEntry *entries,
+                                      guint num_entries)
+{
+}
+
+class OrchestrationServerTests : public testing::Test
+{
+  protected:
+    virtual void SetUp()
+    {
+    }
+
+    virtual void TearDown()
+    {
+    }
+};
+
+TEST_F(OrchestrationServerTests, WhenCalledServerInit_WithInvalidcb_ExpectedErrorReturn)
+{
+    ASSERT_EQ(orchestration_server_initialize(NULL), ORCH_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledServerInit_OccuredDbusError_ExpectedErrorReturn)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_return_NULL;
+
+    ASSERT_EQ(orchestration_server_initialize(fake_request_cb), ORCH_ERROR_DBUS_FAILURE);
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledOrchDbusInit_GetSyncFailure_ExpectedErrorReturn)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_return_NULL;
+
+    ASSERT_EQ(orch_dbus_initialize(), ORCH_ERROR_DBUS_FAILURE);
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledOrchDbusInit_OwnNameFailure_ExpectedErrorReturn)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_success;
+    _gdbus.own_name = fake_own_name_return_error;
+
+    ASSERT_EQ(orch_dbus_initialize(), ORCH_ERROR_DBUS_FAILURE);
+
+    free(valid_GDBusConnection);
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledOrchDbusInit_SetNodeInfoNewForXMLFailure_ExpectedErrorReturn)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_success;
+    _gdbus.own_name = fake_own_name_success;
+    _gdbus.node_info_new_for_xml = fake_node_info_new_for_xml_return_NULL;
+
+    ASSERT_EQ(orch_dbus_initialize(), ORCH_ERROR_DBUS_FAILURE);
+
+    free(valid_GDBusConnection);
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledOrchDbusInit_ConnectionRegisterObjectFailure_ExpectedErrorReturn)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_success;
+    _gdbus.own_name = fake_own_name_success;
+    _gdbus.node_info_new_for_xml = fake_node_info_new_for_xml_success;
+    _gdbus.connection_register_object = fake_connection_register_object_return_error;
+
+    ASSERT_EQ(orch_dbus_initialize(), ORCH_ERROR_DBUS_FAILURE);
+
+    free(valid_GDBusConnection);
+    free(valid_GDBusNodeInfo);
+    free(valid_GDBusinterface);
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledServerInit_ExpectedSuccess)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_success;
+    _gdbus.own_name = fake_own_name_success;
+    _gdbus.node_info_new_for_xml = fake_node_info_new_for_xml_success;
+    _gdbus.connection_register_object = fake_connection_register_object_success;
+
+    ASSERT_EQ(orchestration_server_initialize(fake_request_cb), ORCH_ERROR_NONE);
+
+    free(valid_GDBusConnection);
+    free(valid_GDBusNodeInfo);
+    requestCbResult = false;
+}
+
+TEST_F(OrchestrationServerTests, WhenCalledHandleMethodCall_WithRequestServiceMethod_WithInvalidParam_ExpectedReturnError)
+{
+    set_default_dbus_interface();
+    _gdbus.get_sync = fake_get_sync_success;
+    _gdbus.own_name = fake_own_name_success;
+    _gdbus.node_info_new_for_xml = fake_node_info_new_for_xml_success;
+    _gdbus.connection_register_object = fake_connection_register_object_success;
+    _gdbus.invocation_return_value = fake_invocation_return_value;
+    _gdbus.invocation_return_gerror = fake_invocation_return_gerror;
+    _gdbus.register_error_domain = fake_error_register_error_domain;
+    _gdbus.error_new = fake_error_new;
+    _gdbus.error_free = fake_error_free;
+    _handle_method_call(NULL,
+                        NULL,
+                        NULL,
+                        NULL,
+                        "request_service",
+                        NULL,
+                        NULL,
+                        NULL);
+
+    EXPECT_EQ(requestCbResult, false);
+
+    free(valid_GDBusConnection);
+    free(valid_GDBusNodeInfo);
+}
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}