Impl logic of get detected/ignored/judge malware 55/67955/3
authorKyungwook Tak <k.tak@samsung.com>
Fri, 29 Apr 2016 10:12:27 +0000 (19:12 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 2 May 2016 02:02:24 +0000 (11:02 +0900)
Change-Id: Ifccccafb58d5408a6933b0673381f4295386aaee
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
15 files changed:
src/framework/client/async-logic.cpp
src/framework/client/content-screening.cpp
src/framework/common/command-id.h
src/framework/common/cs-detected.h
src/framework/db/manager.cpp
src/framework/db/manager.h
src/framework/db/row.h
src/framework/service/file-system.cpp
src/framework/service/logic.cpp
src/framework/service/logic.h
src/include/csr/error.h
test/internals/test-db.cpp
test/internals/test-file-system.cpp
test/test-api-content-screening-async.cpp
test/test-api-content-screening.cpp

index 1662e1e..b0ac593 100644 (file)
@@ -72,9 +72,12 @@ AsyncLogic::Ending AsyncLogic::scanDirs(const std::shared_ptr<StrSet> &dirs)
 AsyncLogic::Ending AsyncLogic::scanDir(const std::string &dir)
 {
        // For in case of there's already detected malware for dir
+       StrSet dirset;
+       dirset.insert(dir);
+
        auto retResults =
                m_dispatcher->methodCall<std::pair<int, std::vector<CsDetected *>>>(
-                       CommandId::DIR_GET_RESULTS, m_ctx, dir);
+                       CommandId::GET_DETECTED_LIST, dirset);
 
        if (retResults.first != CSR_ERROR_NONE) {
                ERROR("[Error] ret: " << retResults.first);
index b60e662..c1d24ad 100644 (file)
@@ -621,7 +621,6 @@ int csr_cs_judge_detected_malware(csr_cs_context_h handle,
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
        auto ret = hExt->dispatch<int>(
                                   CommandId::JUDGE_STATUS,
-                                  hExt->getContext(),
                                   reinterpret_cast<CsDetected *>(detected)->targetName,
                                   static_cast<int>(action));
 
@@ -646,9 +645,7 @@ int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path,
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
        auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
-                                  CommandId::GET_DETECTED,
-                                  hExt->getContext(),
-                                  std::string(file_path));
+                                  CommandId::GET_DETECTED, std::string(file_path));
 
        if (ret.first != CSR_ERROR_NONE) {
                ERROR("Error! ret: " << ret.first);
@@ -697,7 +694,7 @@ int csr_cs_get_detected_malwares(csr_cs_context_h handle,
                return CSR_ERROR_INVALID_PARAMETER;
 
        auto ret = hExt->dispatch<std::pair<int, std::vector<CsDetected *>>>(
-                                  CommandId::GET_DETECTED_LIST, hExt->getContext(), dirSet);
+                                  CommandId::GET_DETECTED_LIST, dirSet);
 
        if (ret.first != CSR_ERROR_NONE) {
                ERROR("Error! ret: " << ret.first);
@@ -738,9 +735,7 @@ int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path,
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
        auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
-                                  CommandId::GET_IGNORED,
-                                  hExt->getContext(),
-                                  std::string(file_path));
+                                  CommandId::GET_IGNORED, std::string(file_path));
 
        if (ret.first != CSR_ERROR_NONE) {
                ERROR("Error! ret: " << ret.first);
@@ -789,7 +784,7 @@ int csr_cs_get_ignored_malwares(csr_cs_context_h handle,
                return CSR_ERROR_INVALID_PARAMETER;
 
        auto ret = hExt->dispatch<std::pair<int, std::vector<CsDetected *>>>(
-                                  CommandId::GET_IGNORED_LIST, hExt->getContext(), dirSet);
+                                  CommandId::GET_IGNORED_LIST, dirSet);
 
        if (ret.first != CSR_ERROR_NONE) {
                ERROR("Error! ret: " << ret.first);
index f3355d0..c8a4f7b 100644 (file)
@@ -29,12 +29,11 @@ enum class CommandId : int {
        SCAN_DATA           = 0x1001,
        SCAN_FILE           = 0x1002,
        // delta, history
-       DIR_GET_RESULTS     = 0x1101,
-       DIR_GET_FILES       = 0x1102,
-       GET_DETECTED        = 0x1103,
-       GET_DETECTED_LIST   = 0x1104,
-       GET_IGNORED         = 0x1105,
-       GET_IGNORED_LIST    = 0x1106,
+       GET_DETECTED        = 0x1101,
+       GET_DETECTED_LIST   = 0x1102,
+       GET_IGNORED         = 0x1103,
+       GET_IGNORED_LIST    = 0x1104,
+       DIR_GET_FILES       = 0x1105,
        // handle result
        JUDGE_STATUS        = 0x1201,
 
index e9460a5..8dce1ec 100644 (file)
@@ -31,7 +31,6 @@ namespace Csr {
 
 class CsDetected;
 using CsDetectedPtr = std::unique_ptr<CsDetected>;
-using CsDetectedList = std::vector<CsDetectedPtr>;
 
 struct CsDetected : public IResult {
        CsDetected();
index 535014e..784b5ca 100644 (file)
@@ -219,12 +219,12 @@ void Manager::cleanLastScanTime()
 //===========================================================================
 // DETECTED_MALWARE_FILE table
 //===========================================================================
-RowsShPtr Manager::getDetectedMalwares(const std::string &dir)
+RowShPtrs Manager::getDetectedMalwares(const std::string &dir)
 {
        Statement stmt(m_conn, Query::SEL_DETECTED_BY_DIR);
        stmt.bind(dir);
 
-       RowsShPtr rows = std::make_shared<std::vector<RowShPtr>>();
+       RowShPtrs rows;
 
        while (stmt.step()) {
                RowShPtr row = std::make_shared<Row>();
@@ -238,7 +238,7 @@ RowsShPtr Manager::getDetectedMalwares(const std::string &dir)
                row->ts = static_cast<time_t>(stmt.getInt64());
                row->isIgnored = static_cast<bool>(stmt.getInt());
 
-               rows->emplace_back(std::move(row));
+               rows.emplace_back(std::move(row));
        }
 
        return rows;
index 1109127..4ad4c65 100644 (file)
@@ -52,7 +52,7 @@ public:
        void cleanLastScanTime();
 
        // DETECTED_MALWARE_FILE & USER_RESPONSE
-       RowsShPtr getDetectedMalwares(const std::string &dirpath);
+       RowShPtrs getDetectedMalwares(const std::string &dirpath);
        RowShPtr getDetectedMalware(const std::string &filepath);
        void insertDetectedMalware(const CsDetected &, const std::string &dataVersion,
                                                           bool isIgnored);
index 1f7ffad..2dd9f76 100644 (file)
@@ -30,7 +30,7 @@ namespace Db {
 
 class Row;
 using RowShPtr = std::shared_ptr<Row>;
-using RowsShPtr = std::shared_ptr<std::vector<RowShPtr>>;
+using RowShPtrs = std::vector<RowShPtr>;
 
 struct Row : public Csr::CsDetected {
        std::string dataVersion; // engine's data version
index 8051c85..2381f63 100644 (file)
 #include "service/file-system.h"
 
 #include <system_error>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
+#include <cstdio>
+#include <cstring>
+#include <cerrno>
 #include <sys/stat.h>
 
 #include "service/app-deleter.h"
 #include "common/audit/logger.h"
+#include "common/exception.h"
 
 namespace Csr {
 
@@ -174,9 +175,14 @@ bool FileSystemVisitor::isModifiedSince(const std::string &path, time_t since)
        struct stat s;
 
        if (stat(path.c_str(), &s) != 0)
-               return false;
-       else
-               return s.st_mtim.tv_sec >= since;
+               ThrowExc(InternalError, "Failed to stat() on file: " << path <<
+                                ". errno: " << errno);
+
+       DEBUG("Modified since called with file: " << path <<
+                 " file mtime: " << s.st_mtim.tv_sec <<
+                 " since: " << since);
+
+       return s.st_mtim.tv_sec > since;
 }
 
 FileVisitor::FileVisitor(const std::string &fpath, time_t modifiedSince) :
@@ -190,8 +196,10 @@ FileVisitor::~FileVisitor()
 
 FileShrPtr FileVisitor::next()
 {
-       if (m_nextItem && !FileSystemVisitor::isModifiedSince(m_path, m_since))
+       if (m_nextItem && FileSystemVisitor::isModifiedSince(m_path, m_since)) {
+               DEBUG("visitied file is modified since the time. file: " << m_path);
                m_nextItem.reset();
+       }
 
        FileShrPtr item = m_nextItem;
        m_nextItem.reset();
index 0bc43c0..d37271a 100644 (file)
@@ -24,6 +24,7 @@
 #include <string>
 #include <utility>
 #include <algorithm>
+#include <climits>
 
 #include "common/audit/logger.h"
 #include "common/exception.h"
@@ -126,13 +127,6 @@ RawBuffer Logic::dispatch(const RawBuffer &in)
                return scanFile(*cptr, filepath);
        }
 
-       case CommandId::DIR_GET_RESULTS: {
-               CsContextShPtr cptr;
-               std::string dir;
-               info.second.Deserialize(cptr, dir);
-               return dirGetResults(*cptr, dir);
-       }
-
        case CommandId::DIR_GET_FILES: {
                CsContextShPtr cptr;
                std::string dir;
@@ -141,38 +135,34 @@ RawBuffer Logic::dispatch(const RawBuffer &in)
        }
 
        case CommandId::JUDGE_STATUS: {
-               CsContextShPtr cptr;
                std::string filepath;
-               info.second.Deserialize(cptr, filepath);
-               return judgeStatus(*cptr, filepath);
+               int intAction;
+               info.second.Deserialize(filepath, intAction);
+               return judgeStatus(filepath, static_cast<csr_cs_action_e>(intAction));
        }
 
        case CommandId::GET_DETECTED: {
-               CsContextShPtr cptr;
                std::string filepath;
-               info.second.Deserialize(cptr, filepath);
-               return getDetected(*cptr, filepath);
+               info.second.Deserialize(filepath);
+               return getDetected(filepath);
        }
 
        case CommandId::GET_DETECTED_LIST: {
-               CsContextShPtr cptr;
                StrSet dirSet;
-               info.second.Deserialize(cptr, dirSet);
-               return getDetectedList(*cptr, dirSet);
+               info.second.Deserialize(dirSet);
+               return getDetectedList(dirSet);
        }
 
        case CommandId::GET_IGNORED: {
-               CsContextShPtr cptr;
                std::string filepath;
-               info.second.Deserialize(cptr, filepath);
-               return getIgnored(*cptr, filepath);
+               info.second.Deserialize(filepath);
+               return getIgnored(filepath);
        }
 
        case CommandId::GET_IGNORED_LIST: {
-               CsContextShPtr cptr;
                StrSet dirSet;
-               info.second.Deserialize(cptr, dirSet);
-               return getIgnoredList(*cptr, dirSet);
+               info.second.Deserialize(dirSet);
+               return getIgnoredList(dirSet);
        }
 
        // Web protection
@@ -245,35 +235,9 @@ RawBuffer Logic::scanData(const CsContext &context, const RawBuffer &data)
        return BinaryQueue::Serialize(ret, d).pop();
 }
 
-RawBuffer Logic::scanFile(const CsContext &context, const std::string &filepath)
+RawBuffer Logic::scanFileHelper(const CsContext &context,
+                                                               const std::string &filepath)
 {
-       auto history = m_db->getDetectedMalware(filepath);
-
-       if (history) {
-               // history exist of malware detected for the file.
-               // let's check file modified since the detected time.
-               auto file = createVisitor(filepath, static_cast<time_t>(history->ts))->next();
-
-               if (file == nullptr) {
-                       // file isn't modified since the detected time. history can be used.
-                       // TODO: case should be separated. (file isn't exist) and (not modified)
-                       if (context.askUser) {
-                               if (history->isIgnored) {
-                                       history->response = CSR_CS_IGNORE;
-                               } else {
-                                       if ((history->response = getUserResponse(context, *history))
-                                                       == CSR_CS_IGNORE)
-                                               m_db->setDetectedMalwareIgnored(filepath, true);
-                               }
-                       }
-
-                       return BinaryQueue::Serialize(CSR_ERROR_NONE, history).pop();
-               } else {
-                       // file is modified since the detected time. let's remove history!
-                       m_db->deleteDetectedMalware(filepath);
-               }
-       }
-
        CsEngineContext engineContext(m_cs);
        auto &c = engineContext.get();
 
@@ -318,13 +282,54 @@ RawBuffer Logic::scanFile(const CsContext &context, const std::string &filepath)
        return BinaryQueue::Serialize(ret, d).pop();
 }
 
-RawBuffer Logic::dirGetResults(const CsContext &context, const std::string &dir)
+RawBuffer Logic::scanFile(const CsContext &context, const std::string &filepath)
 {
-       INFO("Dir[" << dir << "] get results");
+       auto history = m_db->getDetectedMalware(filepath);
 
-       printCsContext(context);
+       if (!history)
+               return scanFileHelper(context, filepath);
+
+       // history exist of malware detected for the file.
+       // let's check file modified since the detected time.
+       auto file = createVisitor(filepath, static_cast<time_t>(history->ts))->next();
+
+       // file is modified since the detected time. let's remove history!
+       if (!file) {
+               m_db->deleteDetectedMalware(filepath);
+               return scanFileHelper(context, filepath);
+       }
+
+       // file isn't modified since the detected time. history can be used.
+       if (!context.askUser)
+               return BinaryQueue::Serialize(CSR_ERROR_NONE, history).pop();
+
+       if (history->isIgnored) {
+               history->response = CSR_CS_IGNORE;
+       } else {
+               switch (history->response = getUserResponse(context, *history)) {
+               case CSR_CS_IGNORE:
+                       m_db->setDetectedMalwareIgnored(filepath, true);
+                       break;
+
+               case CSR_CS_REMOVE:
+                       if (!file->remove()) {
+                               ERROR("Failed to remove filepath: " << filepath);
+                               return BinaryQueue::Serialize(CSR_ERROR_REMOVE_FAILED, CsDetected()).pop();
+                       }
+
+                       m_db->deleteDetectedMalware(filepath);
+                       break;
+
+               case CSR_CS_SKIP:
+                       break;
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, std::vector<CsDetected>()).pop();
+               default:
+                       ThrowExc(InternalError, "Invalid response from popup: " <<
+                                        static_cast<int>(history->response));
+               }
+       }
+
+       return BinaryQueue::Serialize(CSR_ERROR_NONE, history).pop();
 }
 
 RawBuffer Logic::dirGetFiles(const CsContext &context, const std::string &dir)
@@ -336,76 +341,96 @@ RawBuffer Logic::dirGetFiles(const CsContext &context, const std::string &dir)
        return BinaryQueue::Serialize(CSR_ERROR_NONE, StrSet()).pop();
 }
 
-RawBuffer Logic::judgeStatus(const CsContext &context,
-                                                        const std::string &filepath)
+RawBuffer Logic::judgeStatus(const std::string &filepath,
+                                                        csr_cs_action_e action)
 {
-       INFO("Judge Status[" << filepath << "] by engine");
+       auto history = m_db->getDetectedMalware(filepath);
 
-       printCsContext(context);
+       if (!history) {
+               ERROR("Target to be judged doesn't exist in db. name: " << filepath);
+               return BinaryQueue::Serialize(CSR_ERROR_INVALID_PARAMETER).pop();
+       }
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
-}
+       auto file = createVisitor(filepath, static_cast<time_t>(history->ts))->next();
 
-RawBuffer Logic::getDetected(const CsContext &context,
-                                                        const std::string &filepath)
-{
-       INFO("Get Detected[" << filepath << "] by engine");
+       if (!file) {
+               ERROR("Target doesn't exist on target path on filesystem or "
+                         "modified since db delta inserted. name: " << filepath);
+               m_db->deleteDetectedMalware(filepath);
+               // TODO: is it okay to just refresh db and return success?
+               return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
+       }
 
-       printCsContext(context);
+       switch (action) {
+       case CSR_CS_ACTION_REMOVE:
+               if (!file->remove()) {
+                       ERROR("Failed to remove filepath: " << filepath);
+                       return BinaryQueue::Serialize(CSR_ERROR_REMOVE_FAILED).pop();
+               }
 
-       CsDetected detected;
-       detected.targetName = "test_file";
+               m_db->deleteDetectedMalware(filepath);
+               break;
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, detected).pop();
-}
+       case CSR_CS_ACTION_IGNORE:
+               m_db->setDetectedMalwareIgnored(filepath, true);
+               break;
 
-RawBuffer Logic::getDetectedList(const CsContext &context, const StrSet &dirSet)
-{
-       INFO("Get detected list logic");
-       std::for_each(dirSet.begin(), dirSet.end(), [](const std::string & dir) {
-               INFO("dir[" << dir << "] in directory set of get detected list logic");
-       });
+       case CSR_CS_ACTION_UNIGNORE:
+               m_db->setDetectedMalwareIgnored(filepath, false);
+               break;
 
-       printCsContext(context);
+       default:
+               ThrowExc(InternalError, "Invalid acation enum val: " <<
+                                static_cast<csr_cs_action_e>(action));
+       }
 
-       CsDetectedPtr detected(new CsDetected());
-       detected->targetName = "test_file";
+       return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
+}
 
-       CsDetectedList list;
-       list.emplace_back(std::move(detected));
+RawBuffer Logic::getDetected(const std::string &filepath)
+{
+       auto row = m_db->getDetectedMalware(filepath);
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, list).pop();
+       if (row)
+               return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
+       else
+               return BinaryQueue::Serialize(CSR_ERROR_NONE, CsDetected()).pop();
 }
 
-RawBuffer Logic::getIgnored(const CsContext &context,
-                                                       const std::string &filepath)
+RawBuffer Logic::getDetectedList(const StrSet &dirSet)
 {
-       INFO("Get Ignored[" << filepath << "] by engine");
+       Db::RowShPtrs rows;
+       std::for_each(dirSet.begin(), dirSet.end(),
+       [this, &rows](const std::string & dir) {
+               for (auto &row : m_db->getDetectedMalwares(dir))
+                       rows.emplace_back(std::move(row));
+       });
 
-       printCsContext(context);
+       return BinaryQueue::Serialize(CSR_ERROR_NONE, rows).pop();
+}
 
-       CsDetected detected;
-       detected.targetName = "test_file";
+// TODO: is this command needed?
+RawBuffer Logic::getIgnored(const std::string &filepath)
+{
+       auto row = m_db->getDetectedMalware(filepath);
 
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, detected).pop();
+       if (row && row->isIgnored)
+               return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
+       else
+               return BinaryQueue::Serialize(CSR_ERROR_NONE, CsDetected()).pop();
 }
 
-RawBuffer Logic::getIgnoredList(const CsContext &context, const StrSet &dirSet)
+RawBuffer Logic::getIgnoredList(const StrSet &dirSet)
 {
-       INFO("Get ignored list logic");
-       std::for_each(dirSet.begin(), dirSet.end(), [](const std::string & dir) {
-               INFO("dir[" << dir << "] in directory set of get ignored list logic");
+       Db::RowShPtrs rows;
+       std::for_each(dirSet.begin(), dirSet.end(),
+       [this, &rows](const std::string & dir) {
+               for (auto &row : m_db->getDetectedMalwares(dir))
+                       if (row->isIgnored)
+                               rows.emplace_back(std::move(row));
        });
 
-       printCsContext(context);
-
-       CsDetectedPtr detected(new CsDetected());
-       detected->targetName = "test_file";
-
-       CsDetectedList list;
-       list.emplace_back(std::move(detected));
-
-       return BinaryQueue::Serialize(CSR_ERROR_NONE, list).pop();
+       return BinaryQueue::Serialize(CSR_ERROR_NONE, rows).pop();
 }
 
 RawBuffer Logic::checkUrl(const WpContext &context, const std::string &url)
index 6201177..3ad467b 100644 (file)
@@ -36,6 +36,8 @@
 #include "service/cs-loader.h"
 #include "service/wp-loader.h"
 
+#include "csr/content-screening-types.h"
+
 namespace Csr {
 
 class Logic {
@@ -50,13 +52,13 @@ private:
 
        RawBuffer scanData(const CsContext &context, const RawBuffer &data);
        RawBuffer scanFile(const CsContext &context, const std::string &filepath);
-       RawBuffer dirGetResults(const CsContext &context, const std::string &dir);
+       RawBuffer scanFileHelper(const CsContext &context, const std::string &filepath);
        RawBuffer dirGetFiles(const CsContext &context, const std::string &dir);
-       RawBuffer judgeStatus(const CsContext &context, const std::string &filepath);
-       RawBuffer getDetected(const CsContext &context, const std::string &filepath);
-       RawBuffer getDetectedList(const CsContext &context, const StrSet &dirSet);
-       RawBuffer getIgnored(const CsContext &context, const std::string &filepath);
-       RawBuffer getIgnoredList(const CsContext &context, const StrSet &dirSet);
+       RawBuffer judgeStatus(const std::string &filepath, csr_cs_action_e action);
+       RawBuffer getDetected(const std::string &filepath);
+       RawBuffer getDetectedList(const StrSet &dirSet);
+       RawBuffer getIgnored(const std::string &filepath);
+       RawBuffer getIgnoredList(const StrSet &dirSet);
 
        RawBuffer checkUrl(const WpContext &context, const std::string &url);
 
index 8a976af..15e90a2 100644 (file)
@@ -47,6 +47,7 @@ typedef enum {
        CSR_ERROR_SERVER                = TIZEN_ERROR_CSR | 0x03,        /**< Server has been failed for some reason */
        CSR_ERROR_NO_TASK               = TIZEN_ERROR_CSR | 0x04,        /**< No Task exists*/
        CSR_ERROR_DB                    = TIZEN_ERROR_CSR | 0x05,        /**< DB transaction error */
+       CSR_ERROR_REMOVE_FAILED         = TIZEN_ERROR_CSR | 0x06,        /**< Removing file or application is failed */
        CSR_ERROR_ENGINE_PERMISSION     = TIZEN_ERROR_CSR | 0x11,        /**< Insufficient permission of engine */
        CSR_ERROR_ENGINE_NOT_EXIST      = TIZEN_ERROR_CSR | 0x12,        /**< No engine exists*/
        CSR_ERROR_ENGINE_DIASABLED      = TIZEN_ERROR_CSR | 0x13,        /**< Engine is in disabled state*/
index b48e867..3ce309e 100644 (file)
@@ -141,7 +141,7 @@ BOOST_AUTO_TEST_CASE(detected_malware_file)
        CHECK_IS_NULL(detected);
 
        auto detectedList = db.getDetectedMalwares("/opt");
-       ASSERT_IF(detectedList->empty(), true);
+       ASSERT_IF(detectedList.empty(), true);
 
        db.insertDetectedMalware(malware1, initDataVersion, false);
        detected = db.getDetectedMalware(malware1.targetName);
@@ -157,9 +157,9 @@ BOOST_AUTO_TEST_CASE(detected_malware_file)
 
        // getDetectedMalwares test
        detectedList = db.getDetectedMalwares("/opt");
-       ASSERT_IF(detectedList->size(), static_cast<size_t>(2));
+       ASSERT_IF(detectedList.size(), static_cast<size_t>(2));
 
-       for (auto &item : *detectedList) {
+       for (auto &item : detectedList) {
                if (malware1.targetName == item->targetName)
                        checkSameMalware(malware1, *item);
                else if (malware2.targetName == item->targetName)
index bdb9a56..1ddd7c4 100644 (file)
 
 #include <string>
 #include <iostream>
+#include <climits>
+#include <ctime>
+#include <cstdio>
 #include <fcntl.h>
-#include <stdio.h>
 #include <sys/stat.h>
-#include <boost/test/unit_test.hpp>
 #include <unistd.h>
-#include <time.h>
 #include <glib.h>
 
 #include <package-manager.h>
 #include <pkgmgr-info.h>
 
+#include <boost/test/unit_test.hpp>
+
 #include "test-common.h"
 
 #define TEST_APP_PKG      TEST_DIR "/maliciousapp.tpk"
@@ -201,32 +203,34 @@ BOOST_AUTO_TEST_CASE(file_visitor_positive_existing)
 {
        std::string file = TEST_WRITE_FILE;
 
-       auto visitor = Csr::createVisitor(file, 0);
+       auto visitor = Csr::createVisitor(file, LONG_MAX);
        CHECK_IS_NOT_NULL(visitor);
-
-       int cnt = 0;
-
-       while (visitor->next())
-               cnt++;
-
-       ASSERT_IF(cnt, 1);
+       CHECK_IS_NOT_NULL(visitor->next());
+       CHECK_IS_NULL(visitor->next());
 }
 
 BOOST_AUTO_TEST_CASE(file_visitor_positive_modified)
 {
        std::string file = TEST_WRITE_FILE;
 
+       auto beforeWrite = time(nullptr);
+
+       sleep(1);
+
        __writeFile(file);
 
-       auto visitor = Csr::createVisitor(file, time(0) - 1);
-       CHECK_IS_NOT_NULL(visitor);
+       sleep(1);
 
-       int cnt = 0;
+       auto afterWrite = time(nullptr);
 
-       while (visitor->next())
-               cnt++;
+       auto visitor = Csr::createVisitor(file, beforeWrite);
+       CHECK_IS_NOT_NULL(visitor);
+       CHECK_IS_NULL(visitor->next());
 
-       ASSERT_IF(cnt, 1);
+       visitor = Csr::createVisitor(file, afterWrite);
+       CHECK_IS_NOT_NULL(visitor);
+       CHECK_IS_NOT_NULL(visitor->next());
+       CHECK_IS_NULL(visitor->next());
 }
 
 BOOST_AUTO_TEST_CASE(file_visitor_negative_non_existing)
index df45cde..775ff03 100644 (file)
@@ -95,15 +95,11 @@ BOOST_AUTO_TEST_CASE(set_callbacks_positive)
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
 
-       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled), CSR_ERROR_NONE);
        ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned), CSR_ERROR_NONE);
 
        EXCEPTION_GUARD_END
 }
@@ -136,15 +132,11 @@ BOOST_AUTO_TEST_CASE(scan_files_async_positive)
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
 
-       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed), CSR_ERROR_NONE);
        ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned), CSR_ERROR_NONE);
 
        const char *files[3] = {
                TEST_DIR "/test_malware_file",
@@ -162,9 +154,12 @@ BOOST_AUTO_TEST_CASE(scan_files_async_positive)
        std::unique_lock<std::mutex> l(testCtx.m);
        testCtx.cv.wait(l);
        l.unlock();
-       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 1 &&
-                                                 testCtx.detectedCnt == 2 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
-                                                 "Async request result isn't expected.");
+
+       ASSERT_IF(testCtx.completedCnt, 1);
+       ASSERT_IF(testCtx.scannedCnt, 1);
+       ASSERT_IF(testCtx.detectedCnt, 2);
+       ASSERT_IF(testCtx.cancelledCnt, 0);
+       ASSERT_IF(testCtx.errorCnt, 0);
 
        EXCEPTION_GUARD_END
 }
