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

« back to all changes in this revision

Viewing changes to agent/test-address.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) 2006, 2007 Collabora Ltd.
 
5
 *  Contact: Dafydd Harries
 
6
 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
 
7
 *  Contact: Kai Vehmanen
 
8
 *
 
9
 * The contents of this file are subject to the Mozilla Public License Version
 
10
 * 1.1 (the "License"); you may not use this file except in compliance with
 
11
 * the License. You may obtain a copy of the License at
 
12
 * http://www.mozilla.org/MPL/
 
13
 *
 
14
 * Software distributed under the License is distributed on an "AS IS" basis,
 
15
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
16
 * for the specific language governing rights and limitations under the
 
17
 * License.
 
18
 *
 
19
 * The Original Code is the Nice GLib ICE library.
 
20
 *
 
21
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 
22
 * Corporation. All Rights Reserved.
 
23
 *
 
24
 * Contributors:
 
25
 *   Dafydd Harries, Collabora Ltd.
 
26
 *   Kai Vehmanen, Nokia
 
27
 *
 
28
 * Alternatively, the contents of this file may be used under the terms of the
 
29
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 
30
 * case the provisions of LGPL are applicable instead of those above. If you
 
31
 * wish to allow use of your version of this file only under the terms of the
 
32
 * LGPL and not to allow others to use your version of this file under the
 
33
 * MPL, indicate your decision by deleting the provisions above and replace
 
34
 * them with the notice and other provisions required by the LGPL. If you do
 
35
 * not delete the provisions above, a recipient may use your version of this
 
36
 * file under either the MPL or the LGPL.
 
37
 */
 
38
#ifdef HAVE_CONFIG_H
 
39
# include "config.h"
 
40
#endif
 
41
 
 
42
#include <string.h>
 
43
#include "address.h"
 
44
 
 
45
static void
 
46
test_ipv4 (void)
 
47
{
 
48
  NiceAddress addr;
 
49
  NiceAddress other;
 
50
  gchar str[NICE_ADDRESS_STRING_LEN];
 
51
 
 
52
  nice_address_init (&addr);
 
53
  nice_address_init (&other);
 
54
  nice_address_set_ipv4 (&addr, 0x01020304);
 
55
  g_assert (addr.s.ip4.sin_family == AF_INET);
 
56
 
 
57
  nice_address_to_string (&addr, str);
 
58
  g_assert (0 == strcmp (str, "1.2.3.4"));
 
59
 
 
60
  nice_address_to_string (&addr, str);
 
61
 
 
62
  /* same address */
 
63
  nice_address_set_ipv4 (&other, 0x01020304);
 
64
  g_assert (TRUE == nice_address_equal (&addr, &other));
 
65
 
 
66
  /* from sockaddr_in */
 
67
  nice_address_set_port (&other, 9876); /* in native byte order */
 
68
  other.s.ip4.sin_family = AF_INET;
 
69
  nice_address_set_from_string (&addr, "1.2.3.4");
 
70
  nice_address_set_port (&addr, 9876); /* in native byte order */
 
71
  nice_address_to_string (&addr, str);
 
72
  nice_address_to_string (&other, str);
 
73
  g_assert (TRUE == nice_address_equal (&addr, &other));
 
74
 
 
75
  /* different IP */
 
76
  nice_address_set_ipv4 (&other, 0x01020305);
 
77
  g_assert (FALSE == nice_address_equal (&addr, &other));
 
78
 
 
79
  /* different port */
 
80
  nice_address_set_ipv4 (&other, 0x01020304);
 
81
  nice_address_set_port (&addr, 1);
 
82
  g_assert (FALSE == nice_address_equal (&addr, &other));
 
83
 
 
84
  /* test private address check */
 
85
  {
 
86
    NiceAddress *heap_addr = nice_address_new ();
 
87
    g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1") == TRUE);
 
88
    g_assert (nice_address_is_private (heap_addr) == TRUE);
 
89
    g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1.1") != TRUE);
 
90
    nice_address_free (heap_addr);
 
91
  }
 
92
}
 
