From 94f459274dd6b931258488fc116fe1ce5c09a1b3 Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Tue, 18 May 2021 00:35:34 -0400 Subject: [PATCH] Add test cases for launch.c - Add a TC for aul_launch_app - Add a TC for aul_terminate_pid Change-Id: I29360b43698801e4bfe52a8824313e5667d418ea Signed-off-by: jh9216.park --- packaging/aul.spec | 3 +- test/unit_tests/CMakeLists.txt | 26 ++++++++-- test/unit_tests/main.cc | 16 +++++++ test/unit_tests/mock/socket_mock.cc | 46 ++++++++++++++++++ test/unit_tests/mock/socket_mock.h | 52 ++++++++++++++++++++ test/unit_tests/test_launch.cc | 95 +++++++++++++++++++++++++++++++++++++ 6 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 test/unit_tests/mock/socket_mock.cc create mode 100644 test/unit_tests/mock/socket_mock.h create mode 100644 test/unit_tests/test_launch.cc diff --git a/packaging/aul.spec b/packaging/aul.spec index 5b83ac9..691f8f8 100644 --- a/packaging/aul.spec +++ b/packaging/aul.spec @@ -143,7 +143,8 @@ find . -name '*.gcno' -exec cp '{}' gcov-obj ';' %endif %check -ctest --output-on-failure %{?_smp_mflags} +export LD_LIBRARY_PATH="../../" +ctest -V %if 0%{?gcov:1} lcov -c --ignore-errors graph --no-external -q -d . -o %{name}.info genhtml %{name}.info -o %{name}.out diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt index 9232e70..79e009b 100644 --- a/test/unit_tests/CMakeLists.txt +++ b/test/unit_tests/CMakeLists.txt @@ -1,18 +1,38 @@ AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS) -AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock/ UNIT_TESTS_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock/ UNIT_TESTS_MOCK) -ADD_EXECUTABLE(${TARGET_AUL_UNIT_TESTS} ${UNIT_TESTS_SRCS}) +ADD_EXECUTABLE(${TARGET_AUL_UNIT_TESTS} + ${UNIT_TESTS_SRCS} + ${UNIT_TESTS_MOCK} +) TARGET_INCLUDE_DIRECTORIES(${TARGET_AUL_UNIT_TESTS} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../" + "${CMAKE_CURRENT_SOURCE_DIR}/../../" "${CMAKE_CURRENT_SOURCE_DIR}/../../include" "${CMAKE_CURRENT_SOURCE_DIR}/../../src" + "${CMAKE_CURRENT_SOURCE_DIR}/../../aul" + "${CMAKE_CURRENT_SOURCE_DIR}/../../aul/api" ) APPLY_PKG_CONFIG(${TARGET_AUL_UNIT_TESTS} PUBLIC - GLIB_DEPS GMOCK_DEPS BUNDLE_DEPS + CAPI_SYSTEM_INFO_DEPS + DLOG_DEPS + GIO_DEPS + GLIB_DEPS + INIPARSER_DEPS + LIBSMACK_DEPS + LIBTZPLATFORM_CONFIG_DEPS + LIBXML_DEPS + PARCEL_DEPS + PKGMGR_INFO_DEPS + STORAGE_DEPS + TTRACE_DEPS + UUID_DEPS + VCONF_DEPS + XDGMIME_DEPS ) TARGET_LINK_LIBRARIES(${TARGET_AUL_UNIT_TESTS} PUBLIC ${TARGET_AUL}) diff --git a/test/unit_tests/main.cc b/test/unit_tests/main.cc index 1702774..b3b4e12 100644 --- a/test/unit_tests/main.cc +++ b/test/unit_tests/main.cc @@ -5,6 +5,22 @@ #include #include +//#define LOG_INTERNAL + +#include + +extern "C" int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...) { +#ifdef LOG_INTERNAL + printf("%s:", tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); +#endif + return 0; +} + int main(int argc, char** argv) { try { testing::InitGoogleTest(&argc, argv); diff --git a/test/unit_tests/mock/socket_mock.cc b/test/unit_tests/mock/socket_mock.cc new file mode 100644 index 0000000..081fb86 --- /dev/null +++ b/test/unit_tests/mock/socket_mock.cc @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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 "mock/socket_mock.h" +#include "mock/mock_hook.h" +#include "mock/test_fixture.h" + +extern "C" ssize_t recv (int fd, void* buf, size_t n, int flags) { + return MOCK_HOOK_P4(SocketMock, recv, fd, buf, n, flags); +} + +extern "C" ssize_t send (int fd, const void* buf, size_t n, int flags) { + return MOCK_HOOK_P4(SocketMock, send, fd, buf, n, flags); +} + +extern "C" int socket (int domain, int type, int protocol) { + return MOCK_HOOK_P3(SocketMock, socket, domain, type, protocol); +} + +extern "C" int connect(int sockfd, const struct sockaddr* addr, + socklen_t addrlen) { + return MOCK_HOOK_P3(SocketMock, connect, sockfd, addr, addrlen); +} + +extern "C" int close(int fd) { + return 0; +} + +extern "C" int fcntl(int fd, int cmd, ... /* arg */ ) { + return 0; +} \ No newline at end of file diff --git a/test/unit_tests/mock/socket_mock.h b/test/unit_tests/mock/socket_mock.h new file mode 100644 index 0000000..0f85412 --- /dev/null +++ b/test/unit_tests/mock/socket_mock.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#ifndef TEST_UNIT_TESTS_MOCK_SOCKET_MOCK_H_ +#define TEST_UNIT_TESTS_MOCK_SOCKET_MOCK_H_ + +#include +#include +#include +#include + +#include "mock/module_mock.h" + +constexpr const int kFakeFd = 100; + +class SocketMock : public virtual ModuleMock { + public: + SocketMock() { + using ::testing::_; + using ::testing::Return; + using ::testing::Invoke; + + ON_CALL(*this, send(_, _, _, _)) + .WillByDefault(Invoke([&]( + int, const void*, size_t size, int) -> ssize_t { + return size; + })); + ON_CALL(*this, socket(_, _, _)) + .WillByDefault(Return(kFakeFd)); + } + + MOCK_METHOD4(recv, ssize_t (int, void*, size_t, int)); + MOCK_METHOD4(send, ssize_t (int, const void*, size_t, int)); + MOCK_METHOD3(socket, int (int, int, int)); + MOCK_METHOD3(connect, int (int, const struct sockaddr*, socklen_t)); +}; + +#endif // TEST_UNIT_TESTS_MOCK_SOCKET_MOCK_H_ + diff --git a/test/unit_tests/test_launch.cc b/test/unit_tests/test_launch.cc new file mode 100644 index 0000000..1c84325 --- /dev/null +++ b/test/unit_tests/test_launch.cc @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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 +#include +#include +#include + +#include "mock/socket_mock.h" +#include "mock/mock_hook.h" +#include "mock/test_fixture.h" +#include "mock/socket_mock.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::Invoke; +using ::testing::SaveArg; + +namespace { + +class Mocks : virtual public ::testing::NiceMock {}; + +} // namespace + +class LaunchTest : public TestFixture { + public: + LaunchTest() : TestFixture(std::make_unique<::Mocks>()) {} + + virtual void SetUp() { } + virtual void TearDown() {} +}; + +TEST_F(LaunchTest, aul_launch_app) { + tizen_base::Bundle b; + EXPECT_CALL(GetMock(), socket(_, _, _)) + .Times(1); + + EXPECT_CALL(GetMock(), connect(_, _, _)) + .Times(1); + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1); + + EXPECT_CALL(GetMock(), recv(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t { + int ret = 0; + memcpy(buf, &ret, sizeof(int)); + return sizeof(int); + })); + + int ret = aul_launch_app("test_appid", b.GetHandle()); + EXPECT_EQ(ret, AUL_R_OK); +} + +TEST_F(LaunchTest, aul_terminate_pid) { + tizen_base::Bundle b; + EXPECT_CALL(GetMock(), socket(_, _, _)) + .Times(1); + + EXPECT_CALL(GetMock(), connect(_, _, _)) + .Times(1); + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1); + + EXPECT_CALL(GetMock(), recv(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t { + int ret = 0; + memcpy(buf, &ret, sizeof(int)); + return sizeof(int); + })); + + int ret = aul_terminate_pid(1000); + EXPECT_EQ(ret, AUL_R_OK); +} -- 2.7.4