243203c5c283e166e9fd5b9d74f79783ea506fca
[platform/upstream/libnice.git] / tests / test-tcp.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2012 Collabora Ltd.
5  *  Contact: George Kiagiadakis
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is the Nice GLib ICE library.
18  *
19  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20  * Corporation. All Rights Reserved.
21  *
22  * Contributors:
23  *   George Kiagiadakis, Collabora Ltd.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27  * case the provisions of LGPL are applicable instead of those above. If you
28  * wish to allow use of your version of this file only under the terms of the
29  * LGPL and not to allow others to use your version of this file under the
30  * MPL, indicate your decision by deleting the provisions above and replace
31  * them with the notice and other provisions required by the LGPL. If you do
32  * not delete the provisions above, a recipient may use your version of this
33  * file under either the MPL or the LGPL.
34  */
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <string.h>
40 #include <stdio.h>
41 #include <gio/gnetworking.h>
42
43 #include "socket.h"
44
45 GMainLoop *mainloop = NULL;
46 NiceSocket *active_sock, *client;
47 NiceSocket *passive_sock, *server;
48 NiceAddress tmp;
49 gchar buf[5];
50
51 static gboolean
52 on_server_connection_available (gpointer user_data)
53 {
54   server = nice_tcp_passive_socket_accept (passive_sock);
55   g_assert (server);
56
57   g_main_loop_quit (mainloop);
58
59   return FALSE;
60 }
61
62 static gboolean
63 on_server_input_available (gpointer user_data)
64 {
65   g_assert_cmpint (5, ==, nice_socket_recv (server, &tmp, 5, buf));
66   g_assert (nice_address_equal (&tmp, &client->addr));
67
68   g_main_loop_quit (mainloop);
69
70   return FALSE;
71 }
72
73 static gboolean
74 on_client_input_available (gpointer user_data)
75 {
76   g_assert_cmpint (5, ==, nice_socket_recv (client, &tmp, 5, buf));
77   g_assert (nice_address_equal (&tmp, &server->addr));
78
79   g_main_loop_quit (mainloop);
80
81   return FALSE;
82 }
83
84 int
85 main (void)
86 {
87   NiceAddress active_bind_addr, passive_bind_addr;
88   GSource *srv_listen_source, *srv_input_source, *cli_input_source;
89
90   g_networking_init ();
91
92   mainloop = g_main_loop_new (NULL, FALSE);
93
94   nice_address_init (&active_bind_addr);
95   g_assert (nice_address_set_from_string (&active_bind_addr, "::1"));
96
97   nice_address_init (&passive_bind_addr);
98   g_assert (nice_address_set_from_string (&passive_bind_addr, "127.0.0.1"));
99   nice_address_set_port (&passive_bind_addr, 0);
100
101   nice_address_init (&tmp);
102
103   passive_sock = nice_tcp_passive_socket_new (g_main_loop_get_context (mainloop),
104       &passive_bind_addr);
105   g_assert (passive_sock);
106
107   srv_listen_source = g_socket_create_source (passive_sock->fileno,
108       G_IO_IN, NULL);
109   g_source_set_callback (srv_listen_source,
110       on_server_connection_available, NULL, NULL);
111   g_source_attach (srv_listen_source, g_main_loop_get_context (mainloop));
112
113   active_sock = nice_tcp_active_socket_new (g_main_loop_get_context (mainloop),
114       &active_bind_addr);
115   g_assert (active_sock);
116
117   client = nice_tcp_active_socket_connect (active_sock, &passive_sock->addr);
118   g_assert (client);
119   nice_socket_free (active_sock);
120   active_sock = NULL;
121
122   g_main_loop_run (mainloop); /* -> on_server_connection_available */
123   g_assert (server);
124
125   srv_input_source = g_socket_create_source (server->fileno, G_IO_IN, NULL);
126   g_source_set_callback (srv_input_source,
127       on_server_input_available, NULL, NULL);
128   g_source_attach (srv_input_source, g_main_loop_get_context (mainloop));
129
130   cli_input_source = g_socket_create_source (client->fileno, G_IO_IN, NULL);
131   g_source_set_callback (cli_input_source,
132       on_client_input_available, NULL, NULL);
133   g_source_attach (cli_input_source, g_main_loop_get_context (mainloop));
134
135   g_assert (nice_address_get_port (&client->addr) != 0);
136
137   g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
138   nice_address_set_port (&tmp, nice_address_get_port (&server->addr));
139   g_assert (nice_address_get_port (&tmp) != 0);
140
141
142   g_assert_cmpint (5, ==, nice_socket_send (client, &tmp, 5, "hello"));
143   g_main_loop_run (mainloop); /* -> on_server_input_available */
144   g_assert (0 == strncmp (buf, "hello", 5));
145
146   g_assert_cmpint (5, ==, nice_socket_send (server, &tmp, 5, "uryyb"));
147   g_main_loop_run (mainloop); /* -> on_client_input_available */
148   g_assert (0 == strncmp (buf, "uryyb", 5));
149
150   nice_socket_free (client);
151   nice_socket_free (server);
152   nice_socket_free (passive_sock);
153
154   g_source_unref (srv_listen_source);
155   g_source_unref (srv_input_source);
156   g_source_unref (cli_input_source);
157   g_main_loop_unref (mainloop);
158
159   return 0;
160 }