2 * This file is part of the Nice GLib ICE library.
4 * Unit test for ICE full-mode related features.
6 * (C) 2007 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of the
28 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
29 * case the provisions of LGPL are applicable instead of those above. If you
30 * wish to allow use of your version of this file only under the terms of the
31 * LGPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replace
33 * them with the notice and other provisions required by the LGPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the LGPL.
49 GMainLoop
*error_loop
;
51 gint global_lagent_cands
= 0;
52 gint global_ragent_cands
= 0;
54 gint global_lagent_buffers
= 0;
55 gint global_ragent_buffers
= 0;
57 static gboolean
timer_cb (gpointer pointer
)
59 g_debug ("test-thread:%s: %p", G_STRFUNC
, pointer
);
61 /* note: should not be reached, abort */
62 g_debug ("ERROR: test has got stuck, aborting...");
68 mainloop_thread (gpointer data
)
70 GMainLoop
*loop
= data
;
73 g_main_loop_run (loop
);
80 cb_new_selected_pair(NiceAgent
*agent
,
87 g_debug ("test-thread:%s: %p", G_STRFUNC
, data
);
89 if (GPOINTER_TO_UINT (data
) == 1)
90 g_atomic_int_inc (&global_lagent_cands
);
91 else if (GPOINTER_TO_UINT (data
) == 2)
92 g_atomic_int_inc (&global_ragent_cands
);
96 static void cb_candidate_gathering_done(NiceAgent
*agent
, guint stream_id
, gpointer data
)
98 NiceAgent
*other
= g_object_get_data (G_OBJECT (agent
), "other-agent");
99 gchar
*ufrag
= NULL
, *password
= NULL
;
104 g_debug ("test-thread:%s", G_STRFUNC
);
106 tmp
= g_object_get_data (G_OBJECT (agent
), "id");
107 id
= GPOINTER_TO_UINT (tmp
);
108 tmp
= g_object_get_data (G_OBJECT (other
), "id");
109 other_id
= GPOINTER_TO_UINT (tmp
);
111 nice_agent_get_local_credentials(agent
, id
, &ufrag
, &password
);
112 nice_agent_set_remote_credentials (other
,
113 other_id
, ufrag
, password
);
117 cands
= nice_agent_get_local_candidates(agent
, id
, 1);
118 g_assert (cands
!= NULL
);
120 nice_agent_set_remote_candidates (other
, other_id
, 1, cands
);
122 for (i
= cands
; i
; i
= i
->next
)
123 nice_candidate_free ((NiceCandidate
*) i
->data
);
124 g_slist_free (cands
);
129 static void cb_nice_recv (NiceAgent
*agent
, guint stream_id
, guint component_id
, guint len
, gchar
*buf
, gpointer user_data
)
134 if (GPOINTER_TO_UINT (user_data
) == 1)
135 count
= &global_lagent_buffers
;
136 else if (GPOINTER_TO_UINT (user_data
) == 2)
137 count
= &global_ragent_buffers
;
139 g_error ("Invalid agent ?");
144 g_assert (len
== 10);
146 memset (data
, *count
+'1', 10);
148 g_assert (memcmp (buf
, data
, 10) == 0);
155 if (global_ragent_buffers
== -1 && global_lagent_buffers
== -1)
156 g_main_loop_quit (error_loop
);
160 static void cb_component_state_changed (NiceAgent
*agent
,
169 if (state
!= NICE_COMPONENT_STATE_READY
)
174 memset (data
, i
+'1', 10);
176 nice_agent_send (agent
, stream_id
, component_id
, 10, data
);
182 NiceAgent
*lagent
, *ragent
; /* agent's L and R */
183 NiceAddress baseaddr
;
184 const char *stun_server
= NULL
, *stun_server_port
= NULL
;
185 GMainContext
*lmainctx
, *rmainctx
;
186 GMainLoop
*lmainloop
, *rmainloop
;
187 GThread
*lthread
, *rthread
;
189 GMainContext
*ldmainctx
, *rdmainctx
;
190 GMainLoop
*ldmainloop
, *rdmainloop
;
191 GThread
*ldthread
, *rdthread
;
195 WSAStartup(0x0202, &w
);
198 #if !GLIB_CHECK_VERSION(2,31,8)
202 lmainctx
= g_main_context_new ();
203 rmainctx
= g_main_context_new ();
204 lmainloop
= g_main_loop_new (lmainctx
, FALSE
);
205 rmainloop
= g_main_loop_new (rmainctx
, FALSE
);
207 ldmainctx
= g_main_context_new ();
208 rdmainctx
= g_main_context_new ();
209 ldmainloop
= g_main_loop_new (ldmainctx
, FALSE
);
210 rdmainloop
= g_main_loop_new (rdmainctx
, FALSE
);
212 error_loop
= g_main_loop_new (NULL
, FALSE
);
215 /* step: create the agents L and R */
216 lagent
= nice_agent_new (lmainctx
, NICE_COMPATIBILITY_MSN
);
217 ragent
= nice_agent_new (rmainctx
, NICE_COMPATIBILITY_MSN
);
219 g_object_set_data (G_OBJECT (lagent
), "other-agent", ragent
);
220 g_object_set_data (G_OBJECT (ragent
), "other-agent", lagent
);
222 g_object_set (G_OBJECT (lagent
), "controlling-mode", TRUE
, NULL
);
223 g_object_set (G_OBJECT (ragent
), "controlling-mode", FALSE
, NULL
);
224 g_object_set (G_OBJECT (lagent
), "upnp", FALSE
, NULL
);
225 g_object_set (G_OBJECT (ragent
), "upnp", FALSE
, NULL
);
227 /* step: add a timer to catch state changes triggered by signals */
228 g_timeout_add (30000, timer_cb
, NULL
);
230 /* step: specify which local interface to use */
231 if (!nice_address_set_from_string (&baseaddr
, "127.0.0.1"))
232 g_assert_not_reached ();
233 nice_agent_add_local_address (lagent
, &baseaddr
);
234 nice_agent_add_local_address (ragent
, &baseaddr
);
236 g_signal_connect (G_OBJECT (lagent
), "candidate-gathering-done",
237 G_CALLBACK (cb_candidate_gathering_done
), GUINT_TO_POINTER(1));
238 g_signal_connect (G_OBJECT (ragent
), "candidate-gathering-done",
239 G_CALLBACK (cb_candidate_gathering_done
), GUINT_TO_POINTER(2));
240 g_signal_connect (G_OBJECT (lagent
), "component-state-changed",
241 G_CALLBACK (cb_component_state_changed
), GUINT_TO_POINTER(1));
242 g_signal_connect (G_OBJECT (ragent
), "component-state-changed",
243 G_CALLBACK (cb_component_state_changed
), GUINT_TO_POINTER(2));
244 g_signal_connect (G_OBJECT (lagent
), "new-selected-pair",
245 G_CALLBACK (cb_new_selected_pair
), GUINT_TO_POINTER(1));
246 g_signal_connect (G_OBJECT (ragent
), "new-selected-pair",
247 G_CALLBACK (cb_new_selected_pair
), GUINT_TO_POINTER(2));
249 stun_server
= getenv ("NICE_STUN_SERVER");
250 stun_server_port
= getenv ("NICE_STUN_SERVER_PORT");
252 g_object_set (G_OBJECT (lagent
), "stun-server", stun_server
, NULL
);
253 g_object_set (G_OBJECT (lagent
), "stun-server-port", atoi (stun_server_port
), NULL
);
254 g_object_set (G_OBJECT (ragent
), "stun-server", stun_server
, NULL
);
255 g_object_set (G_OBJECT (ragent
), "stun-server-port", atoi (stun_server_port
), NULL
);
258 /* step: test setter/getter functions for properties */
260 guint max_checks
= 0;
261 gchar
*string
= NULL
;
263 gboolean mode
= FALSE
;
264 g_object_get (G_OBJECT (lagent
), "stun-server", &string
, NULL
);
265 g_assert (stun_server
== NULL
|| strcmp (string
, stun_server
) == 0);
267 g_object_get (G_OBJECT (lagent
), "stun-server-port", &port
, NULL
);
268 g_assert (stun_server_port
== NULL
|| port
== (guint
)atoi (stun_server_port
));
269 g_object_get (G_OBJECT (lagent
), "controlling-mode", &mode
, NULL
);
270 g_assert (mode
== TRUE
);
271 g_object_set (G_OBJECT (lagent
), "max-connectivity-checks", 300, NULL
);
272 g_object_get (G_OBJECT (lagent
), "max-connectivity-checks", &max_checks
, NULL
);
273 g_assert (max_checks
== 300);
276 /* step: run test the first time */
277 g_debug ("test-thread: TEST STARTS / running test for the 1st time");
279 #if !GLIB_CHECK_VERSION(2,31,8)
280 lthread
= g_thread_create (mainloop_thread
, lmainloop
, TRUE
, NULL
);
281 rthread
= g_thread_create (mainloop_thread
, rmainloop
, TRUE
, NULL
);
283 lthread
= g_thread_new ("lthread libnice", mainloop_thread
, lmainloop
);
284 rthread
= g_thread_new ("rthread libnice", mainloop_thread
, rmainloop
);
290 ls_id
= nice_agent_add_stream (lagent
, 2);
291 rs_id
= nice_agent_add_stream (ragent
, 2);
292 g_assert (ls_id
> 0);
293 g_assert (rs_id
> 0);
295 g_object_set_data (G_OBJECT (lagent
), "id", GUINT_TO_POINTER (ls_id
));
296 g_object_set_data (G_OBJECT (ragent
), "id", GUINT_TO_POINTER (rs_id
));
298 nice_agent_gather_candidates (lagent
, ls_id
);
299 nice_agent_gather_candidates (ragent
, rs_id
);
301 nice_agent_attach_recv (lagent
, ls_id
, 1, ldmainctx
, cb_nice_recv
,
302 GUINT_TO_POINTER (1));
303 nice_agent_attach_recv (ragent
, rs_id
, 1, rdmainctx
, cb_nice_recv
,
304 GUINT_TO_POINTER (2));
306 #if !GLIB_CHECK_VERSION(2,31,8)
307 ldthread
= g_thread_create (mainloop_thread
, ldmainloop
, TRUE
, NULL
);
308 rdthread
= g_thread_create (mainloop_thread
, rdmainloop
, TRUE
, NULL
);
310 ldthread
= g_thread_new ("ldthread libnice", mainloop_thread
, ldmainloop
);
311 rdthread
= g_thread_new ("rdthread libnice", mainloop_thread
, rdmainloop
);
316 /* Run loop for error timer */
317 g_main_loop_run (error_loop
);
319 while (!g_main_loop_is_running (ldmainloop
));
320 while (g_main_loop_is_running (ldmainloop
))
321 g_main_loop_quit (ldmainloop
);
322 while (!g_main_loop_is_running (rdmainloop
));
323 while (g_main_loop_is_running (rdmainloop
))
324 g_main_loop_quit (rdmainloop
);
325 while (!g_main_loop_is_running (lmainloop
));
326 while (g_main_loop_is_running (lmainloop
))
327 g_main_loop_quit (lmainloop
);
328 while (!g_main_loop_is_running (rmainloop
));
329 while (g_main_loop_is_running (rmainloop
))
330 g_main_loop_quit (rmainloop
);
332 g_thread_join (ldthread
);
333 g_thread_join (rdthread
);
334 g_thread_join (lthread
);
335 g_thread_join (rthread
);
337 /* note: verify that correct number of local candidates were reported */
338 g_assert (global_lagent_cands
== 1);
339 g_assert (global_ragent_cands
== 1);
341 g_object_unref (lagent
);
342 g_object_unref (ragent
);
344 g_main_loop_unref (lmainloop
);
345 g_main_loop_unref (rmainloop
);
346 g_main_loop_unref (ldmainloop
);
347 g_main_loop_unref (rdmainloop
);
349 g_main_loop_unref (error_loop
);