Refactoring: Some classes renamed to conform to the coding style, Test reports genera...
authorLomtev Dmytro <d.lomtev@samsung.com>
Fri, 14 Jul 2017 10:52:56 +0000 (13:52 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Fri, 14 Jul 2017 10:52:56 +0000 (13:52 +0300)
13 files changed:
device_core/nmdaemon/main.cpp
device_core/nmdaemon/main_thread.cpp
device_core/nmdaemon/main_thread.h
device_core/nmdaemon/proxythread.h
device_core/nmdaemon/report_stub.cpp [new file with mode: 0644]
device_core/nmdaemon/report_stub.h [new file with mode: 0644]
device_core/nmdaemon/thread_base.cpp
device_core/nmdaemon/thread_base.h
device_core/nmdaemon/utils.cpp
device_core/nmdaemon/utils.h
device_core/utest/CMakeLists.txt
device_core/utest/test_IoT.cpp
device_core/utest/test_iot_notification.cpp

index 2296d93..35ef85d 100644 (file)
@@ -26,7 +26,7 @@ using namespace NMD;
 static std::string g_device_name = "Generic device";
 static std::string g_device_model = "Model 1";
 static std::string g_device_type = "iotdevice";
-static main_thread g_main_thread;
+static MainThread g_main_thread;
 
 bool volatile g_running = true;
 
index 073222e..828c5f4 100644 (file)
@@ -1,10 +1,8 @@
 #include <string>
 #include <memory>
-#include <ctime>
-#include "main_thread.h"
 
+#include "main_thread.h"
 #include "utils.h"
-
 #include "hub_resource.h"
 #include "easysetup_server.h"
 #include "reporthandler.h"
 #include "iot_policy_enforce.h"
 #include "proxythread.h"
 #include "iotivity.h"
-#include "RDClient.h"
 #include "control_resource.h"
 #include "agentpolicyservice.h"
+#include "report_stub.h"  // TODO: Remove after migration on report service
 
-using namespace NMD;
 using namespace NetworkManager;
 namespace PH = std::placeholders;
 
 extern bool volatile g_running;
 
-namespace
-{
-
-const std::string CTRL_RESOURCE_URI = "/sec/control";
-const std::string CTRL_RESOURCE_TYPE = "device.control";
-
-/**
- * @brief The TestReport struct used for mocking DPM reports
- */
-struct TestReport
+namespace NMD
 {
-    std::string name;
-    std::string data;
-    int result;
-};
-
-const TestReport test_reports[] = {
-    {
-        "sim",
-        R"({"Content":{"deviceInfo":{"model":"UKS9000","tizenVersion":"2.3","tizenName":"Tizen","platform":"Tizen","manufacturer":"Samsung","cpuArch":"armv7","cpuFreq":"1.5GHz","buildStr":"tztv-2.4-main2016-jazz-m_20160930.1","buildDate":"2016-09-30","buildTime":"04:52:44","duid":"BUYD7RXBSUXKICGD"},"timestamp":"2017-02-22 UTC 11:15:20","appid":"0","nonce":"LocalAttestationNonce","AttReport":{"status":"1","report":{"SIMReport":{"version":"2.0","status":"1","protector":{"EVT":{"symbol":{},"status":"0"},"File":{"symbol":{"/usr/lib/systemd/system":{"attackCount":"1"}},"status":"1"},"PageTable":{"symbol":{},"status":"0"},"StaticMemory":{"symbol":{},"status":"0"},"LSM":{"symbol":{},"status":"0"},"LKM":{"symbol":{},"status":"0"},"VFS":{"symbol":{},"status":"0"},"Netfilter":{"symbol":{},"status":"0"}}}}}},"Integrity":{"Sign":"0"}})",
-        0
-    },
-    {
-        "smart-security",
-        "{\"status\":\"secured\",\"health\": 100}",
-        11
-    }
-};
-
-}
 
-main_thread::main_thread(const std::string& _device_name, const std::string& _device_model, const std::string& _device_type) :
-    thread_base(), m_device_name(_device_name), m_device_model(_device_model), m_device_type(_device_type)
+MainThread::MainThread(const std::string& device_name, const std::string& device_model, const std::string& device_type) :
+    ThreadBase(), m_device_name(device_name), m_device_model(device_model), m_device_type(device_type)
 {
 }
 
-main_thread::~main_thread()
+MainThread::~MainThread()
 {
 }
 
-void main_thread::routine()
+void MainThread::routine()
 {
     write_log("[MAIN_THREADS] started\n");
     std::shared_ptr<OC::OCResource> report_res = nullptr;
@@ -215,8 +184,6 @@ void main_thread::routine()
         AgentPolicyService agent_policy_service(std::bind(&PolicyHandler::enforceCallback, policy_handler.get(), PH::_1, PH::_2));
         std::thread rmi_thread(&AgentPolicyService::run, &agent_policy_service);
 
-        std::srand(time(NULL));
-
         while(m_running)
         {
             std::this_thread::sleep_for(std::chrono::milliseconds(10000));
@@ -225,10 +192,8 @@ void main_thread::routine()
                 hub->findDevices();
             }
 
-            int sel = std::rand() % (sizeof(test_reports) / sizeof(TestReport));
             OCRepresentation rpr;
-            const TestReport& tr = test_reports[sel];
-            rpr.setValue("report", makeReport(iotivity->getDeviceID(), tr.name, tr.result, tr.data));
+            rpr.setValue("report", getReport(iotivity->getDeviceID()));
             rpr.setValue("duid", iotivity->getDeviceID());
 
             if (g_working_mode == WorkingMode::Hub)
@@ -241,9 +206,6 @@ void main_thread::routine()
             }
         }
 
