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"
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"
31 #include "mock/file_mock.h"
32 #include "mock/test_fixture.h"
33 #include "mock/system_info_mock.h"
35 #include "pkgmgr-info.h"
36 #include "pkgmgr_query_index.h"
37 #include "pkgmgrinfo_basic.h"
39 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
41 namespace psd = pkgmgr_server::database;
42 namespace pc = pkgmgr_common;
44 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
46 CreateParserDBHandlerMock(uid_t uid, int pid)
47 : psd::CreateDBHandler(uid, pid) {}
49 MOCK_METHOD0(Connect, bool());
50 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
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, std::vector<std::pair<sqlite3*, uid_t>>());
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, std::vector<std::pair<sqlite3*, uid_t>>());
69 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
71 AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
73 MOCK_METHOD0(Connect, bool());
74 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
77 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
79 DepInfoGetDBHandlerMock(uid_t uid, int pid) :
80 psd::DepInfoGetDBHandler(uid, pid) {}
82 MOCK_METHOD0(Connect, bool());
83 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
86 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
88 class ParserDBHandlerTest : public TestFixture {
90 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
91 virtual ~ParserDBHandlerTest() {}
93 virtual void SetUp() {
96 int ret = sqlite3_open_v2(TEST_PARSER_DB, &db,
97 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
101 ASSERT_EQ(ret, SQLITE_OK);
104 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
105 CreateParserDBHandlerMock create_db_handler(0, 0);
107 EXPECT_CALL(create_db_handler, Connect())
108 .Times(2).WillRepeatedly(testing::Return(true));
109 EXPECT_CALL(create_db_handler, GetConnection())
110 .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
111 EXPECT_CALL(GetMock<SystemInfoMock>(),
112 system_info_get_platform_int(testing::_, testing::_))
113 .WillRepeatedly(testing::DoAll(
114 testing::SetArgPointee<1>(120), testing::Return(0)));
115 fopen_mock_setup(true);
116 ASSERT_EQ(create_db_handler.Execute(), 0);
117 fopen_mock_setup(false);
120 virtual void TearDown() {
121 for (auto& handle : db_handles_)
122 sqlite3_close_v2(handle.first);
124 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
125 std::string journal_path(TEST_PARSER_DB);
126 journal_path += "-journal";
127 ASSERT_EQ(remove(journal_path.c_str()), 0);
130 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
135 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
136 db_handles_ = db_handles;
139 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
142 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
143 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
144 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
145 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
146 GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
147 pkg_set_db_handler.SetPkgInfo(ptr.get());
149 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
150 EXPECT_CALL(pkg_set_db_handler, GetConnection())
151 .WillOnce(testing::Return(GetDBHandles()));
152 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
155 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
156 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
157 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Update);
158 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
159 GetUpdatedTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
160 pkg_set_db_handler.SetPkgInfo(ptr.get());
162 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
163 EXPECT_CALL(pkg_set_db_handler, GetConnection())
164 .WillOnce(testing::Return(GetDBHandles()));
165 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
168 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
169 std::string pkgid = "pkgA";
170 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
172 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Delete);
173 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
174 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
175 pkg_set_db_handler.SetPkgInfo(ptr.get());
177 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
178 EXPECT_CALL(pkg_set_db_handler, GetConnection())
179 .WillOnce(testing::Return(GetDBHandles()));
180 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
182 pkgmgrinfo_pkginfo_filter_h filter;
184 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
185 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
186 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
187 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
188 pkgmgrinfo_pkginfo_filter_destroy);
189 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
190 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
191 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
192 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
194 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
195 pkg_get_db_handler.SetFilter(filter_ptr.get());
196 pkg_get_db_handler.SetLocale("test_lang");
198 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
199 EXPECT_CALL(pkg_get_db_handler, GetConnection())
200 .WillOnce(testing::Return(GetDBHandles()));
201 ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
204 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
205 std::string pkgid = "pkgA";
206 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
207 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
208 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
209 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
210 pkg_set_db_handler.SetPkgInfo(ptr.get());
212 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
213 EXPECT_CALL(pkg_set_db_handler, GetConnection())
214 .WillOnce(testing::Return(GetDBHandles()));
215 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
217 pkgmgrinfo_pkginfo_filter_h filter;
219 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
220 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
221 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
222 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
223 pkgmgrinfo_pkginfo_filter_destroy);
224 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
225 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
226 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
227 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
229 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
230 pkg_get_db_handler.SetFilter(filter_ptr.get());
231 pkg_get_db_handler.SetLocale("test_lang");
233 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
234 EXPECT_CALL(pkg_get_db_handler, GetConnection())
235 .WillOnce(testing::Return(GetDBHandles()));
236 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
238 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
239 ASSERT_EQ(lpkginfo_list.size(), 1);
241 auto test_pkginfo = GetTestPackage(pkgid);
242 std::vector<std::shared_ptr<package_x>> rpkginfo_list;
243 rpkginfo_list.emplace_back(test_pkginfo);
245 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
248 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
249 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
250 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
251 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
252 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
253 pkg_set_db_handler.SetPkgInfo(ptr.get());
255 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
256 EXPECT_CALL(pkg_set_db_handler, GetConnection())
257 .WillOnce(testing::Return(GetDBHandles()));
258 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
260 pkgmgrinfo_appinfo_filter_h filter;
261 std::string appid = "test_app1";
263 ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
264 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
265 pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
266 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
267 pkgmgrinfo_appinfo_filter_destroy);
268 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
269 PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
270 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
271 PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
273 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
274 appinfo_db_handler.SetFilter(filter_ptr.get());
276 EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
277 EXPECT_CALL(appinfo_db_handler, GetConnection())
278 .WillOnce(testing::Return(GetDBHandles()));
279 appinfo_db_handler.SetLocale("test_lang");
280 ASSERT_EQ(appinfo_db_handler.Execute(), 0);
282 auto lappinfo_list = appinfo_db_handler.DetachAppHandle();
283 ASSERT_EQ(lappinfo_list.size(), 1);
285 auto test_appinfo = GetTestApplication(appid);
286 std::vector<std::shared_ptr<application_x>> rappinfo_list;
287 rappinfo_list.emplace_back(test_appinfo);
289 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
292 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
293 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
294 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
295 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
296 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
297 pkg_set_db_handler.SetPkgInfo(ptr.get());
299 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
300 EXPECT_CALL(pkg_set_db_handler, GetConnection())
301 .WillOnce(testing::Return(GetDBHandles()));
302 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
304 DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
305 depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
306 EXPECT_CALL(depinfo_get_db_handler,
307 Connect()).WillOnce(testing::Return(true));
308 EXPECT_CALL(depinfo_get_db_handler, GetConnection())
309 .WillOnce(testing::Return(GetDBHandles()));
310 ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
312 auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
313 ASSERT_EQ(depinfo_from_db.size(), 1);
315 std::vector<dependency_x*> depinfo_list;
316 depinfo_list.emplace_back(GetTestDepInfo("test_package"));
318 EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));