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>
18 #include <gtest/gtest.h>
23 #include "appinfo_db_handler.hh"
24 #include "create_db_handler.hh"
25 #include "depinfo_db_handler.hh"
27 #include "parcel_utils.hh"
28 #include "pkg_get_db_handler.hh"
29 #include "pkg_set_db_handler.hh"
30 #include "mock/test_fixture.h"
31 #include "mock/system_info_mock.h"
32 #include "pkgmgr-info.h"
33 #include "pkgmgr_query_index.h"
34 #include "pkgmgrinfo_basic.h"
35 #include "server/pkgmgrinfo_internal.h"
37 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
39 namespace psd = pkgmgr_server::database;
40 namespace pc = pkgmgr_common;
42 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
44 CreateParserDBHandlerMock(uid_t uid, int pid)
45 : psd::CreateDBHandler(uid, pid) {}
47 MOCK_METHOD0(Connect, bool());
48 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
52 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
54 PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
56 MOCK_METHOD0(Connect, bool());
57 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
61 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
63 PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
65 MOCK_METHOD0(Connect, bool());
66 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
70 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
72 AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
74 MOCK_METHOD0(Connect, bool());
75 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
79 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
81 DepInfoGetDBHandlerMock(uid_t uid, int pid) :
82 psd::DepInfoGetDBHandler(uid, pid) {}
84 MOCK_METHOD0(Connect, bool());
85 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
89 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
91 class ParserDBHandlerTest : public TestFixture {
93 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
94 virtual ~ParserDBHandlerTest() {}
96 virtual void SetUp() {
97 tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
99 db_handles_.emplace_back(std::move(db), 0);
100 CreateParserDBHandlerMock create_db_handler(0, 0);
102 EXPECT_CALL(create_db_handler, Connect())
103 .Times(2).WillRepeatedly(testing::Return(true));
104 EXPECT_CALL(create_db_handler, GetConnection())
105 .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
106 EXPECT_CALL(GetMock<SystemInfoMock>(),
107 system_info_get_platform_int(testing::_, testing::_))
108 .WillRepeatedly(testing::DoAll(
109 testing::SetArgPointee<1>(120), testing::Return(0)));
111 pkgmgr_server::internal::SetEnableUnitTest(true);
112 ASSERT_EQ(create_db_handler.Execute(), 0);
113 pkgmgr_server::internal::SetEnableUnitTest(false);
116 virtual void TearDown() {
118 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
119 std::string journal_path(TEST_PARSER_DB);
120 journal_path += "-journal";
121 ASSERT_EQ(remove(journal_path.c_str()), 0);
125 void MakeVersionFile() {
126 std::remove("./pkg_db_version.txt");
127 std::ofstream ofs("./pkg_db_version.txt");
132 std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
135 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
136 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
137 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
138 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
139 GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
140 pkg_set_db_handler.SetPkgInfo(ptr.get());
142 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
143 EXPECT_CALL(pkg_set_db_handler, GetConnection())
144 .WillOnce(testing::ReturnRef(db_handles_));
145 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
148 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
149 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
150 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Update);
151 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
152 GetUpdatedTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
153 pkg_set_db_handler.SetPkgInfo(ptr.get());
155 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
156 EXPECT_CALL(pkg_set_db_handler, GetConnection())
157 .WillOnce(testing::ReturnRef(db_handles_));
158 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
161 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
162 std::string pkgid = "pkgA";
163 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
165 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Delete);
166 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
167 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
168 pkg_set_db_handler.SetPkgInfo(ptr.get());
170 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
171 EXPECT_CALL(pkg_set_db_handler, GetConnection())
172 .WillOnce(testing::ReturnRef(db_handles_));
173 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
175 pkgmgrinfo_pkginfo_filter_h filter;
177 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
178 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
179 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
180 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
181 pkgmgrinfo_pkginfo_filter_destroy);
182 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
183 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
184 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
185 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
187 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
188 pkg_get_db_handler.SetFilter(filter_ptr.get());
189 pkg_get_db_handler.SetLocale("test_lang");
191 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
192 EXPECT_CALL(pkg_get_db_handler, GetConnection())
193 .WillOnce(testing::ReturnRef(db_handles_));
194 ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
197 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
198 std::string pkgid = "pkgA";
199 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
200 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
201 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
202 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
203 pkg_set_db_handler.SetPkgInfo(ptr.get());
205 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
206 EXPECT_CALL(pkg_set_db_handler, GetConnection())
207 .WillOnce(testing::ReturnRef(db_handles_));
208 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
210 pkgmgrinfo_pkginfo_filter_h filter;
212 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
213 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
214 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
215 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
216 pkgmgrinfo_pkginfo_filter_destroy);
217 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
218 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
219 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
220 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
222 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
223 pkg_get_db_handler.SetFilter(filter_ptr.get());
224 pkg_get_db_handler.SetLocale("test_lang");
226 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
227 EXPECT_CALL(pkg_get_db_handler, GetConnection())
228 .WillOnce(testing::ReturnRef(db_handles_));
229 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
231 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
232 ASSERT_EQ(lpkginfo_list.size(), 1);
234 auto test_pkginfo = GetTestPackage(pkgid);
235 std::vector<std::shared_ptr<package_x>> rpkginfo_list;
236 rpkginfo_list.emplace_back(test_pkginfo, pkgmgrinfo_basic_free_package);
238 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
241 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
242 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
243 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
244 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
245 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
246 pkg_set_db_handler.SetPkgInfo(ptr.get());
248 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
249 EXPECT_CALL(pkg_set_db_handler, GetConnection())
250 .WillOnce(testing::ReturnRef(db_handles_));
251 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
253 pkgmgrinfo_appinfo_filter_h filter;
254 std::string appid = "test_app1";
256 ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
257 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
258 pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
259 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
260 pkgmgrinfo_appinfo_filter_destroy);
261 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
262 PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
263 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
264 PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
266 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
267 appinfo_db_handler.SetFilter(filter_ptr.get());
269 EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
270 EXPECT_CALL(appinfo_db_handler, GetConnection())
271 .WillOnce(testing::ReturnRef(db_handles_));
272 appinfo_db_handler.SetLocale("test_lang");
273 ASSERT_EQ(appinfo_db_handler.Execute(), 0);
275 auto lappinfo_list = appinfo_db_handler.DetachAppHandle();
276 ASSERT_EQ(lappinfo_list.size(), 1);
278 auto test_appinfo = GetTestApplication(appid);
279 std::vector<std::shared_ptr<application_x>> rappinfo_list;
280 rappinfo_list.emplace_back(test_appinfo, std::free);
282 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
285 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
286 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
287 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
288 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
289 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
290 pkg_set_db_handler.SetPkgInfo(ptr.get());
292 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
293 EXPECT_CALL(pkg_set_db_handler, GetConnection())
294 .WillOnce(testing::ReturnRef(db_handles_));
295 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
297 DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
298 depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
299 EXPECT_CALL(depinfo_get_db_handler,
300 Connect()).WillOnce(testing::Return(true));
301 EXPECT_CALL(depinfo_get_db_handler, GetConnection())
302 .WillOnce(testing::ReturnRef(db_handles_));
303 ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
305 auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
306 ASSERT_EQ(depinfo_from_db.size(), 1);
308 std::vector<dependency_x*> depinfo_list;
309 depinfo_list.emplace_back(GetTestDepInfo("test_package"));
311 EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));