21c94603fc90661b33a9beb1c31e4201a090169a
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_client_async_client.cpp
1 /*
2  * Copyright (c) 2014-2020 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, std::string())),
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()
67 {
68     try {
69         RUNNER_DEFER_SCOPE(cynara_async_finish(m_cynara););
70         assertStatus(DISCONNECTED);
71     } catch (...) {
72         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
73     }
74 }
75
76 void Client::assertStatus(enum SocketStatus expectedStatus)
77 {
78     enum SocketStatus currentStatus = m_statusMonitor.getStatus();
79     RUNNER_ASSERT_MSG(currentStatus == expectedStatus,
80                          "SocketStatus mismatch: "
81                              << " currentStatus = " << currentStatus << ","
82                              << " expectedStatus = " << expectedStatus << ".");
83 }
84
85 void Client::checkCache(const CheckData &checkData, int expectedResult)
86 {
87     int ret;
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 << ".");
100 }
101
102 void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
103                            const RequestEntity &callbackData, int expectedResult)
104 {
105     int ret;
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,
111                                                          static_cast<void*>(
112                                                              &m_requestMonitor)););
113     if (ret == CYNARA_API_SUCCESS)
114         m_requestMonitor.registerRequest(id, callbackData);
115
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 << ".");
124 }
125
126 void Client::process(int expectedResult,
127                      enum TimeoutExpectation timeoutExpectation,
128                      time_t timeoutSeconds) {
129     if (m_statusMonitor.getStatus() == DISCONNECTED)
130         return;
131
132     int fd = m_statusMonitor.getFd();
133     fd_set fds;
134     timeval tv;
135     FD_ZERO(&fds);
136     FD_SET(fd, &fds);
137     tv.tv_sec = timeoutSeconds;
138     tv.tv_usec = 0;
139
140 #pragma GCC diagnostic push
141 #pragma GCC diagnostic ignored "-Wrestrict"
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 #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);
162
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 << ".");
168 }
169
170 void Client::cancel(cynara_check_id id, int expectedResult)
171 {
172     int ret;
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 << ".");
179 }
180
181 }// namespace CynaraTestClientAsync