2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <cynara_test_client_async_client.h>
19 #include <dpl/test/test_runner.h>
21 #include <cynara-client-async.h>
26 namespace CynaraTestClientAsync {
28 static std::string suffix(const std::string &major, const std::string &minor)
32 return "_" + major + "_" + minor;
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))
43 CheckData::CheckData(const std::string &major, int minor) : CheckData(major, std::to_string(minor))
47 CheckKey CheckData::toAdminPolicy()
49 return {m_client.c_str(), m_user.c_str(), m_privilege.c_str()};
52 Client::Client(const StatusFunction &userFunction)
53 : m_cynara(nullptr), m_statusMonitor(userFunction)
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.");
63 assertStatus(DISCONNECTED);
66 Client::~Client() noexcept(false)
68 bool oops = std::uncaught_exception();
70 RUNNER_DEFER_SCOPE(cynara_async_finish(m_cynara););
71 assertStatus(DISCONNECTED);
75 RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
79 void Client::assertStatus(enum SocketStatus expectedStatus)
81 enum SocketStatus currentStatus = m_statusMonitor.getStatus();
82 RUNNER_ASSERT_MSG(currentStatus == expectedStatus,
83 "SocketStatus mismatch: "
84 << " currentStatus = " << currentStatus << ","
85 << " expectedStatus = " << expectedStatus << ".");
88 void Client::checkCache(const CheckData &checkData, int expectedResult)
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 << ".");
105 void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
106 const RequestEntity &callbackData, int expectedResult)
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,
115 &m_requestMonitor)););
116 if (ret == CYNARA_API_SUCCESS)
117 m_requestMonitor.registerRequest(id, callbackData);
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 << ".");
129 void Client::process(int expectedResult,
130 enum TimeoutExpectation timeoutExpectation,
131 time_t timeoutSeconds) {
132 if (m_statusMonitor.getStatus() == DISCONNECTED)
135 int fd = m_statusMonitor.getFd();
140 tv.tv_sec = timeoutSeconds;
144 if (m_statusMonitor.getStatus() == READ)
145 ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, NULL, NULL, &tv));
147 ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, &fds, NULL, &tv));
150 RUNNER_ASSERT_MSG(timeoutExpectation != EXPECT_NO_TIMEOUT,
151 "Unexpected select timeout."
152 << " ret = " << ret);
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);
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 << ".");
169 void Client::cancel(cynara_check_id id, int expectedResult)
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 << ".");
180 }// namespace CynaraTestClientAsync