version 0.1.1
[sipe-libnice.git] / tests / test-dribble.c
blob42865863f39ad32621340e769709470979d0e956
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * Unit test for ICE in dribble mode (adding remote candidates while the
5 * machine is running).
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
18 * License.
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.
25 * Contributors:
26 * Kai Vehmanen, Nokia
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.
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
42 #include "agent.h"
44 #include <stdlib.h>
45 #include <string.h>
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...");
80 return FALSE;
83 static gboolean quit_loop_cb (gpointer pointer)
85 g_debug ("test-dribble:%s: %p", G_STRFUNC, pointer);
87 g_main_loop_quit (global_mainloop);
88 return FALSE;
91 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
93 g_debug ("test-dribble:%s: %p", G_STRFUNC, user_data);
95 /* XXX: dear compiler, these are for you: */
96 (void)agent; (void)stream_id; (void)component_id; (void)buf;
99 * Lets ignore stun packets that got through
101 if (len < 8)
102 return;
103 if (strncmp ("12345678", buf, 8))
104 return;
106 if (GPOINTER_TO_UINT (user_data) == 2) {
107 global_ragent_read = len;
108 g_main_loop_quit (global_mainloop);
112 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
114 g_debug ("test-dribble:%s: %p", G_STRFUNC, data);
116 if (GPOINTER_TO_UINT (data) == 1)
117 global_lagent_gathering_done = TRUE;
118 else if (GPOINTER_TO_UINT (data) == 2)
119 global_ragent_gathering_done = TRUE;
121 if (global_lagent_gathering_done &&
122 global_ragent_gathering_done)
123 g_main_loop_quit (global_mainloop);
125 /* XXX: dear compiler, these are for you: */
126 (void)agent;
129 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
131 g_debug ("test-dribble:%s: %p", __func__, data);
133 if (GPOINTER_TO_UINT (data) == 1)
134 global_lagent_state = state;
135 else if (GPOINTER_TO_UINT (data) == 2)
136 global_ragent_state = state;
138 if (state == NICE_COMPONENT_STATE_READY)
139 global_components_ready++;
140 if (state == NICE_COMPONENT_STATE_FAILED)
141 global_components_failed++;
143 g_debug ("test-dribble: checks READY/EXIT-AT %u/%u.", global_components_ready, global_components_ready_exit);
144 g_debug ("test-dribble: checks FAILED/EXIT-AT %u/%u.", global_components_failed, global_components_failed_exit);
146 /* signal status via a global variable */
147 if (global_components_ready == global_components_ready_exit &&
148 global_components_failed == global_components_failed_exit) {
149 g_main_loop_quit (global_mainloop);
150 return;
153 #if 0
154 /* signal status via a global variable */
155 if (global_components_failed == global_components_failed_exit) {
156 g_main_loop_quit (global_mainloop);
157 return;
159 #endif
161 /* XXX: dear compiler, these are for you: */
162 (void)agent; (void)stream_id; (void)data; (void)component_id;
165 static void cb_new_selected_pair(NiceAgent *agent, guint stream_id, guint component_id,
166 gchar *lfoundation, gchar* rfoundation, gpointer data)
168 g_debug ("test-dribble:%s: %p", __func__, data);
170 if (GPOINTER_TO_UINT (data) == 1)
171 ++global_lagent_cands;
172 else if (GPOINTER_TO_UINT (data) == 2)
173 ++global_ragent_cands;
175 /* XXX: dear compiler, these are for you: */
176 (void)agent; (void)stream_id; (void)component_id; (void)lfoundation; (void)rfoundation;
179 static void cb_new_candidate(NiceAgent *agent, guint stream_id, guint component_id,
180 gchar *foundation, gpointer data)
182 g_debug ("test-dribble:%s: %p", __func__, data);
184 /* XXX: dear compiler, these are for you: */
185 (void)agent; (void)stream_id; (void)data; (void)component_id; (void)foundation;
188 static void cb_initial_binding_request_received(NiceAgent *agent, guint stream_id, gpointer data)
190 g_debug ("test-dribble:%s: %p", __func__, data);
192 if (GPOINTER_TO_UINT (data) == 1)
193 global_lagent_ibr_received = TRUE;
194 else if (GPOINTER_TO_UINT (data) == 2)
195 global_ragent_ibr_received = TRUE;
197 /* XXX: dear compiler, these are for you: */
198 (void)agent; (void)stream_id; (void)data;
202 int main (void)
204 NiceAgent *lagent, *ragent; /* agent's L and R */
205 NiceAddress baseaddr;
206 guint timer_id;
207 GSList *cands, *i;
208 guint ls_id, rs_id;
210 g_type_init ();
211 g_thread_init (NULL);
212 global_mainloop = g_main_loop_new (NULL, FALSE);
214 /* step: create the agents L and R */
215 lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
216 NICE_COMPATIBILITY_GOOGLE);
217 ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
218 NICE_COMPATIBILITY_GOOGLE);
220 if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
221 g_assert_not_reached ();
222 nice_agent_add_local_address (lagent, &baseaddr);
223 nice_agent_add_local_address (ragent, &baseaddr);
225 /* step: add a timer to catch state changes triggered by signals */
226 timer_id = g_timeout_add (30000, timer_cb, NULL);
228 g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
229 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
230 g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
231 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER (2));
232 g_signal_connect (G_OBJECT (lagent), "component-state-changed",
233 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (1));
234 g_signal_connect (G_OBJECT (ragent), "component-state-changed",
235 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (2));
236 g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
237 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
238 g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
239 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
240 g_signal_connect (G_OBJECT (lagent), "new-candidate",
241 G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (1));
242 g_signal_connect (G_OBJECT (ragent), "new-candidate",
243 G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (2));
244 g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received",
245 G_CALLBACK (cb_initial_binding_request_received),
246 GUINT_TO_POINTER (1));
247 g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
248 G_CALLBACK (cb_initial_binding_request_received),
249 GUINT_TO_POINTER (2));
251 /* step: run test */
252 g_debug ("test-dribble: running test");
254 /* step: initialize variables modified by the callbacks */
255 global_components_ready = 0;
256 global_components_ready_exit = 2;
257 global_components_failed = 0;
258 global_components_failed_exit = 0;
259 global_lagent_gathering_done = FALSE;
260 global_ragent_gathering_done = FALSE;
261 global_lagent_ibr_received =
262 global_ragent_ibr_received = FALSE;
263 global_lagent_cands =
264 global_ragent_cands = 0;
266 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
267 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
269 /* step: add one stream, with RTP+RTCP components, to each agent */
270 ls_id = nice_agent_add_stream (lagent, 1);
272 rs_id = nice_agent_add_stream (ragent, 1);
273 g_assert (ls_id > 0);
274 g_assert (rs_id > 0);
277 nice_agent_gather_candidates (lagent, ls_id);
278 nice_agent_gather_candidates (ragent, rs_id);
280 /* step: attach to mainloop (needed to register the fds) */
281 nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTP,
282 g_main_loop_get_context (global_mainloop), cb_nice_recv,
283 GUINT_TO_POINTER (1));
284 nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
285 g_main_loop_get_context (global_mainloop), cb_nice_recv,
286 GUINT_TO_POINTER (2));
288 /* step: run mainloop until local candidates are ready
289 * (see timer_cb() above) */
290 if (global_lagent_gathering_done != TRUE ||
291 global_ragent_gathering_done != TRUE) {
292 g_debug ("test-dribble: Added streams, running mainloop until 'candidate-gathering-done'...");
293 g_main_loop_run (global_mainloop);
294 g_assert (global_lagent_gathering_done == TRUE);
295 g_assert (global_ragent_gathering_done == TRUE);
299 gchar *ufrag = NULL, *password = NULL;
300 nice_agent_get_local_credentials(lagent, ls_id, &ufrag, &password);
301 nice_agent_set_remote_credentials (ragent,
302 rs_id, ufrag, password);
303 g_free (ufrag);
304 g_free (password);
305 nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
306 nice_agent_set_remote_credentials (lagent,
307 ls_id, ufrag, password);
308 g_free (ufrag);
309 g_free (password);
311 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
312 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
313 for (i = cands; i; i = i->next)
314 nice_candidate_free ((NiceCandidate *) i->data);
315 g_slist_free (cands);
316 cands = nice_agent_get_local_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
317 nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, cands);
318 for (i = cands; i; i = i->next)
319 nice_candidate_free ((NiceCandidate *) i->data);
320 g_slist_free (cands);
322 g_debug ("test-dribble: Set properties, next running mainloop until connectivity checks succeed...");
324 /* step: run the mainloop until connectivity checks succeed
325 * (see timer_cb() above) */
326 g_main_loop_run (global_mainloop);
328 /* note: verify that STUN binding requests were sent */
329 g_assert (global_lagent_ibr_received == TRUE);
330 g_assert (global_ragent_ibr_received == TRUE);
332 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
333 g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY);
334 /* note: verify that correct number of local candidates were reported */
335 g_assert (global_lagent_cands == 1);
336 g_assert (global_ragent_cands == 1);
338 g_debug ("test-dribble: agents are ready.. now adding new buggy candidate");
340 g_timeout_add (500, quit_loop_cb, NULL);
341 g_main_loop_run (global_mainloop);
343 global_components_ready--;
345 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
346 nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
347 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
348 for (i = cands; i; i = i->next)
349 nice_candidate_free ((NiceCandidate *) i->data);
350 g_slist_free (cands);
352 g_assert (global_lagent_state == NICE_COMPONENT_STATE_CONNECTED);
353 g_main_loop_run (global_mainloop);
354 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
357 g_debug ("test-dribble: buggy candidate worked, testing lower priority cand");
359 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
360 nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
361 ((NiceCandidate *) cands->data)->priority -= 100;
362 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
363 for (i = cands; i; i = i->next)
364 nice_candidate_free ((NiceCandidate *) i->data);
365 g_slist_free (cands);
367 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);*/
369 /* note: test payload send and receive */
370 global_ragent_read = 0;
371 g_assert (nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678") == 16);
372 g_main_loop_run (global_mainloop);
373 g_assert (global_ragent_read == 16);
375 g_debug ("test-dribble: Ran mainloop, removing streams...");
377 /* step: clean up resources and exit */
379 nice_agent_remove_stream (lagent, ls_id);
380 nice_agent_remove_stream (ragent, rs_id);
381 priv_print_global_status ();
382 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
383 g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY);
384 /* note: verify that correct number of local candidates were reported */
385 g_assert (global_lagent_cands == 1);
386 g_assert (global_ragent_cands == 1);
389 g_object_unref (lagent);
390 g_object_unref (ragent);
392 g_main_loop_unref (global_mainloop);
393 global_mainloop = NULL;
395 g_source_remove (timer_id);
397 return 0;