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>
18 #include <gtest/gtest.h>
23 #include "create_db_handler.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"
35 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
36 #define DB_VERSION_FILE "./pkg_db_version.txt"
38 namespace psd = pkgmgr_server::database;
39 namespace pc = pkgmgr_common;
41 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
43 CreateParserDBHandlerMock(uid_t uid, int pid)
44 : psd::CreateDBHandler(uid, pid) {}
46 MOCK_METHOD0(Connect, bool());
47 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
51 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
53 PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
55 MOCK_METHOD0(Connect, bool());
56 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
60 class QueryHandlerMock : public psd::QueryHandler {
62 QueryHandlerMock(uid_t uid, int pid) :
63 psd::QueryHandler(uid, pid) {}
65 MOCK_METHOD0(Connect, bool());
66 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
70 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
72 class ParserDBHandlerTest : public TestFixture {
74 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
75 virtual ~ParserDBHandlerTest() {}
77 virtual void SetUp() {
78 tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
80 db_handles_.emplace_back(std::move(db), 0);
81 CreateParserDBHandlerMock create_db_handler(0, 0);
83 EXPECT_CALL(create_db_handler, Connect())
84 .Times(2).WillRepeatedly(testing::Return(true));
85 EXPECT_CALL(create_db_handler, GetConnection())
86 .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
87 EXPECT_CALL(GetMock<SystemInfoMock>(),
88 system_info_get_platform_int(testing::_, testing::_))
89 .WillRepeatedly(testing::DoAll(
90 testing::SetArgPointee<1>(120), testing::Return(0)));
92 pkgmgr_server::internal::SetEnableUnitTest(true);
93 ASSERT_EQ(create_db_handler.Execute(), 0);
94 pkgmgr_server::internal::SetEnableUnitTest(false);
97 virtual void TearDown() {
99 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
100 std::string journal_path(TEST_PARSER_DB);
101 journal_path += "-journal";
102 ASSERT_EQ(remove(journal_path.c_str()), 0);
103 ASSERT_EQ(remove(DB_VERSION_FILE), 0);
107 void MakeVersionFile() {
108 std::ofstream ofs(DB_VERSION_FILE);
113 std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
116 TEST_F(ParserDBHandlerTest, QueryDBHandler_PluginInfoTest) {
117 const std::string pkgid = "test_pkgid";
118 const std::string appid = "test_appid";
119 const std::string plugin_type = "test_plugin_type";
120 const std::string plugin_name = "test_plugin_name";
122 QueryHandlerMock set_query_handler(0, 0);
123 set_query_handler.SetQueryArgs(
124 std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
126 QUERY_INDEX_INSERT_PACKAGE_PLUGIN_EXECUTION_INFO,
127 std::vector<std::optional<std::string>>{
128 pkgid, appid, plugin_type, plugin_name})});
129 set_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
130 set_query_handler.SetOpType(
131 pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
133 EXPECT_CALL(set_query_handler,
134 Connect()).WillOnce(testing::Return(true));
135 EXPECT_CALL(set_query_handler, GetConnection())
136 .WillOnce(testing::ReturnRef(db_handles_));
137 ASSERT_EQ(set_query_handler.Execute(), 0);
139 QueryHandlerMock get_query_handler(0, 0);
140 get_query_handler.SetQueryArgs(
141 std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
143 QUERY_INDEX_PLUGININFO_GET_APPIDS,
144 std::vector<std::optional<std::string>>{
145 pkgid, plugin_type, plugin_name})});
146 get_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
147 get_query_handler.SetOpType(
148 pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
150 EXPECT_CALL(get_query_handler,
151 Connect()).WillOnce(testing::Return(true));
152 EXPECT_CALL(get_query_handler, GetConnection())
153 .WillOnce(testing::ReturnRef(db_handles_));
154 ASSERT_EQ(get_query_handler.Execute(), 0);
155 auto result = get_query_handler.GetResult();
156 ASSERT_EQ(result.size(), 1);
157 ASSERT_EQ(result[0].size(), 1);
158 ASSERT_EQ(appid, result[0][0]);
161 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetLocaledLabelTest) {
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("test_package"), pkgmgrinfo_basic_free_package);
166 pkg_set_db_handler.SetPkgInfo(ptr.get());
168 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
169 EXPECT_CALL(pkg_set_db_handler, GetConnection())
170 .WillOnce(testing::ReturnRef(db_handles_));
171 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
173 std::vector<std::pair<int,
174 std::vector<std::optional<std::string>>>> query_args;
175 std::vector<std::vector<std::optional<std::string>>> expected_label;
176 for (GList* app_list = ptr.get()->application; app_list;
177 app_list = app_list->next) {
178 application_x* app = reinterpret_cast<application_x*>(app_list->data);
179 ASSERT_NE(app, nullptr);
180 ASSERT_NE(app->appid, nullptr);
181 ASSERT_NE(app->label, nullptr);
183 for (GList* label_list = app->label; label_list;
184 label_list = label_list->next) {
185 label_x* label = reinterpret_cast<label_x*>(label_list->data);
186 ASSERT_NE(label, nullptr);
187 ASSERT_NE(label->lang, nullptr);
188 ASSERT_NE(label->text, nullptr);
189 query_args.emplace_back(std::make_pair(
190 QUERY_INDEX_APPINFO_GET_LOCALED_LABEL,
191 std::vector<std::optional<std::string>>{app->appid, label->lang}));
192 expected_label.emplace_back(std::vector<std::optional<std::string>>{label->text});
196 QueryHandlerMock query_handler(0, 0);
197 query_handler.SetQueryArgs(std::move(query_args));
198 query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
199 query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
201 EXPECT_CALL(query_handler,
202 Connect()).WillOnce(testing::Return(true));
203 EXPECT_CALL(query_handler, GetConnection())
204 .WillOnce(testing::ReturnRef(db_handles_));
205 ASSERT_EQ(query_handler.Execute(), 0);
206 auto result = query_handler.GetResult();
207 ASSERT_EQ(result, expected_label);
210 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetDataControlTest) {
211 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
212 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
213 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
214 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
215 pkg_set_db_handler.SetPkgInfo(ptr.get());
217 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
218 EXPECT_CALL(pkg_set_db_handler, GetConnection())
219 .WillOnce(testing::ReturnRef(db_handles_));
220 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
222 std::vector<std::pair<int,
223 std::vector<std::optional<std::string>>>> query_args;
224 std::vector<std::vector<std::optional<std::string>>> expected_label;
225 for (GList* app_list = ptr.get()->application; app_list;
226 app_list = app_list->next) {
227 application_x* app = reinterpret_cast<application_x*>(app_list->data);
228 ASSERT_NE(app, nullptr);
229 ASSERT_NE(app->appid, nullptr);
230 ASSERT_NE(app->datacontrol, nullptr);
232 for (GList* dc_list = app->datacontrol; dc_list; dc_list = dc_list->next) {
233 datacontrol_x* dc = reinterpret_cast<datacontrol_x*>(dc_list->data);
234 ASSERT_NE(dc, nullptr);
235 ASSERT_NE(dc->providerid, nullptr);
236 ASSERT_NE(dc->type, nullptr);
237 ASSERT_NE(dc->access, nullptr);
238 query_args.emplace_back(std::make_pair(
239 QUERY_INDEX_APPINFO_GET_DATACONTROL_INFO,
240 std::vector<std::optional<std::string>>{dc->providerid, dc->type}));
241 expected_label.emplace_back(
242 std::vector<std::optional<std::string>>{app->appid, dc->access});
246 QueryHandlerMock query_handler(0, 0);
247 query_handler.SetQueryArgs(std::move(query_args));
248 query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
249 query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
251 EXPECT_CALL(query_handler,
252 Connect()).WillOnce(testing::Return(true));
253 EXPECT_CALL(query_handler, GetConnection())
254 .WillOnce(testing::ReturnRef(db_handles_));
255 ASSERT_EQ(query_handler.Execute(), 0);
256 auto result = query_handler.GetResult();
257 ASSERT_EQ(result, expected_label);
260 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetDataControlAppIdTest) {
261 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
262 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
263 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
264 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
265 pkg_set_db_handler.SetPkgInfo(ptr.get());
267 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
268 EXPECT_CALL(pkg_set_db_handler, GetConnection())
269 .WillOnce(testing::ReturnRef(db_handles_));
270 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
272 std::vector<std::pair<int,
273 std::vector<std::optional<std::string>>>> query_args;
274 std::vector<std::vector<std::optional<std::string>>> expected_appids;
275 std::map<std::string, std::vector<std::optional<std::string>>> provider_map;
276 for (GList* app_list = ptr.get()->application; app_list;
277 app_list = app_list->next) {
278 application_x* app = reinterpret_cast<application_x*>(app_list->data);
279 ASSERT_NE(app, nullptr);
280 ASSERT_NE(app->appid, nullptr);
281 ASSERT_NE(app->datacontrol, nullptr);
283 for (GList* dc_list = app->datacontrol; dc_list; dc_list = dc_list->next) {
284 datacontrol_x* dc = reinterpret_cast<datacontrol_x*>(dc_list->data);
285 ASSERT_NE(dc, nullptr);
286 ASSERT_NE(dc->providerid, nullptr);
287 ASSERT_NE(dc->type, nullptr);
288 ASSERT_NE(dc->access, nullptr);
289 provider_map[dc->providerid].emplace_back(app->appid);
293 for (auto& it : provider_map) {
294 query_args.emplace_back(std::make_pair(
295 QUERY_INDEX_APPINFO_GET_DATACONTROL_APPID,
296 std::vector<std::optional<std::string>>{it.first}));
297 expected_appids.emplace_back(it.second);
300 QueryHandlerMock query_handler(0, 0);
301 query_handler.SetQueryArgs(std::move(query_args));
302 query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
303 query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
305 EXPECT_CALL(query_handler,
306 Connect()).WillOnce(testing::Return(true));
307 EXPECT_CALL(query_handler, GetConnection())
308 .WillOnce(testing::ReturnRef(db_handles_));
309 ASSERT_EQ(query_handler.Execute(), 0);
310 auto result = query_handler.GetResult();
311 ASSERT_NE(result.size(), 0);
312 for (auto& it : result)
313 std::sort(it.begin(), it.end());
314 for (auto& it : expected_appids)
315 std::sort(it.begin(), it.end());
316 ASSERT_EQ(result, expected_appids);