Categorize tests into positive and negative
[platform/core/security/key-manager.git] / tests / test_comm-manager.cpp
1 /*
2  *  Copyright (c) 2000-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  * @file       test_comm-manager.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <boost_macros_wrapper.h>
23 #include <boost/test/results_reporter.hpp>
24 #include <communication-manager.h>
25 #include <string>
26 #include <random>
27 #include <chrono>
28
29 namespace {
30 struct MessageA {
31         explicit MessageA(int ai) : i(ai) {}
32         int i;
33 };
34
35 struct MessageB {
36         explicit MessageB(char ac) : c(ac) {}
37         char c;
38 };
39
40 struct MessageC {
41         explicit MessageC(const std::string &astr) : str(astr) {}
42         std::string str;
43 };
44
45 struct Listener {
46         Listener() : i(0) {}
47
48         void Handle(const MessageA &msg)
49         {
50                 i = msg.i;
51         }
52
53         void Handle(const MessageC &msg)
54         {
55                 str = msg.str;
56         }
57
58         int i;
59         std::string str;
60 };
61
62 } // namespace anonymous
63
64 BOOST_AUTO_TEST_SUITE(MESSAGE_MANAGER_TEST)
65
66 POSITIVE_TEST_CASE(TMM_0010_NoListener)
67 {
68         CKM::CommunicationManager<MessageA> mgr;
69         BOOST_REQUIRE_MESSAGE(0 == mgr.SendMessage(MessageA(22)),
70                                                   "There should be no listener.");
71 }
72
73 POSITIVE_TEST_CASE(TMM_0020_Basic)
74 {
75         CKM::CommunicationManager<MessageA> mgr;
76         int received = 0;
77         mgr.Register<MessageA>([&](const MessageA & msg) {
78                 received = msg.i;
79         });
80         BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(4)), "No listener found");
81         BOOST_REQUIRE_MESSAGE(received != 0, "Message not received");
82         BOOST_REQUIRE_MESSAGE(received == 4, "Wrong message received i=" << received);
83 }
84
85 POSITIVE_TEST_CASE(TMM_0030_MultipleMessages)
86 {
87         CKM::CommunicationManager<MessageA, MessageB> mgr;
88         int reci = 0;
89         char recc = 0;
90         mgr.Register<MessageA>([&](const MessageA & msg) {
91                 reci = msg.i;
92         });
93         mgr.Register<MessageB>([&](const MessageB & msg) {
94                 recc = msg.c;
95         });
96         BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageB('c')), "No listener found");
97         BOOST_REQUIRE_MESSAGE(reci == 0, "Unexpected message received");
98         BOOST_REQUIRE_MESSAGE(recc != 0, "Message not received");
99         BOOST_REQUIRE_MESSAGE(recc == 'c', "Wrong message received c=" << recc);
100
101         BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(42)), "No listener found");
102         BOOST_REQUIRE_MESSAGE(reci != 0, "Message not received");
103         BOOST_REQUIRE_MESSAGE(reci == 42, "Wrong message received i=" << reci);
104         BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc);
105 }
106
107 POSITIVE_TEST_CASE(TMM_0040_Listener)
108 {
109         CKM::CommunicationManager<MessageA, MessageB, MessageC> mgr;
110         Listener l;
111         mgr.Register<MessageC>([&](const MessageC & msg) {
112                 l.Handle(msg);
113         });
114         mgr.Register<MessageA>([&](const MessageA & msg) {
115                 l.Handle(msg);
116         });
117
118         BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageC("lorem ipsum")),
119                                                   "No listener found");
120         BOOST_REQUIRE_MESSAGE(l.i == 0, "Unexpected message received");
121         BOOST_REQUIRE_MESSAGE(!l.str.empty(), "Message not received");
122         BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum",
123                                                   "Wrong message received c=" << l.str);
124
125         BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(3)), "No listener found");
126         BOOST_REQUIRE_MESSAGE(l.i != 0, "Message not received");
127         BOOST_REQUIRE_MESSAGE(l.i == 3, "Wrong message received i=" << l.i);
128         BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum",
129                                                   "Previous message overwritten str=" << l.str);
130 }
131
132 POSITIVE_TEST_CASE(TMM_0050_2Listeners)
133 {
134         CKM::CommunicationManager<MessageA> mgr;
135         bool called[2];
136         called[0] = false;
137         called[1] = false;
138         mgr.Register<MessageA>([&](const MessageA & msg) {
139                 BOOST_REQUIRE_MESSAGE(msg.i == 5, "Unexpected message received i=" << msg.i);
140                 called[0] = true;
141         });
142         mgr.Register<MessageA>([&](const MessageA & msg) {
143                 BOOST_REQUIRE_MESSAGE(msg.i == 5, "Unexpected message received i=" << msg.i);
144                 called[1] = true;
145         });
146
147         BOOST_REQUIRE_MESSAGE(2 == mgr.SendMessage(MessageA(5)), "No listener found");
148         BOOST_REQUIRE_MESSAGE(called[0], "First listener not called");
149         BOOST_REQUIRE_MESSAGE(called[1], "Second listener not called");
150 }
151
152 POSITIVE_TEST_CASE(TMM_0060_Stress)
153 {
154         CKM::CommunicationManager<MessageA, MessageB, MessageC> mgr;
155
156         std::default_random_engine generator(
157                 std::chrono::system_clock::now().time_since_epoch().count());
158         std::uniform_int_distribution<size_t> message_dist(0, 2);
159         std::uniform_int_distribution<size_t> count_dist(1, 10);
160
161         size_t a = 0;
162         size_t b = 0;
163         size_t c = 0;
164         mgr.Register<MessageA>([&](const MessageA & msg) {
165                 BOOST_REQUIRE_MESSAGE(msg.i == 42, "Wrong message: " << msg.i);
166                 a++;
167         });
168         mgr.Register<MessageB>([&](const MessageB & msg) {
169                 BOOST_REQUIRE_MESSAGE(msg.c == 'c', "Wrong message: " << msg.c);
170                 b++;
171         });
172         mgr.Register<MessageC>([&](const MessageC & msg) {
173                 BOOST_REQUIRE_MESSAGE(msg.str == "lorem ipsum", "Wrong message: " << msg.str);
174                 c++;
175         });
176
177         for (size_t i = 0; i < 1000; i++) {
178                 size_t cnt = count_dist(generator);
179
180                 for (size_t s = 0; s < cnt; s++) {
181                         switch (message_dist(generator)) {
182                         case 0:
183                                 BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(42)), "No listener found");
184                                 a--;
185                                 break;
186
187                         case 1:
188                                 BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageB('c')), "No listener found");
189                                 b--;
190                                 break;
191
192                         case 2:
193                                 BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageC("lorem ipsum")),
194                                                                           "No listener found");
195                                 c--;
196                                 break;
197
198                         default:
199                                 BOOST_FAIL("Unexpected message type");
200                         }
201                 }
202         }
203
204         BOOST_REQUIRE_MESSAGE(a == 0, "Unexpected number of MessageA: " << a);
205         BOOST_REQUIRE_MESSAGE(b == 0, "Unexpected number of MessageB: " << b);
206         BOOST_REQUIRE_MESSAGE(c == 0, "Unexpected number of MessageC: " << c);
207 }
208
209 BOOST_AUTO_TEST_SUITE_END()
210
211