Move TestExtMath to php
[hiphop-php.git] / hphp / test / ext / test_ext_socket.cpp
blobcc89273e79ede859bee6ec43ad3537ea24e992f0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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) {
24 bool ret = true;
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);
53 return ret;
56 // so we run on different range of ports every time
57 static int get_random_port() {
58 static int base = -1;
59 if (base == -1) {
60 base = 12345 + (int)((time(0) * 100) % 30000);
62 return ++base;
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;
70 return 0;
73 ///////////////////////////////////////////////////////////////////////////////
75 bool TestExtSocket::test_socket_create() {
76 VERIFY(!same(f_socket_create(k_AF_INET, k_SOCK_STREAM, k_SOL_TCP), false));
77 return Count(true);
80 bool TestExtSocket::test_socket_create_listen() {
81 int port = get_random_port();
82 f_socket_create_listen(port);
83 return Count(true);
86 bool TestExtSocket::test_socket_create_pair() {
87 Variant fds;
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));
92 return Count(true);
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);
98 return Count(true);
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));
105 Variant address;
106 VERIFY(f_socket_getpeername(s, ref(address)));
108 VERIFY(!address.toString().empty());
109 return Count(true);
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));
116 Variant address;
117 VERIFY(f_socket_getsockname(s, ref(address)));
119 VERIFY(!address.toString().empty());
120 return Count(true);
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));
126 return Count(true);
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));
132 return Count(true);
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)));
139 return Count(true);
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));
145 return Count(true);
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");
151 VERIFY(port != 0);
152 VERIFY(f_socket_listen(s));
153 return Count(true);
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");
159 VERIFY(port != 0);
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");
170 return Count(true);
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");
176 VERIFY(port != 0);
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);
190 return Count(true);
193 bool TestExtSocket::test_socket_server() {
194 Variant server;
195 int port;
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);
214 return Count(true);
217 bool TestExtSocket::test_socket_accept() {
218 // tested with test_socket_listen
219 return Count(true);
222 bool TestExtSocket::test_socket_read() {
223 // tested with test_socket_listen
224 return Count(true);
227 bool TestExtSocket::test_socket_write() {
228 // tested with test_socket_listen
229 return Count(true);
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");
235 VERIFY(port != 0);
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));
245 Variant buffer;
246 VERIFY(f_socket_recv(s, ref(buffer), 100, 0));
247 VS(buffer, "test");
248 return Count(true);
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");
254 VERIFY(port != 0);
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));
264 Variant buffer;
265 Variant name, vport;
266 VERIFY(f_socket_recvfrom(s, ref(buffer), 100, 0, ref(name), ref(vport)));
267 VS(buffer, "test");
268 return Count(true);
271 bool TestExtSocket::test_socket_recv() {
272 // tested with test_socket_send
273 return Count(true);
276 bool TestExtSocket::test_socket_recvfrom() {
277 // tested with test_socket_sendto
278 return Count(true);
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");
284 VERIFY(port != 0);
285 VERIFY(f_socket_listen(s));
286 VERIFY(f_socket_shutdown(s));
287 return Count(true);
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");
293 VERIFY(port != 0);
294 VERIFY(f_socket_listen(s));
295 f_socket_close(s);
296 return Count(true);
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);
307 return Count(true);
310 bool TestExtSocket::test_socket_last_error() {
311 // tested with test_socket_strerror
312 return Count(true);
315 bool TestExtSocket::test_socket_clear_error() {
316 // tested with test_socket_strerror
317 return Count(true);