Fix potential buffer overflow error CID: 40674
[platform/core/security/key-manager.git] / tests / test_comm-manager.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 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/test/unit_test.hpp>
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     MessageA(int ai) : i(ai) {}
32     int i;
33 };
34
35 struct MessageB {
36     MessageB(char ac) : c(ac) {}
37     char c;
38 };
39
40 struct MessageC {
41     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         i = msg.i;
50     }
51
52     void Handle(const MessageC& msg) {
53         str = msg.str;
54     }
55
56     int i;
57     std::string str;
58 };
59
60 } // namespace anonymous
61
62 BOOST_AUTO_TEST_SUITE(MESSAGE_MANAGER_TEST)
63
64 BOOST_AUTO_TEST_CASE(TMM_0010_NoListener) {
65     CKM::CommunicationManager<MessageA> mgr;
66     BOOST_REQUIRE_MESSAGE(0 == mgr.SendMessage(MessageA(22)), "There should be no listener.");
67 }
68
69 BOOST_AUTO_TEST_CASE(TMM_0020_Basic) {
70     CKM::CommunicationManager<MessageA> mgr;
71     int received = 0;
72     mgr.Register<MessageA>([&](const MessageA& msg){ received = msg.i; });
73     BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(4)), "No listener found");
74     BOOST_REQUIRE_MESSAGE(received != 0, "Message not received");
75     BOOST_REQUIRE_MESSAGE(received == 4, "Wrong message received i=" << received);
76 }
77
78 BOOST_AUTO_TEST_CASE(TMM_0030_MultipleMessages) {
79     CKM::CommunicationManager<MessageA, MessageB> mgr;
80     int reci = 0;
81     char recc = 0;
82     mgr.Register<MessageA>([&](const MessageA& msg){ reci = msg.i; });
83     mgr.Register<MessageB>([&](const MessageB& msg){ recc = msg.c; });
84     BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageB('c')), "No listener found");
85     BOOST_REQUIRE_MESSAGE(reci == 0, "Unexpected message received");
86     BOOST_REQUIRE_MESSAGE(recc != 0, "Message not received");
87     BOOST_REQUIRE_MESSAGE(recc == 'c', "Wrong message received c=" << recc);
88
89     BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(42)), "No listener found");
90     BOOST_REQUIRE_MESSAGE(reci!= 0, "Message not received");
91     BOOST_REQUIRE_MESSAGE(reci == 42, "Wrong message received i=" << reci);
92     BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc);
93 }
94
95 BOOST_AUTO_TEST_CASE(TMM_0040_Listener) {
96     CKM::CommunicationManager<MessageA, MessageB, MessageC> mgr;
97     Listener l;
98     mgr.Register<MessageC>([&](const MessageC& msg){ l.Handle(msg); });
99     mgr.Register<MessageA>([&](const MessageA& msg){ l.Handle(msg); });
100
101     BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageC("lorem ipsum")), "No listener found");
102     BOOST_REQUIRE_MESSAGE(l.i == 0, "Unexpected message received");
103     BOOST_REQUIRE_MESSAGE(!l.str.empty(), "Message not received");
104     BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum", "Wrong message received c=" << l.str);
105
106     BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(3)), "No listener found");
107     BOOST_REQUIRE_MESSAGE(l.i!= 0, "Message not received");
108     BOOST_REQUIRE_MESSAGE(l.i == 3, "Wrong message received i=" << l.i);
109     BOOST_REQUIRE_MESSAGE(l.str == "lorem ipsum", "Previous message overwritten str=" << l.str);
110 }
111
112 BOOST_AUTO_TEST_CASE(TMM_0050_2Listeners) {
113     CKM::CommunicationManager<MessageA> mgr;
114     bool called[2];
115     called[0] = false;
116     called[1] = false;
117     mgr.Register<MessageA>([&](const MessageA& msg){
118         BOOST_REQUIRE_MESSAGE(msg.i == 5, "Unexpected message received i=" << msg.i);
119         called[0] = true;
120     });
121     mgr.Register<MessageA>([&](const MessageA& msg){
122         BOOST_REQUIRE_MESSAGE(msg.i == 5, "Unexpected message received i=" << msg.i);
123         called[1] = true;
124     });
125
126     BOOST_REQUIRE_MESSAGE(2 == mgr.SendMessage(MessageA(5)), "No listener found");
127     BOOST_REQUIRE_MESSAGE(called[0], "First listener not called");
128     BOOST_REQUIRE_MESSAGE(called[1], "Second listener not called");
129 }
130
131 BOOST_AUTO_TEST_CASE(TMM_0060_Stress) {
132     CKM::CommunicationManager<MessageA, MessageB, MessageC> mgr;
133
134     std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
135     std::uniform_int_distribution<size_t> message_dist(0,2);
136     std::uniform_int_distribution<size_t> count_dist(1,10);
137
138     size_t a = 0;
139     size_t b = 0;
140     size_t c = 0;
141     mgr.Register<MessageA>([&](const MessageA& msg) {
142         BOOST_REQUIRE_MESSAGE(msg.i == 42, "Wrong message: " << msg.i);
143         a++;
144     });
145     mgr.Register<MessageB>([&](const MessageB& msg) {
146         BOOST_REQUIRE_MESSAGE(msg.c == 'c', "Wrong message: " << msg.c);
147         b++;
148     });
149     mgr.Register<MessageC>([&](const MessageC& msg) {
150         BOOST_REQUIRE_MESSAGE(msg.str == "lorem ipsum", "Wrong message: " << msg.str);
151         c++;
152     });
153
154     for (size_t i=0; i < 1000; i++)
155     {
156         size_t cnt = count_dist(generator);
157         for (size_t s = 0; s < cnt; s++) {
158             switch(message_dist(generator))
159             {
160             case 0:
161                 BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageA(42)), "No listener found");
162                 a--;
163                 break;
164             case 1:
165                 BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageB('c')), "No listener found");
166                 b--;
167                 break;
168             case 2:
169                 BOOST_REQUIRE_MESSAGE(1 == mgr.SendMessage(MessageC("lorem ipsum")), "No listener found");
170                 c--;
171                 break;
172             default:
173                 BOOST_FAIL("Unexpected message type");
174             }
175         }
176     }
177     BOOST_REQUIRE_MESSAGE(a == 0, "Unexpected number of MessageA: " << a);
178     BOOST_REQUIRE_MESSAGE(b == 0, "Unexpected number of MessageB: " << b);
179     BOOST_REQUIRE_MESSAGE(c == 0, "Unexpected number of MessageC: " << c);
180 }
181
182 BOOST_AUTO_TEST_SUITE_END()
183
184