--- /dev/null
+/*
+ * Copyright (c) 2020 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 <gtest/gtest.h>
+
+#include <rua.h>
+#include <rua_internal.h>
+
+#include <string>
+#include <vector>
+#include <memory>
+
+#include "aul_mock.h"
+#include "sqlite3_mock.h"
+#include "gio_mock.h"
+#include "test_fixture.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::SetArgPointee;
+using ::testing::Return;
+
+static char** __create_table(void) {
+ char** table = (char** )calloc(10, sizeof(char*));
+ table[0] = strdup("pkgname");
+ if (table[0] == nullptr)
+ goto out;
+
+ table[1] = strdup("apppath");
+ if (table[1] == nullptr)
+ goto out;
+
+ table[2] = strdup("arg");
+ if (table[2] == nullptr)
+ goto out;
+
+ table[3] = strdup("122232");
+ if (table[3] == nullptr)
+ goto out;
+
+ table[4] = strdup("instance_id");
+ if (table[4] == nullptr)
+ goto out;
+
+ table[5] = strdup("instance_name");
+ if (table[5] == nullptr)
+ goto out;
+
+ table[6] = strdup("icon");
+ if (table[6] == nullptr)
+ goto out;
+
+ table[7] = strdup("uri");
+ if (table[7] == nullptr)
+ goto out;
+
+ table[8] = strdup("image");
+ if (table[8] == nullptr)
+ goto out;
+
+ table[9] = strdup("compid");
+ if (table[9] == nullptr)
+ goto out;
+
+ return table;
+
+out:
+ for (int i = 0; i < 10; i++) {
+ if (table[i])
+ free(table[i]);
+ }
+
+ return nullptr;
+}
+
+class InternalMocks : public ::testing::NiceMock<GioMock>,
+ public ::testing::NiceMock<SqlMock> {};
+
+class RuaInternalTest : public TestFixture {
+ public:
+ RuaInternalTest() : TestFixture(std::make_unique<InternalMocks>()) {}
+ virtual ~RuaInternalTest() {}
+
+ virtual void SetUp() {
+ table_ = __create_table();
+ if (table_ == nullptr)
+ return;
+ }
+
+ virtual void TearDown() {
+ if (table_) {
+ for (int i = 0; i < 10; i++)
+ free(table_[i]);
+ free(table_);
+ }
+ }
+
+ public:
+ char** table_ = nullptr;
+};
+
+TEST_F(RuaInternalTest, rua_db_add_history) {
+ ASSERT_TRUE(table_ != nullptr);
+ sqlite3* db = (sqlite3*) table_;
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_free_table(_));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_get_table(_, _, _, _, _, _)).
+ WillOnce(DoAll(SetArgPointee<2>(table_),
+ SetArgPointee<3>(1),
+ SetArgPointee<4>(0), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_int(_, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+ WillOnce(Return(true));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_flush_sync(_, _, _)).
+ WillOnce(Return(true));
+
+ char** get_table = nullptr;
+ int rows = 0;
+ int cols = 0;
+ struct rua_rec record;
+ int ret = rua_history_load_db(&get_table, &rows, &cols);
+ EXPECT_EQ(ret, 0);
+ EXPECT_TRUE(get_table != nullptr);
+
+ ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
+ EXPECT_EQ(ret, 0);
+
+ ret = rua_db_add_history(&record);
+ EXPECT_EQ(ret, 0);
+
+ ret = rua_history_unload_db(&get_table);
+ EXPECT_EQ(ret, 0);
+}
+
+TEST_F(RuaInternalTest, rua_db_add_history_for_update) {
+ ASSERT_TRUE(table_ != nullptr);
+
+ free(table_[5]);
+ table_[5] = nullptr;
+ sqlite3* db = (sqlite3*) table_;
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_free_table(_));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_get_table(_, _, _, _, _, _)).
+ WillOnce(DoAll(SetArgPointee<2>(table_),
+ SetArgPointee<3>(1),
+ SetArgPointee<4>(0), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_int(_, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+ WillOnce(Return(true));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_flush_sync(_, _, _)).
+ WillOnce(Return(true));
+
+ char** get_table = nullptr;
+ int rows = 0;
+ int cols = 0;
+ struct rua_rec record;
+ int ret = rua_history_load_db(&get_table, &rows, &cols);
+ EXPECT_EQ(ret, 0);
+ EXPECT_TRUE(get_table != nullptr);
+
+ ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
+ EXPECT_EQ(ret, 0);
+
+ ret = rua_db_add_history(&record);
+ EXPECT_EQ(ret, 0);
+
+ ret = rua_history_unload_db(&get_table);
+ EXPECT_EQ(ret, 0);
+}
+
+TEST_F(RuaInternalTest, rua_usr_db_update_image) {
+ ASSERT_TRUE(table_ != nullptr);
+ sqlite3* db = (sqlite3*) table_;
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+ WillOnce(Return(true));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_flush_sync(_, _, _)).
+ WillOnce(Return(true));
+
+ int ret = rua_db_update_image("pkg_name", "comp_id", "inst_id", "image");
+ EXPECT_EQ(ret, 0);
+}
+
+TEST_F(RuaInternalTest, rua_db_delete_history) {
+ ASSERT_TRUE(table_ != nullptr);
+ sqlite3* db = (sqlite3*) table_;
+
+ bundle* b = bundle_create();
+ ASSERT_TRUE(b != nullptr);
+
+ if (b) {
+ bundle_add_str(b, AUL_K_RUA_PKGNAME, "pkgname");
+ }
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+ WillOnce(Return(true));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_flush_sync(_, _, _)).
+ WillOnce(Return(true));
+
+ int ret = rua_db_delete_history(b);
+ EXPECT_EQ(ret, 0);
+
+ if (b)
+ bundle_free(b);
+}
+
+TEST_F(RuaInternalTest, rua_db_delete_history_apppath) {
+ ASSERT_TRUE(table_ != nullptr);
+ sqlite3* db = (sqlite3*) table_;
+
+ bundle* b = bundle_create();
+ ASSERT_TRUE(b != nullptr);
+
+ if (b)
+ bundle_add_str(b, AUL_K_RUA_APPPATH, "apppath");
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+ WillOnce(Return(true));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_flush_sync(_, _, _)).
+ WillOnce(Return(true));
+
+ int ret = rua_db_delete_history(b);
+ EXPECT_EQ(ret, 0);
+
+ if (b)
+ bundle_free(b);
+}
+
+TEST_F(RuaInternalTest, rua_db_delete_history_clear) {
+ ASSERT_TRUE(table_ != nullptr);
+ sqlite3* db = (sqlite3*) table_;
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+ WillOnce(Return(true));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_flush_sync(_, _, _)).
+ WillOnce(Return(true));
+
+ int ret = rua_db_delete_history(nullptr);
+ EXPECT_EQ(ret, 0);
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2020 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 <gtest/gtest.h>
+
+#include <rua.h>
+#include <rua_stat.h>
+#include <rua_stat_internal.h>
+
+#include <string>
+#include <memory>
+
+#include "sqlite3_mock.h"
+#include "test_fixture.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::SetArgPointee;
+using ::testing::Return;
+
+static int __rua_stat_tag_iter(const char* stat_tag, void* user_data) {
+
+}
+
+class StatMocks : public ::testing::NiceMock<SqlMock> {};
+
+class RuaStatTest : public TestFixture {
+ public:
+ RuaStatTest() : TestFixture(std::make_unique<StatMocks>()) {}
+ virtual ~RuaStatTest() {}
+
+ virtual void SetUp() {
+ }
+
+ virtual void TearDown() {
+ }
+
+};
+
+TEST_F(RuaStatTest, rua_stat_update_for_uid_N) {
+ int ret = rua_stat_update_for_uid("caller", "tag", 5001);
+ EXPECT_NE(ret, 0);
+}
+
+TEST_F(RuaStatTest, rua_stat_get_stat_tags) {
+ char** table = (char** )calloc(10, sizeof(char*));
+ ASSERT_TRUE(table != nullptr);
+ sqlite3* db = (sqlite3*) table;
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillOnce(Return(0));
+
+ int ret = rua_stat_get_stat_tags("caller", __rua_stat_tag_iter, nullptr);
+ EXPECT_EQ(ret, 0);
+
+ free(table);
+}
+
+TEST_F(RuaStatTest, rua_stat_db_update) {
+ char** table = (char** )calloc(10, sizeof(char*));
+ ASSERT_TRUE(table != nullptr);
+ sqlite3* db = (sqlite3*) table;
+
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_open_v2(_, _, _, _)).
+ WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_close_v2(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_exec(_, _, _, _, _)).WillRepeatedly(Return(SQLITE_OK));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_int(_, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_double(_, _, _)).WillOnce(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_step(_)).WillRepeatedly(Return(SQLITE_DONE));
+ EXPECT_CALL(GetMock<SqlMock>(),
+ sqlite3_finalize(_)).WillRepeatedly(Return(0));
+
+ int ret = rua_stat_db_update("caller", "rua_stat_tag");
+ EXPECT_EQ(ret, SQLITE_DONE);
+
+ free(table);
+}
\ No newline at end of file