Minor refactoring. Unit tests for policy enforcement groups added.
authorDmytro Lomtiev <d.lomtev@samsung.com>
Mon, 5 Mar 2018 14:42:26 +0000 (16:42 +0200)
committerDmytro Lomtiev <d.lomtev@samsung.com>
Fri, 16 Mar 2018 14:18:36 +0000 (16:18 +0200)
Change-Id: If3b29f81115189eaeed9b5aeda13fbe8c1bca28e

32 files changed:
device-agent/CMakeLists.txt
device-agent/communication/inc/settings.h
device-agent/communication/src/settings.cpp
device-agent/samonitor/default.conf
device-agent/samonitor/dpm/common_enforce.cpp
device-agent/samonitor/dpm/common_enforce.h
device-agent/samonitor/dpm/dpm_api_mapper.cpp
device-agent/samonitor/dpm/i_policy_group_enforce.h
device-agent/samonitor/dpm/iot_common_enforce.cpp [deleted file]
device-agent/samonitor/dpm/iot_common_enforce.h [deleted file]
device-agent/samonitor/dpm/iot_i_policy_group_enforce.h [deleted file]
device-agent/samonitor/dpm/iot_policy_enforce.cpp [deleted file]
device-agent/samonitor/dpm/iot_policy_enforce.h [deleted file]
device-agent/samonitor/dpm/iot_tvext_enforce.cpp [deleted file]
device-agent/samonitor/dpm/iot_tvext_enforce.h [deleted file]
device-agent/samonitor/dpm/log.h [deleted file]
device-agent/samonitor/dpm/policy_enforce.cpp
device-agent/samonitor/dpm/policy_enforce.h
device-agent/samonitor/dpm/tvext_enforce.cpp
device-agent/samonitor/dpm/tvext_enforce.h
device-agent/samonitor/main_thread.cpp
device-agent/utest/CMakeLists.txt
device-agent/utest/mock/device_policy_manager_mock.h
device-agent/utest/mock/device_policy_manager_stub.cpp
device-agent/utest/mock/device_policy_manager_stub.h
device-agent/utest/mock/policy_enforce_mock.h
device-agent/utest/test_common_enforce.cpp [new file with mode: 0644]
device-agent/utest/test_connection.cpp
device-agent/utest/test_event.cpp
device-agent/utest/test_policy_enforce.cpp
device-agent/utest/test_settings.cpp
device-agent/utest/test_tvext_policy_enforce.cpp [new file with mode: 0644]

index f792c04..da667a4 100644 (file)
@@ -82,7 +82,7 @@ ENDIF(DOXYGEN_FOUND)
 find_program(LCOV NAMES lcov)
 message(STATUS "LCOV: " ${LCOV})
 string(TIMESTAMP TIME "%Y-%m-%d_%H:%M:%S")
-SET(COV_FOLDER "coverage/${TIME}")
+SET(COV_FOLDER "/tmp/coverage/${TIME}")
 if(${LCOV} STREQUAL "LCOV-NOTFOUND")
     message(STATUS "LCOV not found, using GCOVR")
     add_custom_target(coverage
index 2b57dd1..ec23be1 100644 (file)
@@ -65,7 +65,7 @@ public:
      * @brief getKeepAliveTimeout returns KeepAlive timeout period
      * @return KeepAlive timeout period
      */
-    const std::chrono::seconds& getKeepAliveTimeout() const
+    const std::chrono::milliseconds& getKeepAliveTimeout() const
     {
         return keepAliveTimeout;
     }
@@ -101,7 +101,7 @@ public:
      * @brief setKeepAliveTimeout sets KeepAlive timeout period
      * @param keepalive KeepAlive timeout period
      */
-    void setKeepAliveTimeout(const std::chrono::seconds& keepalive)
+    void setKeepAliveTimeout(const std::chrono::milliseconds& keepalive)
     {
         keepAliveTimeout = keepalive;
     }
@@ -156,7 +156,7 @@ private:
     std::string serverAddress;
     std::string deviceId;
     std::string serial;
-    std::chrono::seconds keepAliveTimeout;
+    std::chrono::milliseconds keepAliveTimeout;
     std::string saveFileName;
     bool loaded;
 
index d40e571..35cdd61 100644 (file)
@@ -91,7 +91,7 @@ bool Settings::_load(const std::string& fileName)
         serverAddress = properties.get<std::string>(PROPKEY_SERVER_ADDR, std::string{});
         deviceId = properties.get<std::string>(PROPKEY_DUID, std::string{});
         serial = properties.get<std::string>(PROPKEY_SERIAL, std::string{});
-        keepAliveTimeout = std::chrono::seconds(properties.get<int>(PROPKEY_KEEPALIVE, DEFAULT_KEEPALIVE_SECONDS));
+        keepAliveTimeout = std::chrono::milliseconds(properties.get<int>(PROPKEY_KEEPALIVE, DEFAULT_KEEPALIVE_SECONDS));
         loaded = true;
     } catch (std::exception& e) {
         LOG_E(TAG, "Fail to load configuration from \"%s\". Error: %s", fileName.c_str(), e.what());
index 1f39b66..e257d34 100644 (file)
@@ -1,6 +1,6 @@
 [Server]
 URL=http://106.125.46.74:11000/api/device-service/
-keepalive=5
+keepalive=5000
 [Device]
 type=tv
 model=1
index 8488661..73d7fe7 100644 (file)
@@ -1,25 +1,23 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_common_enforce.cpp
+ * @file   common_enforce.cpp
  * @brief  Implementation of common policy
  * @date   Created Jul 05, 2016
  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
  */
 
-#include "iot_common_enforce.h"
-#include "logging.h"
-#include "dpm_api_mapper.h"
 #include <thread>
 #include <chrono>
+#include "common_enforce.h"
+#include "logging.h"
+#include "samonitor_tag.h"
+#include "dpm_api_mapper.h"
 
-#define TAG "nmdaemon"
 
-namespace iot
-{
 namespace core
 {
 
@@ -100,4 +98,3 @@ bool CommonPolicyEnforce::ParseGroup(Json::Value& groupList)
 CommonPolicyEnforce commonEnforce(PolicyEnforce::GetInstance());
 
 } //namespace core
-} //namespace iot
index 5497650..ad9f497 100644 (file)
@@ -1,24 +1,22 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_common_enforce.h
+ * @file   common_enforce.h
  * @brief  Implementation of common policy
  * @date   Created Oct 04, 2017
  * @author Mail to: <A HREF="mailto:a.zabolotnyi@samsung.com">Andrey Zabolotnyi, a.zabolotnyi@samsung.com</A>
  */
-#ifndef IOT_COMMON_ENFORCE_H
-#define IOT_COMMON_ENFORCE_H
+#ifndef COMMON_ENFORCE_H
+#define COMMON_ENFORCE_H
 
 #include <string>
 
-#include "iot_i_policy_group_enforce.h"
-#include "iot_policy_enforce.h"
+#include "i_policy_group_enforce.h"
+#include "policy_enforce.h"
 
-namespace iot
-{
 namespace core
 {
 
@@ -69,6 +67,5 @@ private:
 };
 
 } //namespace core
-} //namespace iot
 
-#endif // IOT_COMMON_ENFORCE_H
+#endif // COMMON_ENFORCE_H
index 5447e50..8e5c6c0 100644 (file)
@@ -19,9 +19,6 @@
 #include <unistd.h>
 #include <pwd.h>
 
-#if defined(__BUILD_UBUNTU__) || defined(__MOCK_THIRDPARTY__)
-#include "mock/device-policy-manager.h"
-#else
 #include <dpm/device-policy-manager.h>
 #include <dpm/bluetooth.h>
 #include <dpm/wifi.h>
 #include <dpm/application.h>
 #include <dpm/security.h>
 #include <dpm/firewall.h>
-//#include <dpm/tv.h>
-#endif
 
 #include "logging.h"
+#include "samonitor_tag.h"
 #include "dpm_api_mapper.h"
 
-#define TAG "nmdaemon"
-
-//#define USE_MIS
-
 using namespace dpm_api;
 using namespace std;
 
@@ -49,90 +41,47 @@ typedef function<int(dpmh, int)>  ApiInt;
 typedef function<int(dpmh, const char*)>  ApiStr;
 typedef function<int(dpmh, int, const char*)>  ApiIntStr;
 
