Fix static analysis issues
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / test_cert_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 "cert_set_db_handler.hh"
24 #include "cert_get_db_handler.hh"
25 #include "create_db_handler.hh"
26 #include "db_type.hh"
27 #include "mock/file_mock.h"
28 #include "parcel_utils.hh"
29
30 #include "pkgmgr-info.h"
31 #include "pkgmgrinfo_basic.h"
32
33 #define TEST_CERT_DB "test.pkgmgr_cert.db"
34
35 namespace psd = pkgmgr_server::database;
36
37 class CreateCertDBHandlerMock : public psd::CreateDBHandler {
38  public:
39   CreateCertDBHandlerMock(uid_t uid, int pid)
40       : psd::CreateDBHandler(uid, pid) {}
41
42   MOCK_METHOD0(Connect, bool());
43   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
44 };
45
46 class CertSetDBHandlerMock : public psd::CertSetDBHandler {
47  public:
48   CertSetDBHandlerMock(uid_t uid, int pid) : psd::CertSetDBHandler(uid, pid) {}
49
50   MOCK_METHOD0(Connect, bool());
51   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
52 };
53
54 class CertGetDBHandlerMock : public psd::CertGetDBHandler {
55  public:
56   CertGetDBHandlerMock(uid_t uid, int pid) : psd::CertGetDBHandler(uid, pid) {}
57
58   MOCK_METHOD0(Connect, bool());
59   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
60 };
61
62 class CertDBHandlerTest : public ::testing::Test {
63  public:
64   virtual void SetUp() {
65     sqlite3 *db;
66
67     int ret = sqlite3_open_v2(TEST_CERT_DB, &db,
68         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
69     if (ret != SQLITE_OK)
70       sqlite3_close_v2(db);
71
72     ASSERT_EQ(ret, SQLITE_OK);
73
74     SetDBHandles(
75         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
76     CreateCertDBHandlerMock create_db_handler(0, 0);
77
78     EXPECT_CALL(create_db_handler, Connect())
79         .Times(2).WillRepeatedly(testing::Return(true));
80     EXPECT_CALL(create_db_handler, GetConnection())
81         .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
82
83     fopen_mock_setup(true);
84     ASSERT_EQ(create_db_handler.Execute(), 0);
85     fopen_mock_setup(false);
86   }
87
88   virtual void TearDown() {
89     for (auto& handle : db_handles_)
90       sqlite3_close_v2(handle.first);
91
92     ASSERT_EQ(remove(TEST_CERT_DB), 0);
93     std::string journal_path(TEST_CERT_DB);
94     journal_path += "-journal";
95     ASSERT_EQ(remove(journal_path.c_str()), 0);
96   }
97
98   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
99     return db_handles_;
100   }
101
102  private:
103   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>> db_handles) {
104     db_handles_ = std::move(db_handles);
105   }
106
107   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
108 };
109
110 TEST_F(CertDBHandlerTest, CertDBHandlerTest) {
111     CertSetDBHandlerMock cert_set_db_handler(0, 0);
112     std::unique_ptr<pkgmgr_certinfo_x,
113         decltype(pkgmgrinfo_destroy_certinfo_set_handle)*> ptr(
114             GetTestCertificate(), pkgmgrinfo_destroy_certinfo_set_handle);
115   cert_set_db_handler.SetCertHandle(ptr.get());
116
117   EXPECT_CALL(cert_set_db_handler, Connect()).WillOnce(testing::Return(true));
118   EXPECT_CALL(cert_set_db_handler, GetConnection())
119       .WillOnce(testing::Return(GetDBHandles()));
120   ASSERT_EQ(cert_set_db_handler.Execute(), 0);
121
122   CertGetDBHandlerMock cert_get_db_handler(0, 0);
123   EXPECT_CALL(cert_get_db_handler, Connect()).WillOnce(testing::Return(true));
124   EXPECT_CALL(cert_get_db_handler, GetConnection())
125       .WillOnce(testing::Return(GetDBHandles()));
126   cert_get_db_handler.SetPkgID("test_pkgid");
127   ASSERT_EQ(cert_get_db_handler.Execute(), PMINFO_R_OK);
128
129   auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
130   ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);
131 }