summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-04-04 10:11:22 +0000
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-04-04 10:11:22 +0000
commit2a4640f6719b9688a68b796f697209b9b14cd179 (patch)
treec90a814eba255846196eede28260c3b64e391e8b
parent815fbd802f1aeee32d3d1b3751bf294e46a4c090 (diff)
downloadmonitoring-plugins-2a4640f6719b9688a68b796f697209b9b14cd179.tar.gz
make parse_ini die like in N::P if section isn't found
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1976 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--lib/extra_opts.c6
-rw-r--r--lib/parse_ini.c8
-rw-r--r--lib/tests/test_ini.c12
-rw-r--r--lib/tests/test_opts.c26
4 files changed, 26 insertions, 26 deletions
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 8c17fa4a..b555417a 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -35,11 +35,11 @@ int
35is_option2 (char *str) 35is_option2 (char *str)
36{ 36{
37 if (!str) 37 if (!str)
38 return 0; 38 return FALSE;
39 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) 39 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
40 return 1; 40 return TRUE;
41 else 41 else
42 return 0; 42 return FALSE;
43} 43}
44 44
45/* this is the externally visible function used by plugins */ 45/* this is the externally visible function used by plugins */
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index d5661c9e..96551cfe 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -34,7 +34,6 @@
34#include <sys/stat.h> 34#include <sys/stat.h>
35#include <unistd.h> 35#include <unistd.h>
36 36
37/* TODO: die like N::P if section is not found */
38/* TODO: die like N::P if config file is not found */ 37/* TODO: die like N::P if config file is not found */
39 38
40/* np_ini_info contains the result of parsing a "locator" in the format 39/* np_ini_info contains the result of parsing a "locator" in the format
@@ -104,11 +103,8 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
104 inifile=fopen(i.file, "r"); 103 inifile=fopen(i.file, "r");
105 } 104 }
106 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file")); 105 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
107 if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdin) { 106 if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
108 /* We got nothing, try the default section */ 107 die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
109 rewind(inifile);
110 read_defaults(inifile, default_section, &defaults);
111 }
112 108
113 free(i.file); 109 free(i.file);
114 if(inifile!=stdin) fclose(inifile); 110 if(inifile!=stdin) fclose(inifile);
diff --git a/lib/tests/test_ini.c b/lib/tests/test_ini.c
index 80396caa..1ab1e5d3 100644
--- a/lib/tests/test_ini.c
+++ b/lib/tests/test_ini.c
@@ -55,7 +55,7 @@ main (int argc, char **argv)
55{ 55{
56 char *optstr=NULL; 56 char *optstr=NULL;
57 57
58 plan_tests(12); 58 plan_tests(11);
59 59
60 optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk")); 60 optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
61 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected"); 61 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected");
@@ -65,9 +65,13 @@ main (int argc, char **argv)
65 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific"); 65 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific");
66 my_free(optstr); 66 my_free(optstr);
67 67
68 optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section")); 68 /*
69 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name over specified one"); 69 * This check is expected to die - It's commented so we can eventually put
70 my_free(optstr); 70 * it in a separate file for testing the return value
71 */
72 /* optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
73 ok( 0, "die if section isn't found");
74 my_free(optstr); */
71 75
72 optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk")); 76 optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
73 ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected"); 77 ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c
index 61370a9c..e878abd8 100644
--- a/lib/tests/test_opts.c
+++ b/lib/tests/test_opts.c
@@ -59,7 +59,7 @@ main (int argc, char **argv)
59 char **argv_test=NULL, **argv_known=NULL; 59 char **argv_test=NULL, **argv_known=NULL;
60 int i, argc_test; 60 int i, argc_test;
61 61
62 plan_tests(12); 62 plan_tests(9);
63 63
64 argv_test=(char **)malloc(2*sizeof(char **)); 64 argv_test=(char **)malloc(2*sizeof(char **));
65 argv_test[0] = "prog_name"; 65 argv_test[0] = "prog_name";
@@ -145,10 +145,10 @@ main (int argc, char **argv)
145 ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections"); 145 ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
146 my_free(&argc_test,argv_test); 146 my_free(&argc_test,argv_test);
147 147
148 /* Next three checks should die according to N::P - for now they're useful 148 /* Next three checks dre expected to die. They are commented out as they
149 * to test code is working properly (i.e. no srash or unexpected behavior) 149 * could possibly go in a sepatare test checked for return value.
150 */ 150 */
151 argv_test=(char **)malloc(6*sizeof(char **)); 151 /* argv_test=(char **)malloc(6*sizeof(char **));
152 argv_test[0] = "prog_name"; 152 argv_test[0] = "prog_name";
153 argv_test[1] = "arg1"; 153 argv_test[1] = "arg1";
154 argv_test[2] = "--extra-opts=missing@./config-opts.ini"; 154 argv_test[2] = "--extra-opts=missing@./config-opts.ini";
@@ -164,9 +164,9 @@ main (int argc, char **argv)
164 argv_known[4] = NULL; 164 argv_known[4] = NULL;
165 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing"); 165 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
166 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1"); 166 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1");
167 my_free(&argc_test,argv_test); 167 my_free(&argc_test,argv_test); */
168 168
169 argv_test=(char **)malloc(7*sizeof(char **)); 169 /* argv_test=(char **)malloc(7*sizeof(char **));
170 argv_test[0] = "prog_name"; 170 argv_test[0] = "prog_name";
171 argv_test[1] = "arg1"; 171 argv_test[1] = "arg1";
172 argv_test[2] = "--extra-opts"; 172 argv_test[2] = "--extra-opts";
@@ -183,9 +183,9 @@ main (int argc, char **argv)
183 argv_known[4] = NULL; 183 argv_known[4] = NULL;
184 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing"); 184 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
185 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2"); 185 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2");
186 my_free(&argc_test,argv_test); 186 my_free(&argc_test,argv_test); */
187 187
188 argv_test=(char **)malloc(6*sizeof(char **)); 188 /* argv_test=(char **)malloc(6*sizeof(char **));
189 argv_test[0] = "prog_name"; 189 argv_test[0] = "prog_name";
190 argv_test[1] = "arg1"; 190 argv_test[1] = "arg1";
191 argv_test[2] = "--extra-opts"; 191 argv_test[2] = "--extra-opts";
@@ -201,7 +201,7 @@ main (int argc, char **argv)
201 argv_known[4] = NULL; 201 argv_known[4] = NULL;
202 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing"); 202 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
203 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3"); 203 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3");
204 my_free(&argc_test,argv_test); 204 my_free(&argc_test,argv_test); */
205 205
206 setenv("NAGIOS_CONFIG_PATH", ".", 1); 206 setenv("NAGIOS_CONFIG_PATH", ".", 1);
207 argv_test=(char **)malloc(6*sizeof(char **)); 207 argv_test=(char **)malloc(6*sizeof(char **));
@@ -257,8 +257,8 @@ main (int argc, char **argv)
257 argv_test=(char **)malloc(6*sizeof(char **)); 257 argv_test=(char **)malloc(6*sizeof(char **));
258 argv_test[0] = "prog_name"; 258 argv_test[0] = "prog_name";
259 argv_test[1] = "arg1"; 259 argv_test[1] = "arg1";
260 argv_test[2] = "--extra-opts=section2"; 260 argv_test[2] = "--extra-opts";
261 argv_test[3] = "--arg3"; 261 argv_test[3] = "-arg3";
262 argv_test[4] = "val2"; 262 argv_test[4] = "val2";
263 argv_test[5] = NULL; 263 argv_test[5] = NULL;
264 argc_test=5; 264 argc_test=5;
@@ -266,11 +266,11 @@ main (int argc, char **argv)
266 argv_known[0] = "prog_name"; 266 argv_known[0] = "prog_name";
267 argv_known[1] = "--foo=bar"; 267 argv_known[1] = "--foo=bar";
268 argv_known[2] = "arg1"; 268 argv_known[2] = "arg1";
269 argv_known[3] = "--arg3"; 269 argv_known[3] = "-arg3";
270 argv_known[4] = "val2"; 270 argv_known[4] = "val2";
271 argv_known[5] = NULL; 271 argv_known[5] = NULL;
272 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk"); 272 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
273 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 3"); 273 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 4");
274 my_free(&argc_test,argv_test); 274 my_free(&argc_test,argv_test);
275 275
276 return exit_status(); 276 return exit_status();