Imported Upstream version 0.1.17
[platform/upstream/libnice.git] / tests / test-interfaces.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
4  * (C) 2020 Fabrice Bellet <fabrice@bellet.info>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is the Nice GLib ICE library.
17  *
18  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
19  * Corporation. All Rights Reserved.
20  *
21  * Alternatively, the contents of this file may be used under the terms of the
22  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
23  * case the provisions of LGPL are applicable instead of those above. If you
24  * wish to allow use of your version of this file only under the terms of the
25  * LGPL and not to allow others to use your version of this file under the
26  * MPL, indicate your decision by deleting the provisions above and replace
27  * them with the notice and other provisions required by the LGPL. If you do
28  * not delete the provisions above, a recipient may use your version of this
29  * file under either the MPL or the LGPL.
30  */
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <string.h>
36 #include <address.h>
37 #include "../agent/interfaces.c"
38
39 #ifdef G_OS_UNIX
40 static void
41 test_ipv4 (void)
42 {
43   NiceAddress addr;
44   union {
45     struct sockaddr addr;
46     struct sockaddr_in in;
47     struct sockaddr_in6 in6;
48   } sin;
49
50   /* test private addresses */
51   nice_address_set_from_string (&addr, "10.1.2.3");
52   nice_address_copy_to_sockaddr (&addr, &sin.addr);
53   g_assert (nice_interfaces_is_private_ip (&sin.addr));
54
55   nice_address_set_from_string (&addr, "172.22.22.22");
56   nice_address_copy_to_sockaddr (&addr, &sin.addr);
57   g_assert (nice_interfaces_is_private_ip (&sin.addr));
58
59   nice_address_set_from_string (&addr, "192.168.122.1");
60   nice_address_copy_to_sockaddr (&addr, &sin.addr);
61   g_assert (nice_interfaces_is_private_ip (&sin.addr));
62
63   nice_address_set_from_string (&addr, "169.254.1.2");
64   nice_address_copy_to_sockaddr (&addr, &sin.addr);
65   g_assert (nice_interfaces_is_private_ip (&sin.addr));
66
67   /* test public addresses */
68   nice_address_set_from_string (&addr, "1.2.3.4");
69   nice_address_copy_to_sockaddr (&addr, &sin.addr);
70   g_assert (nice_interfaces_is_private_ip (&sin.addr) == FALSE);
71
72 }
73
74 static void
75 test_ipv6 (void)
76 {
77   NiceAddress addr;
78   union {
79     struct sockaddr addr;
80     struct sockaddr_in in;
81     struct sockaddr_in6 in6;
82   } sin;
83
84   /* test private addresses */
85   nice_address_set_from_string (&addr,
86       "fe8f:2233:4455:6677:8899:aabb:ccdd:eeff");
87   nice_address_copy_to_sockaddr (&addr, &sin.addr);
88   g_assert (nice_interfaces_is_private_ip (&sin.addr));
89
90   /* test public addresses */
91   nice_address_set_from_string (&addr,
92       "11:2233:4455:6677:8899:aabb:ccdd:eeff");
93   nice_address_copy_to_sockaddr (&addr, &sin.addr);
94   g_assert (nice_interfaces_is_private_ip (&sin.addr) == FALSE);
95 }
96 #endif /* G_OS_UNIX */
97
98 int
99 main (void)
100 {
101 #ifdef G_OS_UNIX
102   test_ipv4 ();
103   test_ipv6 ();
104 #endif
105   return 0;
106 }
107