Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/select.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 "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
44 enum
45 {
46 READ_FDS,
47 WRITE_FDS,
48 EXCEPT_FDS
49 };
50
51 #define THIS_FILE "select_test"
52
53 /*
54 * do_select()
55 *
56 * Perform pj_sock_select() and find out which sockets
57 * are signalled.
58 */
59 static int do_select( pj_sock_t sock1, pj_sock_t sock2,
60 int setcount[])
61 {
62 pj_fd_set_t fds[3];
63 pj_time_val timeout;
64 int i, n, nfds;
65
66 for (i=0; i<3; ++i) {
67 PJ_FD_ZERO(&fds[i]);
68 PJ_FD_SET(sock1, &fds[i]);
69 PJ_FD_SET(sock2, &fds[i]);
70 setcount[i] = 0;
71 }
72
73 timeout.sec = 5;
74 timeout.msec = 0;
75
76 nfds = (int)(sock1 > sock2 ? sock1 : sock2) + 1;
77 n = pj_sock_select(nfds, &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 */
99 int 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 int addrlen;
112
113 PJ_LOG(3, (THIS_FILE, "...Testing simple UDP select()"));
114
115 /* Create two UDP sockets. */
116 rc = pj_sock_socket( pj_AF_INET(), pj_SOCK_DGRAM(), 0, &udp1);
117 if (rc != PJ_SUCCESS) {
118 app_perror("...error: unable to create socket", rc);
119 status=-10; goto on_return;
120 }
121 rc = pj_sock_socket( pj_AF_INET(), pj_SOCK_DGRAM(), 0, &udp2);
122 if (rc != PJ_SUCCESS) {
123 app_perror("...error: unable to create socket", rc);
124 status=-20; goto on_return;
125 }
126
127 /* Bind to an ephemeral port to avoid conflicts with other tests. */
128 pj_bzero(&udp_addr, sizeof(udp_addr));
129 udp_addr.sin_family = pj_AF_INET();
130 udp_addr.sin_port = 0;
131 udp_addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
132
133 rc = pj_sock_bind(udp2, &udp_addr, sizeof(udp_addr));
134 if (rc != PJ_SUCCESS) {
135 app_perror("...error: unable to bind socket", rc);
136 status=-30; goto on_return;
137 }
138
139 /* Get the assigned port. */
140 addrlen = sizeof(udp_addr);
141 rc = pj_sock_getsockname(udp2, &udp_addr, &addrlen);
142 if (rc != PJ_SUCCESS) {
143 app_perror("...error: unable to get socket name", rc);
144 status=-35; goto on_return;
145 }
146
147 /* Send data. */
148 sent = datalen;
149 rc = pj_sock_sendto(udp1, data, &sent, 0, &udp_addr, sizeof(udp_addr));
150 if (rc != PJ_SUCCESS || sent != datalen) {
151 app_perror("...error: sendto() error", rc);
152 status=-40; goto on_return;
153 }
154
155 /* Sleep a bit to let the UDP packet arrive via loopback before
156 * calling select(). See https://github.com/pjsip/pjproject/issues/890
157 */
158 pj_thread_sleep(10);
159
160 /* Check that socket is marked as readable.
161 * Note that select() may also report that sockets are writable.
162 */
163 status = do_select(udp1, udp2, setcount);
164 if (status < 0) {
165 char errbuf[128];
166 pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf));
167 PJ_LOG(1,(THIS_FILE, "...error: %s", errbuf));
168 status=-50; goto on_return;
169 }
170 if (status == 0) {
171 status=-60; goto on_return;
172 }
173
174 if (setcount[READ_FDS] != 1) {
175 status=-70; goto on_return;
176 }
177 if (setcount[WRITE_FDS] != 0) {
178 if (setcount[WRITE_FDS] == 2) {
179 PJ_LOG(3,(THIS_FILE, "...info: system reports writable sockets"));
180 } else {
181 status=-80; goto on_return;
182 }
183 } else {
184 PJ_LOG(3,(THIS_FILE,
185 "...info: system doesn't report writable sockets"));
186 }
187 if (setcount[EXCEPT_FDS] != 0) {
188 status=-90; goto on_return;
189 }
190
191 /* Read the socket to clear readable sockets. */
192 received = sizeof(buf);
193 rc = pj_sock_recv(udp2, buf, &received, 0);
194 if (rc != PJ_SUCCESS || received != 5) {
195 status=-100; goto on_return;
196 }
197
198 status = 0;
199
200 /* Test timeout on the read part.
201 * This won't necessarily return zero, as select() may report that
202 * sockets are writable.
203 */
204 setcount[0] = setcount[1] = setcount[2] = 0;
205 status = do_select(udp1, udp2, setcount);
206 if (status != 0 && status != setcount[WRITE_FDS]) {
207 PJ_LOG(3,(THIS_FILE, "...error: expecting timeout but got %d sks set",
208 status));
209 PJ_LOG(3,(THIS_FILE, " rdset: %d, wrset: %d, exset: %d",
210 setcount[0], setcount[1], setcount[2]));
211 status = -110; goto on_return;
212 }
213 if (setcount[READ_FDS] != 0) {
214 PJ_LOG(3,(THIS_FILE, "...error: readable socket not expected"));
215 status = -120; goto on_return;
216 }
217
218 status = 0;
219
220 on_return:
221 if (udp1 != PJ_INVALID_SOCKET)
222 pj_sock_close(udp1);
223 if (udp2 != PJ_INVALID_SOCKET)
224 pj_sock_close(udp2);
225 return status;
226 }
227
228 #else
229 /* To prevent warning about "translation unit is empty"
230 * when this test is disabled.
231 */
232 int dummy_select_test;
233 #endif /* INCLUDE_SELECT_TEST */
234
235