-#if defined(__BUILD_UBUNTU__) || defined(__MOCK_THIRDPARTY__)
-
-enum {
-    DPM_ERROR_NONE,
-    DPM_ERROR_INVALID_PARAMETER,
-    DPM_ERROR_CONNECTION_REFUSED,
-    DPM_ERROR_TIMED_OUT,
-    DPM_ERROR_PERMISSION_DENIED,
-    DPM_ERROR_NOT_PERMITTED,
-    DPM_ERROR_FILE_EXISTS,
-    DPM_ERROR_OUT_OF_MEMORY,
-    DPM_ERROR_NO_DATA,
-};
-
-#endif
-
-#if !defined(DPM_BUILD_TV) && !defined(__BUILD_UBUNTU__) && !defined(__MOCK_THIRDPARTY__)
-int dpm_restriction_set_usb_client_state(dpmh handle, int allow)
-{
-    return 0;
-}
-int dpm_restriction_get_usb_client_state(dpmh handle, int* is_allowed)
-{
-    return 0;
-}
-int dpm_restriction_set_sound_state(dpmh handle, int allow)
-{
-    return 0;
-}
-int dpm_restriction_get_sound_state(dpmh handle, int* is_allowed)
-{
-    return 0;
-}
-int dpm_restriction_set_tuner_state(dpmh handle, int allow)
-{
-    return 0;
-}
-int dpm_restriction_get_tuner_state(dpmh handle, int* is_allowed)
-{
-    return 0;
-}
-#endif
-
 const map<const string, ApiNone> m = {
-    {"lockout-screen",         dpm_security_lockout_screen},
+    {"lockout-screen",  dpm_security_lockout_screen},
 };
 
 const map<const string, ApiInt> mi = {
-    {"sound",                  dpm_restriction_set_sound_state},
-    {"bluetooth",              dpm_restriction_set_bluetooth_mode_change_state},
-    {"wifi",                   dpm_restriction_set_wifi_state},
-    {"usb",                    dpm_restriction_set_usb_client_state},
-    {"dtv-tunner",             dpm_restriction_set_tuner_state},
-
-    {"camera",                         dpm_restriction_set_camera_state},
-    {"microphone",             dpm_restriction_set_microphone_state},
-    {"location",               dpm_restriction_set_location_state},
-    {"clipboard",              dpm_restriction_set_clipboard_state},
-    {"usb-debug",              dpm_restriction_set_usb_debugging_state},
-    {"wifi-hotspot",   dpm_restriction_set_wifi_hotspot_state},
-    {"bt-tethering",   dpm_restriction_set_bluetooth_tethering_state},
-    {"usb-tethering",  dpm_restriction_set_usb_tethering_state},
-    {"bt-mode-change",         dpm_restriction_set_bluetooth_mode_change_state},
+    {"sound",           dpm_restriction_set_sound_state},
+    {"bluetooth",       dpm_restriction_set_bluetooth_mode_change_state},
+    {"wifi",            dpm_restriction_set_wifi_state},
+    {"usb",             dpm_restriction_set_usb_client_state},
+    {"dtv-tunner",      dpm_restriction_set_tuner_state},
+
+    {"camera",          dpm_restriction_set_camera_state},
+    {"microphone",      dpm_restriction_set_microphone_state},
+    {"location",        dpm_restriction_set_location_state},
+    {"clipboard",       dpm_restriction_set_clipboard_state},
+    {"usb-debug",       dpm_restriction_set_usb_debugging_state},
+    {"wifi-hotspot",    dpm_restriction_set_wifi_hotspot_state},
+    {"bt-tethering",    dpm_restriction_set_bluetooth_tethering_state},
+    {"usb-tethering",   dpm_restriction_set_usb_tethering_state},
+    {"bt-mode-change",  dpm_restriction_set_bluetooth_mode_change_state},
     {"bt-desktop-conn", dpm_restriction_set_bluetooth_desktop_connectivity_state},
-    {"bt-pairing",             dpm_restriction_set_bluetooth_pairing_state},
-    {"email",                  dpm_restriction_set_popimap_email_state},
-    {"browser",                dpm_restriction_set_browser_state},
-    {"bt-mac",                 dpm_bluetooth_set_device_restriction},
-    {"bt-uuid",                dpm_bluetooth_set_uuid_restriction},
-    {"int-stor-enc",   dpm_security_set_internal_storage_encryption},
-    {"ext-stor-enc",   dpm_security_set_external_storage_encryption},
-    {"wifi-profile",   dpm_wifi_set_profile_change_restriction},
-    {"wifi-network",   dpm_wifi_set_network_access_restriction},
+    {"bt-pairing",      dpm_restriction_set_bluetooth_pairing_state},
+    {"email",           dpm_restriction_set_popimap_email_state},
+    {"browser",         dpm_restriction_set_browser_state},
+    {"bt-mac",          dpm_bluetooth_set_device_restriction},
+    {"bt-uuid",         dpm_bluetooth_set_uuid_restriction},
+    {"int-stor-enc",    dpm_security_set_internal_storage_encryption},
+    {"ext-stor-enc",    dpm_security_set_external_storage_encryption},
+    {"wifi-profile",    dpm_wifi_set_profile_change_restriction},
+    {"wifi-network",    dpm_wifi_set_network_access_restriction},
 };
 
 const map<const string, ApiStr> ms = {
-    {"iptables",               dpm_firewall_apply_deny_rules},
+    {"iptables",        dpm_firewall_apply_deny_rules},
 
-    {"bt-mac-add",             dpm_bluetooth_add_device_to_blacklist},
-    {"bt-mac-del",             dpm_bluetooth_remove_device_from_blacklist},
-    {"bt-uuid-add",    dpm_bluetooth_add_uuid_to_blacklist},
-    {"bt-uuid-del",    dpm_bluetooth_remove_uuid_from_blacklist},
-    {"wifi-ssid-add",  dpm_wifi_add_ssid_to_blocklist},
-    {"wifi-ssid-del",  dpm_wifi_remove_ssid_from_blocklist},
+    {"bt-mac-add",      dpm_bluetooth_add_device_to_blacklist},
+    {"bt-mac-del",      dpm_bluetooth_remove_device_from_blacklist},
+    {"bt-uuid-add",     dpm_bluetooth_add_uuid_to_blacklist},
+    {"bt-uuid-del",     dpm_bluetooth_remove_uuid_from_blacklist},
+    {"wifi-ssid-add",   dpm_wifi_add_ssid_to_blocklist},
+    {"wifi-ssid-del",   dpm_wifi_remove_ssid_from_blocklist},
 
 };
 
@@ -141,13 +90,13 @@ const map<const string, ApiIntStr> mis;
 #endif
 
 
