2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006, 2007 Collabora Ltd.
5 * Contact: Dafydd Harries
6 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
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/
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
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
25 * Dafydd Harries, Collabora Ltd.
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.
50 gchar str[NICE_ADDRESS_STRING_LEN];
52 nice_address_init (&addr);
53 nice_address_init (&other);
54 nice_address_set_ipv4 (&addr, 0x01020304);
55 g_assert_cmpint (addr.s.ip4.sin_family, ==, AF_INET);
57 nice_address_to_string (&addr, str);
58 g_assert_cmpstr (str, ==, "1.2.3.4");
60 nice_address_to_string (&addr, str);
63 nice_address_set_ipv4 (&other, 0x01020304);
64 g_assert (TRUE == nice_address_equal (&addr, &other));
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));
76 nice_address_set_ipv4 (&other, 0x01020305);
77 g_assert (FALSE == nice_address_equal (&addr, &other));
80 nice_address_set_ipv4 (&other, 0x01020304);
81 nice_address_set_port (&addr, 1);
82 g_assert (FALSE == nice_address_equal (&addr, &other));
84 /* test private address check */
86 NiceAddress *heap_addr = nice_address_new ();
88 g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1.1") != TRUE);
90 g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1") == TRUE);
91 g_assert (nice_address_is_private (heap_addr) == TRUE);
93 g_assert (nice_address_set_from_string (heap_addr, "127.1.1.1") == TRUE);
94 g_assert (nice_address_is_private (heap_addr) == TRUE);
96 g_assert (nice_address_set_from_string(heap_addr, "192.168.2.0"));
97 g_assert (nice_address_is_private (heap_addr) == TRUE);
99 g_assert (nice_address_set_from_string(heap_addr, "192.168.15.69"));
100 g_assert (nice_address_is_private (heap_addr) == TRUE);
102 g_assert (nice_address_set_from_string(heap_addr, "192.169.0.0"));
103 g_assert (nice_address_is_private (heap_addr) == FALSE);
105 g_assert (nice_address_set_from_string(heap_addr, "192.167.0.0"));
106 g_assert (nice_address_is_private (heap_addr) == FALSE);
108 g_assert (nice_address_set_from_string(heap_addr, "10.2.1.2"));
109 g_assert (nice_address_is_private (heap_addr) == TRUE);
111 g_assert (nice_address_set_from_string(heap_addr, "11.0.0.0"));
112 g_assert (nice_address_is_private (heap_addr) == FALSE);
114 g_assert (nice_address_set_from_string(heap_addr, "9.255.255.255"));
115 g_assert (nice_address_is_private (heap_addr) == FALSE);
117 g_assert (nice_address_set_from_string(heap_addr, "172.15.255.255"));
118 g_assert (nice_address_is_private (heap_addr) == FALSE);
120 g_assert (nice_address_set_from_string(heap_addr, "172.16.0.0"));
121 g_assert (nice_address_is_private (heap_addr) == TRUE);
123 g_assert (nice_address_set_from_string(heap_addr, "172.31.255.255"));
124 g_assert (nice_address_is_private (heap_addr) == TRUE);
126 g_assert (nice_address_set_from_string(heap_addr, "172.32.0.0"));
127 g_assert (nice_address_is_private (heap_addr) == FALSE);
128 g_assert (nice_address_set_from_string(heap_addr, "172.63.0.0"));
129 g_assert (nice_address_is_private (heap_addr) == FALSE);
131 g_assert (nice_address_set_from_string(heap_addr, "169.253.255.255"));
132 g_assert (nice_address_is_private (heap_addr) == FALSE);
134 g_assert (nice_address_set_from_string(heap_addr, "169.254.0.0"));
135 g_assert (nice_address_is_private (heap_addr) == TRUE);
137 g_assert (nice_address_set_from_string(heap_addr, "169.254.255.255"));
138 g_assert (nice_address_is_private (heap_addr) == TRUE);
140 g_assert (nice_address_set_from_string(heap_addr, "169.255.0.0"));
141 g_assert (nice_address_is_private (heap_addr) == FALSE);
143 g_assert (nice_address_set_from_string(heap_addr, "fe70::0"));
144 g_assert (nice_address_is_private (heap_addr) == FALSE);
146 g_assert (nice_address_set_from_string(heap_addr, "fe80::0"));
147 g_assert (nice_address_is_private (heap_addr) == TRUE);
149 g_assert (nice_address_set_from_string(heap_addr, "fe81::0"));
150 g_assert (nice_address_is_private (heap_addr) == TRUE);
152 nice_address_free (heap_addr);
159 NiceAddress addr, other, v4addr;
160 gchar str[NICE_ADDRESS_STRING_LEN];
162 struct sockaddr_in6 in6;
163 struct sockaddr addr;
166 g_assert (nice_address_set_from_string (&v4addr, "172.1.0.1") == TRUE);
168 memset (&sin, 0, sizeof (sin));
169 memset (&sin2, 0, sizeof (sin2));
171 memset (&addr, 0, sizeof (NiceAddress));
172 memset (&other, 0, sizeof (NiceAddress));
173 nice_address_init (&addr);
174 nice_address_init (&other);
175 nice_address_set_ipv6 (&addr, (guchar *)
180 g_assert_cmpint (addr.s.ip6.sin6_family, ==, AF_INET6);
182 nice_address_to_string (&addr, str);
183 g_assert_cmpstr (str, ==, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
185 nice_address_set_port (&addr, 9876); /* in native byte order */
186 nice_address_set_from_string (&other, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
187 nice_address_set_port (&other, 9876); /* in native byte order */
189 nice_address_copy_to_sockaddr (&other, &sin2.addr);
190 nice_address_copy_to_sockaddr (&addr, &sin.addr);
191 g_assert (nice_address_equal (&addr, &other) == TRUE);
192 nice_address_to_string (&addr, str);
193 nice_address_to_string (&other, str);
195 g_assert_cmpmem (&sin, sizeof(sin), &sin2, sizeof(sin2));
197 /* private IPv6 address */
198 nice_address_set_ipv6 (&addr, (guchar *)
203 g_assert (nice_address_is_private (&addr) == TRUE);
204 nice_address_set_ipv6 (&addr, (guchar *)
209 g_assert (nice_address_is_private (&addr) == TRUE);
211 /* mismatching address families */
212 g_assert (nice_address_equal (&addr, &v4addr) != TRUE);
214 /* mismatched type */
215 addr.s.addr.sa_family = AF_UNSPEC;
216 /*g_assert (nice_address_equal (&addr, &v4addr) != TRUE);*/
227 WSAStartup(0x0202, &w);