summaryrefslogtreecommitdiff
path: root/libnm-core/tests/test-compare.c
blob: dca5c7d3d148fad5e646574133a81615cfb92e3d (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2007 - 2014 Red Hat, Inc.
 * Copyright 2007 - 2008 Novell, Inc.
 */

#include "nm-default.h"

#include <arpa/inet.h>
#include <netinet/in.h>

#include "nm-property-compare.h"

#include "nm-test-utils.h"

static void
compare_ints (void)
{
	GVariant *value1, *value2;

	value1 = g_variant_new_int32 (5);
	value2 = g_variant_new_int32 (5);
	g_assert (nm_property_compare (value1, value2) == 0);

	g_variant_unref (value2);
	value2 = g_variant_new_int32 (10);
	g_assert (nm_property_compare (value1, value2) < 0);

	g_variant_unref (value2);
	value2 = g_variant_new_int32 (-1);
	g_assert (nm_property_compare (value1, value2) > 0);

	g_variant_unref (value1);
	g_variant_unref (value2);
}

static void
compare_strings (void)
{
	GVariant *value1, *value2;
	const char *str1 = "hello";
	const char *str2 = "world";

	value1 = g_variant_new_string (str1);
	value2 = g_variant_new_string (str1);
	g_assert (nm_property_compare (value1, value2) == 0);

	g_variant_unref (value2);
	value2 = g_variant_new_string (str2);
	g_assert (nm_property_compare (value1, value2) < 0);

	g_assert (nm_property_compare (value2, value1) > 0);

	g_variant_unref (value1);
	g_variant_unref (value2);
}

static void
compare_strv (void)
{
	GVariant *value1, *value2;
	const char * const strv1[] = { "foo", "bar", "baz", NULL };
	const char * const strv2[] = { "foo", "bar", "bar", NULL };
	const char * const strv3[] = { "foo", "bar", NULL };
	const char * const strv4[] = { "foo", "bar", "baz", "bam", NULL };

	value1 = g_variant_new_strv (strv1, -1);
	value2 = g_variant_new_strv (strv1, -1);
	g_assert (nm_property_compare (value1, value2) == 0);

	g_variant_unref (value2);
	value2 = g_variant_new_strv (strv2, -1);
	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value2);
	value2 = g_variant_new_strv (strv3, -1);
	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value2);
	value2 = g_variant_new_strv (strv4, -1);
	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value1);
	g_variant_unref (value2);
}

static void
compare_arrays (void)
{
	GVariant *value1, *value2;
	guint32 array[] = { 0, 1, 2, 3, 4 };

	value1 = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
	                                    array, G_N_ELEMENTS (array),
	                                    sizeof (guint32));
	value2 = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
	                                    array, G_N_ELEMENTS (array),
	                                    sizeof (guint32));

	g_assert (nm_property_compare (value1, value2) == 0);

	g_variant_unref (value2);
	value2 = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
	                                    array + 1, G_N_ELEMENTS (array) - 1,
	                                    sizeof (guint32));
	g_assert (nm_property_compare (value1, value2) != 0);

	array[0] = 7;
	g_variant_unref (value2);
	value2 = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
	                                    array, G_N_ELEMENTS (array),
	                                    sizeof (guint32));
	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value1);
	g_variant_unref (value2);
}

static void
compare_str_hash (void)
{
	GVariant *value1, *value2;
	GVariantBuilder builder;

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
	g_variant_builder_add (&builder, "{ss}", "key1", "hello");
	g_variant_builder_add (&builder, "{ss}", "key2", "world");
	g_variant_builder_add (&builder, "{ss}", "key3", "!");
	value1 = g_variant_builder_end (&builder);

	g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
	g_variant_builder_add (&builder, "{ss}", "key3", "!");
	g_variant_builder_add (&builder, "{ss}", "key2", "world");
	g_variant_builder_add (&builder, "{ss}", "key1", "hello");
	value2 = g_variant_builder_end (&builder);

	g_assert (nm_property_compare (value1, value2) == 0);

	g_variant_unref (value2);
	g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
	g_variant_builder_add (&builder, "{ss}", "key1", "hello");
	g_variant_builder_add (&builder, "{ss}", "key3", "!");
	value2 = g_variant_builder_end (&builder);

	g_assert (nm_property_compare (value1, value2) != 0);
	g_assert (nm_property_compare (value2, value1) != 0);

	g_variant_unref (value2);
	g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
	g_variant_builder_add (&builder, "{ss}", "key1", "hello");
	g_variant_builder_add (&builder, "{ss}", "key2", "moon");
	g_variant_builder_add (&builder, "{ss}", "key3", "!");
	value2 = g_variant_builder_end (&builder);

	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value1);
	g_variant_unref (value2);
}

static void
compare_ip6_addresses (void)
{
	GVariant *value1, *value2;
	struct in6_addr addr1;
	struct in6_addr addr2;
	struct in6_addr addr3;
	guint32 prefix1 = 64;
	guint32 prefix2 = 64;
	guint32 prefix3 = 0;

	inet_pton (AF_INET6, "1:2:3:4:5:6:7:8", &addr1);
	inet_pton (AF_INET6, "ffff:2:3:4:5:6:7:8", &addr2);
	inet_pton (AF_INET6, "::", &addr3);

	value1 = g_variant_new ("(@ayu@ay)",
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr1.s6_addr, 16, 1),
	                        prefix1,
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr3.s6_addr, 16, 1));

	value2 = g_variant_new ("(@ayu@ay)",
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr1.s6_addr, 16, 1),
	                        prefix1,
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr3.s6_addr, 16, 1));

	g_assert (nm_property_compare (value1, value2) == 0);

	g_variant_unref (value2);
	value2 = g_variant_new ("(@ayu@ay)",
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr2.s6_addr, 16, 1),
	                        prefix2,
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr3.s6_addr, 16, 1));

	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value2);
	value2 = g_variant_new ("(@ayu@ay)",
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr3.s6_addr, 16, 1),
	                        prefix3,
	                        g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
	                                                   (guint8 *) addr3.s6_addr, 16, 1));

	g_assert (nm_property_compare (value1, value2) != 0);

	g_variant_unref (value1);
	g_variant_unref (value2);
}

NMTST_DEFINE ();

int
main (int argc, char *argv[])
{
	nmtst_init (&argc, &argv, TRUE);

	g_test_add_func ("/libnm/compare/ints", compare_ints);
	g_test_add_func ("/libnm/compare/strings", compare_strings);
	g_test_add_func ("/libnm/compare/strv", compare_strv);
	g_test_add_func ("/libnm/compare/arrays", compare_arrays);
	g_test_add_func ("/libnm/compare/str_hash", compare_str_hash);
	g_test_add_func ("/libnm/compare/ip6_addresses", compare_ip6_addresses);

	return g_test_run ();
}