-//             {"wipe external data", dpm_security_wipe_data},
-//             {"wipe internal data", dpm_security_wipe_data},
-//             {"messaging",           dpm_restriction_set_messaging_state},
-//             {"privil-add",          dpm_application_add_privilege_to_blacklist},
-//             {"privil-del",          dpm_application_remove_privilege_from_blacklist},
-//             {"package-set", dpm_application_set_mode_restriction},
-//             {"package-unset", dpm_application_unset_mode_restriction},
+//      {"wipe external data", dpm_security_wipe_data},
+//      {"wipe internal data", dpm_security_wipe_data},
+//      {"messaging",       dpm_restriction_set_messaging_state},
+//      {"privil-add",      dpm_application_add_privilege_to_blacklist},
+//      {"privil-del",      dpm_application_remove_privilege_from_blacklist},
+//      {"package-set", dpm_application_set_mode_restriction},
+//      {"package-unset", dpm_application_unset_mode_restriction},
 
 Mapper::Mapper() : handle(nullptr)
 {
@@ -190,23 +139,23 @@ dpm_api::ErrorCode convert_err(int err)
 const char* Mapper::getErrorString(dpm_api::ErrorCode err)
 {
     struct errs {
-        dpm_api::ErrorCode     code;
-        const char*     name;
+        dpm_api::ErrorCode  code;
+        const char*  name;
     };
 
     static const errs errs_arr[] = {
-        {SUCCESS,                              "The operation was successful"},
-        {INVALID_PARAMETER,            "Invalid parameter"},
-        {CONNECTION_REFUSED,           "Connection refused"},
-        {TIMED_OUT,                    "Time out"},
-        {PERMISSION_DENIED,            "Access privilege is not sufficient"},
-        {NOT_PERMITTED,                "Operation not permitted"},
-        {FILE_EXISTS,                  "File exists"},
-        {OUT_OF_MEMORY,                "Out of memory"},
-        {NO_DATA,              "No Data"},
-        {NOT_INITIALIZED,      "Library not initialized"},
-        {NAME_NOT_FOUND,       "Policy's name not found"},
-        {UNKNOWN,                      "Unknown error"},
+        {SUCCESS,               "The operation was successful"},
+        {INVALID_PARAMETER,     "Invalid parameter"},
+        {CONNECTION_REFUSED,    "Connection refused"},
+        {TIMED_OUT,             "Time out"},
+        {PERMISSION_DENIED,     "Access privilege is not sufficient"},
+        {NOT_PERMITTED,         "Operation not permitted"},
+        {FILE_EXISTS,           "File exists"},
+        {OUT_OF_MEMORY,         "Out of memory"},
+        {NO_DATA,               "No Data"},
+        {NOT_INITIALIZED,       "Library not initialized"},
+        {NAME_NOT_FOUND,        "Policy's name not found"},
+        {UNKNOWN,               "Unknown error"},
     };
 
     for (unsigned int i = 0; i < sizeof(errs_arr) / sizeof(errs_arr[0]); ++i) {
index 953eccd..996bdc3 100644 (file)
@@ -1,10 +1,10 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_i_policy_group_enforce.h
+ * @file   i_policy_group_enforce.h
  * @brief  Interface for policy parsing and applying
  * @date   Created May 10, 2017
  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
@@ -18,8 +18,6 @@
 #include <jsoncpp/json/value.h>
 
 
-namespace iot
-{
 namespace core
 {
 
@@ -67,8 +65,6 @@ typedef IPolicyGroupEnforce* IPolicyGroupEnforcePtr;
 
 
 } //namespace core
-} //namespace iot
-
 
 
 #endif //IPOLICY_GRP_APPL_H
diff --git a/device-agent/samonitor/dpm/iot_common_enforce.cpp b/device-agent/samonitor/dpm/iot_common_enforce.cpp
deleted file mode 100644 (file)
index a4aaa67..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_common_enforce.cpp
- * @brief  Implementation of common policy
- * @date   Created Jul 05, 2016
- * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
- */
-
-#include "iot_common_enforce.h"
-#include "logging.h"
-#include "dpm_api_mapper.h"
-#include <thread>
-#include <chrono>
-
-#define TAG "nmdaemon"
-
-namespace iot
-{
-namespace core
-{
-
-CommonPolicyEnforce::CommonPolicyEnforce(PolicyEnforce& enforce)
-    : m_enforce(enforce)
-{
-    LOG_D(TAG, "Register common policy enforce class");
-    m_enforce.RegisterGroup(static_cast<IPolicyGroupEnforce*>(this), std::string("common-policies"));
-}
-
-CommonPolicyEnforce::~CommonPolicyEnforce()
-{
-}
-
-bool CommonPolicyEnforce::Init(/*PolicyContext& context*/)
-{
-    LOG_D(TAG, "Init common policy applier");
-
-    return true;
-}
-
-void CommonPolicyEnforce::Deinit()
-{
-    LOG_D(TAG, "De-Init common policy applier");
-}
-
-bool CommonPolicyEnforce::ParseGroup(Json::Value& groupList)
-{
-    LOG_D(TAG, "...Start common policies parsing and applying...");
-
-    dpm_api::Mapper mapper;
-    bool first_time = true;
-
-    for (auto& node : groupList) {
-        std::string name = node.get("name", "").asString();
-        int         state = node.get("state", 0).asInt();
-        Json::Value items = node.get("items", "");
-
-        if (name.empty()) {
-            LOG_E(TAG, "skiping policy without name.");
-            continue;
-        }
-
-        dpm_api::ErrorCode err;
-
-        std::vector<std::string> v;
-        for (auto& item : items) {
-            v.push_back(item.asString());
-        }
-
-        LOG_D(TAG, "  Enforce policy [%s] to state %d", name.c_str(), state);
-
-        try {
-            if (first_time) {
-                first_time = false;
-            } else {
-                std::this_thread::sleep_for(std::chrono::seconds(1));
-            }
-
-            LOG_D(TAG, "  mapper apply %s to state %d", name.c_str(), state);
-
-            err = mapper.apply(name, state, v);
-
-            if (err != dpm_api::SUCCESS) {
-                LOG_E(TAG, "  Enforce policy [%s] error: %s", name.c_str(), mapper.getErrorString(err));
-            } else {
-                LOG_D(TAG, "  Enforce policy [%s] ok", name.c_str());
-            }
-        } catch (std::exception& e) {
-            LOG_E(TAG, "  Enforce policy [%s] exception: %s", name.c_str(), e.what());
-        }
-    }
-    LOG_D(TAG, "...Finish common policies parsing and applying...");
-
-    return true;
-}
-
-CommonPolicyEnforce commonEnforce(PolicyEnforce::GetInstance());
-
-} //namespace core
-} //namespace iot
diff --git a/device-agent/samonitor/dpm/iot_common_enforce.h b/device-agent/samonitor/dpm/iot_common_enforce.h
deleted file mode 100644 (file)
index 8a09ee5..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_common_enforce.h
- * @brief  Implementation of common policy
- * @date   Created Oct 04, 2017
- * @author Mail to: <A HREF="mailto:a.zabolotnyi@samsung.com">Andrey Zabolotnyi, a.zabolotnyi@samsung.com</A>
- */
-#ifndef IOT_COMMON_ENFORCE_H
-#define IOT_COMMON_ENFORCE_H
-
-#include <string>
-
-#include "iot_i_policy_group_enforce.h"
-#include "iot_policy_enforce.h"
-
-namespace iot
-{
-namespace core
-{
-
-/**
- * @class CommonPolicyEnforce
- * @brief Parse and apply policies to common-policies dpm module
- */
-class CommonPolicyEnforce: public IPolicyGroupEnforce
-{
-
-public:
-    /**
-     * @brief Constructor
-     * @param policy_enforce main policy module
-     */
-    CommonPolicyEnforce(PolicyEnforce& policy_enforce);
-
-    /**
-     * @brief Destructor
-     */
-    virtual ~CommonPolicyEnforce();
-
-    /**
-     * @brief Initialize common policy module.
-     * @return True if success.
-     */
-    bool Init() override;
-
-    /**
-    * @brief Deinitialize common policy module.
-    */
-    void Deinit() override;
-
-    /**
-     * @brief Parse and apply policy
-     * @param policy policy as parsed JSON object
-     * @return True in case of success, false otherwise
-     */
-    bool ParseGroup(Json::Value& policy) override;
-
-private:
-    CommonPolicyEnforce()                                    = delete;   /**< Copy constructor */
-    CommonPolicyEnforce& operator = (const PolicyEnforce&)   = delete;   /**< = operator copy constructor */
-
-
-private:
-    PolicyEnforce&          m_enforce;  /**< link to PolicyEnforce instance */
-};
-
-} //namespace core
-} //namespace iot
-
-#endif // IOT_COMMON_ENFORCE_H
diff --git a/device-agent/samonitor/dpm/iot_i_policy_group_enforce.h b/device-agent/samonitor/dpm/iot_i_policy_group_enforce.h
deleted file mode 100644 (file)
index 717632f..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_i_policy_group_enforce.h
- * @brief  Interface for policy parsing and applying
- * @date   Created May 10, 2017
- * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
- */
-
-
-#ifndef IPOLICY_GRP_ENF_H
-#define IPOLICY_GRP_ENF_H
-
-#include <string>
-#include <jsoncpp/json/value.h>
-
-
-namespace iot
-{
-namespace core
-{
-
-/**
- * @interface IPolicyGroupEnforce
- * @brief interface for policy group applier instances. Each policy group should have own applier implementation.
- * Used by PolicyEnforce class
- * @see PolicyEnforce
- */
-class IPolicyGroupEnforce
-{
-
-public:
-    /**
-     * @brief Destructor
-     */
-    virtual ~IPolicyGroupEnforce() {};
-
-    /**
-     * @brief Init function. Used to init context of related module of policy group
-     * @return True in case of success, false otherwise
-     */
-    virtual bool Init() = 0;
-
-
-    /**
-     * @brief De-Init function. Used to close context of related module of policy group
-     */
-    virtual void Deinit() = 0;
-
-
-    /**
-     * @brief Parse and apply policies related to policy group.
-     * @param policy policy as parsed JSON object
-     * @return True in case of success, false otherwise
-     */
-    virtual bool ParseGroup(Json::Value& policy) = 0;
-};
-
-
-/**
- * @brief Pointer type of IPolicyGroupEnforce interface object instance
- */
-typedef IPolicyGroupEnforce* IPolicyGroupEnforcePtr;
-
-
-} //namespace core
-} //namespace iot
-
-
-
-#endif //IPOLICY_GRP_APPL_H
diff --git a/device-agent/samonitor/dpm/iot_policy_enforce.cpp b/device-agent/samonitor/dpm/iot_policy_enforce.cpp
deleted file mode 100644 (file)
index e16618b..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_policy_enforce.cpp
- * @brief  Policy group agregator
- * @date   Created Jul 01, 2016
- * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
- */
-
-#include "iot_policy_enforce.h"
-#include "iot_tvext_enforce.h"
-
-//#include "tpm_context.h"
-
-#include "logging.h"
-#include <jsoncpp/json/reader.h>
-#include <iostream>
-#define TAG "nmdaemon"
-
-namespace iot
-{
-namespace core
-{
-
-PolicyEnforce::PolicyEnforce()
-    : m_policyGroups()
-{
-}
-
-PolicyEnforce::Result
-PolicyEnforce::ParsePolicy(const std::string& jsonString)
-{
-    LOG_D(TAG, "PolicyEnforce::ParsePolicy");
-    Result result = Result::SUCCESS;
-
-    try {
-        if (jsonString.empty()) {
-            return Result::ERROR_PARSING; //TODO
-        }
-
-        Json::Value policyGroupList;
-        Json::Reader reader;
-
-        bool parsingSuccessful = reader.parse(jsonString.c_str(), policyGroupList);
-
-        if (!parsingSuccessful || policyGroupList.empty()) {
-            return Result::ERROR_PARSING; //TODO
-        }
-
-        for (auto& node : policyGroupList) {
-            std::string groupName = node.get("group", "").asString();
-
-            if (groupName.empty()) {
-                LOG_E(TAG, "No group node in class");
-                return Result::ERROR_PARSING;
-            }
-
-            PolicyGroupMap::iterator it = m_policyGroups.find(groupName);
-
-            if (it == m_policyGroups.end()) {
-                //FIXME Set error if error was occured
-                LOG_E(TAG, "Group \"%s\" not found", groupName.c_str());
-                continue; //TODO
-            }
-
-            IPolicyGroupEnforce& groupEnforce = *(it->second);
-
-            if (!groupEnforce.Init(/*m_context*/)) {
-                LOG_E(TAG, "Group \"%s\" init failed", groupName.c_str());
-                result = Result::ERROR_GROUP;
-                continue; //TODO
-            }
-
-            Json::Value policiesList = node.get("policies", "");
-
-            if (policiesList.empty()) {
-                LOG_E(TAG, "No group node in class");
-                return Result::ERROR_PARSING;
-            }
-
-            if (!groupEnforce.ParseGroup(policiesList)) {
-                LOG_E(TAG, "Failed to apply policy group: %s", groupName.c_str());
-                result = Result::ERROR_GROUP;
-            }
-
-            groupEnforce.Deinit();
-        }
-    } catch (std::exception& e) {
-        LOG_E(TAG, "Failed to apply policy, exception: %s", e.what());
-        LOG_E(TAG, "Policy which caused this exception: %s", jsonString.c_str());
-        result = Result::ERROR_PARSING;
-    }
-
-    return result;
-}
-
-PolicyEnforce& PolicyEnforce::GetInstance()
-{
-    static PolicyEnforce policyEnforce;
-
-    return policyEnforce;
-}
-
-void PolicyEnforce::RegisterGroup(IPolicyGroupEnforcePtr groupEnforce, const std::string& groupName)
-{
-    m_policyGroups[groupName] = groupEnforce;
-}
-
-} //namespace core
-} //namespace iot
-
-
diff --git a/device-agent/samonitor/dpm/iot_policy_enforce.h b/device-agent/samonitor/dpm/iot_policy_enforce.h
deleted file mode 100644 (file)
index 16ae859..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_policy_enforce.h
- * @brief  Policy group agregator
- * @date   Created Jul 01, 2016
- * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
- */
-
-
-#ifndef POLICY_ENF_H
-#define POLICY_ENF_H
-
-#include <string>
-#include <map>
-
-#include "iot_i_policy_group_enforce.h"
-
-
-namespace iot
-{
-namespace core
-{
-
-/**
- * @typedef PolicyGroupMap
- * @brief  Map of policy group appliers instances
- */
-typedef std::map<std::string, IPolicyGroupEnforcePtr> PolicyGroupMap;
-
-/**
- * @class       PolicyEnforce
- * @brief       Parses Policy Config xml string and apply policy groups to related parsers
- */
-class PolicyEnforce
-{
-public:
-
-    /**
-     * @brief        Return enum type for PolicyEnforce errors and policy group appliers
-     */
-    enum class Result {
-        SUCCESS = 0,    /**< Success result         */
-        ERROR_PARSING,  /**< json data parsing error */
-        ERROR_DPM,      /**< Error recived from DPM modules  */
-        ERROR_GROUP     /**< Error init or parse a group  */
-    };
-
-public:
-
-    /**
-     * @brief    Singleton instance recieving method
-     * @return   Link to PolicyEnforce instance
-     */
-    static PolicyEnforce& GetInstance();
-
-    /**
-     * @brief    Register policy group applier object instance for defined policy group
-     * @param policy_group pointer to the applier instance
-     * @param group_name name of the policy group
-     */
-    void RegisterGroup(IPolicyGroupEnforcePtr policy_group, const std::string& group_name);
-
-    /**
-     * @brief Parse string policy and apply policy group nodes to related policy group appliers instances
-     * @param policy JSON string with policy configuration
-     * @return Error code
-     */
-    Result ParsePolicy(const std::string& policy);
-private:
-    PolicyEnforce();                                                    /**< Default constructor    */
-    PolicyEnforce(const PolicyEnforce&)                 = delete;       /**< Copy constructor       */
-    PolicyEnforce& operator = (const PolicyEnforce&)    = delete;       /**< = operator copy constructor*/
-
-private:
-    PolicyGroupMap  m_policyGroups;   /**< Map of policy group appliers instances */
-
-};
-
-} //namespace core
-} //namespace iot
-
-
-
-#endif //POLICY_ENF_H
diff --git a/device-agent/samonitor/dpm/iot_tvext_enforce.cpp b/device-agent/samonitor/dpm/iot_tvext_enforce.cpp
deleted file mode 100644 (file)
index b25fc6e..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_tvext_enforce.cpp
- * @brief  Implementation of TV policy
- * @date   Created Jul 05, 2016
- * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
- */
-
-
-#include "iot_tvext_enforce.h"
-#include "logging.h"
-#include "dpm_api_mapper.h"
-#include <thread>
-#include <chrono>
-
-#define TAG "nmdaemon"
-
-namespace iot
-{
-namespace core
-{
-
-TvExtPolicyEnforce::TvExtPolicyEnforce(PolicyEnforce& enforce)
-    : m_enforce(enforce)
-{
-    LOG_D(TAG, "Register tvext policy enforce class");
-    m_enforce.RegisterGroup(static_cast<IPolicyGroupEnforce*>(this), std::string("tv-extension"));
-}
-
-TvExtPolicyEnforce::~TvExtPolicyEnforce()
-{
-}
-
-bool TvExtPolicyEnforce::Init()
-{
-    LOG_D(TAG, "Init tvext policy applier");
-
-    return true;
-}
-
-void TvExtPolicyEnforce::Deinit()
-{
-    LOG_D(TAG, "De-Init tvext policy applier");
-}
-
-bool TvExtPolicyEnforce::ParseGroup(Json::Value& groupList)
-{
-    LOG_D(TAG, "...Start tvext policies parsing and applying...");
-
-    dpm_api::Mapper mapper;
-    bool first_time = true;
-
-    for (auto& node : groupList) {
-        std::string name = node.get("name", "").asString();
-        int         state = node.get("state", 0).asInt();
-        Json::Value items = node.get("items", "");
-
-        if (name.empty()) {
-            LOG_E(TAG, "skiping policy without name.");
-            continue;
-        }
-
-        dpm_api::ErrorCode err;
-
-        std::vector<std::string> v;
-        for (auto& item : items) {
-            v.push_back(item.asString());
-        }
-
-        LOG_D(TAG, "  Enforce policy [%s] to state %d", name.c_str(), state);
-
-        try {
-            if (first_time) {
-                first_time = false;
-            } else {
-                std::this_thread::sleep_for(std::chrono::seconds(1));
-            }
-
-            LOG_D(TAG, "  mapper apply %s to state %d", name.c_str(), state);
-
-            err = mapper.apply(name, state, v);
-
-            if (err != dpm_api::SUCCESS) {
-                LOG_E(TAG, "  Enforce policy [%s] error: %s", name.c_str(), mapper.getErrorString(err));
-            } else {
-                LOG_D(TAG, "  Enforce policy [%s] ok", name.c_str());
-            }
-        } catch (std::exception& e) {
-            LOG_E(TAG, "  Enforce policy [%s] exception: %s", name.c_str(), e.what());
-        }
-    }
-    LOG_D(TAG, "...Finish tvext policies parsing and applying...");
-
-    return true;
-}
-
-TvExtPolicyEnforce tvextEnforce(PolicyEnforce::GetInstance());
-
-} //namespace core
-} //namespace iot
diff --git a/device-agent/samonitor/dpm/iot_tvext_enforce.h b/device-agent/samonitor/dpm/iot_tvext_enforce.h
deleted file mode 100644 (file)
index f7f68be..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   iot_tvext_enforce.h
- * @brief  Implementation of TV policy
- * @date   Created Jul 05, 2016
- * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
- */
-
-#ifndef TVEXT_APPL_H
-#define TVEXT_APPL_H
-
-#include <string>
-
-#include "iot_i_policy_group_enforce.h"
-#include "iot_policy_enforce.h"
-
-namespace iot
-{
-namespace core
-{
-
-/**
- * @class TvExtPolicyEnforce
- * @brief Parse and apply policies to tv extension dpm module
- */
-class TvExtPolicyEnforce: public IPolicyGroupEnforce
-{
-
-public:
-    /**
-     * @brief Default constructor
-     * @param policy_enforce main policy module
-     */
-    TvExtPolicyEnforce(PolicyEnforce& policy_enforce);
-
-    /**
-     * @brief Destructor
-     */
-    virtual ~TvExtPolicyEnforce();
-
-    /**
-     * @brief Init function. Used to init context of TV extensions policy group
-     * @return True in case of success, false otherwise
-     */
-    bool Init() override;
-
-    /**
-     * @brief De-Init function. Used to close tpm tvext context.
-     */
-    void Deinit() override;
-
-    /**
-     * @brief Parse and apply policy to tvext tpm module.
-     * @param policy policy as parsed JSON object
-     * @return True in case of success, false otherwise
-     */
-    bool ParseGroup(Json::Value& policy) override;
-
-private:
-    TvExtPolicyEnforce()                                    = delete;   /**< Copy constructor */
-    TvExtPolicyEnforce& operator = (const PolicyEnforce&)   = delete;   /**< = operator copy constructor */
-
-
-private:
-    PolicyEnforce&          m_enforce;  /**< link to PolicyEnforce instance */
-
-};
-
-} //namespace core
-} //namespace iot
-
-
-
-#endif //TVEXT_ENF_H
diff --git a/device-agent/samonitor/dpm/log.h b/device-agent/samonitor/dpm/log.h
deleted file mode 100644 (file)
index 387807a..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
- * Samsung Ukraine R&D Center (SRK under a contract between)
- * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- */
-/**
- * @file   log.h
- * @brief  Logging macro
- * @date   Created May 19, 2017
- * @author Mail to: <A HREF="mailto:i.metelytsia@samsung.com">Iurii Metelytsia, i.metelytsia@samsung.com</A>
- * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
- */
-#include <stdio.h>
-
-/**
- * @brief Error logging
- */
-#ifndef LOG_ERR
-#define LOG_ERR(format, args...) printf("ERR  --  %s [%s:%d] " format "\n",__FILE__, __func__, __LINE__, ##args)
-#endif
-
-/**
- * @brief Debug logging
- */
-#ifndef LOG_DBG
-#define LOG_DBG(format, args...) printf("DBG  --  %s [%s:%d] " format "\n",__FILE__, __func__, __LINE__, ##args)
-#endif
-
-
-
index a113684..6089e75 100644 (file)
@@ -1,27 +1,22 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_policy_enforce.cpp
+ * @file   policy_enforce.cpp
  * @brief  Policy group agregator
  * @date   Created Jul 01, 2016
  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
  */
 
-#include "iot_policy_enforce.h"
-#include "iot_tvext_enforce.h"
-
-//#include "tpm_context.h"
-
+#include "policy_enforce.h"
+#include "tvext_enforce.h"
 #include "logging.h"
+#include "samonitor_tag.h"
 #include <jsoncpp/json/reader.h>
 #include <iostream>
-#define TAG "nmdaemon"
 
-namespace iot
-{
 namespace core
 {
 
@@ -110,6 +105,3 @@ void PolicyEnforce::RegisterGroup(IPolicyGroupEnforcePtr groupEnforce, const std
 }
 
 } //namespace core
-} //namespace iot
-
-
index dacc2a2..948c912 100644 (file)
@@ -1,10 +1,10 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_policy_enforce.h
+ * @file   policy_enforce.h
  * @brief  Policy group agregator
  * @date   Created Jul 01, 2016
  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
 #include <string>
 #include <map>
 
-#include "iot_i_policy_group_enforce.h"
+#include "i_policy_group_enforce.h"
 
 
-namespace iot
-{
 namespace core
 {
 
@@ -81,8 +79,5 @@ private:
 };
 
 } //namespace core
-} //namespace iot
-
-
 
 #endif //POLICY_ENF_H
index 60d669d..f4b2b06 100644 (file)
@@ -1,26 +1,23 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_tvext_enforce.cpp
+ * @file   tvext_enforce.cpp
  * @brief  Implementation of TV policy
  * @date   Created Jul 05, 2016
  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
  */
 
 
-#include "iot_tvext_enforce.h"
+#include "tvext_enforce.h"
 #include "logging.h"
+#include "samonitor_tag.h"
 #include "dpm_api_mapper.h"
 #include <thread>
 #include <chrono>
 
-#define TAG "nmdaemon"
-
-namespace iot
-{
 namespace core
 {
 
@@ -101,4 +98,3 @@ bool TvExtPolicyEnforce::ParseGroup(Json::Value& groupList)
 TvExtPolicyEnforce tvextEnforce(PolicyEnforce::GetInstance());
 
 } //namespace core
-} //namespace iot
index cd9e096..dba136a 100644 (file)
@@ -1,10 +1,10 @@
 /**
  * Samsung Ukraine R&D Center (SRK under a contract between)
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
- * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
 /**
- * @file   iot_tvext_enforce.h
+ * @file   tvext_enforce.h
  * @brief  Implementation of TV policy
  * @date   Created Jul 05, 2016
  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
 
 #include <string>
 
-#include "iot_i_policy_group_enforce.h"
-#include "iot_policy_enforce.h"
+#include "i_policy_group_enforce.h"
+#include "policy_enforce.h"
 
-namespace iot
-{
 namespace core
 {
 
@@ -71,8 +69,5 @@ private:
 };
 
 } //namespace core
-} //namespace iot
-
-
 
 #endif //TVEXT_ENF_H
index 2ef8977..10f3797 100644 (file)
 #include <restservice.h>
 #include "main_thread.h"
 #include "utils.h"
-#include "iot_policy_enforce.h"
+#include "policy_enforce.h"
 #include "proxythread.h"
 #include "audit_trail_client.h"
 #include "application_service.h"
 #include "reportadapter.h"
 
 #include "logging.h"
-#define TAG "SAMonitor"
+#include "samonitor_tag.h"
 
 
 namespace PH = std::placeholders;
index e1a4cfa..67d71f7 100644 (file)
@@ -20,7 +20,7 @@ include_directories (
 
 include_directories(BEFORE ../mock)
 
-FILE(GLOB SRCS *.cpp ../samonitor/*.cpp mock/*.cpp)
+FILE(GLOB SRCS *.cpp ../samonitor/*.cpp ../samonitor/dpm/*.cpp mock/*.cpp)
 
 FILE(GLOB DAEMON_MAIN ../samonitor/main.cpp)
 FILE(GLOB DAEMON_MAIN_THREAD ../samonitor/main_thread.cpp)
@@ -38,7 +38,6 @@ set (TEST_LINK_LIBRARIES ${GTEST_LIB}
        jsoncpp
        boost_system boost_thread boost_serialization
        dlog
-       dpm
        curl
        capi-system-info
 )
index 530ea76..4a05c1d 100644 (file)
@@ -2,7 +2,8 @@
 #define DEVICEPOLICYMANAGERMOCK_H
 
 #include <gmock/gmock.h>
-#include "mock/device-policy-manager.h"
+#include <dpm/device-policy-manager.h>
+#include <device_policy_manager_stub.h>
 
 class DPMMock: public DPMInterface
 {
@@ -18,7 +19,7 @@ public:
     }
 
     MOCK_METHOD0(dpm_manager_create, device_policy_manager_h());
-    MOCK_METHOD0(dpm_manager_destroy, void());
+    MOCK_METHOD0(dpm_manager_destroy, int());
 
     MOCK_METHOD1(dpm_application_install_package, int(const char* app_name));
     MOCK_METHOD1(dpm_application_uninstall_package, int(const char* app_name));
index eb402a6..db2c476 100644 (file)
@@ -1,7 +1,13 @@
 #include <cassert>
-#include "mock/device-policy-manager.h"
+#include <dpm/administration.h>
+#include <dpm/application.h>
+#include <dpm/bluetooth.h>
+#include <dpm/firewall.h>
+#include <dpm/restriction.h>
+#include <dpm/security.h>
+#include <dpm/wifi.h>
 
-#define TAG "nmdaemon"
+#include "device_policy_manager_stub.h"
 
 namespace
 {
@@ -23,238 +29,238 @@ void dpm_restore_implementation()
 device_policy_manager_h dpm_manager_create()
 {
     assert(dpm_impl != nullptr);
-    return dpm_impl->dpm_manager_create();
+    return (device_policy_manager_h)dpm_impl->dpm_manager_create();
 }
 
-void dpm_manager_destroy(device_policy_manager_h handle)
+int dpm_manager_destroy(device_policy_manager_h handle)
 {
     assert(handle != nullptr);
-    handle->dpm_manager_destroy();
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_manager_destroy();
 }
 
 int dpm_application_install_package(device_policy_manager_h handle, const char* package_name)
 {
     assert(handle != nullptr);
-    return handle->dpm_application_install_package(package_name);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_application_install_package(package_name);
 }
 
 int dpm_application_uninstall_package(device_policy_manager_h handle, const char* package_name)
 {
     assert(handle != nullptr);
-    return handle->dpm_application_uninstall_package(package_name);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_application_uninstall_package(package_name);
 }
 
 int dpm_security_lockout_screen(device_policy_manager_h handle)
 {
     assert(handle != nullptr);
-    return handle->dpm_security_lockout_screen();
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_security_lockout_screen();
 }
 
 int dpm_restriction_set_camera_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_camera_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_camera_state(state);
 }
 int dpm_restriction_set_microphone_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_microphone_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_microphone_state(state);
 }
 int dpm_restriction_set_location_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_location_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_location_state(state);
 }
 int dpm_restriction_set_external_storage_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_external_storage_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_external_storage_state(state);
 }
 int dpm_restriction_set_clipboard_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_clipboard_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_clipboard_state(state);
 }
 int dpm_restriction_set_usb_debugging_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_usb_debugging_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_usb_debugging_state(state);
 }
 int dpm_restriction_set_wifi_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_wifi_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_wifi_state(state);
 }
 int dpm_restriction_set_wifi_hotspot_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_wifi_hotspot_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_wifi_hotspot_state(state);
 }
 int dpm_restriction_set_bluetooth_tethering_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_bluetooth_tethering_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_bluetooth_tethering_state(state);
 }
 int dpm_restriction_set_usb_tethering_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_usb_tethering_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_usb_tethering_state(state);
 }
 int dpm_restriction_set_bluetooth_mode_change_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_bluetooth_mode_change_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_bluetooth_mode_change_state(state);
 }
 int dpm_restriction_set_bluetooth_desktop_connectivity_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_bluetooth_desktop_connectivity_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_bluetooth_desktop_connectivity_state(state);
 }
 int dpm_restriction_set_bluetooth_pairing_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_bluetooth_pairing_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_bluetooth_pairing_state(state);
 }
 int dpm_restriction_set_popimap_email_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_popimap_email_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_popimap_email_state(state);
 }
 int dpm_restriction_set_browser_state(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_browser_state(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_browser_state(state);
 }
 int dpm_bluetooth_set_device_restriction(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_bluetooth_set_device_restriction(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_bluetooth_set_device_restriction(state);
 }
 int dpm_bluetooth_set_uuid_restriction(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_bluetooth_set_uuid_restriction(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_bluetooth_set_uuid_restriction(state);
 }
 int dpm_security_set_internal_storage_encryption(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_security_set_internal_storage_encryption(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_security_set_internal_storage_encryption(state);
 }
 int dpm_security_set_external_storage_encryption(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_security_set_external_storage_encryption(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_security_set_external_storage_encryption(state);
 }
 int dpm_wifi_set_profile_change_restriction(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_wifi_set_profile_change_restriction(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_wifi_set_profile_change_restriction(state);
 }
 int dpm_wifi_set_network_access_restriction(device_policy_manager_h handle, int state)
 {
     assert(handle != nullptr);
-    return handle->dpm_wifi_set_network_access_restriction(state);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_wifi_set_network_access_restriction(state);
 }
 
 int dpm_bluetooth_add_device_to_blacklist(device_policy_manager_h handle, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_bluetooth_add_device_to_blacklist(item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_bluetooth_add_device_to_blacklist(item);
 }
 int dpm_bluetooth_remove_device_from_blacklist(device_policy_manager_h handle, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_bluetooth_remove_device_from_blacklist(item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_bluetooth_remove_device_from_blacklist(item);
 }
 int dpm_bluetooth_add_uuid_to_blacklist(device_policy_manager_h handle, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_bluetooth_add_uuid_to_blacklist(item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_bluetooth_add_uuid_to_blacklist(item);
 }
 int dpm_bluetooth_remove_uuid_from_blacklist(device_policy_manager_h handle, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_bluetooth_remove_uuid_from_blacklist(item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_bluetooth_remove_uuid_from_blacklist(item);
 }
 int dpm_wifi_add_ssid_to_blocklist(device_policy_manager_h handle, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_wifi_add_ssid_to_blocklist(item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_wifi_add_ssid_to_blocklist(item);
 }
 int dpm_wifi_remove_ssid_from_blocklist(device_policy_manager_h handle, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_wifi_remove_ssid_from_blocklist(item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_wifi_remove_ssid_from_blocklist(item);
 }
 
 int dpm_security_wipe_data(device_policy_manager_h handle, int state, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_security_wipe_data(state, item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_security_wipe_data(state, item);
 }
 int dpm_restriction_set_messaging_state(device_policy_manager_h handle, int state, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_messaging_state(state, item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_messaging_state(state, item);
 }
 int dpm_application_add_privilege_to_blacklist(device_policy_manager_h handle, int state, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_application_add_privilege_to_blacklist(state, item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_application_add_privilege_to_blacklist(state, item);
 }
 int dpm_application_remove_privilege_from_blacklist(device_policy_manager_h handle, int state, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_application_remove_privilege_from_blacklist(state, item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_application_remove_privilege_from_blacklist(state, item);
 }
 int dpm_application_set_mode_restriction(device_policy_manager_h handle, int state, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_application_set_mode_restriction(state, item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_application_set_mode_restriction(state, item);
 }
 int dpm_application_unset_mode_restriction(device_policy_manager_h handle, int state, const char* item)
 {
     assert(handle != nullptr);
-    return handle->dpm_application_unset_mode_restriction(state, item);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_application_unset_mode_restriction(state, item);
 }
 int dpm_firewall_apply_deny_rules(device_policy_manager_h handle, const char* rules)
 {
     assert(handle != nullptr);
-    return handle->dpm_firewall_apply_deny_rules(rules);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_firewall_apply_deny_rules(rules);
 }
 int dpm_firewall_flush_deny_rules(device_policy_manager_h handle)
 {
     assert(handle != nullptr);
-    return handle->dpm_firewall_flush_deny_rules();
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_firewall_flush_deny_rules();
 }
 
 int dpm_restriction_set_usb_client_state(device_policy_manager_h handle, int allow)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_usb_client_state(allow);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_usb_client_state(allow);
 }
 int dpm_restriction_get_usb_client_state(device_policy_manager_h handle, int* is_allowed)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_get_usb_client_state(is_allowed);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_get_usb_client_state(is_allowed);
 }
 int dpm_restriction_set_sound_state(device_policy_manager_h handle, int allow)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_sound_state(allow);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_sound_state(allow);
 }
 int dpm_restriction_get_sound_state(device_policy_manager_h handle, int* is_allowed)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_get_sound_state(is_allowed);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_get_sound_state(is_allowed);
 }
 int dpm_restriction_set_tuner_state(device_policy_manager_h handle, int allow)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_set_tuner_state(allow);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_set_tuner_state(allow);
 }
 int dpm_restriction_get_tuner_state(device_policy_manager_h handle, int* is_allowed)
 {
     assert(handle != nullptr);
-    return handle->dpm_restriction_get_tuner_state(is_allowed);
+    return reinterpret_cast<DPMInterface*>(handle)->dpm_restriction_get_tuner_state(is_allowed);
 }
index 1d6a7e6..97d2404 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef DEVICE_POLICY_MANAGER_H
 #define DEVICE_POLICY_MANAGER_H
 
-#include <cassert>
-
 /**
  * @brief The DPMInterface class represents supported DPM features
  */
@@ -13,12 +11,12 @@ public:
     {
     }
 
-    virtual DPMInterface* dpm_manager_create()
+    virtual void* dpm_manager_create()
     {
         return this;
     }
 
-    virtual void dpm_manager_destroy() = 0;
+    virtual int dpm_manager_destroy() = 0;
 
     virtual int dpm_application_install_package(const char* app_name) = 0;
     virtual int dpm_application_uninstall_package(const char* app_name) = 0;
@@ -87,8 +85,9 @@ public:
         return 0;
     }
 
-    void dpm_manager_destroy() override
+    int dpm_manager_destroy() override
     {
+        return 0;
     }
 
     int dpm_security_lockout_screen() override
@@ -264,8 +263,6 @@ public:
     }
 };
 
