~ubuntu-branches/debian/squeeze/libnice/squeeze

« back to all changes in this revision

Viewing changes to stun/tests/test-format.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-01-04 17:45:34 UTC
  • Revision ID: james.westby@ubuntu.com-20090104174534-dh5u1pfonumqa99c
Tags: upstream-0.0.4
Import upstream version 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Nice GLib ICE library.
 
3
 *
 
4
 * (C) 2007 Nokia Corporation. All rights reserved.
 
5
 *  Contact: Rémi Denis-Courmont
 
6
 *
 
7
 * The contents of this file are subject to the Mozilla Public License Version
 
8
 * 1.1 (the "License"); you may not use this file except in compliance with
 
9
 * the License. You may obtain a copy of the License at
 
10
 * http://www.mozilla.org/MPL/
 
11
 *
 
12
 * Software distributed under the License is distributed on an "AS IS" basis,
 
13
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
14
 * for the specific language governing rights and limitations under the
 
15
 * License.
 
16
 *
 
17
 * The Original Code is the Nice GLib ICE library.
 
18
 *
 
19
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 
20
 * Corporation. All Rights Reserved.
 
21
 *
 
22
 * Contributors:
 
23
 *   Rémi Denis-Courmont, 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
 
 
36
#ifdef HAVE_CONFIG_H
 
37
# include <config.h>
 
38
#endif
 
39
 
 
40
#include <sys/types.h>
 
41
 
 
42
#include "stun/stunagent.h"
 
43
#include <stdio.h>
 
44
#include <string.h>
 
45
#include <stdlib.h>
 
46
#include <stdarg.h>
 
47
#include <assert.h>
 
48
 
 
49
#ifdef _WIN32
 
50
#include <winsock2.h>
 
51
#define ENOENT -1
 
52
#define EINVAL -2
 
53
#define ENOBUFS -3
 
54
#define EAFNOSUPPORT -4
 
55
#define EPROTO -5
 
56
#define EACCES -6
 
57
#define EINPROGRESS -7
 
58
#define EAGAIN -8
 
59
#define ENOSYS -9
 
60
#else
 
61
#include <sys/types.h>
 
62
#include <sys/socket.h>
 
63
#include <arpa/inet.h>
 
64
#include <errno.h>
 
65
#endif
 
66
 
 
67
 
 
68
static void fatal (const char *msg, ...)
 
69
{
 
70
  va_list ap;
 
71
  va_start (ap, msg);
 
72
  vfprintf (stderr, msg, ap);
 
73
  va_end (ap);
 
74
  fputc ('\n', stderr);
 
75
  exit (1);
 
76
}
 
77
 
 
78
static const char usr[] = "admin";
 
79
static const char pwd[] = "s3kr3t";
 
80
 
 
81
bool dynamic_check_validater (StunAgent *agent,
 
82
    StunMessage *message, uint8_t *username, uint16_t username_len,
 
83
    uint8_t **password, size_t *password_len, void *user_data)
 
84
{
 
85
 
 
86
  if (username_len != strlen (usr) ||
 
87
      memcmp (username, usr, strlen (usr)) != 0)
 
88
    fatal ("vector test : Validater received wrong username!");
 
89
 
 
90
  *password = (uint8_t *) pwd;
 
91
  *password_len = strlen (pwd);
 
92
 
 
93
 
 
94
  return true;
 
95
}
 
96
static void
 
97
dynamic_check (StunAgent *agent, StunMessage *msg, size_t len)
 
98
{
 
99
  StunMessage msg2;
 
100
 
 
101
  if (stun_agent_validate (agent, &msg2, msg->buffer, len, dynamic_check_validater, NULL) != STUN_VALIDATION_SUCCESS)
 
102
    fatal ("Could not validate message");
 
103
 
 
104
  printf ("Built message of %u bytes\n", (unsigned)len);
 
105
}
 
