2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/test/ext/test_ext_socket.h"
18 #include "hphp/runtime/ext/ext_socket.h"
19 #include "hphp/runtime/ext/ext_network.h"
21 ///////////////////////////////////////////////////////////////////////////////
23 bool TestExtSocket::RunTests(const std::string
&which
) {
26 RUN_TEST(test_socket_create
);
27 RUN_TEST(test_socket_create_listen
);
28 RUN_TEST(test_socket_create_pair
);
29 RUN_TEST(test_socket_get_option
);
30 RUN_TEST(test_socket_getpeername
);
31 RUN_TEST(test_socket_getsockname
);
32 RUN_TEST(test_socket_set_block
);
33 RUN_TEST(test_socket_set_nonblock
);
34 RUN_TEST(test_socket_set_option
);
35 RUN_TEST(test_socket_connect
);
36 RUN_TEST(test_socket_bind
);
37 RUN_TEST(test_socket_listen
);
38 RUN_TEST(test_socket_select
);
39 RUN_TEST(test_socket_server
);
40 RUN_TEST(test_socket_accept
);
41 RUN_TEST(test_socket_read
);
42 RUN_TEST(test_socket_write
);
43 RUN_TEST(test_socket_send
);
44 RUN_TEST(test_socket_sendto
);
45 RUN_TEST(test_socket_recv
);
46 RUN_TEST(test_socket_recvfrom
);
47 RUN_TEST(test_socket_shutdown
);
48 RUN_TEST(test_socket_close
);
49 RUN_TEST(test_socket_strerror
);
50 RUN_TEST(test_socket_last_error
);
51 RUN_TEST(test_socket_clear_error
);
56 // so we run on different range of ports every time
57 static int get_random_port() {
60 base
= 12345 + (int)((time(0) * 100) % 30000);
65 static int bind_random_port(CObjRef socket
, CStrRef address
) {
66 for (int i
= 0; i
< 20; i
++) {
67 int port
= get_random_port();
68 if (f_socket_bind(socket
, address
, port
)) return port
;
73 ///////////////////////////////////////////////////////////////////////////////
75 bool TestExtSocket::test_socket_create() {
76 VERIFY(!same(f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
), false));
80 bool TestExtSocket::test_socket_create_listen() {
81 int port
= get_random_port();
82 f_socket_create_listen(port
);
86 bool TestExtSocket::test_socket_create_pair() {
88 VERIFY(f_socket_create_pair(k_AF_UNIX
, k_SOCK_STREAM
, 0, ref(fds
)));
89 VS(fds
.toArray().size(), 2);
90 VERIFY(more(fds
[0], 0));
91 VERIFY(more(fds
[1], 0));
95 bool TestExtSocket::test_socket_get_option() {
96 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
97 VS(f_socket_get_option(s
, k_SOL_SOCKET
, k_SO_TYPE
), k_SOCK_STREAM
);
101 bool TestExtSocket::test_socket_getpeername() {
102 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
103 VERIFY(f_socket_connect(s
, "facebook.com", 80));
106 VERIFY(f_socket_getpeername(s
, ref(address
)));
108 VERIFY(!address
.toString().empty());
112 bool TestExtSocket::test_socket_getsockname() {
113 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
114 VERIFY(f_socket_connect(s
, "facebook.com", 80));
117 VERIFY(f_socket_getsockname(s
, ref(address
)));
119 VERIFY(!address
.toString().empty());
123 bool TestExtSocket::test_socket_set_block() {
124 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
125 VERIFY(f_socket_set_block(s
));
129 bool TestExtSocket::test_socket_set_nonblock() {
130 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
131 VERIFY(f_socket_set_nonblock(s
));
135 bool TestExtSocket::test_socket_set_option() {
136 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
137 VERIFY(f_socket_set_option(s
, k_SOL_SOCKET
, k_SO_RCVTIMEO
,
138 CREATE_MAP2("sec", 1, "usec", 0)));
142 bool TestExtSocket::test_socket_connect() {
143 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
144 VERIFY(f_socket_connect(s
, "facebook.com", 80));
148 bool TestExtSocket::test_socket_bind() {
149 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
150 int port
= bind_random_port(s
, "127.0.0.1");
152 VERIFY(f_socket_listen(s
));
156 bool TestExtSocket::test_socket_listen() {
157 Variant server
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
158 int port
= bind_random_port(server
, "127.0.0.1");
160 VERIFY(f_socket_listen(server
));
162 Variant client
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
163 VERIFY(f_socket_connect(client
, "127.0.0.1", port
));
165 Variant s
= f_socket_accept(server
);
166 VERIFY(f_socket_write(client
, "testing"));
168 // this could fail with shorter returns, but it never does...
169 VS(f_socket_read(s
, 100), "testing");
173 bool TestExtSocket::test_socket_select() {
174 Variant server
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
175 int port
= bind_random_port(server
, "127.0.0.1");
177 VERIFY(f_socket_listen(server
));
179 Variant client
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
180 VERIFY(f_socket_connect(client
, "127.0.0.1", port
));
182 Variant s
= f_socket_accept(server
);
184 Variant reads
= CREATE_VECTOR1(s
);
185 VS(f_socket_select(ref(reads
), uninit_null(), uninit_null(), 1, 0), 0);
187 VERIFY(f_socket_write(client
, "testing"));
188 reads
= CREATE_VECTOR1(s
);
189 VS(f_socket_select(ref(reads
), uninit_null(), uninit_null(), 1, 0), 1);
193 bool TestExtSocket::test_socket_server() {
196 for (int i
= 0; i
< 20; i
++) {
197 port
= get_random_port();
198 server
= f_socket_server("127.0.0.1", port
);
199 if (!same(server
, false)) break;
201 VERIFY(!same(server
, false));
203 Variant client
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
204 VERIFY(f_socket_connect(client
, "127.0.0.1", port
));
206 Variant s
= f_socket_accept(server
);
208 Variant reads
= CREATE_VECTOR1(s
);
209 VS(f_socket_select(ref(reads
), uninit_null(), uninit_null(), 1, 0), 0);
211 VERIFY(f_socket_write(client
, "testing"));
212 reads
= CREATE_VECTOR1(s
);
213 VS(f_socket_select(ref(reads
), uninit_null(), uninit_null(), 1, 0), 1);
217 bool TestExtSocket::test_socket_accept() {
218 // tested with test_socket_listen
222 bool TestExtSocket::test_socket_read() {
223 // tested with test_socket_listen
227 bool TestExtSocket::test_socket_write() {
228 // tested with test_socket_listen
232 bool TestExtSocket::test_socket_send() {
233 Variant server
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
234 int port
= bind_random_port(server
, "127.0.0.1");
236 VERIFY(f_socket_listen(server
));
238 Variant client
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
239 VERIFY(f_socket_connect(client
, "127.0.0.1", port
));
241 Variant s
= f_socket_accept(server
);
242 String text
= "testing";
243 VERIFY(f_socket_send(client
, text
, 4, 0));
246 VERIFY(f_socket_recv(s
, ref(buffer
), 100, 0));
251 bool TestExtSocket::test_socket_sendto() {
252 Variant server
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
253 int port
= bind_random_port(server
, "127.0.0.1");
255 VERIFY(f_socket_listen(server
));
257 Variant client
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
258 VERIFY(f_socket_connect(client
, "127.0.0.1", port
));
260 Variant s
= f_socket_accept(server
);
261 String text
= "testing";
262 VERIFY(f_socket_sendto(client
, text
, 4, 0, "127.0.0.1", port
));
266 VERIFY(f_socket_recvfrom(s
, ref(buffer
), 100, 0, ref(name
), ref(vport
)));
271 bool TestExtSocket::test_socket_recv() {
272 // tested with test_socket_send
276 bool TestExtSocket::test_socket_recvfrom() {
277 // tested with test_socket_sendto
281 bool TestExtSocket::test_socket_shutdown() {
282 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
283 int port
= bind_random_port(s
, "127.0.0.1");
285 VERIFY(f_socket_listen(s
));
286 VERIFY(f_socket_shutdown(s
));
290 bool TestExtSocket::test_socket_close() {
291 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
292 int port
= bind_random_port(s
, "127.0.0.1");
294 VERIFY(f_socket_listen(s
));
299 bool TestExtSocket::test_socket_strerror() {
300 Variant s
= f_socket_create(k_AF_INET
, k_SOCK_STREAM
, k_SOL_TCP
);
301 f_socket_bind(s
, "127.0.0.1", 80);
302 if (f_socket_last_error(s
) == 13) {
303 VS(f_socket_strerror(13), "Permission denied");
304 f_socket_clear_error(s
);
306 VS(f_socket_last_error(s
), 0);
310 bool TestExtSocket::test_socket_last_error() {
311 // tested with test_socket_strerror
315 bool TestExtSocket::test_socket_clear_error() {
316 // tested with test_socket_strerror