-typedef DPMInterface* device_policy_manager_h;
-
 /**
  * @brief Set custom DPM implementation to change DPM functionality. Can be used for testing.
  * @param dpm [in] pointer to the object implementing DPMInterface.
@@ -277,64 +274,4 @@ void dpm_set_custom_implementation(DPMInterface* dpm);
  */
 void dpm_restore_implementation();
 
-extern "C"
-{
-
-    device_policy_manager_h dpm_manager_create();
-
-    void dpm_manager_destroy(device_policy_manager_h handle);
-
-    int dpm_application_install_package(device_policy_manager_h handle, const char* package_name);
-
-    int dpm_application_uninstall_package(device_policy_manager_h handle, const char* package_name);
-
-    int dpm_security_lockout_screen(device_policy_manager_h);
-
-    int dpm_restriction_set_camera_state(device_policy_manager_h, int);
-    int dpm_restriction_set_microphone_state(device_policy_manager_h, int);
-    int dpm_restriction_set_location_state(device_policy_manager_h, int);
-    int dpm_restriction_set_external_storage_state(device_policy_manager_h, int);
-    int dpm_restriction_set_clipboard_state(device_policy_manager_h, int);
-    int dpm_restriction_set_usb_debugging_state(device_policy_manager_h, int);
-    int dpm_restriction_set_wifi_state(device_policy_manager_h, int);
-    int dpm_restriction_set_wifi_hotspot_state(device_policy_manager_h, int);
-    int dpm_restriction_set_bluetooth_tethering_state(device_policy_manager_h, int);
-    int dpm_restriction_set_usb_tethering_state(device_policy_manager_h, int);
-    int dpm_restriction_set_bluetooth_mode_change_state(device_policy_manager_h, int);
-    int dpm_restriction_set_bluetooth_desktop_connectivity_state(device_policy_manager_h, int);
-    int dpm_restriction_set_bluetooth_pairing_state(device_policy_manager_h, int);
-    int dpm_restriction_set_popimap_email_state(device_policy_manager_h, int);
-    int dpm_restriction_set_browser_state(device_policy_manager_h, int);
-    int dpm_bluetooth_set_device_restriction(device_policy_manager_h, int);
-    int dpm_bluetooth_set_uuid_restriction(device_policy_manager_h, int);
-    int dpm_security_set_internal_storage_encryption(device_policy_manager_h, int);
-    int dpm_security_set_external_storage_encryption(device_policy_manager_h, int);
-    int dpm_wifi_set_profile_change_restriction(device_policy_manager_h, int);
-    int dpm_wifi_set_network_access_restriction(device_policy_manager_h, int);
-
-    int dpm_bluetooth_add_device_to_blacklist(device_policy_manager_h, const char*);
-    int dpm_bluetooth_remove_device_from_blacklist(device_policy_manager_h, const char*);
-    int dpm_bluetooth_add_uuid_to_blacklist(device_policy_manager_h, const char*);
-    int dpm_bluetooth_remove_uuid_from_blacklist(device_policy_manager_h, const char*);
-    int dpm_wifi_add_ssid_to_blocklist(device_policy_manager_h, const char*);
-    int dpm_wifi_remove_ssid_from_blocklist(device_policy_manager_h, const char*);
-
-    int dpm_security_wipe_data(device_policy_manager_h, int, const char*);
-    int dpm_restriction_set_messaging_state(device_policy_manager_h, int, const char*);
-    int dpm_application_add_privilege_to_blacklist(device_policy_manager_h, int, const char*);
-    int dpm_application_remove_privilege_from_blacklist(device_policy_manager_h, int, const char*);
-    int dpm_application_set_mode_restriction(device_policy_manager_h, int, const char*);
-    int dpm_application_unset_mode_restriction(device_policy_manager_h, int, const char*);
-    int dpm_firewall_apply_deny_rules(device_policy_manager_h handle, const char* rules);
-    int dpm_firewall_flush_deny_rules(device_policy_manager_h handle);
-
-    int dpm_restriction_set_usb_client_state(device_policy_manager_h handle, int allow);
-    int dpm_restriction_get_usb_client_state(device_policy_manager_h handle, int* is_allowed);
-    int dpm_restriction_set_sound_state(device_policy_manager_h handle, int allow);
-    int dpm_restriction_get_sound_state(device_policy_manager_h handle, int* is_allowed);
-    int dpm_restriction_set_tuner_state(device_policy_manager_h handle, int allow);
-    int dpm_restriction_get_tuner_state(device_policy_manager_h handle, int* is_allowed);
-
-}
-
 #endif // DEVICE_POLICY_MANAGER_H
