Imported Upstream version 0.1.17
[platform/upstream/libnice.git] / tests / test-nomination.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <gio/gio.h>
6 #include <gio/gnetworking.h>
7 #include <agent.h>
8
9 static NiceComponentState global_lagent_state[2] = { NICE_COMPONENT_STATE_LAST, NICE_COMPONENT_STATE_LAST };
10 static NiceComponentState global_ragent_state[2] = { NICE_COMPONENT_STATE_LAST, NICE_COMPONENT_STATE_LAST };
11 static guint global_components_ready = 0;
12 static gboolean global_lagent_gathering_done = FALSE;
13 static gboolean global_ragent_gathering_done = FALSE;
14 static int global_lagent_cands = 0;
15 static int global_ragent_cands = 0;
16
17 static gboolean timer_cb (gpointer pointer)
18 {
19   g_debug ("test-nomination:%s: %p", G_STRFUNC, pointer);
20
21   /* signal status via a global variable */
22
23   /* note: should not be reached, abort */
24   g_error ("ERROR: test has got stuck, aborting...");
25
26   return FALSE;
27 }
28
29 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
30 {
31   g_debug ("test-nomination:%s: %p", G_STRFUNC, user_data);
32
33   /* XXX: dear compiler, these are for you: */
34   (void)agent; (void)stream_id; (void)component_id; (void)buf;
35
36   /*
37    * Lets ignore stun packets that got through
38    */
39   if (len < 8)
40     return;
41   if (strncmp ("12345678", buf, 8))
42     return;
43
44   if (component_id != 1)
45     return;
46 }
47
48 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
49 {
50   g_debug ("test-nomination:%s: %p", G_STRFUNC, data);
51
52   if (GPOINTER_TO_UINT (data) == 1)
53     global_lagent_gathering_done = TRUE;
54   else if (GPOINTER_TO_UINT (data) == 2)
55     global_ragent_gathering_done = TRUE;
56 }
57
58
59 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
60 {
61   gboolean ready_to_connected = FALSE;
62   g_debug ("test-nomination:%s: %p", G_STRFUNC, data);
63
64   if (GPOINTER_TO_UINT (data) == 1) {
65     if (global_lagent_state[component_id - 1] == NICE_COMPONENT_STATE_READY &&
66         state == NICE_COMPONENT_STATE_CONNECTED)
67       ready_to_connected = TRUE;
68     global_lagent_state[component_id - 1] = state;
69   } else if (GPOINTER_TO_UINT (data) == 2) {
70     if (global_ragent_state[component_id - 1] == NICE_COMPONENT_STATE_READY &&
71         state == NICE_COMPONENT_STATE_CONNECTED)
72       ready_to_connected = TRUE;
73     global_ragent_state[component_id - 1] = state;
74   }
75
76   if (state == NICE_COMPONENT_STATE_READY)
77     global_components_ready++;
78   else if (state == NICE_COMPONENT_STATE_CONNECTED && ready_to_connected)
79     global_components_ready--;
80   g_assert (state != NICE_COMPONENT_STATE_FAILED);
81
82   g_debug ("test-nomination: checks READY %u.", global_components_ready);
83 }
84
85 static void cb_new_selected_pair(NiceAgent *agent, guint stream_id,
86     guint component_id, gchar *lfoundation, gchar* rfoundation, gpointer data)
87 {
88   g_debug ("test-nomination:%s: %p", G_STRFUNC, data);
89
90   if (GPOINTER_TO_UINT (data) == 1)
91     ++global_lagent_cands;
92   else if (GPOINTER_TO_UINT (data) == 2)
93     ++global_ragent_cands;
94 }
95
96 static void set_candidates (NiceAgent *from, guint from_stream,
97     NiceAgent *to, guint to_stream, guint component)
98 {
99   GSList *cands = NULL, *i;
100
101   cands = nice_agent_get_local_candidates (from, from_stream, component);
102   nice_agent_set_remote_candidates (to, to_stream, component, cands);
103
104   for (i = cands; i; i = i->next)
105     nice_candidate_free ((NiceCandidate *) i->data);
106   g_slist_free (cands);
107 }
108
109 static void set_credentials (NiceAgent *lagent, guint lstream,
110     NiceAgent *ragent, guint rstream)
111 {
112   gchar *ufrag = NULL, *password = NULL;
113
114   nice_agent_get_local_credentials(lagent, lstream, &ufrag, &password);
115   nice_agent_set_remote_credentials (ragent, rstream, ufrag, password);
116   g_free (ufrag);
117   g_free (password);
118   nice_agent_get_local_credentials(ragent, rstream, &ufrag, &password);
119   nice_agent_set_remote_credentials (lagent, lstream, ufrag, password);
120   g_free (ufrag);
121   g_free (password);
122 }
123
124 static void
125 run_test(NiceNominationMode l_nomination_mode,
126   NiceNominationMode r_nomination_mode)
127 {
128   NiceAgent *lagent, *ragent;      /* agent's L and R */
129   const gchar *localhost;
130   NiceAddress localaddr;
131   guint ls_id, rs_id;
132   gulong timer_id;
133
134   localhost = "127.0.0.1";
135
136   /* step: initialize variables modified by the callbacks */
137   global_components_ready = 0;
138   global_lagent_gathering_done = FALSE;
139   global_ragent_gathering_done = FALSE;
140   global_lagent_cands = global_ragent_cands = 0;
141
142   lagent = nice_agent_new_full (NULL,
143     NICE_COMPATIBILITY_RFC5245,
144     l_nomination_mode == NICE_NOMINATION_MODE_REGULAR ?
145     NICE_AGENT_OPTION_REGULAR_NOMINATION : 0);
146
147   ragent = nice_agent_new_full (NULL,
148     NICE_COMPATIBILITY_RFC5245,
149     r_nomination_mode == NICE_NOMINATION_MODE_REGULAR ?
150     NICE_AGENT_OPTION_REGULAR_NOMINATION : 0);
151
152   g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE, NULL);
153   g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE, NULL);
154
155   g_object_set (G_OBJECT (lagent), "upnp", FALSE,  NULL);
156   g_object_set (G_OBJECT (ragent), "upnp", FALSE,  NULL);
157   nice_agent_set_software (lagent, "Test-nomination, Left Agent");
158   nice_agent_set_software (ragent, "Test-nomination, Right Agent");
159
160   timer_id = g_timeout_add (30000, timer_cb, NULL);
161
162   if (!nice_address_set_from_string (&localaddr, localhost))
163     g_assert_not_reached ();
164   nice_agent_add_local_address (lagent, &localaddr);
165   nice_agent_add_local_address (ragent, &localaddr);
166
167   g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
168       G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
169   g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
170       G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER (2));
171   g_signal_connect (G_OBJECT (lagent), "component-state-changed",
172       G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (1));
173   g_signal_connect (G_OBJECT (ragent), "component-state-changed",
174       G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (2));
175   g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
176       G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
177   g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
178       G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
179
180   g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
181   g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
182
183   ls_id = nice_agent_add_stream (lagent, 1);
184   rs_id = nice_agent_add_stream (ragent, 1);
185   g_assert_cmpuint (ls_id, >, 0);
186   g_assert_cmpuint (rs_id, >, 0);
187
188   /* Gather candidates and test nice_agent_set_port_range */
189   g_assert (nice_agent_gather_candidates (lagent, ls_id) == TRUE);
190   g_assert (nice_agent_gather_candidates (ragent, rs_id) == TRUE);
191
192   nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTP,
193       g_main_context_default (), cb_nice_recv, GUINT_TO_POINTER (1));
194   nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
195       g_main_context_default (), cb_nice_recv, GUINT_TO_POINTER (2));
196
197   g_debug ("test-nomination: Added streams, running context until 'candidate-gathering-done'...");
198   while (!global_lagent_gathering_done)
199     g_main_context_iteration (NULL, TRUE);
200   g_assert (global_lagent_gathering_done == TRUE);
201   while (!global_ragent_gathering_done)
202     g_main_context_iteration (NULL, TRUE);
203   g_assert (global_ragent_gathering_done == TRUE);
204
205   set_credentials (lagent, ls_id, ragent, rs_id);
206
207   set_candidates (ragent, rs_id, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
208   set_candidates (lagent, ls_id, ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
209
210   while (global_lagent_state[0] != NICE_COMPONENT_STATE_READY ||
211       global_ragent_state[0] != NICE_COMPONENT_STATE_READY)
212     g_main_context_iteration (NULL, TRUE);
213   g_assert_cmpint (global_lagent_state[0], ==, NICE_COMPONENT_STATE_READY);
214   g_assert_cmpint (global_ragent_state[0], ==, NICE_COMPONENT_STATE_READY);
215
216   nice_agent_remove_stream (lagent, ls_id);
217   nice_agent_remove_stream (ragent, rs_id);
218
219   g_source_remove (timer_id);
220
221   g_clear_object(&lagent);
222   g_clear_object(&ragent);
223 }
224
225 static void
226 regular (void)
227 {
228   run_test(NICE_NOMINATION_MODE_REGULAR, NICE_NOMINATION_MODE_REGULAR);
229 }
230
231 static void
232 aggressive (void)
233 {
234   run_test(NICE_NOMINATION_MODE_AGGRESSIVE, NICE_NOMINATION_MODE_AGGRESSIVE);
235 }
236
237 static void
238 mixed_ra (void)
239 {
240   run_test(NICE_NOMINATION_MODE_REGULAR, NICE_NOMINATION_MODE_AGGRESSIVE);
241 }
242
243 static void
244 mixed_ar (void)
245 {
246   run_test(NICE_NOMINATION_MODE_AGGRESSIVE, NICE_NOMINATION_MODE_REGULAR);
247 }
248
249 int
250 main (int argc, char **argv)
251 {
252   int ret;
253
254   g_networking_init ();
255
256   g_test_init (&argc, &argv, NULL);
257
258   g_test_add_func ("/nice/nomination/regular", regular);
259   g_test_add_func ("/nice/nomination/aggressive", aggressive);
260   g_test_add_func ("/nice/nomination/mixed_ra", mixed_ra);
261   g_test_add_func ("/nice/nomination/mixed_ar", mixed_ar);
262
263   ret = g_test_run ();
264
265   return ret;
266 }