Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_comm-manager.cpp
1 /*
2  *  Copyright (c) 2015-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 <communication-manager.h>
23
24 #include <stdexcept>
25
26 #include <boost_macros_wrapper.h>
27
28 BOOST_AUTO_TEST_SUITE(COMMUNICATION_MANAGER_TEST)
29
30 POSITIVE_TEST_CASE(basic)
31 {
32         CKM::CommunicationManager<int, char> mgr;
33
34         BOOST_REQUIRE_MESSAGE(mgr.SendMessage(22) == 0, "There should be no listener.");
35
36         int reci = 0;
37         char recc = 0;
38         mgr.Register<int>([&](const int& msg) {
39                 reci = msg;
40         });
41         mgr.Register<char>([&](const char& msg) {
42                 recc = msg;
43         });
44
45         BOOST_REQUIRE_MESSAGE(mgr.SendMessage('c') == 1, "No listener found");
46         BOOST_REQUIRE_MESSAGE(reci == 0, "Unexpected message received");
47         BOOST_REQUIRE_MESSAGE(recc != 0, "Message not received");
48         BOOST_REQUIRE_MESSAGE(recc == 'c', "Wrong message received c=" << recc);
49
50         BOOST_REQUIRE_MESSAGE(mgr.SendMessage(42) == 1, "No listener found");
51         BOOST_REQUIRE_MESSAGE(reci != 0, "Message not received");
52         BOOST_REQUIRE_MESSAGE(reci == 42, "Wrong message received i=" << reci);
53         BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc);
54
55         int reci2 = 0;
56         mgr.Register<int>([&](const int& msg) {
57                 reci2 = msg;
58         });
59
60         BOOST_REQUIRE_MESSAGE(mgr.SendMessage(1234) == 2, "Some listeners not found");
61         BOOST_REQUIRE_MESSAGE(reci == 1234, "First receiver not called");
62         BOOST_REQUIRE_MESSAGE(reci2 == 1234, "Second receiver not called");
63         BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc);
64 }
65
66 NEGATIVE_TEST_CASE(throwing_listener)
67 {
68         CKM::CommunicationManager<char> mgr;
69
70         bool called[3] = {};
71         mgr.Register<char>([&](const char&) {
72                 called[0] = true;
73         });
74         mgr.Register<char>([&](const char&) {
75                 called[1] = true;
76                 throw std::runtime_error("Everything is awesome!");
77         });
78         mgr.Register<char>([&](const char&) {
79                 called[2] = true;
80         });
81
82         BOOST_REQUIRE_THROW(mgr.SendMessage('|'), std::runtime_error);
83         BOOST_REQUIRE_MESSAGE(called[0], "1st listener not called");
84         BOOST_REQUIRE_MESSAGE(called[1], "2nd listener not called");
85         BOOST_REQUIRE_MESSAGE(!called[2], "3rd listener called");
86 }
87
88 BOOST_AUTO_TEST_SUITE_END()
89
90