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 class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
37 CreateDBHandlerMock(uid_t uid, int pid)
38 : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
40 MOCK_METHOD0(Connect, bool());
41 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
44 class CertSetDBHandlerMock : public pkgmgr_server::database::CertSetDBHandler {
46 CertSetDBHandlerMock(uid_t uid, int pid)
47 : pkgmgr_server::database::CertSetDBHandler(uid, pid) {}
49 MOCK_METHOD0(Connect, bool());
50 MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
53 class CertGetDBHandlerMock : public pkgmgr_server::database::CertGetDBHandler {
55 CertGetDBHandlerMock(uid_t uid, int pid)
56 : pkgmgr_server::database::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() {
66 ASSERT_EQ(sqlite3_open_v2(TEST_CERT_DB, &db,
67 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
70 std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
71 CreateDBHandlerMock 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::Return(GetDBHandles()));
78 fopen_mock_setup(true);
79 ASSERT_EQ(create_db_handler.Execute(), 0);
80 fopen_mock_setup(false);
83 virtual void TearDown() {
84 for (auto& handle : db_handles_)
85 sqlite3_close_v2(handle.first);
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);
93 const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
98 void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>> db_handles) {
99 db_handles_ = std::move(db_handles);
102 std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
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());
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);
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);
124 auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
125 ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);