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 | |
20 | #include <gio/gio.h> |
21 | |
22 | static void |
23 | event_cb (GSocketListener *listener, |
24 | GSocketListenerEvent event, |
25 | GSocket *socket, |
26 | gpointer data) |
27 | { |
28 | static GSocketListenerEvent expected_event = G_SOCKET_LISTENER_BINDING; |
29 | gboolean *success = (gboolean *)data; |
30 | |
31 | g_assert (G_IS_SOCKET_LISTENER (listener)); |
32 | g_assert (G_IS_SOCKET (socket)); |
33 | g_assert (event == expected_event); |
34 | |
35 | switch (event) |
36 | { |
37 | case G_SOCKET_LISTENER_BINDING: |
38 | expected_event = G_SOCKET_LISTENER_BOUND; |
39 | break; |
40 | case G_SOCKET_LISTENER_BOUND: |
41 | expected_event = G_SOCKET_LISTENER_LISTENING; |
42 | break; |
43 | case G_SOCKET_LISTENER_LISTENING: |
44 | expected_event = G_SOCKET_LISTENER_LISTENED; |
45 | break; |
46 | case G_SOCKET_LISTENER_LISTENED: |
47 | *success = TRUE; |
48 | break; |
49 | } |
50 | } |
51 | |
52 | static void |
53 | test_event_signal (void) |
54 | { |
55 | gboolean success = FALSE; |
56 | GInetAddress *iaddr; |
57 | GSocketAddress *saddr; |
58 | GSocketListener *listener; |
59 | GError *error = NULL; |
60 | |
61 | iaddr = g_inet_address_new_loopback (family: G_SOCKET_FAMILY_IPV4); |
62 | saddr = g_inet_socket_address_new (address: iaddr, port: 0); |
63 | g_object_unref (object: iaddr); |
64 | |
65 | listener = g_socket_listener_new (); |
66 | |
67 | g_signal_connect (listener, "event" , G_CALLBACK (event_cb), &success); |
68 | |
69 | g_socket_listener_add_address (listener, |
70 | address: saddr, |
71 | type: G_SOCKET_TYPE_STREAM, |
72 | protocol: G_SOCKET_PROTOCOL_TCP, |
73 | NULL, |
74 | NULL, |
75 | error: &error); |
76 | g_assert_no_error (error); |
77 | g_assert_true (success); |
78 | |
79 | g_object_unref (object: saddr); |
80 | g_object_unref (object: listener); |
81 | } |
82 | |
83 | int |
84 | main (int argc, |
85 | char *argv[]) |
86 | { |
87 | g_test_init (argc: &argc, argv: &argv, NULL); |
88 | |
89 | g_test_bug_base (uri_pattern: "http://bugzilla.gnome.org/" ); |
90 | |
91 | g_test_add_func (testpath: "/socket-listener/event-signal" , test_func: test_event_signal); |
92 | |
93 | return g_test_run(); |
94 | } |
95 | |