Fix casting from and to void*
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_client_async_request_monitor.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_request_monitor.h>
18
19 #include <dpl/test/test_runner.h>
20
21 #include <exception>
22
23 namespace CynaraTestClientAsync {
24
25 RequestMonitor::~RequestMonitor() noexcept(false)
26 {
27     bool oops = std::uncaught_exception();
28     try {
29         for (auto ent : m_requests)
30         {
31             RUNNER_ERROR_MSG("There was no callback for request with:"
32                           << "id = " << ent.first << ","
33                           << "expectedResponse = " << ent.second.m_expectedResponse << ","
34                           << "expectedCause = " << ent.second.m_expectedCause << ".");
35         }
36         RUNNER_ASSERT_MSG(m_requests.empty(),
37                              m_requests.size() << "requests does not receive callback.");
38     } catch (...) {
39         if (!oops)
40             throw;
41         RUNNER_ERROR_MSG("Error: more exceptions thrown while releasing"
42                              " CynaraTestAsync::RequestMonitor.");
43     }
44 }
45
46 void RequestMonitor::registerRequest(cynara_check_id id, const RequestEntity &request)
47 {
48     auto p = m_requests.insert({id, request});
49     RUNNER_ASSERT_MSG(p.second,
50                           "Request with id = " << p.first->first << " already exists.");
51 }
52
53 void RequestMonitor::updateResponse(cynara_check_id checkId, cynara_async_call_cause cause,
54                                     int response, void *data)
55 {
56     RUNNER_DEFER_TRYCATCH(
57         RequestMonitor *monitor = static_cast<RequestMonitor*>(data);
58         if (!monitor) {
59             RUNNER_FAIL_MSG("Bad user data (nullptr) in response callback.");
60             return;
61         }
62
63         auto it = monitor->m_requests.find(checkId);
64         if (it == monitor->m_requests.end()) {
65             RUNNER_FAIL_MSG("Received unexpected callback for request:"
66                                << "id = " << checkId << ","
67                                << "response = " << response << ","
68                                << "cause = " << cause << ".");
69             return;
70         }
71
72         //save request data and remove request from monitored requests
73         auto expectedResponse = it->second.m_expectedResponse;
74         auto expectedCause = it->second.m_expectedCause;
75         auto userFunction = it->second.m_userFunction;
76         monitor->m_requests.erase(it);
77
78         RUNNER_ASSERT_MSG(cause == expectedCause,
79                              "Unexpected cause in response callback:"
80                                 << "id = " << checkId << ","
81                                 << "received response = " << response << ","
82                                 << "expected response = " << expectedResponse << ","
83                                 << "received cause = " << cause << ","
84                                 << "expected cause = " << expectedCause << ".");
85
86         if (cause == CYNARA_CALL_CAUSE_ANSWER)
87         {
88             RUNNER_ASSERT_MSG(response == expectedResponse,
89                                  "Unexpected response in response callback:"
90                                     << "id = " << checkId << ","
91                                     << "received response = " << response << ","
92                                     << "expected response = " << expectedResponse << ","
93                                     << "received cause = " << cause << ","
94                                     << "expected cause = " << expectedCause << ".");
95         }
96
97         if (userFunction)
98             userFunction();
99     );
100 }
101
102 }// namespace CynaraTestClientAsync