index a0c3131..f8e3308 100644 (file)
@@ -1,11 +1,11 @@
 #ifndef IOT_POLICY_ENFORCE_MOCK_H
 #define IOT_POLICY_ENFORCE_MOCK_H
 
-#include "iot_policy_enforce.h"
+#include "policy_enforce.h"
 /**
  * @brief The IPolicyGroupEnforceMock class used as mock for policy group
  */
-class IPolicyGroupEnforceMock: public iot::core::IPolicyGroupEnforce
+class IPolicyGroupEnforceMock: public core::IPolicyGroupEnforce
 {
 public:
     MOCK_METHOD0(Init, bool());
diff --git a/device-agent/utest/test_common_enforce.cpp b/device-agent/utest/test_common_enforce.cpp
new file mode 100644 (file)
index 0000000..f7897c8
--- /dev/null
@@ -0,0 +1,38 @@
+#include <jsoncpp/json/reader.h>
+#include "device_policy_manager_mock.h"
+#include "common_enforce.h"
+
+using namespace core;
+using ::testing::_;
+using ::testing::Return;
+
+namespace
+{
+const std::string policy = R"-([{"name":"sound","state":1,"items":[]},
+                           {"name":"camera","state":0,"items":[]},
+                           {"name":"iptables","state":1,"items":["127.0.0.0/24|UDP|10-1024","1.1.1.1|TCP|80,443","8.8.8.8"]}])-";
+}
+
+TEST(Test_CommonPolicyEnforce, test_ParseGroup)
+{
+    DPMMock dpm;
+
+    CommonPolicyEnforce comm_pe(PolicyEnforce::GetInstance());
+
+    EXPECT_CALL(dpm, dpm_manager_create()).WillOnce(Return(&dpm));
+
+    EXPECT_CALL(dpm, dpm_restriction_set_sound_state(1)).WillOnce(Return(0));
+
+    EXPECT_CALL(dpm, dpm_restriction_set_camera_state(0)).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_manager_destroy()).Times(1);
+    EXPECT_CALL(dpm, dpm_firewall_flush_deny_rules()).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_firewall_apply_deny_rules(_)).Times(3).WillRepeatedly(Return(0));
+
+    EXPECT_TRUE(comm_pe.Init());
+    Json::Value root;
+    Json::Reader reader;
+    ASSERT_TRUE(reader.parse(policy, root));
+    EXPECT_TRUE(comm_pe.ParseGroup(root));
+
+    EXPECT_NO_THROW(comm_pe.Deinit());
+}
index 19b7d27..9abf706 100644 (file)
@@ -11,12 +11,13 @@ using namespace NetworkManager;
 using ::testing::_;
 using ::testing::Eq;
 using ::testing::Return;
