return cert_info;
}
-bool IsEqualCertInfo(const pkgmgr_certinfo_x* certA,
+bool IsEqualCertInfoStructure(const pkgmgr_certinfo_x* certA,
const pkgmgr_certinfo_x* certB) {
- INT_EQ(certA->for_all_users, certB->for_all_users);
+ if (!IsEqualCertInfo(certA, certB))
+ return false;
+
STR_EQ(certA->pkgid, certB->pkgid);
STR_EQ(certA->cert_value, certB->cert_value);
+ INT_EQ(certA->for_all_users, certB->for_all_users);
+ return true;
+}
+
+bool IsEqualCertInfo(const pkgmgr_certinfo_x* certA,
+ const pkgmgr_certinfo_x* certB) {
for (int i = 0; i < MAX_CERT_TYPE; ++i)
STR_EQ(certA->cert_info[i], certB->cert_info[i]);
for (int i = 0; i < MAX_CERT_TYPE; ++i)
return true;
}
-pkgmgr_certinfo_x *GetTestCertificate() {
+pkgmgr_certinfo_x* GetTestCertificate() {
pkgmgr_certinfo_x* cert_info = reinterpret_cast<pkgmgr_certinfo_x*>(
calloc(1, sizeof(pkgmgr_certinfo_x)));
}
return cert_info;
-}
\ No newline at end of file
+}
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gmock/gmock.h>
+
+#include <gtest/gtest.h>
+
+#include <cstdio>
+
+#include "cert_set_db_handler.hh"
+#include "cert_get_db_handler.hh"
+#include "create_db_handler.hh"
+#include "db_type.hh"
+#include "mock/file_mock.h"
+#include "parcel_utils.hh"
+
+#include "pkgmgr-info.h"
+#include "pkgmgrinfo_basic.h"
+
+#define TEST_CERT_DB "test.pkgmgr_cert.db"
+
+class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
+ public:
+ CreateDBHandlerMock(uid_t uid, int pid)
+ : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
+
+ MOCK_METHOD0(Connect, bool());
+ MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+};
+
+class CertSetDBHandlerMock : public pkgmgr_server::database::CertSetDBHandler {
+ public:
+ CertSetDBHandlerMock(uid_t uid, int pid)
+ : pkgmgr_server::database::CertSetDBHandler(uid, pid) {}
+
+ MOCK_METHOD0(Connect, bool());
+ MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+};
+
+class CertGetDBHandlerMock : public pkgmgr_server::database::CertGetDBHandler {
+ public:
+ CertGetDBHandlerMock(uid_t uid, int pid)
+ : pkgmgr_server::database::CertGetDBHandler(uid, pid) {}
+
+ MOCK_METHOD0(Connect, bool());
+ MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+};
+
+class CertDBHandlerTest : public ::testing::Test {
+ public:
+ virtual void SetUp() {
+ sqlite3 *db;
+ ASSERT_EQ(sqlite3_open_v2(TEST_CERT_DB, &db,
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
+
+ SetDBHandles(
+ std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
+ CreateDBHandlerMock create_db_handler(0, 0);
+
+ EXPECT_CALL(create_db_handler, Connect())
+ .Times(2).WillRepeatedly(testing::Return(true));
+ EXPECT_CALL(create_db_handler, GetConnection())
+ .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
+
+ fopen_mock_setup(true);
+ ASSERT_EQ(create_db_handler.Execute(), 0);
+ fopen_mock_setup(false);
+ }
+
+ virtual void TearDown() {
+ for (auto& handle : db_handles_)
+ sqlite3_close_v2(handle.first);
+
+ ASSERT_EQ(remove(TEST_CERT_DB), 0);
+ std::string journal_path(TEST_CERT_DB);
+ journal_path += "-journal";
+ ASSERT_EQ(remove(journal_path.c_str()), 0);
+ }
+
+ const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
+ return db_handles_;
+ }
+
+ private:
+ void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>> db_handles) {
+ db_handles_ = std::move(db_handles);
+ }
+
+ std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
+};
+
+TEST_F(CertDBHandlerTest, CertDBHandlerTest) {
+ CertSetDBHandlerMock cert_set_db_handler(0, 0);
+ std::unique_ptr<pkgmgr_certinfo_x,
+ decltype(pkgmgrinfo_destroy_certinfo_set_handle)*> ptr(
+ GetTestCertificate(), pkgmgrinfo_destroy_certinfo_set_handle);
+ cert_set_db_handler.SetCertHandle(ptr.get());
+
+ EXPECT_CALL(cert_set_db_handler, Connect()).WillOnce(testing::Return(true));
+ EXPECT_CALL(cert_set_db_handler, GetConnection())
+ .WillOnce(testing::Return(GetDBHandles()));
+ ASSERT_EQ(cert_set_db_handler.Execute(), 0);
+
+ CertGetDBHandlerMock cert_get_db_handler(0, 0);
+ EXPECT_CALL(cert_get_db_handler, Connect()).WillOnce(testing::Return(true));
+ EXPECT_CALL(cert_get_db_handler, GetConnection())
+ .WillOnce(testing::Return(GetDBHandles()));
+ cert_get_db_handler.SetPkgID("test_pkgid");
+ ASSERT_EQ(cert_get_db_handler.Execute(), PMINFO_R_OK);
+
+ auto certinfo_from_db = cert_get_db_handler.GetCertHandle();
+ ASSERT_EQ(IsEqualCertInfo(ptr.get(), certinfo_from_db), true);
+}