2 * Copyright (c) 2014-2020 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, std::string())),
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);
69 RUNNER_DEFER_SCOPE(cynara_async_finish(m_cynara););
70 assertStatus(DISCONNECTED);
72 RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
76 void Client::assertStatus(enum SocketStatus expectedStatus)
78 enum SocketStatus currentStatus = m_statusMonitor.getStatus();
79 RUNNER_ASSERT_MSG(currentStatus == expectedStatus,
80 "SocketStatus mismatch: "
81 << " currentStatus = " << currentStatus << ","
82 << " expectedStatus = " << expectedStatus << ".");
85 void Client::checkCache(const CheckData &checkData, int expectedResult)
88 RUNNER_DEFER_SCOPE(ret = cynara_async_check_cache(m_cynara, checkData.m_client.c_str(),
89 checkData.m_session.c_str(),
90 checkData.m_user.c_str(),
91 checkData.m_privilege.c_str()););
92 RUNNER_ASSERT_MSG(ret == expectedResult,
93 "Cache check returned unexpected value: "
94 << " returned value = " << ret << ","
95 << " expected value = " << expectedResult << ","
96 << " client = " << checkData.m_client << ","
97 << " sesion = " << checkData.m_session << ","
98 << " user = " << checkData.m_user << ","
99 << " privilege = " << checkData.m_privilege << ".");
102 void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
103 const RequestEntity &callbackData, int expectedResult)
106 RUNNER_DEFER_SCOPE(ret = cynara_async_create_request(m_cynara, checkData.m_client.c_str(),
107 checkData.m_session.c_str(),
108 checkData.m_user.c_str(),
109 checkData.m_privilege.c_str(), &id,
110 RequestMonitor::updateResponse,
112 &m_requestMonitor)););
113 if (ret == CYNARA_API_SUCCESS)
114 m_requestMonitor.registerRequest(id, callbackData);
116 RUNNER_ASSERT_MSG(ret == expectedResult,
117 "Create request returned unexpected value: "
118 << " returned value = " << ret << ","
119 << " expected value = " << expectedResult << ","
120 << " client = " << checkData.m_client << ","
121 << " sesion = " << checkData.m_session << ","
122 << " user = " << checkData.m_user << ","
123 << " privilege = " << checkData.m_privilege << ".");
126 void Client::process(int expectedResult,
127 enum TimeoutExpectation timeoutExpectation,
128 time_t timeoutSeconds) {
129 if (m_statusMonitor.getStatus() == DISCONNECTED)
132 int fd = m_statusMonitor.getFd();
137 tv.tv_sec = timeoutSeconds;
140 #pragma GCC diagnostic push
141 #pragma GCC diagnostic ignored "-Wrestrict"
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 #pragma GCC diagnostic push
156 RUNNER_ASSERT_ERRNO_MSG(ret > 0,
157 "Select returned error:"
158 << " ret = " << ret);
159 RUNNER_ASSERT_MSG(timeoutExpectation != EXPECT_TIMEOUT,
160 "Select returned positive value, when timeout was expected."
161 << " ret = " << ret);
163 RUNNER_DEFER_SCOPE(ret = cynara_async_process(m_cynara););
164 RUNNER_ASSERT_MSG(ret == expectedResult,
165 "cynara_async_process returned unexpected value: "
166 << " returned value = " << ret << ","
167 << " expected value = " << expectedResult << ".");
170 void Client::cancel(cynara_check_id id, int expectedResult)
173 RUNNER_DEFER_SCOPE(ret = cynara_async_cancel_request(m_cynara, id););
174 RUNNER_ASSERT_MSG(ret == expectedResult,
175 "Cancel request returned unexpected value: "
176 << " returned value = " << ret << ","
177 << " expected value = " << expectedResult << ","
178 << " id = " << id << ".");
181 }// namespace CynaraTestClientAsync