Stun agent usage flags are now into an enum too
[sipe-libnice.git] / tests / test-thread.c
blob21cece2e8b916a17436f1137f92094499e1c3b46
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * Unit test for ICE full-mode related features.
6 * (C) 2007 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
24 * Contributors:
25 * Kai Vehmanen, Nokia
27 * Alternatively, the contents of this file may be used under the terms of the
28 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
29 * case the provisions of LGPL are applicable instead of those above. If you
30 * wish to allow use of your version of this file only under the terms of the
31 * LGPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replace
33 * them with the notice and other provisions required by the LGPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the LGPL.
37 #ifdef HAVE_CONFIG_H
38 # include <config.h>
39 #endif
41 #include "agent.h"
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 GMainLoop *error_loop;
49 gint global_lagent_cands = 0;
50 gint global_ragent_cands = 0;
52 gint global_lagent_buffers = 0;
53 gint global_ragent_buffers = 0;
55 static gboolean timer_cb (gpointer pointer)
57 g_debug ("test-thread:%s: %p", G_STRFUNC, pointer);
59 /* note: should not be reached, abort */
60 g_debug ("ERROR: test has got stuck, aborting...");
61 exit (-1);
65 static gpointer
66 mainloop_thread (gpointer data)
68 GMainLoop *loop = data;
70 usleep (100000);
71 g_main_loop_run (loop);
73 return NULL;
77 static void
78 cb_new_selected_pair(NiceAgent *agent,
79 guint stream_id,
80 guint component_id,
81 gchar *lfoundation,
82 gchar* rfoundation,
83 gpointer data)
85 g_debug ("test-thread:%s: %p", __func__, data);
87 if ((int)data == 1)
88 g_atomic_int_inc (&global_lagent_cands);
89 else if ((int)data == 2)
90 g_atomic_int_inc (&global_ragent_cands);
94 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
96 NiceAgent *other = g_object_get_data (G_OBJECT (agent), "other-agent");
97 gchar *ufrag = NULL, *password = NULL;
98 GSList *cands, *i;
99 guint id, other_id;
100 gpointer tmp;
102 g_debug ("test-thread:%s", G_STRFUNC);
104 tmp = g_object_get_data (G_OBJECT (agent), "id");
105 id = GPOINTER_TO_UINT (tmp);
106 tmp = g_object_get_data (G_OBJECT (other), "id");
107 other_id = GPOINTER_TO_UINT (tmp);
109 nice_agent_get_local_credentials(agent, id, &ufrag, &password);
110 nice_agent_set_remote_credentials (other,
111 other_id, ufrag, password);
112 g_free (ufrag);
113 g_free (password);
115 cands = nice_agent_get_local_candidates(agent, id, 1);
116 g_assert (cands != NULL);
118 nice_agent_set_remote_candidates (other, other_id, 1, cands);
120 for (i = cands; i; i = i->next)
121 nice_candidate_free ((NiceCandidate *) i->data);
122 g_slist_free (cands);
127 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
129 gchar data[10];
130 gint *count = NULL;
132 if (GPOINTER_TO_UINT (user_data) == 1)
133 count = &global_lagent_buffers;
134 else if (GPOINTER_TO_UINT (user_data) == 2)
135 count = &global_ragent_buffers;
136 else
137 g_error ("Invalid agent ?");
139 if (*count == -1)
140 return;
142 g_assert (len == 10);
144 memset (data, *count+'1', 10);
146 g_assert (memcmp (buf, data, 10) == 0);
148 (*count)++;
150 if (*count == 10)
151 *count = -1;
153 if (global_ragent_buffers == -1 && global_lagent_buffers == -1)
154 g_main_loop_quit (error_loop);
158 static void cb_component_state_changed (NiceAgent *agent,
159 guint stream_id,
160 guint component_id,
161 guint state,
162 gpointer user_data)
164 int i;
165 gchar data[10];
167 if (state != NICE_COMPONENT_STATE_READY)
168 return;
170 for (i=0; i<10; i++)
172 memset (data, i+'1', 10);
174 nice_agent_send (agent, stream_id, component_id, 10, data);
178 int main (void)
180 NiceAgent *lagent, *ragent; /* agent's L and R */
181 NiceAddress baseaddr;
182 guint timer_id;
183 const char *stun_server = NULL, *stun_server_port = NULL;
184 GMainContext *lmainctx, *rmainctx;
185 GMainLoop *lmainloop, *rmainloop;
186 GThread *lthread, *rthread;
187 guint ls_id, rs_id;
188 GMainContext *ldmainctx, *rdmainctx;
189 GMainLoop *ldmainloop, *rdmainloop;
190 GThread *ldthread, *rdthread;
192 g_type_init ();
193 g_thread_init (NULL);
195 lmainctx = g_main_context_new ();
196 rmainctx = g_main_context_new ();
197 lmainloop = g_main_loop_new (lmainctx, FALSE);
198 rmainloop = g_main_loop_new (rmainctx, FALSE);
200 ldmainctx = g_main_context_new ();
201 rdmainctx = g_main_context_new ();
202 ldmainloop = g_main_loop_new (ldmainctx, FALSE);
203 rdmainloop = g_main_loop_new (rdmainctx, FALSE);
205 error_loop = g_main_loop_new (NULL, FALSE);
208 /* step: create the agents L and R */
209 lagent = nice_agent_new (lmainctx, NICE_COMPATIBILITY_MSN);
210 ragent = nice_agent_new (rmainctx, NICE_COMPATIBILITY_MSN);
212 g_object_set_data (G_OBJECT (lagent), "other-agent", ragent);
213 g_object_set_data (G_OBJECT (ragent), "other-agent", lagent);
215 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
216 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
218 /* step: add a timer to catch state changes triggered by signals */
219 timer_id = g_timeout_add (30000, timer_cb, NULL);
221 /* step: specify which local interface to use */
222 if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
223 g_assert_not_reached ();
224 nice_agent_add_local_address (lagent, &baseaddr);
225 nice_agent_add_local_address (ragent, &baseaddr);
227 g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
228 G_CALLBACK (cb_candidate_gathering_done), (gpointer)1);
229 g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
230 G_CALLBACK (cb_candidate_gathering_done), (gpointer)2);
231 g_signal_connect (G_OBJECT (lagent), "component-state-changed",
232 G_CALLBACK (cb_component_state_changed), (gpointer)1);
233 g_signal_connect (G_OBJECT (ragent), "component-state-changed",
234 G_CALLBACK (cb_component_state_changed), (gpointer)2);
235 g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
236 G_CALLBACK (cb_new_selected_pair), (gpointer)1);
237 g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
238 G_CALLBACK (cb_new_selected_pair), (gpointer)2);
240 stun_server = getenv ("NICE_STUN_SERVER");
241 stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
242 if (stun_server) {
243 g_object_set (G_OBJECT (lagent), "stun-server", stun_server, NULL);
244 g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port), NULL);
245 g_object_set (G_OBJECT (ragent), "stun-server", stun_server, NULL);
246 g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port), NULL);
249 /* step: test setter/getter functions for properties */
251 guint max_checks = 0;
252 gchar *string = NULL;
253 guint port = 0;
254 gboolean mode = FALSE;
255 g_object_get (G_OBJECT (lagent), "stun-server", &string, NULL);
256 g_assert (stun_server == NULL || strcmp (string, stun_server) == 0);
257 g_free (string);
258 g_object_get (G_OBJECT (lagent), "stun-server-port", &port, NULL);
259 g_assert (stun_server_port == NULL || port == (guint)atoi (stun_server_port));
260 g_object_get (G_OBJECT (lagent), "turn-server", &string, NULL);
261 g_free (string);
262 g_object_get (G_OBJECT (lagent), "turn-server-port", &port, NULL);
263 g_object_get (G_OBJECT (lagent), "controlling-mode", &mode, NULL);
264 g_assert (mode == TRUE);
265 g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 300, NULL);
266 g_object_get (G_OBJECT (lagent), "max-connectivity-checks", &max_checks, NULL);
267 g_assert (max_checks == 300);
270 /* step: run test the first time */
271 g_debug ("test-thread: TEST STARTS / running test for the 1st time");
273 lthread = g_thread_create (mainloop_thread, lmainloop, TRUE, NULL);
274 g_assert (lthread);
275 rthread = g_thread_create (mainloop_thread, rmainloop, TRUE, NULL);
276 g_assert (rthread);
278 ls_id = nice_agent_add_stream (lagent, 2);
279 rs_id = nice_agent_add_stream (ragent, 2);
280 g_assert (ls_id > 0);
281 g_assert (rs_id > 0);
283 g_object_set_data (G_OBJECT (lagent), "id", GUINT_TO_POINTER (ls_id));
284 g_object_set_data (G_OBJECT (ragent), "id", GUINT_TO_POINTER (rs_id));
286 nice_agent_gather_candidates (lagent, ls_id);
287 nice_agent_gather_candidates (ragent, rs_id);
289 nice_agent_attach_recv (lagent, ls_id, 1, ldmainctx, cb_nice_recv,
290 GUINT_TO_POINTER (1));
291 nice_agent_attach_recv (ragent, rs_id, 1, rdmainctx, cb_nice_recv,
292 GUINT_TO_POINTER (2));
294 ldthread = g_thread_create (mainloop_thread, ldmainloop, TRUE, NULL);
295 g_assert (ldthread);
296 rdthread = g_thread_create (mainloop_thread, rdmainloop, TRUE, NULL);
297 g_assert (rdthread);
299 /* Run loop for error timer */
300 g_main_loop_run (error_loop);
302 while (!g_main_loop_is_running (ldmainloop));
303 while (g_main_loop_is_running (ldmainloop))
304 g_main_loop_quit (ldmainloop);
305 while (!g_main_loop_is_running (rdmainloop));
306 while (g_main_loop_is_running (rdmainloop))
307 g_main_loop_quit (rdmainloop);
308 while (!g_main_loop_is_running (lmainloop));
309 while (g_main_loop_is_running (lmainloop))
310 g_main_loop_quit (lmainloop);
311 while (!g_main_loop_is_running (rmainloop));
312 while (g_main_loop_is_running (rmainloop))
313 g_main_loop_quit (rmainloop);
315 g_thread_join (ldthread);
316 g_thread_join (rdthread);
317 g_thread_join (lthread);
318 g_thread_join (rthread);
320 /* note: verify that correct number of local candidates were reported */
321 g_assert (global_lagent_cands == 1);
322 g_assert (global_ragent_cands == 1);
324 g_object_unref (lagent);
325 g_object_unref (ragent);
327 g_main_loop_unref (lmainloop);
328 g_main_loop_unref (rmainloop);
329 g_main_loop_unref (ldmainloop);
330 g_main_loop_unref (rdmainloop);
332 g_main_loop_unref (error_loop);
334 return 0;