tests: Remove code duplication 13/281113/2
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 12 Sep 2022 09:05:18 +0000 (11:05 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Tue, 13 Sep 2022 06:35:56 +0000 (08:35 +0200)
Change-Id: I78fff774c4bd9d4dd0d085fabfbe62381649037f

tests/unit/CMakeLists.txt
tests/unit/test_common.c [new file with mode: 0644]
tests/unit/test_common.h [new file with mode: 0644]
tests/unit/test_dumpsys_client_system_api.c
tests/unit/test_dumpsys_client_user_api.c
tests/unit/test_dumpsys_service.c
tests/unit/test_libdumpsys.c

index da3327b012f093dd1892346f7aaed1630232d5ee..230619cffee61366ddc4819cf8ea9268e0418743 100644 (file)
@@ -14,6 +14,7 @@ FOREACH(flag ${dumpsys_service_pkgs_CFLAGS})
 ENDFOREACH(flag)
 
 ADD_DEFINITIONS(-DUNIT_TESTS)
+INCLUDE_DIRECTORIES("tests/unit/")
 
 STRING(REPLACE "-O2" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
 STRING(REPLACE "-D_FORTIFY_SOURCE=2 " "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
@@ -25,7 +26,7 @@ function(add_mocked_test)
         set(multiValueArgs SOURCE_DIRS)
         cmake_parse_arguments(T "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
 
-        add_executable(test_${T_NAME} test_${T_NAME}.c)
+        add_executable(test_${T_NAME} test_${T_NAME}.c test_common.c)
 
         add_definitions(-DTEST)
         target_link_libraries(test_${T_NAME} ${dumpsys_service_pkgs_LIBRARIES} -pie -lcmocka -lgcov)
diff --git a/tests/unit/test_common.c b/tests/unit/test_common.c
new file mode 100644 (file)
index 0000000..d2bd858
--- /dev/null
@@ -0,0 +1,19 @@
+#include "test_common.h"
+
+
+gboolean __wrap_g_dbus_connection_send_message (GDBusConnection *connection,
+                                GDBusMessage *message,
+                                GDBusSendMessageFlags flags,
+                                volatile guint32 *out_serial,
+                                GError **error)
+{
+       check_expected(connection);
+       check_expected(flags);
+       return mock_type(gboolean);
+}
+
+GUnixFDList* __wrap_g_dbus_message_get_unix_fd_list(GDBusMessage *message)
+{
+       check_expected(message);
+       return mock_ptr_type(GUnixFDList*);
+}
diff --git a/tests/unit/test_common.h b/tests/unit/test_common.h
new file mode 100644 (file)
index 0000000..12f8251
--- /dev/null
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <stdio.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <gio/gio.h>
+#include <cmocka.h>
+#include <pkgmgr-info.h>
+
+gboolean __wrap_g_dbus_connection_send_message (GDBusConnection *connection,
+                                GDBusMessage *message,
+                                GDBusSendMessageFlags flags,
+                                volatile guint32 *out_serial,
+                                GError **error);
+
+GUnixFDList* __wrap_g_dbus_message_get_unix_fd_list(GDBusMessage *message);
index e47ee5fbe3b9a223f2154c8f15a151d6e612bb85..90e75fce58c610215aafc30a66fb8c5f5ddfd5cd 100644 (file)
@@ -1,14 +1,4 @@
-#include <stdio.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <setjmp.h>
-#include <unistd.h>
-#include <cmocka.h>
-#include <fcntl.h>
-#include <gio/gio.h>
-#include <pkgmgr-info.h>
-
+#include "test_common.h"
 #include "dumpsys-system.c"
 
 GDBusConnection* __wrap_g_bus_get_sync(GBusType type, GCancellable *cancellable, GError **error)
@@ -47,35 +37,19 @@ guint __wrap_g_dbus_connection_register_object(GDBusConnection *connection,
        return 345;
 }
 
-GUnixFDList* __wrap_g_dbus_message_get_unix_fd_list(GDBusMessage *msg)
-{
-       return mock_ptr_type(GUnixFDList*);
-}
-
 int __wrap_g_unix_fd_list_get(GUnixFDList *list, int fd, GError *error)
 {
        check_expected(list);
        return mock_type(int);
 }
 
-gboolean __wrap_g_dbus_connection_send_message (GDBusConnection *connection,
-                                GDBusMessage *message,
-                                GDBusSendMessageFlags flags,
-                                volatile guint32 *out_serial,
-                                GError **error)
-{
-       check_expected(connection);
-       check_expected(flags);
-       return mock_type(gboolean);
-}
-
 GDBusMessage* __wrap_g_dbus_connection_send_message_with_reply_sync(GDBusConnection *conn,
-                                                                   GDBusMessage *message,
-                                                                   GDBusSendMessageFlags flags,
-                                                                   gint timeout_msec,
-                                                                   volatile guint32 *out_serial,
-                                                                   GCancellable *cancellable,
-                                                                   GError **error)
+                                                                                                                                                                                           GDBusMessage *message,
+                                                                                                                                                                                           GDBusSendMessageFlags flags,
+                                                                                                                                                                                           gint timeout_msec,
+                                                                                                                                                                                           volatile guint32 *out_serial,
+                                                                                                                                                                                           GCancellable *cancellable,
+                                                                                                                                                                                           GError **error)
 {
        check_expected_ptr(conn);
        check_expected(flags);
@@ -183,6 +157,7 @@ void dump_call_handler_ok(void **state)
        g_dbus_message_set_body(test_message, test_params);
 
        GUnixFDList *test_list1 = g_unix_fd_list_new();
+       expect_value(__wrap_g_dbus_message_get_unix_fd_list, message, test_message);
        will_return(__wrap_g_dbus_message_get_unix_fd_list, test_list1);
        expect_value(__wrap_g_unix_fd_list_get, list, test_list1);
        will_return(__wrap_g_unix_fd_list_get, 123);
index e623191a6b11a92b4f0004029380ffe0fa472e24..fcc5f3c936f8fe25cd068bf1c34de7806d85b849 100644 (file)
@@ -1,14 +1,4 @@
-#include <stdio.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <setjmp.h>
-#include <unistd.h>
-#include <cmocka.h>
-#include <fcntl.h>
-#include <gio/gio.h>
-#include <pkgmgr-info.h>
-
+#include "test_common.h"
 #include "dumpsys-user.c"
 
 int __wrap_pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
@@ -40,23 +30,23 @@ void __wrap_g_bus_unown_name(guint owner_id)
 }
 
 guint __wrap_g_bus_own_name_on_connection(GDBusConnection *connection,
-                                         const gchar *name,
-                                         GBusNameOwnerFlags flags,
-                                         GBusNameAcquiredCallback name_acquired_handler,
-                                         GBusNameLostCallback name_lost_handler,
-                                         gpointer user_data,
-                                         GDestroyNotify user_data_free_func)
+                                          const gchar *name,
+                                          GBusNameOwnerFlags flags,
+                                          GBusNameAcquiredCallback name_acquired_handler,
+                                          GBusNameLostCallback name_lost_handler,
+                                          gpointer user_data,
+                                          GDestroyNotify user_data_free_func)
 {
        return 234;
 }
 
 guint __wrap_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)
+                                               const gchar *object_path,
+                                               GDBusInterfaceInfo *interface_info,
+                                               const GDBusInterfaceVTable *vtable,
+                                               gpointer user_data,
+                                               GDestroyNotify user_data_free_func,
+                                               GError **error)
 {
        return 345;
 }
