Implement dependencyinfo unittest
[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 "pkgmgrinfo_basic.h"
37
38 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
39
40 namespace psd = pkgmgr_server::database;
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, std::vector<std::pair<sqlite3*, uid_t>>());
49 };
50
51 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
52  public:
53   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
54
55   MOCK_METHOD0(Connect, bool());
56   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
57 };
58
59 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
60  public:
61   PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
62
63   MOCK_METHOD0(Connect, bool());
64   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
65 };
66
67 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
68  public:
69   AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
70
71   MOCK_METHOD0(Connect, bool());
72   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
73 };
74
75 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
76  public:
77   DepInfoGetDBHandlerMock(uid_t uid, int pid) :
78       psd::DepInfoGetDBHandler(uid, pid) {}
79
80   MOCK_METHOD0(Connect, bool());
81   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
82 };
83
84 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
85
86 class ParserDBHandlerTest : public TestFixture {
87  public:
88   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
89   virtual ~ParserDBHandlerTest() {}
90
91   virtual void SetUp() {
92     sqlite3 *db;
93     ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
94         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
95
96     SetDBHandles(
97         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
98     CreateParserDBHandlerMock create_db_handler(0, 0);
99
100     EXPECT_CALL(create_db_handler, Connect())
101         .Times(2).WillRepeatedly(testing::Return(true));
102     EXPECT_CALL(create_db_handler, GetConnection())
103         .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
104     EXPECT_CALL(GetMock<SystemInfoMock>(),
105       system_info_get_platform_int(testing::_, testing::_))
106           .WillRepeatedly(testing::DoAll(
107                   testing::SetArgPointee<1>(120), testing::Return(0)));
108     fopen_mock_setup(true);
109     ASSERT_EQ(create_db_handler.Execute(), 0);
110     fopen_mock_setup(false);
111   }
112
113   virtual void TearDown() {
114     for (auto& handle : db_handles_)
115       sqlite3_close_v2(handle.first);
116
117     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
118     std::string journal_path(TEST_PARSER_DB);
119     journal_path += "-journal";
120     ASSERT_EQ(remove(journal_path.c_str()), 0);
121   }
122
123   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
124     return db_handles_;
125   }
126
127  private:
128   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
129     db_handles_ = db_handles;
130   }
131
132   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
133 };
134
135 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest) {
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::Return(GetDBHandles()));
145   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
146 }
147
148 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
149   std::string pkgid = "pkgA";
150   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
151   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
152   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
153       GetTestPackage(pkgid), 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::Return(GetDBHandles()));
159   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
160
161   pkgmgrinfo_pkginfo_filter_h filter;
162
163   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
164   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
165       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
166           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
167               pkgmgrinfo_pkginfo_filter_destroy);
168   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
169       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
170   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
171       PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
172
173   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
174   pkg_get_db_handler.SetFilter(filter_ptr.get());
175   pkg_get_db_handler.SetLocale("test_lang");
176
177   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
178   EXPECT_CALL(pkg_get_db_handler, GetConnection())
179       .WillOnce(testing::Return(GetDBHandles()));
180   ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
181
182   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
183   ASSERT_EQ(lpkginfo_list.size(), 1);
184
185   auto test_pkginfo = GetTestPackage(pkgid);
186   std::vector<package_x*> rpkginfo_list;
187   rpkginfo_list.emplace_back(test_pkginfo);
188
189   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
190 }
191
192 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
193   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
194   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
195   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
196       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
197   pkg_set_db_handler.SetPkgInfo(ptr.get());
198
199   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
200   EXPECT_CALL(pkg_set_db_handler, GetConnection())
201       .WillOnce(testing::Return(GetDBHandles()));
202   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
203
204   pkgmgrinfo_appinfo_filter_h filter;
205   std::string appid = "test_app1";
206
207   ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
208   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
209       pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
210           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
211               pkgmgrinfo_appinfo_filter_destroy);
212   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
213       PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
214   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
215       PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
216
217   AppInfoDBHandlerMock appinfo_db_handler(0, 0);
218   appinfo_db_handler.SetFilter(filter_ptr.get());
219
220   EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
221   EXPECT_CALL(appinfo_db_handler, GetConnection())
222       .WillOnce(testing::Return(GetDBHandles()));
223   appinfo_db_handler.SetLocale("test_lang");
224   ASSERT_EQ(appinfo_db_handler.Execute(), 0);
225
226   auto lappinfo_list = appinfo_db_handler.GetAppHandle();
227   ASSERT_EQ(lappinfo_list.size(), 1);
228
229   auto test_appinfo = GetTestApplication(appid);
230   std::vector<application_x*> rappinfo_list;
231   rappinfo_list.emplace_back(test_appinfo);
232
233   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
234 }
235
236 TEST_F(ParserDBHandlerTest, DepInfoDBHandlerTest) {
237   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
238   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
239   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
240       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
241   pkg_set_db_handler.SetPkgInfo(ptr.get());
242
243   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
244   EXPECT_CALL(pkg_set_db_handler, GetConnection())
245       .WillOnce(testing::Return(GetDBHandles()));
246   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
247
248   DepInfoGetDBHandlerMock depinfo_get_db_handler(0, 0);
249   depinfo_get_db_handler.SetPkgID("depends_on_pkgid");
250   EXPECT_CALL(depinfo_get_db_handler,
251       Connect()).WillOnce(testing::Return(true));
252   EXPECT_CALL(depinfo_get_db_handler, GetConnection())
253       .WillOnce(testing::Return(GetDBHandles()));
254   ASSERT_EQ(depinfo_get_db_handler.Execute(), 0);
255
256   auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
257   ASSERT_EQ(depinfo_from_db.size(), 1);
258
259   std::vector<dependency_x*> depinfo_list;
260   depinfo_list.emplace_back(GetTestDepInfo("test_package"));
261
262   EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));
263 }