test-thread.c 11.5 KB
Newer Older
Olivier Crête's avatar
Olivier Crête committed
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.
 *
 * Unit test for ICE full-mode related features.
 *
 * (C) 2007 Nokia Corporation. All rights reserved.
 *  Contact: Kai Vehmanen
 *
 * 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:
 *   Kai Vehmanen, Nokia
 *
 * 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

Youness Alaoui's avatar
Youness Alaoui committed
41 42
#include "agent.h"

Olivier Crête's avatar
Olivier Crête committed
43 44
#include <stdlib.h>
#include <string.h>
45
#ifndef G_OS_WIN32
46
#include <unistd.h>
47
#endif
Olivier Crête's avatar
Olivier Crête committed
48 49 50

GMainLoop *error_loop;

51 52
volatile gint global_lagent_cands = 0;
volatile gint global_ragent_cands = 0;
Olivier Crête's avatar
Olivier Crête committed
53

54 55
volatile gint global_lagent_buffers = 0;
volatile gint global_ragent_buffers = 0;
Olivier Crête's avatar
Olivier Crête committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/* Waits about 10 seconds for @var to be NULL/FALSE */
#define WAIT_UNTIL_UNSET(var, context)			\
  if (var)						\
    {							\
      int _i;						\
							\
      for (_i = 0; _i < 13 && (var); _i++)		\
	{						\
	  g_usleep (1000 * (1 << _i));			\
	  g_main_context_iteration (context, FALSE);	\
	}						\
							\
      g_assert (!(var));				\
    }

Olivier Crête's avatar
Olivier Crête committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static gboolean timer_cb (gpointer pointer)
{
  g_debug ("test-thread:%s: %p", G_STRFUNC, pointer);

  /* note: should not be reached, abort */
  g_debug ("ERROR: test has got stuck, aborting...");
  exit (-1);

}

static gpointer
mainloop_thread (gpointer data)
{
  GMainLoop *loop = data;

87 88 89
  /* Synchronise thread starting. */
  while (!g_main_loop_is_running (error_loop));

Olivier Crête's avatar
Olivier Crête committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103
  g_main_loop_run (loop);

  return NULL;
}


static void
cb_new_selected_pair(NiceAgent *agent,
    guint stream_id,
    guint component_id,
    gchar *lfoundation,
    gchar* rfoundation,
    gpointer data)
{
104
  g_debug ("test-thread:%s: %p", G_STRFUNC, data);
Olivier Crête's avatar
Olivier Crête committed
105

106
  if (GPOINTER_TO_UINT (data) == 1)
Olivier Crête's avatar
Olivier Crête committed
107
    g_atomic_int_inc (&global_lagent_cands);
108
  else if (GPOINTER_TO_UINT (data) == 2)
Olivier Crête's avatar
Olivier Crête committed
109 110 111 112
    g_atomic_int_inc (&global_ragent_cands);
}


113
static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
Olivier Crête's avatar
Olivier Crête committed
114 115
{
  NiceAgent *other = g_object_get_data (G_OBJECT (agent), "other-agent");
116 117
  gchar *ufrag = NULL, *password = NULL;
  GSList *cands, *i;
Olivier Crête's avatar
Olivier Crête committed
118 119 120 121 122 123 124 125 126 127 128 129 130
  guint id, other_id;
  gpointer tmp;

  g_debug ("test-thread:%s", G_STRFUNC);

  tmp = g_object_get_data (G_OBJECT (agent), "id");
  id = GPOINTER_TO_UINT (tmp);
  tmp = g_object_get_data (G_OBJECT (other), "id");
  other_id = GPOINTER_TO_UINT (tmp);

  nice_agent_get_local_credentials(agent, id, &ufrag, &password);
  nice_agent_set_remote_credentials (other,
      other_id, ufrag, password);
131 132
  g_free (ufrag);
  g_free (password);
Olivier Crête's avatar
Olivier Crête committed
133 134 135 136 137

  cands = nice_agent_get_local_candidates(agent, id, 1);
  g_assert (cands != NULL);

  nice_agent_set_remote_candidates (other, other_id, 1, cands);
138 139 140 141

  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);
Olivier Crête's avatar
Olivier Crête committed
142 143 144 145 146 147 148
}



static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
{
  gchar data[10];
149 150
  volatile gint *count = NULL;
  gint count_val;
Olivier Crête's avatar
Olivier Crête committed
151 152

  if (GPOINTER_TO_UINT (user_data) == 1)
Youness Alaoui's avatar
Youness Alaoui committed
153
    count = &global_lagent_buffers;
Olivier Crête's avatar
Olivier Crête committed
154
  else if (GPOINTER_TO_UINT (user_data) == 2)
Youness Alaoui's avatar
Youness Alaoui committed
155
    count = &global_ragent_buffers;
Olivier Crête's avatar
Olivier Crête committed
156 157 158
  else
    g_error ("Invalid agent ?");

159 160
  count_val = g_atomic_int_get (count);
  if (count_val == 10)
161 162
    return;

Olivier Crête's avatar
Olivier Crête committed
163 164
  g_assert (len == 10);

165
  memset (data, count_val + '1', 10);
Olivier Crête's avatar
Olivier Crête committed
166 167 168

  g_assert (memcmp (buf, data, 10) == 0);

169
  g_atomic_int_inc (count);
Olivier Crête's avatar
Olivier Crête committed
170

171 172
  if (g_atomic_int_get (&global_ragent_buffers) == 10 &&
      g_atomic_int_get (&global_lagent_buffers) == 10)
Olivier Crête's avatar
Olivier Crête committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    g_main_loop_quit (error_loop);
}


