Implement QueryDBHandler_PluginInfo unittest
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / test_parser_db_handlers.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <gmock/gmock.h>
18
19 #include <gtest/gtest.h>
20
21 #include <cstdio>
22
23 #include "appinfo_db_handler.hh"
24 #include "create_db_handler.hh"
25 #include "depinfo_db_handler.hh"
26 #include "db_type.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"
31
32 #include "mock/file_mock.h"
33 #include "mock/test_fixture.h"
34 #include "mock/system_info_mock.h"
35
36 #include "pkgmgr-info.h"
37 #include "pkgmgr_query_index.h"
38 #include "pkgmgrinfo_basic.h"
39
40 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
41
42 namespace psd = pkgmgr_server::database;
43
44 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
45  public:
46   CreateParserDBHandlerMock(uid_t uid, int pid)
47       : psd::CreateDBHandler(uid, pid) {}
48
49   MOCK_METHOD0(Connect, bool());
50   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
51 };
52
53 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
54  public:
55   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
56
57   MOCK_METHOD0(Connect, bool());
58   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
59 };
60
61 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
62  public:
63   PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
64
65   MOCK_METHOD0(Connect, bool());
66   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
67 };
68
69 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
70  public:
71   AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
72
73   MOCK_METHOD0(Connect, bool());
74   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
75 };
76
77 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
78  public:
79   DepInfoGetDBHandlerMock(uid_t uid, int pid) :
80       psd::DepInfoGetDBHandler(uid, pid) {}
81
82   MOCK_METHOD0(Connect, bool());
83   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
84 };
85
86 class QueryHandlerMock : public psd::QueryHandler {
87  public:
88   QueryHandlerMock(uid_t uid, int pid) :
89       psd::QueryHandler(uid, pid) {}
90
91   MOCK_METHOD0(Connect, bool());
92   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
93 };
94
95 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
96
97 class ParserDBHandlerTest : public TestFixture {
98  public:
99   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
100   virtual ~ParserDBHandlerTest() {}
101
102   virtual void SetUp() {
103     sqlite3 *db;
104     ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
105         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
106
107     SetDBHandles(
108         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
109     CreateParserDBHandlerMock create_db_handler(0, 0);
110
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);
122   }
123
124   virtual void TearDown() {
125     for (auto& handle : db_handles_)
126       sqlite3_close_v2(handle.first);
127
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);
132   }
133
134   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
135     return db_handles_;
136   }
137
138  private:
139   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
140     db_handles_ = db_handles;
141   }
142
143   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
144 };
145
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());
152
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);
157 }
158
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());
166
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);
171
172   pkgmgrinfo_pkginfo_filter_h filter;
173
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);
183
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");
187
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);
192
193   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
194   ASSERT_EQ(lpkginfo_list.size(), 1);
195
196   auto test_pkginfo = GetTestPackage(pkgid);
197   std::vector<package_x*> rpkginfo_list;
198   rpkginfo_list.emplace_back(test_pkginfo);
199
200   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
201 }
202
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());
209
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);
214
215   pkgmgrinfo_appinfo_filter_h filter;
216   std::string appid = "test_app1";
217
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);
227
228   AppInfoDBHandlerMock appinfo_db_handler(0, 0);
229   appinfo_db_handler.SetFilter(filter_ptr.get());
230
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);
236
237   auto lappinfo_list = appinfo_db_handler.GetAppHandle();
238   ASSERT_EQ(lappinfo_list.size(), 1);
239
240   auto test_appinfo = GetTestApplication(appid);
241   std::vector<application_x*> rappinfo_list;
242   rappinfo_list.emplace_back(test_appinfo);
243
244   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
245 }
246
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());
253
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);
258
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);
266
267   auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
268   ASSERT_EQ(depinfo_from_db.size(), 1);
269
270   std::vector<dependency_x*> depinfo_list;
271   depinfo_list.emplace_back(GetTestDepInfo("test_package"));
272
273   EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
274 }
275
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";
281
282   QueryHandlerMock set_query_handler(0, 0);
283   set_query_handler.SetQueryArgs(
284       std::vector<std::pair<int, std::vector<std::string>>>{
285           std::make_pair(
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);
292
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);
298
299   QueryHandlerMock get_query_handler(0, 0);
300   get_query_handler.SetQueryArgs(
301       std::vector<std::pair<int, std::vector<std::string>>>{
302           std::make_pair(
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);
308
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]);
318 }