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)
55 int ret = cynara_async_initialize(&m_cynara, nullptr, StatusMonitor::updateStatus,
56 reinterpret_cast<void*>(&m_statusMonitor));
57 RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
58 "cynara_async_initialize() failed. ret = " << ret << ".");
59 RUNNER_ASSERT_MSG(m_cynara != nullptr, "cynara_async struct was not initialized.");
61 assertStatus(DISCONNECTED);
64 Client::~Client() noexcept(false)
66 bool oops = std::uncaught_exception();
68 cynara_async_finish(m_cynara);
69 assertStatus(DISCONNECTED);
73 RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
77 void Client::assertStatus(enum SocketStatus expectedStatus)
79 enum SocketStatus currentStatus = m_statusMonitor.getStatus();
80 RUNNER_ASSERT_MSG(currentStatus == expectedStatus,
81 "SocketStatus mismatch: "
82 << " currentStatus = " << currentStatus << ","
83 << " expectedStatus = " << expectedStatus << ".");
86 void Client::checkCache(const CheckData &checkData, int expectedResult)
88 int ret = cynara_async_check_cache(m_cynara, checkData.m_client.c_str(),
89 checkData.m_session.c_str(), checkData.m_user.c_str(),
90 checkData.m_privilege.c_str());
91 RUNNER_ASSERT_MSG(ret == expectedResult,
92 "Cache check returned unexpected value: "
93 << " returned value = " << ret << ","
94 << " expected value = " << expectedResult << ","
95 << " client = " << checkData.m_client << ","
96 << " sesion = " << checkData.m_session << ","
97 << " user = " << checkData.m_user << ","
98 << " privilege = " << checkData.m_privilege << ".");
101 void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
102 const RequestEntity &callbackData, int expectedResult)
104 int ret = cynara_async_create_request(m_cynara, checkData.m_client.c_str(),
105 checkData.m_session.c_str(), checkData.m_user.c_str(),
106 checkData.m_privilege.c_str(), &id,
107 RequestMonitor::updateResponse,
108 reinterpret_cast<void*>(&m_requestMonitor));
109 if (ret == CYNARA_API_SUCCESS)
110 m_requestMonitor.registerRequest(id, callbackData);
112 RUNNER_ASSERT_MSG(ret == expectedResult,
113 "Create request returned unexpected value: "
114 << " returned value = " << ret << ","
115 << " expected value = " << expectedResult << ","
116 << " client = " << checkData.m_client << ","
117 << " sesion = " << checkData.m_session << ","
118 << " user = " << checkData.m_user << ","
119 << " privilege = " << checkData.m_privilege << ".");
122 void Client::process(int expectedResult,
123 enum TimeoutExpectation timeoutExpectation,
124 time_t timeoutSeconds) {
125 if (m_statusMonitor.getStatus() == DISCONNECTED)
128 int fd = m_statusMonitor.getFd();
133 tv.tv_sec = timeoutSeconds;
137 if (m_statusMonitor.getStatus() == READ)
138 ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, NULL, NULL, &tv));
140 ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, &fds, NULL, &tv));
143 RUNNER_ASSERT_ERRNO_MSG(timeoutExpectation != EXPECT_NO_TIMEOUT,
144 "Unexpected select timeout."
145 << " ret = " << ret);
148 RUNNER_ASSERT_ERRNO_MSG(ret > 0,
149 "Select returned error:"
150 << " ret = " << ret);
151 RUNNER_ASSERT_ERRNO_MSG(timeoutExpectation != EXPECT_TIMEOUT,
152 "Select returned positive value, when timeout was expected."
153 << " ret = " << ret);
155 ret = cynara_async_process(m_cynara);
156 RUNNER_ASSERT_MSG(ret == expectedResult,
157 "cynara_async_process returned unexpected value: "
158 << " returned value = " << ret << ","
159 << " expected value = " << expectedResult << ".");
162 void Client::cancel(cynara_check_id id, int expectedResult)
164 int ret = cynara_async_cancel_request(m_cynara, id);
165 RUNNER_ASSERT_MSG(ret == expectedResult,
166 "Cancel request returned unexpected value: "
167 << " returned value = " << ret << ","
168 << " expected value = " << expectedResult << ","
169 << " id = " << id << ".");
172 }// namespace CynaraTestClientAsync