index e558759eead90c33ff93d496c1063e0ca2aa416b..e544ccd18e374148df697fd80f13f35c5ce705a6 100644 (file)
@@ -8,6 +8,7 @@
 #include <fcntl.h>
 #include <gio/gio.h>
 
+#include "test_common.h"
 #include "dumpsys-service.c"
 
 
@@ -24,29 +25,18 @@ int __wrap_g_unix_fd_list_append(GUnixFDList *list, int fd, GError *error)
        return mock_type(int);
 }
 
-gboolean __wrap_g_dbus_connection_send_message (GDBusConnection *connection,
-                                GDBusMessage *message,
-                                GDBusSendMessageFlags flags,
-                                volatile guint32 *out_serial,
-                                GError **error)
-{
-       check_expected(connection);
-       check_expected(flags);
-       return mock_type(gboolean);
-}
-
 GDBusMessage* __wrap_g_dbus_connection_send_message_with_reply_sync(GDBusConnection *conn,
-                                                                   GDBusMessage *message,
-                                                                   GDBusSendMessageFlags flags,
-                                                                   gint timeout_msec,
-                                                                   volatile guint32 *out_serial,
-                                                                   GCancellable *cancellable,
-                                                                   GError **error)
+                                                                                                                                                                                           GDBusMessage *message,
+                                                                                                                                                                                           GDBusSendMessageFlags flags,
+                                                                                                                                                                                           gint timeout_msec,
+                                                                                                                                                                                           volatile guint32 *out_serial,
+                                                                                                                                                                                           GCancellable *cancellable,
+                                                                                                                                                                                           GError **error)
 {
-       check_expected_ptr(conn);
-       check_expected(flags);
-       check_expected(timeout_msec);
-       return mock_ptr_type(GDBusMessage*);
+                               check_expected_ptr(conn);
+                               check_expected(flags);
+                               check_expected(timeout_msec);
+                               return mock_ptr_type(GDBusMessage*);
 }
 
 GDBusMessage* __wrap_g_dbus_method_invocation_get_message(GDBusMethodInvocation *invocation)
