Coloring compiler's output
[platform/core/security/vasum.git] / tests / unit_tests / epoll / ut-event-poll.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19
20 /**
21  * @file
22  * @author  Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
23  * @brief   Unit tests of event poll
24  */
25
26 #include "config.hpp"
27 #include "ut.hpp"
28
29 #include "epoll/event-poll.hpp"
30 #include "logger/logger.hpp"
31 #include "ipc/internals/socket.hpp"
32 #include "utils/latch.hpp"
33 #include "utils/glib-loop.hpp"
34 #include "epoll/glib-poll-dispatcher.hpp"
35 #include "epoll/thread-poll-dispatcher.hpp"
36
37 using namespace vasum::utils;
38 using namespace vasum::epoll;
39 using namespace vasum::ipc;
40
41 namespace {
42
43 const int unsigned TIMEOUT = 1000;
44
45 } // namespace
46
47 BOOST_AUTO_TEST_SUITE(EventPollSuite)
48
49 BOOST_AUTO_TEST_CASE(EmptyPoll)
50 {
51     EventPoll poll;
52     BOOST_CHECK(!poll.dispatchIteration(0));
53 }
54
55 BOOST_AUTO_TEST_CASE(ThreadedPoll)
56 {
57     ThreadPollDispatcher dispatcher;
58 }
59
60 BOOST_AUTO_TEST_CASE(GlibPoll)
61 {
62     ScopedGlibLoop loop;
63
64     GlibPollDispatcher dispatcher;
65 }
66
67 void doSocketTest(EventPoll& poll)
68 {
69     const std::string PATH = "/tmp/ut-poll.sock";
70     const std::string MESSAGE = "This is a test message";
71
72     Latch goodMessage;
73     Latch remoteClosed;
74
75     Socket listen = Socket::createSocket(PATH);
76     std::shared_ptr<Socket> server;
77
78     auto serverCallback = [&](int, Events events) -> bool {
79         LOGD("Server events: " << eventsToString(events));
80
81         if (events & EPOLLOUT) {
82             server->write(MESSAGE.data(), MESSAGE.size());
83             poll.removeFD(server->getFD());
84             server.reset();
85         }
86         return true;
87     };
88
89     auto listenCallback = [&](int, Events events) -> bool {
90         LOGD("Listen events: " << eventsToString(events));
91         if (events & EPOLLIN) {
92             server = listen.accept();
93             poll.addFD(server->getFD(), EPOLLHUP | EPOLLRDHUP | EPOLLOUT, serverCallback);
94         }
95         return true;
96     };
97
98     poll.addFD(listen.getFD(), EPOLLIN, listenCallback);
99
100     Socket client = Socket::connectSocket(PATH);
101
102     auto clientCallback = [&](int, Events events) -> bool {
103         LOGD("Client events: " << eventsToString(events));
104
105         if (events & EPOLLIN) {
106             std::string ret(MESSAGE.size(), 'x');
107             client.read(&ret.front(), ret.size());
108             if (ret == MESSAGE) {
109                 goodMessage.set();
110             }
111         }
112         if (events & EPOLLRDHUP) {
113             poll.removeFD(client.getFD());
114             remoteClosed.set();
115         }
116         return true;
117     };
118
119     poll.addFD(client.getFD(), EPOLLHUP | EPOLLRDHUP | EPOLLIN, clientCallback);
120
121     BOOST_CHECK(goodMessage.wait(TIMEOUT));
122     BOOST_CHECK(remoteClosed.wait(TIMEOUT));
123
124     poll.removeFD(listen.getFD());
125 }
126
127 BOOST_AUTO_TEST_CASE(ThreadedPollSocket)
128 {
129     ThreadPollDispatcher dispatcher;
130
131     doSocketTest(dispatcher.getPoll());
132 }
133
134 BOOST_AUTO_TEST_CASE(GlibPollSocket)
135 {
136     ScopedGlibLoop loop;
137
138     GlibPollDispatcher dispatcher;
139
140     doSocketTest(dispatcher.getPoll());
141 }
142
143 BOOST_AUTO_TEST_CASE(PollStacking)
144 {
145     ThreadPollDispatcher dispatcher;
146
147     EventPoll innerPoll;
148
149     auto dispatchInner = [&](int, Events) -> bool {
150         innerPoll.dispatchIteration(0);
151         return true;
152     };
153     dispatcher.getPoll().addFD(innerPoll.getPollFD(), EPOLLIN, dispatchInner);
154     doSocketTest(innerPoll);
155     dispatcher.getPoll().removeFD(innerPoll.getPollFD());
156 }
157
158 BOOST_AUTO_TEST_SUITE_END()
159