@@ -178,15 +173,11 @@ BOOST_AUTO_TEST_CASE(scan_dir_positive)
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
 
-       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed), CSR_ERROR_NONE);
        ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned), CSR_ERROR_NONE);
 
        AsyncTestContext testCtx;
 
@@ -195,9 +186,12 @@ BOOST_AUTO_TEST_CASE(scan_dir_positive)
        std::unique_lock<std::mutex> l(testCtx.m);
        testCtx.cv.wait(l);
        l.unlock();
-       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 0 &&
-                                                 testCtx.detectedCnt == 0 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
-                                                 "Async request result isn't expected.");
+
+       ASSERT_IF(testCtx.completedCnt, 1);
+       ASSERT_IF(testCtx.scannedCnt, 0); // should be 1 after dir_get_files implemented
+       ASSERT_IF(testCtx.detectedCnt, 2);
+       ASSERT_IF(testCtx.cancelledCnt, 0);
+       ASSERT_IF(testCtx.errorCnt, 0);
 
        EXCEPTION_GUARD_END
 }
@@ -209,15 +203,11 @@ BOOST_AUTO_TEST_CASE(scan_dirs_positive)
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
 
-       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_completed(context, on_completed), CSR_ERROR_NONE);
        ASSERT_IF(csr_cs_set_callback_on_error(context, on_error), CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected),
