Use "wrap" compile option to mock fopen
authorIlho Kim <ilho159.kim@samsung.com>
Tue, 27 Apr 2021 07:12:41 +0000 (16:12 +0900)
committer김일호/Tizen Platform Lab(SR)/Engineer/삼성전자 <ilho159.kim@samsung.com>
Mon, 10 May 2021 07:14:07 +0000 (16:14 +0900)
__set_db_version function is moved by commit (2951a7dcf53f4949706d3cfd513b179dc1409096)
this patch abort that change and use "wrap" compile option
to mock fopen, fgets, fclose

Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
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/mock/file_mock.c [new file with mode: 0644]
test/unit_tests/mock/file_mock.h [new file with mode: 0644]
test/unit_tests/test_db_handlers.cc

index e776957..a123fb3 100644 (file)
@@ -30,8 +30,6 @@
 #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 {
 
@@ -62,45 +60,6 @@ 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();
@@ -112,9 +71,6 @@ 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);
 }
 
@@ -129,9 +85,6 @@ 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 cd02fc4..ff40360 100644 (file)
@@ -43,7 +43,6 @@ 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 04177d9..3c15ad2 100644 (file)
@@ -51,6 +51,8 @@ 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");
@@ -137,6 +139,43 @@ 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;
@@ -155,6 +194,9 @@ 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 39a68b4..0b031dd 100644 (file)
@@ -1,14 +1,19 @@
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS)
-
 INCLUDE_DIRECTORIES(
   ${CMAKE_CURRENT_SOURCE_DIR}/../../src/
   ${CMAKE_CURRENT_SOURCE_DIR}/../../parser/
 )
 
+FILE(GLOB_RECURSE UNIT_TESTS_SRCS *.cc *.c)
+FILE(GLOB_RECURSE SERVER_SRCS ${CMAKE_SOURCE_DIR}/src/server/*.cc ${CMAKE_SOURCE_DIR}/src/server/*.c)
+LIST(FILTER SERVER_SRCS EXCLUDE REGEX main.cc)
+
 ADD_EXECUTABLE(${TARGET_PKGMGR_INFO_UNIT_TEST}
   ${UNIT_TESTS_SRCS}
+  ${SERVER_SRCS}
 )
 
+ADD_DEFINITIONS("-DSYSCONFDIR=\"${SYSCONFDIR}\"")
+
 include(FindPkgConfig)
 pkg_check_modules(unit_test_pkgs REQUIRED dlog glib-2.0 gio-2.0 sqlite3 gmock parcel)
 
@@ -22,12 +27,19 @@ 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}")
 target_link_libraries(${TARGET_PKGMGR_INFO_UNIT_TEST} ${unit_test_pkgs_LDFLAGS})
 
+SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES
+  LINK_FLAGS "-Wl,\
+--wrap=malloc,\
+--wrap=fopen,\
+--wrap=fopen64,\
+--wrap=fgets,\
+--wrap=fclose")
+
 ADD_TEST(
   NAME ${TARGET_PKGMGR_INFO_UNIT_TEST}
   COMMAND ${TARGET_PKGMGR_INFO_UNIT_TEST}
diff --git a/test/unit_tests/mock/file_mock.c b/test/unit_tests/mock/file_mock.c
new file mode 100644 (file)
index 0000000..8df9777
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * 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 "file_mock.h"
+
+#include <stdio.h>
+#include <stdbool.h>
+
+extern FILE *__real_fopen(const char *filename, const char *modes);
+extern FILE *__real_fopen64(const char *filename, const char *modes);
+extern char *__real_fgets(char *s, int n, FILE * stream);
+extern int __real_fclose(FILE *stream);
+
+static bool flag_ = false;
+
+void fopen_mock_setup(bool flag) {
+  flag_ = flag;
+}
+
+FILE* __wrap_fopen(const char *filename, const char *modes) {
+  if (!flag_)
+    return __real_fopen(filename, modes);
+
+  return (FILE *)1;
+}
+
+FILE* __wrap_fopen64(const char *filename, const char *modes) {
+  if (!flag_)
+    return __real_fopen64(filename, modes);
+
+  return (FILE *)1;
+}
+
+char * __wrap_fgets(char *s, int n, FILE *stream) {
+  if (!flag_)
+    return __real_fgets(s, n, stream);
+
+  char buf[1024] = "30005";
+  printf("wrap_fopen called\n");
+  snprintf(s, n, "%s", buf);
+
+  return s;
+}
+
+int __wrap_fclose(FILE *stream) {
+  if (!flag_)
+    return __real_fclose(stream);
+
+  return 0;
+}
diff --git a/test/unit_tests/mock/file_mock.h b/test/unit_tests/mock/file_mock.h
new file mode 100644 (file)
index 0000000..baa8cff
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+#ifndef __MOCK_FILE_MOCK_H__
+#define __MOCK_FILE_MOCK_H__
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void fopen_mock_setup(bool flag);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MOCK_FILE_MOCK_H__ */
index 04324de..63fcdbb 100644 (file)
@@ -25,6 +25,7 @@
 #include "parcel_utils.hh"
 #include "pkg_get_db_handler.hh"
 #include "pkg_set_db_handler.hh"
+#include "mock/file_mock.h"
 
 #include "pkgmgr-info.h"
 #include "pkgmgrinfo_basic.h"
@@ -38,7 +39,6 @@ class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
 
   MOCK_METHOD0(Connect, bool());
   MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
-  MOCK_METHOD1(SetDBVersion, int(sqlite3 *conn));
 };
 
 class PkgSetDBHandlerMock : public pkgmgr_server::database::PkgSetDBHandler {
@@ -72,9 +72,9 @@ class DBHandlerTest : public ::testing::Test {
         .Times(2).WillRepeatedly(testing::Return(true));
     EXPECT_CALL(create_db_handler, GetConnection())
         .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
-    EXPECT_CALL(create_db_handler, SetDBVersion(testing::_))
-        .Times(2).WillRepeatedly(testing::Return(0));
+    fopen_mock_setup(true);
     ASSERT_EQ(create_db_handler.Execute(), 0);
+    fopen_mock_setup(false);
   }
 
   virtual void TearDown() {