Replaced __func__ macro with G_STRFUNC in the unit tests
[sipe-libnice.git] / tests / test-thread.c
blob35614818eef905fa6e5030983d801a3692461888
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", G_STRFUNC, data);
87 if (GPOINTER_TO_UINT (data) == 1)
88 g_atomic_int_inc (&global_lagent_cands);
89 else if (GPOINTER_TO_UINT (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 const char *stun_server = NULL, *stun_server_port = NULL;
183 GMainContext *lmainctx, *rmainctx;
184 GMainLoop *lmainloop, *rmainloop;
185 GThread *lthread, *rthread;
186 guint ls_id, rs_id;
187 GMainContext *ldmainctx, *rdmainctx;
188 GMainLoop *ldmainloop, *rdmainloop;
189 GThread *ldthread, *rdthread;
191 g_type_init ();
192 #if !GLIB_CHECK_VERSION(2,31,8)
193 g_thread_init(NULL);
194 #endif
196 lmainctx = g_main_context_new ();
197 rmainctx = g_main_context_new ();
198 lmainloop = g_main_loop_new (lmainctx, FALSE);
199 rmainloop = g_main_loop_new (rmainctx, FALSE);
201 ldmainctx = g_main_context_new ();
202 rdmainctx = g_main_context_new ();
203 ldmainloop = g_main_loop_new (ldmainctx, FALSE);
204 rdmainloop = g_main_loop_new (rdmainctx, FALSE);
206 error_loop = g_main_loop_new (NULL, FALSE);
209 /* step: create the agents L and R */
210 lagent = nice_agent_new (lmainctx, NICE_COMPATIBILITY_MSN);
211 ragent = nice_agent_new (rmainctx, NICE_COMPATIBILITY_MSN);
213 g_object_set_data (G_OBJECT (lagent), "other-agent", ragent);
214 g_object_set_data (G_OBJECT (ragent), "other-agent", lagent);
216 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
217 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
218 g_object_set (G_OBJECT (lagent), "upnp", FALSE, NULL);
219 g_object_set (G_OBJECT (ragent), "upnp", FALSE, NULL);
221 /* step: add a timer to catch state changes triggered by signals */
222 g_timeout_add (30000, timer_cb, NULL);
224 /* step: specify which local interface to use */
225 if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
226 g_assert_not_reached ();
227 nice_agent_add_local_address (lagent, &baseaddr);
228 nice_agent_add_local_address (ragent, &baseaddr);
230 g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
231 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
232 g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
233 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(2));
234 g_signal_connect (G_OBJECT (lagent), "component-state-changed",
235 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER(1));
236 g_signal_connect (G_OBJECT (ragent), "component-state-changed",
237 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER(2));
238 g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
239 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
240 g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
241 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(2));
243 stun_server = getenv ("NICE_STUN_SERVER");
244 stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
245 if (stun_server) {
246 g_object_set (G_OBJECT (lagent), "stun-server", stun_server, NULL);
247 g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port), NULL);
248 g_object_set (G_OBJECT (ragent), "stun-server", stun_server, NULL);
249 g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port), NULL);
252 /* step: test setter/getter functions for properties */
254 guint max_checks = 0;
255 gchar *string = NULL;
256 guint port = 0;
257 gboolean mode = FALSE;
258 g_object_get (G_OBJECT (lagent), "stun-server", &string, NULL);
259 g_assert (stun_server == NULL || strcmp (string, stun_server) == 0);
260 g_free (string);
261 g_object_get (G_OBJECT (lagent), "stun-server-port", &port, NULL);
262 g_assert (stun_server_port == NULL || port == (guint)atoi (stun_server_port));
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 #if !GLIB_CHECK_VERSION(2,31,8)
274 lthread = g_thread_create (mainloop_thread, lmainloop, TRUE, NULL);
275 rthread = g_thread_create (mainloop_thread, rmainloop, TRUE, NULL);
276 #else
277 lthread = g_thread_new ("lthread libnice", mainloop_thread, lmainloop);
278 rthread = g_thread_new ("rthread libnice", mainloop_thread, rmainloop);
279 #endif
281 g_assert (lthread);
282 g_assert (rthread);
284 ls_id = nice_agent_add_stream (lagent, 2);
285 rs_id = nice_agent_add_stream (ragent, 2);
286 g_assert (ls_id > 0);
287 g_assert (rs_id > 0);
289 g_object_set_data (G_OBJECT (lagent), "id", GUINT_TO_POINTER (ls_id));
290 g_object_set_data (G_OBJECT (ragent), "id", GUINT_TO_POINTER (rs_id));
292 nice_agent_gather_candidates (lagent, ls_id);
293 nice_agent_gather_candidates (ragent, rs_id);
295 nice_agent_attach_recv (lagent, ls_id, 1, ldmainctx, cb_nice_recv,
296 GUINT_TO_POINTER (1));
297 nice_agent_attach_recv (ragent, rs_id, 1, rdmainctx, cb_nice_recv,
298 GUINT_TO_POINTER (2));
300 #if !GLIB_CHECK_VERSION(2,31,8)
301 ldthread = g_thread_create (mainloop_thread, ldmainloop, TRUE, NULL);
302 rdthread = g_thread_create (mainloop_thread, rdmainloop, TRUE, NULL);
303 #else
304 ldthread = g_thread_new ("ldthread libnice", mainloop_thread, ldmainloop);
305 rdthread = g_thread_new ("rdthread libnice", mainloop_thread, rdmainloop);
306 #endif
307 g_assert (ldthread);
308 g_assert (rdthread);
310 /* Run loop for error timer */
311 g_main_loop_run (error_loop);
313 while (!g_main_loop_is_running (ldmainloop));
314 while (g_main_loop_is_running (ldmainloop))
315 g_main_loop_quit (ldmainloop);
316 while (!g_main_loop_is_running (rdmainloop));
317 while (g_main_loop_is_running (rdmainloop))
318 g_main_loop_quit (rdmainloop);
319 while (!g_main_loop_is_running (lmainloop));
320 while (g_main_loop_is_running (lmainloop))
321 g_main_loop_quit (lmainloop);
322 while (!g_main_loop_is_running (rmainloop));
323 while (g_main_loop_is_running (rmainloop))
324 g_main_loop_quit (rmainloop);
326 g_thread_join (ldthread);
327 g_thread_join (rdthread);
328 g_thread_join (lthread);
329 g_thread_join (rthread);
331 /* note: verify that correct number of local candidates were reported */
332 g_assert (global_lagent_cands == 1);
333 g_assert (global_ragent_cands == 1);
335 g_object_unref (lagent);
336 g_object_unref (ragent);
338 g_main_loop_unref (lmainloop);
339 g_main_loop_unref (rmainloop);
340 g_main_loop_unref (ldmainloop);
341 g_main_loop_unref (rdmainloop);
343 g_main_loop_unref (error_loop);
345 return 0;