+using ::testing::Throw;
 
 #define TAG "Tests"
 
 namespace
 {
-const std::chrono::seconds TEST_KEEP_ALIVE(1);
+const std::chrono::milliseconds TEST_KEEP_ALIVE(10);
 const std::string TEST_SERVER_ADDRESS{"test-server"};
 const std::string TEST_DEVICE_ID{"device-id"};
 
@@ -68,14 +69,29 @@ public:
     MOCK_METHOD1(accept, void(const Event& event));
 };
 
+namespace NetworkManager
+{
+
+bool operator==(const SessionInfo& si1, const SessionInfo& si2)
+{
+    return si1.duid == si2.duid && si1.authToken == si2.authToken;
+}
+
+}
+
 TEST_F(TestConnection, test_signal)
 {
     RestServiceMock rest;
+    settings.setDeviceId("");
     Connection conn(settings, &rest);
     ReportComposer rc;
     rc.addEvent(Event(TEST_EVENT_TYPE, TEST_EVENT_DATA1));
     rc.addEvent(Event(TEST_EVENT_TYPE, TEST_EVENT_DATA2));
+    SessionInfo checkSessState{"", ""};
 
+    EXPECT_CALL(rest, registerDevice(Eq(checkSessState), _))
+            .WillOnce(Throw(std::runtime_error("")))
+            .WillOnce(Return(TEST_DEVICE_ID));
     EXPECT_CALL(rest, getUpdates(_))
             .WillRepeatedly(Return(""));
     EXPECT_CALL(rest, sendReport(_, ::testing::Matcher<const Json::Value&>(rc.get())))
@@ -87,7 +103,7 @@ TEST_F(TestConnection, test_signal)
     conn.signal(ev);
     conn.signal(Event(TEST_EVENT_TYPE, TEST_EVENT_DATA2));
 
-    std::this_thread::sleep_for(std::chrono::milliseconds(1100));
+    std::this_thread::sleep_for(std::chrono::milliseconds(70));
 
     conn.stop();
 
@@ -123,7 +139,7 @@ TEST_F(TestConnection, test_Listeners)
 
     std::thread t(&Connection::loop, &conn);
 
-    std::this_thread::sleep_for(std::chrono::milliseconds(1200));
+    std::this_thread::sleep_for(std::chrono::milliseconds(15));
 
     conn.stop();
 
index a576e77..84123a5 100644 (file)
@@ -45,6 +45,8 @@ TEST(TestEvent, test_construct_assign)
     ASSERT_EQ(TEST_CONN, (int)ev3.connection);
 
     Event ev4("", "");
+    ASSERT_TRUE(ev4.confirm.empty());
+    ASSERT_EQ(nullptr, ev4.connection);
     ev4 = ev3;
 
     ASSERT_EQ(TEST_EVENT_TYPE, ev4.type);
@@ -62,6 +64,22 @@ TEST(TestEvent, test_construct_assign)
     ASSERT_EQ(TEST_EVENT_DATA, ev5.data);
     ASSERT_EQ(TEST_EVENT_CONFIRM, ev5.confirm);
     ASSERT_EQ(TEST_CONN, (int)ev5.connection);
+
+    Event ev6(TEST_EVENT_TYPE,
+              TEST_EVENT_DATA,
+              TEST_EVENT_CONFIRM);
+
+    ASSERT_EQ(TEST_EVENT_TYPE, ev6.type);
+    ASSERT_EQ(TEST_EVENT_DATA, ev6.data);
+    ASSERT_EQ(TEST_EVENT_CONFIRM, ev6.confirm);
+    ASSERT_EQ(nullptr, ev6.connection);
+
+    Event ev7(TEST_EVENT_TYPE, TEST_EVENT_DATA);
+
+    ASSERT_EQ(TEST_EVENT_TYPE, ev7.type);
+    ASSERT_EQ(TEST_EVENT_DATA, ev7.data);
+    ASSERT_TRUE(ev7.confirm.empty());
+    ASSERT_EQ(nullptr, ev7.connection);
 }
 
 TEST(TestEvent, test_propagate)