-                         CSR_ERROR_NONE);
-       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned),
-                         CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_cancelled(context, on_cancelled), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_detected(context, on_detected), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_set_callback_on_file_scanned(context, on_scanned), CSR_ERROR_NONE);
 
        AsyncTestContext testCtx;
 
@@ -233,9 +223,12 @@ BOOST_AUTO_TEST_CASE(scan_dirs_positive)
        std::unique_lock<std::mutex> l(testCtx.m);
        testCtx.cv.wait(l);
        l.unlock();
-       BOOST_REQUIRE_MESSAGE(testCtx.completedCnt == 1 && testCtx.scannedCnt == 0 &&
-                                                 testCtx.detectedCnt == 0 && testCtx.cancelledCnt == 0 && testCtx.errorCnt == 0,
-                                                 "Async request result isn't expected.");
+
+       ASSERT_IF(testCtx.completedCnt, 1);
+       ASSERT_IF(testCtx.scannedCnt, 0); // should be 1 after dir_get_files implemented
+       ASSERT_IF(testCtx.detectedCnt, 2);
+       ASSERT_IF(testCtx.cancelledCnt, 0);
+       ASSERT_IF(testCtx.errorCnt, 0);
 
        EXCEPTION_GUARD_END
 }
index f33dfff..16ec3e6 100644 (file)
@@ -167,10 +167,12 @@ BOOST_AUTO_TEST_CASE(get_detected_malware)
        auto context = c.get();
        csr_cs_detected_h detected;
 
