From dcb4ae2c14023178e02d5f263a75094937ad3dcf Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Fri, 18 Mar 2022 15:24:26 +0900 Subject: [PATCH] Initial thread unittest codes Change-Id: Id88de3baf2d2c874da4c62b3bea05809382179de Signed-off-by: hyunuk.tak --- packaging/capi-network-thread.spec | 54 ++++++++++++++++++++++- tests/unittest/CMakeLists.txt | 25 +++++++++++ tests/unittest/mocks/thread-mock-dbus.cpp | 66 +++++++++++++++++++++++++++++ tests/unittest/mocks/thread-mock-dummy.cpp | 66 +++++++++++++++++++++++++++++ tests/unittest/mocks/thread-mock-dummy.h | 30 +++++++++++++ tests/unittest/mocks/thread-mock-socket.cpp | 62 +++++++++++++++++++++++++++ tests/unittest/mocks/thread-mock-socket.h | 29 +++++++++++++ tests/unittest/mocks/thread-mock.cpp | 23 ++++++++++ tests/unittest/thread-unittest-init.cpp | 52 +++++++++++++++++++++++ tests/unittest/thread-unittest-main.cpp | 23 ++++++++++ 10 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 tests/unittest/CMakeLists.txt create mode 100644 tests/unittest/mocks/thread-mock-dbus.cpp create mode 100644 tests/unittest/mocks/thread-mock-dummy.cpp create mode 100644 tests/unittest/mocks/thread-mock-dummy.h create mode 100644 tests/unittest/mocks/thread-mock-socket.cpp create mode 100644 tests/unittest/mocks/thread-mock-socket.h create mode 100644 tests/unittest/mocks/thread-mock.cpp create mode 100644 tests/unittest/thread-unittest-init.cpp create mode 100644 tests/unittest/thread-unittest-main.cpp diff --git a/packaging/capi-network-thread.spec b/packaging/capi-network-thread.spec index 1309292..5f79260 100644 --- a/packaging/capi-network-thread.spec +++ b/packaging/capi-network-thread.spec @@ -16,8 +16,13 @@ BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-appfw-app-control) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(gtest) BuildRequires: cmake +%if 0%{?gcov:1} +BuildRequires: lcov +%endif + %description Network Thread Framework @@ -39,6 +44,14 @@ Requires: %{name} = %{version}-%{release} %description test This package is for Network Thread Framework CAPI test application +%if 0%{?gcov:1} +%package gcov +Summary: Coverage Data of %{name} + +%description gcov +The %{name}-gcov package contains gcov objects +%endif + %prep %setup -q cp %{SOURCE1001} %{SOURCE1002} . @@ -57,17 +70,51 @@ export CXXFLAGS+=" -DARCH64" export FFLAGS+=" -DARCH64" %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 + %cmake MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` -%cmake . -DFULLVER=%{version} -DMAJORVER=${MAJORVER} +%cmake . \ + -DFULLVER=%{version} \ + -DMAJORVER=${MAJORVER} \ +%if 0%{?gcov:1} + -DUNITTEST=ON \ +%endif + -DBIN_DIR:PATH=%{_bindir} 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 +%if 0%{?gcov:1} +LD_LIBRARY_PATH=%{buildroot}%{_libdir} ./tests/unittest/thread-unittest +%endif + +%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 @@ -88,4 +135,9 @@ rm -rf %{buildroot} %files test %manifest %{name}.manifest %attr(777,network_fw,network_fw) %{_bindir}/thread-test +%attr(777,network_fw,network_fw) %{_bindir}/thread-unittest +%if 0%{?gcov:1} +%files gcov +%{_datadir}/gcov/obj/* +%endif diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt new file mode 100644 index 0000000..d91138c --- /dev/null +++ b/tests/unittest/CMakeLists.txt @@ -0,0 +1,25 @@ +SET(THREAD_UNITTEST "thread-unittest") +LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR}/include + ${${fw_name}_INCLUDE_DIRS} +) + +SET(GTEST gtest) + +FILE(GLOB UNITTEST_SRCS *.cpp mocks/*.cpp ${CMAKE_SOURCE_DIR}/src/*.c) +ADD_EXECUTABLE(${THREAD_UNITTEST} ${UNITTEST_SRCS}) +TARGET_LINK_LIBRARIES(${THREAD_UNITTEST} + ${TARGET_THREAD} + ${${fw_name}_LIBRARIES} + ${GTEST} +) + +SET_TARGET_PROPERTIES(${THREAD_UNITTEST} PROPERTIES + COMPILE_FLAGS "-fPIE") + +INSTALL(TARGETS ${THREAD_UNITTEST} DESTINATION "${BIN_DIR}") diff --git a/tests/unittest/mocks/thread-mock-dbus.cpp b/tests/unittest/mocks/thread-mock-dbus.cpp new file mode 100644 index 0000000..6a15cf1 --- /dev/null +++ b/tests/unittest/mocks/thread-mock-dbus.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 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 +#include +#include "thread-mock-dummy.h" + +GDBusConnection *g_bus_get_sync(GBusType bus_type, + GCancellable *cancellable, GError **error) +{ + return (GDBusConnection *)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); +} + +GDBusProxy *g_dbus_proxy_new_for_bus_sync(GBusType bus_type, + GDBusProxyFlags flags, GDBusInterfaceInfo *info, + const gchar *name, const gchar *object_path, + const gchar *interface_name, GCancellable *cancellable, + GError **error) +{ + return (GDBusProxy *)g_object_new(G_TYPE_DBUS_PROXY, NULL); +} + +GDBusProxy *g_dbus_proxy_new_sync(GDBusConnection *connection, + GDBusProxyFlags flags, GDBusInterfaceInfo *info, + const gchar *name, const gchar *object_path, + const gchar *interface_name, GCancellable *cancellable, + GError **error) +{ + return (GDBusProxy *)g_object_new(G_TYPE_DBUS_PROXY, NULL); +} + +guint g_dbus_connection_signal_subscribe(GDBusConnection *connection, + const gchar *sender, + const gchar *interface_name, + const gchar *member, + const gchar *object_path, + const gchar *arg0, + GDBusSignalFlags flags, + GDBusSignalCallback callback, + gpointer user_data, + GDestroyNotify user_data_free_func) +{ + return _subscribe_signal(interface_name, member, callback, user_data); +} + +void g_dbus_connection_signal_unsubscribe(GDBusConnection *connection, + guint subscription_id) +{ +} + +void g_object_unref(gpointer object) +{ +} \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-dummy.cpp b/tests/unittest/mocks/thread-mock-dummy.cpp new file mode 100644 index 0000000..726cf3c --- /dev/null +++ b/tests/unittest/mocks/thread-mock-dummy.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 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 "thread-mock-dummy.h" + +static GVariant * __properties_changed() +{ + return nullptr; +} + +struct { + const gchar *interface_name; + const gchar *signal_name; + GVariant *(*create_parameters)(); + GDBusSignalCallback callback; + gpointer user_data; +} thread_gdbus_signal_list[] = { + [PROPERTIES_CHANGED] = { + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + __properties_changed, + NULL, + NULL, + }, + { + NULL, + NULL, + NULL, + NULL, + NULL, + }, +}; + +static bool __is_same_signal(int i, const char *interface_name, const char *signal_name) +{ + return (g_strcmp0(thread_gdbus_signal_list[i].interface_name, interface_name) == 0 + && g_strcmp0(thread_gdbus_signal_list[i].signal_name, signal_name) == 0); +} + +guint _subscribe_signal(const char *interface_name, + const char *member, GDBusSignalCallback callback, + gpointer user_data) +{ + for (int i = 0; ; ++i) { + if (thread_gdbus_signal_list[i].interface_name == NULL) + break; + if (__is_same_signal(i, interface_name, member)) { + thread_gdbus_signal_list[i].callback = callback; + thread_gdbus_signal_list[i].user_data = user_data; + } + } + return DUMMY_SIGNAL_ID; +} \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-dummy.h b/tests/unittest/mocks/thread-mock-dummy.h new file mode 100644 index 0000000..54df057 --- /dev/null +++ b/tests/unittest/mocks/thread-mock-dummy.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 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 +#include + +#define DUMMY_SIGNAL_ID 1 + +typedef enum { + PROPERTIES_CHANGED = 0, +} thread_gdbus_signal_e; + +guint _subscribe_signal(const char *interface_name, + const char *member, GDBusSignalCallback callback, + gpointer user_data); diff --git a/tests/unittest/mocks/thread-mock-socket.cpp b/tests/unittest/mocks/thread-mock-socket.cpp new file mode 100644 index 0000000..27fc5da --- /dev/null +++ b/tests/unittest/mocks/thread-mock-socket.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 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 +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// static bool __thread_mock_socket = false; +// extern int __real_socket(int domain, int type, int protocol); + +// void thread_mock_set_socket(bool flag) +// { +// __thread_mock_socket = flag; +// } + +// int __wrap_socket(int domain, int type, int protocol) +// { +// return __thread_mock_socket ? 1 : __real_socket(domain, type, protocol); +// } + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +{ + return 0; +} + +int write(int fd, const void *buf, unsigned int len) +{ + return len; +} + +int select(int nfds, fd_set *readfds, + fd_set *writefds, fd_set *exceptfds, + struct timeval * timeout) +{ + return 1; +} + +ssize_t read(int fd, void *buf, size_t nbytes) +{ + snprintf((char *)buf, nbytes, "Done\n"); + return 5; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-socket.h b/tests/unittest/mocks/thread-mock-socket.h new file mode 100644 index 0000000..6705f62 --- /dev/null +++ b/tests/unittest/mocks/thread-mock-socket.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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 + +#ifdef __cplusplus +extern "C" { +#endif + +void thread_mock_set_socket(bool flag); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock.cpp b/tests/unittest/mocks/thread-mock.cpp new file mode 100644 index 0000000..d4e38e9 --- /dev/null +++ b/tests/unittest/mocks/thread-mock.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 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 + +int system_info_get_platform_bool(const char *key, bool *value) +{ + *value = true; + return SYSTEM_INFO_ERROR_NONE; +} \ No newline at end of file diff --git a/tests/unittest/thread-unittest-init.cpp b/tests/unittest/thread-unittest-init.cpp new file mode 100644 index 0000000..7c5aed7 --- /dev/null +++ b/tests/unittest/thread-unittest-init.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 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 + +#include "thread.h" +#include "mocks/thread-mock-socket.h" + +class ThreadInitTest : public ::testing::Test +{ +protected: + void SetUp() override + { + //thread_mock_set_socket(false); + } + + void TearDown() override + { + //thread_mock_set_socket(false); + } +}; + +TEST_F(ThreadInitTest, InitAlreadyDone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_initialize()); + EXPECT_EQ(THREAD_ERROR_ALREADY_DONE, thread_initialize()); + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); +} + +TEST_F(ThreadInitTest, DeinitNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_deinitialize()); +} + +TEST_F(ThreadInitTest, InitDeinit) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_initialize()); + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); +} \ No newline at end of file diff --git a/tests/unittest/thread-unittest-main.cpp b/tests/unittest/thread-unittest-main.cpp new file mode 100644 index 0000000..33ab729 --- /dev/null +++ b/tests/unittest/thread-unittest-main.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 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 + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file -- 2.7.4