6 #include <gio/gnetworking.h>
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;
17 static gboolean timer_cb (gpointer pointer)
19 g_debug ("test-nomination:%s: %p", G_STRFUNC, pointer);
21 /* signal status via a global variable */
23 /* note: should not be reached, abort */
24 g_error ("ERROR: test has got stuck, aborting...");
29 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
31 g_debug ("test-nomination:%s: %p", G_STRFUNC, user_data);
33 /* XXX: dear compiler, these are for you: */
34 (void)agent; (void)stream_id; (void)component_id; (void)buf;
37 * Lets ignore stun packets that got through
41 if (strncmp ("12345678", buf, 8))
44 if (component_id != 1)
48 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
50 g_debug ("test-nomination:%s: %p", G_STRFUNC, data);
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;
59 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
61 gboolean ready_to_connected = FALSE;
62 g_debug ("test-nomination:%s: %p", G_STRFUNC, data);
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;
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);
82 g_debug ("test-nomination: checks READY %u.", global_components_ready);
85 static void cb_new_selected_pair(NiceAgent *agent, guint stream_id,
86 guint component_id, gchar *lfoundation, gchar* rfoundation, gpointer data)
88 g_debug ("test-nomination:%s: %p", G_STRFUNC, data);
90 if (GPOINTER_TO_UINT (data) == 1)
91 ++global_lagent_cands;
92 else if (GPOINTER_TO_UINT (data) == 2)
93 ++global_ragent_cands;
96 static void set_candidates (NiceAgent *from, guint from_stream,
97 NiceAgent *to, guint to_stream, guint component)
99 GSList *cands = NULL, *i;
101 cands = nice_agent_get_local_candidates (from, from_stream, component);
102 nice_agent_set_remote_candidates (to, to_stream, component, cands);
104 for (i = cands; i; i = i->next)
105 nice_candidate_free ((NiceCandidate *) i->data);
106 g_slist_free (cands);
109 static void set_credentials (NiceAgent *lagent, guint lstream,
110 NiceAgent *ragent, guint rstream)
112 gchar *ufrag = NULL, *password = NULL;
114 nice_agent_get_local_credentials(lagent, lstream, &ufrag, &password);
115 nice_agent_set_remote_credentials (ragent, rstream, ufrag, password);
118 nice_agent_get_local_credentials(ragent, rstream, &ufrag, &password);
119 nice_agent_set_remote_credentials (lagent, lstream, ufrag, password);
125 run_test(NiceNominationMode l_nomination_mode,
126 NiceNominationMode r_nomination_mode)
128 NiceAgent *lagent, *ragent; /* agent's L and R */
129 const gchar *localhost;
130 NiceAddress localaddr;
134 localhost = "127.0.0.1";
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;
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);
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);
152 g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE, NULL);
153 g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE, NULL);
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");
160 timer_id = g_timeout_add (30000, timer_cb, NULL);
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);
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));
180 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
181 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
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);
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);
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));
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);
205 set_credentials (lagent, ls_id, ragent, rs_id);
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);
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);
216 nice_agent_remove_stream (lagent, ls_id);
217 nice_agent_remove_stream (ragent, rs_id);
219 g_source_remove (timer_id);
221 g_clear_object(&lagent);
222 g_clear_object(&ragent);
228 run_test(NICE_NOMINATION_MODE_REGULAR, NICE_NOMINATION_MODE_REGULAR);
234 run_test(NICE_NOMINATION_MODE_AGGRESSIVE, NICE_NOMINATION_MODE_AGGRESSIVE);
240 run_test(NICE_NOMINATION_MODE_REGULAR, NICE_NOMINATION_MODE_AGGRESSIVE);
246 run_test(NICE_NOMINATION_MODE_AGGRESSIVE, NICE_NOMINATION_MODE_REGULAR);
250 main (int argc, char **argv)
254 g_networking_init ();
256 g_test_init (&argc, &argv, NULL);
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);