--- /dev/null
+/*
+ * 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 <stdlib.h>
+#include <gtest/gtest.h>
+
+#include <bundle_cpp.h>
+#include <iostream>
+#include <memory>
+#include <aul.h>
+#include <aul_sock.h>
+#include <aul_cmd.h>
+
+#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<SocketMock>,
+ virtual public ::testing::NiceMock<OsMock>,
+ virtual public ::testing::NiceMock<DbusMock> {};
+
+} // namespace
+
+class LaunchWithResultTest : public TestFixture {
+ public:
+ LaunchWithResultTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+ virtual void SetUp() {
+ if (aul_is_initialized())
+ return;
+
+ EXPECT_CALL(GetMock<SocketMock>(), 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<app_pkt_t, decltype(free)*> MakePacket(
+ tizen_base::Bundle b) {
+ auto raw = b.ToRaw();
+ app_pkt_t* pkt =
+ static_cast<app_pkt_t*>(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<app_pkt_t, decltype(free)*>(pkt, free);
+ }
+};
+
+TEST_F(LaunchWithResultTest, aul_send_resume_request_for_uid) {
+ tizen_base::Bundle b;
+ int cmd = -1;
+ EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(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<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = header->cmd;
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), 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<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(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<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = header->cmd;
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), 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<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = header->cmd;
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), 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<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = header->cmd;
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), 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<SocketMock>(), send(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = header->cmd;
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), 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);
+}