Add the first unittest codes 47/236247/3
authorCheoleun Moon <chleun.moon@samsung.com>
Tue, 16 Jun 2020 02:41:19 +0000 (11:41 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Tue, 16 Jun 2020 04:05:11 +0000 (13:05 +0900)
- Add a mock for system_info_get_platform_bool
- Add a mock for wifi_aware_gdbus_init
- Add mocks for g_bus_get_sync, g_dbus_connection_call and g_dbus_connection_call_finish

Change-Id: I00d46dfde0fc112bcec140a5a2349857589c48b8

26 files changed:
CMakeLists.txt
include/wifi-aware.h
packaging/capi-network-wifi-aware.manifest [moved from capi-network-wifi-aware.manifest with 100% similarity]
packaging/capi-network-wifi-aware.spec
packaging/wifi-aware-test.manifest [moved from wifi-aware-test.manifest with 100% similarity]
src/wifi-aware-data-path.c
src/wifi-aware-gdbus.c
src/wifi-aware-peer.c
src/wifi-aware-private.c
tests/nan-test/CMakeLists.txt [moved from test/CMakeLists.txt with 97% similarity]
tests/nan-test/wifi-aware-matching-filter-test.h [moved from test/wifi-aware-matching-filter-test.h with 100% similarity]
tests/nan-test/wifi-aware-publish-matching-filter-test.c [moved from test/wifi-aware-publish-matching-filter-test.c with 99% similarity]
tests/nan-test/wifi-aware-publish-test.c [moved from test/wifi-aware-publish-test.c with 100% similarity]
tests/nan-test/wifi-aware-subscribe-matching-filter-test.c [moved from test/wifi-aware-subscribe-matching-filter-test.c with 99% similarity]
tests/nan-test/wifi-aware-subscribe-test.c [moved from test/wifi-aware-subscribe-test.c with 100% similarity]
tests/nan-test/wifi-aware-test.c [moved from test/wifi-aware-test.c with 100% similarity]
tests/nan-test/wifi-aware-test.h [moved from test/wifi-aware-test.h with 100% similarity]
tests/unittest/CMakeLists.txt [new file with mode: 0644]
tests/unittest/mocks/wifi-aware-mock-gdbus-internal.c [new file with mode: 0644]
tests/unittest/mocks/wifi-aware-mock-gdbus-internal.h [new file with mode: 0644]
tests/unittest/mocks/wifi-aware-mock.c [new file with mode: 0644]
tests/unittest/wifi-aware-unittest-fixture.h [new file with mode: 0644]
tests/unittest/wifi-aware-unittest-main.cpp [new file with mode: 0644]
tests/unittest/wifi-aware-unittest-util.cpp [new file with mode: 0644]
tests/unittest/wifi-aware-unittest-util.h [new file with mode: 0644]
tests/unittest/wifi-aware-unittest.cpp [new file with mode: 0644]

index c1b28a8..39e8a73 100644 (file)
@@ -58,5 +58,6 @@ SET(TARGET_CTS_VERIFIER_TOOL "cts-verifier")
 ADD_SUBDIRECTORY(include)
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(pkgconfig)
-ADD_SUBDIRECTORY(test)
+ADD_SUBDIRECTORY(tests/nan-test)
+ADD_SUBDIRECTORY(tests/unittest)
 ADD_SUBDIRECTORY(cts-verifier)
index fc69747..341d973 100644 (file)
@@ -181,6 +181,7 @@ typedef void *wifi_aware_data_path_h;
  * @return 0 on success, otherwise a negative error value
  * @retval #WIFI_AWARE_ERROR_NONE
  * @retval #WIFI_AWARE_ERROR_ALREADY_INITIALIZED
+ * @retval #WIFI_AWARE_ERROR_OPERATION_FAILED
  * @see wifi_aware_deinitialize()
  */
 int wifi_aware_initialize();
index ab440da..08c97c2 100644 (file)
@@ -5,6 +5,7 @@ Release:    1
 Group:      Network & Connectivity/API
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
+Source1:       %{name}.manifest
 
 BuildRequires:  cmake
 BuildRequires:  pkgconfig(gobject-2.0)
@@ -14,6 +15,10 @@ BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  pkgconfig(cynara-client)
+BuildRequires: pkgconfig(gmock)
+%if 0%{?gcov:1}
+BuildRequires: lcov
+%endif
 
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
@@ -42,10 +47,18 @@ summary:    cts-verifier command app
 %description cts-verifier
 cts-verifier command app 
 
+%if 0%{?gcov:1}
+%package gcov
+Summary:       Coverage Data of %{name}
+
+%description gcov
+The %{name}-gcov package contains gcov objects
+%endif
 
 %prep
 %setup -q
 chmod 644 %{SOURCE0}
+cp %{SOURCE1} ./%{name}.manifest
 
 %ifarch %{arm}
 export ARCH=arm
@@ -54,6 +67,13 @@ export ARCH=i586
 %endif
 
 %build
+%if 0%{?gcov:1}
+export CFLAGS+=" -fprofile-arcs -ftest-coverage"
+export CXXFLAGS+=" -fprofile-arcs -ftest-coverage"
+export FFLAGS+=" -fprofile-arcs -ftest-coverage"
+export LDFLAGS+=" -lgcov"
+%endif
+
 %if 0%{?sec_build_binary_debug_enable}
 export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
 export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
@@ -73,17 +93,37 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
         -DMAJORVER=${MAJORVER}
 make %{?jobs:-j%jobs}
 
+%if 0%{?gcov:1}
+mkdir -p gcov-obj
+find . -name '*.gcno' -exec cp '{}' gcov-obj ';'
+%endif
+
 %install
 rm -rf %{buildroot}
 %make_install
 
+%if 0%{?gcov:1}
+mkdir -p %{buildroot}%{_datadir}/gcov/obj
+install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
+%endif
+
+%check
+export LD_LIBRARY_PATH=./src/
+LD_PRELOAD=./tests/unittest/libwifi-aware-mock.so ./tests/unittest/wifi-aware-unittest
+#LD_LIBRARY_PATH=
+
+%if 0%{?gcov:1}
+lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info
+genhtml %{name}.info -o out --legend --show-details
+%endif
+
 %post -p /sbin/ldconfig
 
 %postun -p /sbin/ldconfig
 
 
 %files
-%manifest capi-network-wifi-aware.manifest
+%manifest %{name}.manifest
 %license LICENSE
 %defattr(-,root,root,-)
 %{_libdir}/*.so.*
@@ -95,7 +135,7 @@ rm -rf %{buildroot}
 %{_libdir}/*.so
 
 %files test
-%manifest wifi-aware-test.manifest
+%manifest %{name}.manifest
 %attr(755,root,root) %{_bindir}/wifi-aware-test
 %attr(755,root,root) %{_bindir}/wifi-aware-publish-test
 %attr(755,root,root) %{_bindir}/wifi-aware-subscribe-test
@@ -105,3 +145,8 @@ rm -rf %{buildroot}
 %files cts-verifier
 #%manifest wifi-aware-test.manifest
 %attr(755,root,root) %{_bindir}/cts-verifier
+
+%if 0%{?gcov:1}
+%files gcov
+%{_datadir}/gcov/obj/*
+%endif
index fe565e6..949f9aa 100644 (file)
@@ -69,7 +69,6 @@ static bool __is_valid_wifi_aware_security_type(wifi_aware_security_type_e secur
                && security_type <= WIFI_AWARE_SECURITY_TYPE_PMK;
 }
 
-//LCOV_EXCL_START
 static uint32_t __data_path_get_ndp_id(wifi_aware_data_path_h data_path)
 {
        wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
@@ -471,9 +470,8 @@ int _wifi_aware_data_path_unset_terminated_cb(wifi_aware_data_path_h data_path)
                wifi_aware_gdbus_deregister_signal(DATA_PATH_TERMINATED_SIGNAL);
 
        ndp->terminated_signal_id = 0;
-
+               
        __WIFI_AWARE_FUNC_ENTER__;
 
        return ret;
 }
-//LCOV_EXCL_STOP
index 1afed63..96777e9 100644 (file)
@@ -109,14 +109,14 @@ int wifi_aware_gdbus_init()
        if (gdbus_data.connection != NULL) {
                WIFI_AWARE_LOGI("There's already gdbus connection"); //LCOV_EXCL_LINE
                __WIFI_AWARE_FUNC_EXIT__; //LCOV_EXCL_LINE
-               return 0;
+               return WIFI_AWARE_ERROR_NONE;
        }
 
        gdbus_data.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
        if (gdbus_data.connection == NULL) {
                WIFI_AWARE_LOGI("Failed to get connection, Error[%s]\n", error->message); //LCOV_EXCL_LINE
                g_error_free(error); //LCOV_EXCL_LINE
-               return -1;
+               return WIFI_AWARE_ERROR_OPERATION_FAILED;
        }
        gdbus_data.cancellable = g_cancellable_new();
 
@@ -675,7 +675,6 @@ int wifi_aware_gdbus_update_subscribe(wifi_aware_session_h session,
        return ret;
 }
 
-//LCOV_EXCL_START
 int wifi_aware_gdbus_followup(wifi_aware_session_h session,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
                const unsigned char *message, size_t len,
@@ -819,4 +818,4 @@ int wifi_aware_gdbus_ndp_close(wifi_aware_data_path_h data_path,
        __WIFI_AWARE_FUNC_EXIT__;
        return ret;
 }
-//LCOV_EXCL_STOP
+
index a2c5ad6..ec318f5 100644 (file)
@@ -28,7 +28,6 @@ typedef struct {
        unsigned char mac[WIFI_AWARE_MAC_ADDRESS_LEN];
 } wifi_aware_peer_s;
 
-//LCOV_EXCL_START
 int _wifi_aware_peer_create(wifi_aware_peer_h *peer, unsigned int peer_id)
 {
        wifi_aware_peer_s *peer_handle = (wifi_aware_peer_s *)g_try_malloc0(sizeof(wifi_aware_peer_s));
@@ -109,4 +108,3 @@ void _wifi_aware_peer_set_mac(wifi_aware_peer_h  peer, unsigned char *mac)
        wifi_aware_peer_s *handle = (wifi_aware_peer_s *)peer;
        memcpy(handle->mac, mac, WIFI_AWARE_MAC_ADDRESS_LEN);
 }
-//LCOV_EXCL_STOP
index 0ee8cfc..452b9ee 100644 (file)
@@ -359,7 +359,6 @@ static void __wifi_aware_publish_invoke_callback(wifi_aware_session_h session,
        }
 }
 
-//LCOV_EXCL_START
 static void __wifi_aware_discovered_cb(wifi_aware_session_h session, wifi_aware_peer_h peer,
                const unsigned char *specific_info, size_t specific_info_len, int distance)
 {
@@ -392,7 +391,6 @@ static void __wifi_aware_session_terminated_cb(wifi_aware_session_h session, int
        if (handle->terminated_cb)
                handle->terminated_cb(session, reason, handle->terminated_cb_data);
 }
-//LCOV_EXCL_STOP
 
 static wifi_aware_error_e __get_dbus_result(GObject *src, GAsyncResult *res, uint16_t *id)
 {
@@ -689,7 +687,6 @@ static void __parsing_session_terminated_event(GVariant *parameters, uint16_t *p
        g_variant_get(parameters, "(qi)", pub_sub_id, reason);
 }
 
-//LCOV_EXCL_START
 static void __session_terminated_cb(GDBusConnection *connection,
                    const gchar *sender, const gchar *object_path,
                     const gchar *interface, const gchar *signal,
@@ -711,7 +708,6 @@ static void __session_terminated_cb(GDBusConnection *connection,
        __session_set_pub_sub_id(session, 0);
        __wifi_aware_session_terminated_cb(session, (wifi_aware_termination_reason_e)reason);
 }
-//LCOV_EXCL_STOP
 
 void _wifi_aware_session_set_terminated_cb(wifi_aware_session_h session,
                wifi_aware_session_terminated_cb callback, void *user_data)
@@ -1093,7 +1089,6 @@ static bool __unpack_byte_array(unsigned char *array, GVariant *var, uint16_t le
        return true;
 }
 
-//LCOV_EXCL_START
 static bool __parsing_service_discovered_event(GVariant *parameters,
                uint16_t *sub_id, unsigned int *peer_id, unsigned char *peer_mac,
                unsigned char *specific_info, size_t *specific_info_len, int *distance)
@@ -1243,7 +1238,6 @@ static void __message_received_cb(GDBusConnection *connection,
        _wifi_aware_peer_set_mac(peer, peer_mac);
        __wifi_aware_received_cb(session, peer, message, message_len);
 }
-//LCOV_EXCL_STOP
 
 int _wifi_aware_set_service_discovered_cb(wifi_aware_session_h session,
                wifi_aware_service_discovered_cb callback, void *user_data)
@@ -1327,7 +1321,6 @@ void __destroy_message_data(wifi_aware_message_s *message)
        g_free(message);
 }
 
-//LCOV_EXCL_START
 static void __wifi_aware_followup_invoke_callback(wifi_aware_message_s *message, wifi_aware_error_e error)
 {
        if (message && message->callback) {
@@ -1392,4 +1385,3 @@ int _wifi_aware_send_message(wifi_aware_session_h session,
                        __send_message_reply,
                        message_data);
 }
-//LCOV_EXCL_STOP
similarity index 97%
rename from test/CMakeLists.txt
rename to tests/nan-test/CMakeLists.txt
index 375ebcf..d5f2e5b 100644 (file)
@@ -17,7 +17,7 @@
 
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 
-SET(WIFI_AWARE_TEST_INCLUDE_DIR ${WIFI_AWARE_TEST_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include)
+SET(WIFI_AWARE_TEST_INCLUDE_DIR ${WIFI_AWARE_TEST_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/include)
 INCLUDE_DIRECTORIES(${WIFI_AWARE_TEST_INCLUDE_DIR})
 
 INCLUDE(FindPkgConfig)
@@ -50,12 +50,12 @@ void test_deinit();
 
 static void __print_test_result(int num, bool result)
 {
-       char test_name[20] = {0, };
+       char test_name[30] = {0, };
 
        if (num < NEGATIVE_TEST_01)
-               snprintf(test_name, 19, "%s %d", "Positivie Test", num);
+               snprintf(test_name, 29, "%s %d", "Positive Test", num);
        else
-               snprintf(test_name, 19, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
+               snprintf(test_name, 29, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
 
        if (result)
                printf(MAKE_GREEN"[%s] is pass."RESET_COLOR"\n", test_name);
@@ -53,12 +53,12 @@ void test_deinit();
 
 static void __print_test_result(int num, bool result)
 {
-       char test_name[20] = {0, };
+       char test_name[30] = {0, };
 
        if (num < NEGATIVE_TEST_01)
-               snprintf(test_name, 19, "%s %d", "Positivie Test", num);
+               snprintf(test_name, 29, "%s %d", "Positivie Test", num);
        else
-               snprintf(test_name, 19, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
+               snprintf(test_name, 29, "%s %d", "Negative Test", num - NEGATIVE_TEST_01 + 1);
 
        if (result)
                printf(MAKE_GREEN"[%s] is pass."RESET_COLOR"\n", test_name);
diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..71a0b72
--- /dev/null
@@ -0,0 +1,50 @@
+# Copyright (c) 2020 Samsung Electronics Co., Ltd 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.
+#
+# @file        CMakeLists.txt
+#
+
+SET(WIFI_AWARE_MOCK "wifi-aware-mock")
+SET(WIFI_AWARE_MOCK_SRCS
+       mocks/wifi-aware-mock.c
+       mocks/wifi-aware-mock-gdbus-internal.c)
+
+INCLUDE_DIRECTORIES(
+       ${CMAKE_SOURCE_DIR}/include
+       ${CMAKE_SOURCE_DIR}/src/include
+       ${CMAKE_CURRENT_SOURCE_DIR}/mocks
+       )
+ADD_DEFINITIONS("-fvisibility=default")
+ADD_LIBRARY(${WIFI_AWARE_MOCK} SHARED ${WIFI_AWARE_MOCK_SRCS})
+
+
+SET(WIFI_AWARE_UNITTEST "wifi-aware-unittest")
+
+pkg_check_modules(TARGET_WIFI_AWARE_UNITTEST REQUIRED
+       capi-system-info
+       glib-2.0
+       gmock
+)
+INCLUDE_DIRECTORIES(
+       ${TARGET_WIFI_AWARE_UNITTEST_INCLUDE_DIRS}
+)
+LINK_DIRECTORIES(${TARGET_WIFI_AWARE_UNITTEST_LIBRARY_DIRS})
+
+FILE(GLOB UNITTEST_SRCS *.cpp)
+
+ADD_EXECUTABLE(${WIFI_AWARE_UNITTEST} ${UNITTEST_SRCS})
+TARGET_LINK_LIBRARIES(${WIFI_AWARE_UNITTEST}
+       ${TARGET_WIFI_AWARE}
+       ${TARGET_WIFI_AWARE_UNITTEST_LIBRARIES}
+)
diff --git a/tests/unittest/mocks/wifi-aware-mock-gdbus-internal.c b/tests/unittest/mocks/wifi-aware-mock-gdbus-internal.c
new file mode 100644 (file)
index 0000000..5a1e1ff
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#include <stdio.h>
+#include <glib.h>
+#include "wifi-aware-mock-gdbus-internal.h"
+
+typedef struct {
+       GAsyncReadyCallback callback;
+       GVariant *result;
+       gpointer user_data;
+} gdbus_result_data_s;
+
+static gdbus_result_data_s _gdbus_async_result;
+
+static gboolean __reply_async_method(gpointer data)
+{
+       gdbus_result_data_s *gdbus_result = (gdbus_result_data_s *)data;
+       gdbus_result->callback((GObject *)_get_dummy_gdbus_connection(),
+                       NULL,
+                       gdbus_result->user_data);
+       return FALSE;
+}
+
+static void __enable_handler(GVariant *parameters, GAsyncReadyCallback callback, gpointer user_data)
+{
+       // Always Success
+       _gdbus_async_result.callback = callback;
+       _gdbus_async_result.result = g_variant_new("(ii)", DUMMY_CLIENT_ID, WIFI_AWARE_ERROR_NONE);
+       _gdbus_async_result.user_data = user_data;
+       g_timeout_add(ASYNC_RESULT_DELAY, __reply_async_method, &_gdbus_async_result);
+}
+
+static GVariant *__disable_handler(GVariant *parameters)
+{
+       GVariant *reply = g_variant_new("(i)", WIFI_AWARE_ERROR_NONE);
+       return reply;
+}
+
+struct {
+       const gchar *interface_name;
+       const gchar *method_name;
+       // parameters is omitted
+       void(*async_handler)(GVariant *parameters, GAsyncReadyCallback callback, gpointer user_data);
+       GVariant *(*sync_handler)(GVariant *parameters);
+} wifi_aware_g_dbus_method_list[] = {
+       {
+               "net.nan.discovery",
+               "Enable",
+               __enable_handler,
+               NULL,
+       },
+       {
+               "net.nan.discovery",
+               "Disable",
+               NULL,
+               __disable_handler,
+       },
+       /*
+       {
+               "net.nan.discovery",
+               "Publish",
+       },
+       {
+               "net.nan.discovery",
+               "UpdatePublish",
+       },
+       {
+               "net.nan.discovery",
+               "CancelPublish",
+       },
+       */
+       {
+               NULL,
+               NULL,
+               NULL,
+       },
+};
+
+GDBusConnection *_get_dummy_gdbus_connection()
+{
+       return (GDBusConnection *)g_object_new(G_TYPE_DBUS_CONNECTION, NULL);
+}
+
+bool _check_g_dbus_bus_name(const gchar *bus_name)
+{
+       return g_strcmp0(bus_name, WIFI_AWARE_INTERFACE) == 0;
+}
+
+bool _check_g_dbus_object_path(const gchar *object_path)
+{
+       return g_strcmp0(object_path, WIFI_AWARE_DISCOVERY_PATH) == 0;
+}
+
+bool _check_g_dbus_interface_name(const gchar *interface_name)
+{
+       return g_strcmp0(interface_name, WIFI_AWARE_DISCOVERY_INTERFACE) == 0;
+}
+
+static bool __is_same_method(int i, const char *interface_name, const char *method_name)
+{
+       return (g_strcmp0(wifi_aware_g_dbus_method_list[i].interface_name, interface_name) == 0
+                       && g_strcmp0(wifi_aware_g_dbus_method_list[i].method_name, method_name) == 0);
+}
+
+void _handle_async_method(const gchar *interface_name,
+               const char *method_name, GVariant *parameters,
+               GAsyncReadyCallback callback, gpointer user_data)
+{
+       for (int i = 0; ; ++i) {
+               if (wifi_aware_g_dbus_method_list[i].interface_name == NULL)
+                       return;
+               if (__is_same_method(i, interface_name, method_name))
+                       wifi_aware_g_dbus_method_list[i].async_handler(parameters, callback, user_data);
+       }
+}
+
+GVariant *_get_gdbus_async_result(GError **error)
+{
+       *error = NULL;
+       return _gdbus_async_result.result;
+}
+
+void _set_gdbus_error(gint code, const char *message, GError **error)
+{
+       *error = g_error_new_literal(G_DBUS_ERROR, code, message);
+}
+
+GVariant *_handle_sync_method(const gchar *interface_name,
+               const char *method_name, GVariant *parameters, GError **error)
+{
+       for (int i = 0; ; ++i) {
+               if (wifi_aware_g_dbus_method_list[i].interface_name == NULL) {
+                       _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+                       return NULL;
+               }
+               if (__is_same_method(i, interface_name, method_name))
+                       return wifi_aware_g_dbus_method_list[i].sync_handler(parameters);
+       }
+}
diff --git a/tests/unittest/mocks/wifi-aware-mock-gdbus-internal.h b/tests/unittest/mocks/wifi-aware-mock-gdbus-internal.h
new file mode 100644 (file)
index 0000000..edee2ba
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#pragma once
+
+#include "wifi-aware-gdbus.h"
+
+#define ASYNC_RESULT_DELAY 500
+#define DUMMY_CLIENT_ID 1
+
+GDBusConnection *_get_dummy_gdbus_connection();
+bool _check_g_dbus_bus_name(const gchar *bus_name);
+bool _check_g_dbus_object_path(const gchar *object_path);
+bool _check_g_dbus_interface_name(const gchar *interface_name);
+void _handle_async_method(const gchar *interface_name,
+               const char *method_name, GVariant *parameters,
+               GAsyncReadyCallback callback, gpointer user_data);
+GVariant *_get_gdbus_async_result(GError **error);
+void _set_gdbus_error(gint code, const char *message, GError **error);
+GVariant *_handle_sync_method(const gchar *interface_name,
+               const char *method_name, GVariant *parameters, GError **error);
diff --git a/tests/unittest/mocks/wifi-aware-mock.c b/tests/unittest/mocks/wifi-aware-mock.c
new file mode 100644 (file)
index 0000000..4fb3878
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <system_info.h>
+
+#include "wifi-aware-mock-gdbus-internal.h"
+
+int system_info_get_platform_bool(const char *key, bool *value)
+{
+       *value = true;
+       return SYSTEM_INFO_ERROR_NONE;
+}
+
+bool _wifi_aware_check_permission()
+{
+       return true;
+}
+
+GDBusConnection *g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error)
+{
+       return _get_dummy_gdbus_connection();
+}
+
+GCancellable *g_cancellable_new()
+{
+       return (GCancellable *)g_object_new(G_TYPE_CANCELLABLE, NULL);
+}
+
+void
+g_dbus_connection_call(GDBusConnection *connection,
+               const gchar *bus_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *method_name,
+               GVariant *parameters,
+               const GVariantType *reply_type,
+               GDBusCallFlags flags,
+               gint timeout_msec,
+               GCancellable *cancellable,
+               GAsyncReadyCallback callback,
+               gpointer user_data)
+{
+       if (!_check_g_dbus_bus_name(bus_name))
+               return;
+       if (!_check_g_dbus_object_path(object_path))
+               return;
+       if (!_check_g_dbus_interface_name(interface_name))
+               return;
+       _handle_async_method(interface_name, method_name, parameters, callback, user_data);
+}
+
+GVariant *g_dbus_connection_call_finish(GDBusConnection *connection,
+               GAsyncResult *res,
+               GError **error)
+{
+       return _get_gdbus_async_result(error);
+}
+
+GVariant *g_dbus_connection_call_sync(GDBusConnection *connection,
+               const gchar *bus_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *method_name,
+               GVariant *parameters,
+               const GVariantType *reply_type,
+               GDBusCallFlags flags,
+               gint timeout_msec,
+               GCancellable *cancellable,
+               GError **error)
+{
+       if (!_check_g_dbus_bus_name(bus_name)) {
+               _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+               return NULL;
+       }
+       if (!_check_g_dbus_object_path(object_path)) {
+               _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+               return NULL;
+       }
+       if (!_check_g_dbus_interface_name(interface_name)) {
+               _set_gdbus_error(G_DBUS_ERROR_UNKNOWN_METHOD, "UnknownMethod", error);
+               return NULL;
+       }
+       return _handle_sync_method(interface_name, method_name, parameters, error);
+}
diff --git a/tests/unittest/wifi-aware-unittest-fixture.h b/tests/unittest/wifi-aware-unittest-fixture.h
new file mode 100644 (file)
index 0000000..60d70b0
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#pragma once
+
+#include <gtest/gtest.h>
+#include <system_info.h>
+
+#include "wifi-aware.h"
+
+#define WIFI_AWARE_FEATURE "http://tizen.org/feature/network.wifi.aware"
+
+bool g_enabled;
+bool g_wifi_aware_supported;
+
+class WifiAware : public ::testing::Test
+{
+protected:
+       virtual void SetUp() {
+               g_enabled = false;
+               g_wifi_aware_supported = false;
+               system_info_get_platform_bool(WIFI_AWARE_FEATURE, &g_wifi_aware_supported);
+               wifi_aware_initialize();
+       }
+
+       virtual void TearDown() {
+               wifi_aware_deinitialize();
+       }
+
+};
diff --git a/tests/unittest/wifi-aware-unittest-main.cpp b/tests/unittest/wifi-aware-unittest-main.cpp
new file mode 100644 (file)
index 0000000..37341b2
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#include <gtest/gtest.h>
+
+int main(int argc, char **argv)
+{
+       ::testing::InitGoogleTest(&argc, argv);
+       return RUN_ALL_TESTS();
+}
diff --git a/tests/unittest/wifi-aware-unittest-util.cpp b/tests/unittest/wifi-aware-unittest-util.cpp
new file mode 100644 (file)
index 0000000..3193cb0
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#include "wifi-aware-unittest-util.h"
+
+GMainLoop *g_main_loop;
+guint g_timeout_id;
+
+gboolean _wifi_aware_callback_timeout(gpointer data)
+{
+       GMainLoop *main_loop = NULL;
+       main_loop = (GMainLoop *)data;
+    if (main_loop != NULL)
+    {
+        g_main_loop_quit(main_loop);
+    }
+    return false;
+}
+
+const char *_convert_error_to_string(wifi_aware_error_e error)
+{
+       switch (error) {
+       case WIFI_AWARE_ERROR_NONE:
+               return "NO ERROR";
+       case WIFI_AWARE_ERROR_NOT_PERMITTED:
+               return "NOT_PERMITTED";
+       case WIFI_AWARE_ERROR_OUT_OF_MEMORY:
+               return "OUT_OF_MEMORY";
+       case WIFI_AWARE_ERROR_PERMISSION_DENIED:
+               return "PERMISSION_DENIED";
+       case WIFI_AWARE_ERROR_RESOURCE_BUSY:
+               return "RESOURCE_BUSY";
+       case WIFI_AWARE_ERROR_INVALID_PARAMETER:
+               return "INVALID_PARAMETER";
+       case WIFI_AWARE_ERROR_NOW_IN_PROGRESS:
+               return "NOW_IN_PROGRESS";
+       case WIFI_AWARE_ERROR_NOT_SUPPORTED:
+               return "NOT_SUPPORTED";
+       case WIFI_AWARE_ERROR_OPERATION_FAILED:
+               return "OPERATION_FAILED";
+       case WIFI_AWARE_ERROR_NOT_INITIALIZED:
+               return "NOT_INITIALIZED";
+       case WIFI_AWARE_ERROR_ALREADY_INITIALIZED:
+               return "ALREADY_INITIALIZED";
+       case WIFI_AWARE_ERROR_ALREADY_ENABLED:
+               return "ALREADY_ENABLED";
+       case WIFI_AWARE_ERROR_REJECTED_BY_PEER:
+               return "REJECTED_BY_PEER";
+       case WIFI_AWARE_ERROR_INTERFACE_DOWN:
+               return "INTERFACE_IS_DOWN";
+       default:
+               return "UNKNOWN";
+       }
+}
diff --git a/tests/unittest/wifi-aware-unittest-util.h b/tests/unittest/wifi-aware-unittest-util.h
new file mode 100644 (file)
index 0000000..8b746a2
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#pragma once
+
+#include <glib.h>
+#include "wifi-aware.h"
+
+#define CHECK_WIFI_AWARE_SUPPORTED(ret) do { \
+        if (!g_wifi_aware_supported) { \
+            ASSERT_EQ(ret, WIFI_AWARE_ERROR_NOT_SUPPORTED) \
+                       << "FAIL NOT_SUPPORTED"; \
+        } \
+    } while (0)
+
+#define CHECK_RESULT(ret, expected, message) do { \
+        CHECK_WIFI_AWARE_SUPPORTED(ret); \
+        ASSERT_EQ(ret, expected) << "FAIL " << message; \
+    } while (0)
+
+#define CALLBACK_TIMEOUT 20000
+#define RUN_GMAIN_LOOP do { \
+        g_main_loop = g_main_loop_new(NULL, false); \
+        g_timeout_id = g_timeout_add(CALLBACK_TIMEOUT, _wifi_aware_callback_timeout, g_main_loop); \
+        g_main_loop_run(g_main_loop); \
+        g_source_remove(g_timeout_id); \
+        g_main_loop = NULL; \
+    } while (0)
+
+#define QUIT_GMAIN_LOOP do { \
+        if (g_main_loop) { \
+            g_main_loop_quit(g_main_loop); \
+            g_main_loop = NULL; \
+        } \
+    } while (0)
+
+gboolean _wifi_aware_callback_timeout(gpointer data);
+const char *_convert_error_to_string(wifi_aware_error_e error);
diff --git a/tests/unittest/wifi-aware-unittest.cpp b/tests/unittest/wifi-aware-unittest.cpp
new file mode 100644 (file)
index 0000000..d4e6fab
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd 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.
+ */
+
+#include <iostream>
+#include <gtest/gtest.h>
+
+#include "wifi-aware-unittest-fixture.h"
+#include "wifi-aware-unittest-util.h"
+#include "wifi-aware.h"
+
+extern GMainLoop *g_main_loop;
+extern guint g_timeout_id;
+
+void __enabled_cb(wifi_aware_error_e error, void *user_data)
+{
+       if (error == WIFI_AWARE_ERROR_NONE) {
+       //      LOGI("Successfully Enabled");
+               g_enabled = true;
+       }
+       else {
+       //      LOGE("Fail to enable %s(%d)", _wifi_aware_error_to_string(error), error);
+       }
+       QUIT_GMAIN_LOOP;
+}
+
+TEST_F(WifiAware, wifi_aware_initialize_p)
+{
+       int ret = 0;
+
+       wifi_aware_deinitialize();
+
+       ret = wifi_aware_initialize();
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_initialize");
+}
+
+TEST_F(WifiAware, wifi_aware_initialize_n)
+{
+       int ret = 0;
+       ret = wifi_aware_initialize();
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_ALREADY_INITIALIZED, "wifi_aware_initialize");
+}
+
+TEST_F(WifiAware, wifi_aware_deinitialize_p)
+{
+       int ret = 0;
+       ret = wifi_aware_deinitialize();
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_deinitialize");
+}
+
+TEST_F(WifiAware, wifi_aware_deinitialize_n)
+{
+       int ret = 0;
+       wifi_aware_deinitialize();
+
+       ret = wifi_aware_deinitialize();
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_NOT_INITIALIZED, "wifi_aware_deinitialize");
+}
+
+TEST_F(WifiAware, wifi_aware_enable_p)
+{
+       int ret = 0;
+
+       ret = wifi_aware_enable(__enabled_cb, NULL);
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_enable");
+
+       RUN_GMAIN_LOOP;
+
+       ASSERT_TRUE(g_enabled) << "FAIL Enabled";
+
+       wifi_aware_disable();
+}
+
+TEST_F(WifiAware, wifi_aware_enable_n)
+{
+       int ret = 0;
+
+       wifi_aware_enable(__enabled_cb, NULL);
+       RUN_GMAIN_LOOP;
+
+       ret = wifi_aware_enable(__enabled_cb, NULL);
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_ALREADY_ENABLED, "wifi_aware_enable");
+
+       wifi_aware_disable();
+}
+
+TEST_F(WifiAware, wifi_aware_disable_p)
+{
+       int ret = 0;
+
+       wifi_aware_enable(__enabled_cb, NULL);
+       RUN_GMAIN_LOOP;
+
+       ret = wifi_aware_disable();
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_NONE, "wifi_aware_disable");
+
+       wifi_aware_disable();
+}
+
+TEST_F(WifiAware, wifi_aware_disable_n)
+{
+       int ret = 0;
+
+       ret = wifi_aware_disable();
+       CHECK_RESULT(ret, WIFI_AWARE_ERROR_INVALID_OPERATION, "wifi_aware_disable");
+}