
- All internal source files (except "examples", which are not internal) should include "config.h" first. As also all internal source files should include "nm-default.h", let "config.h" be included by "nm-default.h" and include "nm-default.h" as first in every source file. We already wanted to include "nm-default.h" before other headers because it might contains some fixes (like "nm-glib.h" compatibility) that is required first. - After including "nm-default.h", we optinally allow for including the corresponding header file for the source file at hand. The idea is to ensure that each header file is self contained. - Don't include "config.h" or "nm-default.h" in any header file (except "nm-sd-adapt.h"). Public headers anyway must not include these headers, and internal headers are never included after "nm-default.h", as of the first previous point. - Include all internal headers with quotes instead of angle brackets. In practice it doesn't matter, because in our public headers we must include other headers with angle brackets. As we use our public headers also to compile our interal source files, effectively the result must be the same. Still do it for consistency. - Except for <config.h> itself. Include it with angle brackets as suggested by https://www.gnu.org/software/autoconf/manual/autoconf.html#Configuration-Headers
247 lines
8.0 KiB
C
247 lines
8.0 KiB
C
/* -*- 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 ();
|
|
}
|