Don't throw exception in dtor
[platform/upstream/csr-framework.git] / test / test-common.h
index 4a875b9..cdec29a 100644 (file)
  */
 #pragma once
 
+#include <sstream>
+#include <iostream>
+#include <ios>
 #include <functional>
 #include <typeinfo>
 #include <string>
 #include <cstring>
+#include <cerrno>
+#include <system_error>
+#include <unistd.h>
 
 #include <boost/test/unit_test.hpp>
 
-#include <csr/content-screening.h>
-#include <csr/web-protection.h>
+#include <csr-content-screening.h>
+#include <csr-web-protection.h>
 
-#include <csre/content-screening.h>
-#include <csre/web-protection.h>
-
-#include <csre/content-screening-engine-info.h>
-#include <csre/web-protection-engine-info.h>
+#include <csre-content-screening.h>
+#include <csre-web-protection.h>
 
 #ifndef __FILENAME__
 #define __FILENAME__ (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
 #endif
 
+#define TOSTRING(ITEMS)                                                         \
+       (dynamic_cast<std::ostringstream &>(std::ostringstream().seekp(             \
+                                                                               0, std::ios_base::cur) << ITEMS)).str()
+
+#define ASSERT_IF_MSG(value, expected, msg) \
+       Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, TOSTRING(msg))
+
+#define WARN_IF_MSG(value, expected, msg) \
+       Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, TOSTRING(msg))
+
 #define ASSERT_IF(value, expected) \
-       Test::_assert(value, expected, __FILENAME__, __func__, __LINE__)
+       Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, true, "")
+
+#define WARN_IF(value, expected) \
+       Test::_assert(value, expected, __FILENAME__, __func__, __LINE__, false, "")
 
 #define ASSERT_SUCCESS(value) \
-       Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__)
+       Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, true, "")
+
+#define WARN_SUCCESS(value) \
+       Test::_assert(value, CSR_ERROR_NONE, __FILENAME__, __func__, __LINE__, false, "")
 
 #define ASSERT_INSTALL_APP(path, type)                       \
        BOOST_REQUIRE_MESSAGE(Test::install_app(path, type),     \
@@ -65,11 +84,19 @@ namespace Test {
 
 template <typename T, typename U>
 void _assert(const T &value, const U &expected, const std::string &filename,
-                        const std::string &funcname, unsigned int line)
+                        const std::string &funcname, unsigned int line, bool isAssert,
+                        const std::string &msg)
 {
-       BOOST_REQUIRE_MESSAGE(value == expected,
-                                                 "[" << filename << " > " << funcname << " : " << line << "]" <<
-                                                 " returned[" << value << "] expected[" << expected << "]");
+       if (isAssert)
+               BOOST_REQUIRE_MESSAGE(value == expected,
+                                                         "[" << filename << " > " << funcname << " : " << line <<
+                                                         "]" << " returned[" << value << "] expected[" << expected <<
+                                                         "] " << msg);
+       else
+               BOOST_WARN_MESSAGE(value == expected,
+                                                         "[" << filename << " > " << funcname << " : " << line <<
+                                                         "]" << " returned[" << value << "] expected[" << expected <<
+                                                         "] " << msg);
 }
 
 template <>
@@ -77,35 +104,131 @@ void _assert<csr_error_e, csr_error_e>(const csr_error_e &value,
                                                                           const csr_error_e &expected,
                                                                           const std::string &filename,
                                                                           const std::string &funcname,
-                                                                          unsigned int line);
+                                                                          unsigned int line,
+                                                                          bool isAssert,
+                                                                          const std::string &msg);
 
 template <>
 void _assert<csr_error_e, int>(const csr_error_e &value,
                                                           const int &expected,
                                                           const std::string &filename,
                                                           const std::string &funcname,
-                                                          unsigned int line);
+                                                          unsigned int line,
+                                                          bool isAssert,
+                                                          const std::string &msg);
 
 template <>
 void _assert<int, csr_error_e>(const int &value,
                                                           const csr_error_e &expected,
                                                           const std::string &filename,
                                                           const std::string &funcname,
-                                                          unsigned int line);
+                                                          unsigned int line,
+                                                          bool isAssert,
+                                                          const std::string &msg);
+
+template <>
+void _assert<const char *, const char *>(const char * const &value,
+                                                                                const char * const &expected,
+                                                                                const std::string &filename,
+                                                                                const std::string &funcname,
+                                                                                unsigned int line,
+                                                                                bool isAssert,
+                                                                                const std::string &msg);
+
+template <>
+void _assert<char *, const char *>(char * const &value,
+                                                                  const char * const &expected,
+                                                                  const std::string &filename,
+                                                                  const std::string &funcname,
+                                                                  unsigned int line,
+                                                                  bool isAssert,
+                                                                  const std::string &msg);
 