93
 
 
94
static void
 
95
test_ipv6 (void)
 
96
{
 
97
  NiceAddress addr, other, v4addr;
 
98
  gchar str[NICE_ADDRESS_STRING_LEN];
 
99
  struct sockaddr_in6 sin, sin2;
 
100
 
 
101
  g_assert (nice_address_set_from_string (&v4addr, "172.1.0.1") == TRUE);
 
102
 
 
103
  memset (&sin, 0, sizeof (sin));
 
104
  memset (&sin2, 0, sizeof (sin2));
 
105
 
 
106
  memset (&addr, 0, sizeof (NiceAddress));
 
107
  memset (&other, 0, sizeof (NiceAddress));
 
108
  nice_address_init (&addr);
 
109
  nice_address_init (&other);
 
110
  nice_address_set_ipv6 (&addr, (guchar *)
 
111
      "\x00\x11\x22\x33"
 
112
      "\x44\x55\x66\x77"
 
113
      "\x88\x99\xaa\xbb"
 
114
      "\xcc\xdd\xee\xff");
 
115
  g_assert (addr.s.ip6.sin6_family == AF_INET6);
 
116
 
 
117
  nice_address_to_string (&addr, str);
 
118
  g_assert (0 == strcmp (str, "11:2233:4455:6677:8899:aabb:ccdd:eeff"));
 
119
 
 
120
  nice_address_set_port (&addr, 9876); /* in native byte order */
 
121
  nice_address_set_from_string (&other, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
 
122
  nice_address_set_port (&other, 9876); /* in native byte order */
 
123
 
 
124
  nice_address_copy_to_sockaddr (&other, (struct sockaddr*)&sin2);
 
125
  nice_address_copy_to_sockaddr (&addr, (struct sockaddr*)&sin);
 
126
  g_assert (nice_address_equal (&addr, &other) == TRUE);
 
127
  nice_address_to_string (&addr, str);
 
128
  nice_address_to_string (&other, str);
 
129
 
 
130
  g_assert (memcmp (&sin, &sin2, sizeof(sin)) == 0);
 
131
 
 
132
  /* private IPv6 address */
 
133
  nice_address_set_ipv6 (&addr, (guchar *)
 
134
      "\xfc\x00\x00\x00"
 
135
      "\x00\x00\x00\x00"
 
136
      "\x00\x00\x00\x00"
 
137
      "\x00\x00\x00\x01");
 
138
  g_assert (nice_address_is_private (&addr) == TRUE);
 
139
  nice_address_set_ipv6 (&addr, (guchar *)
 
140
      "\x00\x00\x00\x00"
 
141
      "\x00\x00\x00\x00"
 
142
      "\x00\x00\x00\x00"
 
143
      "\x00\x00\x00\x01");
 
144
  g_assert (nice_address_is_private (&addr) == TRUE);
 
145
 
 
146
  /* mismatching address families */
 
147
  g_assert (nice_address_equal (&addr, &v4addr) != TRUE);
 
148
 
 
149
  /* mismatched type */
 
150
  addr.s.addr.sa_family = AF_UNSPEC;
 
151
  /*g_assert (nice_address_equal (&addr, &v4addr) != TRUE);*/
 
152
}
 
153
 
 
154
int
 
155
main (void)
 
156
{
 
157
#ifdef G_OS_WIN32
 
158
  WSADATA w;
 
159
#endif
 
160
 
 
161
#ifdef G_OS_WIN32
 
162
  WSAStartup(0x0202, &w);
 
163
#endif
 
164
  test_ipv4 ();
 
165
  test_ipv6 ();
 
166
 
 
167
#ifdef G_OS_WIN32
 
168
  WSACleanup();
 
169
#endif
 
170
  return 0;
 
171
}
 
172