-       ASSERT_IF(csr_cs_get_detected_malware(context, "dummy_file_path", &detected),
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected),
                          CSR_ERROR_NONE);
+       CHECK_IS_NOT_NULL(detected);
 
-       // no malware detected
+       ASSERT_IF(csr_cs_get_detected_malware(context, TEST_FILE_RISKY, &detected),
+                         CSR_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
 
        EXCEPTION_GUARD_END
@@ -183,25 +185,25 @@ BOOST_AUTO_TEST_CASE(get_detected_malwares)
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
        csr_cs_detected_list_h detected_list;
+       csr_cs_detected_h detected;
        size_t cnt = 0;
 
-       const char *dirs[5] = {
-               "dummy_dir_path1",
-               "dummy_dir_path2",
-               "dummy_dir_path3",
-               "dummy_dir_path4",
-               "dummy_dir_path5",
+       const char *dirs[1] = {
+               TEST_DIR
        };
 
-       ASSERT_IF(
-               csr_cs_get_detected_malwares(context, dirs, sizeof(dirs) / sizeof(const char *),
-                                                                        &detected_list, &cnt),
-               CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected),
+                         CSR_ERROR_NONE);
+       CHECK_IS_NOT_NULL(detected);
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected),
+                         CSR_ERROR_NONE);
+       CHECK_IS_NOT_NULL(detected);
 