static void cb_component_state_changed (NiceAgent *agent,
    guint stream_id,
    guint component_id,
    guint state,
    gpointer user_data)
{
  int i;
  gchar data[10];

  if (state != NICE_COMPONENT_STATE_READY)
    return;

  for (i=0; i<10; i++)
    {
      memset (data, i+'1', 10);

      nice_agent_send (agent, stream_id, component_id, 10, data);
    }
}

int main (void)
{
  NiceAgent *lagent, *ragent;      /* agent's L and R */
  NiceAddress baseaddr;
  const char *stun_server = NULL, *stun_server_port = NULL;
  GMainContext *lmainctx, *rmainctx;
  GMainLoop *lmainloop, *rmainloop;
  GThread *lthread, *rthread;
  guint ls_id, rs_id;
  GMainContext *ldmainctx, *rdmainctx;
  GMainLoop *ldmainloop, *rdmainloop;
  GThread *ldthread, *rdthread;

210 211 212 213
#ifdef G_OS_WIN32
  WSADATA w;
  WSAStartup(0x0202, &w);
#endif
Olivier Crête's avatar
Olivier Crête committed
214
  g_type_init ();
215
  g_thread_init(NULL);
Olivier Crête's avatar
Olivier Crête committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

  lmainctx = g_main_context_new ();
  rmainctx = g_main_context_new ();
  lmainloop = g_main_loop_new (lmainctx, FALSE);
  rmainloop = g_main_loop_new (rmainctx, FALSE);

  ldmainctx = g_main_context_new ();
  rdmainctx = g_main_context_new ();
  ldmainloop = g_main_loop_new (ldmainctx, FALSE);
  rdmainloop = g_main_loop_new (rdmainctx, FALSE);

  error_loop = g_main_loop_new (NULL, FALSE);


  /* step: create the agents L and R */
231 232
  lagent = nice_agent_new (lmainctx, NICE_COMPATIBILITY_MSN);
  ragent = nice_agent_new (rmainctx, NICE_COMPATIBILITY_MSN);
Olivier Crête's avatar
Olivier Crête committed
233 234 235 236

  g_object_set_data (G_OBJECT (lagent), "other-agent", ragent);
  g_object_set_data (G_OBJECT (ragent), "other-agent", lagent);

237 238
  g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
  g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
Youness Alaoui's avatar
Youness Alaoui committed
239 240
  g_object_set (G_OBJECT (lagent), "upnp", FALSE, NULL);
  g_object_set (G_OBJECT (ragent), "upnp", FALSE, NULL);
241

Olivier Crête's avatar
Olivier Crête committed
242
  /* step: add a timer to catch state changes triggered by signals */
243
  g_timeout_add (30000, timer_cb, NULL);
Olivier Crête's avatar
Olivier Crête committed
244 245 246 247 248 249 250 251

  /* step: specify which local interface to use */
  if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
    g_assert_not_reached ();
  nice_agent_add_local_address (lagent, &baseaddr);
  nice_agent_add_local_address (ragent, &baseaddr);

  g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
252
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
Olivier Crête's avatar
Olivier Crête committed
253
  g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
254
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(2));
Olivier Crête's avatar
Olivier Crête committed
255
  g_signal_connect (G_OBJECT (lagent), "component-state-changed",
256
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER(1));
Olivier Crête's avatar
Olivier Crête committed
257
  g_signal_connect (G_OBJECT (ragent), "component-state-changed",
258
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER(2));
Olivier Crête's avatar
Olivier Crête committed
259
  g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
260
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
Olivier Crête's avatar
Olivier Crête committed
261
  g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
262
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(2));
Olivier Crête's avatar
Olivier Crête committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

  stun_server = getenv ("NICE_STUN_SERVER");
  stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
  if (stun_server) {
    g_object_set (G_OBJECT (lagent), "stun-server", stun_server,  NULL);
    g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port),  NULL);
    g_object_set (G_OBJECT (ragent), "stun-server", stun_server,  NULL);
    g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port),  NULL);
  }

  /* step: test setter/getter functions for properties */
  {
    guint max_checks = 0;
    gchar *string = NULL;
    guint port = 0;
    gboolean mode = FALSE;
    g_object_get (G_OBJECT (lagent), "stun-server", &string, NULL);
    g_assert (stun_server == NULL || strcmp (string, stun_server) == 0);
    g_free (string);
    g_object_get (G_OBJECT (lagent), "stun-server-port", &port, NULL);
    g_assert (stun_server_port == NULL || port == (guint)atoi (stun_server_port));
    g_object_get (G_OBJECT (lagent), "controlling-mode", &mode, NULL);
    g_assert (mode == TRUE);
    g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 300, NULL);
    g_object_get (G_OBJECT (lagent), "max-connectivity-checks", &max_checks, NULL);
    g_assert (max_checks == 300);
  }

  /* step: run test the first time */
  g_debug ("test-thread: TEST STARTS / running test for the 1st time");

