コード例 #1
0
ファイル: test-icetcp.c プロジェクト: kakaroto/libnice
int main (void)
{
  NiceAgent *lagent, *ragent;      /* agent's L and R */
  NiceAddress baseaddr;
  int result;
  guint timer_id;

#ifdef G_OS_WIN32
  WSADATA w;

  WSAStartup(0x0202, &w);
#endif
  g_type_init ();

  global_mainloop = g_main_loop_new (NULL, FALSE);

  /* step: create the agents L and R */
  lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY_RFC5245);
  ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY_RFC5245);

  g_object_set (G_OBJECT (lagent), "ice-udp", FALSE,  NULL);
  g_object_set (G_OBJECT (ragent), "ice-udp", FALSE,  NULL);
  nice_agent_set_software (lagent, "Test-icetcp, Left Agent");
  nice_agent_set_software (ragent, "Test-icetcp, Right Agent");

  /* step: add a timer to catch state changes triggered by signals */
  timer_id = g_timeout_add (30000, timer_cb, NULL);

  /* 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",
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
  g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
  g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "new-candidate",
      G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "new-candidate",
      G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received",
      G_CALLBACK (cb_initial_binding_request_received),
      GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
      G_CALLBACK (cb_initial_binding_request_received),
      GUINT_TO_POINTER (2));

  /* step: run test the first time */
  g_debug ("test-icetcp: TEST STARTS / running test for the 1st time");
  result = run_full_test (lagent, ragent, &baseaddr, 4 ,0);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
  /* note: verify that correct number of local candidates were reported */
  g_assert (global_lagent_cands >= 2);
  g_assert (global_ragent_cands >= 2);


  /* step: run test again without unref'ing agents */
  g_debug ("test-icetcp: TEST STARTS / running test for the 2nd time");
  result = run_full_test (lagent, ragent, &baseaddr, 4, 0);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
  /* note: verify that correct number of local candidates were reported */
  g_assert (global_lagent_cands >= 2);
  g_assert (global_ragent_cands >= 2);

  g_object_unref (lagent);
  g_object_unref (ragent);

  g_main_loop_unref (global_mainloop);
  global_mainloop = NULL;

  g_source_remove (timer_id);
#ifdef G_OS_WIN32
  WSACleanup();
#endif
  return result;
}
コード例 #2
0
ファイル: test-fullmode.c プロジェクト: WilliamRen/libnice
int main (void)
{
  NiceAgent *lagent, *ragent;      /* agent's L and R */
  NiceAddress baseaddr;
  int result;
  guint timer_id;
  const char *stun_server = NULL, *stun_server_port = NULL;

#ifdef G_OS_WIN32
  WSADATA w;

  WSAStartup(0x0202, &w);
#endif
  g_type_init ();
#if !GLIB_CHECK_VERSION(2,31,8)
  g_thread_init(NULL);
#endif

  global_mainloop = g_main_loop_new (NULL, FALSE);

  /* Note: impl limits ...
   * - no multi-stream support
   * - no IPv6 support
   */

  /* step: create the agents L and R */
#if USE_RELIABLE
  lagent = nice_agent_new_reliable (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY);
  ragent = nice_agent_new_reliable (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY);
#else
  lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY);
  ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY);
#endif

  nice_agent_set_software (lagent, "Test-fullmode, Left Agent");
  nice_agent_set_software (ragent, "Test-fullmode, Right Agent");

  /* step: add a timer to catch state changes triggered by signals */
#if USE_TURN
  timer_id = g_timeout_add (300000, timer_cb, NULL);
#else
  timer_id = g_timeout_add (30000, timer_cb, NULL);
#endif

  /* step: specify which local interface to use */
#if USE_LOOPBACK
  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);
