Separate parser db handler and query db handler unit test
[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
19 #include <gtest/gtest.h>
20
21 #include <cstdio>
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
31 #include "mock/file_mock.h"
32 #include "mock/test_fixture.h"
33 #include "mock/system_info_mock.h"
34
35 #include "pkgmgr-info.h"
36 #include "pkgmgr_query_index.h"
37 #include "pkgmgrinfo_basic.h"
38
39 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
40
41 namespace psd = pkgmgr_server::database;
42 namespace pc = pkgmgr_common;
43
44 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
45  public:
46   CreateParserDBHandlerMock(uid_t uid, int pid)
47       : psd::CreateDBHandler(uid, pid) {}
48
49   MOCK_METHOD0(Connect, bool());
50   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, 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, std::vector<std::pair<sqlite3*, 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, std::vector<std::pair<sqlite3*, uid_t>>());
67 };
68
69 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
70  public:
71   AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
72
73   MOCK_METHOD0(Connect, bool());
74   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
75 };
76
77 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
78  public:
79   DepInfoGetDBHandlerMock(uid_t uid, int pid) :
80       psd::DepInfoGetDBHandler(uid, pid) {}
81
82   MOCK_METHOD0(Connect, bool());
83   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
84 };
85
86 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
87
88 class ParserDBHandlerTest : public TestFixture {
89  public:
90   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
91   virtual ~ParserDBHandlerTest() {}
92
93   virtual void SetUp() {
94     sqlite3 *db;
95     ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
96         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
97
98     SetDBHandles(
99         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(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::Return(GetDBHandles()));
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     fopen_mock_setup(true);
111     ASSERT_EQ(create_db_handler.Execute(), 0);
112     fopen_mock_setup(false);
113   }
114
115   virtual void TearDown() {
116     for (auto& handle : db_handles_)
117       sqlite3_close_v2(handle.first);
118
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   }
124
125   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
126     return db_handles_;
127   }
128
129  private:
130   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
131     db_handles_ = db_handles;
132   }
133
134   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
135 };
136
137 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest) {
138   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
139   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
140   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
141       GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
142   pkg_set_db_handler.SetPkgInfo(ptr.get());
143
144   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
145   EXPECT_CALL(pkg_set_db_handler, GetConnection())
146       .WillOnce(testing::Return(GetDBHandles()));
147   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
148 }
149
150 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
151   std::string pkgid = "pkgA";
152   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
153   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
154   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
155       GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
156   pkg_set_db_handler.SetPkgInfo(ptr.get());
157
158   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
159   EXPECT_CALL(pkg_set_db_handler, GetConnection())
160       .WillOnce(testing::Return(GetDBHandles()));
161   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
162
163   pkgmgrinfo_pkginfo_filter_h filter;
164
165   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
166   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
167       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
168           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
169               pkgmgrinfo_pkginfo_filter_destroy);
170   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
171       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
172   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
173       PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
174
175   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
176   pkg_get_db_handler.SetFilter(filter_ptr.get());
177   pkg_get_db_handler.SetLocale("test_lang");
178
179   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
180   EXPECT_CALL(pkg_get_db_handler, GetConnection())
181       .WillOnce(testing::Return(GetDBHandles()));
182   ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
183
184   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
185   ASSERT_EQ(lpkginfo_list.size(), 1);
186
187   auto test_pkginfo = GetTestPackage(pkgid);
188   std::vector<package_x*> rpkginfo_list;
189   rpkginfo_list.emplace_back(test_pkginfo);
190
191   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
192 }
193
194 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
195   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
196   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
197   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
198       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
199   pkg_set_db_handler.SetPkgInfo(ptr.get());
200
201   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
202   EXPECT_CALL(pkg_set_db_handler, GetConnection())
203       .WillOnce(testing::Return(GetDBHandles()));
204   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
205
206   pkgmgrinfo_appinfo_filter_h filter;
207   std::string appid = "test_app1";
208
209   ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
210   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
211       pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
212           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
213               pkgmgrinfo_appinfo_filter_destroy);
214   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
215       PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
216   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
217       PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
218
219   AppInfoDBHandlerMock appinfo_db_handler(0, 0);
220   appinfo_db_handler.SetFilter(filter_ptr.get());
221
222   EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
223   EXPECT_CALL(appinfo_db_handler, GetConnection())
224       .WillOnce(testing::Return(GetDBHandles()));
225   appinfo_db_handler.SetLocale("test_lang");
226   ASSERT_EQ(appinfo_db_handler.Execute(), 0);
227
228   auto lappinfo_list = appinfo_db_handler.GetAppHandle();
229   ASSERT_EQ(lappinfo_list.size(), 1);
230
231   auto test_appinfo = GetTestApplication(appid);
232   std::vector<application_x*> rappinfo_list;
233   rappinfo_list.emplace_back(test_appinfo);
234
235   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
236 }
237
238 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
239   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
240   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
241   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
242       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
243   pkg_set_db_handler.SetPkgInfo(ptr.get());
244
245   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
246   EXPECT_CALL(pkg_set_db_handler, GetConnection())
247       .WillOnce(testing::Return(GetDBHandles()));
248   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
249
250   DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
251   depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
252   EXPECT_CALL(depinfo_get_db_handler,
253       Connect()).WillOnce(testing::Return(true));
254   EXPECT_CALL(depinfo_get_db_handler, GetConnection())
255       .WillOnce(testing::Return(GetDBHandles()));
256   ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
257
258   auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
259   ASSERT_EQ(depinfo_from_db.size(), 1);
260
261   std::vector<dependency_x*> depinfo_list;
262   depinfo_list.emplace_back(GetTestDepInfo("test_package"));
263
264   EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
265 }