e081fabd934a9a7d788540b47447bf1877c79fcd
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / test_query_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 #include <gtest/gtest.h>
19
20 #include <cstdio>
21 #include <fstream>
22
23 #include "create_db_handler.hh"
24 #include "db_type.hh"
25 #include "parcel_utils.hh"
26 #include "pkg_set_db_handler.hh"
27 #include "query_handler.hh"
28 #include "mock/test_fixture.h"
29 #include "mock/system_info_mock.h"
30 #include "pkgmgr-info.h"
31 #include "pkgmgr_query_index.h"
32 #include "pkgmgrinfo_basic.h"
33 #include "server/pkgmgrinfo_internal.h"
34
35 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
36
37 namespace psd = pkgmgr_server::database;
38 namespace pc = pkgmgr_common;
39
40 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
41  public:
42   CreateParserDBHandlerMock(uid_t uid, int pid)
43       : psd::CreateDBHandler(uid, pid) {}
44
45   MOCK_METHOD0(Connect, bool());
46   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
47       uid_t>>&());
48 };
49
50 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
51  public:
52   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
53
54   MOCK_METHOD0(Connect, bool());
55   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
56       uid_t>>&());
57 };
58
59 class QueryHandlerMock : public psd::QueryHandler {
60  public:
61   QueryHandlerMock(uid_t uid, int pid) :
62       psd::QueryHandler(uid, pid) {}
63
64   MOCK_METHOD0(Connect, bool());
65   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
66       uid_t>>&());
67 };
68
69 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
70
71 class ParserDBHandlerTest : public TestFixture {
72  public:
73   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
74   virtual ~ParserDBHandlerTest() {}
75
76   virtual void SetUp() {
77     tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
78         SQLITE_OPEN_CREATE);
79     db_handles_.emplace_back(std::move(db), 0);
80     CreateParserDBHandlerMock create_db_handler(0, 0);
81
82     EXPECT_CALL(create_db_handler, Connect())
83         .Times(2).WillRepeatedly(testing::Return(true));
84     EXPECT_CALL(create_db_handler, GetConnection())
85         .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
86     EXPECT_CALL(GetMock<SystemInfoMock>(),
87       system_info_get_platform_int(testing::_, testing::_))
88           .WillRepeatedly(testing::DoAll(
89                   testing::SetArgPointee<1>(120), testing::Return(0)));
90     MakeVersionFile();
91     pkgmgr_server::internal::SetEnableUnitTest(true);
92     ASSERT_EQ(create_db_handler.Execute(), 0);
93     pkgmgr_server::internal::SetEnableUnitTest(false);
94   }
95
96   virtual void TearDown() {
97     db_handles_.clear();
98     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
99     std::string journal_path(TEST_PARSER_DB);
100     journal_path += "-journal";
101     ASSERT_EQ(remove(journal_path.c_str()), 0);
102   }
103
104  private:
105   void MakeVersionFile() {
106     std::remove("./pkg_db_version.txt");
107     std::ofstream ofs("./pkg_db_version.txt");
108     ofs << "30005";
109   }
110
111  protected:
112   std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
113 };
114
115 TEST_F(ParserDBHandlerTest, QueryDBHandler_PluginInfoTest) {
116   const std::string pkgid = "test_pkgid";
117   const std::string appid = "test_appid";
118   const std::string plugin_type = "test_plugin_type";
119   const std::string plugin_name = "test_plugin_name";
120
121   QueryHandlerMock set_query_handler(0, 0);
122   set_query_handler.SetQueryArgs(
123       std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
124           std::make_pair(
125               QUERY_INDEX_INSERT_PACKAGE_PLUGIN_EXECUTION_INFO,
126               std::vector<std::optional<std::string>>{
127                   pkgid, appid, plugin_type, plugin_name})});
128   set_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
129   set_query_handler.SetOpType(
130       pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
131
132   EXPECT_CALL(set_query_handler,
133       Connect()).WillOnce(testing::Return(true));
134   EXPECT_CALL(set_query_handler, GetConnection())
135       .WillOnce(testing::ReturnRef(db_handles_));
136   ASSERT_EQ(set_query_handler.Execute(), 0);
137
138   QueryHandlerMock get_query_handler(0, 0);
139   get_query_handler.SetQueryArgs(
140       std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
141           std::make_pair(
142               QUERY_INDEX_PLUGININFO_GET_APPIDS,
143               std::vector<std::optional<std::string>>{
144                   pkgid, plugin_type, plugin_name})});
145   get_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
146   get_query_handler.SetOpType(
147       pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
148
149   EXPECT_CALL(get_query_handler,
150       Connect()).WillOnce(testing::Return(true));
151   EXPECT_CALL(get_query_handler, GetConnection())
152       .WillOnce(testing::ReturnRef(db_handles_));
153   ASSERT_EQ(get_query_handler.Execute(), 0);
154   auto result = get_query_handler.GetResult();
155   ASSERT_EQ(result.size(), 1);
156   ASSERT_EQ(result[0].size(), 1);
157   ASSERT_EQ(appid, result[0][0]);
158 }
159
160 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetLocaledLabelTest) {
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("test_package"), 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::ReturnRef(db_handles_));
170   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
171
172   std::vector<std::pair<int,
173       std::vector<std::optional<std::string>>>> query_args;
174   std::vector<std::vector<std::optional<std::string>>> expected_label;
175   for (GList* app_list = ptr.get()->application; app_list;
176       app_list = app_list->next) {
177     application_x* app = reinterpret_cast<application_x*>(app_list->data);
178     ASSERT_NE(app, nullptr);
179     ASSERT_NE(app->appid, nullptr);
180     ASSERT_NE(app->label, nullptr);
181
182     for (GList* label_list = app->label; label_list;
183         label_list = label_list->next) {
184       label_x* label = reinterpret_cast<label_x*>(label_list->data);
185       ASSERT_NE(label, nullptr);
186       ASSERT_NE(label->lang, nullptr);
187       ASSERT_NE(label->text, nullptr);
188       query_args.emplace_back(std::make_pair(
189           QUERY_INDEX_APPINFO_GET_LOCALED_LABEL,
190           std::vector<std::optional<std::string>>{app->appid, label->lang}));
191       expected_label.emplace_back(std::vector<std::optional<std::string>>{label->text});
192     }
193   }
194
195   QueryHandlerMock query_handler(0, 0);
196   query_handler.SetQueryArgs(std::move(query_args));
197   query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
198   query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
199
200   EXPECT_CALL(query_handler,
201       Connect()).WillOnce(testing::Return(true));
202   EXPECT_CALL(query_handler, GetConnection())
203       .WillOnce(testing::ReturnRef(db_handles_));
204   ASSERT_EQ(query_handler.Execute(), 0);
205   auto result = query_handler.GetResult();
206   ASSERT_EQ(result, expected_label);
207 }
208
209 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetDataControlTest) {
210   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
211   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
212   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
213       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
214   pkg_set_db_handler.SetPkgInfo(ptr.get());
215
216   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
217   EXPECT_CALL(pkg_set_db_handler, GetConnection())
218       .WillOnce(testing::ReturnRef(db_handles_));
219   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
220
221   std::vector<std::pair<int,
222       std::vector<std::optional<std::string>>>> query_args;
223   std::vector<std::vector<std::optional<std::string>>> expected_label;
224   for (GList* app_list = ptr.get()->application; app_list;
225       app_list = app_list->next) {
226     application_x* app = reinterpret_cast<application_x*>(app_list->data);
227     ASSERT_NE(app, nullptr);
228     ASSERT_NE(app->appid, nullptr);
229     ASSERT_NE(app->datacontrol, nullptr);
230
231     for (GList* dc_list = app->datacontrol; dc_list; dc_list = dc_list->next) {
232       datacontrol_x* dc = reinterpret_cast<datacontrol_x*>(dc_list->data);
233       ASSERT_NE(dc, nullptr);
234       ASSERT_NE(dc->providerid, nullptr);
235       ASSERT_NE(dc->type, nullptr);
236       ASSERT_NE(dc->access, nullptr);
237       query_args.emplace_back(std::make_pair(
238           QUERY_INDEX_APPINFO_GET_DATACONTROL_INFO,
239           std::vector<std::optional<std::string>>{dc->providerid, dc->type}));
240       expected_label.emplace_back(
241           std::vector<std::optional<std::string>>{app->appid, dc->access});
242     }
243   }
244
245   QueryHandlerMock query_handler(0, 0);
246   query_handler.SetQueryArgs(std::move(query_args));
247   query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
248   query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
249
250   EXPECT_CALL(query_handler,
251       Connect()).WillOnce(testing::Return(true));
252   EXPECT_CALL(query_handler, GetConnection())
253       .WillOnce(testing::ReturnRef(db_handles_));
254   ASSERT_EQ(query_handler.Execute(), 0);
255   auto result = query_handler.GetResult();
256   ASSERT_EQ(result, expected_label);
257 }
258
259 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetDataControlAppIdTest) {
260   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
261   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
262   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
263       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
264   pkg_set_db_handler.SetPkgInfo(ptr.get());
265
266   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
267   EXPECT_CALL(pkg_set_db_handler, GetConnection())
268       .WillOnce(testing::ReturnRef(db_handles_));
269   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
270
271   std::vector<std::pair<int,
272       std::vector<std::optional<std::string>>>> query_args;
273   std::vector<std::vector<std::optional<std::string>>> expected_appids;
274   std::map<std::string, std::vector<std::optional<std::string>>> provider_map;
275   for (GList* app_list = ptr.get()->application; app_list;
276       app_list = app_list->next) {
277     application_x* app = reinterpret_cast<application_x*>(app_list->data);
278     ASSERT_NE(app, nullptr);
279     ASSERT_NE(app->appid, nullptr);
280     ASSERT_NE(app->datacontrol, nullptr);
281
282     for (GList* dc_list = app->datacontrol; dc_list; dc_list = dc_list->next) {
283       datacontrol_x* dc = reinterpret_cast<datacontrol_x*>(dc_list->data);
284       ASSERT_NE(dc, nullptr);
285       ASSERT_NE(dc->providerid, nullptr);
286       ASSERT_NE(dc->type, nullptr);
287       ASSERT_NE(dc->access, nullptr);
288       provider_map[dc->providerid].emplace_back(app->appid);
289     }
290   }
291
292   for (auto& it : provider_map) {
293     query_args.emplace_back(std::make_pair(
294         QUERY_INDEX_APPINFO_GET_DATACONTROL_APPID,
295         std::vector<std::optional<std::string>>{it.first}));
296     expected_appids.emplace_back(it.second);
297   }
298
299   QueryHandlerMock query_handler(0, 0);
300   query_handler.SetQueryArgs(std::move(query_args));
301   query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
302   query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
303
304   EXPECT_CALL(query_handler,
305       Connect()).WillOnce(testing::Return(true));
306   EXPECT_CALL(query_handler, GetConnection())
307       .WillOnce(testing::ReturnRef(db_handles_));
308   ASSERT_EQ(query_handler.Execute(), 0);
309   auto result = query_handler.GetResult();
310   ASSERT_NE(result.size(), 0);
311   for (auto& it : result)
312     std::sort(it.begin(), it.end());
313   for (auto& it : expected_appids)
314     std::sort(it.begin(), it.end());
315   ASSERT_EQ(result, expected_appids);
316 }