2a2e0aefd67faedaf806eb5f4d5641ee39fe0d6e
[platform/core/test/security-tests.git] / src / cynara-tests / common / cynara_test_client_async_status_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_status_monitor.h>
18
19 #include <dpl/test/test_runner.h>
20
21 namespace CynaraTestClientAsync {
22
23 StatusMonitor::StatusMonitor(const StatusFunction &userFunction)
24     : m_fd(-1), m_status(CYNARA_STATUS_FOR_READ), m_userFunction(userFunction)
25 {
26 }
27
28 void StatusMonitor::updateStatus(int oldFd, int newFd, cynara_async_status status, void *data)
29 {
30     StatusMonitor *monitor = reinterpret_cast<StatusMonitor*>(data);
31     if (!monitor) {
32         RUNNER_FAIL_MSG("Bad user data (nullptr) in status callback.");
33         return;
34     }
35
36     RUNNER_ASSERT_MSG(monitor->m_fd == oldFd,
37                          "fd value mismatch: "
38                              << " last saved fd = " << monitor->m_fd << ","
39                              << " callback oldFd = " << oldFd << ".");
40
41     monitor->m_fd = newFd;
42     monitor->m_status = status;
43     if (monitor->m_userFunction)
44         monitor->m_userFunction(oldFd, newFd, status);
45 }
46
47 int StatusMonitor::getFd(void) const
48 {
49     return m_fd;
50 }
51
52 enum SocketStatus StatusMonitor::getStatus(void) const
53 {
54     if (m_fd == -1)
55         return DISCONNECTED;
56
57     switch (m_status) {
58         case CYNARA_STATUS_FOR_READ:
59             return READ;
60         case CYNARA_STATUS_FOR_RW:
61             return READWRITE;
62     }
63     RUNNER_FAIL_MSG("Unknown cynara socket status = " << m_status << ","
64                         << " fd = " << m_fd << ".");
65     return UNKNOWN;
66 }
67
68 }// namespace CynaraTestClientAsync