test-stun.c 7.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2006, 2007 Collabora Ltd.
 *  Contact: Dafydd Harries
 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
 *  Contact: Kai Vehmanen
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Dafydd Harries, Collabora Ltd.
26
 *   Kai Vehmanen, Nokia
27 28 29 30 31 32 33 34 35 36 37
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */
38 39 40
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
41 42 43

#include <string.h>

Dafydd Harries's avatar
Dafydd Harries committed
44 45 46
#include "stun.h"
#include "udp-fake.h"
#include "agent.h"
47

48 49 50
static void
test_stun_no_password (
  NiceAgent *agent,
51 52
  NiceAddress from,
  NiceUDPSocket *sock)
53
{
54
  NiceAddress to;
55 56
  guint len;
  gchar buf[1024];
57 58
  guint packed_len;
  gchar *packed;
59

60
  memset (&to, 0, sizeof (to));
61 62
  memset (buf, '\0', 1024);

63 64 65 66
    {
      StunMessage *breq;

      /* send binding request without username */
67
      breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
68
          "0123456789abcdef", 0);
69
      packed_len = stun_message_pack (breq, &packed);
Dafydd Harries's avatar
Dafydd Harries committed
70
      nice_udp_fake_socket_push_recv (sock, &from, packed_len, packed);
71 72 73
      g_free (packed);
      stun_message_free (breq);
    }
74 75

  /* tell the agent there's a packet waiting */
76
  nice_agent_poll_read (agent, NULL, NULL, NULL);
77

78
  /* error response should have been sent */
Dafydd Harries's avatar
Dafydd Harries committed
79 80
  len = nice_udp_fake_socket_pop_send (sock, &to,
      sizeof (buf) / sizeof (gchar), buf);
81 82 83 84 85 86 87 88 89
  g_assert (len != 0);

    {
      StunMessage *bres;

      /* construct expected response */
      bres = stun_message_new (STUN_MESSAGE_BINDING_ERROR_RESPONSE,
          "0123456789abcdef", 0);
      packed_len = stun_message_pack (bres, &packed);
90 91

      stun_message_free (bres);
92 93 94 95
    }

  g_assert (len == packed_len);
  g_assert (0 == memcmp (buf, packed, len));
96
  g_free (packed);
97
}
98

99 100 101
static void
test_stun_invalid_password (
  NiceAgent *agent,
102 103
  NiceAddress from,
  NiceUDPSocket *sock)
104
{
105
  NiceAddress to;
106 107
  guint len;
  gchar buf[1024];
108 109
  guint packed_len;
  gchar *packed;
110

111
  memset (&to, 0, sizeof (to));
112 113 114 115 116 117
  memset (buf, '\0', 1024);

    {
      StunMessage *breq;

      /* send binding request with incorrect username */
118
      breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
119
          "0123456789abcdef", 1);
120 121 122
      breq->attributes[0] = stun_attribute_username_new ("lala");
      packed_len = stun_message_pack (breq, &packed);
      g_assert (packed_len != 0);
Dafydd Harries's avatar
Dafydd Harries committed
123
      nice_udp_fake_socket_push_recv (sock, &from, packed_len, packed);
124 125 126
      g_free (packed);
      stun_message_free (breq);
    }
127 128

  /* tell the agent there's a packet waiting */
129
  nice_agent_poll_read (agent, NULL, NULL, NULL);
130

131
  /* error should have been sent */
Dafydd Harries's avatar
Dafydd Harries committed
132 133
  len = nice_udp_fake_socket_pop_send (sock, &to,
      sizeof (buf) / sizeof (gchar), buf);
134 135 136 137 138 139 140 141 142
  g_assert (len != 0);

    {
      StunMessage *bres;

      /* construct expected response */
      bres = stun_message_new (STUN_MESSAGE_BINDING_ERROR_RESPONSE,
          "0123456789abcdef", 0);
      packed_len = stun_message_pack (bres, &packed);
143
      stun_message_free (bres);
144 145 146 147
    }

  g_assert (len == packed_len);
  g_assert (0 == memcmp (buf, packed, len));
