blob: 1bdd637eef55b180ed7c3870e63680bccc77bda2 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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_gethostname()
44 * - pj_sock_socket()
45 * - pj_sock_close()
46 * - pj_sock_send()
47 * - pj_sock_sendto()
48 * - pj_sock_recv()
49 * - pj_sock_recvfrom()
50 * - pj_sock_bind()
51 * - pj_sock_connect()
52 * - pj_sock_listen()
53 * - pj_sock_accept()
54 *
55 *
56 * This file is <b>pjlib-test/sock.c</b>
57 *
58 * \include pjlib-test/sock.c
59 */
60
61#if INCLUDE_SOCK_TEST
62
63#define UDP_PORT 51234
64#define TCP_PORT (UDP_PORT+10)
65#define BIG_DATA_LEN 9000
Benny Prijono42c5b9e2006-05-10 19:24:40 +000066#define ADDRESS "127.0.0.1"
67#define A0 127
68#define A1 0
69#define A2 0
70#define A3 1
71
Benny Prijono5dcb38d2005-11-21 01:55:47 +000072
73static char bigdata[BIG_DATA_LEN];
74static char bigbuffer[BIG_DATA_LEN];
75
76static int format_test(void)
77{
Benny Prijono42c5b9e2006-05-10 19:24:40 +000078 pj_str_t s = pj_str(ADDRESS);
79 unsigned char *p;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000080 pj_in_addr addr;
81 const pj_str_t *hostname;
82
83 PJ_LOG(3,("test", "...format_test()"));
84
85 /* pj_inet_aton() */
86 if (pj_inet_aton(&s, &addr) != 1)
87 return -10;
88
89 /* Check the result. */
Benny Prijono42c5b9e2006-05-10 19:24:40 +000090 p = (unsigned char*)&addr;
91 if (p[0]!=A0 || p[1]!=A1 || p[2]!=A2 || p[3]!=A3) {
92 PJ_LOG(3,("test", " error: mismatched address. p0=%d, p1=%d, "
93 "p2=%d, p3=%d", p[0] & 0xFF, p[1] & 0xFF,
94 p[2] & 0xFF, p[3] & 0xFF));
Benny Prijono5dcb38d2005-11-21 01:55:47 +000095 return -15;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000096 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +000097
98 /* pj_inet_ntoa() */
Benny Prijonod8410532006-06-15 11:04:33 +000099 p = (unsigned char*) pj_inet_ntoa(addr);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000100 if (!p)
101 return -20;
102
Benny Prijonod8410532006-06-15 11:04:33 +0000103 if (pj_strcmp2(&s, (char*)p) != 0)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000104 return -30;
105
106 /* pj_gethostname() */
107 hostname = pj_gethostname();
108 if (!hostname || !hostname->ptr || !hostname->slen)
109 return -40;
110
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000111 PJ_LOG(3,("test", "....hostname is %.*s",
112 (int)hostname->slen, hostname->ptr));
113
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000114 /* pj_gethostaddr() */
115
116 return 0;
117}
118
119static int simple_sock_test(void)
120{
121 int types[2];
122 pj_sock_t sock;
123 int i;
124 pj_status_t rc = PJ_SUCCESS;
125
126 types[0] = PJ_SOCK_STREAM;
127 types[1] = PJ_SOCK_DGRAM;
128
129 PJ_LOG(3,("test", "...simple_sock_test()"));
130
131 for (i=0; i<sizeof(types)/sizeof(types[0]); ++i) {
132
133 rc = pj_sock_socket(PJ_AF_INET, types[i], 0, &sock);
134 if (rc != PJ_SUCCESS) {
135 app_perror("...error: unable to create socket type %d", rc);
136 break;
137 } else {
138 rc = pj_sock_close(sock);
139 if (rc != 0) {
140 app_perror("...error: close socket", rc);
141 break;
142 }
143 }
144 }
145 return rc;
146}
147
148
149static int send_recv_test(int sock_type,
150 pj_sock_t ss, pj_sock_t cs,
151 pj_sockaddr_in *dstaddr, pj_sockaddr_in *srcaddr,
152 int addrlen)
153{
154 enum { DATA_LEN = 16 };
155 char senddata[DATA_LEN+4], recvdata[DATA_LEN+4];
156 pj_ssize_t sent, received, total_received;
157 pj_status_t rc;
158
159 TRACE_(("test", "....create_random_string()"));
160 pj_create_random_string(senddata, DATA_LEN);
161 senddata[DATA_LEN-1] = '\0';
162
163 /*
164 * Test send/recv small data.
165 */
166 TRACE_(("test", "....sendto()"));
167 if (dstaddr) {
168 sent = DATA_LEN;
169 rc = pj_sock_sendto(cs, senddata, &sent, 0, dstaddr, addrlen);
170 if (rc != PJ_SUCCESS || sent != DATA_LEN) {
171 app_perror("...sendto error", rc);
172 rc = -140; goto on_error;
173 }
174 } else {
175 sent = DATA_LEN;
176 rc = pj_sock_send(cs, senddata, &sent, 0);
177 if (rc != PJ_SUCCESS || sent != DATA_LEN) {
178 app_perror("...send error", rc);
179 rc = -145; goto on_error;
180 }
181 }
182
183 TRACE_(("test", "....recv()"));
184 if (srcaddr) {
185 pj_sockaddr_in addr;
186 int srclen = sizeof(addr);
187
188 pj_memset(&addr, 0, sizeof(addr));
189
190 received = DATA_LEN;
191 rc = pj_sock_recvfrom(ss, recvdata, &received, 0, &addr, &srclen);
192 if (rc != PJ_SUCCESS || received != DATA_LEN) {
193 app_perror("...recvfrom error", rc);
194 rc = -150; goto on_error;
195 }
196 if (srclen != addrlen)
197 return -151;
198 if (pj_memcmp(&addr, srcaddr, srclen) != 0) {
199 char srcaddr_str[32], addr_str[32];
200 strcpy(srcaddr_str, pj_inet_ntoa(srcaddr->sin_addr));
201 strcpy(addr_str, pj_inet_ntoa(addr.sin_addr));
202 PJ_LOG(3,("test", "...error: src address mismatch (original=%s, "
203 "recvfrom addr=%s)",
204 srcaddr_str, addr_str));
205 return -152;
206 }
207
208 } else {
209 /* Repeat recv() until all data is received.
210 * This applies only for non-UDP of course, since for UDP
211 * we would expect all data to be received in one packet.
212 */
213 total_received = 0;
214 do {
215 received = DATA_LEN-total_received;
216 rc = pj_sock_recv(ss, recvdata+total_received, &received, 0);
217 if (rc != PJ_SUCCESS) {
218 app_perror("...recv error", rc);
219 rc = -155; goto on_error;
220 }
221 if (received <= 0) {
222 PJ_LOG(3,("", "...error: socket has closed! (received=%d)",
223 received));
224 rc = -156; goto on_error;
225 }
226 if (received != DATA_LEN-total_received) {
227 if (sock_type != PJ_SOCK_STREAM) {
228 PJ_LOG(3,("", "...error: expecting %u bytes, got %u bytes",
229 DATA_LEN-total_received, received));
230 rc = -157; goto on_error;
231 }
232 }
233 total_received += received;
234 } while (total_received < DATA_LEN);
235 }
236
237 TRACE_(("test", "....memcmp()"));
238 if (pj_memcmp(senddata, recvdata, DATA_LEN) != 0) {
239 PJ_LOG(3,("","...error: received data mismatch "
240 "(got:'%s' expecting:'%s'",
241 recvdata, senddata));
242 rc = -160; goto on_error;
243 }
244
245 /*
246 * Test send/recv big data.
247 */
248 TRACE_(("test", "....sendto()"));
249 if (dstaddr) {
250 sent = BIG_DATA_LEN;
251 rc = pj_sock_sendto(cs, bigdata, &sent, 0, dstaddr, addrlen);
252 if (rc != PJ_SUCCESS || sent != BIG_DATA_LEN) {
253 app_perror("...sendto error", rc);
254 rc = -161; goto on_error;
255 }
256 } else {
257 sent = BIG_DATA_LEN;
258 rc = pj_sock_send(cs, bigdata, &sent, 0);
259 if (rc != PJ_SUCCESS || sent != BIG_DATA_LEN) {
260 app_perror("...send error", rc);
261 rc = -165; goto on_error;
262 }
263 }
264
265 TRACE_(("test", "....recv()"));
266
267 /* Repeat recv() until all data is received.
268 * This applies only for non-UDP of course, since for UDP
269 * we would expect all data to be received in one packet.
270 */
271 total_received = 0;
272 do {
273 received = BIG_DATA_LEN-total_received;
274 rc = pj_sock_recv(ss, bigbuffer+total_received, &received, 0);
275 if (rc != PJ_SUCCESS) {
276 app_perror("...recv error", rc);
277 rc = -170; goto on_error;
278 }
279 if (received <= 0) {
280 PJ_LOG(3,("", "...error: socket has closed! (received=%d)",
281 received));
282 rc = -173; goto on_error;
283 }
284 if (received != BIG_DATA_LEN-total_received) {
285 if (sock_type != PJ_SOCK_STREAM) {
286 PJ_LOG(3,("", "...error: expecting %u bytes, got %u bytes",
287 BIG_DATA_LEN-total_received, received));
288 rc = -176; goto on_error;
289 }
290 }
291 total_received += received;
292 } while (total_received < BIG_DATA_LEN);
293
294 TRACE_(("test", "....memcmp()"));
295 if (pj_memcmp(bigdata, bigbuffer, BIG_DATA_LEN) != 0) {
296 PJ_LOG(3,("", "...error: received data has been altered!"));
297 rc = -180; goto on_error;
298 }
299
300 rc = 0;
301
302on_error:
303 return rc;
304}
305
306static int udp_test(void)
307{
308 pj_sock_t cs = PJ_INVALID_SOCKET, ss = PJ_INVALID_SOCKET;
309 pj_sockaddr_in dstaddr, srcaddr;
310 pj_str_t s;
311 pj_status_t rc = 0, retval;
312
313 PJ_LOG(3,("test", "...udp_test()"));
314
315 rc = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &ss);
316 if (rc != 0) {
317 app_perror("...error: unable to create socket", rc);
318 return -100;
319 }
320
321 rc = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &cs);
322 if (rc != 0)
323 return -110;
324
325 /* Bind server socket. */
326 pj_memset(&dstaddr, 0, sizeof(dstaddr));
327 dstaddr.sin_family = PJ_AF_INET;
328 dstaddr.sin_port = pj_htons(UDP_PORT);
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000329 dstaddr.sin_addr = pj_inet_addr(pj_cstr(&s, ADDRESS));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000330
331 if ((rc=pj_sock_bind(ss, &dstaddr, sizeof(dstaddr))) != 0) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000332 app_perror("...bind error udp:"ADDRESS, rc);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000333 rc = -120; goto on_error;
334 }
335
336 /* Bind client socket. */
337 pj_memset(&srcaddr, 0, sizeof(srcaddr));
338 srcaddr.sin_family = PJ_AF_INET;
339 srcaddr.sin_port = pj_htons(UDP_PORT-1);
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000340 srcaddr.sin_addr = pj_inet_addr(pj_cstr(&s, ADDRESS));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000341
342 if ((rc=pj_sock_bind(cs, &srcaddr, sizeof(srcaddr))) != 0) {
343 app_perror("...bind error", rc);
344 rc = -121; goto on_error;
345 }
346
347 /* Test send/recv, with sendto */
348 rc = send_recv_test(PJ_SOCK_DGRAM, ss, cs, &dstaddr, NULL,
349 sizeof(dstaddr));
350 if (rc != 0)
351 goto on_error;
352
353 /* Test send/recv, with sendto and recvfrom */
354 rc = send_recv_test(PJ_SOCK_DGRAM, ss, cs, &dstaddr,
355 &srcaddr, sizeof(dstaddr));
356 if (rc != 0)
357 goto on_error;
358
359 /* connect() the sockets. */
360 rc = pj_sock_connect(cs, &dstaddr, sizeof(dstaddr));
361 if (rc != 0) {
362 app_perror("...connect() error", rc);
363 rc = -122; goto on_error;
364 }
365
366 /* Test send/recv with send() */
367 rc = send_recv_test(PJ_SOCK_DGRAM, ss, cs, NULL, NULL, 0);
368 if (rc != 0)
369 goto on_error;
370
371 /* Test send/recv with send() and recvfrom */
372 rc = send_recv_test(PJ_SOCK_DGRAM, ss, cs, NULL, &srcaddr,
373 sizeof(srcaddr));
374 if (rc != 0)
375 goto on_error;
376
377on_error:
378 retval = rc;
379 if (cs != PJ_INVALID_SOCKET) {
380 rc = pj_sock_close(cs);
381 if (rc != PJ_SUCCESS) {
382 app_perror("...error in closing socket", rc);
383 return -1000;
384 }
385 }
386 if (ss != PJ_INVALID_SOCKET) {
387 rc = pj_sock_close(ss);
388 if (rc != PJ_SUCCESS) {
389 app_perror("...error in closing socket", rc);
390 return -1010;
391 }
392 }
393
394 return retval;
395}
396
397static int tcp_test(void)
398{
399 pj_sock_t cs, ss;
400 pj_status_t rc = 0, retval;
401
402 PJ_LOG(3,("test", "...tcp_test()"));
403
404 rc = app_socketpair(PJ_AF_INET, PJ_SOCK_STREAM, 0, &ss, &cs);
405 if (rc != PJ_SUCCESS) {
406 app_perror("...error: app_socketpair():", rc);
407 return -2000;
408 }
409
410 /* Test send/recv with send() and recv() */
411 retval = send_recv_test(PJ_SOCK_STREAM, ss, cs, NULL, NULL, 0);
412
413 rc = pj_sock_close(cs);
414 if (rc != PJ_SUCCESS) {
415 app_perror("...error in closing socket", rc);
416 return -2000;
417 }
418
419 rc = pj_sock_close(ss);
420 if (rc != PJ_SUCCESS) {
421 app_perror("...error in closing socket", rc);
422 return -2010;
423 }
424
425 return retval;
426}
427
428static int ioctl_test(void)
429{
430 return 0;
431}
432
433int sock_test()
434{
435 int rc;
436
437 pj_create_random_string(bigdata, BIG_DATA_LEN);
438
439 rc = format_test();
440 if (rc != 0)
441 return rc;
442
443 rc = simple_sock_test();
444 if (rc != 0)
445 return rc;
446
447 rc = ioctl_test();
448 if (rc != 0)
449 return rc;
450
451 rc = udp_test();
452 if (rc != 0)
453 return rc;
454
455 rc = tcp_test();
456 if (rc != 0)
457 return rc;
458
459 return 0;
460}
461
462
463#else
464/* To prevent warning about "translation unit is empty"
465 * when this test is disabled.
466 */
467int dummy_sock_test;
468#endif /* INCLUDE_SOCK_TEST */
469