Implement PkgGetDBHandler unit test
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / test_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 "create_db_handler.hh"
24 #include "db_type.hh"
25 #include "parcel_utils.hh"
26 #include "pkg_get_db_handler.hh"
27 #include "pkg_set_db_handler.hh"
28
29 #include "pkgmgr-info.h"
30 #include "pkgmgrinfo_basic.h"
31
32 #define TEST_PARSER_DB "test.pkgmgr_parser.db"
33
34 class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
35  public:
36   CreateDBHandlerMock(uid_t uid, int pid)
37       : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
38
39   MOCK_METHOD0(Connect, bool());
40   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
41   MOCK_METHOD1(SetDBVersion, int(sqlite3 *conn));
42 };
43
44 class PkgSetDBHandlerMock : public pkgmgr_server::database::PkgSetDBHandler {
45  public:
46   PkgSetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgSetDBHandler(uid, pid) {}
47
48   MOCK_METHOD0(Connect, bool());
49   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
50 };
51
52 class PkgGetDBHandlerMock : public pkgmgr_server::database::PkgGetDBHandler {
53  public:
54   PkgGetDBHandlerMock(uid_t uid, int pid) : pkgmgr_server::database::PkgGetDBHandler(uid, pid) {}
55
56   MOCK_METHOD0(Connect, bool());
57   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
58 };
59
60 class DBHandlerTest : public ::testing::Test {
61  public:
62   virtual void SetUp() {
63     sqlite3 *db;
64     ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
65         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
66
67     SetDBHandles(
68         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
69     CreateDBHandlerMock create_db_handler(0, 0);
70
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);
78   }
79
80   virtual void TearDown() {
81     for (auto& handle : db_handles_)
82       sqlite3_close_v2(handle.first);
83
84     ASSERT_EQ(remove(TEST_PARSER_DB), 0);
85   }
86
87   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
88     return db_handles_;
89   }
90
91  private:
92   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
93     db_handles_ = db_handles;
94   }
95
96   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
97 };
98
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());
105
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);
110 }
111
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());
119
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);
124
125   pkgmgrinfo_pkginfo_filter_h filter;
126
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);
136
137   PkgGetDBHandlerMock pkg_get_db_handler(0, 0);
138   pkg_get_db_handler.SetFilter(filter_ptr.get());
139
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);
144 }