Add win32 directory to EXTRA_DIST
[sipe-libnice.git] / tests / test-dribble.c
blobf38531559ddc0908acce67cdab48361cfe3d31a7
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", G_STRFUNC, 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 /* XXX: dear compiler, these are for you: */
154 (void)agent; (void)stream_id; (void)data; (void)component_id;
157 static void cb_new_selected_pair(NiceAgent *agent, guint stream_id, guint component_id,
158 gchar *lfoundation, gchar* rfoundation, gpointer data)
160 g_debug ("test-dribble:%s: %p", G_STRFUNC, data);
162 if (GPOINTER_TO_UINT (data) == 1)
163 ++global_lagent_cands;
164 else if (GPOINTER_TO_UINT (data) == 2)
165 ++global_ragent_cands;
167 /* XXX: dear compiler, these are for you: */
168 (void)agent; (void)stream_id; (void)component_id; (void)lfoundation; (void)rfoundation;
171 static void cb_new_candidate(NiceAgent *agent, guint stream_id, guint component_id,
172 gchar *foundation, gpointer data)
174 g_debug ("test-dribble:%s: %p", G_STRFUNC, data);
176 /* XXX: dear compiler, these are for you: */
177 (void)agent; (void)stream_id; (void)data; (void)component_id; (void)foundation;
180 static void cb_initial_binding_request_received(NiceAgent *agent, guint stream_id, gpointer data)
182 g_debug ("test-dribble:%s: %p", G_STRFUNC, data);
184 if (GPOINTER_TO_UINT (data) == 1)
185 global_lagent_ibr_received = TRUE;
186 else if (GPOINTER_TO_UINT (data) == 2)
187 global_ragent_ibr_received = TRUE;
189 /* XXX: dear compiler, these are for you: */
190 (void)agent; (void)stream_id; (void)data;
194 int main (void)
196 NiceAgent *lagent, *ragent; /* agent's L and R */
197 NiceAddress baseaddr;
198 guint timer_id;
199 GSList *cands, *i;
200 guint ls_id, rs_id;
202 #ifdef G_OS_WIN32
203 WSADATA w;
205 WSAStartup(0x0202, &w);
206 #endif
208 g_type_init ();
209 #if !GLIB_CHECK_VERSION(2,31,8)
210 g_thread_init (NULL);
211 #endif
213 global_mainloop = g_main_loop_new (NULL, FALSE);
215 /* step: create the agents L and R */
216 lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
217 NICE_COMPATIBILITY_GOOGLE);
218 ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
219 NICE_COMPATIBILITY_GOOGLE);
221 if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
222 g_assert_not_reached ();
223 nice_agent_add_local_address (lagent, &baseaddr);
224 nice_agent_add_local_address (ragent, &baseaddr);
226 /* step: add a timer to catch state changes triggered by signals */
227 timer_id = g_timeout_add (30000, timer_cb, NULL);
229 g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
230 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
231 g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
232 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER (2));
233 g_signal_connect (G_OBJECT (lagent), "component-state-changed",
234 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (1));
235 g_signal_connect (G_OBJECT (ragent), "component-state-changed",
236 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (2));
237 g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
238 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
239 g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
240 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
241 g_signal_connect (G_OBJECT (lagent), "new-candidate",
242 G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (1));
243 g_signal_connect (G_OBJECT (ragent), "new-candidate",
244 G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (2));
245 g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received",
246 G_CALLBACK (cb_initial_binding_request_received),
247 GUINT_TO_POINTER (1));
248 g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
249 G_CALLBACK (cb_initial_binding_request_received),
250 GUINT_TO_POINTER (2));
252 /* step: run test */
253 g_debug ("test-dribble: running test");
255 /* step: initialize variables modified by the callbacks */
256 global_components_ready = 0;
257 global_components_ready_exit = 2;
258 global_components_failed = 0;
259 global_components_failed_exit = 0;
260 global_lagent_gathering_done = FALSE;
261 global_ragent_gathering_done = FALSE;
262 global_lagent_ibr_received =
263 global_ragent_ibr_received = FALSE;
264 global_lagent_cands =
265 global_ragent_cands = 0;
267 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
268 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
270 /* step: add one stream, with RTP+RTCP components, to each agent */
271 ls_id = nice_agent_add_stream (lagent, 1);
273 rs_id = nice_agent_add_stream (ragent, 1);
274 g_assert (ls_id > 0);
275 g_assert (rs_id > 0);
278 nice_agent_gather_candidates (lagent, ls_id);
279 nice_agent_gather_candidates (ragent, rs_id);
281 /* step: attach to mainloop (needed to register the fds) */
282 nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTP,
283 g_main_loop_get_context (global_mainloop), cb_nice_recv,
284 GUINT_TO_POINTER (1));
285 nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
286 g_main_loop_get_context (global_mainloop), cb_nice_recv,
287 GUINT_TO_POINTER (2));
289 /* step: run mainloop until local candidates are ready
290 * (see timer_cb() above) */
291 if (global_lagent_gathering_done != TRUE ||
292 global_ragent_gathering_done != TRUE) {
293 g_debug ("test-dribble: Added streams, running mainloop until 'candidate-gathering-done'...");
294 g_main_loop_run (global_mainloop);
295 g_assert (global_lagent_gathering_done == TRUE);
296 g_assert (global_ragent_gathering_done == TRUE);
300 gchar *ufrag = NULL, *password = NULL;
301 nice_agent_get_local_credentials(lagent, ls_id, &ufrag, &password);
302 nice_agent_set_remote_credentials (ragent,
303 rs_id, ufrag, password);
304 g_free (ufrag);
305 g_free (password);
306 nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
307 nice_agent_set_remote_credentials (lagent,
308 ls_id, ufrag, password);
309 g_free (ufrag);
310 g_free (password);
312 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
313 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
314 for (i = cands; i; i = i->next)
315 nice_candidate_free ((NiceCandidate *) i->data);
316 g_slist_free (cands);
317 cands = nice_agent_get_local_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
318 nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, cands);
319 for (i = cands; i; i = i->next)
320 nice_candidate_free ((NiceCandidate *) i->data);
321 g_slist_free (cands);
323 g_debug ("test-dribble: Set properties, next running mainloop until connectivity checks succeed...");
325 /* step: run the mainloop until connectivity checks succeed
326 * (see timer_cb() above) */
327 g_main_loop_run (global_mainloop);
329 /* note: verify that STUN binding requests were sent */
330 g_assert (global_lagent_ibr_received == TRUE);
331 g_assert (global_ragent_ibr_received == TRUE);
333 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
334 g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY);
335 /* note: verify that correct number of local candidates were reported */
336 g_assert (global_lagent_cands == 1);
337 g_assert (global_ragent_cands == 1);
339 g_debug ("test-dribble: agents are ready.. now adding new buggy candidate");
341 g_timeout_add (500, quit_loop_cb, NULL);
342 g_main_loop_run (global_mainloop);
344 global_components_ready--;
346 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
347 nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
348 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
349 for (i = cands; i; i = i->next)
350 nice_candidate_free ((NiceCandidate *) i->data);
351 g_slist_free (cands);
353 g_assert (global_lagent_state == NICE_COMPONENT_STATE_CONNECTED);
354 g_main_loop_run (global_mainloop);
355 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
358 g_debug ("test-dribble: buggy candidate worked, testing lower priority cand");
360 cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
361 nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
362 ((NiceCandidate *) cands->data)->priority -= 100;
363 nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
364 for (i = cands; i; i = i->next)
365 nice_candidate_free ((NiceCandidate *) i->data);
366 g_slist_free (cands);
368 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);*/
370 /* note: test payload send and receive */
371 global_ragent_read = 0;
372 g_assert (nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678") == 16);
373 g_main_loop_run (global_mainloop);
374 g_assert (global_ragent_read == 16);
376 g_debug ("test-dribble: Ran mainloop, removing streams...");
378 /* step: clean up resources and exit */
380 nice_agent_remove_stream (lagent, ls_id);
381 nice_agent_remove_stream (ragent, rs_id);
382 priv_print_global_status ();
383 g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
384 g_assert (global_ragent_state >= NICE_COMPONENT_STATE_CONNECTED);
385 /* note: verify that correct number of local candidates were reported */
386 g_assert (global_lagent_cands == 1);
387 g_assert (global_ragent_cands == 1);
390 g_object_unref (lagent);
391 g_object_unref (ragent);
393 g_main_loop_unref (global_mainloop);
394 global_mainloop = NULL;
396 g_source_remove (timer_id);
398 #ifdef G_OS_WIN32
399 WSACleanup();
400 #endif
401 return 0;