test-tcp.c 4.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2012 Collabora Ltd.
 *  Contact: George Kiagiadakis
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   George Kiagiadakis, Collabora Ltd.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <string.h>
#include <stdio.h>
41
#include <gio/gnetworking.h>
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

#include "socket.h"

GMainLoop *mainloop = NULL;
NiceSocket *active_sock, *client;
NiceSocket *passive_sock, *server;
NiceAddress tmp;
gchar buf[5];

static gboolean
on_server_connection_available (gpointer user_data)
{
  server = nice_tcp_passive_socket_accept (passive_sock);
  g_assert (server);
  nice_socket_free (passive_sock);
  passive_sock = NULL;

  g_main_loop_quit (mainloop);

  return FALSE;
}

static gboolean
on_server_input_available (gpointer user_data)
{
  g_assert (5 == nice_socket_recv (server, &tmp, 5, buf));
  g_assert (nice_address_equal (&tmp, &client->addr));

  g_main_loop_quit (mainloop);

  return FALSE;
}

static gboolean
on_client_input_available (gpointer user_data)
{
  g_assert (5 == nice_socket_recv (client, &tmp, 5, buf));
  g_assert (nice_address_equal (&tmp, &server->addr));

  g_main_loop_quit (mainloop);

  return FALSE;
}

int
main (void)
{
  NiceAddress active_bind_addr, passive_bind_addr;
  GSource *srv_listen_source, *srv_input_source, *cli_input_source;

92 93
  g_networking_init ();

94 95 96 97 98 99
  mainloop = g_main_loop_new (NULL, FALSE);

  nice_address_init (&active_bind_addr);
  g_assert (nice_address_set_from_string (&active_bind_addr, "::1"));

  nice_address_init (&passive_bind_addr);
100 101
  g_assert (nice_address_set_from_string (&passive_bind_addr, "127.0.0.1"));
  nice_address_set_port (&passive_bind_addr, 0);
102 103 104 105

  nice_address_init (&tmp);

  passive_sock = nice_tcp_passive_socket_new (g_main_loop_get_context (mainloop),
106
      &passive_bind_addr);
107 108 109 110 111 112 113 114 115
  g_assert (passive_sock);

  srv_listen_source = g_socket_create_source (passive_sock->fileno,
      G_IO_IN, NULL);
  g_source_set_callback (srv_listen_source,
      on_server_connection_available, NULL, NULL);
  g_source_attach (srv_listen_source, g_main_loop_get_context (mainloop));

  active_sock = nice_tcp_active_socket_new (g_main_loop_get_context (mainloop),
116
      &active_bind_addr);
117 118
  g_assert (active_sock);

119
  client = nice_tcp_active_socket_connect (active_sock, &passive_sock->addr);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  g_assert (client);
  nice_socket_free (active_sock);
  active_sock = NULL;

  g_main_loop_run (mainloop); /* -> on_server_connection_available */
  g_assert (server);

  srv_input_source = g_socket_create_source (server->fileno, G_IO_IN, NULL);
  g_source_set_callback (srv_input_source,
      on_server_input_available, NULL, NULL);
  g_source_attach (srv_input_source, g_main_loop_get_context (mainloop));

  cli_input_source = g_socket_create_source (client->fileno, G_IO_IN, NULL);
  g_source_set_callback (cli_input_source,
      on_client_input_available, NULL, NULL);
  g_source_attach (cli_input_source, g_main_loop_get_context (mainloop));

  g_assert (nice_address_get_port (&client->addr) != 0);

139
  g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  nice_address_set_port (&tmp, nice_address_get_port (&server->addr));
  g_assert (nice_address_get_port (&tmp) != 0);


  g_assert (5 == nice_socket_send (client, &tmp, 5, "hello"));
  g_main_loop_run (mainloop); /* -> on_server_input_available */
  g_assert (0 == strncmp (buf, "hello", 5));

  g_assert (5 == nice_socket_send (server, &tmp, 5, "uryyb"));
  g_main_loop_run (mainloop); /* -> on_client_input_available */
  g_assert (0 == strncmp (buf, "uryyb", 5));

  nice_socket_free (client);
  nice_socket_free (server);

  g_source_unref (srv_listen_source);
  g_source_unref (srv_input_source);
  g_source_unref (cli_input_source);
  g_main_loop_unref (mainloop);

  return 0;
}