Add Agent class wrapping usage of libcynara-agent 87/33387/5
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 8 Jan 2015 10:24:27 +0000 (11:24 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 15 Jan 2015 18:39:26 +0000 (19:39 +0100)
With Agent object tests can play role of cynara agent and
communicate with cynara service. That can be useful for:
* testing libcynara-agent library;
* using agent in plugins testing;
* using agent in asynchronous client testing.

Change-Id: Idc783fa963074d5ff6f1dedb6a89b6aae6f65dbe

tests/cynara-tests/CMakeLists.txt
tests/cynara-tests/common/cynara_test_agent.cpp [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_agent.h [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_agent_request.cpp [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_agent_request.h [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_agent_response.cpp [new file with mode: 0644]
tests/cynara-tests/common/cynara_test_agent_response.h [new file with mode: 0644]

index f30b62a..98037dd 100644 (file)
@@ -22,6 +22,7 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
     REQUIRED
     libprivilege-control
     cynara-admin
+    cynara-agent
     cynara-client
     cynara-client-async
     cynara-plugin
@@ -31,6 +32,9 @@ PKG_CHECK_MODULES(CYNARA_TARGET_DEP
 #files to compile
 SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_admin.cpp
+    ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_agent.cpp
+    ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_agent_request.cpp
+    ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_agent_response.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client_async_client.cpp
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client_async_request_monitor.cpp
diff --git a/tests/cynara-tests/common/cynara_test_agent.cpp b/tests/cynara-tests/common/cynara_test_agent.cpp
new file mode 100644 (file)
index 0000000..840d393
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <cstdlib>
+#include <string>
+
+#include <cynara_test_agent.h>
+#include <plugins.h>
+#include <dpl/test/test_runner.h>
+
+namespace CynaraTestAgent {
+
+Agent::Agent()
+    : m_agent(nullptr)
+{
+    int ret = cynara_agent_initialize(&m_agent, CynaraTestPlugins::TEST_AGENT_TYPE.c_str());
+    RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
+                         "cynara_agent_initialize failed. ret: " << ret);
+    RUNNER_ASSERT_MSG(m_agent != nullptr,
+                         "cynara_agent struct was not initialized");
+}
+
+Agent::~Agent()
+{
+    cynara_agent_finish(m_agent);
+}
+
+void Agent::getRequest(AgentRequest &request, int expectedResult)
+{
+    cynara_agent_msg_type type;
+    cynara_agent_req_id id;
+    void *data = nullptr;
+    size_t dataSize;
+
+    int ret = cynara_agent_get_request(m_agent, &type, &id, &data, &dataSize);
+    if (ret == CYNARA_API_SUCCESS) {
+        RUNNER_ASSERT_MSG(!data == !dataSize,
+                             "cynara_agent_get_request returned contradictory values: "
+                                 << "data = " << data << " ,"
+                                 << "size = " << dataSize << ".");
+        request.set(type, id, data, dataSize);
+        free(data);
+    }
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                         "cynara_agent_get_request returned wrong value: "
+                             << ret << " != " << expectedResult << ".");
+}
+
+void Agent::putResponse(const AgentResponse &response, int expectedResult)
+{
+    int ret = cynara_agent_put_response(m_agent,
+                                        response.type(),
+                                        response.id(),
+                                        reinterpret_cast<const void*>(response.data().data()),
+                                        response.data().size());
+
+    RUNNER_ASSERT_MSG(ret == expectedResult,
+                     "cynara_agent_put_response returned wrong value: "
+                         << ret << " != " << expectedResult << "."
+                         << "response = " << response);
+}
+
+} // namespace CynaraTestAgent
diff --git a/tests/cynara-tests/common/cynara_test_agent.h b/tests/cynara-tests/common/cynara_test_agent.h
new file mode 100644 (file)
index 0000000..f224fa1
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef CYNARA_TEST_AGENT_H
+#define CYNARA_TEST_AGENT_H
+
+#include <cynara-agent.h>
+
+#include <cynara_test_agent_request.h>
+#include <cynara_test_agent_response.h>
+
+namespace CynaraTestAgent {
+
+class Agent
+{
+public:
+    Agent();
+    ~Agent();
+
+    void getRequest(AgentRequest &request, int expectedResult = CYNARA_API_SUCCESS);
+    void putResponse(const AgentResponse &response, int expectedResult = CYNARA_API_SUCCESS);
+
+private:
+    struct cynara_agent *m_agent;
+};
+
+} // namespace CynaraTestAgent
+
+#endif // CYNARA_TEST_AGENT_H
diff --git a/tests/cynara-tests/common/cynara_test_agent_request.cpp b/tests/cynara-tests/common/cynara_test_agent_request.cpp
new file mode 100644 (file)
index 0000000..52cb46a
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <cynara_test_agent_request.h>
+#include <plugins.h>
+#include <dpl/test/test_runner.h>
+
+namespace CynaraTestAgent {
+
+std::ostream& operator<<(std::ostream& os, const AgentRequest &request)
+{
+    os << "{";
+    os << " valid = " << request.m_valid << ",";
+    os << " type = " << request.m_type << ",";
+    os << " id = " << request.m_id << ",";
+    os << " data = " << request.m_data << ",";
+    os << " client = " << request.m_client << ",";
+    os << " user = " << request.m_user << ",";
+    os << " privilege = " << request.m_privilege;
+    os << " }";
+    return os;
+}
+
+void AgentRequest::set(cynara_agent_msg_type type, cynara_agent_req_id id,
+                       const void *data, size_t dataSize)
+{
+    m_type = type;
+    m_id = id;
+    m_data = Cynara::PluginData(reinterpret_cast<const char*>(data), dataSize);
+    m_client.clear();
+    m_user.clear();
+    m_privilege.clear();
+
+    if (m_type == CYNARA_MSG_TYPE_ACTION) {
+        CynaraTestPlugins::AgentDataVector parsedData;
+
+        bool unwrapSuccess = CynaraTestPlugins::unwrapAgentData(m_data, parsedData);
+        RUNNER_ASSERT_MSG(unwrapSuccess,
+                             "Format error. Cannot succesfully unwrap request. "
+                                 << *this);
+
+        RUNNER_ASSERT_MSG(parsedData.size() == 3,
+                             "Received unexpected [" << parsedData.size() << "] number of units,"
+                                 << " while expecting 3."
+                                 << " Cannot succesfully unwrap request. "
+                                 << *this);
+
+        m_client = parsedData[0];
+        m_user = parsedData[1];
+        m_privilege = parsedData[2];
+    }
+    m_valid = true;
+}
+
+void AgentRequest::assertAction(std::string client, std::string user, std::string privilege)
+{
+    RUNNER_ASSERT_MSG(m_valid,
+                         "assertAction failed: request is not valid. "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_type == CYNARA_MSG_TYPE_ACTION,
+                         "assertAction failed: request's type is " << m_type
+                             << ", expected type is " << CYNARA_MSG_TYPE_ACTION << ". "
+                             << *this);
+    RUNNER_ASSERT_MSG(!m_data.empty(),
+                         "assertAction failed: m_data is empty. "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_client == client,
+                         "assertAction failed: unexpected client value " << m_client
+                             << ", expected value is " << client << ". "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_user == user,
+                         "assertAction failed: unexpected user value " << m_user
+                             << ", expected value is " << user << ". "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_privilege == privilege,
+                         "assertAction failed: unexpected privilege value " << m_privilege
+                             << ", expected value is " << privilege << ". "
+                             << *this);
+}
+
+void AgentRequest::assertCancel()
+{
+    RUNNER_ASSERT_MSG(m_valid,
+                         "assertCancel failed: request is not valid. "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_type == CYNARA_MSG_TYPE_CANCEL,
+                         "assertCancel failed: request's type is " << m_type
+                             << ", expected type is " << CYNARA_MSG_TYPE_CANCEL << ". "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_data.empty(),
+                         "assertCancel failed: m_data is not empty. "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_client.empty(),
+                         "assertCancel failed: m_client is not empty. "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_user.empty(),
+                         "assertCancel failed: m_user is not empty. "
+                             << *this);
+    RUNNER_ASSERT_MSG(m_privilege.empty(),
+                         "assertCancel failed: m_privilege is not empty. "
+                             << *this);
+}
+
+} // namespace CynaraTestAgent
diff --git a/tests/cynara-tests/common/cynara_test_agent_request.h b/tests/cynara-tests/common/cynara_test_agent_request.h
new file mode 100644 (file)
index 0000000..a9b0d4f
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef CYNARA_TEST_AGENT_REQUEST_H
+#define CYNARA_TEST_AGENT_REQUEST_H
+
+#include <cynara_test_commons.h>
+
+#include <cynara-agent.h>
+#include <cynara-plugin.h>
+#include <ostream>
+#include <string>
+
+namespace CynaraTestAgent {
+
+class AgentRequest
+{
+public:
+    AgentRequest() : m_valid(false), m_type(CYNARA_MSG_TYPE_ACTION), m_id(0)
+    {}
+
+    void set(cynara_agent_msg_type type, cynara_agent_req_id id, const void *data, size_t dataSize);
+
+    bool valid() const
+    {
+        return m_valid;
+    }
+
+    cynara_agent_msg_type type() const
+    {
+        return m_type;
+    }
+
+    cynara_agent_req_id id() const
+    {
+        return m_id;
+    }
+
+    void assertAction(std::string client, std::string user, std::string privilege);
+    void assertCancel();
+
+    friend std::ostream& operator<<(std::ostream& os, const AgentRequest &request);
+
+private:
+    bool m_valid;
+    cynara_agent_msg_type m_type;
+    cynara_agent_req_id m_id;
+    Cynara::PluginData m_data;
+    std::string m_client;
+    std::string m_user;
+    std::string m_privilege;
+};
+
+} // namespace CynaraTestAgent
+
+#endif // CYNARA_TEST_AGENT_REQUEST_H
diff --git a/tests/cynara-tests/common/cynara_test_agent_response.cpp b/tests/cynara-tests/common/cynara_test_agent_response.cpp
new file mode 100644 (file)
index 0000000..70b9ea4
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <cynara_test_agent_response.h>
+#include <plugins.h>
+#include <cynara-agent.h>
+
+namespace CynaraTestAgent {
+
+std::ostream& operator<<(std::ostream& os, const AgentResponse &response)
+{
+    os << "{";
+    os << " type = " << response.m_type << ",";
+    os << " id = " << response.m_id << ",";
+    os << " data = " << response.m_data;
+    os << " }";
+    return os;
+}
+
+AgentResponse AgentResponse::createAllow(cynara_agent_req_id id)
+{
+    CynaraTestPlugins::AgentDataVector rawData = {CynaraTestPlugins::AGENT_DATA_ALLOW};
+    return AgentResponse(CYNARA_MSG_TYPE_ACTION, id, CynaraTestPlugins::wrapAgentData(rawData));
+}
+
+AgentResponse AgentResponse::createDeny(cynara_agent_req_id id)
+{
+    CynaraTestPlugins::AgentDataVector rawData = {CynaraTestPlugins::AGENT_DATA_DENY};
+    return AgentResponse(CYNARA_MSG_TYPE_ACTION, id, CynaraTestPlugins::wrapAgentData(rawData));
+}
+
+AgentResponse AgentResponse::createCancel(cynara_agent_req_id id)
+{
+    return AgentResponse(CYNARA_MSG_TYPE_CANCEL, id, Cynara::PluginData());
+}
+
+} // namespace CynaraTestAgent
diff --git a/tests/cynara-tests/common/cynara_test_agent_response.h b/tests/cynara-tests/common/cynara_test_agent_response.h
new file mode 100644 (file)
index 0000000..6d3f46b
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef CYNARA_TEST_AGENT_RESPONSE_H
+#define CYNARA_TEST_AGENT_RESPONSE_H
+
+#include <cynara_test_commons.h>
+
+#include <cynara-agent.h>
+#include <cynara-plugin.h>
+#include <ostream>
+
+namespace CynaraTestAgent {
+
+class AgentResponse
+{
+public:
+    AgentResponse() = delete;
+    static AgentResponse createAllow(cynara_agent_req_id id);
+    static AgentResponse createDeny(cynara_agent_req_id id);
+    static AgentResponse createCancel(cynara_agent_req_id id);
+
+    cynara_agent_msg_type type() const
+    {
+        return m_type;
+    }
+
+    cynara_agent_req_id id() const
+    {
+        return m_id;
+    }
+
+    Cynara::PluginData data() const
+    {
+        return m_data;
+    }
+
+    friend std::ostream& operator<<(std::ostream& os, const AgentResponse &response);
+
+private:
+    AgentResponse(cynara_agent_msg_type type, cynara_agent_req_id id, Cynara::PluginData data)
+        : m_type(type), m_id(id), m_data(data)
+    {}
+
+    cynara_agent_msg_type m_type;
+    cynara_agent_req_id m_id;
+    Cynara::PluginData m_data;
+};
+
+} // namespace CynaraTestAgent
+
+#endif // CYNARA_TEST_AGENT_RESPONSE_H