-//        iotivity->unPublishAllResources();
-//        hub->unownAll();
-
         if (proxy_thread)
         {
             proxy_thread->stop();
@@ -261,7 +223,9 @@ void main_thread::routine()
     {
         write_log("[MAIN_THREADS] error - %s\n", _e.what());
     }
+
     write_log("[MAIN_THREADS] stopped\n");
-    std::cout << "Stop!" << std::endl;
     g_running = false;
 }
+
+} // namespace NMD
index 42c5ab3..11d2c24 100644 (file)
@@ -1,36 +1,33 @@
 #ifndef __MAIN_THREAD_H__\r
 #define __MAIN_THREAD_H__\r
 \r
-#include <iostream>\r
 #include <string>\r
-#include <vector>\r
-\r
 #include "thread_base.h"\r
 \r
 namespace NMD\r
 {\r
 \r
-class main_thread : public thread_base\r
+class MainThread : public ThreadBase\r
 {\r
 public:\r
-    main_thread(const std::string& _device_name = "", const std::string& _device_model = "", const std::string& _device_type = "");\r
+    MainThread(const std::string& device_name = "", const std::string& device_model = "", const std::string& device_type = "");\r
 \r
-    void set_device_name(const std::string& _device_name)\r
+    void set_device_name(const std::string& device_name)\r
     {\r
-        m_device_name = _device_name;\r
+        m_device_name = device_name;\r
     }\r
 \r
-    void set_device_model(const std::string& _device_model)\r
+    void set_device_model(const std::string& device_model)\r
     {\r
-        m_device_model = _device_model;\r
+        m_device_model = device_model;\r
     }\r
 \r
-    void set_device_type(const std::string& _device_type)\r
+    void set_device_type(const std::string& device_type)\r
     {\r
-        m_device_type = "oic.d." + _device_type;\r
+        m_device_type = "oic.d." + device_type;\r
     }\r
 \r
-    virtual ~main_thread();\r
+    virtual ~MainThread();\r
 \r
     virtual void routine();\r
 \r
index 5a05a46..c145f86 100644 (file)
@@ -8,7 +8,7 @@
 #include "thread_base.h"
 #include <iostream>
 
-class ProxyThread : public NMD::thread_base
+class ProxyThread : public NMD::ThreadBase
 {
 public:
     ProxyThread()
diff --git a/device_core/nmdaemon/report_stub.cpp b/device_core/nmdaemon/report_stub.cpp
new file mode 100644 (file)
index 0000000..3012f1d
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * @brief  Report creation stub. Used as temporary solutution for test perposes.
+ *         Migrate to normal report service when possible.
+ * @date   Created 14.07.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *         between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *         and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
+ */
+
+#include <cstdlib>
+#include <sstream>
+#include <OCApi.h>
+#include <ctime>
+#include "report_stub.h"
+
+namespace
+{
+
+/**
+ * @brief The TestReport struct used for mocking DPM reports
+ */
+struct TestReport
+{
+    std::string name;
+    std::string data;
+    int result;
+};
+
+const TestReport test_reports[] = {
+    {
+        "sim",
+        R"({"Content":{"deviceInfo":{"model":"UKS9000","tizenVersion":"2.3","tizenName":"Tizen","platform":"Tizen","manufacturer":"Samsung","cpuArch":"armv7","cpuFreq":"1.5GHz","buildStr":"tztv-2.4-main2016-jazz-m_20160930.1","buildDate":"2016-09-30","buildTime":"04:52:44","duid":"BUYD7RXBSUXKICGD"},"timestamp":"2017-02-22 UTC 11:15:20","appid":"0","nonce":"LocalAttestationNonce","AttReport":{"status":"1","report":{"SIMReport":{"version":"2.0","status":"1","protector":{"EVT":{"symbol":{},"status":"0"},"File":{"symbol":{"/usr/lib/systemd/system":{"attackCount":"1"}},"status":"1"},"PageTable":{"symbol":{},"status":"0"},"StaticMemory":{"symbol":{},"status":"0"},"LSM":{"symbol":{},"status":"0"},"LKM":{"symbol":{},"status":"0"},"VFS":{"symbol":{},"status":"0"},"Netfilter":{"symbol":{},"status":"0"}}}}}},"Integrity":{"Sign":"0"}})",
+        0
+    },
+    {
+        "smart-security",
+        "{\"status\":\"secured\",\"health\": 100}",
+        11
+    }
+};
+
+} // namespace
+
+namespace NMD
+{
+
+static std::string currentDateTime();
+
+std::string getReport(const std::string& id)
+{
+    int sel = std::rand() % (sizeof(test_reports) / sizeof(TestReport));
+    const TestReport& tr = test_reports[sel];
+    return makeReport(id, tr.name, tr.result, tr.data);
+}
+
+std::string makeReport(const std::string& id, const std::string& name, int result, const std::string& data)
+{
+    std::ostringstream oss;
+    std::string current_time = currentDateTime();
+    oss << "{\"did\": \"" << id << "\",";
+    oss << "\"date\": \"" << current_time << "\",";
+    oss << "\"name\": \"" << name << "\",";
+    oss << "\"result\": " << result << ",";
+    oss << "\"data\": " << data << "}";
+    return oss.str();
+}
+
+std::string currentDateTime()
+{
+    time_t rawtime;
+    struct tm* timeinfo;
+    char buffer[128];
+    time(&rawtime);
+    timeinfo = localtime(&rawtime);
+    strftime(buffer, sizeof(buffer), "%d-%m-%Y %I:%M:%S", timeinfo);
+    return std::string(buffer);
+}
+
+} // namespace NMD
diff --git a/device_core/nmdaemon/report_stub.h b/device_core/nmdaemon/report_stub.h
new file mode 100644 (file)
index 0000000..12dcb45
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef REPORT_STUB_H
+#define REPORT_STUB_H
+
+#include <string>
+
+namespace NMD
+{
+
+/**
+ * @brief Generate test report
+ * @param id [in] duid value to insert to JSON
+ * @return report JSON
+ */
+std::string getReport(const std::string& id);
+
+/**
+ * @brief Create test report
+ * @param id [in] device identifier
+ * @param name [in] report name
+ * @param result [in] report result
+ * @param data [in] report data
+ * @return  report JSON
+ */
+std::string makeReport(const std::string& id, const std::string& name, int result, const std::string& data);
+
+} // namespace NMD
+
+#endif // REPORT_STUB_H
index 6df08ac..0a9d09e 100644 (file)
 namespace NMD\r
 {\r
 \r
-thread_base::thread_base() : m_running(false)\r
+ThreadBase::ThreadBase() : m_running(false)\r
 {\r
 }\r
 \r
-thread_base::~thread_base()\r
+ThreadBase::~ThreadBase()\r
 {\r
 }\r
 \r
-void thread_base::start()\r
+void ThreadBase::start()\r
 {\r
     m_running = true;\r
-    m_thread = std::thread(&thread_base::routine, this);\r
+    m_thread = std::thread(&ThreadBase::routine, this);\r
 }\r
 \r
-void thread_base::stop()\r
+void ThreadBase::stop()\r
 {\r
     m_running = false;\r
 }\r
 \r
-void thread_base::routine()\r
+void ThreadBase::routine()\r
 {\r
 }\r
 \r
-void thread_base::join()\r
+void ThreadBase::join()\r
 {\r
     if (m_thread.joinable())\r
     {\r
index e8991a6..ea1f8fa 100644 (file)
 namespace NMD\r
 {\r
 \r
-class thread_base\r
+class ThreadBase\r
 {\r
 public:\r
-    thread_base();\r
+    ThreadBase();\r
 \r
-    thread_base(const thread_base&) = delete;\r
+    ThreadBase(const ThreadBase&) = delete;\r
 \r
-    virtual ~thread_base();\r
+    virtual ~ThreadBase();\r
 \r
-    thread_base& operator=(const thread_base&) = delete;\r
+    ThreadBase& operator=(const ThreadBase&) = delete;\r
 \r
     virtual void start();\r
 \r
index e7ae97c..ee1ec67 100644 (file)
@@ -138,27 +138,4 @@ static bool wl(const char* _msg, va_list _vl)
     return res;\r
 }\r
 \r
-static std::string currentDateTime()\r
-{\r
-    time_t rawtime;\r
-    struct tm* timeinfo;\r
-    char buffer[128];\r
-    time(&rawtime);\r
-    timeinfo = localtime(&rawtime);\r
-    strftime(buffer, sizeof(buffer), "%d-%m-%Y %I:%M:%S", timeinfo);\r
-    return std::string(buffer);\r
-}\r
-\r
-std::string makeReport(const std::string& id, const std::string& name, int result, const std::string& data)\r
-{\r
-    std::ostringstream oss;\r
-    std::string current_time = currentDateTime();\r
-    oss << "{\"did\": \"" << id << "\",";\r
-    oss << "\"date\": \"" << current_time << "\",";\r
-    oss << "\"name\": \"" << name << "\",";\r
-    oss << "\"result\": " << result << ",";\r
-    oss << "\"data\": " << data << "}";\r
-    return oss.str();\r
-}\r
-\r
 }\r
index 4519815..b4d6e42 100644 (file)
@@ -57,8 +57,6 @@ void delete_config();
  */\r
 void clear_hub_cache();\r
 \r
-std::string makeReport(const std::string& id, const std::string& name, int result, const std::string& data);\r
-\r
 bool write_log(const char* _msg, ...);\r
 \r
 #if defined(__TIZEN__)\r
index b0a387d..52f3e4e 100644 (file)
@@ -19,6 +19,7 @@ FILE(GLOB SRCS *.cpp
        ../nmdaemon/utils.cpp
        ../nmdaemon/dpm/dpm_api_mapper.cpp
        ../nmdaemon/control_resource.cpp
+       ../nmdaemon/report_stub.cpp
        )
 
 add_executable (${PROJECT_NAME} ${SRCS})
index c056750..e8b1a32 100644 (file)
@@ -24,6 +24,7 @@
 #include "easysetup_server.h"
 #include "iot_resource.h"
 #include "utils.h"
+#include "report_stub.h"
 
 using namespace std;
 using namespace OC;
index 6bac08d..676ee46 100644 (file)
@@ -19,6 +19,7 @@
 #include "nmexceptions.h"
 #include "securitycontext.h"
 #include "utils.h"
+#include "report_stub.h"
 
 using namespace std;
 using namespace NetworkManager;