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() {
95 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
96 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
99 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(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::Return(GetDBHandles()));
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)));
110 fopen_mock_setup(true);
111 ASSERT_EQ(create_db_handler.Execute(), 0);
112 fopen_mock_setup(false);
115 virtual void TearDown() {
116 for (auto& handle : db_handles_)
117 sqlite3_close_v2(handle.first);
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);
125 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
130 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
131 db_handles_ = db_handles;
134 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
137 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
138 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
139 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
140 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
141 GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
142 pkg_set_db_handler.SetPkgInfo(ptr.get());
144 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
145 EXPECT_CALL(pkg_set_db_handler, GetConnection())
146 .WillOnce(testing::Return(GetDBHandles()));
147 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
150 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
151 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
152 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Update);
153 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
154 GetUpdatedTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
155 pkg_set_db_handler.SetPkgInfo(ptr.get());
157 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
158 EXPECT_CALL(pkg_set_db_handler, GetConnection())
159 .WillOnce(testing::Return(GetDBHandles()));
160 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
163 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
164 std::string pkgid = "pkgA";
165 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
167 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Delete);
168 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
169 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
170 pkg_set_db_handler.SetPkgInfo(ptr.get());
172 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
173 EXPECT_CALL(pkg_set_db_handler, GetConnection())
174 .WillOnce(testing::Return(GetDBHandles()));
175 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
177 pkgmgrinfo_pkginfo_filter_h filter;
179 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
180 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
181 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
182 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
183 pkgmgrinfo_pkginfo_filter_destroy);
184 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
185 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
186 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
187 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
189 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
190 pkg_get_db_handler.SetFilter(filter_ptr.get());
191 pkg_get_db_handler.SetLocale("test_lang");
193 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
194 EXPECT_CALL(pkg_get_db_handler, GetConnection())
195 .WillOnce(testing::Return(GetDBHandles()));
196 ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
199 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
200 std::string pkgid = "pkgA";
201 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
202 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
203 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
204 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
205 pkg_set_db_handler.SetPkgInfo(ptr.get());
207 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
208 EXPECT_CALL(pkg_set_db_handler, GetConnection())
209 .WillOnce(testing::Return(GetDBHandles()));
210 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
212 pkgmgrinfo_pkginfo_filter_h filter;
214 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
215 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
216 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
217 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
218 pkgmgrinfo_pkginfo_filter_destroy);
219 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
220 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
221 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
222 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
224 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
225 pkg_get_db_handler.SetFilter(filter_ptr.get());
226 pkg_get_db_handler.SetLocale("test_lang");
228 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
229 EXPECT_CALL(pkg_get_db_handler, GetConnection())
230 .WillOnce(testing::Return(GetDBHandles()));
231 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
233 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
234 ASSERT_EQ(lpkginfo_list.size(), 1);
236 auto test_pkginfo = GetTestPackage(pkgid);
237 std::vector<package_x*> rpkginfo_list;
238 rpkginfo_list.emplace_back(test_pkginfo);
240 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
243 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
244 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
245 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
246 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
247 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
248 pkg_set_db_handler.SetPkgInfo(ptr.get());
250 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
251 EXPECT_CALL(pkg_set_db_handler, GetConnection())
252 .WillOnce(testing::Return(GetDBHandles()));
253 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
255 pkgmgrinfo_appinfo_filter_h filter;
256 std::string appid = "test_app1";
258 ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
259 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
260 pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
261 reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
262 pkgmgrinfo_appinfo_filter_destroy);
263 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
264 PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
265 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
266 PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
268 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
269 appinfo_db_handler.SetFilter(filter_ptr.get());
271 EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
272 EXPECT_CALL(appinfo_db_handler, GetConnection())
273 .WillOnce(testing::Return(GetDBHandles()));
274 appinfo_db_handler.SetLocale("test_lang");
275 ASSERT_EQ(appinfo_db_handler.Execute(), 0);
277 auto lappinfo_list = appinfo_db_handler.GetAppHandle();
278 ASSERT_EQ(lappinfo_list.size(), 1);
280 auto test_appinfo = GetTestApplication(appid);
281 std::vector<application_x*> rappinfo_list;
282 rappinfo_list.emplace_back(test_appinfo);
284 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
287 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
288 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
289 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
290 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
291 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
292 pkg_set_db_handler.SetPkgInfo(ptr.get());
294 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
295 EXPECT_CALL(pkg_set_db_handler, GetConnection())
296 .WillOnce(testing::Return(GetDBHandles()));
297 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
299 DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
300 depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
301 EXPECT_CALL(depinfo_get_db_handler,
302 Connect()).WillOnce(testing::Return(true));
303 EXPECT_CALL(depinfo_get_db_handler, GetConnection())
304 .WillOnce(testing::Return(GetDBHandles()));
305 ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
307 auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
308 ASSERT_EQ(depinfo_from_db.size(), 1);
310 std::vector<dependency_x*> depinfo_list;
311 depinfo_list.emplace_back(GetTestDepInfo("test_package"));
313 EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));