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"
38 #define DB_VERSION_FILE "./pkg_db_version.txt"
40 namespace psd = pkgmgr_server::database;
41 namespace pc = pkgmgr_common;
43 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
45 CreateParserDBHandlerMock(uid_t uid, int pid)
46 : psd::CreateDBHandler(uid, pid) {}
48 MOCK_METHOD0(Connect, bool());
49 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
53 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
55 PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
57 MOCK_METHOD0(Connect, bool());
58 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
62 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
64 PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
66 MOCK_METHOD0(Connect, bool());
67 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
71 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
73 AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
75 MOCK_METHOD0(Connect, bool());
76 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
80 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
82 DepInfoGetDBHandlerMock(uid_t uid, int pid) :
83 psd::DepInfoGetDBHandler(uid, pid) {}
85 MOCK_METHOD0(Connect, bool());
86 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
90 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
92 class ParserDBHandlerTest : public TestFixture {
94 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
95 virtual ~ParserDBHandlerTest() {}
97 virtual void SetUp() {
98 tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
100 db_handles_.emplace_back(std::move(db), 0);
101 CreateParserDBHandlerMock create_db_handler(0, 0);
103 EXPECT_CALL(create_db_handler, Connect())
104 .Times(2).WillRepeatedly(testing::Return(true));
105 EXPECT_CALL(create_db_handler, GetConnection())
106 .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
107 EXPECT_CALL(GetMock<SystemInfoMock>(),
108 system_info_get_platform_int(testing::_, testing::_))
109 .WillRepeatedly(testing::DoAll(
110 testing::SetArgPointee<1>(120), testing::Return(0)));
112 pkgmgr_server::internal::SetEnableUnitTest(true);
113 ASSERT_EQ(create_db_handler.Execute(), 0);
114 pkgmgr_server::internal::SetEnableUnitTest(false);
117 virtual void TearDown() {
119 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
120 std::string journal_path(TEST_PARSER_DB);
121 journal_path += "-journal";
122 ASSERT_EQ(remove(journal_path.c_str()), 0);
123 ASSERT_EQ(remove(DB_VERSION_FILE), 0);
127 void MakeVersionFile() {
128 std::ofstream ofs(DB_VERSION_FILE);
133 std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
136 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
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("pkgA"), 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::ReturnRef(db_handles_));
146 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
149 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
150 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
151 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Update);
152 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
153 GetUpdatedTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
154 pkg_set_db_handler.SetPkgInfo(ptr.get());
156 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
157 EXPECT_CALL(pkg_set_db_handler, GetConnection())
158 .WillOnce(testing::ReturnRef(db_handles_));
159 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
162 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
163 std::string pkgid = "pkgA";
164 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
166 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Delete);
167 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
168 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
169 pkg_set_db_handler.SetPkgInfo(ptr.get());
171 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
172 EXPECT_CALL(pkg_set_db_handler, GetConnection())
173 .WillOnce(testing::ReturnRef(db_handles_));
174 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
176 pkgmgrinfo_pkginfo_filter_h filter;
178 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
179 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
180 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
181 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
182 pkgmgrinfo_pkginfo_filter_destroy);
183 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
184 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
185 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
186 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
188 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
189 pkg_get_db_handler.SetFilter(filter_ptr.get());
190 pkg_get_db_handler.SetLocale("test_lang");
192 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
193 EXPECT_CALL(pkg_get_db_handler, GetConnection())
194 .WillOnce(testing::ReturnRef(db_handles_));
195 ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
198 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
199 std::string pkgid = "pkgA";
200 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
201 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
202 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
203 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
204 pkg_set_db_handler.SetPkgInfo(ptr.get());
206 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
207 EXPECT_CALL(pkg_set_db_handler, GetConnection())
208 .WillOnce(testing::ReturnRef(db_handles_));
209 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
211 pkgmgrinfo_pkginfo_filter_h filter;
213 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
214 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
215 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
216 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
217 pkgmgrinfo_pkginfo_filter_destroy);
218 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
219 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
220 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
221 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
223 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
224 pkg_get_db_handler.SetFilter(filter_ptr.get());
225 pkg_get_db_handler.SetLocale("test_lang");
227 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
228 EXPECT_CALL(pkg_get_db_handler, GetConnection())
229 .WillOnce(testing::ReturnRef(db_handles_));
230 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
232 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
233 ASSERT_EQ(lpkginfo_list.size(), 1);
235 auto test_pkginfo = GetTestPackage(pkgid);
236 std::vector<std::shared_ptr<package_x>> rpkginfo_list;
237 rpkginfo_list.emplace_back(test_pkginfo, pkgmgrinfo_basic_free_package);
239 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
242 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
243 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
244 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
245 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
246 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
247 pkg_set_db_handler.SetPkgInfo(ptr.get());
249 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
250 EXPECT_CALL(pkg_set_db_handler, GetConnection())
251 .WillOnce(testing::ReturnRef(db_handles_));
252 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
254 pkgmgrinfo_appinfo_filter_h filter;
255 std::string appid = "test_app1";
257 ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
258 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
259 pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
260 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
261 pkgmgrinfo_appinfo_filter_destroy);
262 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
263 PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
264 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
265 PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
267 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
268 appinfo_db_handler.SetFilter(filter_ptr.get());
270 EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
271 EXPECT_CALL(appinfo_db_handler, GetConnection())
272 .WillOnce(testing::ReturnRef(db_handles_));
273 appinfo_db_handler.SetLocale("test_lang");
274 ASSERT_EQ(appinfo_db_handler.Execute(), 0);
276 auto lappinfo_list = appinfo_db_handler.DetachAppHandle();
277 ASSERT_EQ(lappinfo_list.size(), 1);
279 auto test_appinfo = GetTestApplication(appid);
280 std::vector<std::shared_ptr<application_x>> rappinfo_list;
281 rappinfo_list.emplace_back(test_appinfo, std::free);
283 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
286 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
287 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
288 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
289 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
290 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
291 pkg_set_db_handler.SetPkgInfo(ptr.get());
293 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
294 EXPECT_CALL(pkg_set_db_handler, GetConnection())
295 .WillOnce(testing::ReturnRef(db_handles_));
296 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
298 DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
299 depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
300 EXPECT_CALL(depinfo_get_db_handler,
301 Connect()).WillOnce(testing::Return(true));
302 EXPECT_CALL(depinfo_get_db_handler, GetConnection())
303 .WillOnce(testing::ReturnRef(db_handles_));
304 ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
306 auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
307 ASSERT_EQ(depinfo_from_db.size(), 1);
309 std::vector<dependency_x*> depinfo_list;
310 depinfo_list.emplace_back(GetTestDepInfo("test_package"));
312 EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));