Fix casting from and to void*
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_client_async_client.cpp
1 /*
2  * Copyright (c) 2014 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 <cynara_test_client_async_client.h>
18
19 #include <dpl/test/test_runner.h>
20
21 #include <cynara-client-async.h>
22
23 #include <exception>
24 #include <unistd.h>
25
26 namespace CynaraTestClientAsync {
27
28 static std::string suffix(const std::string &major, const std::string &minor)
29 {
30     if (minor.empty())
31         return major;
32     return "_" + major + "_" + minor;
33 }
34
35 CheckData::CheckData(const std::string &major, const std::string &minor) :
36     m_client("client" + suffix(major, minor)),
37     m_session("session" + suffix(major, minor)),
38     m_user("user" + suffix(major, minor)),
39     m_privilege("privilege" + suffix(major, minor))
40 {
41 }
42
43 CheckData::CheckData(const std::string &major, int minor) : CheckData(major, std::to_string(minor))
44 {
45 }
46
47 CheckKey CheckData::toAdminPolicy()
48 {
49     return {m_client.c_str(), m_user.c_str(), m_privilege.c_str()};
50 }
51
52 Client::Client(const StatusFunction &userFunction)
53     : m_cynara(nullptr), m_statusMonitor(userFunction)
54 {
55     int ret;
56     RUNNER_DEFER_SCOPE(ret = cynara_async_initialize(&m_cynara, nullptr,
57                                                      StatusMonitor::updateStatus,
58                                                      static_cast<void*>(&m_statusMonitor)););
59     RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
60                          "cynara_async_initialize() failed. ret = " << ret << ".");
61     RUNNER_ASSERT_MSG(m_cynara != nullptr, "cynara_async struct was not initialized.");
62
63     assertStatus(DISCONNECTED);
64 }
65
66 Client::~Client() noexcept(false)
67 {
68     bool oops = std::uncaught_exception();
69     try {
70         RUNNER_DEFER_SCOPE(cynara_async_finish(m_cynara););
71         assertStatus(DISCONNECTED);
72     } catch (...) {
73         if (!oops)
74             throw;
75         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
76     }
77 }
78
79 void Client::assertStatus(enum SocketStatus expectedStatus)
80 {
81     enum SocketStatus currentStatus = m_statusMonitor.getStatus();
82     RUNNER_ASSERT_MSG(currentStatus == expectedStatus,
83                          "SocketStatus mismatch: "
84                              << " currentStatus = " << currentStatus << ","
85                              << " expectedStatus = " << expectedStatus << ".");
86 }
87
88 void Client::checkCache(const CheckData &checkData, int expectedResult)
89 {
90     int ret;
91     RUNNER_DEFER_SCOPE(ret = cynara_async_check_cache(m_cynara, checkData.m_client.c_str(),
92                                                       checkData.m_session.c_str(),
93                                                       checkData.m_user.c_str(),
94                                                       checkData.m_privilege.c_str()););
95     RUNNER_ASSERT_MSG(ret == expectedResult,
96                          "Cache check returned unexpected value: "
97                              << " returned value = " << ret << ","
98                              << " expected value = " << expectedResult << ","
99                              << " client = " << checkData.m_client << ","
100                              << " sesion = " << checkData.m_session << ","
101                              << " user = " << checkData.m_user << ","
102                              << " privilege = " << checkData.m_privilege << ".");
103 }
104
105 void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
106                            const RequestEntity &callbackData, int expectedResult)
107 {
108     int ret;
109     RUNNER_DEFER_SCOPE(ret = cynara_async_create_request(m_cynara, checkData.m_client.c_str(),
110                                                          checkData.m_session.c_str(),
111                                                          checkData.m_user.c_str(),
112                                                          checkData.m_privilege.c_str(), &id,
113                                                          RequestMonitor::updateResponse,
114                                                          static_cast<void*>(
115                                                              &m_requestMonitor)););
116     if (ret == CYNARA_API_SUCCESS)
117         m_requestMonitor.registerRequest(id, callbackData);
118
119     RUNNER_ASSERT_MSG(ret == expectedResult,
120                          "Create request returned unexpected value: "
121                              << " returned value = " << ret << ","
122                              << " expected value = " << expectedResult << ","
123                              << " client = " << checkData.m_client << ","
124                              << " sesion = " << checkData.m_session << ","
125                              << " user = " << checkData.m_user << ","
126                              << " privilege = " << checkData.m_privilege << ".");
127 }
128
129 void Client::process(int expectedResult,
130                      enum TimeoutExpectation timeoutExpectation,
131                      time_t timeoutSeconds) {
132     if (m_statusMonitor.getStatus() == DISCONNECTED)
133         return;
134
135     int fd = m_statusMonitor.getFd();
136     fd_set fds;
137     timeval tv;
138     FD_ZERO(&fds);
139     FD_SET(fd, &fds);
140     tv.tv_sec = timeoutSeconds;
141     tv.tv_usec = 0;
142
143     int ret;
144     if (m_statusMonitor.getStatus() == READ)
145         ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, NULL, NULL, &tv));
146     else
147         ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, &fds, NULL, &tv));
148
149     if (ret == 0) {
150         RUNNER_ASSERT_MSG(timeoutExpectation != EXPECT_NO_TIMEOUT,
151                              "Unexpected select timeout."
152                              << " ret = " << ret);
153         return;
154     }
155     RUNNER_ASSERT_ERRNO_MSG(ret > 0,
156                                "Select returned error:"
157                                << " ret = " << ret);
158     RUNNER_ASSERT_MSG(timeoutExpectation != EXPECT_TIMEOUT,
159                          "Select returned positive value, when timeout was expected."
160                          << " ret = " << ret);
161
162     RUNNER_DEFER_SCOPE(ret = cynara_async_process(m_cynara););
163     RUNNER_ASSERT_MSG(ret == expectedResult,
164                          "cynara_async_process returned unexpected value: "
165                              << " returned value = " << ret << ","
166                              << " expected value = " << expectedResult << ".");
167 }
168
169 void Client::cancel(cynara_check_id id, int expectedResult)
170 {
171     int ret;
172     RUNNER_DEFER_SCOPE(ret = cynara_async_cancel_request(m_cynara, id););
173     RUNNER_ASSERT_MSG(ret == expectedResult,
174                          "Cancel request returned unexpected value: "
175                              << " returned value = " << ret << ","
176                              << " expected value = " << expectedResult << ","
177                              << " id = " << id << ".");
178 }
179
180 }// namespace CynaraTestClientAsync