Improve readability
[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 "db_type.hh"
26 #include "parcel_utils.hh"
27 #include "pkg_get_db_handler.hh"
28 #include "pkg_set_db_handler.hh"
29
30 #include "mock/file_mock.h"
31 #include "mock/test_fixture.h"
32 #include "mock/system_info_mock.h"
33
34 #include "pkgmgr-info.h"
35 #include "pkgmgrinfo_basic.h"
36
37 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
38
39 namespace psd = pkgmgr_server::database;
40
41 class CreateDBHandlerMock : public psd::CreateDBHandler {
42  public:
43   CreateDBHandlerMock(uid_t uid, int pid)
44       : psd::CreateDBHandler(uid, pid) {}
45
46   MOCK_METHOD0(Connect, bool());
47   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
48 };
49
50 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
51  public:
52   PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
53
54   MOCK_METHOD0(Connect, bool());
55   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
56 };
57
58 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
59  public:
60   PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
61
62   MOCK_METHOD0(Connect, bool());
63   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
64 };
65
66 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
67  public:
68   AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
69
70   MOCK_METHOD0(Connect, bool());
71   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
72 };
73
74 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
75
76 class ParserDBHandlerTest : public TestFixture {
77  public:
78   ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
79   virtual ~ParserDBHandlerTest() {}
80
81   virtual void SetUp() {
82     sqlite3 *db;
83     ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
84         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
85
86     SetDBHandles(
87         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
88     CreateDBHandlerMock create_db_handler(0, 0);
89
90     EXPECT_CALL(create_db_handler, Connect())
91         .Times(2).WillRepeatedly(testing::Return(true));
92     EXPECT_CALL(create_db_handler, GetConnection())
93         .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
94     EXPECT_CALL(GetMock<SystemInfoMock>(),
95       system_info_get_platform_int(testing::_, testing::_))
96           .WillRepeatedly(testing::DoAll(
97                   testing::SetArgPointee<1>(120), testing::Return(0)));
98     fopen_mock_setup(true);
99     ASSERT_EQ(create_db_handler.Execute(), 0);
100     fopen_mock_setup(false);
101   }
102
103   virtual void TearDown() {
104     for (auto& handle : db_handles_)
105       sqlite3_close_v2(handle.first);
106
107     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
108     std::string journal_path(TEST_PARSER_DB);
109     journal_path += "-journal";
110     ASSERT_EQ(remove(journal_path.c_str()), 0);
111   }
112
113   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
114     return db_handles_;
115   }
116
117  private:
118   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
119     db_handles_ = db_handles;
120   }
121
122   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
123 };
124
125 TEST_F(ParserDBHandlerTest, PkgSetDBHandlerTest) {
126   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
127   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
128   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
129       GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
130   pkg_set_db_handler.SetPkgInfo(ptr.get());
131
132   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
133   EXPECT_CALL(pkg_set_db_handler, GetConnection())
134       .WillOnce(testing::Return(GetDBHandles()));
135   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
136 }
137
138 TEST_F(ParserDBHandlerTest, PkgGetDBHandlerTest) {
139   std::string pkgid = "pkgA";
140   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
141   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
142   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
143       GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
144   pkg_set_db_handler.SetPkgInfo(ptr.get());
145
146   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
147   EXPECT_CALL(pkg_set_db_handler, GetConnection())
148       .WillOnce(testing::Return(GetDBHandles()));
149   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
150
151   pkgmgrinfo_pkginfo_filter_h filter;
152
153   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
154   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
155       pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
156           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
157               pkgmgrinfo_pkginfo_filter_destroy);
158   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
159       PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
160   ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
161       PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
162
163   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
164   pkg_get_db_handler.SetFilter(filter_ptr.get());
165   pkg_get_db_handler.SetLocale("test_lang");
166
167   EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
168   EXPECT_CALL(pkg_get_db_handler, GetConnection())
169       .WillOnce(testing::Return(GetDBHandles()));
170   ASSERT_EQ(pkg_get_db_handler.Execute(), 0);
171
172   auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
173   ASSERT_EQ(lpkginfo_list.size(), 1);
174
175   auto test_pkginfo = GetTestPackage(pkgid);
176   std::vector<package_x*> rpkginfo_list;
177   rpkginfo_list.emplace_back(test_pkginfo);
178
179   ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
180 }
181
182 TEST_F(ParserDBHandlerTest, AppInfoDBHandlerTest) {
183   PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
184   pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
185   std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
186       GetTestPackage("test_package"), pkgmgrinfo_basic_free_package);
187   pkg_set_db_handler.SetPkgInfo(ptr.get());
188
189   EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
190   EXPECT_CALL(pkg_set_db_handler, GetConnection())
191       .WillOnce(testing::Return(GetDBHandles()));
192   ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
193
194   pkgmgrinfo_appinfo_filter_h filter;
195   std::string appid = "test_app1";
196
197   ASSERT_EQ(pkgmgrinfo_appinfo_filter_create(&filter), PMINFO_R_OK);
198   std::unique_ptr<pkgmgrinfo_filter_x, decltype(
199       pkgmgrinfo_appinfo_filter_destroy)*> filter_ptr(
200           reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
201               pkgmgrinfo_appinfo_filter_destroy);
202   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_string(filter,
203       PMINFO_APPINFO_PROP_APP_ID, appid.c_str()), PMINFO_R_OK);
204   ASSERT_EQ(pkgmgrinfo_appinfo_filter_add_bool(filter,
205       PMINFO_APPINFO_PROP_APP_DISABLE, false), PMINFO_R_OK);
206
207   AppInfoDBHandlerMock appinfo_db_handler(0, 0);
208   appinfo_db_handler.SetFilter(filter_ptr.get());
209
210   EXPECT_CALL(appinfo_db_handler, Connect()).WillOnce(testing::Return(true));
211   EXPECT_CALL(appinfo_db_handler, GetConnection())
212       .WillOnce(testing::Return(GetDBHandles()));
213   appinfo_db_handler.SetLocale("test_lang");
214   ASSERT_EQ(appinfo_db_handler.Execute(), 0);
215
216   auto lappinfo_list = appinfo_db_handler.GetAppHandle();
217   ASSERT_EQ(lappinfo_list.size(), 1);
218
219   auto test_appinfo = GetTestApplication(appid);
220   std::vector<application_x*> rappinfo_list;
221   rappinfo_list.emplace_back(test_appinfo);
222
223   ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
224 }