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"
37 namespace psd = pkgmgr_server::database;
38 namespace pc = pkgmgr_common;
40 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
42 CreateParserDBHandlerMock(uid_t uid, int pid)
43 : psd::CreateDBHandler(uid, pid) {}
45 MOCK_METHOD0(Connect, bool());
46 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
50 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
52 PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
54 MOCK_METHOD0(Connect, bool());
55 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
59 class QueryHandlerMock : public psd::QueryHandler {
61 QueryHandlerMock(uid_t uid, int pid) :
62 psd::QueryHandler(uid, pid) {}
64 MOCK_METHOD0(Connect, bool());
65 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
69 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
71 class ParserDBHandlerTest : public TestFixture {
73 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
74 virtual ~ParserDBHandlerTest() {}
76 virtual void SetUp() {
77 tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
79 db_handles_.emplace_back(std::move(db), 0);
80 CreateParserDBHandlerMock create_db_handler(0, 0);
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)));
91 pkgmgr_server::internal::SetEnableUnitTest(true);
92 ASSERT_EQ(create_db_handler.Execute(), 0);
93 pkgmgr_server::internal::SetEnableUnitTest(false);
96 virtual void TearDown() {
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);
105 void MakeVersionFile() {
106 std::remove("./pkg_db_version.txt");
107 std::ofstream ofs("./pkg_db_version.txt");
112 std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
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";
121 QueryHandlerMock set_query_handler(0, 0);
122 set_query_handler.SetQueryArgs(
123 std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
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);
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);
138 QueryHandlerMock get_query_handler(0, 0);
139 get_query_handler.SetQueryArgs(
140 std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
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);
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]);
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());
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);
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);
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});
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);
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);
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());
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);
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);
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});
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);
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);
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());
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);
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);
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);
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);
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);
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);