2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
23 #include "appinfo_db_handler.hh"
24 #include "create_db_handler.hh"
26 #include "parcel_utils.hh"
27 #include "pkg_get_db_handler.hh"
28 #include "pkg_set_db_handler.hh"
29 #include "mock/file_mock.h"
30 #include "mock/test_fixture.h"
31 #include "mock/system_info_mock.h"
33 #include "pkgmgr-info.h"
34 #include "pkgmgrinfo_basic.h"
36 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
38 class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
40 CreateDBHandlerMock(uid_t uid, int pid)
41 : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
43 MOCK_METHOD0(Connect, bool());
44 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
47 class PkgSetDBHandlerMock : public pkgmgr_server::database::PkgSetDBHandler {
49 PkgSetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgSetDBHandler(uid, pid) {}
51 MOCK_METHOD0(Connect, bool());
52 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
55 class PkgGetDBHandlerMock : public pkgmgr_server::database::PkgGetDBHandler {
57 PkgGetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgGetDBHandler(uid, pid) {}
59 MOCK_METHOD0(Connect, bool());
60 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
63 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
65 class AppInfoDBHandlerMock : public pkgmgr_server::database::AppInfoDBHandler {
67 AppInfoDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::AppInfoDBHandler(uid, pid) {}
69 MOCK_METHOD0(Connect, bool());
70 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
73 class DBHandlerTest : public TestFixture {
75 DBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
76 virtual ~DBHandlerTest() {}
78 virtual void SetUp() {
80 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
81 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
84 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
85 CreateDBHandlerMock create_db_handler(0, 0);
87 EXPECT_CALL(create_db_handler, Connect())
88 .Times(2).WillRepeatedly(testing::Return(true));
89 EXPECT_CALL(create_db_handler, GetConnection())
90 .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
91 EXPECT_CALL(GetMock<SystemInfoMock>(),
92 system_info_get_platform_int(testing::_, testing::_))
93 .WillRepeatedly(testing::DoAll(
94 testing::SetArgPointee<1>(120), testing::Return(0)));
95 fopen_mock_setup(true);
96 ASSERT_EQ(create_db_handler.Execute(), 0);
97 fopen_mock_setup(false);
100 virtual void TearDown() {
101 for (auto& handle : db_handles_)
102 sqlite3_close_v2(handle.first);
104 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
105 std::string journal_path(TEST_PARSER_DB);
106 journal_path += "-journal";
107 ASSERT_EQ(remove(journal_path.c_str()), 0);
110 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
115 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
116 db_handles_ = db_handles;
119 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
122 TEST_F(DBHandlerTest, PkgSetDBHandlerTest) {
123 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
124 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
125 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
126 GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
127 pkg_set_db_handler.SetPkgInfo(ptr.get());
129 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
130 EXPECT_CALL(pkg_set_db_handler, GetConnection())
131 .WillOnce(testing::Return(GetDBHandles()));
132 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
135 TEST_F(DBHandlerTest, PkgGetDBHandlerTest) {
136 std::string pkgid = "pkgA";
137 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
138 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
139 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
140 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
141 pkg_set_db_handler.SetPkgInfo(ptr.get());
143 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
144 EXPECT_CALL(pkg_set_db_handler, GetConnection())
145 .WillOnce(testing::Return(GetDBHandles()));
146 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
148 pkgmgrinfo_pkginfo_filter_h filter;
150 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
151 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
152 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
153 reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
154 pkgmgrinfo_pkginfo_filter_destroy);
155 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
156 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
157 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
158 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
160 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
161 pkg_get_db_handler.SetFilter(filter_ptr.get());
162 pkg_get_db_handler.SetLocale("test_lang");
164 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
165 EXPECT_CALL(pkg_get_db_handler, GetConnection())
166 .WillOnce(testing::Return(GetDBHandles()));
167 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
169 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
170 ASSERT_EQ(lpkginfo_list.size(), 1);
172 auto test_pkginfo = GetTestPackage(pkgid);
173 std::vector<package_x*> rpkginfo_list;
174 rpkginfo_list.emplace_back(test_pkginfo);
176 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
179 TEST_F(DBHandlerTest, AppInfoDBHandlerTest) {
180 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
181 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
182 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
183 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
184 pkg_set_db_handler.SetPkgInfo(ptr.get());
186 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
187 EXPECT_CALL(pkg_set_db_handler, GetConnection())
188 .WillOnce(testing::Return(GetDBHandles()));
189 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
191 pkgmgrinfo_appinfo_filter_h filter;
192 std::string appid = "test_app1";
194 ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
195 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
196 pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
197 reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
198 pkgmgrinfo_appinfo_filter_destroy);
199 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
200 PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
201 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
202 PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
204 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
205 appinfo_db_handler.SetFilter(filter_ptr.get());
207 EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
208 EXPECT_CALL(appinfo_db_handler, GetConnection())
209 .WillOnce(testing::Return(GetDBHandles()));
210 appinfo_db_handler.SetLocale("test_lang");
211 ASSERT_EQ(appinfo_db_handler.Execute(), 0);
213 auto lappinfo_list = appinfo_db_handler.GetAppHandle();
214 ASSERT_EQ(lappinfo_list.size(), 1);
216 auto test_appinfo = GetTestApplication(appid);
217 std::vector<application_x*> rappinfo_list;
218 rappinfo_list.emplace_back(test_appinfo);
220 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);