From 28241d8c9eb826ed132c58719b04fd609a6961ec Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Tue, 25 May 2021 04:32:49 -0400 Subject: [PATCH] Add TCs for launch_with_result.c Change-Id: If4f333dff2ee10aed92e2ebe5aeb0be67a056b41 Signed-off-by: jh9216.park --- test/unit_tests/mock/os_mock.cc | 33 ++++ test/unit_tests/mock/os_mock.h | 38 +++++ test/unit_tests/mock/socket_mock.cc | 17 +- test/unit_tests/mock/socket_mock.h | 3 +- test/unit_tests/test_launch.cc | 2 + test/unit_tests/test_launch_with_result.cc | 266 +++++++++++++++++++++++++++++ 6 files changed, 350 insertions(+), 9 deletions(-) create mode 100644 test/unit_tests/mock/os_mock.cc create mode 100644 test/unit_tests/mock/os_mock.h create mode 100644 test/unit_tests/test_launch_with_result.cc diff --git a/test/unit_tests/mock/os_mock.cc b/test/unit_tests/mock/os_mock.cc new file mode 100644 index 0000000..0758acd --- /dev/null +++ b/test/unit_tests/mock/os_mock.cc @@ -0,0 +1,33 @@ +/* + * 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/mock_hook.h" +#include "mock/os_mock.h" +#include "mock/test_fixture.h" + +extern "C" int mkdir(const char* path, mode_t mode) { + return MOCK_HOOK_P2(OsMock, mkdir, path, mode); +} + +extern "C" int close(int fd) { + return 0; +} + +extern "C" int fcntl(int fd, int cmd, ... /* arg */ ) { + return 0; +} diff --git a/test/unit_tests/mock/os_mock.h b/test/unit_tests/mock/os_mock.h new file mode 100644 index 0000000..1e46950 --- /dev/null +++ b/test/unit_tests/mock/os_mock.h @@ -0,0 +1,38 @@ +/* + * 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_OS_MOCK_H_ +#define TEST_UNIT_TESTS_MOCK_OS_MOCK_H_ + +#include +#include +#include +#include + +#include "mock/module_mock.h" + +class OsMock : public virtual ModuleMock { + public: + OsMock() { + using ::testing::_; + using ::testing::Return; + using ::testing::Invoke; + } + + MOCK_METHOD2(mkdir, int (const char* path, mode_t)); +}; + +#endif // TEST_UNIT_TESTS_MOCK_OS_MOCK_H_ diff --git a/test/unit_tests/mock/socket_mock.cc b/test/unit_tests/mock/socket_mock.cc index 081fb86..f3a7885 100644 --- a/test/unit_tests/mock/socket_mock.cc +++ b/test/unit_tests/mock/socket_mock.cc @@ -20,15 +20,15 @@ #include "mock/mock_hook.h" #include "mock/test_fixture.h" -extern "C" ssize_t recv (int fd, void* buf, size_t n, int flags) { +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) { +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) { +extern "C" int socket(int domain, int type, int protocol) { return MOCK_HOOK_P3(SocketMock, socket, domain, type, protocol); } @@ -37,10 +37,11 @@ extern "C" int connect(int sockfd, const struct sockaddr* addr, return MOCK_HOOK_P3(SocketMock, connect, sockfd, addr, addrlen); } -extern "C" int close(int fd) { - return 0; +extern "C" int listen(int sockfd, int backlog) { + return MOCK_HOOK_P2(SocketMock, listen, sockfd, backlog); } -extern "C" int fcntl(int fd, int cmd, ... /* arg */ ) { - return 0; -} \ No newline at end of file +extern "C" int bind(int sockfd, const struct sockaddr* addr, + socklen_t addrlen) { + return MOCK_HOOK_P3(SocketMock, bind, sockfd, addr, addrlen); +} diff --git a/test/unit_tests/mock/socket_mock.h b/test/unit_tests/mock/socket_mock.h index 0f85412..cb7ab40 100644 --- a/test/unit_tests/mock/socket_mock.h +++ b/test/unit_tests/mock/socket_mock.h @@ -46,7 +46,8 @@ class SocketMock : public virtual ModuleMock { 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)); + MOCK_METHOD2(listen, int (int, int)); + MOCK_METHOD3(bind, 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 index ea1cc26..7875fbc 100644 --- a/test/unit_tests/test_launch.cc +++ b/test/unit_tests/test_launch.cc @@ -27,6 +27,7 @@ #include "launch.h" #include "mock/dbus_mock.h" #include "mock/mock_hook.h" +#include "mock/os_mock.h" #include "mock/socket_mock.h" #include "mock/test_fixture.h" @@ -40,6 +41,7 @@ using ::testing::SaveArg; namespace { class Mocks : virtual public ::testing::NiceMock, + virtual public ::testing::NiceMock, virtual public ::testing::NiceMock {}; } // namespace diff --git a/test/unit_tests/test_launch_with_result.cc b/test/unit_tests/test_launch_with_result.cc new file mode 100644 index 0000000..33979c1 --- /dev/null +++ b/test/unit_tests/test_launch_with_result.cc @@ -0,0 +1,266 @@ +/* + * 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 +#include + +#include "launch.h" +#include "mock/dbus_mock.h" +#include "mock/mock_hook.h" +#include "mock/os_mock.h" +#include "mock/socket_mock.h" +#include "mock/test_fixture.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, + virtual public ::testing::NiceMock, + virtual public ::testing::NiceMock {}; + +} // namespace + +class LaunchWithResultTest : public TestFixture { + public: + LaunchWithResultTest() : TestFixture(std::make_unique<::Mocks>()) {} + + virtual void SetUp() { + if (aul_is_initialized()) + return; + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + return n; + })); + aul_launch_init(nullptr, nullptr); + } + + virtual void TearDown() { + } + + std::unique_ptr MakePacket( + tizen_base::Bundle b) { + auto raw = b.ToRaw(); + app_pkt_t* pkt = + static_cast(calloc(1, sizeof(app_pkt_t) + raw.second)); + pkt->opt = AUL_SOCK_BUNDLE; + pkt->cmd = AUL_R_OK; + pkt->len = raw.second; + memcpy(pkt->data, raw.first.get(), raw.second); + + return std::unique_ptr(pkt, free); + } +}; + +TEST_F(LaunchWithResultTest, aul_send_resume_request_for_uid) { + tizen_base::Bundle b; + int cmd = -1; + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + + int ret = aul_send_resume_request_for_uid("test_appid", b.GetHandle(), + getuid(), [](int, void*) {}, nullptr); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_SEND_RESUME_REQUEST); +} + +TEST_F(LaunchWithResultTest, aul_send_launch_request_sync_for_uid) { + int cmd = -1; + tizen_base::Bundle b = { {"Key1", "Val1"} }; + auto pkt = MakePacket(b); + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + EXPECT_CALL(GetMock(), recv(_, _, _, _)) + .Times(2) + .WillOnce(Invoke([&](int fd, void* buf, size_t n, int flags) -> ssize_t { + memcpy(buf, pkt.get(), n); // Header + return n; + })) + .WillOnce(Invoke([&](int fd, void* buf, size_t n, int flags) -> ssize_t { + memcpy(buf, pkt->data, n); // Body + return n; + })); + + bundle* res_b_raw = nullptr; + int ret = aul_send_launch_request_sync_for_uid("test_appid", b.GetHandle(), + getuid(), &res_b_raw); + tizen_base::Bundle res_b(res_b_raw, false, true); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_SEND_LAUNCH_REQUEST_SYNC); + EXPECT_EQ(res_b.GetString("Key1"), "Val1"); +} + +TEST_F(LaunchWithResultTest, aul_send_launch_request_for_uid) { + int cmd = -1; + tizen_base::Bundle b; + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + + int ret = aul_send_launch_request_for_uid("test_appid", b.GetHandle(), + getuid(), [](bundle*, int, void*) {}, [](int, void*) {}, nullptr); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_SEND_LAUNCH_REQUEST); +} + +TEST_F(LaunchWithResultTest, aul_launch_app_with_result_async_for_uid) { + int cmd = -1; + tizen_base::Bundle b; + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + 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_with_result_async_for_uid("test_appid", + b.GetHandle(), [](bundle*, int, void*) {}, nullptr, getuid()); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_START_RES_ASYNC); +} + +TEST_F(LaunchWithResultTest, aul_launch_app_with_result_async) { + int cmd = -1; + tizen_base::Bundle b; + + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + 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_with_result_async("test_appid", b.GetHandle(), + [](bundle*, int, void*) {}, nullptr); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_START_RES_ASYNC); +} + +TEST_F(LaunchWithResultTest, aul_subapp_terminate_request_pid) { + int cmd = -1; + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + 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_subapp_terminate_request_pid(100); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_TERM_REQ_BY_PID); +} + +TEST_F(LaunchWithResultTest, aul_create_result_bundle) { + tizen_base::Bundle inb = { + { AUL_K_CALLER_UID, "10" }, + { AUL_K_CALLER_PID, "100" }, + { AUL_K_SEQ_NUM, "11" } + }; + bundle* outb_raw; + int ret = aul_create_result_bundle(inb.GetHandle(), &outb_raw); + tizen_base::Bundle outb(outb_raw, false, true); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_NE(outb.GetHandle(), nullptr); +} + +TEST_F(LaunchWithResultTest, aul_forward_app) { + int cmd = -1; + EXPECT_CALL(GetMock(), send(_, _, _, _)) + .Times(1) + .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags) + -> ssize_t { + const app_pkt_t* header = reinterpret_cast(buf); + cmd = header->cmd; + return n; + })); + 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); + })); + + tizen_base::Bundle b = { + { AUL_K_CALLER_UID, "10" }, + { AUL_K_CALLER_PID, "100" }, + { AUL_K_SEQ_NUM, "11" } + }; + int ret = aul_forward_app("test-app", b.GetHandle()); + EXPECT_EQ(ret, AUL_R_OK); + EXPECT_EQ(cmd, APP_START); +} -- 2.7.4