1 | // |
2 | // DatagramSocketTest.cpp |
3 | // |
4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "DatagramSocketTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "UDPEchoServer.h" |
15 | #include "Poco/Net/DatagramSocket.h" |
16 | #include "Poco/Net/SocketAddress.h" |
17 | #include "Poco/Net/NetworkInterface.h" |
18 | #include "Poco/Net/NetException.h" |
19 | #include "Poco/Timespan.h" |
20 | #include "Poco/Buffer.h" |
21 | #include "Poco/Stopwatch.h" |
22 | #include <cstring> |
23 | |
24 | |
25 | using Poco::Net::Socket; |
26 | using Poco::Net::DatagramSocket; |
27 | using Poco::Net::SocketAddress; |
28 | using Poco::Net::IPAddress; |
29 | #ifdef POCO_NET_HAS_INTERFACE |
30 | using Poco::Net::NetworkInterface; |
31 | #endif |
32 | using Poco::Timespan; |
33 | using Poco::Buffer; |
34 | using Poco::Stopwatch; |
35 | using Poco::TimeoutException; |
36 | using Poco::InvalidArgumentException; |
37 | using Poco::IOException; |
38 | |
39 | |
40 | DatagramSocketTest::DatagramSocketTest(const std::string& name): CppUnit::TestCase(name) |
41 | { |
42 | } |
43 | |
44 | |
45 | DatagramSocketTest::~DatagramSocketTest() |
46 | { |
47 | } |
48 | |
49 | |
50 | void DatagramSocketTest::testEcho() |
51 | { |
52 | UDPEchoServer echoServer; |
53 | DatagramSocket ss; |
54 | char buffer[256]; |
55 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
56 | int n = ss.sendBytes("hello" , 5); |
57 | assertTrue (n == 5); |
58 | n = ss.receiveBytes(buffer, sizeof(buffer)); |
59 | assertTrue (n == 5); |
60 | assertTrue (std::string(buffer, n) == "hello" ); |
61 | ss.close(); |
62 | } |
63 | |
64 | |
65 | void DatagramSocketTest::testEchoBuffer() |
66 | { |
67 | UDPEchoServer echoServer; |
68 | DatagramSocket ss; |
69 | Buffer<char> buffer(0); |
70 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
71 | int n = ss.receiveBytes(buffer); |
72 | assertTrue (n == 0); |
73 | assertTrue (buffer.size() == 0); |
74 | n = ss.sendBytes("hello" , 5); |
75 | assertTrue (n == 5); |
76 | n = ss.receiveBytes(buffer); |
77 | assertTrue (n == 5); |
78 | assertTrue (buffer.size() == 5); |
79 | assertTrue (std::string(buffer.begin(), n) == "hello" ); |
80 | ss.close(); |
81 | } |
82 | |
83 | |
84 | void DatagramSocketTest::testSendToReceiveFrom() |
85 | { |
86 | UDPEchoServer echoServer(SocketAddress("127.0.0.1" , 0)); |
87 | DatagramSocket ss(SocketAddress::IPv4); |
88 | int n = ss.sendTo("hello" , 5, SocketAddress("127.0.0.1" , echoServer.port())); |
89 | assertTrue (n == 5); |
90 | char buffer[256]; |
91 | SocketAddress sa; |
92 | n = ss.receiveFrom(buffer, sizeof(buffer), sa); |
93 | assertTrue (sa.host() == echoServer.address().host()); |
94 | assertTrue (sa.port() == echoServer.port()); |
95 | assertTrue (n == 5); |
96 | assertTrue (std::string(buffer, n) == "hello" ); |
97 | ss.close(); |
98 | } |
99 | |
100 | |
101 | void DatagramSocketTest::testUnbound() |
102 | { |
103 | UDPEchoServer echoServer; |
104 | DatagramSocket ss; |
105 | char buffer[256]; |
106 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
107 | int n = ss.sendBytes("hello" , 5); |
108 | assertTrue (n == 5); |
109 | n = ss.receiveBytes(buffer, sizeof(buffer)); |
110 | assertTrue (n == 5); |
111 | assertTrue (std::string(buffer, n) == "hello" ); |
112 | ss.close(); |
113 | } |
114 | |
115 | |
116 | void DatagramSocketTest::testBroadcast() |
117 | { |
118 | UDPEchoServer echoServer; |
119 | DatagramSocket ss(IPAddress::IPv4); |
120 | |
121 | #if defined(POCO_NET_HAS_INTERFACE) && (POCO_OS == POCO_OS_FREE_BSD) |
122 | NetworkInterface ni = NetworkInterface::forName("em0" ); |
123 | SocketAddress sa(ni.broadcastAddress(1), echoServer.port()); |
124 | #else |
125 | SocketAddress sa("255.255.255.255" , echoServer.port()); |
126 | #endif |
127 | // not all socket implementations fail if broadcast option is not set |
128 | /* |
129 | try |
130 | { |
131 | int n = ss.sendTo("hello", 5, sa); |
132 | fail ("broadcast option not set - must throw"); |
133 | } |
134 | catch (IOException&) |
135 | { |
136 | } |
137 | */ |
138 | ss.setBroadcast(true); |
139 | |
140 | #if (POCO_OS == POCO_OS_FREE_BSD) |
141 | int opt = 1; |
142 | poco_socklen_t len = sizeof(opt); |
143 | ss.impl()->setRawOption(IPPROTO_IP, IP_ONESBCAST, (const char*) &opt, len); |
144 | ss.impl()->getRawOption(IPPROTO_IP, IP_ONESBCAST, &opt, len); |
145 | assertTrue (opt == 1); |
146 | #endif |
147 | |
148 | int n = ss.sendTo("hello" , 5, sa); |
149 | assertTrue (n == 5); |
150 | char buffer[256] = { 0 }; |
151 | n = ss.receiveBytes(buffer, 5); |
152 | assertTrue (n == 5); |
153 | assertTrue (std::string(buffer, n) == "hello" ); |
154 | ss.close(); |
155 | } |
156 | |
157 | |
158 | void DatagramSocketTest::testGatherScatterFixed() |
159 | { |
160 | #if defined(POCO_OS_FAMILY_WINDOWS) |
161 | testGatherScatterFixedWin(); |
162 | testGatherScatterSTRFFixedWin(); |
163 | #elif defined(POCO_OS_FAMILY_UNIX) |
164 | testGatherScatterFixedUNIX(); |
165 | testGatherScatterSTRFFixedUNIX(); |
166 | #endif |
167 | } |
168 | |
169 | |
170 | void DatagramSocketTest::testGatherScatterFixedWin() |
171 | { |
172 | #if defined(POCO_OS_FAMILY_WINDOWS) |
173 | UDPEchoServer echoServer; |
174 | DatagramSocket ss; |
175 | Socket::BufVec sbv = Socket::makeBufVec(3, 10); |
176 | assertTrue (sbv.size() == 3); |
177 | |
178 | std::memcpy(sbv[0].buf, "1234567890" , 10); |
179 | std::memcpy(sbv[1].buf, "abcdefghij" , 10); |
180 | std::memcpy(sbv[2].buf, "helloworld" , 10); |
181 | |
182 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
183 | int n = ss.sendBytes(sbv); |
184 | assertTrue (n == 30); |
185 | |
186 | std::memset(sbv[0].buf, 0, 10); |
187 | std::memset(sbv[1].buf, 0, 10); |
188 | std::memset(sbv[2].buf, 0, 10); |
189 | |
190 | char empty[10] = {}; |
191 | assertTrue (0 == std::memcmp(sbv[0].buf, empty, 10)); |
192 | assertTrue (0 == std::memcmp(sbv[1].buf, empty, 10)); |
193 | assertTrue (0 == std::memcmp(sbv[2].buf, empty, 10)); |
194 | |
195 | n = ss.receiveBytes(sbv); |
196 | assertTrue (n == 30); |
197 | |
198 | assertTrue (0 == std::memcmp(sbv[0].buf, "1234567890" , 10)); |
199 | assertTrue (0 == std::memcmp(sbv[1].buf, "abcdefghij" , 10)); |
200 | assertTrue (0 == std::memcmp(sbv[2].buf, "helloworld" , 10)); |
201 | |
202 | Socket::destroyBufVec(sbv); |
203 | |
204 | ss.close(); |
205 | #endif |
206 | } |
207 | |
208 | |
209 | void DatagramSocketTest::testGatherScatterSTRFFixedWin() |
210 | { |
211 | #if defined(POCO_OS_FAMILY_WINDOWS) |
212 | UDPEchoServer echoServer(SocketAddress("127.0.0.1" , 0)); |
213 | DatagramSocket ss; |
214 | Socket::BufVec sbv = Socket::makeBufVec(3, 10); |
215 | assertTrue (sbv.size() == 3); |
216 | |
217 | std::memcpy(sbv[0].buf, "1234567890" , 10); |
218 | std::memcpy(sbv[1].buf, "abcdefghij" , 10); |
219 | std::memcpy(sbv[2].buf, "helloworld" , 10); |
220 | |
221 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
222 | int n = ss.sendTo(sbv, SocketAddress("127.0.0.1" , echoServer.port())); |
223 | assertTrue (n == 30); |
224 | |
225 | std::memset(sbv[0].buf, 0, 10); |
226 | std::memset(sbv[1].buf, 0, 10); |
227 | std::memset(sbv[2].buf, 0, 10); |
228 | |
229 | char empty[10] = {}; |
230 | assertTrue (0 == std::memcmp(sbv[0].buf, empty, 10)); |
231 | assertTrue (0 == std::memcmp(sbv[1].buf, empty, 10)); |
232 | assertTrue (0 == std::memcmp(sbv[2].buf, empty, 10)); |
233 | |
234 | SocketAddress sa; |
235 | n = ss.receiveFrom(sbv, sa); |
236 | assertTrue (sa.host() == echoServer.address().host()); |
237 | assertTrue (sa.port() == echoServer.port()); |
238 | assertTrue (n == 30); |
239 | |
240 | assertTrue (0 == std::memcmp(sbv[0].buf, "1234567890" , 10)); |
241 | assertTrue (0 == std::memcmp(sbv[1].buf, "abcdefghij" , 10)); |
242 | assertTrue (0 == std::memcmp(sbv[2].buf, "helloworld" , 10)); |
243 | |
244 | Socket::destroyBufVec(sbv); |
245 | |
246 | ss.close(); |
247 | #endif |
248 | } |
249 | |
250 | |
251 | void DatagramSocketTest::testGatherScatterFixedUNIX() |
252 | { |
253 | #if defined(POCO_OS_FAMILY_UNIX) |
254 | UDPEchoServer echoServer; |
255 | DatagramSocket ss; |
256 | Socket::BufVec sbv = Socket::makeBufVec(3, 10); |
257 | assertTrue (sbv.size() == 3); |
258 | |
259 | std::memcpy(sbv[0].iov_base, "1234567890" , 10); |
260 | std::memcpy(sbv[1].iov_base, "abcdefghij" , 10); |
261 | std::memcpy(sbv[2].iov_base, "helloworld" , 10); |
262 | |
263 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
264 | int n = ss.sendBytes(sbv); |
265 | assertTrue (n == 30); |
266 | |
267 | std::memset(sbv[0].iov_base, 0, 10); |
268 | std::memset(sbv[1].iov_base, 0, 10); |
269 | std::memset(sbv[2].iov_base, 0, 10); |
270 | |
271 | char empty[10] = {}; |
272 | assertTrue (0 == std::memcmp(sbv[0].iov_base, empty, 10)); |
273 | assertTrue (0 == std::memcmp(sbv[1].iov_base, empty, 10)); |
274 | assertTrue (0 == std::memcmp(sbv[2].iov_base, empty, 10)); |
275 | |
276 | n = ss.receiveBytes(sbv); |
277 | assertTrue (n == 30); |
278 | |
279 | assertTrue (0 == std::memcmp(sbv[0].iov_base, "1234567890" , 10)); |
280 | assertTrue (0 == std::memcmp(sbv[1].iov_base, "abcdefghij" , 10)); |
281 | assertTrue (0 == std::memcmp(sbv[2].iov_base, "helloworld" , 10)); |
282 | |
283 | Socket::destroyBufVec(sbv); |
284 | |
285 | ss.close(); |
286 | #endif |
287 | } |
288 | |
289 | |
290 | void DatagramSocketTest::testGatherScatterSTRFFixedUNIX() |
291 | { |
292 | #if defined(POCO_OS_FAMILY_UNIX) |
293 | UDPEchoServer echoServer(SocketAddress("127.0.0.1" , 0)); |
294 | DatagramSocket ss; |
295 | Socket::BufVec sbv = Socket::makeBufVec(3, 10); |
296 | assertTrue (sbv.size() == 3); |
297 | |
298 | std::memcpy(sbv[0].iov_base, "1234567890" , 10); |
299 | std::memcpy(sbv[1].iov_base, "abcdefghij" , 10); |
300 | std::memcpy(sbv[2].iov_base, "helloworld" , 10); |
301 | |
302 | int n = ss.sendTo(sbv, SocketAddress("127.0.0.1" , echoServer.port())); |
303 | assertTrue (n == 30); |
304 | |
305 | std::memset(sbv[0].iov_base, 0, 10); |
306 | std::memset(sbv[1].iov_base, 0, 10); |
307 | std::memset(sbv[2].iov_base, 0, 10); |
308 | |
309 | char empty[10] = {}; |
310 | assertTrue (0 == std::memcmp(sbv[0].iov_base, empty, 10)); |
311 | assertTrue (0 == std::memcmp(sbv[1].iov_base, empty, 10)); |
312 | assertTrue (0 == std::memcmp(sbv[2].iov_base, empty, 10)); |
313 | |
314 | SocketAddress sa; |
315 | n = ss.receiveFrom(sbv, sa); |
316 | assertTrue (sa.host() == echoServer.address().host()); |
317 | assertTrue (sa.port() == echoServer.port()); |
318 | assertTrue (n == 30); |
319 | |
320 | assertTrue (0 == std::memcmp(sbv[0].iov_base, "1234567890" , 10)); |
321 | assertTrue (0 == std::memcmp(sbv[1].iov_base, "abcdefghij" , 10)); |
322 | assertTrue (0 == std::memcmp(sbv[2].iov_base, "helloworld" , 10)); |
323 | |
324 | Socket::destroyBufVec(sbv); |
325 | |
326 | ss.close(); |
327 | #endif |
328 | } |
329 | |
330 | |
331 | void DatagramSocketTest::testGatherScatterVariable() |
332 | { |
333 | #if defined(POCO_OS_FAMILY_WINDOWS) |
334 | testGatherScatterVariableWin(); |
335 | testGatherScatterSTRFVariableWin(); |
336 | #elif defined(POCO_OS_FAMILY_UNIX) |
337 | testGatherScatterVariableUNIX(); |
338 | testGatherScatterSTRFVariableUNIX(); |
339 | #endif |
340 | } |
341 | |
342 | |
343 | void DatagramSocketTest::testGatherScatterVariableWin() |
344 | { |
345 | #if defined(POCO_OS_FAMILY_WINDOWS) |
346 | UDPEchoServer echoServer(SocketAddress("127.0.0.1" , 0)); |
347 | DatagramSocket ss; |
348 | std::vector<std::string> strOut(3); |
349 | strOut[0] = "123" ; |
350 | strOut[1] = "abcdef" ; |
351 | strOut[2] = "helloworld" ; |
352 | Socket::BufVec sbvOut = Socket::makeBufVec(strOut); |
353 | assertTrue (sbvOut.size() == 3); |
354 | assertTrue (sbvOut[0].len == 3); |
355 | assertTrue (sbvOut[1].len == 6); |
356 | assertTrue (sbvOut[2].len == 10); |
357 | assertTrue (0 == std::memcmp(sbvOut[0].buf, "123" , 3)); |
358 | assertTrue (0 == std::memcmp(sbvOut[1].buf, "abcdef" , 6)); |
359 | assertTrue (0 == std::memcmp(sbvOut[2].buf, "helloworld" , 10)); |
360 | |
361 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
362 | int n = ss.sendTo(sbvOut, SocketAddress("127.0.0.1" , echoServer.port())); |
363 | assertTrue (n == 19); |
364 | |
365 | std::vector<char*> strIn(3); |
366 | strIn[0] = (char*) calloc(4, 1); |
367 | strIn[1] = (char*) calloc(7, 1); |
368 | strIn[2] = (char*) calloc(11, 1); |
369 | std::memcpy(strIn[0], "321" , 3); |
370 | std::memcpy(strIn[1], "fedcba" , 6); |
371 | std::memcpy(strIn[2], "dlrowolleh" , 10); |
372 | Socket::BufVec sbvIn = Socket::makeBufVec(strIn); |
373 | assertTrue (sbvIn.size() == 3); |
374 | assertTrue (sbvIn[0].len == 3); |
375 | assertTrue (sbvIn[1].len == 6); |
376 | assertTrue (sbvIn[2].len == 10); |
377 | assertTrue (0 == std::memcmp(sbvIn[0].buf, "321" , 3)); |
378 | assertTrue (0 == std::memcmp(sbvIn[1].buf, "fedcba" , 6)); |
379 | assertTrue (0 == std::memcmp(sbvIn[2].buf, "dlrowolleh" , 10)); |
380 | |
381 | SocketAddress sa; |
382 | n = ss.receiveFrom(sbvIn, sa); |
383 | assertTrue (sa.host() == echoServer.address().host()); |
384 | assertTrue (sa.port() == echoServer.port()); |
385 | assertTrue (n == 19); |
386 | |
387 | assertTrue (0 == std::memcmp(sbvIn[0].buf, "123" , 3)); |
388 | assertTrue (0 == std::memcmp(sbvIn[1].buf, "abcdef" , 6)); |
389 | assertTrue (0 == std::memcmp(sbvIn[2].buf, "helloworld" , 10)); |
390 | |
391 | n = ss.sendBytes(sbvOut); |
392 | assertTrue (n == 19); |
393 | |
394 | std::reverse(sbvIn.begin(), sbvIn.end()); |
395 | n = ss.receiveBytes(sbvIn); |
396 | assertTrue (n == 19); |
397 | |
398 | assertTrue (0 == std::memcmp(sbvIn[0].buf, "123abcdefh" , 10)); |
399 | assertTrue (0 == std::memcmp(sbvIn[1].buf, "ellowo" , 6)); |
400 | assertTrue (0 == std::memcmp(sbvIn[2].buf, "rld" , 3)); |
401 | |
402 | free(strIn[0]); |
403 | free(strIn[1]); |
404 | free(strIn[2]); |
405 | |
406 | ss.close(); |
407 | #endif |
408 | } |
409 | |
410 | |
411 | void DatagramSocketTest::testGatherScatterSTRFVariableWin() |
412 | { |
413 | #if defined(POCO_OS_FAMILY_WINDOWS) |
414 | UDPEchoServer echoServer; |
415 | DatagramSocket ss; |
416 | std::vector<std::string> strOut(3); |
417 | strOut[0] = "123" ; |
418 | strOut[1] = "abcdef" ; |
419 | strOut[2] = "helloworld" ; |
420 | Socket::BufVec sbvOut = Socket::makeBufVec(strOut); |
421 | assertTrue (sbvOut.size() == 3); |
422 | assertTrue (sbvOut[0].len == 3); |
423 | assertTrue (sbvOut[1].len == 6); |
424 | assertTrue (sbvOut[2].len == 10); |
425 | assertTrue (0 == std::memcmp(sbvOut[0].buf, "123" , 3)); |
426 | assertTrue (0 == std::memcmp(sbvOut[1].buf, "abcdef" , 6)); |
427 | assertTrue (0 == std::memcmp(sbvOut[2].buf, "helloworld" , 10)); |
428 | |
429 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
430 | int n = ss.sendBytes(sbvOut); |
431 | assertTrue (n == 19); |
432 | |
433 | std::vector<char*> strIn(3); |
434 | strIn[0] = new char[4]; |
435 | strIn[1] = new char[7]; |
436 | strIn[2] = new char[11]; |
437 | std::memcpy(strIn[0], "321" , 3); strIn[0][3] = '\0'; |
438 | std::memcpy(strIn[1], "fedcba" , 6); strIn[1][6] = '\0'; |
439 | std::memcpy(strIn[2], "dlrowolleh" , 10); strIn[2][10] = '\0'; |
440 | Socket::BufVec sbvIn = Socket::makeBufVec(strIn); |
441 | assertTrue (sbvIn.size() == 3); |
442 | assertTrue (sbvIn[0].len == 3); |
443 | assertTrue (sbvIn[1].len == 6); |
444 | assertTrue (sbvIn[2].len == 10); |
445 | assertTrue (0 == std::memcmp(sbvIn[0].buf, "321" , 3)); |
446 | assertTrue (0 == std::memcmp(sbvIn[1].buf, "fedcba" , 6)); |
447 | assertTrue (0 == std::memcmp(sbvIn[2].buf, "dlrowolleh" , 10)); |
448 | |
449 | n = ss.receiveBytes(sbvIn); |
450 | assertTrue (n == 19); |
451 | |
452 | assertTrue (0 == std::memcmp(sbvIn[0].buf, "123" , 3)); |
453 | assertTrue (0 == std::memcmp(sbvIn[1].buf, "abcdef" , 6)); |
454 | assertTrue (0 == std::memcmp(sbvIn[2].buf, "helloworld" , 10)); |
455 | |
456 | n = ss.sendBytes(sbvOut); |
457 | assertTrue (n == 19); |
458 | |
459 | std::reverse(sbvIn.begin(), sbvIn.end()); |
460 | n = ss.receiveBytes(sbvIn); |
461 | assertTrue (n == 19); |
462 | |
463 | assertTrue (0 == std::memcmp(sbvIn[0].buf, "123abcdefh" , 10)); |
464 | assertTrue (0 == std::memcmp(sbvIn[1].buf, "ellowo" , 6)); |
465 | assertTrue (0 == std::memcmp(sbvIn[2].buf, "rld" , 3)); |
466 | |
467 | delete [] strIn[0]; |
468 | delete [] strIn[1]; |
469 | delete [] strIn[2]; |
470 | |
471 | ss.close(); |
472 | #endif |
473 | } |
474 | |
475 | |
476 | void DatagramSocketTest::testGatherScatterSTRFVariableUNIX() |
477 | { |
478 | #if defined(POCO_OS_FAMILY_UNIX) |
479 | UDPEchoServer echoServer; |
480 | DatagramSocket ss; |
481 | std::vector<std::string> strOut(3); |
482 | strOut[0] = "123" ; |
483 | strOut[1] = "abcdef" ; |
484 | strOut[2] = "helloworld" ; |
485 | Socket::BufVec sbvOut = Socket::makeBufVec(strOut); |
486 | assertTrue (sbvOut.size() == 3); |
487 | assertTrue (sbvOut[0].iov_len == 3); |
488 | assertTrue (sbvOut[1].iov_len == 6); |
489 | assertTrue (sbvOut[2].iov_len == 10); |
490 | assertTrue (0 == std::memcmp(sbvOut[0].iov_base, "123" , 3)); |
491 | assertTrue (0 == std::memcmp(sbvOut[1].iov_base, "abcdef" , 6)); |
492 | assertTrue (0 == std::memcmp(sbvOut[2].iov_base, "helloworld" , 10)); |
493 | |
494 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
495 | int n = ss.sendBytes(sbvOut); |
496 | assertTrue (n == 19); |
497 | |
498 | std::vector<char*> strIn(3); |
499 | strIn[0] = new char[4]; |
500 | strIn[1] = new char[7]; |
501 | strIn[2] = new char[11]; |
502 | std::memcpy(strIn[0], "321" , 3); strIn[0][3] = '\0'; |
503 | std::memcpy(strIn[1], "fedcba" , 6); strIn[1][6] = '\0'; |
504 | std::memcpy(strIn[2], "dlrowolleh" , 10); strIn[2][10] = '\0'; |
505 | Socket::BufVec sbvIn = Socket::makeBufVec(strIn); |
506 | assertTrue (sbvIn.size() == 3); |
507 | assertTrue (sbvIn[0].iov_len == 3); |
508 | assertTrue (sbvIn[1].iov_len == 6); |
509 | assertTrue (sbvIn[2].iov_len == 10); |
510 | assertTrue (0 == std::memcmp(sbvIn[0].iov_base, "321" , 3)); |
511 | assertTrue (0 == std::memcmp(sbvIn[1].iov_base, "fedcba" , 6)); |
512 | assertTrue (0 == std::memcmp(sbvIn[2].iov_base, "dlrowolleh" , 10)); |
513 | |
514 | n = ss.receiveBytes(sbvIn); |
515 | assertTrue (n == 19); |
516 | |
517 | assertTrue (0 == std::memcmp(sbvIn[0].iov_base, "123" , 3)); |
518 | assertTrue (0 == std::memcmp(sbvIn[1].iov_base, "abcdef" , 6)); |
519 | assertTrue (0 == std::memcmp(sbvIn[2].iov_base, "helloworld" , 10)); |
520 | |
521 | n = ss.sendBytes(sbvOut); |
522 | assertTrue (n == 19); |
523 | |
524 | std::reverse(sbvIn.begin(), sbvIn.end()); |
525 | n = ss.receiveBytes(sbvIn); |
526 | assertTrue (n == 19); |
527 | |
528 | assertTrue (0 == std::memcmp(sbvIn[0].iov_base, "123abcdefh" , 10)); |
529 | assertTrue (0 == std::memcmp(sbvIn[1].iov_base, "ellowo" , 6)); |
530 | assertTrue (0 == std::memcmp(sbvIn[2].iov_base, "rld" , 3)); |
531 | |
532 | delete [] strIn[0]; |
533 | delete [] strIn[1]; |
534 | delete [] strIn[2]; |
535 | |
536 | ss.close(); |
537 | #endif |
538 | } |
539 | |
540 | |
541 | void DatagramSocketTest::testGatherScatterVariableUNIX() |
542 | { |
543 | #if defined(POCO_OS_FAMILY_UNIX) |
544 | UDPEchoServer echoServer; |
545 | DatagramSocket ss; |
546 | std::vector<std::string> strOut(3); |
547 | strOut[0] = "123" ; |
548 | strOut[1] = "abcdef" ; |
549 | strOut[2] = "helloworld" ; |
550 | Socket::BufVec sbvOut = Socket::makeBufVec(strOut); |
551 | assertTrue (sbvOut.size() == 3); |
552 | assertTrue (sbvOut[0].iov_len == 3); |
553 | assertTrue (sbvOut[1].iov_len == 6); |
554 | assertTrue (sbvOut[2].iov_len == 10); |
555 | assertTrue (0 == std::memcmp(sbvOut[0].iov_base, "123" , 3)); |
556 | assertTrue (0 == std::memcmp(sbvOut[1].iov_base, "abcdef" , 6)); |
557 | assertTrue (0 == std::memcmp(sbvOut[2].iov_base, "helloworld" , 10)); |
558 | |
559 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
560 | int n = ss.sendBytes(sbvOut); |
561 | assertTrue (n == 19); |
562 | |
563 | std::vector<char*> strIn(3); |
564 | strIn[0] = new char[4]; |
565 | strIn[1] = new char[7]; |
566 | strIn[2] = new char[11]; |
567 | std::memcpy(strIn[0], "321" , 3); strIn[0][3] = '\0'; |
568 | std::memcpy(strIn[1], "fedcba" , 6); strIn[1][6] = '\0'; |
569 | std::memcpy(strIn[2], "dlrowolleh" , 10); strIn[2][10] = '\0'; |
570 | Socket::BufVec sbvIn = Socket::makeBufVec(strIn); |
571 | assertTrue (sbvIn.size() == 3); |
572 | assertTrue (sbvIn[0].iov_len == 3); |
573 | assertTrue (sbvIn[1].iov_len == 6); |
574 | assertTrue (sbvIn[2].iov_len == 10); |
575 | assertTrue (0 == std::memcmp(sbvIn[0].iov_base, "321" , 3)); |
576 | assertTrue (0 == std::memcmp(sbvIn[1].iov_base, "fedcba" , 6)); |
577 | assertTrue (0 == std::memcmp(sbvIn[2].iov_base, "dlrowolleh" , 10)); |
578 | |
579 | n = ss.receiveBytes(sbvIn); |
580 | assertTrue (n == 19); |
581 | |
582 | assertTrue (0 == std::memcmp(sbvIn[0].iov_base, "123" , 3)); |
583 | assertTrue (0 == std::memcmp(sbvIn[1].iov_base, "abcdef" , 6)); |
584 | assertTrue (0 == std::memcmp(sbvIn[2].iov_base, "helloworld" , 10)); |
585 | |
586 | delete [] strIn[0]; |
587 | delete [] strIn[1]; |
588 | delete [] strIn[2]; |
589 | |
590 | ss.close(); |
591 | #endif |
592 | } |
593 | |
594 | |
595 | void DatagramSocketTest::setUp() |
596 | { |
597 | } |
598 | |
599 | |
600 | void DatagramSocketTest::tearDown() |
601 | { |
602 | } |
603 | |
604 | |
605 | CppUnit::Test* DatagramSocketTest::suite() |
606 | { |
607 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DatagramSocketTest" ); |
608 | |
609 | CppUnit_addTest(pSuite, DatagramSocketTest, testEcho); |
610 | CppUnit_addTest(pSuite, DatagramSocketTest, testEchoBuffer); |
611 | CppUnit_addTest(pSuite, DatagramSocketTest, testSendToReceiveFrom); |
612 | CppUnit_addTest(pSuite, DatagramSocketTest, testUnbound); |
613 | #if (POCO_OS != POCO_OS_FREE_BSD) // works only with local net bcast and very randomly |
614 | CppUnit_addTest(pSuite, DatagramSocketTest, testBroadcast); |
615 | #endif |
616 | CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterFixed); |
617 | CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterVariable); |
618 | |
619 | return pSuite; |
620 | } |
621 | |