8fc35ed7ac81af76ff07e93b31422dbda0e396b6
[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
34 namespace psd = pkgmgr_server::database;
35
36 class CreateCertDBHandlerMock : public psd::CreateDBHandler {
37  public:
38   CreateCertDBHandlerMock(uid_t uid, int pid)
39       : psd::CreateDBHandler(uid, pid) {}
40
41   MOCK_METHOD0(Connect, bool());
42   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
43       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, const std::vector<std::pair<tizen_base::Database,
52       uid_t>>&());
53 };
54
55 class CertGetDBHandlerMock : public psd::CertGetDBHandler {
56  public:
57   CertGetDBHandlerMock(uid_t uid, int pid) : psd::CertGetDBHandler(uid, pid) {}
58
59   MOCK_METHOD0(Connect, bool());
60   MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
61       uid_t>>&());
62 };
63
64 class CertDBHandlerTest : public ::testing::Test {
65  public:
66   virtual void SetUp() {
67     tizen_base::Database db(TEST_CERT_DB, SQLITE_OPEN_READWRITE |
68         SQLITE_OPEN_CREATE);
69     db_handles_.emplace_back(std::move(db), 0);
70     CreateCertDBHandlerMock create_db_handler(0, 0);
71
72     EXPECT_CALL(create_db_handler, Connect())
73         .Times(2).WillRepeatedly(testing::Return(true));
74     EXPECT_CALL(create_db_handler, GetConnection())
75         .Times(2).WillRepeatedly(testing::ReturnRef(db_handles_));
76
77     MakeVersionFile();
78     pkgmgr_server::internal::SetEnableUnitTest(true);
79     ASSERT_EQ(create_db_handler.Execute(), 0);
80     pkgmgr_server::internal::SetEnableUnitTest(false);
81   }
82
83   virtual void TearDown() {
84     db_handles_.clear();
85     ASSERT_EQ(remove(TEST_CERT_DB), 0);
86     std::string journal_path(TEST_CERT_DB);
87     journal_path += "-journal";
88     ASSERT_EQ(remove(journal_path.c_str()), 0);
89   }
90
91  private:
92   void MakeVersionFile() {
93     std::remove("./pkg_db_version.txt");
94     std::ofstream ofs("./pkg_db_version.txt");
95     ofs << "30005";
96   }
97
98  protected:
99   std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
100 };
101
102 TEST_F(CertDBHandlerTest, CertDBHandlerTest) {
103     CertSetDBHandlerMock cert_set_db_handler(0, 0);
104     std::unique_ptr<pkgmgr_certinfo_x,
105         decltype(pkgmgrinfo_destroy_certinfo_set_handle)*> ptr(
106             GetTestCertificate(), pkgmgrinfo_destroy_certinfo_set_handle);
107   cert_set_db_handler.SetCertHandle(ptr.get());
108
109   EXPECT_CALL(cert_set_db_handler, Connect()).WillOnce(testing::Return(true));
110   EXPECT_CALL(cert_set_db_handler, GetConnection())
111       .WillOnce(testing::ReturnRef(db_handles_));
112   ASSERT_EQ(cert_set_db_handler.Execute(), 0);
113
114   CertGetDBHandlerMock cert_get_db_handler(0, 0);
115   EXPECT_CALL(cert_get_db_handler, Connect()).WillOnce(testing::Return(true));
116   EXPECT_CALL(cert_get_db_handler, GetConnection())
117       .WillOnce(testing::ReturnRef(db_handles_));
118   cert_get_db_handler.SetPkgID("test_pkgid");
119   ASSERT_EQ(cert_get_db_handler.Execute(), PMINFO_R_OK);
120
121   auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
122   ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);
123 }