Add additional libcynara-agent tests
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_agent.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include <cstdlib>
18 #include <string>
19
20 #include <cynara_test_agent.h>
21 #include <plugins.h>
22 #include <dpl/test/test_runner.h>
23
24 namespace CynaraTestAgent {
25
26 Agent::Agent()
27     : m_agent(nullptr)
28 {
29     int ret = cynara_agent_initialize(&m_agent, CynaraTestPlugins::TEST_AGENT_TYPE.c_str());
30     RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
31                          "cynara_agent_initialize failed. ret: " << ret);
32     RUNNER_ASSERT_MSG(m_agent != nullptr,
33                          "cynara_agent struct was not initialized");
34 }
35
36 Agent::~Agent()
37 {
38     cynara_agent_finish(m_agent);
39 }
40
41 void Agent::getRequest(AgentRequest &request, int expectedResult)
42 {
43     cynara_agent_msg_type type;
44     cynara_agent_req_id id;
45     void *data = nullptr;
46     size_t dataSize;
47
48     int ret = cynara_agent_get_request(m_agent, &type, &id, &data, &dataSize);
49     if (ret == CYNARA_API_SUCCESS) {
50         RUNNER_ASSERT_MSG(!data == !dataSize,
51                              "cynara_agent_get_request returned contradictory values: "
52                                  << "data = " << data << " ,"
53                                  << "size = " << dataSize << ".");
54         request.set(type, id, data, dataSize);
55         free(data);
56     }
57     RUNNER_ASSERT_MSG(ret == expectedResult,
58                          "cynara_agent_get_request returned wrong value: "
59                              << ret << " != " << expectedResult << ".");
60 }
61
62 void Agent::putResponse(const AgentResponse &response, int expectedResult)
63 {
64     auto size = response.data().size();
65     int ret = cynara_agent_put_response(m_agent,
66                                         response.type(),
67                                         response.id(),
68                                         size ? static_cast<const void*>(response.data().data())
69                                                : nullptr,
70                                         size);
71
72     RUNNER_ASSERT_MSG(ret == expectedResult,
73                      "cynara_agent_put_response returned wrong value: "
74                          << ret << " != " << expectedResult << "."
75                          << "response = " << response);
76 }
77
78 } // namespace CynaraTestAgent