#endif

  g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
  g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
  g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "new-candidate",
      G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "new-candidate",
      G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received",
      G_CALLBACK (cb_initial_binding_request_received),
      GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
      G_CALLBACK (cb_initial_binding_request_received),
      GUINT_TO_POINTER (2));

  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);
  }

  g_object_set (G_OBJECT (lagent), "upnp", USE_UPNP,  NULL);
  g_object_set (G_OBJECT (lagent), "proxy-ip", PROXY_IP,  NULL);
  g_object_set (G_OBJECT (lagent), "proxy-port", PROXY_PORT, NULL);
  g_object_set (G_OBJECT (lagent), "proxy-type", PROXY_TYPE, NULL);
  g_object_set (G_OBJECT (lagent), "proxy-username", PROXY_USERNAME, NULL);
  g_object_set (G_OBJECT (lagent), "proxy-password", PROXY_PASSWORD, NULL);
  g_object_set (G_OBJECT (ragent), "upnp", USE_UPNP,  NULL);
  g_object_set (G_OBJECT (ragent), "proxy-ip", PROXY_IP,  NULL);
  g_object_set (G_OBJECT (ragent), "proxy-port", PROXY_PORT, NULL);
  g_object_set (G_OBJECT (ragent), "proxy-type", PROXY_TYPE, NULL);
  g_object_set (G_OBJECT (ragent), "proxy-username", PROXY_USERNAME, NULL);
  g_object_set (G_OBJECT (ragent), "proxy-password", PROXY_PASSWORD, 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), "proxy-ip", &string, NULL);
    g_assert (strcmp (string, PROXY_IP) == 0);
    g_free (string);
    g_object_get (G_OBJECT (lagent), "proxy-port", &port, NULL);
    g_assert (port == PROXY_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-fullmode: TEST STARTS / running test for the 1st time");
  result = run_full_test (lagent, ragent, &baseaddr, 4 ,0);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
  /* When using TURN, we get peer reflexive candidates for the host cands
     that we removed so we can get another new_selected_pair signal later
     depending on timing/racing, we could double (or not) the amount we expected
  */
#if !(USE_TURN)
  /* note: verify that correct number of local candidates were reported */
    g_assert (global_lagent_cands == 2);
    g_assert (global_ragent_cands == 2);
#endif


  /* step: run test again without unref'ing agents */
  g_debug ("test-fullmode: TEST STARTS / running test for the 2nd time");
  result = run_full_test (lagent, ragent, &baseaddr, 4, 0);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
  /* When using TURN, we get peer reflexive candidates for the host cands
     that we removed so we can get another new_selected_pair signal later
     depending on timing/racing, we could double (or not) the amount we expected
  */
#if !(USE_TURN)
  /* note: verify that correct number of local candidates were reported */
  g_assert (global_lagent_cands == 2);
  g_assert (global_ragent_cands == 2);
#endif


  /* step: run test simulating delayed SDP answer */
  g_debug ("test-fullmode: TEST STARTS / delayed SDP answer");
  result = run_full_test_delayed_answer (lagent, ragent, &baseaddr, 4, 0);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
  /* note: verify that correct number of local candidates were reported */
  /* When using TURN, we get peer reflexive candidates for the host cands
     that we removed so we can get another new_selected_pair signal later
     depending on timing/racing, we could double (or not) the amount we expected
  */
#if !(USE_TURN)
  g_assert (global_lagent_cands == 2);
  g_assert (global_ragent_cands == 2);
#endif

#if TEST_GOOGLE
  return result;
#endif

  /* run test with incorrect credentials (make sure process fails) */
  g_debug ("test-fullmode: TEST STARTS / incorrect credentials");
  result = run_full_test_wrong_password (lagent, ragent, &baseaddr);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_FAILED);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_LAST);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_FAILED);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_LAST);

  /* The max connectivity checks test can't be run with TURN because
     we'll have 3 local candidates instead of 1 and the checks will
     be random, so we can't predict how many will fail/succeed */
#if USE_TURN == 0

  /* step: run test with a hard limit for connecitivity checks */
  g_debug ("test-fullmode: TEST STARTS / max connectivity checks");
  g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 1, NULL);
  g_object_set (G_OBJECT (ragent), "max-connectivity-checks", 1, NULL);
  result = run_full_test (lagent, ragent, &baseaddr, 2, 2);
  priv_print_global_status ();
  g_assert (result == 0);
  /* should FAIL as agent L can't send any checks: */
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_FAILED ||
	    global_lagent_state[1] == NICE_COMPONENT_STATE_FAILED);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_FAILED ||
	    global_lagent_state[1] == NICE_COMPONENT_STATE_FAILED);
#endif

  g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 100, NULL);
  g_object_set (G_OBJECT (ragent), "max-connectivity-checks", 100, NULL);
  result = run_full_test (lagent, ragent, &baseaddr, 4, 0);
  priv_print_global_status ();
  /* should SUCCEED as agent L can send the checks: */
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
  g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 100, NULL);

  /* run test with a conflict in controlling mode: controlling-controlling */
  g_debug ("test-fullmode: TEST STARTS / controlling mode conflict case-1");
  result = run_full_test_control_conflict (lagent, ragent, &baseaddr, TRUE);
  priv_print_global_status ();
  g_assert (result == 0);

  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);

  /* run test with a conflict in controlling mode: controlled-controlled */
  g_debug ("test-fullmode: TEST STARTS / controlling mode conflict case-2");
  result = run_full_test_control_conflict (lagent, ragent, &baseaddr, FALSE);
  priv_print_global_status ();
  g_assert (result == 0);
  g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);

  g_object_unref (lagent);
  g_object_unref (ragent);

  g_main_loop_unref (global_mainloop);
  global_mainloop = NULL;

  g_source_remove (timer_id);
#ifdef G_OS_WIN32
  WSACleanup();
#endif
  return result;
}