d11da6d021972c4f85f78ebb68f45c9b435c4645
[platform/upstream/libnice.git] / tests / test-credentials.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2015 Rohan Garg <rohan@garg.io>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is the Nice GLib ICE library.
17  *
18  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
19  * Corporation. All Rights Reserved.
20  *
21  * Contributors:
22  *   Dafydd Harries, Collabora Ltd.
23  *   Kai Vehmanen, Nokia
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27  * case the provisions of LGPL are applicable instead of those above. If you
28  * wish to allow use of your version of this file only under the terms of the
29  * LGPL and not to allow others to use your version of this file under the
30  * MPL, indicate your decision by deleting the provisions above and replace
31  * them with the notice and other provisions required by the LGPL. If you do
32  * not delete the provisions above, a recipient may use your version of this
33  * file under either the MPL or the LGPL.
34  */
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include "agent.h"
40 #include "agent-priv.h"
41 #include <string.h>
42 #include <stdio.h>
43
44 #define LEFT_AGENT GINT_TO_POINTER(1)
45 #define RIGHT_AGENT GINT_TO_POINTER(2)
46 #define USE_UPNP 0
47
48 static GMainLoop *loop = NULL;
49
50 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
51 {
52   g_debug ("test-credentials:%s: %p", G_STRFUNC, user_data);
53 }
54
55 static void set_credentials(NiceAgent *lagent, NiceAgent *ragent)
56 {
57   gchar *ufrag = NULL, *password = NULL;
58
59   g_debug ("test-credentials:%s", G_STRFUNC);
60
61   nice_agent_get_local_credentials (lagent, 1, &ufrag, &password);
62   nice_agent_set_remote_credentials (ragent, 1, ufrag, password);
63
64   g_free (ufrag);
65   g_free (password);
66
67   nice_agent_get_local_credentials (ragent, 1, &ufrag, &password);
68   nice_agent_set_remote_credentials (lagent, 1, ufrag, password);
69
70   g_free (ufrag);
71   g_free (password);
72 }
73
74 static void swap_candidates(NiceAgent *local, guint local_id, NiceAgent *remote, guint remote_id)
75 {
76   GSList *cands = NULL;
77
78   g_debug ("test-credentials:%s", G_STRFUNC);
79   cands = nice_agent_get_local_candidates(local, local_id,
80                                           NICE_COMPONENT_TYPE_RTP);
81   g_assert (nice_agent_set_remote_candidates(remote, remote_id,
82                                             NICE_COMPONENT_TYPE_RTP, cands));
83
84   g_slist_free_full (cands, (GDestroyNotify) nice_candidate_free);
85 }
86
87
88 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
89 {
90   static gboolean L_CAND_DONE = false, R_CAND_DONE = false;
91   static NiceAgent *lagent = NULL, *ragent = NULL;
92
93   g_debug ("test-credentials:%s: %p", G_STRFUNC, data);
94   if (GPOINTER_TO_UINT(data) == 1) {
95     g_debug ("lagent finished gathering candidates");
96     L_CAND_DONE = true;
97     lagent = agent;
98   } else if (GPOINTER_TO_UINT(data) == 2) {
99     g_debug ("ragent finished gathering candidates");
100     R_CAND_DONE = true;
101     ragent = agent;
102   }
103
104   if (L_CAND_DONE && R_CAND_DONE) {
105     set_credentials (lagent, ragent);
106     swap_candidates (lagent, 1, ragent, 1);
107     swap_candidates (ragent, 1, lagent, 1);
108   }
109 }
110
111 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
112 {
113   if (state == NICE_COMPONENT_STATE_READY) {
114     g_main_loop_quit(loop);
115   }
116 }
117
118 static void setup(NiceAgent *lagent, NiceAgent *ragent)
119 {
120   NiceAddress addr;
121
122   g_assert_cmpuint (nice_agent_add_stream (lagent, 1), ==, 1);
123   g_assert_cmpuint (nice_agent_add_stream (ragent, 1), ==, 1);
124   g_assert (NULL != lagent->streams);
125   g_assert (NULL != ragent->streams);
126
127   nice_address_init (&addr);
128   g_assert (nice_address_set_from_string (&addr, "127.0.0.1"));
129   nice_agent_add_local_address (lagent, &addr);
130   nice_agent_add_local_address (ragent, &addr);
131
132   nice_agent_attach_recv (lagent, 1, NICE_COMPONENT_TYPE_RTP,
133                    g_main_context_default (),
134                    cb_nice_recv, LEFT_AGENT);
135   nice_agent_attach_recv (ragent, 1, NICE_COMPONENT_TYPE_RTP,
136                    g_main_context_default (),
137                    cb_nice_recv, RIGHT_AGENT);
138
139   g_signal_connect(G_OBJECT(lagent), "candidate-gathering-done",
140                    G_CALLBACK(cb_candidate_gathering_done), LEFT_AGENT);
141   g_signal_connect(G_OBJECT(ragent), "candidate-gathering-done",
142                    G_CALLBACK(cb_candidate_gathering_done), RIGHT_AGENT);
143
144   g_signal_connect(G_OBJECT(lagent), "component-state-changed",
145                   G_CALLBACK(cb_component_state_changed), LEFT_AGENT);
146
147   g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE,  NULL);
148   g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE,  NULL);
149
150   g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
151   g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
152
153   g_object_set (G_OBJECT (lagent), "upnp", USE_UPNP, NULL);
154   g_object_set (G_OBJECT (ragent), "upnp", USE_UPNP, NULL);
155
156   g_object_set_data (G_OBJECT (lagent), "other-agent", ragent);
157   g_object_set_data (G_OBJECT (ragent), "other-agent", lagent);
158 }
159
160 static void teardown(NiceAgent *lagent, NiceAgent *ragent)
161 {
162   nice_agent_remove_stream (lagent, 1);
163   nice_agent_remove_stream (ragent, 1);
164 }
165
166 int main (void)
167 {
168   NiceAgent *lagent = NULL, *ragent = NULL;
169   gchar *ufrag = NULL, *password = NULL;
170
171 #ifdef G_OS_WIN32
172   WSADATA w;
173   WSAStartup(0x0202, &w);
174 #endif
175
176   loop = g_main_loop_new (NULL, FALSE);
177
178   lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
179   ragent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
180
181   setup (lagent, ragent);
182
183   nice_agent_set_local_credentials (lagent, 1, "unicorns", "awesome");
184   nice_agent_get_local_credentials (lagent, 1, &ufrag, &password);
185   g_assert_cmpstr ("unicorns", ==, ufrag);
186   g_assert_cmpstr ("awesome", ==, password);
187   g_free (ufrag);
188   g_free (password);
189
190   nice_agent_gather_candidates (lagent, 1);
191   nice_agent_gather_candidates (ragent, 1);
192
193   g_main_loop_run (loop);
194
195   teardown (lagent, ragent);
196
197   g_object_unref (lagent);
198   g_object_unref (ragent);
199
200 #ifdef G_OS_WIN32
201   WSACleanup();
202 #endif
203   return 0;
204 }