294
#if !GLIB_CHECK_VERSION(2,31,8)
Olivier Crête's avatar
Olivier Crête committed
295 296
  lthread = g_thread_create (mainloop_thread, lmainloop, TRUE, NULL);
  rthread = g_thread_create (mainloop_thread, rmainloop, TRUE, NULL);
297 298 299 300 301 302
#else
  lthread = g_thread_new ("lthread libnice", mainloop_thread, lmainloop);
  rthread = g_thread_new ("rthread libnice", mainloop_thread, rmainloop);
#endif

  g_assert (lthread);
Olivier Crête's avatar
Olivier Crête committed
303 304
  g_assert (rthread);

305
  ls_id = nice_agent_add_stream (lagent, 2);
Olivier Crête's avatar
Olivier Crête committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  rs_id = nice_agent_add_stream (ragent, 2);
  g_assert (ls_id > 0);
  g_assert (rs_id > 0);

  g_object_set_data (G_OBJECT (lagent), "id", GUINT_TO_POINTER (ls_id));
  g_object_set_data (G_OBJECT (ragent), "id", GUINT_TO_POINTER (rs_id));

  nice_agent_gather_candidates (lagent, ls_id);
  nice_agent_gather_candidates (ragent, rs_id);

  nice_agent_attach_recv (lagent, ls_id, 1, ldmainctx, cb_nice_recv,
      GUINT_TO_POINTER (1));
  nice_agent_attach_recv (ragent, rs_id, 1, rdmainctx, cb_nice_recv,
      GUINT_TO_POINTER (2));

321
#if !GLIB_CHECK_VERSION(2,31,8)
Olivier Crête's avatar
Olivier Crête committed
322 323
  ldthread = g_thread_create (mainloop_thread, ldmainloop, TRUE, NULL);
  rdthread = g_thread_create (mainloop_thread, rdmainloop, TRUE, NULL);
324 325 326 327 328
#else
  ldthread = g_thread_new ("ldthread libnice", mainloop_thread, ldmainloop);
  rdthread = g_thread_new ("rdthread libnice", mainloop_thread, rdmainloop);
#endif
  g_assert (ldthread);
Olivier Crête's avatar
Olivier Crête committed
329 330 331 332 333
  g_assert (rdthread);

  /* Run loop for error timer */
  g_main_loop_run (error_loop);

334 335 336 337 338 339 340 341 342 343 344 345
  while (!g_main_loop_is_running (ldmainloop));
  while (g_main_loop_is_running (ldmainloop))
    g_main_loop_quit (ldmainloop);
  while (!g_main_loop_is_running (rdmainloop));
  while (g_main_loop_is_running (rdmainloop))
    g_main_loop_quit (rdmainloop);
  while (!g_main_loop_is_running (lmainloop));
  while (g_main_loop_is_running (lmainloop))
    g_main_loop_quit (lmainloop);
  while (!g_main_loop_is_running (rmainloop));
  while (g_main_loop_is_running (rmainloop))
    g_main_loop_quit (rmainloop);
Olivier Crête's avatar
Olivier Crête committed
346 347 348 349 350 351 352

  g_thread_join (ldthread);
  g_thread_join (rdthread);
  g_thread_join (lthread);
  g_thread_join (rthread);

  /* note: verify that correct number of local candidates were reported */
353 354
  g_assert (g_atomic_int_get (&global_lagent_cands) == 1);
  g_assert (g_atomic_int_get (&global_ragent_cands) == 1);
Olivier Crête's avatar
Olivier Crête committed
355

356 357 358
  g_object_add_weak_pointer (G_OBJECT (lagent), (gpointer *) &lagent);
  g_object_add_weak_pointer (G_OBJECT (ragent), (gpointer *) &ragent);

Olivier Crête's avatar
Olivier Crête committed
359 360 361
  g_object_unref (lagent);
  g_object_unref (ragent);

362 363 364
  WAIT_UNTIL_UNSET (lagent, lmainctx);
  WAIT_UNTIL_UNSET (ragent, rmainctx);

Olivier Crête's avatar
Olivier Crête committed
365 366 367 368 369 370
  g_main_loop_unref (lmainloop);
  g_main_loop_unref (rmainloop);
  g_main_loop_unref (ldmainloop);
  g_main_loop_unref (rdmainloop);

  g_main_loop_unref (error_loop);
371 372 373
#ifdef G_OS_WIN32
  WSACleanup();
#endif
Olivier Crête's avatar
Olivier Crête committed
374 375
  return 0;
}