-void _assert(const char *value, const char *expected, const std::string &filename,
-                        const std::string &funcname, unsigned int line);
-void _assert(const char *value, const std::string &expected, const std::string &filename,
-                        const std::string &funcname, unsigned int line);
+template <>
+void _assert<const char *, char *>(const char * const &value,
+                                                                  char * const &expected,
+                                                                  const std::string &filename,
+                                                                  const std::string &funcname,
+                                                                  unsigned int line,
+                                                                  bool isAssert,
+                                                                  const std::string &msg);
+
+template <>
+void _assert<char *, char *>(char * const &value,
+                                                        char * const &expected,
+                                                        const std::string &filename,
+                                                        const std::string &funcname,
+                                                        unsigned int line,
+                                                        bool isAssert,
+                                                        const std::string &msg);
+
+template <>
+void _assert<const char *, std::string>(const char * const &value,
+                                                                               const std::string &expected,
+                                                                               const std::string &filename,
+                                                                               const std::string &funcname,
+                                                                               unsigned int line,
+                                                                               bool isAssert,
+                                                                               const std::string &msg);
+
+template <>
+void _assert<char *, std::string>(char * const &value,
+                                                                 const std::string &expected,
+                                                                 const std::string &filename,
+                                                                 const std::string &funcname,
+                                                                 unsigned int line,
+                                                                 bool isAssert,
+                                                                 const std::string &msg);
 
 void exceptionGuard(const std::function<void()> &);
 
-void copy_file(const char *src_file, const char *dest_file);
+std::string capi_ec_to_string(csr_error_e ec);
+std::string capi_ec_to_string(int ec);
+
+void make_dir(const char *dir);
+void make_dir_assert(const char *dir);
+void copy_file(const char *src_file, const char *dst_file);
+void copy_file_assert(const char *src_file, const char *dst_file);
 void touch_file(const char *file);
+void touch_file_assert(const char *file);
+void remove_file(const char *file);
+void remove_file_assert(const char *file);
 bool is_file_exist(const char *file);
 bool install_app(const char *app_path, const char *app_type);
 bool uninstall_app(const char *pkg_id);
