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 "create_db_handler.hh"
25 #include "parcel_utils.hh"
26 #include "pkg_get_db_handler.hh"
27 #include "pkg_set_db_handler.hh"
29 #include "pkgmgr-info.h"
30 #include "pkgmgrinfo_basic.h"
32 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
34 class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
36 CreateDBHandlerMock(uid_t uid, int pid)
37 : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
39 MOCK_METHOD0(Connect, bool());
40 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
41 MOCK_METHOD1(SetDBVersion, int(sqlite3 *conn));
44 class PkgSetDBHandlerMock : public pkgmgr_server::database::PkgSetDBHandler {
46 PkgSetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgSetDBHandler(uid, pid) {}
48 MOCK_METHOD0(Connect, bool());
49 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
52 class PkgGetDBHandlerMock : public pkgmgr_server::database::PkgGetDBHandler {
54 PkgGetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgGetDBHandler(uid, pid) {}
56 MOCK_METHOD0(Connect, bool());
57 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
60 class DBHandlerTest : public ::testing::Test {
62 virtual void SetUp() {
64 ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
65 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
68 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
69 CreateDBHandlerMock create_db_handler(0, 0);
71 EXPECT_CALL(create_db_handler, Connect())
72 .Times(2).WillRepeatedly(testing::Return(true));
73 EXPECT_CALL(create_db_handler, GetConnection())
74 .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
75 EXPECT_CALL(create_db_handler, SetDBVersion(testing::_))
76 .Times(2).WillRepeatedly(testing::Return(0));
77 ASSERT_EQ(create_db_handler.Execute(), 0);
80 virtual void TearDown() {
81 for (auto& handle : db_handles_)
82 sqlite3_close_v2(handle.first);
84 ASSERT_EQ(remove(TEST_PARSER_DB), 0);
87 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
92 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
93 db_handles_ = db_handles;
96 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
99 TEST_F(DBHandlerTest, PkgSetDBHandlerTest) {
100 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
101 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
102 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
103 GetTestPackage("pkgA"), pkgmgrinfo_basic_free_package);
104 pkg_set_db_handler.SetPkgInfo(ptr.get());
106 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
107 EXPECT_CALL(pkg_set_db_handler, GetConnection())
108 .WillOnce(testing::Return(GetDBHandles()));
109 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
112 TEST_F(DBHandlerTest, PkgGetDBHandlerTest) {
113 std::string pkgid = "pkgA";
114 PkgSetDBHandlerMock pkg_set_db_handler(0, 0);
115 pkg_set_db_handler.SetWriteType(pkgmgr_common::PkgWriteType::Insert);
116 std::unique_ptr<package_x, decltype(pkgmgrinfo_basic_free_package)*> ptr(
117 GetTestPackage(pkgid), pkgmgrinfo_basic_free_package);
118 pkg_set_db_handler.SetPkgInfo(ptr.get());
120 EXPECT_CALL(pkg_set_db_handler, Connect()).WillOnce(testing::Return(true));
121 EXPECT_CALL(pkg_set_db_handler, GetConnection())
122 .WillOnce(testing::Return(GetDBHandles()));
123 ASSERT_EQ(pkg_set_db_handler.Execute(), 0);
125 pkgmgrinfo_pkginfo_filter_h filter;
127 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_create(&filter), PMINFO_R_OK);
128 std::unique_ptr<pkgmgrinfo_filter_x, decltype(
129 pkgmgrinfo_pkginfo_filter_destroy)*> filter_ptr(
130 reinterpret_cast<pkgmgrinfo_filter_x *>(filter),
131 pkgmgrinfo_pkginfo_filter_destroy);
132 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_string(filter,
133 PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgid.c_str()), PMINFO_R_OK);
134 ASSERT_EQ(pkgmgrinfo_pkginfo_filter_add_bool(filter,
135 PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false), PMINFO_R_OK);
137 PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
138 pkg_get_db_handler.SetFilter(filter_ptr.get());
140 EXPECT_CALL(pkg_get_db_handler, Connect()).WillOnce(testing::Return(true));
141 EXPECT_CALL(pkg_get_db_handler, GetConnection())
142 .WillOnce(testing::Return(GetDBHandles()));
143 ASSERT_EQ(pkg_get_db_handler.Execute(), 0);