PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/gio/tests/socket.c

https://gitlab.com/ImageMagick/glib
C | 1656 lines | 1292 code | 298 blank | 66 comment | 62 complexity | be536f31ed8a7e01190d3f1af8df9dd4 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. /* GLib testing framework examples and tests
  2. *
  3. * Copyright (C) 2008-2011 Red Hat, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General
  16. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <gio/gio.h>
  19. #ifdef G_OS_UNIX
  20. #include <errno.h>
  21. #include <sys/wait.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <gio/gnetworking.h>
  25. #include <gio/gunixconnection.h>
  26. #endif
  27. #include "gnetworkingprivate.h"
  28. static gboolean ipv6_supported;
  29. typedef struct {
  30. GSocket *server;
  31. GSocket *client;
  32. GSocketFamily family;
  33. GThread *thread;
  34. GMainLoop *loop;
  35. GCancellable *cancellable; /* to shut down dgram echo server thread */
  36. } IPTestData;
  37. static gpointer
  38. echo_server_dgram_thread (gpointer user_data)
  39. {
  40. IPTestData *data = user_data;
  41. GSocketAddress *sa;
  42. GCancellable *cancellable = data->cancellable;
  43. GSocket *sock;
  44. GError *error = NULL;
  45. gssize nread, nwrote;
  46. gchar buf[128];
  47. sock = data->server;
  48. while (TRUE)
  49. {
  50. nread = g_socket_receive_from (sock, &sa, buf, sizeof (buf), cancellable, &error);
  51. if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  52. break;
  53. g_assert_no_error (error);
  54. g_assert_cmpint (nread, >=, 0);
  55. nwrote = g_socket_send_to (sock, sa, buf, nread, cancellable, &error);
  56. if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  57. break;
  58. g_assert_no_error (error);
  59. g_assert_cmpint (nwrote, ==, nread);
  60. g_object_unref (sa);
  61. }
  62. g_clear_error (&error);
  63. return NULL;
  64. }
  65. static gpointer
  66. echo_server_thread (gpointer user_data)
  67. {
  68. IPTestData *data = user_data;
  69. GSocket *sock;
  70. GError *error = NULL;
  71. gssize nread, nwrote;
  72. gchar buf[128];
  73. sock = g_socket_accept (data->server, NULL, &error);
  74. g_assert_no_error (error);
  75. while (TRUE)
  76. {
  77. nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
  78. g_assert_no_error (error);
  79. g_assert_cmpint (nread, >=, 0);
  80. if (nread == 0)
  81. break;
  82. nwrote = g_socket_send (sock, buf, nread, NULL, &error);
  83. g_assert_no_error (error);
  84. g_assert_cmpint (nwrote, ==, nread);
  85. }
  86. g_socket_close (sock, &error);
  87. g_assert_no_error (error);
  88. g_object_unref (sock);
  89. return NULL;
  90. }
  91. static IPTestData *
  92. create_server_full (GSocketFamily family,
  93. GSocketType socket_type,
  94. GThreadFunc server_thread,
  95. gboolean v4mapped)
  96. {
  97. IPTestData *data;
  98. GSocket *server;
  99. GError *error = NULL;
  100. GSocketAddress *addr;
  101. GInetAddress *iaddr;
  102. data = g_slice_new (IPTestData);
  103. data->family = family;
  104. data->server = server = g_socket_new (family,
  105. socket_type,
  106. G_SOCKET_PROTOCOL_DEFAULT,
  107. &error);
  108. g_assert_no_error (error);
  109. g_assert_cmpint (g_socket_get_family (server), ==, family);
  110. g_assert_cmpint (g_socket_get_socket_type (server), ==, socket_type);
  111. g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
  112. g_socket_set_blocking (server, TRUE);
  113. #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
  114. if (v4mapped)
  115. {
  116. g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
  117. if (! g_socket_speaks_ipv4 (data->server))
  118. {
  119. g_object_unref (data->server);
  120. g_slice_free (IPTestData, data);
  121. return NULL;
  122. }
  123. }
  124. #endif
  125. if (v4mapped)
  126. iaddr = g_inet_address_new_any (family);
  127. else
  128. iaddr = g_inet_address_new_loopback (family);
  129. addr = g_inet_socket_address_new (iaddr, 0);
  130. g_object_unref (iaddr);
  131. g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
  132. g_socket_bind (server, addr, TRUE, &error);
  133. g_assert_no_error (error);
  134. g_object_unref (addr);
  135. addr = g_socket_get_local_address (server, &error);
  136. g_assert_no_error (error);
  137. g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
  138. g_object_unref (addr);
  139. if (socket_type == G_SOCKET_TYPE_STREAM)
  140. {
  141. g_socket_listen (server, &error);
  142. g_assert_no_error (error);
  143. }
  144. else
  145. {
  146. data->cancellable = g_cancellable_new ();
  147. }
  148. data->thread = g_thread_new ("server", server_thread, data);
  149. return data;
  150. }
  151. static IPTestData *
  152. create_server (GSocketFamily family,
  153. GThreadFunc server_thread,
  154. gboolean v4mapped)
  155. {
  156. return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped);
  157. }
  158. static const gchar *testbuf = "0123456789abcdef";
  159. static gboolean
  160. test_ip_async_read_ready (GSocket *client,
  161. GIOCondition cond,
  162. gpointer user_data)
  163. {
  164. IPTestData *data = user_data;
  165. GError *error = NULL;
  166. gssize len;
  167. gchar buf[128];
  168. g_assert_cmpint (cond, ==, G_IO_IN);
  169. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  170. g_assert_no_error (error);
  171. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  172. g_assert_cmpstr (testbuf, ==, buf);
  173. g_main_loop_quit (data->loop);
  174. return FALSE;
  175. }
  176. static gboolean
  177. test_ip_async_write_ready (GSocket *client,
  178. GIOCondition cond,
  179. gpointer user_data)
  180. {
  181. IPTestData *data = user_data;
  182. GError *error = NULL;
  183. GSource *source;
  184. gssize len;
  185. g_assert_cmpint (cond, ==, G_IO_OUT);
  186. len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
  187. g_assert_no_error (error);
  188. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  189. source = g_socket_create_source (client, G_IO_IN, NULL);
  190. g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
  191. data, NULL);
  192. g_source_attach (source, NULL);
  193. g_source_unref (source);
  194. return FALSE;
  195. }
  196. static gboolean
  197. test_ip_async_timed_out (GSocket *client,
  198. GIOCondition cond,
  199. gpointer user_data)
  200. {
  201. IPTestData *data = user_data;
  202. GError *error = NULL;
  203. GSource *source;
  204. gssize len;
  205. gchar buf[128];
  206. if (data->family == G_SOCKET_FAMILY_IPV4)
  207. {
  208. g_assert_cmpint (cond, ==, G_IO_IN);
  209. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  210. g_assert_cmpint (len, ==, -1);
  211. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
  212. g_clear_error (&error);
  213. }
  214. source = g_socket_create_source (client, G_IO_OUT, NULL);
  215. g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
  216. data, NULL);
  217. g_source_attach (source, NULL);
  218. g_source_unref (source);
  219. return FALSE;
  220. }
  221. static gboolean
  222. test_ip_async_connected (GSocket *client,
  223. GIOCondition cond,
  224. gpointer user_data)
  225. {
  226. IPTestData *data = user_data;
  227. GError *error = NULL;
  228. GSource *source;
  229. gssize len;
  230. gchar buf[128];
  231. g_socket_check_connect_result (client, &error);
  232. g_assert_no_error (error);
  233. /* We do this after the check_connect_result, since that will give a
  234. * more useful assertion in case of error.
  235. */
  236. g_assert_cmpint (cond, ==, G_IO_OUT);
  237. g_assert (g_socket_is_connected (client));
  238. /* This adds 1 second to "make check", so let's just only do it once. */
  239. if (data->family == G_SOCKET_FAMILY_IPV4)
  240. {
  241. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  242. g_assert_cmpint (len, ==, -1);
  243. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
  244. g_clear_error (&error);
  245. source = g_socket_create_source (client, G_IO_IN, NULL);
  246. g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
  247. data, NULL);
  248. g_source_attach (source, NULL);
  249. g_source_unref (source);
  250. }
  251. else
  252. test_ip_async_timed_out (client, 0, data);
  253. return FALSE;
  254. }
  255. static gboolean
  256. idle_test_ip_async_connected (gpointer user_data)
  257. {
  258. IPTestData *data = user_data;
  259. return test_ip_async_connected (data->client, G_IO_OUT, data);
  260. }
  261. static void
  262. test_ip_async (GSocketFamily family)
  263. {
  264. IPTestData *data;
  265. GError *error = NULL;
  266. GSocket *client;
  267. GSocketAddress *addr;
  268. GSource *source;
  269. gssize len;
  270. gchar buf[128];
  271. data = create_server (family, echo_server_thread, FALSE);
  272. addr = g_socket_get_local_address (data->server, &error);
  273. g_assert_no_error (error);
  274. client = g_socket_new (family,
  275. G_SOCKET_TYPE_STREAM,
  276. G_SOCKET_PROTOCOL_DEFAULT,
  277. &error);
  278. g_assert_no_error (error);
  279. data->client = client;
  280. g_assert_cmpint (g_socket_get_family (client), ==, family);
  281. g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
  282. g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
  283. g_socket_set_blocking (client, FALSE);
  284. g_socket_set_timeout (client, 1);
  285. if (g_socket_connect (client, addr, NULL, &error))
  286. {
  287. g_assert_no_error (error);
  288. g_idle_add (idle_test_ip_async_connected, data);
  289. }
  290. else
  291. {
  292. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
  293. g_clear_error (&error);
  294. source = g_socket_create_source (client, G_IO_OUT, NULL);
  295. g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
  296. data, NULL);
  297. g_source_attach (source, NULL);
  298. g_source_unref (source);
  299. }
  300. g_object_unref (addr);
  301. data->loop = g_main_loop_new (NULL, TRUE);
  302. g_main_loop_run (data->loop);
  303. g_main_loop_unref (data->loop);
  304. g_socket_shutdown (client, FALSE, TRUE, &error);
  305. g_assert_no_error (error);
  306. g_thread_join (data->thread);
  307. if (family == G_SOCKET_FAMILY_IPV4)
  308. {
  309. /* Test that reading on a remote-closed socket gets back 0 bytes. */
  310. len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
  311. TRUE, NULL, &error);
  312. g_assert_no_error (error);
  313. g_assert_cmpint (len, ==, 0);
  314. }
  315. else
  316. {
  317. /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
  318. len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
  319. TRUE, NULL, &error);
  320. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
  321. g_assert_cmpint (len, ==, -1);
  322. g_clear_error (&error);
  323. }
  324. g_socket_close (client, &error);
  325. g_assert_no_error (error);
  326. g_socket_close (data->server, &error);
  327. g_assert_no_error (error);
  328. g_object_unref (data->server);
  329. g_object_unref (client);
  330. g_slice_free (IPTestData, data);
  331. }
  332. static void
  333. test_ipv4_async (void)
  334. {
  335. test_ip_async (G_SOCKET_FAMILY_IPV4);
  336. }
  337. static void
  338. test_ipv6_async (void)
  339. {
  340. if (!ipv6_supported)
  341. {
  342. g_test_skip ("No support for IPv6");
  343. return;
  344. }
  345. test_ip_async (G_SOCKET_FAMILY_IPV6);
  346. }
  347. static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  348. static void
  349. test_ip_sync (GSocketFamily family)
  350. {
  351. IPTestData *data;
  352. GError *error = NULL;
  353. GSocket *client;
  354. GSocketAddress *addr;
  355. gssize len;
  356. gchar buf[128];
  357. data = create_server (family, echo_server_thread, FALSE);
  358. addr = g_socket_get_local_address (data->server, &error);
  359. g_assert_no_error (error);
  360. client = g_socket_new (family,
  361. G_SOCKET_TYPE_STREAM,
  362. G_SOCKET_PROTOCOL_DEFAULT,
  363. &error);
  364. g_assert_no_error (error);
  365. g_assert_cmpint (g_socket_get_family (client), ==, family);
  366. g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
  367. g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
  368. g_socket_set_blocking (client, TRUE);
  369. g_socket_set_timeout (client, 1);
  370. g_socket_connect (client, addr, NULL, &error);
  371. g_assert_no_error (error);
  372. g_assert (g_socket_is_connected (client));
  373. g_object_unref (addr);
  374. /* This adds 1 second to "make check", so let's just only do it once. */
  375. if (family == G_SOCKET_FAMILY_IPV4)
  376. {
  377. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  378. g_assert_cmpint (len, ==, -1);
  379. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
  380. g_clear_error (&error);
  381. }
  382. len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
  383. g_assert_no_error (error);
  384. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  385. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  386. g_assert_no_error (error);
  387. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  388. g_assert_cmpstr (testbuf, ==, buf);
  389. {
  390. GOutputVector v[7] = { { NULL, }, };
  391. v[0].buffer = testbuf2 + 0;
  392. v[0].size = 3;
  393. v[1].buffer = testbuf2 + 3;
  394. v[1].size = 5;
  395. v[2].buffer = testbuf2 + 3 + 5;
  396. v[2].size = 0;
  397. v[3].buffer = testbuf2 + 3 + 5;
  398. v[3].size = 6;
  399. v[4].buffer = testbuf2 + 3 + 5 + 6;
  400. v[4].size = 2;
  401. v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
  402. v[5].size = 1;
  403. v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
  404. v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
  405. len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
  406. g_assert_no_error (error);
  407. g_assert_cmpint (len, ==, strlen (testbuf2));
  408. memset (buf, 0, sizeof (buf));
  409. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  410. g_assert_no_error (error);
  411. g_assert_cmpint (len, ==, strlen (testbuf2));
  412. g_assert_cmpstr (testbuf2, ==, buf);
  413. }
  414. g_socket_shutdown (client, FALSE, TRUE, &error);
  415. g_assert_no_error (error);
  416. g_thread_join (data->thread);
  417. if (family == G_SOCKET_FAMILY_IPV4)
  418. {
  419. /* Test that reading on a remote-closed socket gets back 0 bytes. */
  420. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  421. g_assert_no_error (error);
  422. g_assert_cmpint (len, ==, 0);
  423. }
  424. else
  425. {
  426. /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
  427. len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
  428. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
  429. g_assert_cmpint (len, ==, -1);
  430. g_clear_error (&error);
  431. }
  432. g_socket_close (client, &error);
  433. g_assert_no_error (error);
  434. g_socket_close (data->server, &error);
  435. g_assert_no_error (error);
  436. g_object_unref (data->server);
  437. g_object_unref (client);
  438. g_slice_free (IPTestData, data);
  439. }
  440. static void
  441. test_ipv4_sync (void)
  442. {
  443. test_ip_sync (G_SOCKET_FAMILY_IPV4);
  444. }
  445. static void
  446. test_ipv6_sync (void)
  447. {
  448. if (!ipv6_supported)
  449. {
  450. g_test_skip ("No support for IPv6");
  451. return;
  452. }
  453. test_ip_sync (G_SOCKET_FAMILY_IPV6);
  454. }
  455. static void
  456. test_ip_sync_dgram (GSocketFamily family)
  457. {
  458. IPTestData *data;
  459. GError *error = NULL;
  460. GSocket *client;
  461. GSocketAddress *dest_addr;
  462. gssize len;
  463. gchar buf[128];
  464. data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
  465. echo_server_dgram_thread, FALSE);
  466. dest_addr = g_socket_get_local_address (data->server, &error);
  467. client = g_socket_new (family,
  468. G_SOCKET_TYPE_DATAGRAM,
  469. G_SOCKET_PROTOCOL_DEFAULT,
  470. &error);
  471. g_assert_no_error (error);
  472. g_assert_cmpint (g_socket_get_family (client), ==, family);
  473. g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
  474. g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
  475. g_socket_set_blocking (client, TRUE);
  476. g_socket_set_timeout (client, 1);
  477. len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
  478. g_assert_no_error (error);
  479. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  480. len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
  481. g_assert_no_error (error);
  482. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  483. g_assert_cmpstr (testbuf, ==, buf);
  484. {
  485. GOutputMessage m[3] = { { NULL, }, };
  486. GInputMessage im[3] = { { NULL, }, };
  487. GOutputVector v[7] = { { NULL, }, };
  488. GInputVector iv[7] = { { NULL, }, };
  489. v[0].buffer = testbuf2 + 0;
  490. v[0].size = 3;
  491. v[1].buffer = testbuf2 + 3;
  492. v[1].size = 5;
  493. v[2].buffer = testbuf2 + 3 + 5;
  494. v[2].size = 0;
  495. v[3].buffer = testbuf2 + 3 + 5;
  496. v[3].size = 6;
  497. v[4].buffer = testbuf2 + 3 + 5 + 6;
  498. v[4].size = 2;
  499. v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
  500. v[5].size = 1;
  501. v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
  502. v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
  503. iv[0].buffer = buf + 0;
  504. iv[0].size = 3;
  505. iv[1].buffer = buf + 3;
  506. iv[1].size = 5;
  507. iv[2].buffer = buf + 3 + 5;
  508. iv[2].size = 0;
  509. iv[3].buffer = buf + 3 + 5;
  510. iv[3].size = 6;
  511. iv[4].buffer = buf + 3 + 5 + 6;
  512. iv[4].size = 2;
  513. iv[5].buffer = buf + 3 + 5 + 6 + 2;
  514. iv[5].size = 1;
  515. iv[6].buffer = buf + 3 + 5 + 6 + 2 + 1;
  516. iv[6].size = sizeof (buf) - (3 + 5 + 6 + 2 + 1);
  517. len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
  518. g_assert_no_error (error);
  519. g_assert_cmpint (len, ==, strlen (testbuf2));
  520. memset (buf, 0, sizeof (buf));
  521. len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
  522. g_assert_no_error (error);
  523. g_assert_cmpint (len, ==, strlen (testbuf2));
  524. g_assert_cmpstr (testbuf2, ==, buf);
  525. m[0].vectors = &v[0];
  526. m[0].num_vectors = 1;
  527. m[0].address = dest_addr;
  528. m[1].vectors = &v[0];
  529. m[1].num_vectors = 6;
  530. m[1].address = dest_addr;
  531. m[2].vectors = &v[6];
  532. m[2].num_vectors = 1;
  533. m[2].address = dest_addr;
  534. len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
  535. g_assert_no_error (error);
  536. g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
  537. g_assert_cmpint (m[0].bytes_sent, ==, 3);
  538. g_assert_cmpint (m[1].bytes_sent, ==, 17);
  539. g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
  540. memset (buf, 0, sizeof (buf));
  541. len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
  542. g_assert_no_error (error);
  543. g_assert_cmpint (len, ==, 3);
  544. memset (buf, 0, sizeof (buf));
  545. len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
  546. g_assert_no_error (error);
  547. /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
  548. g_assert_cmpint (len, ==, 17);
  549. g_assert (memcmp (testbuf2, buf, 17) == 0);
  550. memset (buf, 0, sizeof (buf));
  551. len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
  552. g_assert_no_error (error);
  553. g_assert_cmpint (len, ==, v[6].size);
  554. g_assert_cmpstr (buf, ==, v[6].buffer);
  555. /* reset since we're re-using the message structs */
  556. m[0].bytes_sent = 0;
  557. m[1].bytes_sent = 0;
  558. m[2].bytes_sent = 0;
  559. /* now try receiving multiple messages */
  560. len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
  561. g_assert_no_error (error);
  562. g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
  563. g_assert_cmpint (m[0].bytes_sent, ==, 3);
  564. g_assert_cmpint (m[1].bytes_sent, ==, 17);
  565. g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
  566. im[0].vectors = &iv[0];
  567. im[0].num_vectors = 1;
  568. im[1].vectors = &iv[0];
  569. im[1].num_vectors = 6;
  570. im[2].vectors = &iv[6];
  571. im[2].num_vectors = 1;
  572. memset (buf, 0, sizeof (buf));
  573. len = g_socket_receive_messages (client, im, G_N_ELEMENTS (im), 0,
  574. NULL, &error);
  575. g_assert_no_error (error);
  576. g_assert_cmpint (len, ==, G_N_ELEMENTS (im));
  577. g_assert_cmpuint (im[0].bytes_received, ==, 3);
  578. /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
  579. g_assert_cmpuint (im[1].bytes_received, ==, 17);
  580. g_assert_cmpuint (im[2].bytes_received, ==, v[6].size);
  581. /* reset since we're re-using the message structs */
  582. m[0].bytes_sent = 0;
  583. m[1].bytes_sent = 0;
  584. m[2].bytes_sent = 0;
  585. /* now try to generate an early return by omitting the destination address on [1] */
  586. m[1].address = NULL;
  587. len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
  588. g_assert_no_error (error);
  589. g_assert_cmpint (len, ==, 1);
  590. g_assert_cmpint (m[0].bytes_sent, ==, 3);
  591. g_assert_cmpint (m[1].bytes_sent, ==, 0);
  592. g_assert_cmpint (m[2].bytes_sent, ==, 0);
  593. /* reset since we're re-using the message structs */
  594. m[0].bytes_sent = 0;
  595. m[1].bytes_sent = 0;
  596. m[2].bytes_sent = 0;
  597. /* now try to generate an error by omitting all destination addresses */
  598. m[0].address = NULL;
  599. m[1].address = NULL;
  600. m[2].address = NULL;
  601. len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
  602. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  603. g_clear_error (&error);
  604. g_assert_cmpint (len, ==, -1);
  605. g_assert_cmpint (m[0].bytes_sent, ==, 0);
  606. g_assert_cmpint (m[1].bytes_sent, ==, 0);
  607. g_assert_cmpint (m[2].bytes_sent, ==, 0);
  608. len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
  609. g_assert_cmpint (len, ==, 3);
  610. }
  611. g_cancellable_cancel (data->cancellable);
  612. g_thread_join (data->thread);
  613. g_socket_close (client, &error);
  614. g_assert_no_error (error);
  615. g_socket_close (data->server, &error);
  616. g_assert_no_error (error);
  617. g_object_unref (data->server);
  618. g_object_unref (data->cancellable);
  619. g_object_unref (client);
  620. g_object_unref (dest_addr);
  621. g_slice_free (IPTestData, data);
  622. }
  623. static void
  624. test_ipv4_sync_dgram (void)
  625. {
  626. test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
  627. }
  628. static void
  629. test_ipv6_sync_dgram (void)
  630. {
  631. if (!ipv6_supported)
  632. {
  633. g_test_skip ("No support for IPv6");
  634. return;
  635. }
  636. test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
  637. }
  638. static gpointer
  639. cancellable_thread_cb (gpointer data)
  640. {
  641. GCancellable *cancellable = data;
  642. g_usleep (0.1 * G_USEC_PER_SEC);
  643. g_cancellable_cancel (cancellable);
  644. g_object_unref (cancellable);
  645. return NULL;
  646. }
  647. static void
  648. test_ip_sync_dgram_timeouts (GSocketFamily family)
  649. {
  650. GError *error = NULL;
  651. GSocket *client = NULL;
  652. GCancellable *cancellable = NULL;
  653. GThread *cancellable_thread = NULL;
  654. gssize len;
  655. client = g_socket_new (family,
  656. G_SOCKET_TYPE_DATAGRAM,
  657. G_SOCKET_PROTOCOL_DEFAULT,
  658. &error);
  659. g_assert_no_error (error);
  660. g_assert_cmpint (g_socket_get_family (client), ==, family);
  661. g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
  662. g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
  663. /* No overall timeout: test the per-operation timeouts instead. */
  664. g_socket_set_timeout (client, 0);
  665. cancellable = g_cancellable_new ();
  666. /* Check for timeouts when no server is running. */
  667. {
  668. gint64 start_time;
  669. GInputMessage im = { NULL, };
  670. GInputVector iv = { NULL, };
  671. guint8 buf[128];
  672. iv.buffer = buf;
  673. iv.size = sizeof (buf);
  674. im.vectors = &iv;
  675. im.num_vectors = 1;
  676. memset (buf, 0, sizeof (buf));
  677. /* Try a non-blocking read. */
  678. g_socket_set_blocking (client, FALSE);
  679. len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
  680. NULL, &error);
  681. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
  682. g_assert_cmpint (len, ==, -1);
  683. g_clear_error (&error);
  684. /* Try a timeout read. Can’t really validate the time taken more than
  685. * checking it’s positive. */
  686. g_socket_set_timeout (client, 1);
  687. g_socket_set_blocking (client, TRUE);
  688. start_time = g_get_monotonic_time ();
  689. len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
  690. NULL, &error);
  691. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
  692. g_assert_cmpint (len, ==, -1);
  693. g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
  694. g_clear_error (&error);
  695. /* Try a blocking read, cancelled from another thread. */
  696. g_socket_set_timeout (client, 0);
  697. cancellable_thread = g_thread_new ("cancellable",
  698. cancellable_thread_cb,
  699. g_object_ref (cancellable));
  700. start_time = g_get_monotonic_time ();
  701. len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
  702. cancellable, &error);
  703. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
  704. g_assert_cmpint (len, ==, -1);
  705. g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
  706. g_clear_error (&error);
  707. g_thread_join (cancellable_thread);
  708. }
  709. g_socket_close (client, &error);
  710. g_assert_no_error (error);
  711. g_object_unref (client);
  712. g_object_unref (cancellable);
  713. }
  714. static void
  715. test_ipv4_sync_dgram_timeouts (void)
  716. {
  717. test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
  718. }
  719. static void
  720. test_ipv6_sync_dgram_timeouts (void)
  721. {
  722. if (!ipv6_supported)
  723. {
  724. g_test_skip ("No support for IPv6");
  725. return;
  726. }
  727. test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
  728. }
  729. static gpointer
  730. graceful_server_thread (gpointer user_data)
  731. {
  732. IPTestData *data = user_data;
  733. GSocket *sock;
  734. GError *error = NULL;
  735. gssize len;
  736. sock = g_socket_accept (data->server, NULL, &error);
  737. g_assert_no_error (error);
  738. len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
  739. g_assert_no_error (error);
  740. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  741. return sock;
  742. }
  743. static void
  744. test_close_graceful (void)
  745. {
  746. GSocketFamily family = G_SOCKET_FAMILY_IPV4;
  747. IPTestData *data;
  748. GError *error = NULL;
  749. GSocket *client, *server;
  750. GSocketAddress *addr;
  751. gssize len;
  752. gchar buf[128];
  753. data = create_server (family, graceful_server_thread, FALSE);
  754. addr = g_socket_get_local_address (data->server, &error);
  755. g_assert_no_error (error);
  756. client = g_socket_new (family,
  757. G_SOCKET_TYPE_STREAM,
  758. G_SOCKET_PROTOCOL_DEFAULT,
  759. &error);
  760. g_assert_no_error (error);
  761. g_assert_cmpint (g_socket_get_family (client), ==, family);
  762. g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
  763. g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
  764. g_socket_set_blocking (client, TRUE);
  765. g_socket_set_timeout (client, 1);
  766. g_socket_connect (client, addr, NULL, &error);
  767. g_assert_no_error (error);
  768. g_assert (g_socket_is_connected (client));
  769. g_object_unref (addr);
  770. server = g_thread_join (data->thread);
  771. /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
  772. g_socket_shutdown (server, FALSE, TRUE, &error);
  773. g_assert_no_error (error);
  774. /* we must timeout */
  775. g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
  776. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
  777. g_clear_error (&error);
  778. /* check that the remaining data is received */
  779. len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
  780. g_assert_no_error (error);
  781. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  782. /* and only then the connection is closed */
  783. len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
  784. g_assert_no_error (error);
  785. g_assert_cmpint (len, ==, 0);
  786. g_socket_close (server, &error);
  787. g_assert_no_error (error);
  788. g_socket_close (client, &error);
  789. g_assert_no_error (error);
  790. g_object_unref (server);
  791. g_object_unref (data->server);
  792. g_object_unref (client);
  793. g_slice_free (IPTestData, data);
  794. }
  795. #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
  796. static gpointer
  797. v4mapped_server_thread (gpointer user_data)
  798. {
  799. IPTestData *data = user_data;
  800. GSocket *sock;
  801. GError *error = NULL;
  802. GSocketAddress *addr;
  803. sock = g_socket_accept (data->server, NULL, &error);
  804. g_assert_no_error (error);
  805. g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
  806. addr = g_socket_get_local_address (sock, &error);
  807. g_assert_no_error (error);
  808. g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
  809. g_object_unref (addr);
  810. addr = g_socket_get_remote_address (sock, &error);
  811. g_assert_no_error (error);
  812. g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
  813. g_object_unref (addr);
  814. g_socket_close (sock, &error);
  815. g_assert_no_error (error);
  816. g_object_unref (sock);
  817. return NULL;
  818. }
  819. static void
  820. test_ipv6_v4mapped (void)
  821. {
  822. IPTestData *data;
  823. GError *error = NULL;
  824. GSocket *client;
  825. GSocketAddress *addr, *v4addr;
  826. GInetAddress *iaddr;
  827. if (!ipv6_supported)
  828. {
  829. g_test_skip ("No support for IPv6");
  830. return;
  831. }
  832. data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
  833. if (data == NULL)
  834. {
  835. g_test_message ("Test not run: not supported by the OS");
  836. return;
  837. }
  838. client = g_socket_new (G_SOCKET_FAMILY_IPV4,
  839. G_SOCKET_TYPE_STREAM,
  840. G_SOCKET_PROTOCOL_DEFAULT,
  841. &error);
  842. g_assert_no_error (error);
  843. g_socket_set_blocking (client, TRUE);
  844. g_socket_set_timeout (client, 1);
  845. addr = g_socket_get_local_address (data->server, &error);
  846. g_assert_no_error (error);
  847. iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
  848. v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
  849. g_object_unref (iaddr);
  850. g_object_unref (addr);
  851. g_socket_connect (client, v4addr, NULL, &error);
  852. g_assert_no_error (error);
  853. g_assert (g_socket_is_connected (client));
  854. g_thread_join (data->thread);
  855. g_socket_close (client, &error);
  856. g_assert_no_error (error);
  857. g_socket_close (data->server, &error);
  858. g_assert_no_error (error);
  859. g_object_unref (data->server);
  860. g_object_unref (client);
  861. g_object_unref (v4addr);
  862. g_slice_free (IPTestData, data);
  863. }
  864. #endif
  865. static void
  866. test_timed_wait (void)
  867. {
  868. IPTestData *data;
  869. GError *error = NULL;
  870. GSocket *client;
  871. GSocketAddress *addr;
  872. gint64 start_time;
  873. gint poll_duration;
  874. data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
  875. addr = g_socket_get_local_address (data->server, &error);
  876. g_assert_no_error (error);
  877. client = g_socket_new (G_SOCKET_FAMILY_IPV4,
  878. G_SOCKET_TYPE_STREAM,
  879. G_SOCKET_PROTOCOL_DEFAULT,
  880. &error);
  881. g_assert_no_error (error);
  882. g_socket_set_blocking (client, TRUE);
  883. g_socket_set_timeout (client, 1);
  884. g_socket_connect (client, addr, NULL, &error);
  885. g_assert_no_error (error);
  886. g_object_unref (addr);
  887. start_time = g_get_monotonic_time ();
  888. g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
  889. NULL, &error);
  890. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
  891. g_clear_error (&error);
  892. poll_duration = g_get_monotonic_time () - start_time;
  893. g_assert_cmpint (poll_duration, >=, 98000);
  894. g_assert_cmpint (poll_duration, <, 112000);
  895. g_socket_close (client, &error);
  896. g_assert_no_error (error);
  897. g_thread_join (data->thread);
  898. g_socket_close (data->server, &error);
  899. g_assert_no_error (error);
  900. g_object_unref (data->server);
  901. g_object_unref (client);
  902. g_slice_free (IPTestData, data);
  903. }
  904. static int
  905. duplicate_fd (int fd)
  906. {
  907. #ifdef G_OS_WIN32
  908. HANDLE newfd;
  909. if (!DuplicateHandle (GetCurrentProcess (),
  910. (HANDLE)fd,
  911. GetCurrentProcess (),
  912. &newfd,
  913. 0,
  914. FALSE,
  915. DUPLICATE_SAME_ACCESS))
  916. {
  917. return -1;
  918. }
  919. return (int)newfd;
  920. #else
  921. return dup (fd);
  922. #endif
  923. }
  924. static void
  925. test_fd_reuse (void)
  926. {
  927. IPTestData *data;
  928. GError *error = NULL;
  929. GSocket *client;
  930. GSocket *client2;
  931. GSocketAddress *addr;
  932. int fd;
  933. gssize len;
  934. gchar buf[128];
  935. g_test_bug ("741707");
  936. data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
  937. addr = g_socket_get_local_address (data->server, &error);
  938. g_assert_no_error (error);
  939. client = g_socket_new (G_SOCKET_FAMILY_IPV4,
  940. G_SOCKET_TYPE_STREAM,
  941. G_SOCKET_PROTOCOL_DEFAULT,
  942. &error);
  943. g_assert_no_error (error);
  944. g_socket_set_blocking (client, TRUE);
  945. g_socket_set_timeout (client, 1);
  946. g_socket_connect (client, addr, NULL, &error);
  947. g_assert_no_error (error);
  948. g_assert (g_socket_is_connected (client));
  949. g_object_unref (addr);
  950. /* we have to dup otherwise the fd gets closed twice on unref */
  951. fd = duplicate_fd (g_socket_get_fd (client));
  952. client2 = g_socket_new_from_fd (fd, &error);
  953. g_assert_no_error (error);
  954. g_assert_cmpint (g_socket_get_family (client2), ==, g_socket_get_family (client));
  955. g_assert_cmpint (g_socket_get_socket_type (client2), ==, g_socket_get_socket_type (client));
  956. g_assert_cmpint (g_socket_get_protocol (client2), ==, G_SOCKET_PROTOCOL_TCP);
  957. len = g_socket_send (client2, testbuf, strlen (testbuf) + 1, NULL, &error);
  958. g_assert_no_error (error);
  959. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  960. len = g_socket_receive (client2, buf, sizeof (buf), NULL, &error);
  961. g_assert_no_error (error);
  962. g_assert_cmpint (len, ==, strlen (testbuf) + 1);
  963. g_assert_cmpstr (testbuf, ==, buf);
  964. g_socket_shutdown (client, FALSE, TRUE, &error);
  965. g_assert_no_error (error);
  966. /* The semantics of dup()+shutdown() are ambiguous; this call will succeed
  967. * on Linux, but return ENOTCONN on OS X.
  968. */
  969. g_socket_shutdown (client2, FALSE, TRUE, NULL);
  970. g_thread_join (data->thread);
  971. g_socket_close (client, &error);
  972. g_assert_no_error (error);
  973. g_socket_close (client2, &error);
  974. g_assert_no_error (error);
  975. g_socket_close (data->server, &error);
  976. g_assert_no_error (error);
  977. g_assert_cmpint (g_socket_get_fd (client), ==, -1);
  978. g_assert_cmpint (g_socket_get_fd (client2), ==, -1);
  979. g_assert_cmpint (g_socket_get_fd (data->server), ==, -1);
  980. g_object_unref (data->server);
  981. g_object_unref (client);
  982. g_object_unref (client2);
  983. g_slice_free (IPTestData, data);
  984. }
  985. static void
  986. test_sockaddr (void)
  987. {
  988. struct sockaddr_in6 sin6, gsin6;
  989. GSocketAddress *saddr;
  990. GInetSocketAddress *isaddr;
  991. GInetAddress *iaddr;
  992. GError *error = NULL;
  993. memset (&sin6, 0, sizeof (sin6));
  994. sin6.sin6_family = AF_INET6;
  995. sin6.sin6_addr = in6addr_loopback;
  996. sin6.sin6_port = g_htons (42);
  997. sin6.sin6_scope_id = 17;
  998. sin6.sin6_flowinfo = 1729;
  999. saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
  1000. g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
  1001. isaddr = G_INET_SOCKET_ADDRESS (saddr);
  1002. iaddr = g_inet_socket_address_get_address (isaddr);
  1003. g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
  1004. g_assert (g_inet_address_get_is_loopback (iaddr));
  1005. g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
  1006. g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
  1007. g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
  1008. g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
  1009. g_assert_no_error (error);
  1010. g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
  1011. g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
  1012. g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
  1013. g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
  1014. g_object_unref (saddr);
  1015. }
  1016. #ifdef G_OS_UNIX
  1017. static void
  1018. test_unix_from_fd (void)
  1019. {
  1020. gint fd;
  1021. GError *error;
  1022. GSocket *s;
  1023. fd = socket (AF_UNIX, SOCK_STREAM, 0);
  1024. g_assert_cmpint (fd, !=, -1);
  1025. error = NULL;
  1026. s = g_socket_new_from_fd (fd, &error);
  1027. g_assert_no_error (error);
  1028. g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
  1029. g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
  1030. g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
  1031. g_object_unref (s);
  1032. }
  1033. static void
  1034. test_unix_connection (void)
  1035. {
  1036. gint fd;
  1037. GError *error;
  1038. GSocket *s;
  1039. GSocketConnection *c;
  1040. fd = socket (AF_UNIX, SOCK_STREAM, 0);
  1041. g_assert_cmpint (fd, !=, -1);
  1042. error = NULL;
  1043. s = g_socket_new_from_fd (fd, &error);
  1044. g_assert_no_error (error);
  1045. c = g_socket_connection_factory_create_connection (s);
  1046. g_assert (G_IS_UNIX_CONNECTION (c));
  1047. g_object_unref (c);
  1048. g_object_unref (s);
  1049. }
  1050. static GSocketConnection *
  1051. create_connection_for_fd (int fd)
  1052. {
  1053. GError *err = NULL;
  1054. GSocket *socket;
  1055. GSocketConnection *connection;
  1056. socket = g_socket_new_from_fd (fd, &err);
  1057. g_assert_no_error (err);
  1058. g_assert (G_IS_SOCKET (socket));
  1059. connection = g_socket_connection_factory_create_connection (socket);
  1060. g_assert (G_IS_UNIX_CONNECTION (connection));
  1061. g_object_unref (socket);
  1062. return connection;
  1063. }
  1064. #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
  1065. static void
  1066. test_unix_connection_ancillary_data (void)
  1067. {
  1068. GError *err = NULL;
  1069. gint pv[2], sv[3];
  1070. gint status, fd, len;
  1071. char buffer[1024];
  1072. pid_t pid;
  1073. status = pipe (pv);
  1074. g_assert_cmpint (status, ==, 0);
  1075. status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
  1076. g_assert_cmpint (status, ==, 0);
  1077. pid = fork ();
  1078. g_assert_cmpint (pid, >=, 0);
  1079. /* Child: close its copy of the write end of the pipe, receive it
  1080. * again from the parent over the socket, and write some text to it.
  1081. *
  1082. * Parent: send the write end of the pipe (still open for the
  1083. * parent) over the socket, close it, and read some text from the
  1084. * read end of the pipe.
  1085. */
  1086. if (pid == 0)
  1087. {
  1088. GSocketConnection *connection;
  1089. close (sv[1]);
  1090. connection = create_connection_for_fd (sv[0]);
  1091. status = close (pv[1]);
  1092. g_assert_cmpint (status, ==, 0);
  1093. err = NULL;
  1094. fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
  1095. &err);
  1096. g_assert_no_error (err);
  1097. g_assert_cmpint (fd, >, -1);
  1098. g_object_unref (connection);
  1099. do
  1100. len = write (fd, TEST_DATA, sizeof (TEST_DATA));
  1101. while (len == -1 && errno == EINTR);
  1102. g_assert_cmpint (len, ==, sizeof (TEST_DATA));
  1103. exit (0);
  1104. }
  1105. else
  1106. {
  1107. GSocketConnection *connection;
  1108. close (sv[0]);
  1109. connection = create_connection_for_fd (sv[1]);
  1110. err = NULL;
  1111. g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
  1112. &err);
  1113. g_assert_no_error (err);
  1114. g_object_unref (connection);
  1115. status = close (pv[1]);
  1116. g_assert_cmpint (status, ==, 0);
  1117. memset (buffer, 0xff, sizeof buffer);
  1118. do
  1119. len = read (pv[0], buffer, sizeof buffer);
  1120. while (len == -1 && errno == EINTR);
  1121. g_assert_cmpint (len, ==, sizeof (TEST_DATA));
  1122. g_assert_cmpstr (buffer, ==, TEST_DATA);
  1123. waitpid (pid, &status, 0);
  1124. g_assert (WIFEXITED (status));
  1125. g_assert_cmpint (WEXITSTATUS (status), ==, 0);
  1126. }
  1127. /* TODO: add test for g_unix_connection_send_credentials() and
  1128. * g_unix_connection_receive_credentials().
  1129. */
  1130. }
  1131. #endif /* G_OS_UNIX */
  1132. static void
  1133. test_reuse_tcp (void)
  1134. {
  1135. GSocket *sock1, *sock2;
  1136. GError *error = NULL;
  1137. GInetAddress *iaddr;
  1138. GSocketAddress *addr;
  1139. sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
  1140. G_SOCKET_TYPE_STREAM,
  1141. G_SOCKET_PROTOCOL_DEFAULT,
  1142. &error);
  1143. g_assert_no_error (error);
  1144. iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
  1145. addr = g_inet_socket_address_new (iaddr, 0);
  1146. g_object_unref (iaddr);
  1147. g_socket_bind (sock1, addr, TRUE, &error);
  1148. g_object_unref (addr);
  1149. g_assert_no_error (error);
  1150. g_socket_listen (sock1, &error);
  1151. g_assert_no_error (error);
  1152. sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
  1153. G_SOCKET_TYPE_STREAM,
  1154. G_SOCKET_PROTOCOL_DEFAULT,
  1155. &error);
  1156. g_assert_no_error (error);
  1157. addr = g_socket_get_local_address (sock1, &error);
  1158. g_assert_no_error (error);
  1159. g_socket_bind (sock2, addr, TRUE, &error);
  1160. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
  1161. g_clear_error (&error);
  1162. g_object_unref (addr);
  1163. g_object_unref (sock1);
  1164. g_object_unref (sock2);
  1165. }
  1166. static void
  1167. test_reuse_udp (void)
  1168. {
  1169. GSocket *sock1, *sock2;
  1170. GError *error = NULL;
  1171. GInetAddress *iaddr;
  1172. GSocketAddress *addr;
  1173. sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
  1174. G_SOCKET_TYPE_DATAGRAM,
  1175. G_SOCKET_PROTOCOL_DEFAULT,
  1176. &error);
  1177. g_assert_no_error (error);
  1178. iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
  1179. addr = g_inet_socket_address_new (iaddr, 0);
  1180. g_object_unref (iaddr);
  1181. g_socket_bind (sock1, addr, TRUE, &error);
  1182. g_object_unref (addr);
  1183. g_assert_no_error (error);
  1184. sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
  1185. G_SOCKET_TYPE_DATAGRAM,
  1186. G_SOCKET_PROTOCOL_DEFAULT,
  1187. &error);
  1188. g_assert_no_error (error);
  1189. addr = g_socket_get_local_address (sock1, &error);
  1190. g_assert_no_error (error);
  1191. g_socket_bind (sock2, addr, TRUE, &error);
  1192. g_object_unref (addr);
  1193. g_assert_no_error (error);
  1194. g_object_unref (sock1);
  1195. g_object_unref (sock2);
  1196. }
  1197. static void
  1198. test_get_available (gconstpointer user_data)
  1199. {
  1200. GSocketType socket_type = GPOINTER_TO_UINT (user_data);
  1201. GError *err = NULL;
  1202. GSocket *listener, *server, *client;
  1203. GInetAddress *addr;
  1204. GSocketAddress *saddr;
  1205. gchar data[] = "0123456789abcdef";
  1206. gchar buf[34];
  1207. gssize nread;
  1208. listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
  1209. socket_type,
  1210. G_SOCKET_PROTOCOL_DEFAULT,
  1211. &err);
  1212. g_assert_no_error (err);
  1213. g_assert (G_IS_SOCKET (listener));
  1214. client = g_socket_new (G_SOCKET_FAMILY_IPV4,
  1215. socket_type,
  1216. G_SOCKET_PROTOCOL_DEFAULT,
  1217. &err);
  1218. g_assert_no_error (err);
  1219. g_assert (G_IS_SOCKET (client));
  1220. if (socket_type == G_SOCKET_TYPE_STREAM)
  1221. {
  1222. g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
  1223. g_assert_no_error (err);
  1224. }
  1225. addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
  1226. saddr = g_inet_socket_address_new (addr, 0);
  1227. g_socket_bind (listener, saddr, TRUE, &err);
  1228. g_assert_no_error (err);
  1229. g_object_unref (saddr);
  1230. g_object_unref (addr);
  1231. saddr = g_socket_get_local_address (listener, &err);
  1232. g_assert_no_error (err);
  1233. if (socket_type == G_SOCKET_TYPE_STREAM)
  1234. {
  1235. g_socket_listen (listener, &err);
  1236. g_assert_no_error (err);
  1237. g_socket_connect (client, saddr, NULL, &err);
  1238. g_assert_no_error (err);
  1239. server = g_socket_accept (listener, NULL, &err);
  1240. g_assert_no_error (err);
  1241. g_socket_set_blocking (server, FALSE);
  1242. g_object_unref (listener);
  1243. }
  1244. else
  1245. server = listener;
  1246. g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
  1247. g_assert_no_error (err);
  1248. while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
  1249. ;
  1250. g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
  1251. g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
  1252. g_assert_no_error (err);
  1253. /* We need to wait until the data has actually been copied into the
  1254. * server socket's buffers, but g_socket_condition_wait() won't help
  1255. * here since the socket is definitely already readable. So there's
  1256. * a race condition in checking its available bytes. In the TCP
  1257. * case, we poll for a bit until the new data shows up. In the UDP
  1258. * case, there's not much we can do, but at least the failure mode
  1259. * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
  1260. */
  1261. if (socket_type == G_SOCKET_TYPE_STREAM)
  1262. {
  1263. int tries;
  1264. for (tries = 0; tries < 100; tries++)
  1265. {
  1266. if (g_socket_get_available_bytes (server) > sizeof (data))
  1267. break;
  1268. g_usleep (100000);
  1269. }
  1270. g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
  1271. }
  1272. else
  1273. {
  1274. g_usleep (100000);
  1275. g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
  1276. }
  1277. g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
  1278. nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
  1279. g_assert_no_error (err);
  1280. if (socket_type == G_SOCKET_TYPE_STREAM)
  1281. {
  1282. g_assert_cmpint (nread, ==, 2 * sizeof (data));
  1283. g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
  1284. }
  1285. else
  1286. {
  1287. g_assert_cmpint (nread, ==, sizeof (data));
  1288. g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
  1289. }
  1290. nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
  1291. if (socket_type == G_SOCKET_TYPE_STREAM)
  1292. {
  1293. g_assert_cmpint (nread, ==, -1);
  1294. g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
  1295. g_clear_error (&err);
  1296. }
  1297. else
  1298. {
  1299. g_assert_cmpint (nread, ==, sizeof (data));
  1300. g_assert_no_error (err);
  1301. }
  1302. g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
  1303. g_socket_close (server, &err);
  1304. g_assert_no_error (err);
  1305. g_object_unref (saddr);
  1306. g_object_unref (server);
  1307. g_object_unref (client);
  1308. }
  1309. int
  1310. main (int argc,
  1311. char *argv[])
  1312. {
  1313. GSocket *sock;
  1314. GError *error = NULL;
  1315. g_test_init (&argc, &argv, NULL);
  1316. g_test_bug_base ("https://bugzilla.gnome.org/");
  1317. sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
  1318. G_SOCKET_TYPE_STREAM,
  1319. G_SOCKET_PROTOCOL_DEFAULT,
  1320. &error);
  1321. if (sock != NULL)
  1322. {
  1323. ipv6_supported = TRUE;
  1324. g_object_unref (sock);
  1325. }
  1326. else
  1327. {
  1328. g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
  1329. g_clear_error (&error);
  1330. }
  1331. g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
  1332. g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
  1333. g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
  1334. g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
  1335. g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
  1336. g_test_add_func ("/socket/ipv4_sync/datagram/timeouts", test_ipv4_sync_dgram_timeouts);
  1337. g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
  1338. g_test_add_func ("/socket/ipv6_sync/datagram/timeouts", test_ipv6_sync_dgram_timeouts);
  1339. #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
  1340. g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
  1341. #endif
  1342. g_test_add_func ("/socket/close_graceful", test_close_graceful);
  1343. g_test_add_func ("/socket/timed_wait", test_timed_wait);
  1344. g_test_add_func ("/socket/fd_reuse", test_fd_reuse);
  1345. g_test_add_func ("/socket/address", test_sockaddr);
  1346. #ifdef G_OS_UNIX
  1347. g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
  1348. g_test_add_func ("/socket/unix-connection", test_unix_connection);
  1349. g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
  1350. #endif
  1351. g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
  1352. g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
  1353. g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
  1354. test_get_available);
  1355. g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
  1356. test_get_available);
  1357. return g_test_run();
  1358. }