--- /dev/null
+/*
+ * Copyright (c) 2022 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 <aul.h>
+#include <aul_cmd.h>
+#include <aul_sock.h>
+#include <gtest/gtest.h>
+
+#include <bundle_cpp.h>
+
+#include "mock/mock_hook.h"
+#include "mock/socket_mock.h"
+#include "mock/test_fixture.h"
+#include "src/launch.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::Invoke;
+
+namespace {
+
+constexpr const char kAppId[] = "org.tizen.helloworld";
+constexpr const pid_t kPid = 100;
+constexpr const char kExec[] =
+ "/home/owner/apps_rw/org.tizen.helloworld/bin/helloworld";
+
+class Mocks : virtual public ::testing::NiceMock<SocketMock> {};
+
+int AulStatusLocalCb(int status, void* user_data) {
+ return 0;
+}
+
+} // namespace
+
+class StatusTest : public TestFixture {
+ public:
+ StatusTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+ virtual void SetUp() {
+ if (aul_is_initialized())
+ return;
+
+ aul_launch_init(nullptr, nullptr);
+ }
+
+ virtual void TearDown() {
+ aul_launch_fini();
+ }
+
+ bool touched_ = false;
+};
+
+TEST_F(StatusTest, aul_status_update_P) {
+ int cmd = -1;
+ int opt = -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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ return n;
+ }));
+
+ int ret = aul_status_update(STATUS_VISIBLE);
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd, APP_STATUS_UPDATE);
+ EXPECT_EQ(opt, AUL_SOCK_NOREPLY);
+}
+
+TEST_F(StatusTest, aul_app_get_status_bypid_P) {
+ int cmd = -1;
+ int opt = -1;
+ bundle* kb = nullptr;
+
+ 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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ kb = bundle_decode(pkt->data, pkt->len);
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+ int ret = STATUS_BG;
+ memcpy(buf, &ret, sizeof(int));
+ return sizeof(int);
+ }));
+
+ int status = aul_app_get_status_bypid(100);
+ EXPECT_NE(kb, nullptr);
+ tizen_base::Bundle b(kb, false, true);
+
+ EXPECT_EQ(status, STATUS_BG);
+ EXPECT_EQ(cmd, APP_GET_STATUS);
+ EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+ EXPECT_EQ(b.GetString(AUL_K_PID), std::to_string(100));
+}
+
+TEST_F(StatusTest, aul_app_get_status_P) {
+ int cmd = -1;
+ int opt = -1;
+ bundle* kb = nullptr;
+
+ 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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ kb = bundle_decode(pkt->data, pkt->len);
+ return n;
+ }));
+ EXPECT_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+ .Times(1)
+ .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+ int ret = STATUS_VISIBLE;
+ memcpy(buf, &ret, sizeof(int));
+ return sizeof(int);
+ }));
+
+ int status = aul_app_get_status(kAppId);
+ EXPECT_NE(kb, nullptr);
+ tizen_base::Bundle b(kb, false, true);
+
+ EXPECT_EQ(status, STATUS_VISIBLE);
+ EXPECT_EQ(cmd, APP_GET_STATUS_BY_APPID);
+ EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+ EXPECT_EQ(b.GetString(AUL_K_APPID), kAppId);
+}
+
+TEST_F(StatusTest, aul_app_get_status_N) {
+ int status = aul_app_get_status(nullptr);
+ EXPECT_EQ(status, AUL_R_EINVAL);
+}
+
+TEST_F(StatusTest, aul_add_status_local_cb_P) {
+ int ret = aul_add_status_local_cb(AulStatusLocalCb, this);
+ EXPECT_EQ(ret, AUL_R_OK);
+}
+
+TEST_F(StatusTest, aul_add_status_local_cb_N) {
+ int ret = aul_add_status_local_cb(nullptr, nullptr);
+ EXPECT_EQ(ret, -1);
+}
+
+TEST_F(StatusTest, aul_remove_status_local_cb_P) {
+ int ret = aul_add_status_local_cb(AulStatusLocalCb, this);
+ EXPECT_EQ(ret, AUL_R_OK);
+
+ ret = aul_remove_status_local_cb(AulStatusLocalCb, this);
+ EXPECT_EQ(ret, AUL_R_OK);
+}
+
+TEST_F(StatusTest, aul_remove_status_local_cb_N) {
+ int ret = aul_remove_status_local_cb(nullptr, nullptr);
+ EXPECT_EQ(ret, -1);
+}
+
+TEST_F(StatusTest, aul_invoke_status_local_cb_P) {
+ touched_ = false;
+ int ret = aul_add_status_local_cb(
+ [](int status, void* user_data) {
+ auto* test = static_cast<StatusTest*>(user_data);
+ test->touched_ = true;
+ return -1;
+ }, this);
+ EXPECT_EQ(ret, AUL_R_OK);
+
+ ret = aul_invoke_status_local_cb(STATUS_VISIBLE);
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(touched_, true);
+}
+
+TEST_F(StatusTest, aul_set_process_group_P) {
+ int cmd = -1;
+ int opt = -1;
+ bundle* kb = nullptr;
+
+ 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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ kb = bundle_decode(pkt->data, pkt->len);
+ 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_set_process_group(kPid, getpid());
+ EXPECT_NE(kb, nullptr);
+ tizen_base::Bundle b(kb, false, true);
+
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd, APP_SET_PROCESS_GROUP);
+ EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+ EXPECT_EQ(b.GetString(AUL_K_OWNER_PID), std::to_string(kPid));
+ EXPECT_EQ(b.GetString(AUL_K_CHILD_PID), std::to_string(getpid()));
+}
+
+TEST_F(StatusTest, aul_listen_app_status_P) {
+ int cmd = -1;
+ int opt = -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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ 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);
+ }));
+
+ status_listen_h handle = nullptr;
+ int ret = aul_listen_app_status(kAppId,
+ [](aul_app_info* info, int status, void* user_data) {
+ return 0;
+ }, this, &handle);
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd, APP_COM_CREATE);
+ EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+}
+
+TEST_F(StatusTest, aul_listen_app_status_N) {
+ int ret = aul_listen_app_status(kAppId,
+ [](aul_app_info* info, int status, void* user_data) {
+ return 0;
+ }, this, nullptr);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+
+ status_listen_h handle = nullptr;
+ ret = aul_listen_app_status(kAppId, nullptr, this, &handle);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+
+ ret = aul_listen_app_status(nullptr,
+ [](aul_app_info* info, int status, void* user_data) {
+ return 0;
+ }, this, &handle);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+}
+
+TEST_F(StatusTest, aul_ignore_app_status_N) {
+ int ret = aul_ignore_app_status(nullptr);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+}
+
+TEST_F(StatusTest, aul_notify_exit_P) {
+ int cmd = -1;
+ int opt = -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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ return n;
+ }));
+
+ int ret = aul_notify_exit();
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd, APP_NOTIFY_EXIT);
+ EXPECT_EQ(opt, AUL_SOCK_NOREPLY);
+}
+
+TEST_F(StatusTest, aul_notify_start_P) {
+ int cmd = -1;
+ int opt = -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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ return n;
+ }));
+
+ int ret = aul_notify_start();
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd, APP_NOTIFY_START);
+ EXPECT_EQ(opt, AUL_SOCK_NOREPLY);
+}
+
+TEST_F(StatusTest, aul_app_status_convert_to_string_P) {
+ const char* status = aul_app_status_convert_to_string(STATUS_VISIBLE);
+ EXPECT_NE(status, nullptr);
+ EXPECT_EQ(std::string(status), "STATUS_VISIBLE");
+}
+
+TEST_F(StatusTest, aul_app_status_convert_to_string_N) {
+ const char* status = aul_app_status_convert_to_string(-1);
+ EXPECT_EQ(std::string(status), "Unknown status");
+}
+
+TEST_F(StatusTest, aul_status_update_v2_P) {
+ int cmd = -1;
+ int opt = -1;
+ bundle* kb = nullptr;
+
+ 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* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd = pkt->cmd;
+ opt = pkt->opt;
+ kb = bundle_decode(pkt->data, pkt->len);
+ 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_status_update_v2(STATUS_BG);
+ EXPECT_NE(kb, nullptr);
+ tizen_base::Bundle b(kb, false, true);
+
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd, APP_STATUS_UPDATE_V2);
+ EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+ EXPECT_EQ(b.GetString(AUL_K_STATUS), std::to_string(STATUS_BG));
+}
+
+TEST_F(StatusTest, aul_status_update_v2_N) {
+ int ret = aul_status_update_v2(-1);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+}