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"
33 #define DB_VERSION_FILE "./pkg_db_version.txt"
35 namespace psd = pkgmgr_server::database;
37 class CreateCertDBHandlerMock : public psd::CreateDBHandler {
39 CreateCertDBHandlerMock(uid_t uid, int pid)
40 : psd::CreateDBHandler(uid, pid) {}
42 MOCK_METHOD0(Connect, bool());
43 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
47 class CertSetDBHandlerMock : public psd::CertSetDBHandler {
49 CertSetDBHandlerMock(uid_t uid, int pid) : psd::CertSetDBHandler(uid, pid) {}
51 MOCK_METHOD0(Connect, bool());
52 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
56 class CertGetDBHandlerMock : public psd::CertGetDBHandler {
58 CertGetDBHandlerMock(uid_t uid, int pid) : psd::CertGetDBHandler(uid, pid) {}
60 MOCK_METHOD0(Connect, bool());
61 MOCK_METHOD0(GetConnection, const std::vector<std::pair<tizen_base::Database,
65 class CertDBHandlerTest : public ::testing::Test {
67 virtual void SetUp() {
68 tizen_base::Database db(TEST_CERT_DB, SQLITE_OPEN_READWRITE |
70 db_handles_.emplace_back(std::move(db), 0);
71 CreateCertDBHandlerMock create_db_handler(0, 0);
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_));
79 pkgmgr_server::internal::SetEnableUnitTest(true);
80 ASSERT_EQ(create_db_handler.Execute(), 0);
81 pkgmgr_server::internal::SetEnableUnitTest(false);
84 virtual void TearDown() {
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);
94 void MakeVersionFile() {
95 std::ofstream ofs(DB_VERSION_FILE);
100 std::vector<std::pair<tizen_base::Database, uid_t>> db_handles_;
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());
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);
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);
122 auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
123 ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);