Implement Empty DBHandler unit test
authorIlho Kim <ilho159.kim@samsung.com>
Fri, 16 Apr 2021 09:26:58 +0000 (18:26 +0900)
committer김일호/Tizen Platform Lab(SR)/Engineer/삼성전자 <ilho159.kim@samsung.com>
Fri, 7 May 2021 00:59:15 +0000 (09:59 +0900)
Change-Id: I2642b3e190720f7281304d47e4d51118a64631dd
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/server/database/abstract_db_handler.hh
src/server/database/create_db_handler.cc
src/server/database/create_db_handler.hh
src/server/initialize_db_internal.c
test/unit_tests/CMakeLists.txt
test/unit_tests/test_db_handlers.cc [new file with mode: 0644]

index 5554c01..cf3eb0c 100644 (file)
@@ -45,10 +45,10 @@ class EXPORT_API AbstractDBHandler {
   pkgmgr_common::DBOperationType GetOpType();
 
  protected:
-  bool Connect();
+  virtual bool Connect();
   int GetPID();
   uid_t GetUID();
-  std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
+  virtual std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
   void ClearDBHandle();
   std::string GetLocale();
   static std::shared_timed_mutex lock_;
index a123fb3..e776957 100644 (file)
@@ -30,6 +30,8 @@
 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
 #endif
 
+#define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
+
 namespace pkgmgr_server {
 namespace database {
 
@@ -60,6 +62,45 @@ int CreateDBHandler::Execute() {
   return PMINFO_R_OK;
 }
 
+int CreateDBHandler::SetDBVersion(sqlite3* conn) {
+  static const char query_raw[] = "PRAGMA user_version=%Q";
+  int ret;
+  FILE *fp = NULL;
+  char version[PKG_STRING_LEN_MAX] = { 0 };
+  char *query = NULL;
+
+  fp = fopen(DB_VERSION_PATH, "r");
+  if (fp == NULL) {
+    printf("Failed to open db version file %s\n", DB_VERSION_PATH);
+    printf("errno : %d\n", errno);
+    LOGE("Failed to open db version file");
+    return -1;
+  }
+
+  if (fgets(version, sizeof(version), fp) == NULL) {
+    LOGE("Failed to get version information");
+    fclose(fp);
+    return -1;
+  }
+  fclose(fp);
+
+  query = sqlite3_mprintf(query_raw, version);
+  if (!query) {
+    LOGE("Out of memory");
+    return -1;
+  }
+
+  ret = sqlite3_exec(conn, query, NULL, NULL, NULL);
+  if (ret != SQLITE_OK) {
+    LOGE("exec failed: %s", sqlite3_errmsg(conn));
+    sqlite3_free(query);
+    return -1;
+  }
+  sqlite3_free(query);
+
+  return 0;
+}
+
 int CreateDBHandler::CreateParserDB() {
   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
   ClearDBHandle();
@@ -71,6 +112,9 @@ int CreateDBHandler::CreateParserDB() {
   sqlite3* conn = conn_list.front().first;
   uid_t uid = conn_list.front().second;
 
+  if (SetDBVersion(conn) < 0)
+    return PMINFO_R_ERROR;
+
   return pkgmgr_parser_internal_initialize_db(conn, uid);
 }
 
@@ -85,6 +129,9 @@ int CreateDBHandler::CreateCertDB() {
   sqlite3* conn = conn_list.front().first;
   uid_t uid = conn_list.front().second;
 
+  if (SetDBVersion(conn) < 0)
+    return PMINFO_R_ERROR;
+
   return pkgmgr_parser_internal_initialize_db(conn, uid);
 }
 
index ff40360..cd02fc4 100644 (file)
@@ -43,6 +43,7 @@ class EXPORT_API CreateDBHandler : public AbstractDBHandler {
   std::vector<std::vector<std::string>> GetResult();
   int CreateParserDB();
   int CreateCertDB();
+  virtual int SetDBVersion(sqlite3* conn);
 
  private:
   std::vector<std::vector<std::string>> result_;
index 3c15ad2..04177d9 100644 (file)
@@ -51,8 +51,6 @@ do {                                                                           \
                _LOGD("chsmack -a %s %s", DB_LABEL, x);                        \
 } while (0)
 
-#define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
-
 static const char *__get_cert_db_path(void)
 {
        return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
@@ -139,43 +137,6 @@ static int __set_db_permission(const char *path, uid_t uid)
        return 0;
 }
 
-static int __set_db_version(sqlite3 *db) {
-       static const char query_raw[] = "PRAGMA user_version=%Q";
-       int ret;
-       FILE *fp = NULL;
-       char version[PKG_STRING_LEN_MAX] = { 0 };
-       char *query = NULL;
-
-       fp = fopen(DB_VERSION_PATH, "r");
-       if (fp == NULL) {
-               _LOGE("Failed to open db version file");
-               return -1;
-       }
-
-       if (fgets(version, sizeof(version), fp) == NULL) {
-               _LOGE("Failed to get version information");
-               fclose(fp);
-               return -1;
-       }
-       fclose(fp);
-
-       query = sqlite3_mprintf(query_raw, version);
-       if (!query) {
-               _LOGE("Out of memory");
-               return -1;
-       }
-
-       ret = sqlite3_exec(db, query, NULL, NULL, NULL);
-       if (ret != SQLITE_OK) {
-               _LOGE("exec failed: %s", sqlite3_errmsg(db));
-               sqlite3_free(query);
-               return -1;
-       }
-       sqlite3_free(query);
-
-       return 0;
-}
-
 static int __create_tables(sqlite3 *db, const char **queries)
 {
        int ret;
@@ -194,9 +155,6 @@ static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
 {
        const char **queries;
 
-       if (__set_db_version(db))
-               return -1;
-
        if (strstr(dbpath, ".pkgmgr_parser.db")) {
                queries = PARSER_INIT_QUERIES;
        } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
index 6b7f457..39a68b4 100644 (file)
@@ -22,6 +22,7 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -fpic -std=c++14")
 TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} ${libpkgs_LDFLAGS})
 TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} ${libpkgmgr-parser_LDFLAGS})
 TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PUBLIC pkgmgr-info)
+TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PUBLIC pkgmgr-info-server)
 SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE -fpic")
 SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie")
 set_target_properties(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES COMPILE_FLAGS "${unit_test_pkgs_CFLAGS_str}")
diff --git a/test/unit_tests/test_db_handlers.cc b/test/unit_tests/test_db_handlers.cc
new file mode 100644 (file)
index 0000000..0d9c0b3
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * 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 "db_type.hh"
+#include "create_db_handler.hh"
+#include "pkgmgrinfo_basic.h"
+
+#include "parcel_utils.hh"
+
+#define TEST_PARSER_DB "test.pkgmgr_parser.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>>());
+  MOCK_METHOD1(SetDBVersion, int(sqlite3 *conn));
+};
+
+class DBHandlerTest : public ::testing::Test {
+ public:
+  virtual void SetUp() {
+    sqlite3 *db;
+    ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_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 handler(0, 0);
+
+    EXPECT_CALL(handler, Connect())
+        .Times(2).WillRepeatedly(testing::Return(true));
+    EXPECT_CALL(handler, GetConnection())
+        .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
+    EXPECT_CALL(handler, SetDBVersion(testing::_))
+        .Times(2).WillRepeatedly(testing::Return(0));
+    ASSERT_EQ(handler.Execute(), 0);
+  }
+
+  virtual void TearDown() {
+    for (auto& handle : db_handles_)
+      sqlite3_close_v2(handle.first);
+
+    ASSERT_EQ(remove(TEST_PARSER_DB), 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_ = db_handles;
+  }
+
+  std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
+};
+
+TEST_F(DBHandlerTest, EmptyTest) {
+}