Use g_io_create_watch and g_source_attach instead of g_io_add_watch 00/279900/1
authorCheoleun Moon <chleun.moon@samsung.com>
Tue, 11 May 2021 09:44:24 +0000 (18:44 +0900)
committerAnjali Nijhara <a.nijhara@samsung.com>
Thu, 18 Aug 2022 05:37:51 +0000 (11:07 +0530)
Change-Id: Ie891c6f01b486b27c052bd0e1a74ba3e4ff41e0e
Signed-off-by: Cheoleun Moon <chleun.moon@samsung.com>
CMakeLists.txt
packaging/capi-network-nsd.spec
src/dns-sd/dns-sd.c
tests/CMakeLists.txt
tests/mock/nsd-mock-cynara.c
tests/mock/nsd-mock-gsource.c
tests/mock/nsd-mock.c

index 89360a5..c2349bd 100644 (file)
@@ -29,7 +29,7 @@ INCLUDE(FindPkgConfig)
 SET(LIB_DEPS "dlog glib-2.0 gio-2.0 capi-base-common capi-system-info gio-unix-2.0 gssdp-1.0 dns_sd cynara-client")
 SET(PC_DEPS "capi-base-common")
 SET(TEST_DEPS "glib-2.0")
-SET(GTEST_DEPS "gmock capi-system-info cynara-client")
+SET(GTEST_DEPS "gmock" ${LIB_DEPS})
 
 SET(TARGET_NSD "nsd")
 SET(TARGET_LIB_NSD_DNS_SD "nsd-dns-sd")
@@ -41,4 +41,4 @@ ADD_DEFINITIONS("-DUSE_DLOG")
 ADD_SUBDIRECTORY(include)
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(pkgconfig)
-ADD_SUBDIRECTORY(tests)
\ No newline at end of file
+ADD_SUBDIRECTORY(tests)
index a158c3e..22f3f2e 100644 (file)
@@ -107,7 +107,7 @@ tar xf %{name}-gcov.tar -C %{buildroot}%{_datadir}/gcov/obj
 %endif
 
 %check
-LD_LIBRARY_PATH=./src/ssdp:./src/dns-sd tests/tct-nsd-core
+tests/tct-nsd-core
 %if 0%{?gcov:1}
 lcov -c --ignore-errors graph --no-external -b . -d . -o nsd.info
 genhtml nsd.info -o out --legend --show-details
index 2506e03..dbb2bec 100644 (file)
@@ -320,16 +320,31 @@ static gboolean __dnssd_resolve_data_io_events(GIOChannel *source,
        return __dnssd_process_result(condition, reply->sd_ref);
 }
 
-static guint __dnssd_io_add_watch(int fd, GIOFunc func, gpointer data)
+guint __dnssd_io_add_watch(GIOChannel *channel,
+               GIOFunc func, gpointer data)
+{
+       guint watch_id;
+       GMainContext *context = NULL;
+       GSource *source = g_io_create_watch(channel,
+                       (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL));
+
+       g_source_set_callback(source, (GSourceFunc)func, data, NULL);
+       context = g_main_context_get_thread_default();
+
+       watch_id = g_source_attach(source, context);
+       g_source_unref(source);
+
+       return watch_id;
+}
+
+static guint __dnssd_watch_fd(int fd, GIOFunc func, gpointer data)
 {
        guint watch_id;
        GIOChannel *sock_io = NULL;
        sock_io = g_io_channel_unix_new(fd);
        g_io_channel_set_flags(sock_io, G_IO_FLAG_NONBLOCK, NULL);
        g_io_channel_set_close_on_unref(sock_io, FALSE);
-       watch_id = g_io_add_watch(sock_io, (G_IO_IN | G_IO_HUP |
-                               G_IO_ERR | G_IO_NVAL), func, data);
-       DNSSD_LOGD("watch_id %u", watch_id);
+       watch_id = __dnssd_io_add_watch(sock_io, func, data);
        g_io_channel_unref(sock_io);
        return watch_id;
 }
@@ -343,7 +358,7 @@ static void __dnssd_handle_add_event_handler(guint *watch_id, dnssd_handle_s *ha
                return;
        }
 
-       *watch_id = __dnssd_io_add_watch(dns_sd_fd, __dnssd_handle_io_events, handle);
+       *watch_id = __dnssd_watch_fd(dns_sd_fd, __dnssd_handle_io_events, handle);
 }
 
 static void __dnssd_resolve_data_add_event_handler(guint *watch_id, resolve_reply_data *data)
