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