Fix coding rule
[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     ASSERT_EQ(sqlite3_open_v2(TEST_CERT_DB, &db,
67         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
68
69     SetDBHandles(
70         std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(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::Return(GetDBHandles()));
77
78     fopen_mock_setup(true);
79     ASSERT_EQ(create_db_handler.Execute(), 0);
80     fopen_mock_setup(false);
81   }
82
83   virtual void TearDown() {
84     for (auto& handle : db_handles_)
85       sqlite3_close_v2(handle.first);
86
87     ASSERT_EQ(remove(TEST_CERT_DB), 0);
88     std::string journal_path(TEST_CERT_DB);
89     journal_path += "-journal";
90     ASSERT_EQ(remove(journal_path.c_str()), 0);
91   }
92
93   const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
94     return db_handles_;
95   }
96
97  private:
98   void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>> db_handles) {
99     db_handles_ = std::move(db_handles);
100   }
101
102   std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
103 };
104
105 TEST_F(CertDBHandlerTest, CertDBHandlerTest) {
106     CertSetDBHandlerMock cert_set_db_handler(0, 0);
107     std::unique_ptr<pkgmgr_certinfo_x,
108         decltype(pkgmgrinfo_destroy_certinfo_set_handle)*> ptr(
109             GetTestCertificate(), pkgmgrinfo_destroy_certinfo_set_handle);
110   cert_set_db_handler.SetCertHandle(ptr.get());
111
112   EXPECT_CALL(cert_set_db_handler, Connect()).WillOnce(testing::Return(true));
113   EXPECT_CALL(cert_set_db_handler, GetConnection())
114       .WillOnce(testing::Return(GetDBHandles()));
115   ASSERT_EQ(cert_set_db_handler.Execute(), 0);
116
117   CertGetDBHandlerMock cert_get_db_handler(0, 0);
118   EXPECT_CALL(cert_get_db_handler, Connect()).WillOnce(testing::Return(true));
119   EXPECT_CALL(cert_get_db_handler, GetConnection())
120       .WillOnce(testing::Return(GetDBHandles()));
121   cert_get_db_handler.SetPkgID("test_pkgid");
122   ASSERT_EQ(cert_get_db_handler.Execute(), PMINFO_R_OK);
123
124   auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
125   ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);
126 }