Implement certinfo unit tests
authorJunghyun Yeon <jungh.yeon@samsung.com>
Wed, 2 Jun 2021 07:01:01 +0000 (16:01 +0900)
committer연정현/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <jungh.yeon@samsung.com>
Thu, 3 Jun 2021 10:05:07 +0000 (19:05 +0900)
Implement certinfo unittest and separate
structure comparsion function and retrieved one from database.

Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
test/unit_tests/parcel_utils.cc
test/unit_tests/parcel_utils.hh
test/unit_tests/test_cert_db_handlers.cc [new file with mode: 0644]
test/unit_tests/test_parcel.cc

index 21460d2..706a1c7 100644 (file)
@@ -793,11 +793,19 @@ pkgmgr_certinfo_x* GetTestCertInfo() {
   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)
@@ -835,7 +843,7 @@ bool IsEqualDepInfo(const std::vector<dependency_x*>& depA,
   return true;
 }
 
-pkgmgr_certinfo_x *GetTestCertificate() {
+pkgmgr_certinfo_xGetTestCertificate() {
   pkgmgr_certinfo_x* cert_info = reinterpret_cast<pkgmgr_certinfo_x*>(
       calloc(1, sizeof(pkgmgr_certinfo_x)));
 
@@ -850,4 +858,4 @@ pkgmgr_certinfo_x *GetTestCertificate() {
   }
 
   return cert_info;
-}
\ No newline at end of file
+}
index eb7a925..4f6d289 100644 (file)
@@ -45,9 +45,12 @@ pkgmgr_certinfo_x *GetTestCertInfo();
 bool IsEqualCertInfo(const pkgmgr_certinfo_x *certA,
     const pkgmgr_certinfo_x *certB);
 
+bool IsEqualCertInfoStructure(const pkgmgr_certinfo_x *certA,
+    const pkgmgr_certinfo_x *certB);
+
 dependency_x *GetTestDepInfo(std::string pkgid);
 
 bool IsEqualDepInfo(const std::vector<dependency_x *>& depA,
     const std::vector<dependency_x *>& depB);
 
-pkgmgr_certinfo_x *GetTestCertificate();
\ No newline at end of file
+pkgmgr_certinfo_x *GetTestCertificate();
diff --git a/test/unit_tests/test_cert_db_handlers.cc b/test/unit_tests/test_cert_db_handlers.cc
new file mode 100644 (file)
index 0000000..82b0426
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * 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);
+}
index 97a2739..fce46d3 100644 (file)
@@ -158,7 +158,7 @@ TEST_F(ParcelTest, CertInfoParcelable) {
   parcel.WriteParcelable(origin_parcelable);
   parcel.ReadParcelable(&new_parcelable);
 
-  EXPECT_TRUE(IsEqualCertInfo(origin_parcelable.GetCertInfo(),
+  EXPECT_TRUE(IsEqualCertInfoStructure(origin_parcelable.GetCertInfo(),
                               new_parcelable.GetCertInfo()));
   EXPECT_EQ(origin_parcelable.GetPkgId(), new_parcelable.GetPkgId());
 }