1 /*****************************************************************************
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * $Id$
17 *
18 *****************************************************************************/
20 #include "common.h"
21 #include "parse_ini.h"
22 #include "utils_base.h"
24 #include "tap.h"
26 void my_free(char *string) {
27 if (string != NULL) {
28 printf("string:\n\t|%s|\n", string);
29 free(string);
30 }
31 }
33 char*
34 list2str(np_arg_list *optlst)
35 {
36 char *optstr=NULL;
38 /* Put everything as a space-separated string */
39 asprintf(&optstr, "");
40 while (optlst) {
41 asprintf(&optstr, "%s%s ", optstr, optlst->arg);
42 optlst=optlst->next;
43 }
44 /* Strip last whitespace */
45 if (strlen(optstr)>1) optstr[strlen(optstr)-1]='\0';
47 return optstr;
48 }
50 int
51 main (int argc, char **argv)
52 {
53 char *optstr=NULL;
55 plan_tests(10);
57 optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
58 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected");
59 my_free(optstr);
61 optstr=list2str(np_get_defaults("@./config-tiny.ini", "section"));
62 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific");
63 my_free(optstr);
65 optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
66 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name over specified one");
67 my_free(optstr);
69 optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
70 ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
71 my_free(optstr);
73 optstr=list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk"));
74 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
75 my_free(optstr);
77 optstr=list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk"));
78 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
79 my_free(optstr);
81 optstr=list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk"));
82 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
83 my_free(optstr);
85 optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
86 ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
87 my_free(optstr);
89 optstr=list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk"));
90 ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
91 my_free(optstr);
93 optstr=list2str(np_get_defaults("check space_and_flags@./plugin.ini", "check_disk"));
94 ok( !strcmp(optstr, "--foo=bar -a -b --bar"), "plugin.ini space in stanza and flag arguments");
95 my_free(optstr);
97 return exit_status();
98 }