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 #include <gtest/gtest.h>
19
20 #include <cstdio>
21 #include <fstream>
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 "parcel_utils.hh"
28 #include "pkgmgr-info.h"
29 #include "pkgmgrinfo_basic.h"
30 #include "server/pkgmgrinfo_internal.h"
31
32 #define TEST_CERT_DB "test.pkgmgr_cert.db"
33 #define DB_VERSION_FILE "./pkg_db_version.txt"
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, const std::vector<std::pair<tizen_base::Database,
44       uid_t>>&());
45 };
46
47 class CertSetDBHandlerMock : public psd::CertSetDBHandler {
48  public:
49   CertSetDBHandlerMock(uid_t uid, int pid) : psd::CertSetDBHandler(uid, pid) {}
50
51   MOCK_METHOD0(Connect, bool());
52   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
53       uid_t>>&());
54 };
55
56 class CertGetDBHandlerMock : public psd::CertGetDBHandler {
57  public:
58   CertGetDBHandlerMock(uid_t uid, int pid) : psd::CertGetDBHandler(uid, pid) {}
59
60   MOCK_METHOD0(Connect, bool());
61   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
62       uid_t>>&());
63 };
64
65 class CertDBHandlerTest : public ::testing::Test {
66  public:
67   virtual void SetUp() {
68     tizen_base::Database db(TEST_CERT_DB, SQLITE_OPEN_READWRITE |
69         SQLITE_OPEN_CREATE);
70     db_handles_.emplace_back(std::move(db), 0);
71     CreateCertDBHandlerMock create_db_handler(0, 0);
72
73     EXPECT_CALL(create_db_handler, Connect())
74         .Times(2).WillRepeatedly(testing::Return(true));
75     EXPECT_CALL(create_db_handler, GetConnection())
76         .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
77
78     MakeVersionFile();
79     pkgmgr_server::internal::SetEnableUnitTest(true);
80     ASSERT_EQ(create_db_handler.Execute(), 0);
81     pkgmgr_server::internal::SetEnableUnitTest(false);
82   }
83
84   virtual void TearDown() {
85     db_handles_.clear();
86     ASSERT_EQ(remove(TEST_CERT_DB), 0);
87     std::string journal_path(TEST_CERT_DB);
88     journal_path += "-journal";
89     ASSERT_EQ(remove(journal_path.c_str()), 0);
90     ASSERT_EQ(remove(DB_VERSION_FILE), 0);
91   }
92
93  private:
94   void MakeVersionFile() {
95     std::ofstream ofs(DB_VERSION_FILE);
96     ofs << "30005";
97   }
98
99  protected:
100   std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
101 };
102
103 TEST_F(CertDBHandlerTest, CertDBHandlerTest) {
104     CertSetDBHandlerMock cert_set_db_handler(0, 0);
105     std::unique_ptr<pkgmgr_certinfo_x,
106         decltype(pkgmgrinfo_destroy_certinfo_set_handle)*> ptr(
107             GetTestCertificate(), pkgmgrinfo_destroy_certinfo_set_handle);
108   cert_set_db_handler.SetCertHandle(ptr.get());
109
110   EXPECT_CALL(cert_set_db_handler, Connect()).WillOnce(testing::Return(true));
111   EXPECT_CALL(cert_set_db_handler, GetConnection())
112       .WillOnce(testing::ReturnRef(db_handles_));
113   ASSERT_EQ(cert_set_db_handler.Execute(), 0);
114
115   CertGetDBHandlerMock cert_get_db_handler(0, 0);
116   EXPECT_CALL(cert_get_db_handler, Connect()).WillOnce(testing::Return(true));
117   EXPECT_CALL(cert_get_db_handler, GetConnection())
118       .WillOnce(testing::ReturnRef(db_handles_));
119   cert_get_db_handler.SetPkgID("test_pkgid");
120   ASSERT_EQ(cert_get_db_handler.Execute(), PMINFO_R_OK);
121
122   auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
123   ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);
124 }