-bool is_app_exist(const char *pkg_id);
+void initialize_db();
+
+struct ScopedCstr {
+       char *ptr;
+
+       ScopedCstr() : ptr(nullptr) {}
+       ~ScopedCstr()
+       {
+               if (ptr)
+                       free(ptr);
+       }
+};
+
+class ScopedChDir {
+public:
+       ScopedChDir(const std::string &dirpath)
+       {
+               if (::getcwd(cdbuf, PATH_MAX + 1) == nullptr)
+                       throw std::system_error(errno, std::system_category(), "getcwd failed");
+
+               if (::chdir(dirpath.c_str()) == -1)
+                       throw std::system_error(errno, std::system_category(),
+                                                                       dirpath + " chdir failed");
+       }
+
+       ~ScopedChDir()
+       {
+               ::chdir(cdbuf);
+       }
+
+private:
+       char cdbuf[PATH_MAX + 1];
+};
 
 template <typename T>
 class Context {
@@ -139,13 +262,13 @@ class Context<csr_cs_context_h> {
 public:
        Context() : m_context(nullptr)
        {
-               ASSERT_IF(csr_cs_context_create(&m_context), CSR_ERROR_NONE);
+               ASSERT_SUCCESS(csr_cs_context_create(&m_context));
                BOOST_REQUIRE(m_context != nullptr);
        }
 
        virtual ~Context()
        {
-               ASSERT_IF(csr_cs_context_destroy(m_context), CSR_ERROR_NONE);
+               ASSERT_SUCCESS(csr_cs_context_destroy(m_context));
        }
 
        csr_cs_context_h get(void) const
@@ -162,13 +285,13 @@ class Context<csr_wp_context_h> {
 public:
        Context() : m_context(nullptr)
        {
-               ASSERT_IF(csr_wp_context_create(&m_context), CSR_ERROR_NONE);
+               ASSERT_SUCCESS(csr_wp_context_create(&m_context));
                BOOST_REQUIRE(m_context != nullptr);
        }
 
        virtual ~Context()
        {
-               ASSERT_IF(csr_wp_context_destroy(m_context), CSR_ERROR_NONE);
+               ASSERT_SUCCESS(csr_wp_context_destroy(m_context));
        }
 
        csr_wp_context_h get(void) const
@@ -185,13 +308,13 @@ class Context<csre_cs_context_h> {
 public:
        Context() : m_context(nullptr)
        {
-               ASSERT_IF(csre_cs_context_create(&m_context), CSRE_ERROR_NONE);
+               ASSERT_SUCCESS(csre_cs_context_create(&m_context));
                BOOST_REQUIRE(m_context != nullptr);
        }
 
        virtual ~Context()
        {
-               ASSERT_IF(csre_cs_context_destroy(m_context), CSRE_ERROR_NONE);
+               ASSERT_SUCCESS(csre_cs_context_destroy(m_context));
        }
 
        csre_cs_context_h get(void) const
@@ -208,13 +331,13 @@ class Context<csre_wp_context_h> {
 public:
        Context() : m_context(nullptr)
        {
-               ASSERT_IF(csre_wp_context_create(&m_context), CSRE_ERROR_NONE);
+               ASSERT_SUCCESS(csre_wp_context_create(&m_context));
                BOOST_REQUIRE(m_context != nullptr);
        }
 
        virtual ~Context()
        {
-               ASSERT_IF(csre_wp_context_destroy(m_context), CSRE_ERROR_NONE);
+               ASSERT_SUCCESS(csre_wp_context_destroy(m_context));
        }
 
        csre_wp_context_h get(void) const
@@ -226,50 +349,4 @@ private:
        csre_wp_context_h m_context;
 };
 
-template <>
-class Context<csre_cs_engine_h> {
-public:
-       Context() : m_context(nullptr)
-       {
-               ASSERT_IF(csre_cs_engine_get_info(&m_context), CSRE_ERROR_NONE);
-               BOOST_REQUIRE(m_context != nullptr);
-       }
-
-       virtual ~Context()
-       {
-               ASSERT_IF(csre_cs_engine_destroy(m_context), CSRE_ERROR_NONE);
-       }
-
-       csre_cs_engine_h get(void) const
-       {
-               return m_context;
-       }
-
-private:
-       csre_cs_engine_h m_context;
-};
-
-template <>
-class Context<csre_wp_engine_h> {
-public:
-       Context() : m_context(nullptr)
-       {
-               ASSERT_IF(csre_wp_engine_get_info(&m_context), CSRE_ERROR_NONE);
-               BOOST_REQUIRE(m_context != nullptr);
-       }
-
-       virtual ~Context()
-       {
-               ASSERT_IF(csre_wp_engine_destroy(m_context), CSRE_ERROR_NONE);
-       }
-
-       csre_wp_engine_h get(void) const
-       {
-               return m_context;
-       }
-
-private:
-       csre_wp_engine_h m_context;
-};
-
 } // namespace Test