Turn on feature: engine state (enable / disable) 91/69791/2
authorKyungwook Tak <k.tak@samsung.com>
Mon, 16 May 2016 11:21:07 +0000 (20:21 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Thu, 19 May 2016 09:29:42 +0000 (18:29 +0900)
Change-Id: Ife9f6930211854e37fb5419c882e89a4ebe36e5d
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
16 files changed:
src/framework/db/manager.cpp
src/framework/db/manager.h
src/framework/db/query.h
src/framework/service/cs-logic.cpp
src/framework/service/em-logic.cpp
src/framework/service/exception.h
src/framework/service/server-service.cpp
src/framework/service/wp-logic.cpp
src/framework/service/wp-logic.h
test/CMakeLists.txt
test/internals/CMakeLists.txt
test/internals/test-api-engine-content-screening.cpp [moved from test/test-api-engine-content-screening.cpp with 99% similarity]
test/internals/test-api-engine-web-protection.cpp [moved from test/test-api-engine-web-protection.cpp with 100% similarity]
test/internals/test-db.cpp
test/internals/test-main.cpp
test/test-main.cpp

index 4521f04..474d639 100644 (file)
@@ -176,23 +176,37 @@ void Manager::setSchemaVersion(int sv)
 //===========================================================================
 // ENGINE_STATE table
 //===========================================================================
-int Manager::getEngineState(int engineId)
+csr_state_e Manager::getEngineState(csr_engine_id_e id)
 {
-       Statement stmt(this->m_conn, Query::SEL_ENGINE_STATE);
+       std::lock_guard<std::mutex> l(this->m_mutex);
 
-       stmt.bind(engineId);
+       if (this->m_stateMap.size() == 0) {
+               Statement stmt(this->m_conn, Query::SEL_ENGINE_STATE_ALL);
 
-       return stmt.step() ? stmt.getInt() : -1;
+               while (stmt.step()) {
+                       auto _id = static_cast<csr_engine_id_e>(stmt.getInt());
+                       auto _state = static_cast<csr_state_e>(stmt.getInt());
+
+                       this->m_stateMap[_id] = _state;
+               }
+       }
+
+       return (this->m_stateMap.count(id) == 0) ? static_cast<csr_state_e>(-1) :
+                                                                                          this->m_stateMap[id];
 }
 
-void Manager::setEngineState(int engineId, int state)
+void Manager::setEngineState(csr_engine_id_e id, csr_state_e state)
 {
+       std::lock_guard<std::mutex> l(this->m_mutex);
+
        Statement stmt(this->m_conn, Query::INS_ENGINE_STATE);
 
-       stmt.bind(engineId);
-       stmt.bind(state);
+       stmt.bind(static_cast<int>(id));
+       stmt.bind(static_cast<int>(state));
 
        stmt.exec();
+
+       this->m_stateMap[id] = state;
 }
 
 //===========================================================================
index cf39157..48e3259 100644 (file)
 #include <string>
 #include <vector>
 #include <memory>
+#include <map>
+#include <mutex>
 
 #include "db/connection.h"
 #include "db/row.h"
 #include "common/cs-detected.h"
 
+#include "csr/engine-manager.h"
+
 namespace Csr {
 namespace Db {
 
@@ -41,8 +45,8 @@ public:
        int getSchemaVersion();
 
        // ENGINE_STATE
-       int getEngineState(int engineId);
-       void setEngineState(int engineId, int state);
+       csr_state_e getEngineState(csr_engine_id_e);
+       void setEngineState(csr_engine_id_e, csr_state_e);
 
        // SCAN_REQUEST
        time_t getLastScanTime(const std::string &dir, const std::string &dataVersion);
@@ -79,6 +83,9 @@ private:
 
        Csr::Db::Connection m_conn;
        std::string m_scriptsDir;
+
+       std::map<csr_engine_id_e, csr_state_e> m_stateMap;
+       std::mutex m_mutex;
 };
 
 } // namespace Db
index 4966548..eb7ce8a 100644 (file)
@@ -34,6 +34,9 @@ const std::string SEL_ENGINE_STATE =
 const std::string INS_ENGINE_STATE =
        "insert or replace into ENGINE_STATE (id, state) values (?, ?)";
 
+const std::string SEL_ENGINE_STATE_ALL =
+       "select id, state from ENGINE_STATE";
+
 const std::string SEL_SCAN_REQUEST =
        "select last_scan from SCAN_REQUEST where dir = ? and data_version = ?";
 
index c6a389f..ad0e04e 100644 (file)
@@ -67,6 +67,9 @@ RawBuffer CsLogic::scanData(const CsContext &context, const RawBuffer &data)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        setCoreUsage(context.coreUsage);
 
        CsEngineContext engineContext(this->m_loader);
@@ -299,6 +302,9 @@ RawBuffer CsLogic::scanFile(const CsContext &context, const std::string &filepat
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        setCoreUsage(context.coreUsage);
 
        if (File::isInApp(filepath))
@@ -375,6 +381,9 @@ RawBuffer CsLogic::getScannableFiles(const std::string &dir)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        auto lastScanTime = this->m_db.getLastScanTime(dir, this->m_dataVersion);
 
        auto visitor = FsVisitor::create(dir, lastScanTime);
index 8c53618..d6dd2b6 100644 (file)
 
 namespace Csr {
 
+namespace {
+
+bool _isValid(const csr_engine_id_e &id)
+{
+       switch (id) {
+       case CSR_ENGINE_CS:
+       case CSR_ENGINE_WP:
+               return true;
+
+       default:
+               return false;
+       }
+}
+
+bool _isValid(const csr_state_e &state)
+{
+       switch (state) {
+       case CSR_ENABLE:
+       case CSR_DISABLE:
+               return true;
+
+       default:
+               return false;
+       }
+}
+
+} // namespace anonymous
+
 EmLogic::EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db) :
        m_cs(cs), m_wp(wp), m_db(db)
 {
@@ -43,6 +71,9 @@ RawBuffer EmLogic::getEngineName(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(context.engineId) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        if (context.engineId == CSR_ENGINE_CS) {
                CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
@@ -76,6 +107,9 @@ RawBuffer EmLogic::getEngineVendor(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(context.engineId) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        if (context.engineId == CSR_ENGINE_CS) {
                CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
@@ -109,6 +143,9 @@ RawBuffer EmLogic::getEngineVersion(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(context.engineId) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        if (context.engineId == CSR_ENGINE_CS) {
                CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
@@ -142,6 +179,9 @@ RawBuffer EmLogic::getEngineDataVersion(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(context.engineId) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        if (context.engineId == CSR_ENGINE_CS) {
                CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
@@ -175,6 +215,9 @@ RawBuffer EmLogic::getEngineUpdatedTime(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(context.engineId) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        if (context.engineId == CSR_ENGINE_CS) {
                CsEngineInfo engineInfo(this->m_cs);
                auto &c = engineInfo.get();
@@ -204,6 +247,9 @@ RawBuffer EmLogic::getEngineActivated(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(context.engineId) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        csr_activated_e activated = CSR_NOT_ACTIVATED;
 
        if (context.engineId == CSR_ENGINE_CS) {
@@ -249,12 +295,14 @@ RawBuffer EmLogic::getEngineState(const EmContext &context)
 {
        EXCEPTION_GUARD_START
 
-       auto state = this->m_db.getEngineState(static_cast<int>(context.engineId));
+       if (!_isValid(context.engineId))
+               ThrowExc(InternalError, "invalid engine id comes to get engine state.");
 
-       if (state == -1)
-               ThrowExc(DbFailed, "No engine state exist...");
+       auto state = this->m_db.getEngineState(context.engineId);
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, state).pop();
+       return BinaryQueue::Serialize(CSR_ERROR_NONE,
+                       static_cast<int>(state) == -1 ? static_cast<int>(CSR_DISABLE) :
+                                                                                       static_cast<int>(state)).pop();
 
        EXCEPTION_GUARD_CLOSER(ret)
 
@@ -267,7 +315,10 @@ RawBuffer EmLogic::setEngineState(const EmContext &context, csr_state_e state)
 {
        EXCEPTION_GUARD_START
 
-       this->m_db.setEngineState(static_cast<int>(context.engineId), static_cast<int>(state));
+       if (!_isValid(context.engineId) || !_isValid(state))
+               ThrowExc(InternalError, "invalid argument comes to set engine state.");
+
+       this->m_db.setEngineState(context.engineId, state);
 
        return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
 
index 77aa2e3..4eaf84b 100644 (file)
@@ -30,6 +30,7 @@ using DbFailed           = DerivedException<CSR_ERROR_DB>;
 using EngineError        = DerivedException<CSR_ERROR_ENGINE_INTERNAL>;
 using EngineNotActivated = DerivedException<CSR_ERROR_ENGINE_NOT_ACTIVATED>;
 using EnginePermDenied   = DerivedException<CSR_ERROR_ENGINE_PERMISSION>;
+using EngineDisabled     = DerivedException<CSR_ERROR_ENGINE_DISABLED>;
 using RemoveFailed       = DerivedException<CSR_ERROR_REMOVE_FAILED>;
 using FileChanged        = DerivedException<CSR_ERROR_FILE_CHANGED>;
 
index ab566d8..8556893 100644 (file)
@@ -60,7 +60,7 @@ ServerService::ServerService() :
        m_wp(new WpLoader(WP_ENGINE_PATH, SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR)),
        m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE)),
        m_cslogic(*m_cs, *m_db),
-       m_wplogic(*m_wp),
+       m_wplogic(*m_wp, *m_db),
        m_emlogic(*m_cs, *m_wp, *m_db)
 {
        this->add(SockId::CS);
index a6729ce..4dcc1c5 100644 (file)
@@ -31,7 +31,7 @@
 
 namespace Csr {
 
-WpLogic::WpLogic(WpLoader &loader) : m_loader(loader)
+WpLogic::WpLogic(WpLoader &loader, Db::Manager &db) : m_loader(loader), m_db(db)
 {
        WpEngineInfo wpEngineInfo(this->m_loader);
        toException(this->m_loader.getEngineDataVersion(wpEngineInfo.get(),
@@ -46,6 +46,9 @@ RawBuffer WpLogic::checkUrl(const WpContext &context, const std::string &url)
 {
        EXCEPTION_GUARD_START
 
+       if (this->m_db.getEngineState(CSR_ENGINE_WP) != CSR_ENABLE)
+               ThrowExc(EngineDisabled, "engine is disabled");
+
        WpEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
index 1a9ad69..83e93b7 100644 (file)
@@ -26,6 +26,7 @@
 #include "common/types.h"
 #include "common/wp-context.h"
 #include "common/wp-result.h"
+#include "db/manager.h"
 #include "service/wp-loader.h"
 #include "service/logic.h"
 
@@ -33,7 +34,7 @@ namespace Csr {
 
 class WpLogic : public Logic {
 public:
-       WpLogic(WpLoader &loader);
+       WpLogic(WpLoader &loader, Db::Manager &db);
        virtual ~WpLogic();
 
        RawBuffer checkUrl(const WpContext &context, const std::string &url);
@@ -46,6 +47,8 @@ private:
                        const WpResult &result);
 
        WpLoader &m_loader;
+       Db::Manager &m_db;
+
        std::string m_dataVersion;
 };
 
index ee43719..7f58ef0 100755 (executable)
@@ -68,8 +68,6 @@ SET(${TARGET_CSR_TEST}_SRCS
        test-api-content-screening-async.cpp
        test-api-engine-manager.cpp
        test-api-web-protection.cpp
-       test-api-engine-content-screening.cpp
-       test-api-engine-web-protection.cpp
        test-main.cpp
        test-helper.cpp
 )
@@ -89,8 +87,6 @@ ADD_EXECUTABLE(${TARGET_CSR_TEST} ${${TARGET_CSR_TEST}_SRCS})
 TARGET_LINK_LIBRARIES(${TARGET_CSR_TEST}
        ${TARGET_CSR_TEST_COMMON}
        ${TARGET_CSR_CLIENT}
-       ${TARGET_CSR_CS_ENGINE_SAMPLE}
-       ${TARGET_CSR_WP_ENGINE_SAMPLE}
        ${${TARGET_CSR_TEST}_DEP_LIBRARIES}
        -lboost_unit_test_framework
        -ldl
index ce84411..0e3f7c0 100644 (file)
@@ -44,6 +44,8 @@ SET(${TARGET_CSR_INTERNAL_TEST}_SRCS
        test-db.cpp
        test-cpucore.cpp
        test-file-system.cpp
+       test-api-engine-content-screening.cpp
+       test-api-engine-web-protection.cpp
        test-cs-loader.cpp
        test-wp-loader.cpp
        test-canonicalize.cpp
@@ -52,10 +54,14 @@ SET(${TARGET_CSR_INTERNAL_TEST}_SRCS
 )
 
 INCLUDE_DIRECTORIES(
+       SYSTEM
+       ${${TARGET_CSR_INTERNAL_TEST}_DEP_INCLUDE_DIRS}
+)
+
+INCLUDE_DIRECTORIES(
        ${PROJECT_SOURCE_DIR}/src/include
        ${CSR_FW_SRC_PATH}
        ${CSR_TEST_SRC_PATH}
-       ${${TARGET_CSR_INTERNAL_TEST}_DEP_INCLUDE_DIRS}
 )
 
 ADD_EXECUTABLE(${TARGET_CSR_INTERNAL_TEST} ${${TARGET_CSR_INTERNAL_TEST}_SRCS})
@@ -63,6 +69,8 @@ ADD_EXECUTABLE(${TARGET_CSR_INTERNAL_TEST} ${${TARGET_CSR_INTERNAL_TEST}_SRCS})
 TARGET_LINK_LIBRARIES(${TARGET_CSR_INTERNAL_TEST}
        ${TARGET_CSR_TEST_COMMON}
        ${TARGET_CSR_COMMON}
+       ${TARGET_CSR_CS_ENGINE_SAMPLE}
+       ${TARGET_CSR_WP_ENGINE_SAMPLE}
        ${${TARGET_CSR_INTERNAL_TEST}_DEP_LIBRARIES}
        -lboost_unit_test_framework
        -ldl
@@ -102,8 +102,7 @@ BOOST_AUTO_TEST_CASE(context_create_destroy)
 {
        EXCEPTION_GUARD_START
 
-       auto c = Test::Context<csre_cs_context_h>();
-       (void) c;
+       Test::Context<csre_cs_context_h>();
 
        EXCEPTION_GUARD_END
 }
index c9b818a..5bdce01 100644 (file)
@@ -66,14 +66,14 @@ BOOST_AUTO_TEST_CASE(engine_state)
 
        Db::Manager db(TEST_DB_FILE, TEST_DB_SCRIPTS);
 
-       db.setEngineState(1, 1);
-       db.setEngineState(2, 2);
+       db.setEngineState(CSR_ENGINE_CS, CSR_ENABLE);
+       db.setEngineState(CSR_ENGINE_WP, CSR_DISABLE);
 
-       ASSERT_IF(db.getEngineState(1), 1);
-       ASSERT_IF(db.getEngineState(2), 2);
+       ASSERT_IF(db.getEngineState(CSR_ENGINE_CS), CSR_ENABLE);
+       ASSERT_IF(db.getEngineState(CSR_ENGINE_WP), CSR_DISABLE);
 
-       db.setEngineState(1, 2);
-       ASSERT_IF(db.getEngineState(1), 2);
+       db.setEngineState(CSR_ENGINE_CS, CSR_DISABLE);
+       ASSERT_IF(db.getEngineState(CSR_ENGINE_CS), CSR_DISABLE);
 
        EXCEPTION_GUARD_END
 }
index 1a3a3c4..ea6012a 100644 (file)
@@ -25,6 +25,9 @@
 #include <boost/test/results_reporter.hpp>
 #include <colour_log_formatter.h>
 
+#include "csre/content-screening.h"
+#include "csre/web-protection.h"
+
 struct TestConfig {
        TestConfig()
        {
@@ -36,4 +39,47 @@ struct TestConfig {
        }
 };
 
+bool isEngineInitialized = false;
+struct Initializer {
+       Initializer()
+       {
+               if (!isEngineInitialized) {
+                       int ret = csre_cs_global_initialize(
+                                                 SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR);
+
+                       if (ret != CSRE_ERROR_NONE)
+                               throw std::logic_error("Failed to init content screening engine.");
+
+                       ret = csre_wp_global_initialize(
+                                         SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR);
+
+                       if (ret != CSRE_ERROR_NONE)
+                               throw std::logic_error("Failed to init web protection engine.");
+
+                       isEngineInitialized = true;
+
+                       BOOST_MESSAGE("Initialize engines");
+               }
+       }
+
+       ~Initializer()
+       {
+               if (!isEngineInitialized)
+                       return;
+
+               int ret = csre_cs_global_deinitialize();
+
+               if (ret != CSRE_ERROR_NONE)
+                       throw std::logic_error("Failed to deinit content screening engine.");
+
+               ret = csre_wp_global_deinitialize();
+
+               if (ret != CSRE_ERROR_NONE)
+                       throw std::logic_error("Failed to deinit web protection engine.");
+
+               BOOST_MESSAGE("Deinitialize engines");
+       }
+};
+
 BOOST_GLOBAL_FIXTURE(TestConfig)
+BOOST_GLOBAL_FIXTURE(Initializer)
index c3e9a55..b7c3536 100644 (file)
 #include <boost/test/results_reporter.hpp>
 #include <colour_log_formatter.h>
 
-#include "csre/content-screening.h"
-#include "csre/web-protection.h"
+#include "csr/engine-manager.h"
+
+namespace {
+
+csr_state_e setEngineState(csr_engine_id_e id, csr_state_e state)
+{
+       csr_engine_h handle;
+       auto ret = csr_get_current_engine(id, &handle);
+       if (ret != CSR_ERROR_NONE)
+               throw std::logic_error("Failed to csr_get_current_engine.");
+
+       csr_state_e current;
+       ret = csr_engine_get_state(handle, &current);
+       if (ret != CSR_ERROR_NONE)
+               throw std::logic_error("Failed to csr_get_state.");
+
+       if (current == state)
+               return current;
+
+       ret = csr_engine_set_state(handle, state);
+       if (ret != CSR_ERROR_NONE)
+               throw std::logic_error("Failed to csr_engine_set_state.");
+
+       return current;
+}
+
+}
 
 struct TestConfig {
        TestConfig()
@@ -33,8 +58,7 @@ struct TestConfig {
                boost::unit_test::unit_test_log.set_threshold_level(
                        boost::unit_test::log_test_units);
                boost::unit_test::results_reporter::set_level(boost::unit_test::SHORT_REPORT);
-               boost::unit_test::unit_test_log.set_formatter(new
-                               Csr::Test::colour_log_formatter);
+               boost::unit_test::unit_test_log.set_formatter(new Csr::Test::colour_log_formatter);
        }
 };
 
@@ -42,42 +66,18 @@ bool isEngineInitialized = false;
 struct Initializer {
        Initializer()
        {
-               if (!isEngineInitialized) {
-                       int ret = csre_cs_global_initialize(
-                                                 SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR);
-
-                       if (ret != CSRE_ERROR_NONE)
-                               throw std::logic_error("Failed to init content screening engine.");
-
-                       ret = csre_wp_global_initialize(
-                                         SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR);
-
-                       if (ret != CSRE_ERROR_NONE)
-                               throw std::logic_error("Failed to init web protection engine.");
-
-                       isEngineInitialized = true;
-
-                       BOOST_MESSAGE("Initialize engines");
-               }
+               m_oldCsState = setEngineState(CSR_ENGINE_CS, CSR_ENABLE);
+               m_oldWpState = setEngineState(CSR_ENGINE_WP, CSR_ENABLE);
        }
 
        ~Initializer()
        {
-               if (!isEngineInitialized)
-                       return;
-
-               int ret = csre_cs_global_deinitialize();
-
-               if (ret != CSRE_ERROR_NONE)
-                       throw std::logic_error("Failed to deinit content screening engine.");
-
-               ret = csre_wp_global_deinitialize();
-
-               if (ret != CSRE_ERROR_NONE)
-                       throw std::logic_error("Failed to deinit web protection engine.");
-
-               BOOST_MESSAGE("Deinitialize engines");
+               setEngineState(CSR_ENGINE_CS, m_oldCsState);
+               setEngineState(CSR_ENGINE_WP, m_oldWpState);
        }
+
+       csr_state_e m_oldCsState;
+       csr_state_e m_oldWpState;
 };
 
 BOOST_GLOBAL_FIXTURE(TestConfig)