From 53bd84feff7675f549afa30a39c430069c458021 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 12 Mar 2013 13:38:39 -0400 Subject: [PATCH] config: move NMConfig into its own subdirectory/library Also, remove the unused NMConfigError, and add a config-parsing test program. --- .gitignore | 1 + configure.ac | 2 + src/Makefile.am | 5 +- src/config/Makefile.am | 16 ++++ src/{ => config}/nm-config.c | 11 --- src/{ => config}/nm-config.h | 7 -- src/config/tests/Makefile.am | 24 ++++++ src/config/tests/NetworkManager.conf | 14 ++++ src/config/tests/bad.conf | 1 + src/config/tests/test-config.c | 120 +++++++++++++++++++++++++++ src/generated/Makefile.am | 2 + 11 files changed, 183 insertions(+), 20 deletions(-) create mode 100644 src/config/Makefile.am rename src/{ => config}/nm-config.c (97%) rename src/{ => config}/nm-config.h (94%) create mode 100644 src/config/tests/Makefile.am create mode 100644 src/config/tests/NetworkManager.conf create mode 100644 src/config/tests/bad.conf create mode 100644 src/config/tests/test-config.c diff --git a/.gitignore b/.gitignore index ef0dac0a4..193d381bc 100644 --- a/.gitignore +++ b/.gitignore @@ -178,6 +178,7 @@ src/tests/test-dhcp-options src/tests/test-policy-hosts src/tests/test-wifi-ap-utils src/dhcp-manager/tests/test-dhcp-dhclient +src/config/tests/test-config src/settings/plugins/keyfile/tests/test-keyfile src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh diff --git a/configure.ac b/configure.ac index f7b88fa5c..81346803e 100644 --- a/configure.ac +++ b/configure.ac @@ -635,6 +635,8 @@ src/Makefile src/tests/Makefile src/generated/Makefile src/logging/Makefile +src/config/Makefile +src/config/tests/Makefile src/posix-signals/Makefile src/dns-manager/Makefile src/vpn-manager/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 45de7fd39..7848b7d64 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,7 @@ SUBDIRS= \ generated \ logging \ + config \ posix-signals \ dns-manager \ vpn-manager \ @@ -29,6 +30,7 @@ INCLUDES = -I${top_srcdir} \ -I${top_builddir}/src/generated \ -I${top_srcdir}/src/generated \ -I${top_srcdir}/src/logging \ + -I${top_srcdir}/src/config \ -I${top_srcdir}/src/posix-signals \ -I${top_srcdir}/src/dns-manager \ -I${top_srcdir}/src/vpn-manager \ @@ -161,8 +163,6 @@ NetworkManager_SOURCES = \ nm-ip6-config.h \ nm-active-connection.h \ nm-active-connection.c \ - nm-config.h \ - nm-config.c \ main.c \ nm-policy.c \ nm-policy.h \ @@ -323,6 +323,7 @@ endif NetworkManager_LDADD = \ ./generated/libnm-generated.la \ ./logging/libnm-logging.la \ + ./config/libnm-config.la \ ./posix-signals/libnm-posix-signals.la \ ./dns-manager/libdns-manager.la \ ./vpn-manager/libvpn-manager.la \ diff --git a/src/config/Makefile.am b/src/config/Makefile.am new file mode 100644 index 000000000..b79d27408 --- /dev/null +++ b/src/config/Makefile.am @@ -0,0 +1,16 @@ +SUBDIRS = . tests + +noinst_LTLIBRARIES = libnm-config.la + +libnm_config_la_SOURCES = \ + nm-config.c \ + nm-config.h + +libnm_config_la_CPPFLAGS = \ + $(GLIB_CFLAGS) \ + -I$(top_srcdir)/src/logging \ + -DNMCONFDIR=\"$(nmconfdir)\" + +libnm_config_la_LIBADD = \ + $(GLIB_LIBS) + diff --git a/src/nm-config.c b/src/config/nm-config.c similarity index 97% rename from src/nm-config.c rename to src/config/nm-config.c index 5d66330ad..548c107ca 100644 --- a/src/nm-config.c +++ b/src/config/nm-config.c @@ -48,17 +48,6 @@ G_DEFINE_TYPE (NMConfig, nm_config, G_TYPE_OBJECT) /************************************************************************/ -GQuark -nm_config_error_quark (void) -{ - static GQuark quark = 0; - if (!quark) - quark = g_quark_from_static_string ("nm-config-error"); - return quark; -} - -/************************************************************************/ - const char * nm_config_get_path (NMConfig *config) { diff --git a/src/nm-config.h b/src/config/nm-config.h similarity index 94% rename from src/nm-config.h rename to src/config/nm-config.h index 9681e78d8..ef351c1b2 100644 --- a/src/nm-config.h +++ b/src/config/nm-config.h @@ -44,13 +44,6 @@ typedef struct { GType nm_config_get_type (void); -typedef enum { - NM_CONFIG_ERROR_NO_MEMORY = 0, /*< nick=NoMemory >*/ -} NMConfigError; - -#define NM_CONFIG_ERROR (nm_config_error_quark ()) -GQuark nm_config_error_quark (void); - NMConfig *nm_config_get (void); NMConfig *nm_config_new (const char *cli_config_path, const char *cli_plugins, diff --git a/src/config/tests/Makefile.am b/src/config/tests/Makefile.am new file mode 100644 index 000000000..cf91697ef --- /dev/null +++ b/src/config/tests/Makefile.am @@ -0,0 +1,24 @@ +if ENABLE_TESTS + +INCLUDES = \ + -I$(top_srcdir)/src/config + +noinst_PROGRAMS = \ + test-config + +test_config_CPPFLAGS = \ + -DSRCDIR=\""$(srcdir)"\" \ + $(GLIB_CFLAGS) + +test_config_LDADD = \ + $(top_builddir)/src/config/libnm-config.la \ + $(top_builddir)/src/logging/libnm-logging.la + +check-local: test-config + $(abs_builddir)/test-config + +EXTRA_DIST = \ + NetworkManager.conf \ + bad.conf + +endif diff --git a/src/config/tests/NetworkManager.conf b/src/config/tests/NetworkManager.conf new file mode 100644 index 000000000..3261e4063 --- /dev/null +++ b/src/config/tests/NetworkManager.conf @@ -0,0 +1,14 @@ +[main] +dhcp=dhclient +plugins=foo,bar,baz + +[logging] +level=INFO + +[connectivity] +uri=http://example.com +interval=100 +response=Hello + +[extra-section] +extra-key=some value diff --git a/src/config/tests/bad.conf b/src/config/tests/bad.conf new file mode 100644 index 000000000..20d6a67a1 --- /dev/null +++ b/src/config/tests/bad.conf @@ -0,0 +1 @@ +This is not a keyfile. diff --git a/src/config/tests/test-config.c b/src/config/tests/test-config.c new file mode 100644 index 000000000..9b7082838 --- /dev/null +++ b/src/config/tests/test-config.c @@ -0,0 +1,120 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2013 Red Hat, Inc. + * + */ + +#include + +#include + +static void +test_config_simple (void) +{ + NMConfig *config; + GError *error = NULL; + const char **plugins; + + config = nm_config_new (SRCDIR "/NetworkManager.conf", + NULL, NULL, NULL, NULL, -1, NULL, + &error); + g_assert_no_error (error); + + g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); + g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhclient"); + g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO"); + g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 100); + + plugins = nm_config_get_plugins (config); + g_assert_cmpint (g_strv_length ((char **)plugins), ==, 3); + g_assert_cmpstr (plugins[0], ==, "foo"); + g_assert_cmpstr (plugins[1], ==, "bar"); + g_assert_cmpstr (plugins[2], ==, "baz"); + + g_object_unref (config); +} + +static void +test_config_non_existent (void) +{ + NMConfig *config; + GError *error = NULL; + + config = nm_config_new (SRCDIR "/no-such-file", + NULL, NULL, NULL, NULL, -1, NULL, + &error); + g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND); +} + +static void +test_config_parse_error (void) +{ + NMConfig *config; + GError *error = NULL; + + config = nm_config_new (SRCDIR "/bad.conf", + NULL, NULL, NULL, NULL, -1, NULL, + &error); + g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE); +} + +static void +test_config_override (void) +{ + NMConfig *config; + GError *error = NULL; + const char **plugins; + + config = nm_config_new (SRCDIR "/NetworkManager.conf", + "alpha,beta,gamma,delta", NULL, NULL, NULL, 12, NULL, + &error); + g_assert_no_error (error); + + g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); + g_assert_cmpstr (nm_config_get_dhcp_client (config), ==, "dhclient"); + g_assert_cmpstr (nm_config_get_log_level (config), ==, "INFO"); + g_assert_cmpint (nm_config_get_connectivity_interval (config), ==, 12); + + plugins = nm_config_get_plugins (config); + g_assert_cmpint (g_strv_length ((char **)plugins), ==, 4); + g_assert_cmpstr (plugins[0], ==, "alpha"); + g_assert_cmpstr (plugins[1], ==, "beta"); + g_assert_cmpstr (plugins[2], ==, "gamma"); + g_assert_cmpstr (plugins[3], ==, "delta"); + + g_object_unref (config); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/config/simple", test_config_simple); + g_test_add_func ("/config/non-existent", test_config_non_existent); + g_test_add_func ("/config/parse-error", test_config_parse_error); + + /* This one has to come last, because it leaves its values in + * nm-config.c's global variables, and there's no way to reset + * those to NULL. + */ + g_test_add_func ("/config/override", test_config_override); + + return g_test_run (); +} + diff --git a/src/generated/Makefile.am b/src/generated/Makefile.am index 307766e1e..323dca756 100644 --- a/src/generated/Makefile.am +++ b/src/generated/Makefile.am @@ -11,6 +11,7 @@ libnm_generated_la_SOURCES = \ nm_daemon_all_sources = \ $(top_srcdir)/src/*.[ch] \ $(top_srcdir)/src/logging/*.[ch] \ + $(top_srcdir)/src/config/*.[ch] \ $(top_srcdir)/src/dns-manager/*.[ch] \ $(top_srcdir)/src/vpn-manager/*.[ch] \ $(top_srcdir)/src/dhcp-manager/*.[ch] \ @@ -55,6 +56,7 @@ INCLUDES = \ -I${top_builddir}/include \ -I${top_srcdir}/src \ -I${top_srcdir}/src/logging \ + -I${top_srcdir}/src/config \ -I${top_srcdir}/src/dns-manager \ -I${top_srcdir}/src/vpn-manager \ -I${top_srcdir}/src/dhcp-manager \