test-address.c 5.26 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
Dafydd Harries's avatar
Dafydd Harries committed
41
42
43
44

#include <string.h>
#include "address.h"

45
46
static void
test_ipv4 (void)
Dafydd Harries's avatar
Dafydd Harries committed
47
{
48
49
  NiceAddress addr;
  NiceAddress other;
50
  gchar str[NICE_ADDRESS_STRING_LEN];
Dafydd Harries's avatar
Dafydd Harries committed
51

52
53
  nice_address_init (&addr);
  nice_address_init (&other);
Dafydd Harries's avatar
Dafydd Harries committed
54
  nice_address_set_ipv4 (&addr, 0x01020304);
55
  g_assert_cmpint (addr.s.ip4.sin_family, ==, AF_INET);
Dafydd Harries's avatar
Dafydd Harries committed
56

57
  nice_address_to_string (&addr, str);
58
  g_assert_cmpstr (str, ==, "1.2.3.4");
Dafydd Harries's avatar
Dafydd Harries committed
59

60
61
  nice_address_to_string (&addr, str);

Dafydd Harries's avatar
Dafydd Harries committed
62
63
64
65
  /* same address */
  nice_address_set_ipv4 (&other, 0x01020304);
  g_assert (TRUE == nice_address_equal (&addr, &other));

66
  /* from sockaddr_in */
67
68
  nice_address_set_port (&other, 9876); /* in native byte order */
  other.s.ip4.sin_family = AF_INET;
69
70
  nice_address_set_from_string (&addr, "1.2.3.4");
  nice_address_set_port (&addr, 9876); /* in native byte order */
71
72
73
74
  nice_address_to_string (&addr, str);
  nice_address_to_string (&other, str);
  g_assert (TRUE == nice_address_equal (&addr, &other));

Dafydd Harries's avatar
Dafydd Harries committed
75
76
77
78
79
80
  /* different IP */
  nice_address_set_ipv4 (&other, 0x01020305);
  g_assert (FALSE == nice_address_equal (&addr, &other));

  /* different port */
  nice_address_set_ipv4 (&other, 0x01020304);
81
  nice_address_set_port (&addr, 1);
Dafydd Harries's avatar
Dafydd Harries committed
82
  g_assert (FALSE == nice_address_equal (&addr, &other));
83
84
85
86

  /* test private address check */
  {
    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
91
    nice_address_free (heap_addr);
  }
92
93
94
95
96
}

static void
test_ipv6 (void)
{
97
  NiceAddress addr, other, v4addr;
98
  gchar str[NICE_ADDRESS_STRING_LEN];
99
100
101
102
  union {
    struct sockaddr_in6 in6;
    struct sockaddr addr;
  } sin, sin2;
103

104
  g_assert (nice_address_set_from_string (&v4addr, "172.1.0.1") == TRUE);
105
106
107

  memset (&sin, 0, sizeof (sin));
  memset (&sin2, 0, sizeof (sin2));
108

109
110
  memset (&addr, 0, sizeof (NiceAddress));
  memset (&other, 0, sizeof (NiceAddress));
111
  nice_address_init (&addr);
112
  nice_address_init (&other);
113
  nice_address_set_ipv6 (&addr, (guchar *)
114
115
116
117
      "\x00\x11\x22\x33"
      "\x44\x55\x66\x77"
      "\x88\x99\xaa\xbb"
      "\xcc\xdd\xee\xff");
118
  g_assert_cmpint (addr.s.ip6.sin6_family, ==, AF_INET6);
Dafydd Harries's avatar
Dafydd Harries committed
119

120
  nice_address_to_string (&addr, str);
121
  g_assert_cmpstr (str, ==, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
122

123
  nice_address_set_port (&addr, 9876); /* in native byte order */
124
125
126
  nice_address_set_from_string (&other, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
  nice_address_set_port (&other, 9876); /* in native byte order */

127
128
  nice_address_copy_to_sockaddr (&other, &sin2.addr);
  nice_address_copy_to_sockaddr (&addr, &sin.addr);
129
  g_assert (nice_address_equal (&addr, &other) == TRUE);
130
131
  nice_address_to_string (&addr, str);
  nice_address_to_string (&other, str);
132

133
  g_assert_cmpmem (&sin,  sizeof(sin), &sin2, sizeof(sin2));
134
135

  /* private IPv6 address */
136
  nice_address_set_ipv6 (&addr, (guchar *)
137
138
139
140
141
      "\xfc\x00\x00\x00"
      "\x00\x00\x00\x00"
      "\x00\x00\x00\x00"
      "\x00\x00\x00\x01");
  g_assert (nice_address_is_private (&addr) == TRUE);
142
  nice_address_set_ipv6 (&addr, (guchar *)
143
144
145
146
147
148
149
150
151
152
      "\x00\x00\x00\x00"
      "\x00\x00\x00\x00"
      "\x00\x00\x00\x00"
      "\x00\x00\x00\x01");
  g_assert (nice_address_is_private (&addr) == TRUE);

  /* mismatching address families */
  g_assert (nice_address_equal (&addr, &v4addr) != TRUE);

  /* mismatched type */
153
  addr.s.addr.sa_family = AF_UNSPEC;
154
  /*g_assert (nice_address_equal (&addr, &v4addr) != TRUE);*/
155
156
157
158
159
}

int
main (void)
{
160
161
162
163
164
165
166
#ifdef G_OS_WIN32
  WSADATA w;
#endif

#ifdef G_OS_WIN32
  WSAStartup(0x0202, &w);
#endif
167
168
  test_ipv4 ();
  test_ipv6 ();
169
170
171
172

#ifdef G_OS_WIN32
  WSACleanup();
#endif
Dafydd Harries's avatar
Dafydd Harries committed
173
174
175
  return 0;
}