583830eda286d6826e757dc45e053e3c6ab1979c
[platform/upstream/libnice.git] / tests / test-address.c
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_cmpint (addr.s.ip4.sin_family, ==, AF_INET);
56
57   nice_address_to_string (&addr, str);
58   g_assert_cmpstr (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
88     g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1.1") != TRUE);
89
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);
92
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);
95
96     g_assert (nice_address_set_from_string(heap_addr, "192.168.2.0"));
97     g_assert (nice_address_is_private (heap_addr) == TRUE);
98
99     g_assert (nice_address_set_from_string(heap_addr, "192.168.15.69"));
100     g_assert (nice_address_is_private (heap_addr) == TRUE);
101
102     g_assert (nice_address_set_from_string(heap_addr, "192.169.0.0"));
103     g_assert (nice_address_is_private (heap_addr) == FALSE);
104
105     g_assert (nice_address_set_from_string(heap_addr, "192.167.0.0"));
106     g_assert (nice_address_is_private (heap_addr) == FALSE);
107
108     g_assert (nice_address_set_from_string(heap_addr, "10.2.1.2"));
109     g_assert (nice_address_is_private (heap_addr) == TRUE);
110
111     g_assert (nice_address_set_from_string(heap_addr, "11.0.0.0"));
112     g_assert (nice_address_is_private (heap_addr) == FALSE);
113
114     g_assert (nice_address_set_from_string(heap_addr, "9.255.255.255"));
115     g_assert (nice_address_is_private (heap_addr) == FALSE);
116
117     g_assert (nice_address_set_from_string(heap_addr, "172.15.255.255"));
118     g_assert (nice_address_is_private (heap_addr) == FALSE);
119
120     g_assert (nice_address_set_from_string(heap_addr, "172.16.0.0"));
121     g_assert (nice_address_is_private (heap_addr) == TRUE);
122
123     g_assert (nice_address_set_from_string(heap_addr, "172.31.255.255"));
124     g_assert (nice_address_is_private (heap_addr) == TRUE);
125
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);
130
131     g_assert (nice_address_set_from_string(heap_addr, "169.253.255.255"));
132     g_assert (nice_address_is_private (heap_addr) == FALSE);
133
134     g_assert (nice_address_set_from_string(heap_addr, "169.254.0.0"));
135     g_assert (nice_address_is_private (heap_addr) == TRUE);
136
137     g_assert (nice_address_set_from_string(heap_addr, "169.254.255.255"));
138     g_assert (nice_address_is_private (heap_addr) == TRUE);
139
140     g_assert (nice_address_set_from_string(heap_addr, "169.255.0.0"));
141     g_assert (nice_address_is_private (heap_addr) == FALSE);
142
143     g_assert (nice_address_set_from_string(heap_addr, "fe70::0"));
144     g_assert (nice_address_is_private (heap_addr) == FALSE);
145     
146     g_assert (nice_address_set_from_string(heap_addr, "fe80::0"));
147     g_assert (nice_address_is_private (heap_addr) == TRUE);
148
149     g_assert (nice_address_set_from_string(heap_addr, "fe81::0"));
150     g_assert (nice_address_is_private (heap_addr) == TRUE);
151
152     nice_address_free (heap_addr);
153   }
154 }
155
156 static void
157 test_ipv6 (void)
158 {
159   NiceAddress addr, other, v4addr;
160   gchar str[NICE_ADDRESS_STRING_LEN];
161   union {
162     struct sockaddr_in6 in6;
163     struct sockaddr addr;
164   } sin, sin2;
165
166   g_assert (nice_address_set_from_string (&v4addr, "172.1.0.1") == TRUE);
167
168   memset (&sin, 0, sizeof (sin));
169   memset (&sin2, 0, sizeof (sin2));
170
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 *)
176       "\x00\x11\x22\x33"
177       "\x44\x55\x66\x77"
178       "\x88\x99\xaa\xbb"
179       "\xcc\xdd\xee\xff");
180   g_assert_cmpint (addr.s.ip6.sin6_family, ==, AF_INET6);
181
182   nice_address_to_string (&addr, str);
183   g_assert_cmpstr (str, ==, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
184
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 */
188
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);
194
195   g_assert_cmpmem (&sin,  sizeof(sin), &sin2, sizeof(sin2));
196
197   /* private IPv6 address */
198   nice_address_set_ipv6 (&addr, (guchar *)
199       "\xfc\x00\x00\x00"
200       "\x00\x00\x00\x00"
201       "\x00\x00\x00\x00"
202       "\x00\x00\x00\x01");
203   g_assert (nice_address_is_private (&addr) == TRUE);
204   nice_address_set_ipv6 (&addr, (guchar *)
205       "\x00\x00\x00\x00"
206       "\x00\x00\x00\x00"
207       "\x00\x00\x00\x00"
208       "\x00\x00\x00\x01");
209   g_assert (nice_address_is_private (&addr) == TRUE);
210
211   /* mismatching address families */
212   g_assert (nice_address_equal (&addr, &v4addr) != TRUE);
213
214   /* mismatched type */
215   addr.s.addr.sa_family = AF_UNSPEC;
216   /*g_assert (nice_address_equal (&addr, &v4addr) != TRUE);*/
217 }
218
219 int
220 main (void)
221 {
222 #ifdef G_OS_WIN32
223   WSADATA w;
224 #endif
225
226 #ifdef G_OS_WIN32
227   WSAStartup(0x0202, &w);
228 #endif
229   test_ipv4 ();
230   test_ipv6 ();
231
232 #ifdef G_OS_WIN32
233   WSACleanup();
234 #endif
235   return 0;
236 }
237