blob: 42836c03ba805b4d4cc28a692bddeeae710e3c0b [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 "test.h"
20
21/**
22 * \page page_pjlib_select_test Test: Socket Select()
23 *
24 * This file provides implementation of \b select_test(). It tests the
25 * functionality of the pj_sock_select() API.
26 *
27 *
28 * This file is <b>pjlib-test/select.c</b>
29 *
30 * \include pjlib-test/select.c
31 */
32
33
34#if INCLUDE_SELECT_TEST
35
36#include <pj/sock.h>
37#include <pj/sock_select.h>
38#include <pj/log.h>
39#include <pj/string.h>
40#include <pj/assert.h>
41#include <pj/os.h>
42#include <pj/errno.h>
43
44enum
45{
46 READ_FDS,
47 WRITE_FDS,
48 EXCEPT_FDS
49};
50
51#define UDP_PORT 51232
52#define THIS_FILE "select_test"
53
54/*
55 * do_select()
56 *
57 * Perform pj_sock_select() and find out which sockets
58 * are signalled.
59 */
60static int do_select( pj_sock_t sock1, pj_sock_t sock2,
61 int setcount[])
62{
63 pj_fd_set_t fds[3];
64 pj_time_val timeout;
65 int i, n;
66
67 for (i=0; i<3; ++i) {
68 PJ_FD_ZERO(&fds[i]);
69 PJ_FD_SET(sock1, &fds[i]);
70 PJ_FD_SET(sock2, &fds[i]);
71 setcount[i] = 0;
72 }
73
74 timeout.sec = 1;
75 timeout.msec = 0;
76
77 n = pj_sock_select(FD_SETSIZE, &fds[0], &fds[1], &fds[2],
78 &timeout);
79 if (n < 0)
80 return n;
81 if (n == 0)
82 return 0;
83
84 for (i=0; i<3; ++i) {
85 if (PJ_FD_ISSET(sock1, &fds[i]))
86 setcount[i]++;
87 if (PJ_FD_ISSET(sock2, &fds[i]))
88 setcount[i]++;
89 }
90
91 return n;
92}
93
94/*
95 * select_test()
96 *
97 * Test main entry.
98 */
99int select_test()
100{
101 pj_sock_t udp1=PJ_INVALID_SOCKET, udp2=PJ_INVALID_SOCKET;
102 pj_sockaddr_in udp_addr;
103 int status;
104 int setcount[3];
105 pj_str_t s;
106 const char data[] = "hello";
107 const int datalen = 5;
108 pj_ssize_t sent, received;
109 char buf[10];
110 pj_status_t rc;
111
112 PJ_LOG(3, (THIS_FILE, "...Testing simple UDP select()"));
113
114 // Create two UDP sockets.
115 rc = pj_sock_socket( PJ_AF_INET, PJ_SOCK_DGRAM, 0, &udp1);
116 if (rc != PJ_SUCCESS) {
117 app_perror("...error: unable to create socket", rc);
118 status=-10; goto on_return;
119 }
120 rc = pj_sock_socket( PJ_AF_INET, PJ_SOCK_DGRAM, 0, &udp2);
121 if (udp2 == PJ_INVALID_SOCKET) {
122 app_perror("...error: unable to create socket", rc);
123 status=-20; goto on_return;
124 }
125
126 // Bind one of the UDP socket.
Benny Prijonoac623b32006-07-03 15:19:31 +0000127 pj_bzero(&udp_addr, sizeof(udp_addr));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000128 udp_addr.sin_family = PJ_AF_INET;
129 udp_addr.sin_port = UDP_PORT;
130 udp_addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
131
132 if (pj_sock_bind(udp2, &udp_addr, sizeof(udp_addr))) {
133 status=-30; goto on_return;
134 }
135
136 // Send data.
137 sent = datalen;
138 rc = pj_sock_sendto(udp1, data, &sent, 0, &udp_addr, sizeof(udp_addr));
139 if (rc != PJ_SUCCESS || sent != datalen) {
140 app_perror("...error: sendto() error", rc);
141 status=-40; goto on_return;
142 }
143
144 // Check that socket is marked as reable.
145 // Note that select() may also report that sockets are writable.
146 status = do_select(udp1, udp2, setcount);
147 if (status < 0) {
148 char errbuf[128];
149 pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf));
150 PJ_LOG(1,(THIS_FILE, "...error: %s", errbuf));
151 status=-50; goto on_return;
152 }
153 if (status == 0) {
154 status=-60; goto on_return;
155 }
156
157 if (setcount[READ_FDS] != 1) {
158 status=-70; goto on_return;
159 }
160 if (setcount[WRITE_FDS] != 0) {
161 if (setcount[WRITE_FDS] == 2) {
162 PJ_LOG(3,(THIS_FILE, "...info: system reports writable sockets"));
163 } else {
164 status=-80; goto on_return;
165 }
166 } else {
167 PJ_LOG(3,(THIS_FILE,
168 "...info: system doesn't report writable sockets"));
169 }
170 if (setcount[EXCEPT_FDS] != 0) {
171 status=-90; goto on_return;
172 }
173
174 // Read the socket to clear readable sockets.
175 received = sizeof(buf);
176 rc = pj_sock_recv(udp2, buf, &received, 0);
177 if (rc != PJ_SUCCESS || received != 5) {
178 status=-100; goto on_return;
179 }
180
181 status = 0;
182
183 // Test timeout on the read part.
184 // This won't necessarily return zero, as select() may report that
185 // sockets are writable.
186 setcount[0] = setcount[1] = setcount[2] = 0;
187 status = do_select(udp1, udp2, setcount);
188 if (status != 0 && status != setcount[WRITE_FDS]) {
189 PJ_LOG(3,(THIS_FILE, "...error: expecting timeout but got %d sks set",
190 status));
191 PJ_LOG(3,(THIS_FILE, " rdset: %d, wrset: %d, exset: %d",
192 setcount[0], setcount[1], setcount[2]));
193 status = -110; goto on_return;
194 }
195 if (setcount[READ_FDS] != 0) {
196 PJ_LOG(3,(THIS_FILE, "...error: readable socket not expected"));
197 status = -120; goto on_return;
198 }
199
200 status = 0;
201
202on_return:
203 if (udp1 != PJ_INVALID_SOCKET)
204 pj_sock_close(udp1);
205 if (udp2 != PJ_INVALID_SOCKET)
206 pj_sock_close(udp2);
207 return status;
208}
209
210#else
211/* To prevent warning about "translation unit is empty"
212 * when this test is disabled.
213 */
214int dummy_select_test;
215#endif /* INCLUDE_SELECT_TEST */
216
217