-       // no malware detected
+       ASSERT_IF(csr_cs_get_detected_malwares(context, dirs,
+                                                                                  sizeof(dirs) / sizeof(const char *),
+                                                                                  &detected_list, &cnt), CSR_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected_list);
-
-       ASSERT_IF(cnt, static_cast<size_t>(1));
+       ASSERT_IF(cnt, static_cast<size_t>(2));
 
        EXCEPTION_GUARD_END
 }
@@ -212,12 +214,18 @@ BOOST_AUTO_TEST_CASE(get_ignored_malware)
 
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
-       csr_cs_detected_h ignored;
+       csr_cs_detected_h detected;
 
-       ASSERT_IF(csr_cs_get_ignored_malware(context, "dummy_file_path", &ignored),
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected),
                          CSR_ERROR_NONE);
+       CHECK_IS_NOT_NULL(detected);
+       ASSERT_IF(csr_cs_judge_detected_malware(context, detected,
+                                                                                       CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE);
 
-       CHECK_IS_NOT_NULL(ignored);
+       ASSERT_IF(csr_cs_get_ignored_malware(context, TEST_FILE_MALWARE, &detected),
+                         CSR_ERROR_NONE);
+
+       CHECK_IS_NOT_NULL(detected);
 
        EXCEPTION_GUARD_END
 }
