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"
26 #include "parcel_utils.hh"
27 #include "pkg_get_db_handler.hh"
28 #include "pkg_set_db_handler.hh"
30 #include "mock/file_mock.h"
31 #include "mock/test_fixture.h"
32 #include "mock/system_info_mock.h"
34 #include "pkgmgr-info.h"
35 #include "pkgmgrinfo_basic.h"
37 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
39 namespace psd = pkgmgr_server::database;
41 class CreateDBHandlerMock : public psd::CreateDBHandler {
43 CreateDBHandlerMock(uid_t uid, int pid)
44 : psd::CreateDBHandler(uid, pid) {}
46 MOCK_METHOD0(Connect, bool());
47 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
50 class PkgSetDBHandlerMock : public psd::PkgSetDBHandler {
52 PkgSetDBHandlerMock(uid_t uid, int pid) : psd::PkgSetDBHandler(uid, pid) {}
54 MOCK_METHOD0(Connect, bool());
55 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
58 class PkgGetDBHandlerMock : public psd::PkgGetDBHandler {
60 PkgGetDBHandlerMock(uid_t uid, int pid) : psd::PkgGetDBHandler(uid, pid) {}
62 MOCK_METHOD0(Connect, bool());
63 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
66 class AppInfoDBHandlerMock : public psd::AppInfoDBHandler {
68 AppInfoDBHandlerMock(uid_t uid, int pid) : psd::AppInfoDBHandler(uid, pid) {}
70 MOCK_METHOD0(Connect, bool());
71 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
74 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
76 class ParserDBHandlerTest : public TestFixture {
78 ParserDBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
79 virtual ~ParserDBHandlerTest() {}
81 virtual void SetUp() {
83 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
84 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
87 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
88 CreateDBHandlerMock create_db_handler(0, 0);
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);
103 virtual void TearDown() {
104 for (auto& handle : db_handles_)
105 sqlite3_close_v2(handle.first);
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);
113 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
118 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
119 db_handles_ = db_handles;
122 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
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());
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);
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());
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);
151 pkgmgrinfo_pkginfo_filter_h filter;
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);
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");
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);
172 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
173 ASSERT_EQ(lpkginfo_list.size(), 1);
175 auto test_pkginfo = GetTestPackage(pkgid);
176 std::vector<package_x*> rpkginfo_list;
177 rpkginfo_list.emplace_back(test_pkginfo);
179 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
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());
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);
194 pkgmgrinfo_appinfo_filter_h filter;
195 std::string appid = "test_app1";
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);
207 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
208 appinfo_db_handler.SetFilter(filter_ptr.get());
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);
216 auto lappinfo_list = appinfo_db_handler.GetAppHandle();
217 ASSERT_EQ(lappinfo_list.size(), 1);
219 auto test_appinfo = GetTestApplication(appid);
220 std::vector<application_x*> rappinfo_list;
221 rappinfo_list.emplace_back(test_appinfo);
223 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);