Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/sock.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjlib.h>
20 #include "test.h"
21
22
23 /**
24 * \page page_pjlib_sock_test Test: Socket
25 *
26 * This file provides implementation of \b sock_test(). It tests the
27 * various aspects of the socket API.
28 *
29 * \section sock_test_scope_sec Scope of the Test
30 *
31 * The scope of the test:
32 * - verify the validity of the address structs.
33 * - verify that address manipulation API works.
34 * - simple socket creation and destruction.
35 * - simple socket send/recv and sendto/recvfrom.
36 * - UDP connect()
37 * - send/recv big data.
38 * - all for both UDP and TCP.
39 *
40 * The APIs tested in this test:
41 * - pj_inet_aton()
42 * - pj_inet_ntoa()
43 * - pj_inet_pton() (only if IPv6 is enabled)
44 * - pj_inet_ntop() (only if IPv6 is enabled)
45 * - pj_gethostname()
46 * - pj_sock_socket()
47 * - pj_sock_close()
48 * - pj_sock_send()
49 * - pj_sock_sendto()
50 * - pj_sock_recv()
51 * - pj_sock_recvfrom()
52 * - pj_sock_bind()
53 * - pj_sock_connect()
54 * - pj_sock_listen()
55 * - pj_sock_accept()
56 * - pj_gethostbyname()
57 *
58 *
59 * This file is <b>pjlib-test/sock.c</b>
60 *
61 * \include pjlib-test/sock.c
62 */
63
64 #if INCLUDE_SOCK_TEST
65
66 #define UDP_PORT 51234
67 #define TCP_PORT (UDP_PORT+10)
68 #define BIG_DATA_LEN 8192
69 #define ADDRESS "127.0.0.1"
70
71 static char bigdata[BIG_DATA_LEN];
72 static char bigbuffer[BIG_DATA_LEN];
73
74 /* Macro for checking the value of "sin_len" member of sockaddr
75 * (it must always be zero).
76 */
77 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
78 # define CHECK_SA_ZERO_LEN(addr, ret) \
79 if (((pj_addr_hdr*)(addr))->sa_zero_len != 0) \
80 return ret
81 #else
82 # define CHECK_SA_ZERO_LEN(addr, ret)
83 #endif
84
85
86 static int format_test(void)
87 {
88 pj_str_t s = pj_str(ADDRESS);
89 unsigned char *p;
90 pj_in_addr addr;
91 char zero[64];
92 pj_sockaddr_in addr2;
93 const pj_str_t *hostname;
94 const unsigned char A[] = {127, 0, 0, 1};
95
96 PJ_LOG(3,("test", "...format_test()"));
97
98 /* pj_inet_aton() */
99 if (pj_inet_aton(&s, &addr) != 1)
100 return -10;
101
102 /* Check the result. */
103 p = (unsigned char*)&addr;
104 if (p[0]!=A[0] || p[1]!=A[1] || p[2]!=A[2] || p[3]!=A[3]) {
105 PJ_LOG(3,("test", " error: mismatched address. p0=%d, p1=%d, "
106 "p2=%d, p3=%d", p[0] & 0xFF, p[1] & 0xFF,
107 p[2] & 0xFF, p[3] & 0xFF));
108 return -15;
109 }
110
111 /* pj_inet_ntoa() */
112 p = (unsigned char*) pj_inet_ntoa(addr);
113 if (!p)
114 return -20;
115
116 if (pj_strcmp2(&s, (char*)p) != 0)
117 return -22;
118
119 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
120 /* pj_inet_pton() */
121 /* pj_inet_ntop() */
122 {
123 const pj_str_t s_ipv4 = pj_str("127.0.0.1");
124 const pj_str_t s_ipv6 = pj_str("fe80::2ff:83ff:fe7c:8b42");
125 char buf_ipv4[PJ_INET_ADDRSTRLEN];
126 char buf_ipv6[PJ_INET6_ADDRSTRLEN];
127 pj_in_addr ipv4;
128 pj_in6_addr ipv6;
129
130 if (pj_inet_pton(pj_AF_INET(), &s_ipv4, &ipv4) != PJ_SUCCESS)
131 return -24;
132
133 p = (unsigned char*)&ipv4;
134 if (p[0]!=A[0] || p[1]!=A[1] || p[2]!=A[2] || p[3]!=A[3]) {
135 return -25;
136 }
137
138 if (pj_inet_pton(pj_AF_INET6(), &s_ipv6, &ipv6) != PJ_SUCCESS)
139 return -26;
140
141 p = (unsigned char*)&ipv6;
142 if (p[0] != 0xfe || p[1] != 0x80 || p[2] != 0 || p[3] != 0 ||
143 p[4] != 0 || p[5] != 0 || p[6] != 0 || p[7] != 0 ||
144 p[8] != 0x02 || p[9] != 0xff || p[10] != 0x83 || p[11] != 0xff ||
145 p[12]!=0xfe || p[13]!=0x7c || p[14] != 0x8b || p[15]!=0x42)
146 {
147 return -27;
148 }
149
150 if (pj_inet_ntop(pj_AF_INET(), &ipv4, buf_ipv4, sizeof(buf_ipv4)) != PJ_SUCCESS)
151 return -28;
152 if (pj_stricmp2(&s_ipv4, buf_ipv4) != 0)
153 return -29;
154
155 if (pj_inet_ntop(pj_AF_INET6(), &ipv6, buf_ipv6, sizeof(buf_ipv6)) != PJ_SUCCESS)
156 return -30;
157 if (pj_stricmp2(&s_ipv6, buf_ipv6) != 0)
158 return -31;
159 }
160
161 #endif /* PJ_HAS_IPV6 */
162
163 #ifdef NDEBUG
164 /* This should not crash */
165 pj_sockaddr_in_init(NULL, 0, 1000);
166 #endif
167
168 /* Test that pj_sockaddr_in_init() initialize the whole structure,
169 * including sin_zero_pad.
170 */
171 pj_sockaddr_in_init(&addr2, 0, 1000);
172 pj_bzero(zero, sizeof(zero));
173 if (pj_memcmp(addr2.sin_zero_pad, zero, sizeof(addr2.sin_zero_pad)) != 0)
174 return -35;
175
176 /* pj_gethostname() */
177 hostname = pj_gethostname();
178 if (!hostname || !hostname->ptr || !hostname->slen)
179 return -40;
180
181 PJ_LOG(3,("test", "....hostname is %.*s",
182 (int)hostname->slen, hostname->ptr));
183
184 /* pj_gethostaddr() */
185
186 /* Various constants */
187 #if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0
188 if (PJ_AF_INET==0xFFFF) return -5500;
189 if (PJ_AF_INET6==0xFFFF) return -5501;
190
191 /* 0xFFFF could be a valid SOL_SOCKET (e.g: on some Win or Mac) */
192 //if (PJ_SOL_SOCKET==0xFFFF) return -5503;
193
194 if (PJ_SOL_IP==0xFFFF) return -5502;
195 if (PJ_SOL_TCP==0xFFFF) return -5510;
196 if (PJ_SOL_UDP==0xFFFF) return -5520;
197 if (PJ_SOL_IPV6==0xFFFF) return -5530;
198
199 if (PJ_SO_TYPE==0xFFFF) return -5540;
200 if (PJ_SO_RCVBUF==0xFFFF) return -5550;
201 if (PJ_SO_SNDBUF==0xFFFF) return -5560;
202 if (PJ_TCP_NODELAY==0xFFFF) return -5570;
203 if (PJ_SO_REUSEADDR==0xFFFF) return -5580;
204
205 if (PJ_MSG_OOB==0xFFFF) return -5590;
206 if (PJ_MSG_PEEK==0xFFFF) return -5600;
207 #endif
208
209 return 0;
210 }
211
212 static int parse_test(void)
213 {
214 #define IPv4 1
215 #define IPv6 2
216
217 struct test_t {
218 const char *input;
219 int result_af;
220 const char *result_ip;
221 pj_uint16_t result_port;
222 };
223 struct test_t valid_tests[] =
224 {
225 /* IPv4 */
226 { "10.0.0.1:80", IPv4, "10.0.0.1", 80},
227 { "10.0.0.1", IPv4, "10.0.0.1", 0},
228 { "10.0.0.1:", IPv4, "10.0.0.1", 0},
229 { "10.0.0.1:0", IPv4, "10.0.0.1", 0},
230 { ":80", IPv4, "0.0.0.0", 80},
231 { ":", IPv4, "0.0.0.0", 0},
232 #if !PJ_SYMBIAN
233 { "localhost", IPv4, "127.0.0.1", 0},
234 { "localhost:", IPv4, "127.0.0.1", 0},
235 { "localhost:80", IPv4, "127.0.0.1", 80},
236 #endif
237
238 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
239 { "fe::01:80", IPv6, "fe::01:80", 0},
240 { "[fe::01]:80", IPv6, "fe::01", 80},
241 { "fe::01", IPv6, "fe::01", 0},
242 { "[fe::01]", IPv6, "fe::01", 0},
243 { "fe::01:", IPv6, "fe::01", 0},
244 { "[fe::01]:", IPv6, "fe::01", 0},
245 { "::", IPv6, "::0", 0},
246 { "[::]", IPv6, "::", 0},
247 { ":::", IPv6, "::", 0},
248 { "[::]:", IPv6, "::", 0},
249 { ":::80", IPv6, "::", 80},
250 { "[::]:80", IPv6, "::", 80},
251 #endif
252 };
253 struct test_t invalid_tests[] =
254 {
255 /* IPv4 */
256 { "10.0.0.1:abcd", IPv4}, /* port not numeric */
257 { "10.0.0.1:-1", IPv4}, /* port contains illegal character */
258 { "10.0.0.1:123456", IPv4}, /* port too big */
259 //this actually is fine on my Mac OS 10.9
260 //it will be resolved with gethostbyname() and something is returned!
261 //{ "1.2.3.4.5:80", IPv4}, /* invalid IP */
262 { "10:0:80", IPv4}, /* hostname has colon */
263
264 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
265 { "[fe::01]:abcd", IPv6}, /* port not numeric */
266 { "[fe::01]:-1", IPv6}, /* port contains illegal character */
267 { "[fe::01]:123456", IPv6}, /* port too big */
268 { "fe::01:02::03:04:80", IPv6}, /* invalid IP */
269 { "[fe::01:02::03:04]:80", IPv6}, /* invalid IP */
270 { "[fe:01", IPv6}, /* Unterminated bracket */
271 #endif
272 };
273
274 unsigned i;
275
276 PJ_LOG(3,("test", "...IP address parsing"));
277
278 for (i=0; i<PJ_ARRAY_SIZE(valid_tests); ++i) {
279 pj_status_t status;
280 pj_str_t input;
281 pj_sockaddr addr, result;
282
283 switch (valid_tests[i].result_af) {
284 case IPv4:
285 valid_tests[i].result_af = PJ_AF_INET;
286 break;
287 case IPv6:
288 valid_tests[i].result_af = PJ_AF_INET6;
289 break;
290 default:
291 pj_assert(!"Invalid AF!");
292 continue;
293 }
294
295 /* Try parsing with PJ_AF_UNSPEC */
296 status = pj_sockaddr_parse(PJ_AF_UNSPEC, 0,
297 pj_cstr(&input, valid_tests[i].input),
298 &addr);
299 if (status != PJ_SUCCESS) {
300 PJ_LOG(1,("test", ".... failed when parsing %s (i=%d)",
301 valid_tests[i].input, i));
302 return -10;
303 }
304
305 /* Check "sin_len" member of parse result */
306 CHECK_SA_ZERO_LEN(&addr, -20);
307
308 /* Build the correct result */
309 status = pj_sockaddr_init(valid_tests[i].result_af,
310 &result,
311 pj_cstr(&input, valid_tests[i].result_ip),
312 valid_tests[i].result_port);
313 if (status != PJ_SUCCESS) {
314 PJ_LOG(1,("test", ".... error building IP address %s",
315 valid_tests[i].input));
316 return -30;
317 }
318
319 /* Compare the result */
320 if (pj_sockaddr_cmp(&addr, &result) != 0) {
321 PJ_LOG(1,("test", ".... parsed result mismatched for %s",
322 valid_tests[i].input));
323 return -40;
324 }
325
326 /* Parse again with the specified af */
327 status = pj_sockaddr_parse(valid_tests[i].result_af, 0,
328 pj_cstr(&input, valid_tests[i].input),
329 &addr);
330 if (status != PJ_SUCCESS) {
331 PJ_LOG(1,("test", ".... failed when parsing %s",
332 valid_tests[i].input));
333 return -50;
334 }
335
336 /* Check "sin_len" member of parse result */
337 CHECK_SA_ZERO_LEN(&addr, -55);
338
339 /* Compare the result again */
340 if (pj_sockaddr_cmp(&addr, &result) != 0) {
341 PJ_LOG(1,("test", ".... parsed result mismatched for %s",
342 valid_tests[i].input));
343 return -60;
344 }
345 }
346
347 for (i=0; i<PJ_ARRAY_SIZE(invalid_tests); ++i) {
348 pj_status_t status;
349 pj_str_t input;
350 pj_sockaddr addr;
351
352 switch (invalid_tests[i].result_af) {
353 case IPv4:
354 invalid_tests[i].result_af = PJ_AF_INET;
355 break;
356 case IPv6:
357 invalid_tests[i].result_af = PJ_AF_INET6;
358 break;
359 default:
360 pj_assert(!"Invalid AF!");
361 continue;
362 }
363
364 /* Try parsing with PJ_AF_UNSPEC */
365 status = pj_sockaddr_parse(PJ_AF_UNSPEC, 0,
366 pj_cstr(&input, invalid_tests[i].input),
367 &addr);
368 if (status == PJ_SUCCESS) {
369 PJ_LOG(1,("test", ".... expecting failure when parsing %s",
370 invalid_tests[i].input));
371 return -100;
372 }
373 }
374
375 return 0;
376 }
377
378 static int purity_test(void)
379 {
380 PJ_LOG(3,("test", "...purity_test()"));
381
382 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
383 /* Check on "sin_len" member of sockaddr */
384 {
385 const pj_str_t str_ip = {"1.1.1.1", 7};
386 pj_sockaddr addr[16];
387 pj_addrinfo ai[16];
388 unsigned cnt;
389 pj_status_t rc;
390
391 /* pj_enum_ip_interface() */
392 cnt = PJ_ARRAY_SIZE(addr);
393 rc = pj_enum_ip_interface(pj_AF_UNSPEC(), &cnt, addr);
394 if (rc == PJ_SUCCESS) {
395 while (cnt--)
396 CHECK_SA_ZERO_LEN(&addr[cnt], -10);
397 }
398
399 /* pj_gethostip() on IPv4 */
400 rc = pj_gethostip(pj_AF_INET(), &addr[0]);
401 if (rc == PJ_SUCCESS)
402 CHECK_SA_ZERO_LEN(&addr[0], -20);
403
404 /* pj_gethostip() on IPv6 */
405 rc = pj_gethostip(pj_AF_INET6(), &addr[0]);
406 if (rc == PJ_SUCCESS)
407 CHECK_SA_ZERO_LEN(&addr[0], -30);
408
409 /* pj_getdefaultipinterface() on IPv4 */
410 rc = pj_getdefaultipinterface(pj_AF_INET(), &addr[0]);
411 if (rc == PJ_SUCCESS)
412 CHECK_SA_ZERO_LEN(&addr[0], -40);
413
414 /* pj_getdefaultipinterface() on IPv6 */
415 rc = pj_getdefaultipinterface(pj_AF_INET6(), &addr[0]);
416 if (rc == PJ_SUCCESS)
417 CHECK_SA_ZERO_LEN(&addr[0], -50);
418
419 /* pj_getaddrinfo() on a host name */
420 cnt = PJ_ARRAY_SIZE(ai);
421 rc = pj_getaddrinfo(pj_AF_UNSPEC(), pj_gethostname(), &cnt, ai);
422 if (rc == PJ_SUCCESS) {
423 while (cnt--)
424 CHECK_SA_ZERO_LEN(&ai[cnt].ai_addr, -60);
425 }
426
427 /* pj_getaddrinfo() on an IP address */
428 cnt = PJ_ARRAY_SIZE(ai);
429 rc = pj_getaddrinfo(pj_AF_UNSPEC(), &str_ip, &cnt, ai);
430 if (rc == PJ_SUCCESS) {
431 while (cnt--)
432 CHECK_SA_ZERO_LEN(&ai[cnt].ai_addr, -70);
433 }
434 }
435 #endif
436
437 return 0;
438 }
439
440 static int simple_sock_test(void)
441 {
442 int types[2];
443 pj_sock_t sock;
444 int i;
445 pj_status_t rc = PJ_SUCCESS;
446
447 types[0] = pj_SOCK_STREAM();
448 types[1] = pj_SOCK_DGRAM();
449
450 PJ_LOG(3,("test", "...simple_sock_test()"));
451
452 for (i=0; i<(int)PJ_ARRAY_SIZE(types); ++i) {
453 rc = pj_sock_socket(pj_AF_INET(), types[i], 0, &sock);
454 if (rc != PJ_SUCCESS) {
455 app_perror("...error: unable to create socket", rc);
456 break;
457 } else {
458 rc = pj_sock_close(sock);
459 if (rc != 0) {
460 app_perror("...error: close socket", rc);
461 break;
462 }
463 }
464 }
465 return rc;
466 }
467
468
469 static int send_recv_test(int sock_type,
470 pj_sock_t ss, pj_sock_t cs,
471 pj_sockaddr_in *dstaddr, pj_sockaddr_in *srcaddr,
472 int addrlen)
473 {
474 enum { DATA_LEN = 16 };
475 char senddata[DATA_LEN+4], recvdata[DATA_LEN+4];
476 pj_ssize_t sent, received, total_received;
477 pj_status_t rc;
478
479 TRACE_(("test", "....create_random_string()"));
480 pj_create_random_string(senddata, DATA_LEN);
481 senddata[DATA_LEN-1] = '\0';
482
483 /*
484 * Test send/recv small data.
485 */
486 TRACE_(("test", "....sendto()"));
487 if (dstaddr) {
488 sent = DATA_LEN;
489 rc = pj_sock_sendto(cs, senddata, &sent, 0, dstaddr, addrlen);
490 if (rc != PJ_SUCCESS || sent != DATA_LEN) {
491 app_perror("...sendto error", rc);
492 rc = -140; goto on_error;
493 }
494 } else {
495 sent = DATA_LEN;
496 rc = pj_sock_send(cs, senddata, &sent, 0);
497 if (rc != PJ_SUCCESS || sent != DATA_LEN) {
498 app_perror("...send error", rc);
499 rc = -145; goto on_error;
500 }
501 }
502
503 TRACE_(("test", "....recv()"));
504 if (srcaddr) {
505 pj_sockaddr_in addr;
506 int srclen = sizeof(addr);
507
508 pj_bzero(&addr, sizeof(addr));
509
510 received = DATA_LEN;
511 rc = pj_sock_recvfrom(ss, recvdata, &received, 0, &addr, &srclen);
512 if (rc != PJ_SUCCESS || received != DATA_LEN) {
513 app_perror("...recvfrom error", rc);
514 rc = -150; goto on_error;
515 }
516 if (srclen != addrlen)
517 return -151;
518 if (pj_sockaddr_cmp(&addr, srcaddr) != 0) {
519 char srcaddr_str[32], addr_str[32];
520 pj_ansi_strxcpy(srcaddr_str, pj_inet_ntoa(srcaddr->sin_addr),
521 sizeof(srcaddr_str));
522 pj_ansi_strxcpy(addr_str, pj_inet_ntoa(addr.sin_addr),
523 sizeof(addr_str));
524 PJ_LOG(3,("test", "...error: src address mismatch (original=%s, "
525 "recvfrom addr=%s)",
526 srcaddr_str, addr_str));
527 return -152;
528 }
529
530 } else {
531 /* Repeat recv() until all data is received.
532 * This applies only for non-UDP of course, since for UDP
533 * we would expect all data to be received in one packet.
534 */
535 total_received = 0;
536 do {
537 received = DATA_LEN-total_received;
538 rc = pj_sock_recv(ss, recvdata+total_received, &received, 0);
539 if (rc != PJ_SUCCESS) {
540 app_perror("...recv error", rc);
541 rc = -155; goto on_error;
542 }
543 if (received <= 0) {
544 PJ_LOG(3,("", "...error: socket has closed! (received=%lu)",
545 (unsigned long)received));
546 rc = -156; goto on_error;
547 }
548 if (received != DATA_LEN-total_received) {
549 if (sock_type != pj_SOCK_STREAM()) {
550 PJ_LOG(3,("", "...error: expecting %lu bytes, got %lu bytes",
551 (unsigned long)(DATA_LEN-total_received),
552 (unsigned long)received));
553 rc = -157; goto on_error;
554 }
555 }
556 total_received += received;
557 } while (total_received < DATA_LEN);
558 }
559
560 TRACE_(("test", "....memcmp()"));
561 if (pj_memcmp(senddata, recvdata, DATA_LEN) != 0) {
562 PJ_LOG(3,("","...error: received data mismatch "
563 "(got:'%s' expecting:'%s'",
564 recvdata, senddata));
565 rc = -160; goto on_error;
566 }
567
568 /*
569 * Test send/recv big data.
570 */
571 TRACE_(("test", "....sendto()"));
572 if (dstaddr) {
573 sent = BIG_DATA_LEN;
574 rc = pj_sock_sendto(cs, bigdata, &sent, 0, dstaddr, addrlen);
575 if (rc != PJ_SUCCESS || sent != BIG_DATA_LEN) {
576 app_perror("...sendto error", rc);
577 rc = -161; goto on_error;
578 }
579 } else {
580 sent = BIG_DATA_LEN;
581 rc = pj_sock_send(cs, bigdata, &sent, 0);
582 if (rc != PJ_SUCCESS || sent != BIG_DATA_LEN) {
583 app_perror("...send error", rc);
584 rc = -165; goto on_error;
585 }
586 }
587
588 TRACE_(("test", "....recv()"));
589
590 /* Repeat recv() until all data is received.
591 * This applies only for non-UDP of course, since for UDP
592 * we would expect all data to be received in one packet.
593 */
594 total_received = 0;
595 do {
596 received = BIG_DATA_LEN-total_received;
597 rc = pj_sock_recv(ss, bigbuffer+total_received, &received, 0);
598 if (rc != PJ_SUCCESS) {
599 app_perror("...recv error", rc);
600 rc = -170; goto on_error;
601 }
602 if (received <= 0) {
603 PJ_LOG(3,("", "...error: socket has closed! (received=%lu)",
604 (unsigned long)received));
605 rc = -173; goto on_error;
606 }
607 if (received != BIG_DATA_LEN-total_received) {
608 if (sock_type != pj_SOCK_STREAM()) {
609 PJ_LOG(3,("", "...error: expecting %lu bytes, got %lu bytes",
610 (unsigned long)BIG_DATA_LEN-total_received,
611 (unsigned long)received));
612 rc = -176; goto on_error;
613 }
614 }
615 total_received += received;
616 } while (total_received < BIG_DATA_LEN);
617
618 TRACE_(("test", "....memcmp()"));
619 if (pj_memcmp(bigdata, bigbuffer, BIG_DATA_LEN) != 0) {
620 PJ_LOG(3,("", "...error: received data has been altered!"));
621 rc = -180; goto on_error;
622 }
623
624 rc = 0;
625
626 on_error:
627 return rc;
628 }
629
630 static int udp_test(void)
631 {
632 pj_sock_t cs = PJ_INVALID_SOCKET, ss = PJ_INVALID_SOCKET;
633 pj_sockaddr_in dstaddr, srcaddr;
634 pj_str_t s;
635 pj_status_t rc = 0, retval;
636
637 PJ_LOG(3,("test", "...udp_test()"));
638
639 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ss);
640 if (rc != 0) {
641 app_perror("...error: unable to create socket", rc);
642 return -100;
643 }
644
645 rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &cs);
646 if (rc != 0) {
647 rc = -110; goto on_error;
648 }
649
650 /* Bind server socket. */
651 pj_bzero(&dstaddr, sizeof(dstaddr));
652 dstaddr.sin_family = pj_AF_INET();
653 dstaddr.sin_port = pj_htons(UDP_PORT);
654 dstaddr.sin_addr = pj_inet_addr(pj_cstr(&s, ADDRESS));
655
656 if ((rc=pj_sock_bind(ss, &dstaddr, sizeof(dstaddr))) != 0) {
657 app_perror("...bind error udp:"ADDRESS, rc);
658 rc = -120; goto on_error;
659 }
660
661 /* Bind client socket. */
662 pj_bzero(&srcaddr, sizeof(srcaddr));
663 srcaddr.sin_family = pj_AF_INET();
664 srcaddr.sin_port = pj_htons(UDP_PORT-1);
665 srcaddr.sin_addr = pj_inet_addr(pj_cstr(&s, ADDRESS));
666
667 if ((rc=pj_sock_bind(cs, &srcaddr, sizeof(srcaddr))) != 0) {
668 app_perror("...bind error", rc);
669 rc = -121; goto on_error;
670 }
671
672 /* Test send/recv, with sendto */
673 rc = send_recv_test(pj_SOCK_DGRAM(), ss, cs, &dstaddr, NULL,
674 sizeof(dstaddr));
675 if (rc != 0)
676 goto on_error;
677
678 /* Test send/recv, with sendto and recvfrom */
679 rc = send_recv_test(pj_SOCK_DGRAM(), ss, cs, &dstaddr,
680 &srcaddr, sizeof(dstaddr));
681 if (rc != 0)
682 goto on_error;
683
684 /* Disable this test on Symbian since UDP connect()/send() failed
685 * with S60 3rd edition (including MR2).
686 * See https://github.com/pjsip/pjproject/issues/264
687 */
688 #if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0
689 /* connect() the sockets. */
690 rc = pj_sock_connect(cs, &dstaddr, sizeof(dstaddr));
691 if (rc != 0) {
692 app_perror("...connect() error", rc);
693 rc = -122; goto on_error;
694 }
695
696 /* Test send/recv with send() */
697 rc = send_recv_test(pj_SOCK_DGRAM(), ss, cs, NULL, NULL, 0);
698 if (rc != 0)
699 goto on_error;
700
701 /* Test send/recv with send() and recvfrom */
702 rc = send_recv_test(pj_SOCK_DGRAM(), ss, cs, NULL, &srcaddr,
703 sizeof(srcaddr));
704 if (rc != 0)
705 goto on_error;
706 #endif
707
708 on_error:
709 retval = rc;
710 if (cs != PJ_INVALID_SOCKET) {
711 rc = pj_sock_close(cs);
712 if (rc != PJ_SUCCESS) {
713 app_perror("...error in closing socket", rc);
714 return -1000;
715 }
716 }
717 if (ss != PJ_INVALID_SOCKET) {
718 rc = pj_sock_close(ss);
719 if (rc != PJ_SUCCESS) {
720 app_perror("...error in closing socket", rc);
721 return -1010;
722 }
723 }
724
725 return retval;
726 }
727
728 static int tcp_test(void)
729 {
730 pj_sock_t cs, ss;
731 pj_status_t rc = 0, retval;
732
733 PJ_LOG(3,("test", "...tcp_test()"));
734
735 rc = app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, &ss, &cs);
736 if (rc != PJ_SUCCESS) {
737 app_perror("...error: app_socketpair():", rc);
738 return -2000;
739 }
740
741 /* Test send/recv with send() and recv() */
742 retval = send_recv_test(pj_SOCK_STREAM(), ss, cs, NULL, NULL, 0);
743
744 rc = pj_sock_close(cs);
745 if (rc != PJ_SUCCESS) {
746 app_perror("...error in closing socket", rc);
747 return -2000;
748 }
749
750 rc = pj_sock_close(ss);
751 if (rc != PJ_SUCCESS) {
752 app_perror("...error in closing socket", rc);
753 return -2010;
754 }
755
756 return retval;
757 }
758
759 static int ioctl_test(void)
760 {
761 return 0;
762 }
763
764 static int gethostbyname_test(void)
765 {
766 pj_str_t host;
767 pj_hostent he;
768 pj_status_t status;
769
770 PJ_LOG(3,("test", "...gethostbyname_test()"));
771
772 /* Testing pj_gethostbyname() with invalid host */
773 host = pj_str("an-invalid-host-name");
774 status = pj_gethostbyname(&host, &he);
775
776 /* Must return failure! */
777 if (status == PJ_SUCCESS)
778 return -20100;
779 else
780 return 0;
781 }
782
783 #if 0
784 #include "../pj/os_symbian.h"
785 static int connect_test()
786 {
787 RSocketServ rSockServ;
788 RSocket rSock;
789 TInetAddr inetAddr;
790 TRequestStatus reqStatus;
791 char buffer[16];
792 TPtrC8 data((const TUint8*)buffer, (TInt)sizeof(buffer));
793 int rc;
794
795 rc = rSockServ.Connect();
796 if (rc != KErrNone)
797 return rc;
798
799 rc = rSock.Open(rSockServ, KAfInet, KSockDatagram, KProtocolInetUdp);
800 if (rc != KErrNone)
801 {
802 rSockServ.Close();
803 return rc;
804 }
805
806 inetAddr.Init(KAfInet);
807 inetAddr.Input(_L("127.0.0.1"));
808 inetAddr.SetPort(80);
809
810 rSock.Connect(inetAddr, reqStatus);
811 User::WaitForRequest(reqStatus);
812
813 if (reqStatus != KErrNone) {
814 rSock.Close();
815 rSockServ.Close();
816 return rc;
817 }
818
819 rSock.Send(data, 0, reqStatus);
820 User::WaitForRequest(reqStatus);
821
822 if (reqStatus!=KErrNone) {
823 rSock.Close();
824 rSockServ.Close();
825 return rc;
826 }
827
828 rSock.Close();
829 rSockServ.Close();
830 return KErrNone;
831 }
832 #endif
833
834 static const char *family2str(int f)
835 {
836 if (f == pj_AF_UNIX())
837 return "AF_UNIX";
838 if (f == pj_AF_INET())
839 return "AF_INET";
840 if (f == pj_AF_INET6())
841 return "AF_INET6";
842 return "?";
843 }
844
845 static const char *type2str(int type)
846 {
847 if (type == pj_SOCK_DGRAM())
848 return "SOCK_DGRAM";
849 if (type == pj_SOCK_STREAM())
850 return "SOCK_STREAM";
851 return "?";
852 }
853
854 static int do_sockpair_tst(int family, int type, int proto)
855 {
856 pj_status_t rc;
857 pj_sock_t sv[2] = {-1, -1};
858 char buf[64];
859 pj_ssize_t len;
860
861 PJ_LOG(3, ("test", "#Test socketpair, family: %s, type: %s",
862 family2str(family), type2str(type)));
863
864 rc = pj_sock_socketpair(family, type, proto, sv);
865 if (rc != PJ_SUCCESS)
866 goto on_error;
867
868 /* sv[0] send text to sv[1] */
869 len = pj_ansi_snprintf(buf, sizeof(buf), "hello, %ld->%ld",
870 (long)sv[0], (long)sv[1]);
871 rc = pj_sock_send(sv[0], buf, &len, 0);
872 if (rc != PJ_SUCCESS)
873 goto on_error;
874 PJ_LOG(3, ("test", "send: %.*s", (int)len, buf));
875
876 len = sizeof(buf);
877 rc = pj_sock_recv(sv[1], buf, &len, 0);
878 if (rc != PJ_SUCCESS)
879 goto on_error;
880 PJ_LOG(3, ("test", "recv: %.*s", (int)len, buf));
881
882 /* sv[1] send text to sv[0] */
883 len = pj_ansi_snprintf(buf, sizeof(buf), "hello, %ld->%ld",
884 (long)sv[1], (long)sv[0]);
885 rc = pj_sock_send(sv[1], buf, &len, 0);
886 if (rc != PJ_SUCCESS)
887 goto on_error;
888 PJ_LOG(3, ("test", "send: %.*s", (int)len, buf));
889
890 len = sizeof(buf);
891 rc = pj_sock_recv(sv[0], buf, &len, 0);
892 if (rc != PJ_SUCCESS)
893 goto on_error;
894 PJ_LOG(3, ("test", "recv: %.*s", (int)len, buf));
895
896 on_error:
897 if (sv[0] > 0)
898 pj_sock_close(sv[0]);
899 if (sv[1] > 0)
900 pj_sock_close(sv[1]);
901
902 if (rc != PJ_SUCCESS) {
903 PJ_PERROR(1, ("test", rc, "error"));
904 }
905
906 return rc;
907 }
908
909 static int socketpair_test()
910 {
911 int rc;
912
913 PJ_LOG(3, ("test", "...socketpair_test()"));
914
915 /* AF_UNIX */
916 #if (defined(PJ_SOCK_HAS_SOCKETPAIR) && PJ_SOCK_HAS_SOCKETPAIR)
917 rc = do_sockpair_tst(pj_AF_UNIX(), pj_SOCK_DGRAM(), 0);
918 if (rc != PJ_SUCCESS)
919 return rc;
920
921 rc = do_sockpair_tst(pj_AF_UNIX(), pj_SOCK_STREAM(), 0);
922 if (rc != PJ_SUCCESS)
923 return rc;
924 #else
925 PJ_LOG(2, ("test", "Not support AF_UNIX"));
926 #endif
927
928 /* AF_INET */
929 rc = do_sockpair_tst(pj_AF_INET(), pj_SOCK_DGRAM(), 0);
930 if (rc != PJ_SUCCESS)
931 return rc;
932 #if PJ_HAS_TCP
933 rc = do_sockpair_tst(pj_AF_INET(), pj_SOCK_STREAM(), 0);
934 if (rc != PJ_SUCCESS)
935 return rc;
936 #endif
937
938 /* AF_INET6 */
939 rc = do_sockpair_tst(pj_AF_INET6(), pj_SOCK_DGRAM(), 0);
940 if (rc != PJ_SUCCESS)
941 return rc;
942 #if PJ_HAS_TCP
943 rc = do_sockpair_tst(pj_AF_INET6(), pj_SOCK_STREAM(), 0);
944 if (rc != PJ_SUCCESS)
945 return rc;
946 #endif
947
948 return 0;
949 }
950
951 int sock_test()
952 {
953 int rc;
954
955 pj_create_random_string(bigdata, BIG_DATA_LEN);
956
957 // Enable this to demonstrate the error witn S60 3rd Edition MR2
958 #if 0
959 rc = connect_test();
960 if (rc != 0)
961 return rc;
962 #endif
963
964 rc = format_test();
965 if (rc != 0)
966 return rc;
967
968 rc = parse_test();
969 if (rc != 0)
970 return rc;
971
972 rc = purity_test();
973 if (rc != 0)
974 return rc;
975
976 rc = gethostbyname_test();
977 if (rc != 0)
978 return rc;
979
980 rc = simple_sock_test();
981 if (rc != 0)
982 return rc;
983
984 rc = ioctl_test();
985 if (rc != 0)
986 return rc;
987
988 rc = udp_test();
989 if (rc != 0)
990 return rc;
991
992 rc = tcp_test();
993 if (rc != 0)
994 return rc;
995
996 rc = socketpair_test();
997 if (rc != 0)
998 return rc;
999
1000 return 0;
1001 }
1002
1003
1004 #else
1005 /* To prevent warning about "translation unit is empty"
1006 * when this test is disabled.
1007 */
1008 int dummy_sock_test;
1009 #endif /* INCLUDE_SOCK_TEST */
1010