From 5a38d2d13f3c3d3240eeea51e05fc80f9b39ecf7 Mon Sep 17 00:00:00 2001 From: Radoslaw Bartosiak Date: Fri, 30 Jan 2015 17:42:31 +0100 Subject: [PATCH] Add additional libcynara-agent tests Add test cases for: cynara_agent_initialize(), cynara_agent_finish(), cynara_agent_get_request(), cynara_agent_put_response() *) tcag08_get_request_and_respond_with_wrong_id - client shall not receive a response. *) tcag09_get_request_and_correct_responded_id - client shall receive the response with correct id. *) tcag10_cancel_request - client shall receive the confirmation of its cancel action. *) tcag11_cancel_processed_request - the cancelation of the request processed by the agent is possible but void. *) tcag12_create_two_requests - processing of two requests. *) tcag13_create_many_requests - processing of many requests and sending the responses by the agent in LIFO order. *) tcag14_client_disconnects - the cancel request shall be sent by Cynara when client disconnects. *) tcag15_agent_disconnects - the access denied answer shall be sent by Cynara when agent disconnects. Change-Id: I45c11e79c4977bed6892166a51c336e0414fc02e Signed-off-by: Radoslaw Bartosiak --- src/cynara-tests/common/cynara_test_agent.cpp | 6 +- src/cynara-tests/test_cases_agent.cpp | 275 +++++++++++++++++++++++++- 2 files changed, 274 insertions(+), 7 deletions(-) diff --git a/src/cynara-tests/common/cynara_test_agent.cpp b/src/cynara-tests/common/cynara_test_agent.cpp index 840d393..e2f408d 100644 --- a/src/cynara-tests/common/cynara_test_agent.cpp +++ b/src/cynara-tests/common/cynara_test_agent.cpp @@ -61,11 +61,13 @@ void Agent::getRequest(AgentRequest &request, int expectedResult) void Agent::putResponse(const AgentResponse &response, int expectedResult) { + auto size = response.data().size(); int ret = cynara_agent_put_response(m_agent, response.type(), response.id(), - reinterpret_cast(response.data().data()), - response.data().size()); + size ? static_cast(response.data().data()) + : nullptr, + size); RUNNER_ASSERT_MSG(ret == expectedResult, "cynara_agent_put_response returned wrong value: " diff --git a/src/cynara-tests/test_cases_agent.cpp b/src/cynara-tests/test_cases_agent.cpp index 2b0eff5..51fab54 100644 --- a/src/cynara-tests/test_cases_agent.cpp +++ b/src/cynara-tests/test_cases_agent.cpp @@ -17,6 +17,7 @@ /** * @file test_cases_agent.cpp * @author Lukasz Wojciechowski + * @author Radoslaw Bartosiak * @version 1.0 * @brief Tests for libcynara-agent */ @@ -74,18 +75,20 @@ void restartCynara() serviceManager.restartService(); } -void getAgentRequest(Agent &agent, AgentRequest &request, Client &client) +void getAgentRequest(Agent &agent, AgentRequest &request, Client &client, + int expectedResult = CYNARA_API_SUCCESS, + Timeout::ExpectMode expectTimeoutMode = Timeout::ExpectMode::TIMEOUT) { auto timeLimit = std::chrono::seconds(2); - auto hangOnGetRequest = [&agent, &request]() { - agent.getRequest(request); + auto hangOnGetRequest = [&agent, &request, &expectedResult]() { + agent.getRequest(request, expectedResult); }; Timeout::CancelFunction sendClientRequest = [&client]() { client.process(); client.assertStatus(READ); }; - Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT, + Timeout::callAndWait(timeLimit, expectTimeoutMode, sendClientRequest, hangOnGetRequest); } @@ -183,13 +186,267 @@ void tcag07_get_request_func() auto testTimeLimit = std::chrono::seconds(5); Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest), - std::ref(client)); + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + + agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege); + agent.putResponse(AgentResponse::createAllow(agentRequest.id())); + client.process(); +} + +void tcag08_get_request_and_respond_with_wrong_id_func() +{ + std::string testNo("08"); + CheckData data(testNo); + cynara_check_id id; + RequestEntity callbackData = {RequestFunction(), + CYNARA_API_SUCCESS, + CYNARA_CALL_CAUSE_FINISH}; + + 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), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege); + agent.putResponse(AgentResponse::createAllow(agentRequest.id() + 1)); + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_TIMEOUT, 2); +} + +void tcag09_get_request_and_correct_responded_id_func() +{ + std::string testNo("09"); + 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), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege); + agent.putResponse(AgentResponse::createAllow(agentRequest.id() + 1)); + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_TIMEOUT, 2); agent.putResponse(AgentResponse::createAllow(agentRequest.id())); client.process(); } +void tcag10_cancel_request_func() +{ + std::string testNo("10"); + CheckData data(testNo); + cynara_check_id id; + RequestEntity callbackData = {RequestFunction(), + CYNARA_API_ACCESS_ALLOWED, + CYNARA_CALL_CAUSE_CANCEL}; + + 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), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege); + client.cancel(id); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, + restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest), + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + agentRequest.assertCancel(); + agent.putResponse(AgentResponse::createCancel(id)); + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_NO_TIMEOUT, 2); +} + +void tcag11_cancel_processed_request_func() +{ + std::string testNo("11"); + CheckData data(testNo); + cynara_check_id id; + RequestEntity callbackData = {RequestFunction(), + CYNARA_API_ACCESS_ALLOWED, + CYNARA_CALL_CAUSE_CANCEL}; + + 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), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege); + agent.putResponse(AgentResponse::createCancel(id)); + client.cancel(id); + // we do not expect getting the cancel request in the agent + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::TIMEOUT, + restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest), + std::ref(client), CYNARA_API_SERVICE_NOT_AVAILABLE, Timeout::ExpectMode::TIMEOUT); + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_NO_TIMEOUT, 2); +} + +void tcag12_create_two_requests_func() +{ + std::string testNo("12"); + CheckData data1(testNo, 1), data2(testNo, 2); + cynara_check_id id1, id2; + RequestEntity callbackData1 = {RequestFunction(), + CYNARA_API_ACCESS_DENIED, + CYNARA_CALL_CAUSE_ANSWER}; + RequestEntity callbackData2 = {RequestFunction(), + CYNARA_API_ACCESS_ALLOWED, + CYNARA_CALL_CAUSE_CANCEL}; + + loadAgentPlugin(); + setAgentPolicy(); + + Agent agent; + AgentRequest agentRequest1, agentRequest2, agentRequest3; + Client client; + client.createRequest(data1, id1, callbackData1); + client.assertStatus(READWRITE); + client.createRequest(data2, id2, callbackData2); + client.assertStatus(READWRITE); + + auto testTimeLimit = std::chrono::seconds(5); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, + restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest1), + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, + restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest2), + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::IGNORE); + client.cancel(id2); + client.assertStatus(READWRITE); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, + restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest3), + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + agentRequest1.assertAction(data1.m_client, data1.m_user, data1.m_privilege); + agentRequest2.assertAction(data2.m_client, data2.m_user, data2.m_privilege); + agentRequest3.assertCancel(); + + agent.putResponse(AgentResponse::createDeny(id1)); + agent.putResponse(AgentResponse::createCancel(id2)); + + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_NO_TIMEOUT, 3); + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::IGNORE_TIMEOUT, 1); +} + +void tcag13_create_many_requests_func() +{ + const int numberOfRequests = 4; + std::string testNo("13"); + cynara_check_id ids[numberOfRequests]; + RequestEntity callbackData = {RequestFunction(), + CYNARA_API_ACCESS_DENIED, + CYNARA_CALL_CAUSE_ANSWER}; + loadAgentPlugin(); + setAgentPolicy(); + + Agent agent; + AgentRequest agentRequests[numberOfRequests]; + Client client; + for (int i = 0; i < numberOfRequests; i++) { + CheckData data(testNo, i); + client.createRequest(data, ids[i], callbackData); + client.assertStatus(READWRITE); + auto testTimeLimit = std::chrono::seconds(5); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, restartCynara, + getAgentRequest, std::ref(agent), std::ref(agentRequests[i]), + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + agentRequests[i].assertAction(data.m_client, data.m_user, data.m_privilege); + }; + for (int i = numberOfRequests - 1; i >= 0; i--) { + agent.putResponse(AgentResponse::createDeny(ids[i])); + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_NO_TIMEOUT, 2); + } +} + +void tcag14_client_disconnects_func() +{ + std::string testNo("14"); + CheckData data(testNo); + cynara_check_id id; + RequestEntity callbackData = {RequestFunction(), + CYNARA_API_ACCESS_ALLOWED, + CYNARA_CALL_CAUSE_FINISH}; + + loadAgentPlugin(); + setAgentPolicy(); + Agent agent; + AgentRequest agentRequest; + auto testTimeLimit = std::chrono::seconds(5); + { + Client client; + client.createRequest(data, id, callbackData); + client.assertStatus(READWRITE); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, + restartCynara, getAgentRequest, std::ref(agent), + std::ref(agentRequest), std::ref(client), + CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + }; + auto getAgentRequestWrap = [&agent, &agentRequest]() { + agent.getRequest(agentRequest, CYNARA_API_SUCCESS); + }; + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, + restartCynara, getAgentRequestWrap); + agentRequest.assertCancel(); +} + +void tcag15_agent_disconnects_func() +{ + std::string testNo("15"); + CheckData data(testNo); + cynara_check_id id; + RequestEntity callbackData = {RequestFunction(), + CYNARA_API_ACCESS_DENIED, + CYNARA_CALL_CAUSE_ANSWER}; + + loadAgentPlugin(); + setAgentPolicy(); + Client client; + client.createRequest(data, id, callbackData); + client.assertStatus(READWRITE); + AgentRequest agentRequest; + { + Agent agent; + auto testTimeLimit = std::chrono::seconds(5); + Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED, restartCynara, + getAgentRequest, std::ref(agent), std::ref(agentRequest), + std::ref(client), CYNARA_API_SUCCESS, Timeout::ExpectMode::TIMEOUT); + }; + client.process(CYNARA_API_SUCCESS, Client::TimeoutExpectation::EXPECT_NO_TIMEOUT, 2); +} + RUNNER_TEST_GROUP_INIT(cynara_agent_tests) RUN_CYNARA_TEST(tcag01_set_agent_type_policy_without_plugin) @@ -199,3 +456,11 @@ 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) +RUN_CYNARA_TEST(tcag08_get_request_and_respond_with_wrong_id) +RUN_CYNARA_TEST(tcag09_get_request_and_correct_responded_id) +RUN_CYNARA_TEST(tcag10_cancel_request) +RUN_CYNARA_TEST(tcag11_cancel_processed_request) +RUN_CYNARA_TEST(tcag12_create_two_requests) +RUN_CYNARA_TEST(tcag13_create_many_requests) +RUN_CYNARA_TEST(tcag14_client_disconnects) +RUN_CYNARA_TEST(tcag15_agent_disconnects) -- 2.7.4