148
  g_free (packed);
149
}
150

151 152 153
static void
test_stun_valid_password (
  NiceAgent *agent,
154 155 156
  NiceAddress from,
  NiceCandidate *candidate,
  NiceUDPSocket *sock)
157
{
158
  NiceAddress to;
159 160 161 162
  guint len;
  guint packed_len;
  gchar buf[1024];
  gchar *packed;
163
  gchar *username;
164

165
  memset (&to, 0, sizeof (to));
166 167
  memset (buf, '\0', 1024);

168
  username = g_strconcat (candidate->username, "username", NULL);
169

170 171 172 173 174 175
    {
      StunMessage *breq;
      guint packed_len;
      gchar *packed;

      /* send binding request with correct username */
176
      breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
177
          "0123456789abcdef", 1);
178 179 180
      breq->attributes[0] = stun_attribute_username_new (username);
      packed_len = stun_message_pack (breq, &packed);
      g_assert (packed_len != 0);
Dafydd Harries's avatar
Dafydd Harries committed
181
      nice_udp_fake_socket_push_recv (sock, &from, packed_len, packed);
182 183 184 185 186 187 188 189
      g_free (packed);
      stun_message_free (breq);
    }

    {
      StunMessage *bres;

      /* construct expected response packet */
190
      bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
191
          "0123456789abcdef", 2);
192
      bres->attributes[0] = stun_attribute_mapped_address_new (
193
          from.addr.addr_ipv4, 5678);
194
      bres->attributes[1] = stun_attribute_username_new (username);
195 196 197
      packed_len = stun_message_pack (bres, &packed);
      stun_message_free (bres);
    }
198

199 200
  g_free (username);

201
  /* tell the agent there's a packet waiting */
202
  nice_agent_poll_read (agent, NULL, NULL, NULL);
203

204
  /* compare sent packet to expected */
205 206
  len = nice_udp_fake_socket_pop_send (sock, &to,
      sizeof (buf) / sizeof (gchar), buf);
207 208
  g_assert (len == packed_len);
  g_assert (0 == memcmp (buf, packed, len));
209
  g_assert (nice_address_equal (&to, &from));
210
  g_free (packed);
211 212 213 214 215 216
}

int
main (void)
{
  NiceAgent *agent;
217 218
  NiceAddress local_addr;
  NiceAddress remote_addr;
219
  NiceCandidate *candidate;
Dafydd Harries's avatar
Dafydd Harries committed
220 221
  NiceUDPSocketFactory factory;
  NiceUDPSocket *sock;
222
  GSList *candidates;
223

224 225
  memset (&local_addr, 0, sizeof (local_addr));
  memset (&remote_addr, 0, sizeof (remote_addr));
Dafydd Harries's avatar
Dafydd Harries committed
226 227
  g_type_init ();

Dafydd Harries's avatar
Dafydd Harries committed
228
  nice_udp_fake_socket_factory_init (&factory);
229

230 231
  g_assert (nice_address_set_ipv4_from_string (&local_addr, "192.168.0.1"));
  g_assert (nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5"));
232
  remote_addr.port = 5678;
233 234

  /* set up agent */
Dafydd Harries's avatar
Dafydd Harries committed
235
  agent = nice_agent_new (&factory);
236
  nice_agent_add_local_address (agent, &local_addr);
237
  nice_agent_add_stream (agent, 1);
238
  nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
239
      &remote_addr, "username", "password");
240 241 242

  candidates = nice_agent_get_local_candidates (agent, 1, 1);
  candidate = candidates->data;
243
  sock = candidate->sockptr;
244
  g_slist_free (candidates);
245 246

  /* run tests */
247 248 249
  test_stun_no_password (agent, remote_addr, sock);
  test_stun_invalid_password (agent, remote_addr, sock);
  test_stun_valid_password (agent, remote_addr, candidate, sock);
250 251

  /* clean up */
252
  g_object_unref (agent);
Dafydd Harries's avatar
Dafydd Harries committed
253
  nice_udp_socket_factory_close (&factory);
254 255 256 257

  return 0;
}