064cd6903705c233a01db87f47a5a8f1c9780784
[platform/upstream/libnice.git] / tests / test-restart.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2007 Nokia Corporation. All rights reserved.
5  *  Contact: Kai Vehmanen
6  *
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/
11  *
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
15  * License.
16  *
17  * The Original Code is the Nice GLib ICE library.
18  *
19  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20  * Corporation. All Rights Reserved.
21  *
22  * Contributors:
23  *   Kai Vehmanen, Nokia
24  *
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.
34  */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include "agent.h"
40 #include "agent-priv.h" /* for testing purposes */
41
42 #include <stdlib.h>
43 #include <string.h>
44 #ifdef _WIN32
45 #include <io.h>
46 #endif
47
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;
63
64 static void priv_print_global_status (void)
65 {
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);
69 }
70
71 static gboolean timer_cb (gpointer pointer)
72 {
73   g_debug ("test-restart:%s: %p", G_STRFUNC, pointer);
74
75   /* signal status via a global variable */
76
77   /* note: should not be reached, abort */
78   g_debug ("ERROR: test has got stuck, aborting...");
79   exit (-1);
80
81 }
82
83 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
84 {
85   g_debug ("test-restart:%s: %p", G_STRFUNC, user_data);
86
87   /* XXX: dear compiler, these are for you: */
88   (void)agent; (void)stream_id; (void)component_id; (void)buf;
89
90   if ((intptr_t)user_data == 2) {
91     global_ragent_read += len;
92
93     if (global_ragent_read == global_ragent_read_exit)
94       g_main_loop_quit (global_mainloop);
95   }
96 }
97
98 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
99 {
100   g_debug ("test-restart:%s: %p", G_STRFUNC, data);
101
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;
106
107   if (global_lagent_gathering_done &&
108       global_ragent_gathering_done)
109     g_main_loop_quit (global_mainloop);
110
111   /* XXX: dear compiler, these are for you: */
112   (void)agent;
113 }
114
115 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
116 {
117   g_debug ("test-restart:%s: %p", G_STRFUNC, data);
118
119   if ((intptr_t)data == 1)
120     global_lagent_state = state;
121   else if ((intptr_t)data == 2)
122     global_ragent_state = state;
123   
124   if (state == NICE_COMPONENT_STATE_READY)
125     global_components_ready++;
126   if (state == NICE_COMPONENT_STATE_FAILED)
127     global_components_failed++;
128
129   g_debug ("test-restart: READY %u exit at %u.", global_components_ready, global_components_ready_exit);
130
131   /* signal status via a global variable */
132   if (global_components_ready == global_components_ready_exit) {
133     g_main_loop_quit (global_mainloop); 
134     return;
135   }
136
137   /* signal status via a global variable */
138   if (global_components_failed == global_components_failed_exit) {
139     g_main_loop_quit (global_mainloop); 
140     return;
141   }
142
143   /* XXX: dear compiler, these are for you: */
144   (void)agent; (void)stream_id; (void)data; (void)component_id;
145 }
146
147 static void cb_new_selected_pair(NiceAgent *agent, guint stream_id, guint component_id, 
148                                  gchar *lfoundation, gchar* rfoundation, gpointer data)
149 {
150   g_debug ("test-restart:%s: %p", G_STRFUNC, data);
151
152   if ((intptr_t)data == 1)
153     ++global_lagent_cands;
154   else if ((intptr_t)data == 2)
155     ++global_ragent_cands;
156
157   /* XXX: dear compiler, these are for you: */
158   (void)agent; (void)stream_id; (void)component_id; (void)lfoundation; (void)rfoundation;
159 }
160
161 static void cb_new_candidate(NiceAgent *agent, guint stream_id, guint component_id, 
162                              gchar *foundation, gpointer data)
163 {
164   g_debug ("test-restart:%s: %p", G_STRFUNC, data);
165
166   /* XXX: dear compiler, these are for you: */
167   (void)agent; (void)stream_id; (void)data; (void)component_id; (void)foundation;
168 }
169
170 static void cb_initial_binding_request_received(NiceAgent *agent, guint stream_id, gpointer data)
171 {
172   g_debug ("test-restart:%s: %p", G_STRFUNC, data);
173
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;
178
179   /* XXX: dear compiler, these are for you: */
180   (void)agent; (void)stream_id; (void)data;
181 }
182
183 static void priv_get_local_addr (NiceAgent *agent, guint stream_id, guint component_id, NiceAddress *dstaddr)
184 {
185   GSList *cands, *i;
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;
189     if (cand) {
190       g_assert (dstaddr);
191       *dstaddr = cand->addr;
192     }
193   }
194   for (i = cands; i; i = i->next)
195     nice_candidate_free ((NiceCandidate *) i->data);
196   g_slist_free (cands);
197 }
198
199 static int run_restart_test (NiceAgent *lagent, NiceAgent *ragent, NiceAddress *baseaddr)
200 {
201   NiceAddress laddr, raddr, laddr_rtcp, raddr_rtcp;   
202   NiceCandidate cdes;
203   GSList *cands;
204   guint ls_id, rs_id;
205   guint64 tie_breaker;
206
207   /* XXX: dear compiler, these are for you: */
208   (void)baseaddr;
209
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;
215
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;
228
229   g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
230   g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
231
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);
237
238   nice_agent_gather_candidates (lagent, ls_id);
239   nice_agent_gather_candidates (ragent, rs_id);
240
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);
250
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);
259   }
260
261   /* step: find out the local candidates of each agent */
262
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));
266
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));
270
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));
274
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));
278
279   /* step: pass the remote candidates to agents  */
280   cands = g_slist_append (NULL, &cdes);
281   {
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);
286       g_free (ufrag);
287       g_free (password);
288       nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
289       nice_agent_set_remote_credentials (lagent,
290                                          ls_id, ufrag, password);
291       g_free (ufrag);
292       g_free (password);
293   }
294   cdes.component_id = NICE_COMPONENT_TYPE_RTP;
295   cdes.addr = raddr;
296   nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
297   cdes.addr = laddr;
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
306    * has started. */
307   g_object_set (G_OBJECT (ragent), "controlling-mode", TRUE, NULL);
308   g_assert (ragent->controlling_mode == FALSE);
309
310   g_debug ("test-restart: Set properties, next running mainloop until connectivity checks succeed...");
311
312   /* step: run the mainloop until connectivity checks succeed 
313    *       (see timer_cb() above) */
314   g_main_loop_run (global_mainloop);
315
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);
325
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 */
329
330   g_debug ("-------------------------------------------\n"
331            "test-restart: Requesting a RESTART...");
332
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);
336
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);
350   {
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);
355       g_free (ufrag);
356       g_free (password);
357       nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
358       nice_agent_set_remote_credentials (lagent,
359                                          ls_id, ufrag, password);
360       g_free (ufrag);
361       g_free (password);
362   }
363   
364   /* send another packet after restart */
365   g_assert_cmpint (nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678"), ==, 16);
366
367   /* step: reset state variables */
368   global_lagent_ibr_received = FALSE;
369   global_ragent_ibr_received = FALSE;
370   global_components_ready = 0;
371
372   /* step: exchange remote candidates */
373   cdes.component_id = NICE_COMPONENT_TYPE_RTP;
374   cdes.addr = raddr;
375   nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
376   cdes.addr = laddr;
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);
383
384   g_main_loop_run (global_mainloop);
385
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);
393
394   g_debug ("test-restart: Ran mainloop, removing streams...");
395
396   /* step: clean up resources and exit */
397
398   g_slist_free (cands);
399   nice_agent_remove_stream (lagent, ls_id);
400   nice_agent_remove_stream (ragent, rs_id);
401
402   return 0;
403 }
404
405 int main (void)
406 {
407   NiceAgent *lagent, *ragent;      /* agent's L and R */
408   NiceAddress baseaddr;
409   int result;
410   guint timer_id;
411   const char *stun_server = NULL, *stun_server_port = NULL;
412
413 #ifdef G_OS_WIN32
414   WSADATA w;
415
416   WSAStartup(0x0202, &w);
417 #endif
418
419   global_mainloop = g_main_loop_new (NULL, FALSE);
420
421   /* Note: impl limits ...
422    * - no multi-stream support
423    * - no IPv6 support
424    */
425
426
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);
432
433   g_object_set (G_OBJECT (lagent), "upnp", FALSE, NULL);
434   g_object_set (G_OBJECT (ragent), "upnp", FALSE, NULL);
435
436   /* step: add a timer to catch state changes triggered by signals */
437   timer_id = g_timeout_add (30000, timer_cb, NULL);
438
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);
444
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);
465
466   stun_server = getenv ("NICE_STUN_SERVER");
467   stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
468   if (stun_server) {
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);
473   }
474
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);
482
483   g_object_unref (lagent);
484   g_object_unref (ragent);
485
486
487   g_main_loop_unref (global_mainloop);
488   global_mainloop = NULL;
489
490   g_source_remove (timer_id);
491 #ifdef G_OS_WIN32
492   WSACleanup();
493 #endif
494   return result;
495 }