| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitconf.h" |
| 9 |
|
| 10 |
#include "testlib/cockpittest.h" |
| 11 |
|
| 12 |
#include <glib.h> |
| 13 |
|
| 14 |
/* Mock override cockpitconf.c */ |
| 15 |
extern const gchar *cockpit_config_file; |
| 16 |
|
| 17 |
static void |
| 18 |
test_get_strings (void) |
| 19 |
{ |
| 20 |
cockpit_config_file = SRCDIR "/src/common/mock-config/cockpit/cockpit.conf"; |
| 21 |
|
| 22 |
g_assert_null (cockpit_conf_string ("bad-section", "value")); |
| 23 |
g_assert_null (cockpit_conf_string ("Section1", "value")); |
| 24 |
g_assert_cmpstr (cockpit_conf_string ("Section2", "value1"), |
| 25 |
==, "string"); |
| 26 |
g_assert_cmpstr (cockpit_conf_string ("Section2", "value2"), |
| 27 |
==, "commas, or spaces"); |
| 28 |
|
| 29 |
/* Case insensitive */ |
| 30 |
g_assert_cmpstr (cockpit_conf_string ("sectiON2", "Value2"), |
| 31 |
==, "commas, or spaces"); |
| 32 |
|
| 33 |
cockpit_conf_cleanup (); |
| 34 |
} |
| 35 |
|
| 36 |
static void |
| 37 |
test_get_bool (void) |
| 38 |
{ |
| 39 |
cockpit_config_file = SRCDIR "/src/common/mock-config/cockpit/cockpit.conf"; |
| 40 |
|
| 41 |
g_assert_true (cockpit_conf_bool ("bad-section", "value", TRUE)); |
| 42 |
g_assert_false (cockpit_conf_bool ("bad-section", "value", FALSE)); |
| 43 |
g_assert_false (cockpit_conf_bool ("Section2", "missing", FALSE)); |
| 44 |
|
| 45 |
g_assert_true (cockpit_conf_bool ("Section2", "true", FALSE)); |
| 46 |
g_assert_true (cockpit_conf_bool ("Section2", "truelower", FALSE)); |
| 47 |
g_assert_true (cockpit_conf_bool ("Section2", "one", FALSE)); |
| 48 |
g_assert_true (cockpit_conf_bool ("Section2", "yes", FALSE)); |
| 49 |
|
| 50 |
g_assert_false (cockpit_conf_bool ("Section2", "value1", TRUE)); |
| 51 |
|
| 52 |
cockpit_conf_cleanup (); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
test_get_uint (void) |
| 57 |
{ |
| 58 |
cockpit_config_file = SRCDIR "/src/common/mock-config/cockpit/cockpit.conf"; |
| 59 |
|
| 60 |
g_assert_cmpuint (cockpit_conf_uint ("bad-section", "value", 1, 999, 0), ==, 1); |
| 61 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "missing", 1, 999, 0), ==, 1); |
| 62 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "mixed", 10, 999, 0), ==, 10); |
| 63 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "value1", 10, 999, 0), ==, 10); |
| 64 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "toolarge", 10, 999, 0), ==, 10); |
| 65 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "one", 10, 999, 0), ==, 1); |
| 66 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "one", 1, 999, 2), ==, 2); |
| 67 |
g_assert_cmpuint (cockpit_conf_uint ("Section2", "one", 1, 0, 0), ==, 0); |
| 68 |
cockpit_conf_cleanup (); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
test_get_strvs (void) |
| 73 |
{ |
| 74 |
const gchar * const *list = NULL; |
| 75 |
|
| 76 |
cockpit_config_file = SRCDIR "/src/common/mock-config/cockpit/cockpit.conf"; |
| 77 |
|
| 78 |
g_assert_null (cockpit_conf_strv ("bad-section", "value", ' ')); |
| 79 |
g_assert_null (cockpit_conf_strv ("Section1", "value", ' ')); |
| 80 |
|
| 81 |
/* empty list */ |
| 82 |
list = cockpit_conf_strv ("Section2", "empty", ':'); |
| 83 |
g_assert_null (list[0]); |
| 84 |
|
| 85 |
/* list with one element */ |
| 86 |
list = cockpit_conf_strv ("Section2", "value1", ' '); |
| 87 |
g_assert_cmpstr (list[0], ==, "string"); |
| 88 |
g_assert_null (list[1]); |
| 89 |
|
| 90 |
/* list with space separator */ |
| 91 |
list = cockpit_conf_strv ("Section2", "value2", ' '); |
| 92 |
g_assert_cmpstr (list[0], ==, "commas,"); |
| 93 |
g_assert_cmpstr (list[1], ==, "or"); |
| 94 |
g_assert_cmpstr (list[2], ==, "spaces"); |
| 95 |
g_assert_null (list[3]); |
| 96 |
|
| 97 |
/* list with comma separator */ |
| 98 |
list = cockpit_conf_strv ("Section2", "value3", ','); |
| 99 |
g_assert_cmpstr (list[0], ==, "commas"); |
| 100 |
g_assert_cmpstr (list[1], ==, " or spaces"); |
| 101 |
g_assert_null (list[2]); |
| 102 |
|
| 103 |
/* list with only a separator */ |
| 104 |
list = cockpit_conf_strv ("Section2", "emptystrv", ':'); |
| 105 |
g_assert_cmpstr (list[0], ==, ""); |
| 106 |
g_assert_cmpstr (list[1], ==, ""); |
| 107 |
g_assert_null (list[2]); |
| 108 |
|
| 109 |
/* list with empty value in the middle */ |
| 110 |
list = cockpit_conf_strv ("Section2", "strvemptyitems", ':'); |
| 111 |
g_assert_cmpstr (list[0], ==, "one"); |
| 112 |
g_assert_cmpstr (list[1], ==, ""); |
| 113 |
g_assert_cmpstr (list[2], ==, "three"); |
| 114 |
g_assert_null (list[3]); |
| 115 |
|
| 116 |
cockpit_conf_cleanup (); |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
test_load_dir (void) |
| 121 |
{ |
| 122 |
const char * const *dirs; |
| 123 |
|
| 124 |
cockpit_test_setenv ("XDG_CONFIG_DIRS", "/does-not-exist:" SRCDIR "/src/common/mock-config"); |
| 125 |
cockpit_config_file = "cockpit.conf"; |
| 126 |
|
| 127 |
g_assert_cmpstr (cockpit_conf_string ("Section2", "value1"), ==, "string"); |
| 128 |
|
| 129 |
dirs = cockpit_conf_get_dirs (); |
| 130 |
/* PACKAGE_SYSCONF_DIR is always first, even when XDG_CONFIG_DIRS is set */ |
| 131 |
g_assert_cmpstr (dirs[0], ==, PACKAGE_SYSCONF_DIR); |
| 132 |
g_assert_cmpstr (dirs[1], ==, "/does-not-exist"); |
| 133 |
g_assert_cmpstr (dirs[2], ==, SRCDIR "/src/common/mock-config"); |
| 134 |
g_assert_null (dirs[3]); |
| 135 |
|
| 136 |
cockpit_conf_cleanup (); |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
test_fail_load (void) |
| 141 |
{ |
| 142 |
cockpit_config_file = SRCDIR "/does-not-exist"; |
| 143 |
g_assert_null (cockpit_conf_string ("Section2", "value1")); |
| 144 |
cockpit_conf_cleanup (); |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
main (int argc, |
| 149 |
char *argv[]) |
| 150 |
{ |
| 151 |
cockpit_test_init (&argc, &argv); |
| 152 |
|
| 153 |
g_test_add_func ("/conf/test-bool", test_get_bool); |
| 154 |
g_test_add_func ("/conf/test-uint", test_get_uint); |
| 155 |
g_test_add_func ("/conf/test-strings", test_get_strings); |
| 156 |
g_test_add_func ("/conf/test-strvs", test_get_strvs); |
| 157 |
g_test_add_func ("/conf/fail_load", test_fail_load); |
| 158 |
g_test_add_func ("/conf/load_dir", test_load_dir); |
| 159 |
return g_test_run (); |
| 160 |
} |