Add basic libcynara-agent tests 62/33862/7
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 13 Jan 2015 13:53:47 +0000 (14:53 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 17 Mar 2015 14:49:13 +0000 (15:49 +0100)
Add basic tests for libcynara-admin library:
* tcag01_set_agent_type_policy_without_plugin - check if setting
 a policy with policy type provided by not loaded plugin fails;
* tcag02_set_agent_type_policy_with_plugin_loaded - checks if
 setting policy with policy type provided by loaded plugin succeeds;
* tcag03_check_with_no_agent - run cynara check that causes usage
 of plugin needing agent (but agent is not running) and verify
 that check returns DENY;
* tcag04_agent_initialize - check initialization and deinitialization
 og agent;
* tcag05_agent_request_timeout - run agent and register it in cynara
 service, break connection by resetting cynara after timeout;
* tcag06_check_with_unregistered_agent - run cynara check that causes
 usage of plugin needing agent (agent is running but not registered)
 and verify that chack returns DENY;
* tcag07_get_request - run simplest complete agent usage scenario:
    > agent registers in cynara,
    > a check using plugin and agent is done,
    > test acts as agent receiving and verifying request,
    > an ALLOW response is sent to cynara,
    > client receives proper ALLOW answer.

Change-Id: I781e9bb88b8686334acca4f9cf2c13a13bd643d5

src/cynara-tests/CMakeLists.txt
src/cynara-tests/test_cases_agent.cpp [new file with mode: 0644]

index cde27fe..1b275fd 100644 (file)
@@ -46,6 +46,7 @@ SET(CYNARA_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/plugins/plugins.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/cynara-test.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases.cpp
+    ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_agent.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_async.cpp
     ${PROJECT_SOURCE_DIR}/src/cynara-tests/test_cases_db.cpp
     )
diff --git a/src/cynara-tests/test_cases_agent.cpp b/src/cynara-tests/test_cases_agent.cpp
new file mode 100644 (file)
index 0000000..2b0eff5
--- /dev/null
@@ -0,0 +1,201 @@
+/*
+ * 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.
+ */
+
+/**
+ * @file        test_cases_agent.cpp
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       Tests for libcynara-agent
+ */
+
+#include <chrono>
+#include <string>
+
+#include <cynara-error.h>
+#include <cynara-admin-types.h>
+#include <cynara-client-async.h>
+
+#include <dpl/test/test_runner.h>
+#include <cynara_test_admin.h>
+#include <cynara_test_agent.h>
+#include <cynara_test_agent_request.h>
+#include <cynara_test_agent_response.h>
+#include <cynara_test_client_async_client.h>
+#include <cynara_test_commons.h>
+#include <cynara_test_env.h>
+#include <service_manager.h>
+#include <timeout.h>
+
+using namespace CynaraTestAdmin;
+using namespace CynaraTestAgent;
+using namespace CynaraTestClientAsync;
+using namespace CynaraTestPlugins;
+
+void loadAgentPlugin()
+{
+    DirectoryPaths paths;
+    paths.push_back(TEST_PLUGIN_PATH + TEST_AGENT);
+    loadServicePlugins(paths);
+}
+
+void setAgentPolicy(int expectedResult = CYNARA_API_SUCCESS)
+{
+    const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
+    const char *wildcard = CYNARA_ADMIN_WILDCARD;
+    const char *extra = nullptr;
+// collection of policy descriptions defined by plugin that causes use of TestAgent
+    auto testAgentPolicies = POLICY_DESCRIPTIONS.at(TEST_AGENT);
+// any policy type from above collection
+    auto policyType = testAgentPolicies[0].type;
+
+    CynaraPoliciesContainer cp;
+    cp.add(bucket, wildcard, wildcard, wildcard, policyType, extra);
+
+    Admin admin;
+    admin.setPolicies(cp, expectedResult);
+}
+
+void restartCynara()
+{
+    ServiceManager serviceManager(CynaraTestConsts::SERVICE);
+    serviceManager.restartService();
+}
+
+void getAgentRequest(Agent &agent, AgentRequest &request, Client &client)
+{
+    auto timeLimit = std::chrono::seconds(2);
+    auto hangOnGetRequest = [&agent, &request]() {
+        agent.getRequest(request);
+    };
+    Timeout::CancelFunction sendClientRequest = [&client]() {
+        client.process();
+        client.assertStatus(READ);
+    };
+
+    Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT,
+                         sendClientRequest, hangOnGetRequest);
+}
+
+void tcag01_set_agent_type_policy_without_plugin_func()
+{
+    loadServicePlugins(DirectoryPaths());
+    setAgentPolicy(CYNARA_API_INVALID_PARAM);
+}
+
+void tcag02_set_agent_type_policy_with_plugin_loaded_func()
+{
+    loadAgentPlugin();
+    setAgentPolicy();
+}
+
+void tcag03_check_with_no_agent_func()
+{
+    std::string testNo("03");
+    cynara_check_id id;
+    RequestEntity callbackData = {RequestFunction(),
+                                  CYNARA_API_ACCESS_DENIED,
+                                  CYNARA_CALL_CAUSE_ANSWER};
+
+    loadAgentPlugin();
+    setAgentPolicy();
+
+    Client client;
+    client.createRequest({testNo}, id, callbackData);
+    client.assertStatus(READWRITE);
+
+    //send requests
+    client.process();
+    client.process(CYNARA_API_SUCCESS, Client::IGNORE_TIMEOUT);
+}
+
+void tcag04_agent_initialize_func()
+{
+    Agent();
+}
+
+void tcag05_agent_request_timeout_func()
+{
+    Agent agent;
+    AgentRequest request;
+
+    auto testTimeLimit = std::chrono::seconds(2);
+    auto hangOnGetRequest = [&agent, &request]() {
+        agent.getRequest(request, CYNARA_API_SERVICE_NOT_AVAILABLE);
+    };
+
+    Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::TIMEOUT,
+                         restartCynara, hangOnGetRequest);
+}
+
+void tcag06_check_with_unregistered_agent_func()
+{
+    std::string testNo("06");
+    cynara_check_id id;
+    RequestEntity callbackData = {RequestFunction(),
+                                  CYNARA_API_ACCESS_DENIED,
+                                  CYNARA_CALL_CAUSE_ANSWER};
+
+    loadAgentPlugin();
+    setAgentPolicy();
+
+    Agent agent;
+
+    Client client;
+    client.createRequest({testNo}, id, callbackData);
+    client.assertStatus(READWRITE);
+
+    //send requests
+    client.process();
+    client.process(CYNARA_API_SUCCESS, Client::IGNORE_TIMEOUT);
+}
+
+void tcag07_get_request_func()
+{
+    std::string testNo("07");
+    CheckData data(testNo);
+    cynara_check_id id;
+    RequestEntity callbackData = {RequestFunction(),
+                                  CYNARA_API_ACCESS_ALLOWED,
+                                  CYNARA_CALL_CAUSE_ANSWER};
+
+    loadAgentPlugin();
+    setAgentPolicy();
+
+    Agent agent;
+    AgentRequest agentRequest;
+    Client client;
+    client.createRequest(data, id, callbackData);
+    client.assertStatus(READWRITE);
+
+    auto testTimeLimit = std::chrono::seconds(5);
+    Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED,
+                         restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest),
+                         std::ref(client));
+
+    agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege);
+    agent.putResponse(AgentResponse::createAllow(agentRequest.id()));
+    client.process();
+}
+
+RUNNER_TEST_GROUP_INIT(cynara_agent_tests)
+
+RUN_CYNARA_TEST(tcag01_set_agent_type_policy_without_plugin)
+RUN_CYNARA_TEST(tcag02_set_agent_type_policy_with_plugin_loaded)
+RUN_CYNARA_TEST(tcag03_check_with_no_agent)
+RUN_CYNARA_TEST(tcag04_agent_initialize)
+RUN_CYNARA_TEST(tcag05_agent_request_timeout)
+RUN_CYNARA_TEST(tcag06_check_with_unregistered_agent)
+RUN_CYNARA_TEST(tcag07_get_request)