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>
19 #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 "mock/file_mock.h"
28 #include "parcel_utils.hh"
30 #include "pkgmgr-info.h"
31 #include "pkgmgrinfo_basic.h"
33 #define TEST_CERT_DB "test.pkgmgr_cert.db"
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, std::vector<std::pair<sqlite3*, uid_t>>());
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, std::vector<std::pair<sqlite3*, uid_t>>());
54 class CertGetDBHandlerMock : public psd::CertGetDBHandler {
56 CertGetDBHandlerMock(uid_t uid, int pid) : psd::CertGetDBHandler(uid, pid) {}
58 MOCK_METHOD0(Connect, bool());
59 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
62 class CertDBHandlerTest : public ::testing::Test {
64 virtual void SetUp() {
67 int ret = sqlite3_open_v2(TEST_CERT_DB, &db,
68 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
72 ASSERT_EQ(ret, SQLITE_OK);
75 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
76 CreateCertDBHandlerMock create_db_handler(0, 0);
78 EXPECT_CALL(create_db_handler, Connect())
79 .Times(2).WillRepeatedly(testing::Return(true));
80 EXPECT_CALL(create_db_handler, GetConnection())
81 .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
83 fopen_mock_setup(true);
84 ASSERT_EQ(create_db_handler.Execute(), 0);
85 fopen_mock_setup(false);
88 virtual void TearDown() {
89 for (auto& handle : db_handles_)
90 sqlite3_close_v2(handle.first);
92 ASSERT_EQ(remove(TEST_CERT_DB), 0);
93 std::string journal_path(TEST_CERT_DB);
94 journal_path += "-journal";
95 ASSERT_EQ(remove(journal_path.c_str()), 0);
98 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
103 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>> db_handles) {
104 db_handles_ = std::move(db_handles);
107 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
110 TEST_F(CertDBHandlerTest, CertDBHandlerTest) {
111 CertSetDBHandlerMock cert_set_db_handler(0, 0);
112 std::unique_ptr<pkgmgr_certinfo_x,
113 decltype(pkgmgrinfo_destroy_certinfo_set_handle)*> ptr(
114 GetTestCertificate(), pkgmgrinfo_destroy_certinfo_set_handle);
115 cert_set_db_handler.SetCertHandle(ptr.get());
117 EXPECT_CALL(cert_set_db_handler, Connect()).WillOnce(testing::Return(true));
118 EXPECT_CALL(cert_set_db_handler, GetConnection())
119 .WillOnce(testing::Return(GetDBHandles()));
120 ASSERT_EQ(cert_set_db_handler.Execute(), 0);
122 CertGetDBHandlerMock cert_get_db_handler(0, 0);
123 EXPECT_CALL(cert_get_db_handler, Connect()).WillOnce(testing::Return(true));
124 EXPECT_CALL(cert_get_db_handler, GetConnection())
125 .WillOnce(testing::Return(GetDBHandles()));
126 cert_get_db_handler.SetPkgID("test_pkgid");
127 ASSERT_EQ(cert_get_db_handler.Execute(), PMINFO_R_OK);
129 auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
130 ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);