@@ -229,24 +237,29 @@ BOOST_AUTO_TEST_CASE(get_ignored_malwares)
        auto c = Test::Context<csr_cs_context_h>();
        auto context = c.get();
        csr_cs_detected_list_h ignored_list;
+       csr_cs_detected_h detected;
        size_t cnt = 0;
 
-       const char *dirs[5] = {
-               "dummy_dir_path1",
-               "dummy_dir_path2",
-               "dummy_dir_path3",
-               "dummy_dir_path4",
-               "dummy_dir_path5",
+       const char *dirs[1] = {
+               TEST_DIR
        };
 
-       ASSERT_IF(
-               csr_cs_get_ignored_malwares(context, dirs, sizeof(dirs) / sizeof(const char *),
-                                                                       &ignored_list, &cnt),
-               CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected),
+                         CSR_ERROR_NONE);
+       CHECK_IS_NOT_NULL(detected);
+       ASSERT_IF(csr_cs_judge_detected_malware(context, detected,
+                                                                                       CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE);
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_RISKY, &detected),
+                         CSR_ERROR_NONE);
+       CHECK_IS_NOT_NULL(detected);
+       ASSERT_IF(csr_cs_judge_detected_malware(context, detected,
+                                                                                       CSR_CS_ACTION_IGNORE), CSR_ERROR_NONE);
 
