(root)/
glib-2.79.0/
gio/
tests/
socket-listener.c
       1  /* GLib testing framework examples and tests
       2   *
       3   * Copyright 2014 Red Hat, Inc.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       7   * This library is free software; you can redistribute it and/or
       8   * modify it under the terms of the GNU Lesser General Public
       9   * License as published by the Free Software Foundation; either
      10   * version 2.1 of the License, or (at your option) any later version.
      11   *
      12   * This library is distributed in the hope that it will be useful,
      13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15   * Lesser General Public License for more details.
      16   *
      17   * You should have received a copy of the GNU Lesser General Public
      18   * License along with this library; if not, see
      19   * <http://www.gnu.org/licenses/>.
      20   */
      21  
      22  #include <gio/gio.h>
      23  
      24  static void
      25  event_cb (GSocketListener      *listener,
      26            GSocketListenerEvent  event,
      27            GSocket              *socket,
      28            gpointer              data)
      29  {
      30    static GSocketListenerEvent expected_event = G_SOCKET_LISTENER_BINDING;
      31    gboolean *success = (gboolean *)data;
      32  
      33    g_assert (G_IS_SOCKET_LISTENER (listener));
      34    g_assert (G_IS_SOCKET (socket));
      35    g_assert (event == expected_event);
      36  
      37    switch (event)
      38      {
      39        case G_SOCKET_LISTENER_BINDING:
      40          expected_event = G_SOCKET_LISTENER_BOUND;
      41          break;
      42        case G_SOCKET_LISTENER_BOUND:
      43          expected_event = G_SOCKET_LISTENER_LISTENING;
      44          break;
      45        case G_SOCKET_LISTENER_LISTENING:
      46          expected_event = G_SOCKET_LISTENER_LISTENED;
      47          break;
      48        case G_SOCKET_LISTENER_LISTENED:
      49          *success = TRUE;
      50          break;
      51      }
      52  }
      53  
      54  static void
      55  test_event_signal (void)
      56  {
      57    gboolean success = FALSE;
      58    GInetAddress *iaddr;
      59    GSocketAddress *saddr;
      60    GSocketListener *listener;
      61    GError *error = NULL;
      62  
      63    iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
      64    saddr = g_inet_socket_address_new (iaddr, 0);
      65    g_object_unref (iaddr);
      66  
      67    listener = g_socket_listener_new ();
      68  
      69    g_signal_connect (listener, "event", G_CALLBACK (event_cb), &success);
      70  
      71    g_socket_listener_add_address (listener,
      72                                   saddr,
      73                                   G_SOCKET_TYPE_STREAM,
      74                                   G_SOCKET_PROTOCOL_TCP,
      75                                   NULL,
      76                                   NULL,
      77                                   &error);
      78    g_assert_no_error (error);
      79    g_assert_true (success);
      80  
      81    g_object_unref (saddr);
      82    g_object_unref (listener);
      83  }
      84  
      85  int
      86  main (int   argc,
      87        char *argv[])
      88  {
      89    g_test_init (&argc, &argv, NULL);
      90  
      91    g_test_add_func ("/socket-listener/event-signal", test_event_signal);
      92  
      93    return g_test_run();
      94  }