//===========================================================================
// 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;
}
//===========================================================================
#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 {
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);
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
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 = ?";
{
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);
{
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))
{
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);
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)
{
{
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();
{
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();
{
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();
{
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();
{
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();
{
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) {
{
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)
{
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();
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>;
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);
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(),
{
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();
#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"
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);
const WpResult &result);
WpLoader &m_loader;
+ Db::Manager &m_db;
+
std::string m_dataVersion;
};
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
)
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
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
)
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})
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
{
EXCEPTION_GUARD_START
- auto c = Test::Context<csre_cs_context_h>();
- (void) c;
+ Test::Context<csre_cs_context_h>();
EXCEPTION_GUARD_END
}
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
}
#include <boost/test/results_reporter.hpp>
#include <colour_log_formatter.h>
+#include "csre/content-screening.h"
+#include "csre/web-protection.h"
+
struct TestConfig {
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)
#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, ¤t);
+ 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()
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);
}
};
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)