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 class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
41 CreateDBHandlerMock(uid_t uid, int pid)
42 : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
44 MOCK_METHOD0(Connect, bool());
45 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
48 class PkgSetDBHandlerMock : public pkgmgr_server::database::PkgSetDBHandler {
50 PkgSetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgSetDBHandler(uid, pid) {}
52 MOCK_METHOD0(Connect, bool());
53 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
56 class PkgGetDBHandlerMock : public pkgmgr_server::database::PkgGetDBHandler {
58 PkgGetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgGetDBHandler(uid, pid) {}
60 MOCK_METHOD0(Connect, bool());
61 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
64 class AppInfoDBHandlerMock : public pkgmgr_server::database::AppInfoDBHandler {
66 AppInfoDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::AppInfoDBHandler(uid, pid) {}
68 MOCK_METHOD0(Connect, bool());
69 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
72 class Mocks : public ::testing::NiceMock<SystemInfoMock> {};
74 class DBHandlerTest : public TestFixture {
76 DBHandlerTest() : TestFixture(std::make_unique<Mocks>()) {}
77 virtual ~DBHandlerTest() {}
79 virtual void SetUp() {
81 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
82 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
85 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
86 CreateDBHandlerMock create_db_handler(0, 0);
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);
101 virtual void TearDown() {
102 for (auto& handle : db_handles_)
103 sqlite3_close_v2(handle.first);
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);
111 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
116 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
117 db_handles_ = db_handles;
120 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
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());
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);
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());
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);
149 pkgmgrinfo_pkginfo_filter_h filter;
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);
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");
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);
170 auto lpkginfo_list = pkg_get_db_handler.GetPkgHandle();
171 ASSERT_EQ(lpkginfo_list.size(), 1);
173 auto test_pkginfo = GetTestPackage(pkgid);
174 std::vector<package_x*> rpkginfo_list;
175 rpkginfo_list.emplace_back(test_pkginfo);
177 ASSERT_EQ(IsEqualPackagesInfo(lpkginfo_list, rpkginfo_list), true);
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());
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);
192 pkgmgrinfo_appinfo_filter_h filter;
193 std::string appid = "test_app1";
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);
205 AppInfoDBHandlerMock appinfo_db_handler(0, 0);
206 appinfo_db_handler.SetFilter(filter_ptr.get());
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);
214 auto lappinfo_list = appinfo_db_handler.GetAppHandle();
215 ASSERT_EQ(lappinfo_list.size(), 1);
217 auto test_appinfo = GetTestApplication(appid);
218 std::vector<application_x*> rappinfo_list;
219 rappinfo_list.emplace_back(test_appinfo);
221 ASSERT_EQ(IsEqualApplicationsInfo(lappinfo_list, rappinfo_list), true);