2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
23 #include "cert_set_db_handler.hh"
24 #include "cert_get_db_handler.hh"
25 #include "create_db_handler.hh"
27 #include "parcel_utils.hh"
28 #include "pkgmgr-info.h"
29 #include "pkgmgrinfo_basic.h"
30 #include "server/pkgmgrinfo_internal.h"
32 #define TEST_CERT_DB "test.pkgmgr_cert.db"
34 namespace psd = pkgmgr_server::database;
36 class CreateCertDBHandlerMock : public psd::CreateDBHandler {
38 CreateCertDBHandlerMock(uid_t uid, int pid)
39 : psd::CreateDBHandler(uid, pid) {}
41 MOCK_METHOD0(Connect, bool());
42 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
46 class CertSetDBHandlerMock : public psd::CertSetDBHandler {
48 CertSetDBHandlerMock(uid_t uid, int pid) : psd::CertSetDBHandler(uid, pid) {}
50 MOCK_METHOD0(Connect, bool());
51 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
55 class CertGetDBHandlerMock : public psd::CertGetDBHandler {
57 CertGetDBHandlerMock(uid_t uid, int pid) : psd::CertGetDBHandler(uid, pid) {}
59 MOCK_METHOD0(Connect, bool());
60 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
64 class CertDBHandlerTest : public ::testing::Test {
66 virtual void SetUp() {
67 tizen_base::Database db(TEST_CERT_DB, SQLITE_OPEN_READWRITE |
69 db_handles_.emplace_back(std::move(db), 0);
70 CreateCertDBHandlerMock create_db_handler(0, 0);
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_));
78 pkgmgr_server::internal::SetEnableUnitTest(true);
79 ASSERT_EQ(create_db_handler.Execute(), 0);
80 pkgmgr_server::internal::SetEnableUnitTest(false);
83 virtual void TearDown() {
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);
92 void MakeVersionFile() {
93 std::remove("./pkg_db_version.txt");
94 std::ofstream ofs("./pkg_db_version.txt");
99 std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
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());
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);
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);
121 auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
122 ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);