Branch data Line data Source code
1 : : /* Copyright 2006,2007,2011 Savarese Software Research Corporation
2 : : *
3 : : * Licensed under the Apache License, Version 2.0 (the "License");
4 : : * you may not use this file except in compliance with the License.
5 : : * You may obtain a copy of the License at
6 : : *
7 : : * http://www.savarese.com/software/ApacheLicense-2.0
8 : : *
9 : : * Unless required by applicable law or agreed to in writing, software
10 : : * distributed under the License is distributed on an "AS IS" BASIS,
11 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : * See the License for the specific language governing permissions and
13 : : * limitations under the License.
14 : : */
15 : :
16 : : #include <ssrc/spread.h>
17 : :
18 : : #include <utility>
19 : : #include <cstdlib>
20 : :
21 : : #define BOOST_TEST_MODULE MailboxTest
22 : : #include <boost/test/unit_test.hpp>
23 : :
24 : : using namespace NS_SSRC_SPREAD;
25 : : using namespace std::rel_ops;
26 : :
27 : : const char *TestGroupName = "MailboxTestGroup";
28 : : const char *TestGroupName2 = "MailboxTestGroup2";
29 : :
30 : : class MailboxFixture {
31 : : public:
32 : : Mailbox *mbox;
33 : : Mailbox *rmbox;
34 : : Message *message;
35 : :
36 : 9 : MailboxFixture() {
37 : : try {
38 [ + - ][ + - ]: 9 : mbox = new Mailbox(LIBSSRCSPREAD_TEST_DAEMON);
[ + - ][ + - ]
[ + - ][ + - ]
39 [ + - ][ + - ]: 9 : rmbox = new Mailbox(LIBSSRCSPREAD_TEST_DAEMON, "", false);
[ + - ][ + - ]
[ + - ][ + - ]
40 [ # # ]: 0 : } catch(const Error & e) {
41 [ # # ]: 0 : e.print();
42 : 0 : throw e;
43 : : }
44 [ + - ]: 9 : message = new Message();
45 : 9 : }
46 : :
47 : 9 : ~MailboxFixture() {
48 [ + - ]: 9 : delete mbox;
49 [ + - ]: 9 : delete rmbox;
50 [ + - ]: 9 : delete message;
51 : 9 : mbox = 0;
52 : 9 : message = 0;
53 : 9 : }
54 : :
55 : 6 : void join() {
56 [ + - ][ + - ]: 6 : mbox->join(TestGroupName);
[ + - ]
57 [ + - ][ + - ]: 6 : mbox->join(TestGroupName2);
[ + - ]
58 : 6 : }
59 : :
60 : 6 : void leave() {
61 [ + - ][ + - ]: 6 : mbox->leave(TestGroupName);
[ + - ]
62 [ + - ][ + - ]: 6 : mbox->leave(TestGroupName2);
[ + - ]
63 : 6 : }
64 : : };
65 : :
66 : 1 : BOOST_FIXTURE_TEST_SUITE(all, MailboxFixture)
67 : :
68 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_join_and_leave) {
[ + - ]
69 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(this->join());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
70 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(this->leave());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
71 : 1 : }
72 : :
73 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_send) {
[ + - ]
74 : 1 : this->join();
75 : 1 : message->write("test_send", 9);
76 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, TestGroupName));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
77 : 1 : message->clear();
78 : 1 : message->write("test_send2", 10);
79 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, TestGroupName));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
80 : 1 : this->leave();
81 : 1 : }
82 : :
83 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_scatter_send) {
[ + - ]
84 : 2 : ScatterMessage sm;
85 [ + - ][ + - ]: 2 : Message m;
86 : :
87 [ + - ]: 1 : this->join();
88 [ + - ]: 1 : message->write("test_scatter_send", 17);
89 [ + - ]: 1 : m.write("test_scatter_send2", 18);
90 [ + - ][ + - ]: 1 : BOOST_REQUIRE(sm.add(*message));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
91 [ + - ][ + - ]: 1 : BOOST_REQUIRE(sm.add(m));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
92 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(sm, TestGroupName));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
93 : :
94 [ + - ]: 1 : mbox->clear_groups();
95 [ + - ][ + - ]: 1 : mbox->add_group(TestGroupName);
[ + - ]
96 [ + - ]: 1 : mbox->clear_message_parts();
97 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox->add_message_part("test_scatter_send3", 18));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
98 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox->add_message_part("test_scatter_send4", 18));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
99 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
100 [ + - ]: 1 : this->leave();
101 : 1 : }
102 : :
103 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_group_send) {
[ + - ]
104 : 2 : GroupList groups;
105 : :
106 [ + - ][ + - ]: 1 : groups.add(TestGroupName);
[ + - ]
107 [ + - ][ + - ]: 1 : groups.add(TestGroupName2);
[ + - ]
108 : :
109 [ + - ]: 1 : this->join();
110 [ + - ]: 1 : message->write("test_group_send", 15);
111 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, groups));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
112 [ + - ]: 1 : message->clear();
113 [ + - ]: 1 : message->write("test_group_send2", 16);
114 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(*message, groups));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
115 [ + - ]: 1 : this->leave();
116 : 1 : }
117 : :
118 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_scatter_group_send) {
[ + - ]
119 : 2 : GroupList groups;
120 [ + - ][ + - ]: 2 : ScatterMessage sm;
121 [ + - ][ + - ]: 2 : Message m;
122 : :
123 [ + - ][ + - ]: 1 : groups.add(TestGroupName);
[ + - ]
124 [ + - ][ + - ]: 1 : groups.add(TestGroupName2);
[ + - ]
125 : :
126 [ + - ]: 1 : this->join();
127 [ + - ]: 1 : message->write("test_scatter_group_send", 23);
128 [ + - ]: 1 : m.write("test_scatter_group_send2", 24);
129 [ + - ]: 1 : sm.add(*message);
130 [ + - ]: 1 : sm.add(m);
131 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(sm, groups));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
132 [ + - ]: 1 : this->leave();
133 : 1 : }
134 : :
135 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_receive_message) {
[ + - ]
136 : 1 : const char *data = "foobar";
137 : 1 : unsigned int data_len = strlen(data) + 1;
138 : : unsigned int bytes;
139 : 2 : GroupList groups;
140 [ + - ][ + - ]: 2 : Message padding;
141 : :
142 [ + - ]: 1 : mbox->clear_groups();
143 [ + - ]: 1 : mbox->clear_message_parts();
144 [ + - ]: 1 : mbox->add_group(rmbox->private_group());
145 [ + - ]: 1 : mbox->add_message_part(data, data_len);
146 [ + - ]: 1 : mbox->send();
147 : :
148 [ + - ]: 1 : rmbox->clear_groups();
149 [ + - ]: 1 : rmbox->clear_message_parts();
150 [ + - ]: 1 : rmbox->add_message_part(*message);
151 [ + - ]: 1 : rmbox->add_message_part(padding);
152 : :
153 [ + - ]: 1 : padding.resize(padding.capacity());
154 [ + - ][ + - ]: 1 : BOOST_REQUIRE(padding.size() > 0);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
155 : :
156 [ + - ]: 1 : bytes = rmbox->receive();
157 [ + - ]: 1 : rmbox->copy_groups(groups);
158 : :
159 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(padding.size(), 0u);
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
160 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(bytes, data_len);
[ + - ][ + - ]
[ + - ][ - + ]
161 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(message->size(), data_len);
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
162 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(std::memcmp(data, &((*message)[0]), data_len), 0);
[ + - ][ + - ]
[ + - ][ - + ]
163 [ + - ][ + - ]: 1 : BOOST_REQUIRE(rmbox->private_group() == groups[0]);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
164 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox->private_group() == message->sender());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
165 : 1 : }
166 : :
167 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_receive_message_buffer_too_short) {
[ + - ]
168 : 1 : const unsigned int capacity = 131072;
169 : 2 : Message short_buffer(0);
170 : :
171 [ + - ]: 1 : message->resize(capacity);
172 : :
173 [ + + ]: 32769 : for(unsigned int i = 0; i < capacity; i+=sizeof(int)) {
174 : 32768 : const int j = std::rand();
175 [ + - ]: 32768 : message->write(&j, sizeof(j));
176 : : }
177 : :
178 [ + - ]: 1 : mbox->clear_message_parts();
179 [ + - ]: 1 : mbox->add_message_part(*message);
180 [ + - ][ + - ]: 1 : BOOST_REQUIRE_NO_THROW(mbox->send(rmbox->private_group()));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
181 : :
182 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(short_buffer.size(), 0u);
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
183 : :
184 [ + - ]: 1 : const unsigned int bytes = rmbox->receive(short_buffer);
185 : :
186 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(bytes, capacity);
[ + - ][ + - ]
[ + - ][ - + ]
187 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(short_buffer.size(), capacity);
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
188 [ + - ]: 3 : BOOST_REQUIRE_EQUAL(std::memcmp(&((*message)[0]), &short_buffer[0],
[ + - + - ]
[ - + ]
189 [ + - ][ + - ]: 2 : capacity), 0);
190 [ + - ][ + - ]: 1 : BOOST_REQUIRE(rmbox->private_group() == rmbox->group(0));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
191 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox->private_group() == short_buffer.sender());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
192 : 1 : }
193 : :
194 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_receive_membership_message) {
[ + - ]
195 : 1 : this->join();
196 : :
197 : : // mbox join message
198 : 2 : GroupList groups;
199 [ + - ]: 1 : mbox->receive(*message, groups);
200 : :
201 [ + - ][ + - ]: 1 : BOOST_REQUIRE(message->is_membership());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
202 [ + - ][ + - ]: 1 : BOOST_REQUIRE(TestGroupName == message->sender());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
203 : :
204 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
205 [ + - ][ + - ]: 2 : MembershipInfo info;
206 : :
207 [ + - ]: 1 : message->get_membership_info(info);
208 : :
209 [ + - ][ + - ]: 1 : BOOST_REQUIRE(info.is_regular_membership());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
210 [ + - ][ + - ]: 1 : BOOST_REQUIRE(info.caused_by_join());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
211 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox->private_group() == info.changed_member());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
212 : :
213 [ + - ][ + - ]: 2 : GroupList mbox_group, local_members, non_local_members, all_members;
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
214 : :
215 [ + - ]: 1 : mbox_group.add(mbox->private_group());
216 [ + - ]: 1 : info.get_local_members(local_members);
217 [ + - ]: 1 : info.get_non_local_members(non_local_members);
218 [ + - ]: 1 : info.get_all_members(all_members);
219 : :
220 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox_group == local_members);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
221 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox_group != non_local_members);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
222 [ + - ][ + - ]: 1 : BOOST_REQUIRE_EQUAL(non_local_members.size(), 0u);
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
223 [ + - ][ + - ]: 1 : BOOST_REQUIRE(mbox_group == all_members);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
224 [ + - ][ + - ]: 1 : BOOST_REQUIRE(groups == all_members);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
225 : : #endif
226 : :
227 : : // mbox join message
228 [ + - ]: 1 : mbox->receive(*message);
229 [ + - ][ + - ]: 1 : BOOST_REQUIRE(message->is_membership());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
230 [ + - ][ + - ]: 1 : BOOST_REQUIRE(TestGroupName2 == message->sender());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
231 : :
232 : : // rmbox join message
233 [ + - ][ + - ]: 1 : rmbox->join(TestGroupName2);
[ + - ]
234 : :
235 [ + - ]: 1 : mbox->receive(*message);
236 [ + - ][ + - ]: 1 : BOOST_REQUIRE(message->is_membership());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
237 [ + - ][ + - ]: 1 : BOOST_REQUIRE(TestGroupName2 == message->sender());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
238 : :
239 : : // rmbox leave message
240 [ + - ][ + - ]: 1 : rmbox->leave(TestGroupName2);
[ + - ]
241 : :
242 [ + - ]: 1 : mbox->receive(*message);
243 [ + - ][ + - ]: 1 : BOOST_REQUIRE(message->is_membership());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
244 : :
245 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
246 [ + - ]: 1 : message->get_membership_info(info);
247 : :
248 [ + - ][ + - ]: 1 : BOOST_REQUIRE(info.caused_by_leave());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
249 : : #endif
250 : :
251 [ + - ][ + - ]: 1 : BOOST_REQUIRE(TestGroupName2 == message->sender());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
252 : :
253 [ + - ]: 1 : this->leave();
254 : 1 : }
255 : :
256 [ + - ][ + - ]: 4 : BOOST_AUTO_TEST_CASE(test_self_leave_message) {
[ + - ]
257 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
258 [ + - ][ + - ]: 1 : mbox->join(TestGroupName);
[ + - ]
259 : 1 : mbox->receive(*message);
260 [ + - ][ + - ]: 1 : mbox->leave(TestGroupName);
[ + - ]
261 : 1 : mbox->receive(*message);
262 : :
263 : 2 : MembershipInfo info;
264 : :
265 [ + - ]: 1 : message->get_membership_info(info);
266 : :
267 [ + - ][ + - ]: 1 : BOOST_REQUIRE(info.caused_by_leave());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
268 [ + - ][ + - ]: 1 : BOOST_REQUIRE(info.is_self_leave());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
269 : : #endif
270 : 1 : }
271 : :
272 [ + - ][ + - ]: 3 : BOOST_AUTO_TEST_SUITE_END()
|