+       ASSERT_IF(csr_cs_get_ignored_malwares(context, dirs,
+                                                                                 sizeof(dirs) / sizeof(const char *),
+                                                                                 &ignored_list, &cnt), CSR_ERROR_NONE);
        CHECK_IS_NOT_NULL(ignored_list);
-
-       ASSERT_IF(cnt, static_cast<size_t>(1));
+       ASSERT_IF(cnt, static_cast<size_t>(2));
 
        EXCEPTION_GUARD_END
 }
@@ -259,13 +272,11 @@ BOOST_AUTO_TEST_CASE(judge_detected_malware)
        auto context = c.get();
        csr_cs_detected_h detected;
 
-       ASSERT_IF(csr_cs_get_detected_malware(context, "dummy_file_path", &detected),
+       ASSERT_IF(csr_cs_scan_file(context, TEST_FILE_MALWARE, &detected),
                          CSR_ERROR_NONE);
        CHECK_IS_NOT_NULL(detected);
-
        ASSERT_IF(csr_cs_judge_detected_malware(context, detected,
-                                                                                       CSR_CS_ACTION_REMOVE),
-                         CSR_ERROR_NONE);
+                                                                                       CSR_CS_ACTION_UNIGNORE), CSR_ERROR_NONE);
 
        EXCEPTION_GUARD_END
 }