Fix security_manager_02_app_install_uninstall_full test
[platform/core/test/security-tests.git] / tests / 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 = 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.");
60
61     assertStatus(DISCONNECTED);
62 }
63
64 Client::~Client() noexcept(false)
65 {
66     bool oops = std::uncaught_exception();
67     try {
68         cynara_async_finish(m_cynara);
69         assertStatus(DISCONNECTED);
70     } catch (...) {
71         if (!oops)
72             throw;
73         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing CynaraTestAsync::Client.");
74     }
75 }
76
77 void Client::assertStatus(enum SocketStatus expectedStatus)
78 {
79     enum SocketStatus currentStatus = m_statusMonitor.getStatus();
80     RUNNER_ASSERT_MSG(currentStatus == expectedStatus,
81                          "SocketStatus mismatch: "
82                              << " currentStatus = " << currentStatus << ","
83                              << " expectedStatus = " << expectedStatus << ".");
84 }
85
86 void Client::checkCache(const CheckData &checkData, int expectedResult)
87 {
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 << ".");
99 }
100
101 void Client::createRequest(const CheckData &checkData, cynara_check_id &id,
102                            const RequestEntity &callbackData, int expectedResult)
103 {
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);
111
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 << ".");
120 }
121
122 void Client::process(int expectedResult,
123                      enum TimeoutExpectation timeoutExpectation,
124                      time_t timeoutSeconds) {
125     if (m_statusMonitor.getStatus() == DISCONNECTED)
126         return;
127
128     int fd = m_statusMonitor.getFd();
129     fd_set fds;
130     timeval tv;
131     FD_ZERO(&fds);
132     FD_SET(fd, &fds);
133     tv.tv_sec = timeoutSeconds;
134     tv.tv_usec = 0;
135
136     int ret;
137     if (m_statusMonitor.getStatus() == READ)
138         ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, NULL, NULL, &tv));
139     else
140         ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, &fds, NULL, &tv));
141
142     if (ret == 0) {
143         RUNNER_ASSERT_ERRNO_MSG(timeoutExpectation != EXPECT_NO_TIMEOUT,
144                                    "Unexpected select timeout."
145                                    << " ret = " << ret);
146         return;
147     }
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);
154
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 << ".");
160 }
161
162 void Client::cancel(cynara_check_id id, int expectedResult)
163 {
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 << ".");
170 }
171
172 }// namespace CynaraTestClientAsync