From b707370f13871faf163f34433a80dcbcd86f55be Mon Sep 17 00:00:00 2001 From: Marius Vollmer Date: Fri, 23 Nov 2012 17:58:50 +0200 Subject: [PATCH] Double settings. --- service/realm-settings.c | 21 +++++++ service/realm-settings.h | 4 ++ tests/Makefile.am | 8 ++- tests/test-settings.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 tests/test-settings.c diff --git a/service/realm-settings.c b/service/realm-settings.c index d3fed7e..18cc26d 100644 --- a/service/realm-settings.c +++ b/service/realm-settings.c @@ -182,6 +182,27 @@ realm_settings_string (const gchar *section, return string; } +gdouble +realm_settings_double (const gchar *section, + const gchar *key, + gdouble def) +{ + const gchar *string; + gchar *end_ptr; + gdouble val; + + string = realm_settings_value (section, key); + if (string == NULL) + return def; + + val = g_ascii_strtod (string, &end_ptr); + if (*end_ptr != '\0') { + g_critical ("Can't parse '%s' as a floating point number", string); + return def; + } + return val; +} + gboolean realm_settings_boolean (const gchar *section, const gchar *key) diff --git a/service/realm-settings.h b/service/realm-settings.h index 30acce2..9bd14ce 100644 --- a/service/realm-settings.h +++ b/service/realm-settings.h @@ -42,6 +42,10 @@ const gchar * realm_settings_value (const gchar *section, const gchar * realm_settings_string (const gchar *section, const gchar *key); +gdouble realm_settings_double (const gchar *section, + const gchar *key, + gdouble def); + gboolean realm_settings_boolean (const gchar *section, const gchar *key); diff --git a/tests/Makefile.am b/tests/Makefile.am index da00e2b..1b20766 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -19,6 +19,7 @@ TEST_PROGS = \ test-sssd-config \ test-login-name \ test-samba-ou-format \ + test-settings \ $(NULL) check_PROGRAMS = \ @@ -52,6 +53,11 @@ test_samba_ou_format_SOURCES = \ $(top_srcdir)/service/realm-samba-util.c \ $(NULL) +test_settings_SOURCES = \ + test-settings.c \ + $(top_srcdir)/service/realm-settings.c \ + $(NULL) + frob_install_packages_CFLAGS = \ -DI_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE \ $(PACKAGEKIT_CFLAGS) \ @@ -91,4 +97,4 @@ check-local: test check-memory: perform-memcheck -.PHONY: check-memory \ No newline at end of file +.PHONY: check-memory diff --git a/tests/test-settings.c b/tests/test-settings.c new file mode 100644 index 0000000..fc3f081 --- /dev/null +++ b/tests/test-settings.c @@ -0,0 +1,160 @@ +/* realmd -- Realm configuration service + * + * Copyright 2013 Red Hat Inc + * + * This program 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 licence or (at + * your option) any later version. + * + * See the included COPYING file for more information. + * + * Author: Stef Walter + */ + +#include "config.h" + +#include "service/realm-settings.h" + +#include +#include + +#include + +static void +write_config (const char *contents) +{ + GError *error = NULL; + int ret; + + ret = g_mkdir_with_parents ("/tmp/realmd-etc", 0700); + g_assert (ret >= 0); + g_file_set_contents ("/tmp/realmd-etc/realmd.conf", contents, -1, &error); + g_assert_no_error (error); +} + +typedef struct { + GLogLevelFlags old_fatals; + int n_criticals; +} Test; + +static void +log_counter (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + Test *test = user_data; + if ((log_level & G_LOG_LEVEL_MASK) == G_LOG_LEVEL_CRITICAL) + test->n_criticals += 1; +} + +static void +setup (Test *test, + gconstpointer unused) +{ + test->n_criticals = 0; + g_log_set_default_handler (log_counter, test); + test->old_fatals = g_log_set_always_fatal (0); +} + +static void +teardown (Test *test, + gconstpointer unused) +{ + g_log_set_default_handler (g_log_default_handler, NULL); + g_log_set_always_fatal (test->old_fatals); +} + +static void +test_string (Test *test, + gconstpointer unused) +{ + const gchar *value; + + write_config ("[one]\n" + "key = value\n"); + + realm_settings_init (); + value = realm_settings_string ("one", "key"); + g_assert_cmpstr (value, ==, "value"); + realm_settings_uninit (); +} + +static void +test_boolean (Test *test, + gconstpointer unused) +{ + gboolean value; + + write_config ("[one]\n" + "true-1 = yes\n" + "true-2 = 1\n" + "true-3 = true\n" + "true-4 = TRUE\n" + "true-5 = Yes\n" + "false-1 = no\n" + "false-2 = 0\n" + "false-3 = false\n" + "false-4 = nope\n"); + +#define ASSERT_TRUE(n) \ + value = realm_settings_boolean ("one", n); \ + g_assert_cmpint (value, ==, TRUE); + +#define ASSERT_FALSE(n) \ + value = realm_settings_boolean ("one", n); \ + g_assert_cmpint (value, ==, FALSE); + + realm_settings_init (); + ASSERT_TRUE("true-1"); + ASSERT_TRUE("true-2"); + ASSERT_TRUE("true-3"); + ASSERT_TRUE("true-4"); + ASSERT_TRUE("true-5"); + ASSERT_FALSE("false-1"); + ASSERT_FALSE("false-2"); + ASSERT_FALSE("false-3"); + ASSERT_FALSE("false-4"); + realm_settings_uninit (); +} + +static void +test_double (Test *test, + gconstpointer unused) +{ + gdouble value; + + write_config ("[one]\n" + "key = 1234.0\n" + "malformed = abc\n"); + + realm_settings_init (); + + value = realm_settings_double ("one", "key", 0.0); + g_assert_cmpfloat (value, ==, 1234.0); + + value = realm_settings_double ("one", "non-existing", 5678.0); + g_assert_cmpfloat (value, ==, 5678.0); + + value = realm_settings_double ("one", "malformed", 1212.0); + g_assert_cmpfloat (value, ==, 1212.0); + g_assert_cmpint (test->n_criticals, ==, 1); + + realm_settings_uninit (); +} + +int +main (int argc, + char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + g_set_prgname ("test-ini-config"); + + g_test_add ("/realmd/settings/string", Test, NULL, setup, test_string, teardown); + g_test_add ("/realmd/settings/double", Test, NULL, setup, test_double, teardown); + g_test_add ("/realmd/settings/boolean", Test, NULL, setup, test_boolean, teardown); + + return g_test_run (); +} -- 1.8.1.4