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