@@ -354,7 +369,7 @@ static void __dnssd_resolve_data_add_event_handler(guint *watch_id, resolve_repl
                return;
        }
 
-       *watch_id = __dnssd_io_add_watch(dns_sd_fd, __dnssd_resolve_data_io_events, data);
+       *watch_id = __dnssd_watch_fd(dns_sd_fd, __dnssd_resolve_data_io_events, data);
 }
 
 EXPORT_API int dnssd_initialize(void)
index 5566d98..4b48f7d 100644 (file)
@@ -59,8 +59,9 @@ INCLUDE_DIRECTORIES(${unittest_pkgs_INCLUDE_DIRS})
 LINK_DIRECTORIES(${unittest_pkgs_LIBRARY_DIRS})
 
 INCLUDE_DIRECTORIES(
-    ${CMAKE_SOURCE_DIR}/include
-    ${CMAKE_CURRENT_SOURCE_DIR}/include
+       ${CMAKE_SOURCE_DIR}/include
+       ${CMAKE_CURRENT_SOURCE_DIR}/include
+       ${unittest_pkgs_INCLUDE_DIRS}
 )
 
 FILE(GLOB NSD_MOCK_SRCS mock/*.c)
@@ -73,12 +74,27 @@ SET(TC_SOURCES
 )
 
 # Build
-ADD_EXECUTABLE(${UNITTEST_NAME} ${UNITTEST_NAME}.cpp ${TC_SOURCES} ${NSD_MOCK_SRCS})
+FILE(GLOB GTEST_SRCS ${CMAKE_SOURCE_DIR}/src/dns-sd/*.c ${CMAKE_SOURCE_DIR}/src/ssdp/*.c ${UNITTEST_NAME}.cpp ${TC_SOURCES} ${NSD_MOCK_SRCS})
+ADD_EXECUTABLE(${UNITTEST_NAME} ${GTEST_SRCS})
 TARGET_LINK_LIBRARIES(${UNITTEST_NAME}
-    ${unittest_pkgs_LIBRARIES}
-    ${TARGET_LIB_NSD_DNS_SD}
-    ${TARGET_LIB_NSD_SSDP}
+       ${unittest_pkgs_LIBRARIES}
     dl
 )
 
-INSTALL(TARGETS ${UNITTEST_NAME} RUNTIME DESTINATION ${BIN_DIR})
\ No newline at end of file
+SET_TARGET_PROPERTIES(${UNITTEST_NAME} PROPERTIES
+       COMPILE_FLAGS "-fPIE"
+       LINK_FLAGS "-Wl,\
+--wrap=g_signal_connect_data,\
+--wrap=g_signal_handler_disconnect,\
+--wrap=g_io_channel_unix_new,\
+--wrap=g_io_channel_set_flags,\
+--wrap=g_io_channel_set_close_on_unref,\
+--wrap=g_io_channel_unref,\
+--wrap=g_io_create_watch,\
+--wrap=g_source_set_callback,\
+--wrap=g_source_attach,\
+--wrap=g_source_unref,\
+--wrap=g_source_remove,\
+--wrap=cynara_check")
+
+INSTALL(TARGETS ${UNITTEST_NAME} RUNTIME DESTINATION ${BIN_DIR})
index b7094b0..573293d 100644 (file)
 
 #include <cynara-client.h>
 
-#ifndef API
-#define API __attribute__ ((visibility("default")))
-#endif
-
-API int cynara_check(cynara *p_cynara,
+int __wrap_cynara_check(cynara *p_cynara,
                const char *client, const char *client_session,
                const char *user, const char *privilege)
 {
index 2acf4f5..529f863 100644 (file)
 #include <glib.h>
 #include <gio/gio.h>
 
-#ifndef API
-#define API __attribute__ ((visibility("default")))
-#endif
-
 #define TIZEN_MOCK_IO_CHANNEL_SOURCE 5
 
-API GIOChannel *g_io_channel_unix_new(gint fd)
+extern void __real_g_source_set_callback(GSource *source, GSourceFunc func,
+               gpointer data, GDestroyNotify notify);
+extern guint __real_g_source_attach(GSource *source, GMainContext *context);
+
+GIOChannel *__wrap_g_io_channel_unix_new(gint fd)
 {
        return g_new(GIOChannel, 1);
 }
 
-API GIOStatus g_io_channel_set_flags(GIOChannel  *channel,
+GIOStatus __wrap_g_io_channel_set_flags(GIOChannel  *channel,
                GIOFlags flags,
                GError **error)
 {
@@ -37,14 +37,13 @@ API GIOStatus g_io_channel_set_flags(GIOChannel  *channel,
 }
 
 
-API void g_io_channel_set_close_on_unref(GIOChannel *channel,
+void __wrap_g_io_channel_set_close_on_unref(GIOChannel *channel,
                                 gboolean do_close)
 {
        return;
 }
 
 typedef struct {
-       GIOChannel *channel;
        GIOFunc func;
        gpointer user_data;
        gboolean called_once;
@@ -60,57 +59,53 @@ static gboolean __mock_gio_channel_cb(gpointer user_data)
        if (cb_data->called_once)
                return TRUE;
 
-       cb_data->func(cb_data->channel, G_IO_IN, cb_data->user_data);
+       cb_data->func(NULL, G_IO_IN, cb_data->user_data);
        cb_data->called_once = TRUE;
 
        return TRUE;
 }
 
-static void __remove_mock_gio_channel_cb(gpointer data)
+GSource *__wrap_g_io_create_watch(GIOChannel *channel, GIOCondition condition)
 {
-       mock_gio_channel_cb_data_s *cb_data = (mock_gio_channel_cb_data_s *)data;
-       if (!cb_data)
-               return;
-       g_free(cb_data);
+       return g_idle_source_new();
 }
 
-API guint g_io_add_watch(GIOChannel *channel,
-               GIOCondition condition,
-               GIOFunc func,
-               gpointer user_data)
+void __wrap_g_source_set_callback(GSource *source, GSourceFunc func,
+               gpointer data, GDestroyNotify notify)
 {
        mock_gio_channel_cb_data_s *cb_data = g_try_malloc0(sizeof(mock_gio_channel_cb_data_s));
-       guint g_mock_gio_channel_cb_source = 0;
-
-       cb_data->channel = channel;
-       cb_data->func = func;
-       cb_data->user_data = user_data;
        cb_data->called_once = FALSE;
-       g_mock_gio_channel_cb_source = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
-                       __mock_gio_channel_cb,
-                       (gpointer)cb_data,
-                       __remove_mock_gio_channel_cb);
+       cb_data->func = (GIOFunc)func;
+       cb_data->user_data = data;
+       __real_g_source_set_callback(source, __mock_gio_channel_cb, cb_data, notify);
+}
 
-       return g_mock_gio_channel_cb_source;
+guint __wrap_g_source_attach(GSource *source, GMainContext *context)
+{
+       return __real_g_source_attach(source, context);
+}
+
+void __wrap_g_source_unref(GSource *source)
+{
 }
 
-API gboolean g_source_remove (guint tag)
+gboolean __wrap_g_source_remove(guint tag)
 {
        GSource *source;
 
        if (tag == 0 || tag > 1000)
                return FALSE;
 
-       source = g_main_context_find_source_by_id (NULL, tag);
+       source = g_main_context_find_source_by_id(NULL, tag);
        if (source)
-               g_source_destroy (source);
+               g_source_destroy(source);
        else
                printf("Source ID %u was not found when attempting to remove it\n", tag);
 
   return source != NULL;
 }
 
-API void g_io_channel_unref(GIOChannel *channel)
+void __wrap_g_io_channel_unref(GIOChannel *channel)
 {
        g_free(channel);
 }
index 38407d0..4cffd4e 100644 (file)
@@ -66,7 +66,7 @@ API unsigned int if_nametoindex(const char *ifname)
        else
                return 1;
 }
-API gulong g_signal_connect_data(gpointer instance,
+API gulong __wrap_g_signal_connect_data(gpointer instance,
                const gchar *detailed_signal,
                GCallback c_handler,
                gpointer data,
@@ -76,7 +76,7 @@ API gulong g_signal_connect_data(gpointer instance,
        return DEFAULT_MOCK_GOBJECT_SIGNAL;
 }
 
-API void g_signal_handler_disconnect (gpointer instance, gulong handler_id)
+API void __wrap_g_signal_handler_disconnect (gpointer instance, gulong handler_id)
 {
        return;
 }