30545bde5b42ca86460aeb92803060acbc768d3d
[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 #include <gtest/gtest.h>
19
20 #include <cstdio>
21 #include <fstream>
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 "mock/test_fixture.h"
31 #include "mock/system_info_mock.h"
32 #include "pkgmgr-info.h"
33 #include "pkgmgr_query_index.h"
34 #include "pkgmgrinfo_basic.h"
35 #include "server/pkgmgrinfo_internal.h"
36
37 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
38
39 namespace psd = pkgmgr_server::database;
40 namespace pc = pkgmgr_common;
41
42 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
43  public:
44   CreateParserDBHandlerMock(uid_t uid, int pid)
45       : psd::CreateDBHandler(uid, pid) {}
46
47   MOCK_METHOD0(Connect, bool());
48   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
49       uid_t>>&());
50 };
51
52 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
53  public:
54   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
55
56   MOCK_METHOD0(Connect, bool());
57   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
58       uid_t>>&());
59 };
60
61 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
62  public:
63   PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
64
65   MOCK_METHOD0(Connect, bool());
66   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
67       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, const std::vector<std::pair<tizen_base::Database,
76       uid_t>>&());
77 };
78
79 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
80  public:
81   DepInfoGetDBHandlerMock(uid_t uid, int pid) :
82       psd::DepInfoGetDBHandler(uid, pid) {}
83
84   MOCK_METHOD0(Connect, bool());
85   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
86       uid_t>>&());
87 };
88
89 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
90
91 class ParserDBHandlerTest : public TestFixture {
92  public:
93   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
94   virtual ~ParserDBHandlerTest() {}
95
96   virtual void SetUp() {
97     tizen_base::Database db(TEST_PARSER_DB, SQLITE_OPEN_READWRITE |
98         SQLITE_OPEN_CREATE);
99     db_handles_.emplace_back(std::move(db), 0);
100     CreateParserDBHandlerMock create_db_handler(0, 0);
101
102     EXPECT_CALL(create_db_handler, Connect())
103         .Times(2).WillRepeatedly(testing::Return(true));
104     EXPECT_CALL(create_db_handler, GetConnection())
105         .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
106     EXPECT_CALL(GetMock<SystemInfoMock>(),
107       system_info_get_platform_int(testing::_, testing::_))
108           .WillRepeatedly(testing::DoAll(
109                   testing::SetArgPointee<1>(120), testing::Return(0)));
110     MakeVersionFile();
111     pkgmgr_server::internal::SetEnableUnitTest(true);
112     ASSERT_EQ(create_db_handler.Execute(), 0);
113     pkgmgr_server::internal::SetEnableUnitTest(false);
114   }
115
116   virtual void TearDown() {
117     db_handles_.clear();
118     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
119     std::string journal_path(TEST_PARSER_DB);
120     journal_path += "-journal";
121     ASSERT_EQ(remove(journal_path.c_str()), 0);
122   }
123
124  private:
125   void MakeVersionFile() {
126     std::remove("./pkg_db_version.txt");
127     std::ofstream ofs("./pkg_db_version.txt");
128     ofs << "30005";
129   }
130
131  protected:
132   std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
133 };
134
135 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Install) {
136   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
137   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
138   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
139       GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
140   pkg_set_db_handler.SetPkgInfo(ptr.get());
141
142   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
143   EXPECT_CALL(pkg_set_db_handler, GetConnection())
144       .WillOnce(testing::ReturnRef(db_handles_));
145   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
146 }
147
148 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Update) {
149   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
150   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Update);
151   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
152       GetUpdatedTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
153   pkg_set_db_handler.SetPkgInfo(ptr.get());
154
155   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
156   EXPECT_CALL(pkg_set_db_handler, GetConnection())
157       .WillOnce(testing::ReturnRef(db_handles_));
158   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
159 }
160
161 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest_Delete) {
162   std::string pkgid = "pkgA";
163   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
164
165   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Delete);
166   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
167       GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
168   pkg_set_db_handler.SetPkgInfo(ptr.get());
169
170   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
171   EXPECT_CALL(pkg_set_db_handler, GetConnection())
172       .WillOnce(testing::ReturnRef(db_handles_));
173   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
174
175   pkgmgrinfo_pkginfo_filter_h filter;
176
177   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
178   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
179       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
180           reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
181               pkgmgrinfo_pkginfo_filter_destroy);
182   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
183       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
184   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
185       PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
186
187   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
188   pkg_get_db_handler.SetFilter(filter_ptr.get());
189   pkg_get_db_handler.SetLocale("test_lang");
190
191   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
192   EXPECT_CALL(pkg_get_db_handler, GetConnection())
193       .WillOnce(testing::ReturnRef(db_handles_));
194   ASSERT_EQ(pkg_get_db_handler.Execute(), PMINFO_R_ENOENT);
195 }
196
197 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
198   std::string pkgid = "pkgA";
199   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
200   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
201   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
202       GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
203   pkg_set_db_handler.SetPkgInfo(ptr.get());
204
205   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
206   EXPECT_CALL(pkg_set_db_handler, GetConnection())
207       .WillOnce(testing::ReturnRef(db_handles_));
208   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
209
210   pkgmgrinfo_pkginfo_filter_h filter;
211
212   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
213   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
214       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
215           reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
216               pkgmgrinfo_pkginfo_filter_destroy);
217   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
218       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
219   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
220       PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
221
222   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
223   pkg_get_db_handler.SetFilter(filter_ptr.get());
224   pkg_get_db_handler.SetLocale("test_lang");
225
226   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
227   EXPECT_CALL(pkg_get_db_handler, GetConnection())
228       .WillOnce(testing::ReturnRef(db_handles_));
229   ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
230
231   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
232   ASSERT_EQ(lpkginfo_list.size(), 1);
233
234   auto test_pkginfo = GetTestPackage(pkgid);
235   std::vector<std::shared_ptr<package_x>> rpkginfo_list;
236   rpkginfo_list.emplace_back(test_pkginfo, pkgmgrinfo_basic_free_package);
237
238   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
239 }
240
241 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
242   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
243   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
244   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
245       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
246   pkg_set_db_handler.SetPkgInfo(ptr.get());
247
248   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
249   EXPECT_CALL(pkg_set_db_handler, GetConnection())
250       .WillOnce(testing::ReturnRef(db_handles_));
251   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
252
253   pkgmgrinfo_appinfo_filter_h filter;
254   std::string appid = "test_app1";
255
256   ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
257   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
258       pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
259           reinterpret_cast<pkgmgrinfo_filter_x*>(filter),
260               pkgmgrinfo_appinfo_filter_destroy);
261   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
262       PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
263   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
264       PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
265
266   AppInfoDBHandlerMock appinfo_db_handler(0, 0);
267   appinfo_db_handler.SetFilter(filter_ptr.get());
268
269   EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
270   EXPECT_CALL(appinfo_db_handler, GetConnection())
271       .WillOnce(testing::ReturnRef(db_handles_));
272   appinfo_db_handler.SetLocale("test_lang");
273   ASSERT_EQ(appinfo_db_handler.Execute(), 0);
274
275   auto lappinfo_list = appinfo_db_handler.DetachAppHandle();
276   ASSERT_EQ(lappinfo_list.size(), 1);
277
278   auto test_appinfo = GetTestApplication(appid);
279   std::vector<std::shared_ptr<application_x>> rappinfo_list;
280   rappinfo_list.emplace_back(test_appinfo, std::free);
281
282   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
283 }
284
285 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
286   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
287   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
288   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
289       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
290   pkg_set_db_handler.SetPkgInfo(ptr.get());
291
292   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
293   EXPECT_CALL(pkg_set_db_handler, GetConnection())
294       .WillOnce(testing::ReturnRef(db_handles_));
295   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
296
297   DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
298   depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
299   EXPECT_CALL(depinfo_get_db_handler,
300       Connect()).WillOnce(testing::Return(true));
301   EXPECT_CALL(depinfo_get_db_handler, GetConnection())
302       .WillOnce(testing::ReturnRef(db_handles_));
303   ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
304
305   auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
306   ASSERT_EQ(depinfo_from_db.size(), 1);
307
308   std::vector<dependency_x*> depinfo_list;
309   depinfo_list.emplace_back(GetTestDepInfo("test_package"));
310
311   EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
312 }