Branch data Line data Source code
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 : 4 : event_cb (GSocketListener *listener, 26 : : GSocketListenerEvent event, 27 : : GSocket *socket, 28 : : gpointer data) 29 : : { 30 : : static GSocketListenerEvent expected_event = G_SOCKET_LISTENER_BINDING; 31 : 4 : gboolean *success = (gboolean *)data; 32 : : 33 : 4 : g_assert (G_IS_SOCKET_LISTENER (listener)); 34 : 4 : g_assert (G_IS_SOCKET (socket)); 35 : 4 : g_assert (event == expected_event); 36 : : 37 [ + + + + : 4 : switch (event) - ] 38 : : { 39 : 1 : case G_SOCKET_LISTENER_BINDING: 40 : 1 : expected_event = G_SOCKET_LISTENER_BOUND; 41 : 1 : break; 42 : 1 : case G_SOCKET_LISTENER_BOUND: 43 : 1 : expected_event = G_SOCKET_LISTENER_LISTENING; 44 : 1 : break; 45 : 1 : case G_SOCKET_LISTENER_LISTENING: 46 : 1 : expected_event = G_SOCKET_LISTENER_LISTENED; 47 : 1 : break; 48 : 1 : case G_SOCKET_LISTENER_LISTENED: 49 : 1 : *success = TRUE; 50 : 1 : break; 51 : : } 52 : 4 : } 53 : : 54 : : static void 55 : 1 : test_event_signal (void) 56 : : { 57 : 1 : gboolean success = FALSE; 58 : : GInetAddress *iaddr; 59 : : GSocketAddress *saddr; 60 : : GSocketListener *listener; 61 : 1 : GError *error = NULL; 62 : : 63 : 1 : iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); 64 : 1 : saddr = g_inet_socket_address_new (iaddr, 0); 65 : 1 : g_object_unref (iaddr); 66 : : 67 : 1 : listener = g_socket_listener_new (); 68 : : 69 : 1 : g_signal_connect (listener, "event", G_CALLBACK (event_cb), &success); 70 : : 71 : 1 : 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 : 1 : g_assert_no_error (error); 79 : 1 : g_assert_true (success); 80 : : 81 : 1 : g_object_unref (saddr); 82 : 1 : g_object_unref (listener); 83 : 1 : } 84 : : 85 : : int 86 : 1 : main (int argc, 87 : : char *argv[]) 88 : : { 89 : 1 : g_test_init (&argc, &argv, NULL); 90 : : 91 : 1 : g_test_add_func ("/socket-listener/event-signal", test_event_signal); 92 : : 93 : 1 : return g_test_run(); 94 : : }