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"
30 #include "query_handler.hh"
32 #include "mock/file_mock.h"
33 #include "mock/test_fixture.h"
34 #include "mock/system_info_mock.h"
36 #include "pkgmgr-info.h"
37 #include "pkgmgr_query_index.h"
38 #include "pkgmgrinfo_basic.h"
40 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
42 namespace psd = pkgmgr_server::database;
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 QueryHandlerMock : public psd::QueryHandler {
88 QueryHandlerMock(uid_t uid, int pid) :
89 psd::QueryHandler(uid, pid) {}
91 MOCK_METHOD0(Connect, bool());
92 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
95 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
97 class ParserDBHandlerTest : public TestFixture {
99 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
100 virtual ~ParserDBHandlerTest() {}
102 virtual void SetUp() {
104 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
105 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
108 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
109 CreateParserDBHandlerMock create_db_handler(0, 0);
111 EXPECT_CALL(create_db_handler, Connect())
112 .Times(2).WillRepeatedly(testing::Return(true));
113 EXPECT_CALL(create_db_handler, GetConnection())
114 .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
115 EXPECT_CALL(GetMock<SystemInfoMock>(),
116 system_info_get_platform_int(testing::_, testing::_))
117 .WillRepeatedly(testing::DoAll(
118 testing::SetArgPointee<1>(120), testing::Return(0)));
119 fopen_mock_setup(true);
120 ASSERT_EQ(create_db_handler.Execute(), 0);
121 fopen_mock_setup(false);
124 virtual void TearDown() {
125 for (auto& handle : db_handles_)
126 sqlite3_close_v2(handle.first);
128 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
129 std::string journal_path(TEST_PARSER_DB);
130 journal_path += "-journal";
131 ASSERT_EQ(remove(journal_path.c_str()), 0);
134 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
139 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
140 db_handles_ = db_handles;
143 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
146 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest) {
147 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
148 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
149 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
150 GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
151 pkg_set_db_handler.SetPkgInfo(ptr.get());
153 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
154 EXPECT_CALL(pkg_set_db_handler, GetConnection())
155 .WillOnce(testing::Return(GetDBHandles()));
156 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
159 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
160 std::string pkgid = "pkgA";
161 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
162 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
163 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
164 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
165 pkg_set_db_handler.SetPkgInfo(ptr.get());
167 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
168 EXPECT_CALL(pkg_set_db_handler, GetConnection())
169 .WillOnce(testing::Return(GetDBHandles()));
170 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
172 pkgmgrinfo_pkginfo_filter_h filter;
174 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
175 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
176 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
177 reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
178 pkgmgrinfo_pkginfo_filter_destroy);
179 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
180 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
181 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
182 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
184 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
185 pkg_get_db_handler.SetFilter(filter_ptr.get());
186 pkg_get_db_handler.SetLocale("test_lang");
188 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
189 EXPECT_CALL(pkg_get_db_handler, GetConnection())
190 .WillOnce(testing::Return(GetDBHandles()));
191 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
193 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
194 ASSERT_EQ(lpkginfo_list.size(), 1);
196 auto test_pkginfo = GetTestPackage(pkgid);
197 std::vector<package_x*> rpkginfo_list;
198 rpkginfo_list.emplace_back(test_pkginfo);
200 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
203 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
204 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
205 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
206 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
207 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
208 pkg_set_db_handler.SetPkgInfo(ptr.get());
210 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
211 EXPECT_CALL(pkg_set_db_handler, GetConnection())
212 .WillOnce(testing::Return(GetDBHandles()));
213 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
215 pkgmgrinfo_appinfo_filter_h filter;
216 std::string appid = "test_app1";
218 ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
219 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
220 pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
221 reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
222 pkgmgrinfo_appinfo_filter_destroy);
223 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
224 PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
225 ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
226 PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
228 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
229 appinfo_db_handler.SetFilter(filter_ptr.get());
231 EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
232 EXPECT_CALL(appinfo_db_handler, GetConnection())
233 .WillOnce(testing::Return(GetDBHandles()));
234 appinfo_db_handler.SetLocale("test_lang");
235 ASSERT_EQ(appinfo_db_handler.Execute(), 0);
237 auto lappinfo_list = appinfo_db_handler.GetAppHandle();
238 ASSERT_EQ(lappinfo_list.size(), 1);
240 auto test_appinfo = GetTestApplication(appid);
241 std::vector<application_x*> rappinfo_list;
242 rappinfo_list.emplace_back(test_appinfo);
244 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
247 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
248 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
249 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
250 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
251 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
252 pkg_set_db_handler.SetPkgInfo(ptr.get());
254 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
255 EXPECT_CALL(pkg_set_db_handler, GetConnection())
256 .WillOnce(testing::Return(GetDBHandles()));
257 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
259 DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
260 depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
261 EXPECT_CALL(depinfo_get_db_handler,
262 Connect()).WillOnce(testing::Return(true));
263 EXPECT_CALL(depinfo_get_db_handler, GetConnection())
264 .WillOnce(testing::Return(GetDBHandles()));
265 ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
267 auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
268 ASSERT_EQ(depinfo_from_db.size(), 1);
270 std::vector<dependency_x*> depinfo_list;
271 depinfo_list.emplace_back(GetTestDepInfo("test_package"));
273 EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
276 TEST_F(ParserDBHandlerTest, QueryDBHandler_PluginInfoTest) {
277 const std::string pkgid = "test_pkgid";
278 const std::string appid = "test_appid";
279 const std::string plugin_type = "test_plugin_type";
280 const std::string plugin_name = "test_plugin_name";
282 QueryHandlerMock set_query_handler(0, 0);
283 set_query_handler.SetQueryArgs(
284 std::vector<std::pair<int, std::vector<std::string>>>{
286 QUERY_INDEX_INSERT_PACKAGE_PLUGIN_EXECUTION_INFO,
287 std::vector<std::string>{
288 pkgid, appid, plugin_type, plugin_name})});
289 set_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
290 set_query_handler.SetOpType(
291 pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
293 EXPECT_CALL(set_query_handler,
294 Connect()).WillOnce(testing::Return(true));
295 EXPECT_CALL(set_query_handler, GetConnection())
296 .WillOnce(testing::Return(GetDBHandles()));
297 ASSERT_EQ(set_query_handler.Execute(), 0);
299 QueryHandlerMock get_query_handler(0, 0);
300 get_query_handler.SetQueryArgs(
301 std::vector<std::pair<int, std::vector<std::string>>>{
303 QUERY_INDEX_PLUGININFO_GET_APPIDS,
304 std::vector<std::string>{pkgid, plugin_type, plugin_name})});
305 get_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
306 get_query_handler.SetOpType(
307 pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
309 EXPECT_CALL(get_query_handler,
310 Connect()).WillOnce(testing::Return(true));
311 EXPECT_CALL(get_query_handler, GetConnection())
312 .WillOnce(testing::Return(GetDBHandles()));
313 ASSERT_EQ(get_query_handler.Execute(), 0);
314 auto result = get_query_handler.GetResult();
315 ASSERT_EQ(result.size(), 1);
316 ASSERT_EQ(result[0].size(), 1);
317 ASSERT_EQ(appid, result[0][0]);