2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
23 #include "appinfo_db_handler.hh"
24 #include "create_db_handler.hh"
25 #include "depinfo_db_handler.hh"
27 #include "parcel_utils.hh"
28 #include "pkg_get_db_handler.hh"
29 #include "pkg_set_db_handler.hh"
31 #include "mock/file_mock.h"
32 #include "mock/test_fixture.h"
33 #include "mock/system_info_mock.h"
35 #include "pkgmgr-info.h"
36 #include "pkgmgrinfo_basic.h"
38 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
40 namespace psd = pkgmgr_server::database;
42 class CreateParserDBHandlerMock : public psd::CreateDBHandler {
44 CreateParserDBHandlerMock(uid_t uid, int pid)
45 : psd::CreateDBHandler(uid, pid) {}
47 MOCK_METHOD0(Connect, bool());
48 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
51 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
53 PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
55 MOCK_METHOD0(Connect, bool());
56 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
59 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
61 PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
63 MOCK_METHOD0(Connect, bool());
64 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
67 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
69 AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
71 MOCK_METHOD0(Connect, bool());
72 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
75 class DepInfoGetDBHandlerMock : public psd::DepInfoGetDBHandler {
77 DepInfoGetDBHandlerMock(uid_t uid, int pid) :
78 psd::DepInfoGetDBHandler(uid, pid) {}
80 MOCK_METHOD0(Connect, bool());
81 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
84 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
86 class ParserDBHandlerTest : public TestFixture {
88 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
89 virtual ~ParserDBHandlerTest() {}
91 virtual void SetUp() {
93 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
94 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
97 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
98 CreateParserDBHandlerMock create_db_handler(0, 0);
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);
113 virtual void TearDown() {
114 for (auto& handle : db_handles_)
115 sqlite3_close_v2(handle.first);
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);
123 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
128 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
129 db_handles_ = db_handles;
132 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
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());
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);
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());
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);
161 pkgmgrinfo_pkginfo_filter_h filter;
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);
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");
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);
182 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
183 ASSERT_EQ(lpkginfo_list.size(), 1);
185 auto test_pkginfo = GetTestPackage(pkgid);
186 std::vector<package_x*> rpkginfo_list;
187 rpkginfo_list.emplace_back(test_pkginfo);
189 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
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());
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);
204 pkgmgrinfo_appinfo_filter_h filter;
205 std::string appid = "test_app1";
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);
217 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
218 appinfo_db_handler.SetFilter(filter_ptr.get());
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);
226 auto lappinfo_list = appinfo_db_handler.GetAppHandle();
227 ASSERT_EQ(lappinfo_list.size(), 1);
229 auto test_appinfo = GetTestApplication(appid);
230 std::vector<application_x*> rappinfo_list;
231 rappinfo_list.emplace_back(test_appinfo);
233 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);
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());
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);
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);
256 auto depinfo_from_db = depinfo_get_db_handler.GetDependencyList();
257 ASSERT_EQ(depinfo_from_db.size(), 1);
259 std::vector<dependency_x*> depinfo_list;
260 depinfo_list.emplace_back(GetTestDepInfo("test_package"));
262 EXPECT_TRUE(IsEqualDepInfo(depinfo_list, depinfo_from_db));