106
 
 
107
 
 
108
static size_t
 
109
finish_check (StunAgent *agent, StunMessage *msg)
 
110
{
 
111
  uint8_t buf[STUN_MAX_MESSAGE_SIZE + 8];
 
112
  size_t len;
 
113
  uint16_t plen;
 
114
  StunMessage msg2 = {0};
 
115
  msg2.agent = msg->agent;
 
116
  msg2.buffer = buf;
 
117
  msg2.buffer_len = sizeof(buf);
 
118
  memcpy (msg2.buffer, msg->buffer, sizeof(buf) > msg->buffer_len ? msg->buffer_len : sizeof(buf));
 
119
 
 
120
  len = stun_agent_finish_message (agent, msg, NULL, 0);
 
121
 
 
122
  if (len <= 0)
 
123
    fatal ("Cannot finish message");
 
124
  dynamic_check (agent, msg, len);
 
125
 
 
126
  if (stun_message_find (&msg2, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, &plen) != NULL)
 
127
    fatal ("Missing HMAC test failed");
 
128
 
 
129
  stun_message_append_string (&msg2, STUN_ATTRIBUTE_USERNAME, usr);
 
130
 
 
131
  len = stun_agent_finish_message (agent, &msg2, pwd, strlen (pwd));
 
132
 
 
133
  if (len <= 0)
 
134
    fatal ("Cannot finish message with short-term creds");
 
135
  dynamic_check (agent, &msg2, len);
 
136
 
 
137
  return len;
 
138
}
 
139
 
 
140
static void
 
141
check_af (const char *name, int family, socklen_t addrlen)
 
142
{
 
143
  struct sockaddr_storage addr;
 
144
  uint8_t buf[100];
 
145
  StunAgent agent;
 
146
  StunMessage msg;
 
147
  uint16_t known_attributes[] = {STUN_ATTRIBUTE_USERNAME, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, STUN_ATTRIBUTE_ERROR_CODE, 0};
 
148
 
 
149
  stun_agent_init (&agent, known_attributes,
 
150
      STUN_COMPATIBILITY_RFC5389, STUN_AGENT_USAGE_USE_FINGERPRINT);
 
151
 
 
152
  assert (addrlen <= sizeof (addr));
 
153
 
 
154
  memset (&addr, 0, sizeof (addr));
 
155
  stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
 
156
 
 
157
  if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
 
158
                        (struct sockaddr *)&addr, addrlen) != EAFNOSUPPORT)
 
159
    fatal ("Unknown address family test failed");
 
160
  if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
 
161
                        (struct sockaddr *)&addr, addrlen) != EAFNOSUPPORT)
 
162
    fatal ("Unknown address family xor test failed");
 
163
 
 
164
  addr.ss_family = family;
 
165
  if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
 
166
                        (struct sockaddr *)&addr, addrlen - 1) != EINVAL)
 
167
    fatal ("Too small %s sockaddr test failed", name);
 
168
 
 
169
  if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
 
170
                        (struct sockaddr *)&addr, addrlen - 1) != EINVAL)
 
171
    fatal ("Too small %s sockaddr xor test failed", name);
 
172
 
 
173
  if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_MAPPED_ADDRESS,
 
174
                        (struct sockaddr *)&addr, addrlen))
 
175
    fatal ("%s sockaddr test failed", name);
 
176
 
 
177
  if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
 
178
                            (struct sockaddr *)&addr, addrlen))
 
179
    fatal ("%s sockaddr xor test failed", name);
 
180
}
 
181
 
 
182
int main (void)
 