index 984fa5fc7f96ab7aec7f9972430ff8ad900da1db..5a7e7d4dbbf59b06bc0657e986ec8c952f3ca454 100644 (file)
@@ -1,14 +1,4 @@
-#include <stdio.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <setjmp.h>
-#include <unistd.h>
-#include <cmocka.h>
-#include <fcntl.h>
-#include <gio/gio.h>
-#include <pkgmgr-info.h>
-
+#include "test_common.h"
 #include "libdumpsys.c"
 
 GDBusConnection* __wrap_g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error)
@@ -42,12 +32,6 @@ GDBusMessageType __wrap_g_dbus_message_get_message_type(GDBusMessage *message)
        return mock_type(GDBusMessageType);
 }
 
-GUnixFDList* __wrap_g_dbus_message_get_unix_fd_list(GDBusMessage *message)
-{
-       check_expected(message);
-       return mock_type(GUnixFDList*);
-}
-
 gint __wrap_g_unix_fd_list_get(GUnixFDList *list,
                                gint index,
                                GError **error)
@@ -58,6 +42,25 @@ gint __wrap_g_unix_fd_list_get(GUnixFDList *list,
        return mock_type(gint);
 }
 
+void setup_connection_send_message_with_reply_sync(GDBusConnection *t_connection, GDBusMessage *t_reply)
+{
+       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection);
+       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE);
+       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS);
+       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL);
+       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL);
+       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL);
+       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply);
+}
+
+void setup_get_sync(GDBusConnection *t_connection)
+{
+       expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM);
+       expect_value(__wrap_g_bus_get_sync, cancellable, NULL);
+       expect_value(__wrap_g_bus_get_sync, *error, NULL);
+       will_return(__wrap_g_bus_get_sync, t_connection);
+}
+
 void dumpsys_dump_ok(void **state)
 {
        static const char *t_service_name = "some.serivce.name";
@@ -78,13 +81,7 @@ void dumpsys_dump_ok(void **state)
        expect_value(__wrap_g_bus_get_sync, *error, NULL);
        will_return(__wrap_g_bus_get_sync, t_connection);
 
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply);
+       setup_connection_send_message_with_reply_sync(t_connection, t_reply);
 
        expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply);
        will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
@@ -174,8 +171,6 @@ void dumpsys_dump_fail6(void **state)
        will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, g_error_new(1, 2, "error")); // some error
        will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply);
 
-
-
        expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply);
        will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
 
@@ -198,18 +193,9 @@ void dumpsys_dump_fail7(void **state)
        g_dbus_message_set_unix_fd_list(t_reply, t_fd_list);
        int t_out_fd;
 
-       expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM);
-       expect_value(__wrap_g_bus_get_sync, cancellable, NULL);
-       expect_value(__wrap_g_bus_get_sync, *error, NULL);
-       will_return(__wrap_g_bus_get_sync, t_connection);
+       setup_get_sync(t_connection);
 
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply);
+       setup_connection_send_message_with_reply_sync(t_connection, t_reply);
 
        expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply);
        will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
@@ -233,18 +219,9 @@ void dumpsys_dump_fail8(void **state)
        g_dbus_message_set_unix_fd_list(t_reply, t_fd_list);
        int t_out_fd;
 
-       expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM);
-       expect_value(__wrap_g_bus_get_sync, cancellable, NULL);
-       expect_value(__wrap_g_bus_get_sync, *error, NULL);
-       will_return(__wrap_g_bus_get_sync, t_connection);
+       setup_get_sync(t_connection);
 
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply);
+       setup_connection_send_message_with_reply_sync(t_connection, t_reply);
 
        expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply);
        will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
@@ -271,18 +248,9 @@ void dumpsys_dump_fail9(void **state)
        g_dbus_message_set_unix_fd_list(t_reply, t_fd_list);
        int t_out_fd;
 
-       expect_value(__wrap_g_bus_get_sync, bus_type, G_BUS_TYPE_SYSTEM);
-       expect_value(__wrap_g_bus_get_sync, cancellable, NULL);
-       expect_value(__wrap_g_bus_get_sync, *error, NULL);
-       will_return(__wrap_g_bus_get_sync, t_connection);
+       setup_get_sync(t_connection);
 
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, conn, t_connection);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, flags, G_DBUS_SEND_MESSAGE_FLAGS_NONE);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, timeout_msec, SERVICE_TIMEOUT_MS);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, cancellable, NULL);
-       expect_value(__wrap_g_dbus_connection_send_message_with_reply_sync, *error, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, NULL);
-       will_return(__wrap_g_dbus_connection_send_message_with_reply_sync, t_reply);
+       setup_connection_send_message_with_reply_sync(t_connection, t_reply);
 
        expect_value(__wrap_g_dbus_message_get_message_type, message, t_reply);
        will_return(__wrap_g_dbus_message_get_message_type, G_DBUS_MESSAGE_TYPE_METHOD_RETURN);