2 * This file is part of the Nice GLib ICE library.
4 * Unit test for ICE in dribble mode (adding remote candidates while the
7 * (C) 2007 Nokia Corporation. All rights reserved.
8 * Contact: Kai Vehmanen
10 * The contents of this file are subject to the Mozilla Public License Version
11 * 1.1 (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 * for the specific language governing rights and limitations under the
20 * The Original Code is the Nice GLib ICE library.
22 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
23 * Corporation. All Rights Reserved.
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
48 static NiceComponentState global_lagent_state
= NICE_COMPONENT_STATE_LAST
;
49 static NiceComponentState global_ragent_state
= NICE_COMPONENT_STATE_LAST
;
50 static guint global_components_ready
= 0;
51 static guint global_components_ready_exit
= 0;
52 static guint global_components_failed
= 0;
53 static guint global_components_failed_exit
= 0;
54 static GMainLoop
*global_mainloop
= NULL
;
55 static gboolean global_lagent_gathering_done
= FALSE
;
56 static gboolean global_ragent_gathering_done
= FALSE
;
57 static gboolean global_lagent_ibr_received
= FALSE
;
58 static gboolean global_ragent_ibr_received
= FALSE
;
59 static int global_lagent_cands
= 0;
60 static int global_ragent_cands
= 0;
61 static gint global_ragent_read
= 0;
63 static void priv_print_global_status (void)
65 g_debug ("\tgathering_done=%d", global_lagent_gathering_done
&& global_ragent_gathering_done
);
66 g_debug ("\tlstate=%d", global_lagent_state
);
67 g_debug ("\trstate=%d", global_ragent_state
);
68 g_debug ("\tL cands=%d R cands=%d", global_lagent_cands
, global_ragent_cands
);
71 static gboolean
timer_cb (gpointer pointer
)
73 g_debug ("test-dribble:%s: %p", G_STRFUNC
, pointer
);
75 /* signal status via a global variable */
77 /* note: should not be reached, abort */
78 g_error ("ERROR: test has got stuck, aborting...");
81 static gboolean
quit_loop_cb (gpointer pointer
)
83 g_debug ("test-dribble:%s: %p", G_STRFUNC
, pointer
);
85 g_main_loop_quit (global_mainloop
);
89 static void cb_nice_recv (NiceAgent
*agent
, guint stream_id
, guint component_id
, guint len
, gchar
*buf
, gpointer user_data
)
91 g_debug ("test-dribble:%s: %p", G_STRFUNC
, user_data
);
93 /* XXX: dear compiler, these are for you: */
94 (void)agent
; (void)stream_id
; (void)component_id
; (void)buf
;
97 * Lets ignore stun packets that got through
101 if (strncmp ("12345678", buf
, 8))
104 if (GPOINTER_TO_UINT (user_data
) == 2) {
105 global_ragent_read
= len
;
106 g_main_loop_quit (global_mainloop
);
110 static void cb_candidate_gathering_done(NiceAgent
*agent
, guint stream_id
, gpointer data
)
112 g_debug ("test-dribble:%s: %p", G_STRFUNC
, data
);
114 if (GPOINTER_TO_UINT (data
) == 1)
115 global_lagent_gathering_done
= TRUE
;
116 else if (GPOINTER_TO_UINT (data
) == 2)
117 global_ragent_gathering_done
= TRUE
;
119 if (global_lagent_gathering_done
&&
120 global_ragent_gathering_done
)
121 g_main_loop_quit (global_mainloop
);
123 /* XXX: dear compiler, these are for you: */
127 static void cb_component_state_changed (NiceAgent
*agent
, guint stream_id
, guint component_id
, guint state
, gpointer data
)
129 g_debug ("test-dribble:%s: %p", __func__
, data
);
131 if (GPOINTER_TO_UINT (data
) == 1)
132 global_lagent_state
= state
;
133 else if (GPOINTER_TO_UINT (data
) == 2)
134 global_ragent_state
= state
;
136 if (state
== NICE_COMPONENT_STATE_READY
)
137 global_components_ready
++;
138 if (state
== NICE_COMPONENT_STATE_FAILED
)
139 global_components_failed
++;
141 g_debug ("test-dribble: checks READY/EXIT-AT %u/%u.", global_components_ready
, global_components_ready_exit
);
142 g_debug ("test-dribble: checks FAILED/EXIT-AT %u/%u.", global_components_failed
, global_components_failed_exit
);
144 /* signal status via a global variable */
145 if (global_components_ready
== global_components_ready_exit
&&
146 global_components_failed
== global_components_failed_exit
) {
147 g_main_loop_quit (global_mainloop
);
152 /* signal status via a global variable */
153 if (global_components_failed
== global_components_failed_exit
) {
154 g_main_loop_quit (global_mainloop
);
159 /* XXX: dear compiler, these are for you: */
160 (void)agent
; (void)stream_id
; (void)data
; (void)component_id
;
163 static void cb_new_selected_pair(NiceAgent
*agent
, guint stream_id
, guint component_id
,
164 gchar
*lfoundation
, gchar
* rfoundation
, gpointer data
)
166 g_debug ("test-dribble:%s: %p", __func__
, data
);
168 if (GPOINTER_TO_UINT (data
) == 1)
169 ++global_lagent_cands
;
170 else if (GPOINTER_TO_UINT (data
) == 2)
171 ++global_ragent_cands
;
173 /* XXX: dear compiler, these are for you: */
174 (void)agent
; (void)stream_id
; (void)component_id
; (void)lfoundation
; (void)rfoundation
;
177 static void cb_new_candidate(NiceAgent
*agent
, guint stream_id
, guint component_id
,
178 gchar
*foundation
, gpointer data
)
180 g_debug ("test-dribble:%s: %p", __func__
, data
);
182 /* XXX: dear compiler, these are for you: */
183 (void)agent
; (void)stream_id
; (void)data
; (void)component_id
; (void)foundation
;
186 static void cb_initial_binding_request_received(NiceAgent
*agent
, guint stream_id
, gpointer data
)
188 g_debug ("test-dribble:%s: %p", __func__
, data
);
190 if (GPOINTER_TO_UINT (data
) == 1)
191 global_lagent_ibr_received
= TRUE
;
192 else if (GPOINTER_TO_UINT (data
) == 2)
193 global_ragent_ibr_received
= TRUE
;
195 /* XXX: dear compiler, these are for you: */
196 (void)agent
; (void)stream_id
; (void)data
;
202 NiceAgent
*lagent
, *ragent
; /* agent's L and R */
203 NiceAddress baseaddr
;
209 g_thread_init (NULL
);
210 global_mainloop
= g_main_loop_new (NULL
, FALSE
);
212 /* step: create the agents L and R */
213 lagent
= nice_agent_new (g_main_loop_get_context (global_mainloop
),
214 NICE_COMPATIBILITY_GOOGLE
);
215 ragent
= nice_agent_new (g_main_loop_get_context (global_mainloop
),
216 NICE_COMPATIBILITY_GOOGLE
);
218 if (!nice_address_set_from_string (&baseaddr
, "127.0.0.1"))
219 g_assert_not_reached ();
220 nice_agent_add_local_address (lagent
, &baseaddr
);
221 nice_agent_add_local_address (ragent
, &baseaddr
);
223 /* step: add a timer to catch state changes triggered by signals */
224 timer_id
= g_timeout_add (30000, timer_cb
, NULL
);
226 g_signal_connect (G_OBJECT (lagent
), "candidate-gathering-done",
227 G_CALLBACK (cb_candidate_gathering_done
), GUINT_TO_POINTER(1));
228 g_signal_connect (G_OBJECT (ragent
), "candidate-gathering-done",
229 G_CALLBACK (cb_candidate_gathering_done
), GUINT_TO_POINTER (2));
230 g_signal_connect (G_OBJECT (lagent
), "component-state-changed",
231 G_CALLBACK (cb_component_state_changed
), GUINT_TO_POINTER (1));
232 g_signal_connect (G_OBJECT (ragent
), "component-state-changed",
233 G_CALLBACK (cb_component_state_changed
), GUINT_TO_POINTER (2));
234 g_signal_connect (G_OBJECT (lagent
), "new-selected-pair",
235 G_CALLBACK (cb_new_selected_pair
), GUINT_TO_POINTER(1));
236 g_signal_connect (G_OBJECT (ragent
), "new-selected-pair",
237 G_CALLBACK (cb_new_selected_pair
), GUINT_TO_POINTER (2));
238 g_signal_connect (G_OBJECT (lagent
), "new-candidate",
239 G_CALLBACK (cb_new_candidate
), GUINT_TO_POINTER (1));
240 g_signal_connect (G_OBJECT (ragent
), "new-candidate",
241 G_CALLBACK (cb_new_candidate
), GUINT_TO_POINTER (2));
242 g_signal_connect (G_OBJECT (lagent
), "initial-binding-request-received",
243 G_CALLBACK (cb_initial_binding_request_received
),
244 GUINT_TO_POINTER (1));
245 g_signal_connect (G_OBJECT (ragent
), "initial-binding-request-received",
246 G_CALLBACK (cb_initial_binding_request_received
),
247 GUINT_TO_POINTER (2));
250 g_debug ("test-dribble: running test");
252 /* step: initialize variables modified by the callbacks */
253 global_components_ready
= 0;
254 global_components_ready_exit
= 2;
255 global_components_failed
= 0;
256 global_components_failed_exit
= 0;
257 global_lagent_gathering_done
= FALSE
;
258 global_ragent_gathering_done
= FALSE
;
259 global_lagent_ibr_received
=
260 global_ragent_ibr_received
= FALSE
;
261 global_lagent_cands
=
262 global_ragent_cands
= 0;
264 g_object_set (G_OBJECT (lagent
), "controlling-mode", TRUE
, NULL
);
265 g_object_set (G_OBJECT (ragent
), "controlling-mode", FALSE
, NULL
);
267 /* step: add one stream, with RTP+RTCP components, to each agent */
268 ls_id
= nice_agent_add_stream (lagent
, 1);
270 rs_id
= nice_agent_add_stream (ragent
, 1);
271 g_assert (ls_id
> 0);
272 g_assert (rs_id
> 0);
275 nice_agent_gather_candidates (lagent
, ls_id
);
276 nice_agent_gather_candidates (ragent
, rs_id
);
278 /* step: attach to mainloop (needed to register the fds) */
279 nice_agent_attach_recv (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
,
280 g_main_loop_get_context (global_mainloop
), cb_nice_recv
,
281 GUINT_TO_POINTER (1));
282 nice_agent_attach_recv (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
,
283 g_main_loop_get_context (global_mainloop
), cb_nice_recv
,
284 GUINT_TO_POINTER (2));
286 /* step: run mainloop until local candidates are ready
287 * (see timer_cb() above) */
288 if (global_lagent_gathering_done
!= TRUE
||
289 global_ragent_gathering_done
!= TRUE
) {
290 g_debug ("test-dribble: Added streams, running mainloop until 'candidate-gathering-done'...");
291 g_main_loop_run (global_mainloop
);
292 g_assert (global_lagent_gathering_done
== TRUE
);
293 g_assert (global_ragent_gathering_done
== TRUE
);
297 gchar
*ufrag
= NULL
, *password
= NULL
;
298 nice_agent_get_local_credentials(lagent
, ls_id
, &ufrag
, &password
);
299 nice_agent_set_remote_credentials (ragent
,
300 rs_id
, ufrag
, password
);
303 nice_agent_get_local_credentials(ragent
, rs_id
, &ufrag
, &password
);
304 nice_agent_set_remote_credentials (lagent
,
305 ls_id
, ufrag
, password
);
309 cands
= nice_agent_get_local_candidates (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
);
310 nice_agent_set_remote_candidates (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
, cands
);
311 for (i
= cands
; i
; i
= i
->next
)
312 nice_candidate_free ((NiceCandidate
*) i
->data
);
313 g_slist_free (cands
);
314 cands
= nice_agent_get_local_candidates (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
);
315 nice_agent_set_remote_candidates (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
, cands
);
316 for (i
= cands
; i
; i
= i
->next
)
317 nice_candidate_free ((NiceCandidate
*) i
->data
);
318 g_slist_free (cands
);
320 g_debug ("test-dribble: Set properties, next running mainloop until connectivity checks succeed...");
322 /* step: run the mainloop until connectivity checks succeed
323 * (see timer_cb() above) */
324 g_main_loop_run (global_mainloop
);
326 /* note: verify that STUN binding requests were sent */
327 g_assert (global_lagent_ibr_received
== TRUE
);
328 g_assert (global_ragent_ibr_received
== TRUE
);
330 g_assert (global_lagent_state
== NICE_COMPONENT_STATE_READY
);
331 g_assert (global_ragent_state
== NICE_COMPONENT_STATE_READY
);
332 /* note: verify that correct number of local candidates were reported */
333 g_assert (global_lagent_cands
== 1);
334 g_assert (global_ragent_cands
== 1);
336 g_debug ("test-dribble: agents are ready.. now adding new buggy candidate");
338 g_timeout_add (500, quit_loop_cb
, NULL
);
339 g_main_loop_run (global_mainloop
);
341 global_components_ready
--;
343 cands
= nice_agent_get_local_candidates (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
);
344 nice_address_set_port(&((NiceCandidate
*) cands
->data
)->addr
, 80);
345 nice_agent_set_remote_candidates (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
, cands
);
346 for (i
= cands
; i
; i
= i
->next
)
347 nice_candidate_free ((NiceCandidate
*) i
->data
);
348 g_slist_free (cands
);
350 g_assert (global_lagent_state
== NICE_COMPONENT_STATE_CONNECTED
);
351 g_main_loop_run (global_mainloop
);
352 g_assert (global_lagent_state
== NICE_COMPONENT_STATE_READY
);
355 g_debug ("test-dribble: buggy candidate worked, testing lower priority cand");
357 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
358 nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
359 ((NiceCandidate *) cands->data)->priority -= 100;
360 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
361 for (i = cands; i; i = i->next)
362 nice_candidate_free ((NiceCandidate *) i->data);
363 g_slist_free (cands);
365 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);*/
367 /* note: test payload send and receive */
368 global_ragent_read
= 0;
369 g_assert (nice_agent_send (lagent
, ls_id
, 1, 16, "1234567812345678") == 16);
370 g_main_loop_run (global_mainloop
);
371 g_assert (global_ragent_read
== 16);
373 g_debug ("test-dribble: Ran mainloop, removing streams...");
375 /* step: clean up resources and exit */
377 nice_agent_remove_stream (lagent
, ls_id
);
378 nice_agent_remove_stream (ragent
, rs_id
);
379 priv_print_global_status ();
380 g_assert (global_lagent_state
== NICE_COMPONENT_STATE_READY
);
381 g_assert (global_ragent_state
== NICE_COMPONENT_STATE_READY
);
382 /* note: verify that correct number of local candidates were reported */
383 g_assert (global_lagent_cands
== 1);
384 g_assert (global_ragent_cands
== 1);
387 g_object_unref (lagent
);
388 g_object_unref (ragent
);
390 g_main_loop_unref (global_mainloop
);
391 global_mainloop
= NULL
;
393 g_source_remove (timer_id
);