183
{
 
184
  uint8_t buf[100];
 
185
  size_t len;
 
186
  struct sockaddr addr;
 
187
 
 
188
  StunAgent agent;
 
189
  StunMessage msg;
 
190
  uint16_t known_attributes[] = {STUN_ATTRIBUTE_USERNAME, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, STUN_ATTRIBUTE_ERROR_CODE, 0};
 
191
 
 
192
  stun_agent_init (&agent, known_attributes,
 
193
      STUN_COMPATIBILITY_RFC5389, STUN_AGENT_USAGE_USE_FINGERPRINT);
 
194
 
 
195
  /* Request formatting test */
 
196
  stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
 
197
  finish_check (&agent, &msg);
 
198
  if (memcmp (buf, "\x00\x01", 2))
 
199
    fatal ("Request formatting test failed");
 
200
 
 
201
  /* Response formatting test */
 
202
  stun_agent_init_response (&agent, &msg, buf, sizeof (buf), &msg);
 
203
  finish_check (&agent, &msg);
 
204
  if (memcmp (buf, "\x01\x01", 2))
 
205
    fatal ("Response formatting test failed");
 
206
 
 
207
  /* Error formatting test */
 
208
  stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
 
209
  finish_check (&agent, &msg);
 
210
  if (!stun_agent_init_error (&agent, &msg, buf, sizeof (buf), &msg, 400))
 
211
    fatal ("Error initialization test failed");
 
212
  finish_check (&agent, &msg);
 
213
  if (memcmp (buf, "\x01\x11", 2))
 
214
    fatal ("Error formatting test failed");
 
215
  /* Unknown error formatting test */
 
216
  stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
 
217
  finish_check (&agent, &msg);
 
218
  if (!stun_agent_init_error (&agent, &msg, buf, sizeof (buf), &msg, 666))
 
219
    fatal ("Unknown error initialization test failed");
 
220
  finish_check (&agent, &msg);
 
221
  if (memcmp (buf, "\x01\x11", 2))
 
222
    fatal ("Unknown error formatting test failed");
 
223
 
 
224
  /* Overflow tests */
 
225
  stun_agent_init_request (&agent, &msg, buf, sizeof(buf), STUN_BINDING);
 
226
 
 
227
  for (len = 0;
 
228
       stun_message_append_flag (&msg, 0xffff) != ENOBUFS;
 
229
       len += 4)
 
230
  {
 
231
    if (len > 0xffff)
 
232
      fatal ("Overflow protection test failed");
 
233
  }
 
234
 
 
235
  if (stun_message_append32 (&msg, 0xffff, 0x12345678) != ENOBUFS)
 
236
    fatal ("Double-word overflow test failed");
 
237
  if (stun_message_append64 (&msg, 0xffff,
 
238
                     0x123456789abcdef0) != ENOBUFS)
 
239
    fatal ("Quad-word overflow test failed");
 
240
  if (stun_message_append_string (&msg, 0xffff, "foobar") != ENOBUFS)
 
241
    fatal ("String overflow test failed");
 
242
 
 
243
  memset (&addr, 0, sizeof (addr));
 
244
  addr.sa_family = AF_INET;
 
245
#ifdef HAVE_SA_LEN
 
246
  addr.sa_len = sizeof (addr);
 
247
#endif
 
248
  if (stun_message_append_xor_addr (&msg, 0xffff, &addr,
 
249
                            sizeof (addr)) != ENOBUFS)
 
250
    fatal ("Address overflow test failed");
 
251
  len = sizeof (msg);
 
252
  if (stun_agent_finish_message (&agent, &msg, NULL, 0) != 0)
 
253
    fatal ("Fingerprint overflow test failed");
 
254
  if (stun_agent_finish_message (&agent, &msg, pwd, strlen (pwd)) != 0)
 
255
    fatal ("Message integrity overflow test failed");
 
256
 
 
257
  /* Address attributes tests */
 
258
  check_af ("IPv4", AF_INET, sizeof (struct sockaddr_in));
 
259
#ifdef AF_INET6
 
260
  check_af ("IPv6", AF_INET6, sizeof (struct sockaddr_in6));
 
261
#endif
 
262
 
 
263
  return 0;
 
264
}