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>
19 #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"
29 #include "mock/file_mock.h"
30 #include "mock/test_fixture.h"
31 #include "mock/system_info_mock.h"
33 #include "pkgmgr-info.h"
34 #include "pkgmgr_query_index.h"
35 #include "pkgmgrinfo_basic.h"
37 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
39 namespace psd = pkgmgr_server::database;
40 namespace pc = pkgmgr_common;
42 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
44 CreateParserDBHandlerMock(uid_t uid, int pid)
45 : psd::CreateDBHandler(uid, pid) {}
47 MOCK_METHOD0(Connect, bool());
48 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
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, std::vector<std::pair<sqlite3*, uid_t>>());
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, std::vector<std::pair<sqlite3*, uid_t>>());
68 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
70 class ParserDBHandlerTest : public TestFixture {
72 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
73 virtual ~ParserDBHandlerTest() {}
75 virtual void SetUp() {
78 int ret = sqlite3_open_v2(TEST_PARSER_DB, &db,
79 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
83 ASSERT_EQ(ret, SQLITE_OK);
86 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
87 CreateParserDBHandlerMock create_db_handler(0, 0);
89 EXPECT_CALL(create_db_handler, Connect())
90 .Times(2).WillRepeatedly(testing::Return(true));
91 EXPECT_CALL(create_db_handler, GetConnection())
92 .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
93 EXPECT_CALL(GetMock<SystemInfoMock>(),
94 system_info_get_platform_int(testing::_, testing::_))
95 .WillRepeatedly(testing::DoAll(
96 testing::SetArgPointee<1>(120), testing::Return(0)));
97 fopen_mock_setup(true);
98 ASSERT_EQ(create_db_handler.Execute(), 0);
99 fopen_mock_setup(false);
102 virtual void TearDown() {
103 for (auto& handle : db_handles_)
104 sqlite3_close_v2(handle.first);
106 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
107 std::string journal_path(TEST_PARSER_DB);
108 journal_path += "-journal";
109 ASSERT_EQ(remove(journal_path.c_str()), 0);
112 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
117 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
118 db_handles_ = db_handles;
121 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
124 TEST_F(ParserDBHandlerTest, QueryDBHandler_PluginInfoTest) {
125 const std::string pkgid = "test_pkgid";
126 const std::string appid = "test_appid";
127 const std::string plugin_type = "test_plugin_type";
128 const std::string plugin_name = "test_plugin_name";
130 QueryHandlerMock set_query_handler(0, 0);
131 set_query_handler.SetQueryArgs(
132 std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
134 QUERY_INDEX_INSERT_PACKAGE_PLUGIN_EXECUTION_INFO,
135 std::vector<std::optional<std::string>>{
136 pkgid, appid, plugin_type, plugin_name})});
137 set_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
138 set_query_handler.SetOpType(
139 pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
141 EXPECT_CALL(set_query_handler,
142 Connect()).WillOnce(testing::Return(true));
143 EXPECT_CALL(set_query_handler, GetConnection())
144 .WillOnce(testing::Return(GetDBHandles()));
145 ASSERT_EQ(set_query_handler.Execute(), 0);
147 QueryHandlerMock get_query_handler(0, 0);
148 get_query_handler.SetQueryArgs(
149 std::vector<std::pair<int, std::vector<std::optional<std::string>>>>{
151 QUERY_INDEX_PLUGININFO_GET_APPIDS,
152 std::vector<std::optional<std::string>>{
153 pkgid, plugin_type, plugin_name})});
154 get_query_handler.SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
155 get_query_handler.SetOpType(
156 pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
158 EXPECT_CALL(get_query_handler,
159 Connect()).WillOnce(testing::Return(true));
160 EXPECT_CALL(get_query_handler, GetConnection())
161 .WillOnce(testing::Return(GetDBHandles()));
162 ASSERT_EQ(get_query_handler.Execute(), 0);
163 auto result = get_query_handler.GetResult();
164 ASSERT_EQ(result.size(), 1);
165 ASSERT_EQ(result[0].size(), 1);
166 ASSERT_EQ(appid, result[0][0]);
169 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetLocaledLabelTest) {
170 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
171 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
172 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
173 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
174 pkg_set_db_handler.SetPkgInfo(ptr.get());
176 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
177 EXPECT_CALL(pkg_set_db_handler, GetConnection())
178 .WillOnce(testing::Return(GetDBHandles()));
179 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
181 std::vector<std::pair<int,
182 std::vector<std::optional<std::string>>>> query_args;
183 std::vector<std::vector<std::optional<std::string>>> expected_label;
184 for (GList* app_list = ptr.get()->application; app_list;
185 app_list = app_list->next) {
186 application_x* app = reinterpret_cast<application_x*>(app_list->data);
187 ASSERT_NE(app, nullptr);
188 ASSERT_NE(app->appid, nullptr);
189 ASSERT_NE(app->label, nullptr);
191 for (GList* label_list = app->label; label_list;
192 label_list = label_list->next) {
193 label_x* label = reinterpret_cast<label_x*>(label_list->data);
194 ASSERT_NE(label, nullptr);
195 ASSERT_NE(label->lang, nullptr);
196 ASSERT_NE(label->text, nullptr);
197 query_args.emplace_back(std::make_pair(
198 QUERY_INDEX_APPINFO_GET_LOCALED_LABEL,
199 std::vector<std::optional<std::string>>{app->appid, label->lang}));
200 expected_label.emplace_back(std::vector<std::optional<std::string>>{label->text});
204 QueryHandlerMock query_handler(0, 0);
205 query_handler.SetQueryArgs(std::move(query_args));
206 query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
207 query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
209 EXPECT_CALL(query_handler,
210 Connect()).WillOnce(testing::Return(true));
211 EXPECT_CALL(query_handler, GetConnection())
212 .WillOnce(testing::Return(GetDBHandles()));
213 ASSERT_EQ(query_handler.Execute(), 0);
214 auto result = query_handler.GetResult();
215 ASSERT_EQ(result, expected_label);
218 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetDataControlTest) {
219 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
220 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
221 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
222 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
223 pkg_set_db_handler.SetPkgInfo(ptr.get());
225 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
226 EXPECT_CALL(pkg_set_db_handler, GetConnection())
227 .WillOnce(testing::Return(GetDBHandles()));
228 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
230 std::vector<std::pair<int,
231 std::vector<std::optional<std::string>>>> query_args;
232 std::vector<std::vector<std::optional<std::string>>> expected_label;
233 for (GList* app_list = ptr.get()->application; app_list;
234 app_list = app_list->next) {
235 application_x* app = reinterpret_cast<application_x*>(app_list->data);
236 ASSERT_NE(app, nullptr);
237 ASSERT_NE(app->appid, nullptr);
238 ASSERT_NE(app->datacontrol, nullptr);
240 for (GList* dc_list = app->datacontrol; dc_list; dc_list = dc_list->next) {
241 datacontrol_x* dc = reinterpret_cast<datacontrol_x*>(dc_list->data);
242 ASSERT_NE(dc, nullptr);
243 ASSERT_NE(dc->providerid, nullptr);
244 ASSERT_NE(dc->type, nullptr);
245 ASSERT_NE(dc->access, nullptr);
246 query_args.emplace_back(std::make_pair(
247 QUERY_INDEX_APPINFO_GET_DATACONTROL_INFO,
248 std::vector<std::optional<std::string>>{dc->providerid, dc->type}));
249 expected_label.emplace_back(
250 std::vector<std::optional<std::string>>{app->appid, dc->access});
254 QueryHandlerMock query_handler(0, 0);
255 query_handler.SetQueryArgs(std::move(query_args));
256 query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
257 query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
259 EXPECT_CALL(query_handler,
260 Connect()).WillOnce(testing::Return(true));
261 EXPECT_CALL(query_handler, GetConnection())
262 .WillOnce(testing::Return(GetDBHandles()));
263 ASSERT_EQ(query_handler.Execute(), 0);
264 auto result = query_handler.GetResult();
265 ASSERT_EQ(result, expected_label);
268 TEST_F(ParserDBHandlerTest, QueryDBHandler_GetDataControlAppIdTest) {
269 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
270 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
271 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
272 GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
273 pkg_set_db_handler.SetPkgInfo(ptr.get());
275 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
276 EXPECT_CALL(pkg_set_db_handler, GetConnection())
277 .WillOnce(testing::Return(GetDBHandles()));
278 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
280 std::vector<std::pair<int,
281 std::vector<std::optional<std::string>>>> query_args;
282 std::vector<std::vector<std::optional<std::string>>> expected_appids;
283 std::map<std::string, std::vector<std::optional<std::string>>> provider_map;
284 for (GList* app_list = ptr.get()->application; app_list;
285 app_list = app_list->next) {
286 application_x* app = reinterpret_cast<application_x*>(app_list->data);
287 ASSERT_NE(app, nullptr);
288 ASSERT_NE(app->appid, nullptr);
289 ASSERT_NE(app->datacontrol, nullptr);
291 for (GList* dc_list = app->datacontrol; dc_list; dc_list = dc_list->next) {
292 datacontrol_x* dc = reinterpret_cast<datacontrol_x*>(dc_list->data);
293 ASSERT_NE(dc, nullptr);
294 ASSERT_NE(dc->providerid, nullptr);
295 ASSERT_NE(dc->type, nullptr);
296 ASSERT_NE(dc->access, nullptr);
297 provider_map[dc->providerid].emplace_back(app->appid);
301 for (auto& it : provider_map) {
302 query_args.emplace_back(std::make_pair(
303 QUERY_INDEX_APPINFO_GET_DATACONTROL_APPID,
304 std::vector<std::optional<std::string>>{it.first}));
305 expected_appids.emplace_back(it.second);
308 QueryHandlerMock query_handler(0, 0);
309 query_handler.SetQueryArgs(std::move(query_args));
310 query_handler.SetDBType(pc::DBType::DB_TYPE_FILE_PKGDB);
311 query_handler.SetOpType(pc::DBOperationType::OPERATION_TYPE_READ);
313 EXPECT_CALL(query_handler,
314 Connect()).WillOnce(testing::Return(true));
315 EXPECT_CALL(query_handler, GetConnection())
316 .WillOnce(testing::Return(GetDBHandles()));
317 ASSERT_EQ(query_handler.Execute(), 0);
318 auto result = query_handler.GetResult();
319 ASSERT_NE(result.size(), 0);
320 for (auto& it : result)
321 std::sort(it.begin(), it.end());
322 for (auto& it : expected_appids)
323 std::sort(it.begin(), it.end());
324 ASSERT_EQ(result, expected_appids);