index b7b3ffe..841b3d5 100644 (file)
@@ -1,8 +1,8 @@
 #include <gmock/gmock.h>
 #include <stdexcept>
-#include "iot_policy_enforce_mock.h"
+#include "policy_enforce_mock.h"
 
-using namespace iot::core;
+using namespace core;
 using ::testing::Return;
 using ::testing::Throw;
 
index 354c188..88f0739 100644 (file)
@@ -6,6 +6,7 @@
 #include <fstream>
 #include <cerrno>
 #include <capi-system-info/system_info.h>
+#include <ctime>
 #include "settings.h"
 
 using namespace NetworkManager;
@@ -15,12 +16,12 @@ namespace
 
 const char OS_RELEASE_PATH[] = "/etc/os-release";
 const std::string DEFAULT_UNKNOWN{"unknown"};
-const std::chrono::seconds DEFAULT_KEEPALIVE(10);
+const std::chrono::milliseconds DEFAULT_KEEPALIVE(10);
 
 const std::string DEVICE_ID{"device-id"};
 const std::string DEVICE_SERIAL{"device-serial"};
 const std::string SERVER_ADDRESS{"device-id"};
-const std::chrono::seconds KEEPALIVE_TIME(123);
+const std::chrono::milliseconds KEEPALIVE_TIME(123);
 
 std::string randomString(char minc, char maxc, int len)
 {
@@ -89,11 +90,11 @@ TEST_F(TestSettings, test_Constructors)
     std::string filename = createSettingsFile();
     Settings from_ini(filename);
 
-    ASSERT_TRUE(from_ini.isLoaded());
-    ASSERT_EQ(SERVER_ADDRESS, from_ini.getServerAddress());
-    ASSERT_EQ(DEVICE_ID, from_ini.getDeviceId());
-    ASSERT_EQ(DEVICE_SERIAL, from_ini.getSerial());
-    ASSERT_EQ(KEEPALIVE_TIME, from_ini.getKeepAliveTimeout());
+    EXPECT_TRUE(from_ini.isLoaded());
+    EXPECT_EQ(SERVER_ADDRESS, from_ini.getServerAddress());
+    EXPECT_EQ(DEVICE_ID, from_ini.getDeviceId());
+    EXPECT_EQ(DEVICE_SERIAL, from_ini.getSerial());
+    EXPECT_EQ(KEEPALIVE_TIME, from_ini.getKeepAliveTimeout());
 
     ASSERT_EQ(0, remove(filename.c_str()));
 }
