File: test-address.c

package info (click to toggle)
gnome-vfs 1%3A2.24.4-6.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,144 kB
  • ctags: 10,422
  • sloc: ansic: 78,500; sh: 10,341; makefile: 902; perl: 99
file content (160 lines) | stat: -rw-r--r-- 4,409 bytes parent folder | download | duplicates (6)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-address.c - Test program for the GNOME Virtual File System.

   Copyright (C) 2005 Christian Kellner

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Christian Kellner <gicmo@gnome.org>
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#include <stdio.h>
#include <libgnomevfs/gnome-vfs-init.h>
#include <libgnomevfs/gnome-vfs-address.h>

int
main (int argc, char **argv)
{
	GnomeVFSAddress *v4_a;
	GnomeVFSAddress *v4_b;
	guint32          v4_numeric;
	gboolean         equal;
#ifdef ENABLE_IPV6
	GnomeVFSAddress *v6_a;
	GnomeVFSAddress *v6_b;
#endif
	fprintf (stderr, "Testing GnomeVFSAddress\n");
		
	gnome_vfs_init ();

	/* v4 test cases */
	
	v4_a = gnome_vfs_address_new_from_string ("127.3.2.1");

	/* generate a numeric 127.0.0.1 */
	v4_numeric = g_htonl (2130706433U);
	
	v4_b = gnome_vfs_address_new_from_ipv4 (v4_numeric);
	
	g_assert (v4_a && v4_b);
	g_assert (gnome_vfs_address_get_family_type (v4_a) == AF_INET);
	g_assert (gnome_vfs_address_get_family_type (v4_b) == AF_INET);

	g_assert (gnome_vfs_address_get_ipv4 (v4_b) == v4_numeric);
	
	/* compare the whole address for now */
	
	equal = gnome_vfs_address_match (v4_a, v4_b, 32);
	g_assert (equal == FALSE);
	
	equal = gnome_vfs_address_match (v4_a, v4_b, 24);
	g_assert (equal == FALSE);
	
	equal = gnome_vfs_address_match (v4_a, v4_b, 16);
	g_assert (equal == FALSE);
	
	equal = gnome_vfs_address_match (v4_a, v4_b, 8);
	g_assert (equal == TRUE);

	equal = gnome_vfs_address_match (v4_a, v4_b, 0);
	g_assert (equal == FALSE);

	gnome_vfs_address_free (v4_a);
	v4_a = gnome_vfs_address_new_from_string ("127.0.0.1");

	equal = gnome_vfs_address_match (v4_a, v4_b, 32);
	g_assert (equal == TRUE);

	gnome_vfs_address_free (v4_b);
	v4_b = gnome_vfs_address_dup (v4_a);

	equal = gnome_vfs_address_equal (v4_a, v4_b);
	g_assert (equal == TRUE);
	
	equal = gnome_vfs_address_match (v4_a, v4_b, 32);
	g_assert (equal == TRUE);

#ifdef INADDR_LOOPBACK
	v4_numeric = gnome_vfs_address_get_ipv4 (v4_a);
	g_assert (INADDR_LOOPBACK == g_ntohl (v4_numeric));
#endif
	
#ifdef ENABLE_IPV6
	/* Mapped v4 test cases */

	v6_a = gnome_vfs_address_new_from_string ("::ffff:127.0.0.1");

	g_assert (v6_a);
	g_assert (gnome_vfs_address_get_family_type (v6_a) == AF_INET6);

	/* v4 mapped in v6 test (v4_a still is 127.0.0.1) */
	equal = gnome_vfs_address_match (v4_a, v6_a, 32);
	g_assert (equal == TRUE);

	gnome_vfs_address_free (v6_a);
	
	v6_a = gnome_vfs_address_new_from_string ("fe80::dead:babe");
	v6_b = gnome_vfs_address_new_from_string ("fe80::dead:beef");

	g_assert (v6_a && v6_b);
	g_assert (gnome_vfs_address_get_family_type (v6_a) == AF_INET6);
	g_assert (gnome_vfs_address_get_family_type (v6_b) == AF_INET6);

	equal = gnome_vfs_address_match (v6_a, v6_b, 128);
	g_assert (equal == FALSE);

	/* fe80::dead:bx* */
	equal = gnome_vfs_address_match (v6_a, v6_b, 120);
	g_assert (equal == FALSE);

	/* fe80::dead:b* */
	/* both address are equal from this mask on */
	equal = gnome_vfs_address_match (v6_a, v6_b, 116);
	g_assert (equal == TRUE);

	/* fe80::dead:* */
	equal = gnome_vfs_address_match (v6_a, v6_b, 112);
	g_assert (equal == TRUE);

	/* fe80::* */
	equal = gnome_vfs_address_match (v6_a, v6_b, 64);
	g_assert (equal == TRUE);
	
	/* _dup test for v6 */	
	gnome_vfs_address_free (v6_b);
	v6_b = gnome_vfs_address_dup (v6_a);
	
	equal = gnome_vfs_address_equal (v6_a, v6_b);
	g_assert (equal == TRUE);

	gnome_vfs_address_free (v6_a);
	gnome_vfs_address_free (v6_b);
	
#endif 	
	gnome_vfs_address_free (v4_a);
	gnome_vfs_address_free (v4_b);

	
	return 0;
}