2 * This file is part of the Nice GLib ICE library.
4 * (C) 2007 Nokia Corporation. All rights reserved.
5 * Contact: Kai Vehmanen
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is the Nice GLib ICE library.
19 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20 * Corporation. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of the
26 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27 * case the provisions of LGPL are applicable instead of those above. If you
28 * wish to allow use of your version of this file only under the terms of the
29 * LGPL and not to allow others to use your version of this file under the
30 * MPL, indicate your decision by deleting the provisions above and replace
31 * them with the notice and other provisions required by the LGPL. If you do
32 * not delete the provisions above, a recipient may use your version of this
33 * file under either the MPL or the LGPL.
40 #include "agent-priv.h" /* for testing purposes */
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;
62 static gint global_ragent_read_exit = 0;
64 static void priv_print_global_status (void)
66 g_debug ("\tgathering_done=%d", global_lagent_gathering_done && global_ragent_gathering_done);
67 g_debug ("\tlstate=%d", global_lagent_state);
68 g_debug ("\trstate=%d", global_ragent_state);
71 static gboolean timer_cb (gpointer pointer)
73 g_debug ("test-restart:%s: %p", G_STRFUNC, pointer);
75 /* signal status via a global variable */
77 /* note: should not be reached, abort */
78 g_debug ("ERROR: test has got stuck, aborting...");
83 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
85 g_debug ("test-restart:%s: %p", G_STRFUNC, user_data);
87 /* XXX: dear compiler, these are for you: */
88 (void)agent; (void)stream_id; (void)component_id; (void)buf;
90 if ((intptr_t)user_data == 2) {
91 global_ragent_read += len;
93 if (global_ragent_read == global_ragent_read_exit)
94 g_main_loop_quit (global_mainloop);
98 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
100 g_debug ("test-restart:%s: %p", G_STRFUNC, data);
102 if ((intptr_t)data == 1)
103 global_lagent_gathering_done = TRUE;
104 else if ((intptr_t)data == 2)
105 global_ragent_gathering_done = TRUE;
107 if (global_lagent_gathering_done &&
108 global_ragent_gathering_done)
109 g_main_loop_quit (global_mainloop);
111 /* XXX: dear compiler, these are for you: */
115 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
117 g_debug ("test-restart:%s: %p", G_STRFUNC, data);
119 if ((intptr_t)data == 1)
120 global_lagent_state = state;
121 else if ((intptr_t)data == 2)
122 global_ragent_state = state;
124 if (state == NICE_COMPONENT_STATE_READY)
125 global_components_ready++;
126 if (state == NICE_COMPONENT_STATE_FAILED)
127 global_components_failed++;
129 g_debug ("test-restart: READY %u exit at %u.", global_components_ready, global_components_ready_exit);
131 /* signal status via a global variable */
132 if (global_components_ready == global_components_ready_exit) {
133 g_main_loop_quit (global_mainloop);
137 /* signal status via a global variable */
138 if (global_components_failed == global_components_failed_exit) {
139 g_main_loop_quit (global_mainloop);
143 /* XXX: dear compiler, these are for you: */
144 (void)agent; (void)stream_id; (void)data; (void)component_id;
147 static void cb_new_selected_pair(NiceAgent *agent, guint stream_id, guint component_id,
148 gchar *lfoundation, gchar* rfoundation, gpointer data)
150 g_debug ("test-restart:%s: %p", G_STRFUNC, data);
152 if ((intptr_t)data == 1)
153 ++global_lagent_cands;
154 else if ((intptr_t)data == 2)
155 ++global_ragent_cands;
157 /* XXX: dear compiler, these are for you: */
158 (void)agent; (void)stream_id; (void)component_id; (void)lfoundation; (void)rfoundation;
161 static void cb_new_candidate(NiceAgent *agent, guint stream_id, guint component_id,
162 gchar *foundation, gpointer data)
164 g_debug ("test-restart:%s: %p", G_STRFUNC, data);
166 /* XXX: dear compiler, these are for you: */
167 (void)agent; (void)stream_id; (void)data; (void)component_id; (void)foundation;
170 static void cb_initial_binding_request_received(NiceAgent *agent, guint stream_id, gpointer data)
172 g_debug ("test-restart:%s: %p", G_STRFUNC, data);
174 if ((intptr_t)data == 1)
175 global_lagent_ibr_received = TRUE;
176 else if ((intptr_t)data == 2)
177 global_ragent_ibr_received = TRUE;
179 /* XXX: dear compiler, these are for you: */
180 (void)agent; (void)stream_id; (void)data;
183 static void priv_get_local_addr (NiceAgent *agent, guint stream_id, guint component_id, NiceAddress *dstaddr)
186 cands = nice_agent_get_local_candidates(agent, stream_id, component_id);
187 for (i = cands; i; i = i->next) {
188 NiceCandidate *cand = i->data;
191 *dstaddr = cand->addr;
194 for (i = cands; i; i = i->next)
195 nice_candidate_free ((NiceCandidate *) i->data);
196 g_slist_free (cands);
199 static int run_restart_test (NiceAgent *lagent, NiceAgent *ragent, NiceAddress *baseaddr)
201 NiceAddress laddr, raddr, laddr_rtcp, raddr_rtcp;
207 /* XXX: dear compiler, these are for you: */
210 memset (&cdes, 0, sizeof(NiceCandidate));
211 cdes.priority = 10000;
212 strcpy (cdes.foundation, "1");
213 cdes.type = NICE_CANDIDATE_TYPE_HOST;
214 cdes.transport = NICE_CANDIDATE_TRANSPORT_UDP;
216 /* step: initialize variables modified by the callbacks */
217 global_components_ready = 0;
218 global_components_ready_exit = 4;
219 global_components_failed = 0;
220 global_components_failed_exit = 4;
221 global_lagent_gathering_done = FALSE;
222 global_ragent_gathering_done = FALSE;
223 global_lagent_ibr_received =
224 global_ragent_ibr_received = FALSE;
225 global_lagent_cands =
226 global_ragent_cands = 0;
227 global_ragent_read_exit = -1;
229 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
230 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
232 /* step: add one stream, with RTP+RTCP components, to each agent */
233 ls_id = nice_agent_add_stream (lagent, 2);
234 rs_id = nice_agent_add_stream (ragent, 2);
235 g_assert_cmpuint (ls_id, >, 0);
236 g_assert_cmpuint (rs_id, >, 0);
238 nice_agent_gather_candidates (lagent, ls_id);
239 nice_agent_gather_candidates (ragent, rs_id);
241 /* step: attach to mainloop (needed to register the fds) */
242 nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTP,
243 g_main_loop_get_context (global_mainloop), cb_nice_recv, (gpointer)1);
244 nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP,
245 g_main_loop_get_context (global_mainloop), cb_nice_recv, (gpointer)1);
246 nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
247 g_main_loop_get_context (global_mainloop), cb_nice_recv, (gpointer)2);
248 nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTCP,
249 g_main_loop_get_context (global_mainloop), cb_nice_recv, (gpointer)2);
251 /* step: run mainloop until local candidates are ready
252 * (see timer_cb() above) */
253 if (global_lagent_gathering_done != TRUE ||
254 global_ragent_gathering_done != TRUE) {
255 g_debug ("test-restart: Added streams, running mainloop until 'candidate-gathering-done'...");
256 g_main_loop_run (global_mainloop);
257 g_assert (global_lagent_gathering_done == TRUE);
258 g_assert (global_ragent_gathering_done == TRUE);
261 /* step: find out the local candidates of each agent */
263 priv_get_local_addr (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, &raddr);
264 g_debug ("test-restart: local RTP port R %u",
265 nice_address_get_port (&raddr));
267 priv_get_local_addr (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, &laddr);
268 g_debug ("test-restart: local RTP port L %u",
269 nice_address_get_port (&laddr));
271 priv_get_local_addr (ragent, rs_id, NICE_COMPONENT_TYPE_RTCP, &raddr_rtcp);
272 g_debug ("test-restart: local RTCP port R %u",
273 nice_address_get_port (&raddr_rtcp));
275 priv_get_local_addr (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP, &laddr_rtcp);
276 g_debug ("test-restart: local RTCP port L %u",
277 nice_address_get_port (&laddr_rtcp));
279 /* step: pass the remote candidates to agents */
280 cands = g_slist_append (NULL, &cdes);
282 gchar *ufrag = NULL, *password = NULL;
283 nice_agent_get_local_credentials(lagent, ls_id, &ufrag, &password);
284 nice_agent_set_remote_credentials (ragent,
285 rs_id, ufrag, password);
288 nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
289 nice_agent_set_remote_credentials (lagent,
290 ls_id, ufrag, password);
294 cdes.component_id = NICE_COMPONENT_TYPE_RTP;
296 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
298 nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, cands);
299 cdes.component_id = NICE_COMPONENT_TYPE_RTCP;
300 cdes.addr = raddr_rtcp;
301 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP, cands);
302 cdes.addr = laddr_rtcp;
303 nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTCP, cands);
304 /* This role switch request will be effective after restart. We test
305 * here that the role cannot be externally modified after conncheck
307 g_object_set (G_OBJECT (ragent), "controlling-mode", TRUE, NULL);
308 g_assert (ragent->controlling_mode == FALSE);
310 g_debug ("test-restart: Set properties, next running mainloop until connectivity checks succeed...");
312 /* step: run the mainloop until connectivity checks succeed
313 * (see timer_cb() above) */
314 g_main_loop_run (global_mainloop);
316 /* note: verify that STUN binding requests were sent */
317 g_assert (global_lagent_ibr_received == TRUE);
318 g_assert (global_ragent_ibr_received == TRUE);
319 /* note: verify that correct number of local candidates were reported */
320 g_assert_cmpint (global_lagent_cands, ==, 2);
321 g_assert_cmpint (global_ragent_cands, ==, 2);
322 /* note: verify that agents are in correct state */
323 g_assert_cmpint (global_lagent_state, ==, NICE_COMPONENT_STATE_READY);
324 g_assert_cmpint (global_ragent_state, ==, NICE_COMPONENT_STATE_READY);
326 /* step: next send a packet (should work during restart) and
327 * then request an ICE restart by resetting the remote
328 * candidates for agent R */
330 g_debug ("-------------------------------------------\n"
331 "test-restart: Requesting a RESTART...");
333 /* step: send a new test packet from L ot R */
334 global_ragent_read = 0;
335 g_assert_cmpint (nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678"), ==, 16);
337 /* Both agent have a distinct role at the end of the conncheck */
338 g_assert (lagent->controlling_mode == TRUE);
339 g_assert (ragent->controlling_mode == FALSE);
340 /* step: restart agents, exchange updated credentials */
341 tie_breaker = ragent->tie_breaker;
342 nice_agent_restart (ragent);
343 g_assert (tie_breaker != ragent->tie_breaker);
344 /* This role switch of ragent should be done now, and both agents
345 * have now the same role, which should generate a role conflict
346 * resolution situation */
347 g_assert (lagent->controlling_mode == TRUE);
348 g_assert (ragent->controlling_mode == TRUE);
349 nice_agent_restart (lagent);
351 gchar *ufrag = NULL, *password = NULL;
352 nice_agent_get_local_credentials(lagent, ls_id, &ufrag, &password);
353 nice_agent_set_remote_credentials (ragent,
354 rs_id, ufrag, password);
357 nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
358 nice_agent_set_remote_credentials (lagent,
359 ls_id, ufrag, password);
364 /* send another packet after restart */
365 g_assert_cmpint (nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678"), ==, 16);
367 /* step: reset state variables */
368 global_lagent_ibr_received = FALSE;
369 global_ragent_ibr_received = FALSE;
370 global_components_ready = 0;
372 /* step: exchange remote candidates */
373 cdes.component_id = NICE_COMPONENT_TYPE_RTP;
375 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
377 nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, cands);
378 cdes.component_id = NICE_COMPONENT_TYPE_RTCP;
379 cdes.addr = raddr_rtcp;
380 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP, cands);
381 cdes.addr = laddr_rtcp;
382 nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTCP, cands);
384 g_main_loop_run (global_mainloop);
386 /* note: verify that payload was succesfully received */
387 g_assert_cmpint (global_ragent_read, ==, 32);
388 /* note: verify binding requests were resent after restart */
389 g_assert (global_lagent_ibr_received == TRUE);
390 g_assert (global_ragent_ibr_received == TRUE);
391 /* note: verify that a role switch occured for one of the agents */
392 g_assert (ragent->controlling_mode != lagent->controlling_mode);
394 g_debug ("test-restart: Ran mainloop, removing streams...");
396 /* step: clean up resources and exit */
398 g_slist_free (cands);
399 nice_agent_remove_stream (lagent, ls_id);
400 nice_agent_remove_stream (ragent, rs_id);
407 NiceAgent *lagent, *ragent; /* agent's L and R */
408 NiceAddress baseaddr;
411 const char *stun_server = NULL, *stun_server_port = NULL;
416 WSAStartup(0x0202, &w);
419 global_mainloop = g_main_loop_new (NULL, FALSE);
421 /* Note: impl limits ...
422 * - no multi-stream support
427 /* step: create the agents L and R */
428 lagent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245);
429 ragent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245);
430 g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE, NULL);
431 g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE, NULL);
433 g_object_set (G_OBJECT (lagent), "upnp", FALSE, NULL);
434 g_object_set (G_OBJECT (ragent), "upnp", FALSE, NULL);
436 /* step: add a timer to catch state changes triggered by signals */
437 timer_id = g_timeout_add (30000, timer_cb, NULL);
439 /* step: specify which local interface to use */
440 if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
441 g_assert_not_reached ();
442 nice_agent_add_local_address (lagent, &baseaddr);
443 nice_agent_add_local_address (ragent, &baseaddr);
445 g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
446 G_CALLBACK (cb_candidate_gathering_done), (gpointer)1);
447 g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
448 G_CALLBACK (cb_candidate_gathering_done), (gpointer)2);
449 g_signal_connect (G_OBJECT (lagent), "component-state-changed",
450 G_CALLBACK (cb_component_state_changed), (gpointer)1);
451 g_signal_connect (G_OBJECT (ragent), "component-state-changed",
452 G_CALLBACK (cb_component_state_changed), (gpointer)2);
453 g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
454 G_CALLBACK (cb_new_selected_pair), (gpointer)1);
455 g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
456 G_CALLBACK (cb_new_selected_pair), (gpointer)2);
457 g_signal_connect (G_OBJECT (lagent), "new-candidate",
458 G_CALLBACK (cb_new_candidate), (gpointer)1);
459 g_signal_connect (G_OBJECT (ragent), "new-candidate",
460 G_CALLBACK (cb_new_candidate), (gpointer)2);
461 g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received",
462 G_CALLBACK (cb_initial_binding_request_received), (gpointer)1);
463 g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
464 G_CALLBACK (cb_initial_binding_request_received), (gpointer)2);
466 stun_server = getenv ("NICE_STUN_SERVER");
467 stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
469 g_object_set (G_OBJECT (lagent), "stun-server", stun_server, NULL);
470 g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port), NULL);
471 g_object_set (G_OBJECT (ragent), "stun-server", stun_server, NULL);
472 g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port), NULL);
475 /* step: run test the first time */
476 g_debug ("test-restart: TEST STARTS / restart test");
477 result = run_restart_test (lagent, ragent, &baseaddr);
478 priv_print_global_status ();
479 g_assert_cmpint (result, ==, 0);
480 g_assert_cmpint (global_lagent_state, ==, NICE_COMPONENT_STATE_READY);
481 g_assert_cmpint (global_ragent_state, ==, NICE_COMPONENT_STATE_READY);
483 g_object_unref (lagent);
484 g_object_unref (ragent);
487 g_main_loop_unref (global_mainloop);
488 global_mainloop = NULL;
490 g_source_remove (timer_id);