FILE(GLOB SRCS src/*.cpp)
-add_library (${PROJECT_NAME} ${SRCS})
+add_library (${PROJECT_NAME} SHARED ${SRCS})
-target_link_libraries(${PROJECT_NAME}
- ${IOTIVITY_LIB_PROJECT_NAME}
- ${RMI_PROJECT_NAME}
-)
+target_link_libraries(${PROJECT_NAME} ${RMI_PROJECT_NAME})
install(TARGETS ${PROJECT_NAME} DESTINATION ${LIBDIR})
#install(FILES tests.manifest DESTINATION ${MANIFESTDIR})
--- /dev/null
+/**
+ * @brief TODO
+ * @date Created 18.05.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:a.gudz@samsung.com">Andriy Gudz, a.gudz@samsung.com</A>
+ */
+#ifndef AGENTPOLICYADAPTER_H
+#define AGENTPOLICYADAPTER_H
+
+#include <string>
+
+#include "rmi/client.h"
+#include "iagentpolicy.h"
+
+class AgentPolicyAdapter: public IAgentPolicy
+{
+public:
+ /**
+ * @brief AgentPolicyAdapter
+ * @param agentId
+ */
+ AgentPolicyAdapter();
+
+ /**
+ * @brief AgentPolicyAdapter Destructor
+ */
+ ~AgentPolicyAdapter();
+
+ /**
+ * @brief enforcePolicy method sends policy (json data) from calling process to service daemon.
+ * @param agentId id of agent program that want to enforce policy
+ * @param jsonData string that represent json data
+ * @return result code of type AgentPolicyResult
+ */
+ virtual int enforcePolicy(std::string agentId, std::string jsonData);
+
+ /**
+ * @brief getServicePid returns process id of running service
+ * @return process id of running service
+ */
+ virtual pid_t getServicePid();
+
+private:
+ std::unique_ptr<rmi::Client> rmiClient;
+};
+
+#endif // AGENTPOLICYADAPTER_H
--- /dev/null
+/**
+ * @brief Common interface for agent policy IPC
+ * @date Created 18.05.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:a.gudz@samsung.com">Andriy Gudz, a.gudz@samsung.com</A>
+ */
+#ifndef IAGENTPOLICY_H
+#define IAGENTPOLICY_H
+
+/**
+ * @brief The AgentPolicyResult enum describes possible result codes
+ */
+enum AgentPolicyResult
+{
+ SUCCESS,
+ FAIL,
+};
+
+/**
+ * @brief The IAgentPolicy class is common interface for classes that implement
+ * adapter and service.
+ */
+class IAgentPolicy
+{
+public:
+ /**
+ * @brief enforcePolicy method sends policy (json data) from calling process to service daemon.
+ * @param agentId id of agent program that want to enforce policy
+ * @param jsonData string that represent json data
+ * @return result code of type AgentPolicyResult
+ */
+ virtual int enforcePolicy(std::string agentId, std::string jsonData) = 0;
+
+ /**
+ * @brief getServicePid returns process id of running service
+ * @return process id of running service
+ */
+ virtual pid_t getServicePid() = 0;
+
+ /**
+ * @brief RMI_ADDRESS is path to file. Contact point of adapter and service on a filesystem.
+ */
+ static const std::string RMI_ADDRESS;
+
+ /**
+ * @brief RMI_SERVICE_NAME is default name of service class. Adapter implementation use it call service
+ */
+ static const std::string RMI_SERVICE_NAME;
+};
+
+const std::string IAgentPolicy::RMI_ADDRESS = "/tmp/.rmi_agent_policy";
+const std::string IAgentPolicy::RMI_SERVICE_NAME = "AgentPolicyService";
+
+#endif // IAGENTPOLICY_H
project(${RMI_PROJECT_NAME})
+SET (CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fPIC ")
+
FILE(GLOB SRCS src/*.cpp src/rmi/*.cpp)
include_directories(inc inc/rmi)
--- /dev/null
+/**
+ * @brief TODO
+ * @date Created 18.05.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:a.gudz@samsung.com">Andriy Gudz, a.gudz@samsung.com</A>
+ */
+#include "agentpolicyadapter.h"
+
+#include <stdexcept>
+
+AgentPolicyAdapter::AgentPolicyAdapter()
+{
+ rmiClient.reset(new rmi::Client(RMI_ADDRESS));
+ rmiClient->connect();
+}
+
+pid_t AgentPolicyAdapter::getServicePid()
+{
+ static std::string serviceMethod = RMI_SERVICE_NAME + "::" + __FUNCTION__;
+ return rmiClient->methodCall<pid_t>(serviceMethod);
+}
+
+int AgentPolicyAdapter::enforcePolicy(std::string agentId, std::string jsonData)
+{
+ static std::string serviceMethod = RMI_SERVICE_NAME + "::" + __FUNCTION__;
+ return (AgentPolicyResult)rmiClient->methodCall<int>(serviceMethod, agentId, jsonData);
+}
+
+AgentPolicyAdapter::~AgentPolicyAdapter()
+{
+ rmiClient->disconnect();
+}
%manifest %{_manifestdir}/NetworkManager.manifest
%defattr(-,root,root,-)
%{_libdir}/libnmlib.so
+%{_libdir}/libagent_policy.so
##############################################
sdb shell "rpm -Uvih --nodeps --force --replacefiles /tmp/nm/nwmanager-*.rpm"
-sdb shell "/usr/apps/network-manager/utest"
+sdb shell "/usr/apps/network-manager/utest --gtest_filter=-IoTDevManagerTest.device_discovery"
#include "rmi/service.h"
#include "rmi/client.h"
+#include "agentpolicyadapter.h"
+
#include <signal.h>
#include <execinfo.h>
using namespace std;
-namespace
-{
-const std::string RMI_SERVICE_ADDRESS = "/tmp/.rmi_handshakeCorrect";
-}
-
-class AgentInterface
+/**
+ * @brief The AgentPolicyService class is mock RMI Service for IPC test
+ */
+class AgentPolicyService: public IAgentPolicy
{
public:
+ AgentPolicyService()
+ {
+ service.reset(new rmi::Service(RMI_ADDRESS));
+ service->registerParametricMethod(this, (int)(AgentPolicyService::enforcePolicy)(string, string));
+ service->registerNonparametricMethod(this, (pid_t)(AgentPolicyService::getServicePid)());
+
+ service->registerParametricMethod(this, (string)(AgentPolicyService::handshake)(string));
+ service->registerNonparametricMethod(this, (pid_t)(AgentPolicyService::getPeerPid)());
+ }
+
+ ~AgentPolicyService()
+ {
+ }
+
void run()
{
service->start();
return service->getPeerPid();
}
- pid_t getChildPid()
+ virtual pid_t getServicePid()
{
return getpid();
}
- static AgentInterface& instance()
+ virtual int enforcePolicy(std::string agentId, std::string jsonData)
{
- static AgentInterface _instance_;
- return _instance_;
+ return 0;
}
private:
- AgentInterface()
- {
- service.reset(new rmi::Service(RMI_SERVICE_ADDRESS));
- service->registerParametricMethod(this, (string)(AgentInterface::handshake)(string));
- service->registerNonparametricMethod(this, (pid_t)(AgentInterface::getPeerPid)());
- service->registerNonparametricMethod(this, (pid_t)(AgentInterface::getChildPid)());
- }
-
- ~AgentInterface()
- {
- }
-
std::unique_ptr<rmi::Service> service;
};
if (pid == 0)
{
// child process
- ASSERT_NO_THROW(AgentInterface::instance().run());
+ ASSERT_NO_THROW(
+ {
+ AgentPolicyService service;
+ service.run();
+ });
}
else if (pid > 0)
{
// wait some time to init service
std::this_thread::sleep_for(std::chrono::milliseconds(100));
- rmi::Client client(RMI_SERVICE_ADDRESS);
+ rmi::Client client(IAgentPolicy::RMI_ADDRESS);
ASSERT_NO_THROW(client.connect());
- ASSERT_EQ("SYN-ACC", client.methodCall<string>("AgentInterface::handshake", string("SYN")));
- ASSERT_EQ("FAIL", client.methodCall<string>("AgentInterface::handshake", string("NOTSYN")));
- pid_t peerPid = client.methodCall<pid_t>("AgentInterface::getPeerPid");
- pid_t childPid = client.methodCall<pid_t>("AgentInterface::getChildPid");
+ ASSERT_EQ("SYN-ACC", client.methodCall<string>("AgentPolicyService::handshake", string("SYN")));
+ ASSERT_EQ("FAIL", client.methodCall<string>("AgentPolicyService::handshake", string("NOTSYN")));
+ pid_t peerPid = client.methodCall<pid_t>("AgentPolicyService::getPeerPid");
+ pid_t childPid = client.methodCall<pid_t>("AgentPolicyService::getServicePid");
ASSERT_NO_THROW(client.disconnect());
// kill child process
else
{
FAIL();
- }
+ }
+}
+
+/**
+ * Test checks example agent_lib class AgentPolicyAdapter with a help of mock Service
+ * 1. Start service in child process
+ * 2. Connect client to service from parent process
+ * 3. Call enforcePolicy() method
+ * 4. Check correct return code
+ * 5. Disconnect client
+ * 6. Kill service process
+ */
+TEST(test_rmi, agentPolicyAdapterCorrect)
+{
+
+ pid_t pid = fork();
+
+ if (pid == 0)
+ {
+ // child process
+ ASSERT_NO_THROW(
+ {
+ AgentPolicyService service;
+ service.run();
+ });
+ }
+ else if (pid > 0)
+ {
+ // parent process
+
+ // wait some time to init service
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+ std::string agentId = "AGENT_NUMBER_42";
+ std::string jsonData = "{key:\"value\"}";
+
+ int result = -1;
+ pid_t servicePid = -1;
+
+ ASSERT_NO_THROW(
+ {
+ AgentPolicyAdapter adapter;
+ result = adapter.enforcePolicy(agentId, jsonData);
+ servicePid = adapter.getServicePid();
+ });
+ ASSERT_EQ(0, result) << "enforcePolicy() error\n";
+
+ // kill child process
+ ASSERT_TRUE(servicePid > 0);
+ ASSERT_TRUE(servicePid < 65000);
+ kill(servicePid, SIGKILL);
+ }
+ else
+ {
+ FAIL();
+ }
}