PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/gio/tests/socket-listener.c

https://gitlab.com/ImageMagick/glib
C | 95 lines | 62 code | 15 blank | 18 comment | 2 complexity | 98eced59cd5760701bb44b7ea12aed6d MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. /* GLib testing framework examples and tests
  2. *
  3. * Copyright 2014 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 Public
  16. * License along with this library; if not, see
  17. * <http://www.gnu.org/licenses/>.
  18. */
  19. #include <gio/gio.h>
  20. static void
  21. event_cb (GSocketListener *listener,
  22. GSocketListenerEvent event,
  23. GSocket *socket,
  24. gpointer data)
  25. {
  26. static GSocketListenerEvent expected_event = G_SOCKET_LISTENER_BINDING;
  27. gboolean *success = (gboolean *)data;
  28. g_assert (G_IS_SOCKET_LISTENER (listener));
  29. g_assert (G_IS_SOCKET (socket));
  30. g_assert (event == expected_event);
  31. switch (event)
  32. {
  33. case G_SOCKET_LISTENER_BINDING:
  34. expected_event = G_SOCKET_LISTENER_BOUND;
  35. break;
  36. case G_SOCKET_LISTENER_BOUND:
  37. expected_event = G_SOCKET_LISTENER_LISTENING;
  38. break;
  39. case G_SOCKET_LISTENER_LISTENING:
  40. expected_event = G_SOCKET_LISTENER_LISTENED;
  41. break;
  42. case G_SOCKET_LISTENER_LISTENED:
  43. *success = TRUE;
  44. break;
  45. }
  46. }
  47. static void
  48. test_event_signal (void)
  49. {
  50. gboolean success = FALSE;
  51. GInetAddress *iaddr;
  52. GSocketAddress *saddr;
  53. GSocketListener *listener;
  54. GError *error = NULL;
  55. iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
  56. saddr = g_inet_socket_address_new (iaddr, 0);
  57. g_object_unref (iaddr);
  58. listener = g_socket_listener_new ();
  59. g_signal_connect (listener, "event", G_CALLBACK (event_cb), &success);
  60. g_socket_listener_add_address (listener,
  61. saddr,
  62. G_SOCKET_TYPE_STREAM,
  63. G_SOCKET_PROTOCOL_TCP,
  64. NULL,
  65. NULL,
  66. &error);
  67. g_assert_no_error (error);
  68. g_assert_true (success);
  69. g_object_unref (saddr);
  70. g_object_unref (listener);
  71. }
  72. int
  73. main (int argc,
  74. char *argv[])
  75. {
  76. g_test_init (&argc, &argv, NULL);
  77. g_test_bug_base ("http://bugzilla.gnome.org/");
  78. g_test_add_func ("/socket-listener/event-signal", test_event_signal);
  79. return g_test_run();
  80. }