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