Implement QueryDBHandler_GetLocaledLabel 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 namespace pc = pkgmgr_common;
44
45 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
46  public:
47   CreateParserDBHandlerMock(uid_t uid, int pid)
48       : psd::CreateDBHandler(uid, pid) {}
49
50   MOCK_METHOD0(Connect, bool());
51   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
52 };
53
54 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
55  public:
56   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
57
58   MOCK_METHOD0(Connect, bool());
59   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
60 };
61
62 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
63  public:
64   PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
65
66   MOCK_METHOD0(Connect, bool());
67   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
68 };
69
70 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
71  public:
72   AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
73
74   MOCK_METHOD0(Connect, bool());
75   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
76 };
77
78 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
79  public:
80   DepInfoGetDBHandlerMock(uid_t uid, int pid) :
81       psd::DepInfoGetDBHandler(uid, pid) {}
82
83   MOCK_METHOD0(Connect, bool());
84   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
85 };
86
87 class QueryHandlerMock : public psd::QueryHandler {
88  public:
89   QueryHandlerMock(uid_t uid, int pid) :
90       psd::QueryHandler(uid, pid) {}
91
92   MOCK_METHOD0(Connect, bool());
93   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
94 };
95
96 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
97
98 class ParserDBHandlerTest : public TestFixture {
99  public:
100   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
101   virtual ~ParserDBHandlerTest() {}
102
103   virtual void SetUp() {
104     sqlite3 *db;
105     ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
106         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
107
108     SetDBHandles(
109         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
110     CreateParserDBHandlerMock create_db_handler(0, 0);
111
112     EXPECT_CALL(create_db_handler, Connect())
113         .Times(2).WillRepeatedly(testing::Return(true));
114     EXPECT_CALL(create_db_handler, GetConnection())
115         .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
116     EXPECT_CALL(GetMock<SystemInfoMock>(),
117       system_info_get_platform_int(testing::_, testing::_))
118           .WillRepeatedly(testing::DoAll(
119                   testing::SetArgPointee<1>(120), testing::Return(0)));
120     fopen_mock_setup(true);
121     ASSERT_EQ(create_db_handler.Execute(), 0);
122     fopen_mock_setup(false);
123   }
124
125   virtual void TearDown() {
126     for (auto& handle : db_handles_)
127       sqlite3_close_v2(handle.first);
128
129     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
130     std::string journal_path(TEST_PARSER_DB);
131     journal_path += "-journal";
132     ASSERT_EQ(remove(journal_path.c_str()), 0);
133   }
134
135   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
136     return db_handles_;
137   }
138
139  private:
140   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
141     db_handles_ = db_handles;
142   }
143
144   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
145 };
146
147 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest) {
148   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
149   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
150   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
151       GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
152   pkg_set_db_handler.SetPkgInfo(ptr.get());
153
154   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
155   EXPECT_CALL(pkg_set_db_handler, GetConnection())
156       .WillOnce(testing::Return(GetDBHandles()));
157   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
158 }
159
160 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
161   std::string pkgid = "pkgA";
162   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
163   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
164   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
165       GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
166   pkg_set_db_handler.SetPkgInfo(ptr.get());
167
168   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
169   EXPECT_CALL(pkg_set_db_handler, GetConnection())
170       .WillOnce(testing::Return(GetDBHandles()));
171   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
172
173   pkgmgrinfo_pkginfo_filter_h filter;
174
175   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
176   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
177       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
178           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
179               pkgmgrinfo_pkginfo_filter_destroy);
180   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
181       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
182   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
183       PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
184
185   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
186   pkg_get_db_handler.SetFilter(filter_ptr.get());
187   pkg_get_db_handler.SetLocale("test_lang");
188
189   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
190   EXPECT_CALL(pkg_get_db_handler, GetConnection())
191       .WillOnce(testing::Return(GetDBHandles()));
192   ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
193
194   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
195   ASSERT_EQ(lpkginfo_list.size(), 1);
196
197   auto test_pkginfo = GetTestPackage(pkgid);
198   std::vector<package_x*> rpkginfo_list;
199   rpkginfo_list.emplace_back(test_pkginfo);
200
201   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
202 }
203
204 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
205   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
206   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
207   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
208       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
209   pkg_set_db_handler.SetPkgInfo(ptr.get());
210
211   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
212   EXPECT_CALL(pkg_set_db_handler, GetConnection())
213       .WillOnce(testing::Return(GetDBHandles()));
214   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
215
216   pkgmgrinfo_appinfo_filter_h filter;
217   std::string appid = "test_app1";
218
219   ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
220   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
221       pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
222           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
223               pkgmgrinfo_appinfo_filter_destroy);
224   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
225       PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
226   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
227       PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
228
229   AppInfoDBHandlerMock appinfo_db_handler(0, 0);
230   appinfo_db_handler.SetFilter(filter_ptr.get());
231
232   EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
233   EXPECT_CALL(appinfo_db_handler, GetConnection())
234       .WillOnce(testing::Return(GetDBHandles()));
235   appinfo_db_handler.SetLocale("test_lang");
236   ASSERT_EQ(appinfo_db_handler.Execute(), 0);
237
238   auto lappinfo_list = appinfo_db_handler.GetAppHandle();
239   ASSERT_EQ(lappinfo_list.size(), 1);
240
241   auto test_appinfo = GetTestApplication(appid);
242   std::vector<application_x*> rappinfo_list;
243   rappinfo_list.emplace_back(test_appinfo);
244
245   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
246 }
247
248 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
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());
254
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);
259
260   DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
261   depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
262   EXPECT_CALL(depinfo_get_db_handler,
263       Connect()).WillOnce(testing::Return(true));
264   EXPECT_CALL(depinfo_get_db_handler, GetConnection())
265       .WillOnce(testing::Return(GetDBHandles()));
266   ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
267
268   auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
269   ASSERT_EQ(depinfo_from_db.size(), 1);
270
271   std::vector<dependency_x*> depinfo_list;
272   depinfo_list.emplace_back(GetTestDepInfo("test_package"));
273
274   EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
275 }
276
277 TEST_F(ParserDBHandlerTest, QueryDBHandler_PluginInfoTest) {
278   const std::string pkgid = "test_pkgid";
279   const std::string appid = "test_appid";
280   const std::string plugin_type = "test_plugin_type";
281   const std::string plugin_name = "test_plugin_name";
282
283   QueryHandlerMock set_query_handler(0, 0);
284   set_query_handler.SetQueryArgs(
285       std::vector<std::pair<int, std::vector<std::string>>>{
286           std::make_pair(
287               QUERY_INDEX_INSERT_PACKAGE_PLUGIN_EXECUTION_INFO,
288               std::vector<std::string>{
289                   pkgid, appid, plugin_type, plugin_name})});
290   set_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
291   set_query_handler.SetOpType(
292       pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
293
294   EXPECT_CALL(set_query_handler,
295       Connect()).WillOnce(testing::Return(true));
296   EXPECT_CALL(set_query_handler, GetConnection())
297       .WillOnce(testing::Return(GetDBHandles()));
298   ASSERT_EQ(set_query_handler.Execute(), 0);
299
300   QueryHandlerMock get_query_handler(0, 0);
301   get_query_handler.SetQueryArgs(
302       std::vector<std::pair<int, std::vector<std::string>>>{
303           std::make_pair(
304               QUERY_INDEX_PLUGININFO_GET_APPIDS,
305               std::vector<std::string>{pkgid, plugin_type, plugin_name})});
306   get_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
307   get_query_handler.SetOpType(
308       pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
309
310   EXPECT_CALL(get_query_handler,
311       Connect()).WillOnce(testing::Return(true));
312   EXPECT_CALL(get_query_handler, GetConnection())
313       .WillOnce(testing::Return(GetDBHandles()));
314   ASSERT_EQ(get_query_handler.Execute(), 0);
315   auto result = get_query_handler.GetResult();
316   ASSERT_EQ(result.size(), 1);
317   ASSERT_EQ(result[0].size(), 1);
318   ASSERT_EQ(appid, result[0][0]);
319 }
320
321 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetLocaledLabelTest) {
322   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
323   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
324   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
325       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
326   pkg_set_db_handler.SetPkgInfo(ptr.get());
327
328   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
329   EXPECT_CALL(pkg_set_db_handler, GetConnection())
330       .WillOnce(testing::Return(GetDBHandles()));
331   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
332
333   std::vector<std::pair<int, std::vector<std::string>>> query_args;
334   std::vector<std::vector<std::string>> expected_label;
335   for (GList* app_list = ptr.get()->application; app_list;
336       app_list = app_list->next) {
337     application_x* app = reinterpret_cast<application_x*>(app_list->data);
338     ASSERT_NE(app, nullptr);
339     ASSERT_NE(app->appid, nullptr);
340     ASSERT_NE(app->label, nullptr);
341
342     for (GList* label_list = app->label; label_list;
343         label_list = label_list->next) {
344       label_x* label = reinterpret_cast<label_x*>(label_list->data);
345       ASSERT_NE(label, nullptr);
346       ASSERT_NE(label->lang, nullptr);
347       ASSERT_NE(label->text, nullptr);
348       query_args.emplace_back(std::make_pair(
349           QUERY_INDEX_APPINFO_GET_LOCALED_LABEL,
350           std::vector<std::string>{app->appid, label->lang}));
351       expected_label.emplace_back(std::vector<std::string>{label->text});
352     }
353   }
354
355   QueryHandlerMock query_handler(0, 0);
356   query_handler.SetQueryArgs(std::move(query_args));
357   query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
358   query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
359
360   EXPECT_CALL(query_handler,
361       Connect()).WillOnce(testing::Return(true));
362   EXPECT_CALL(query_handler, GetConnection())
363       .WillOnce(testing::Return(GetDBHandles()));
364   ASSERT_EQ(query_handler.Execute(), 0);
365   auto result = query_handler.GetResult();
366   ASSERT_EQ(result, expected_label);
367 }