@@ -114,7 +115,7 @@ TEST_F(TestSettings, test_getters_setters)
     settings.setServerAddress(rstring);
     ASSERT_EQ(rstring, settings.getServerAddress());
 
-    std::chrono::seconds timeout(333);
+    std::chrono::milliseconds timeout(333);
     settings.setKeepAliveTimeout(timeout);
     ASSERT_EQ(timeout, settings.getKeepAliveTimeout());
 
@@ -122,3 +123,40 @@ TEST_F(TestSettings, test_getters_setters)
     settings.setSaveFileName(rstring);
     ASSERT_EQ(rstring, settings.getSaveFileName());
 }
+
+/**
+ * Test Settings class save method
+ * 1. Save with empty file name
+ * 2. Save to temporary file
+ */
+TEST_F(TestSettings, test_save)
+{
+    Settings settings;
+    ASSERT_FALSE(settings.load());
+
+    std::string rstring = randomString('a', 'z', 10);
+    settings.setDeviceId(rstring);
+
+    rstring = randomString('a', 'z', 10);
+    settings.setServerAddress(rstring);
+
+    std::chrono::milliseconds timeout(333);
+    settings.setKeepAliveTimeout(timeout);
+
+    ASSERT_FALSE(settings.save());
+
+    std::string tmpFile("/tmp/test_settings");
+    tmpFile += std::to_string(time(nullptr));
+
+    settings.setSaveFileName(tmpFile);
+    ASSERT_TRUE(settings.save());
+
+    Settings checkSettings(tmpFile);
+
+    ASSERT_EQ(0, remove(tmpFile.c_str()));
+
+    ASSERT_EQ(checkSettings.getDeviceId(), settings.getDeviceId());
+    ASSERT_EQ(checkSettings.getKeepAliveTimeout(), settings.getKeepAliveTimeout());
+    ASSERT_EQ(checkSettings.getSerial(), settings.getSerial());
+    ASSERT_EQ(checkSettings.getServerAddress(), settings.getServerAddress());
+}
diff --git a/device-agent/utest/test_tvext_policy_enforce.cpp b/device-agent/utest/test_tvext_policy_enforce.cpp
new file mode 100644 (file)
index 0000000..061ca0b
--- /dev/null
@@ -0,0 +1,44 @@
+#include <jsoncpp/json/reader.h>
+#include "device_policy_manager_mock.h"
+#include "tvext_enforce.h"
+
+using namespace core;
+using ::testing::_;
+using ::testing::Return;
+
+namespace
+{
+const std::string policy = R"-([{"name":"sound","state":1,"items":[]},
+                           {"name":"bluetooth","state":1,"items":[]},
+                           {"name":"wifi","state":0,"items":[]},
+                           {"name":"usb","state":0,"items":[]},
+                           {"name":"dtv-tunner","state":1,"items":[]},
+                           {"name":"iptables","state":1,"items":["127.0.0.0/24|UDP|10-1024","1.1.1.1|TCP|80,443","8.8.8.8"]}])-";
+}
+
+TEST(Test_TvExtPolicyEnforce, test_ParseGroup)
+{
+    DPMMock dpm;
+
+    TvExtPolicyEnforce tvep(PolicyEnforce::GetInstance());
+
+    EXPECT_CALL(dpm, dpm_manager_create()).WillOnce(Return(&dpm));
+
+    EXPECT_CALL(dpm, dpm_restriction_set_sound_state(1)).WillOnce(Return(0));
+
+    EXPECT_CALL(dpm, dpm_restriction_set_bluetooth_mode_change_state(1)).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_restriction_set_wifi_state(0)).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_restriction_set_usb_client_state(0)).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_restriction_set_tuner_state(1)).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_manager_destroy()).Times(1);
+    EXPECT_CALL(dpm, dpm_firewall_flush_deny_rules()).WillOnce(Return(0));
+    EXPECT_CALL(dpm, dpm_firewall_apply_deny_rules(_)).Times(3).WillRepeatedly(Return(0));
+
+    EXPECT_TRUE(tvep.Init());
+    Json::Value root;
+    Json::Reader reader;
+    ASSERT_TRUE(reader.parse(policy, root));
+    EXPECT_TRUE(tvep.ParseGroup(root));
+
+    EXPECT_NO_THROW(tvep.Deinit());
+}