Back to index

nagios-plugins  1.4.16
test_ini1.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * This program is free software: you can redistribute it and/or modify
00004 * it under the terms of the GNU General Public License as published by
00005 * the Free Software Foundation, either version 3 of the License, or
00006 * (at your option) any later version.
00007 * 
00008 * This program is distributed in the hope that it will be useful,
00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 * GNU General Public License for more details.
00012 * 
00013 * You should have received a copy of the GNU General Public License
00014 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 * 
00016 * 
00017 *****************************************************************************/
00018 
00019 #include "common.h"
00020 #include "utils_base.h"
00021 #include "parse_ini.h"
00022 
00023 #include "tap.h"
00024 
00025 void my_free(char *string) {
00026        if (string != NULL) {
00027               printf("string:\n\t|%s|\n", string);
00028               free(string);
00029        }
00030 }
00031 
00032 char*
00033 list2str(np_arg_list *optlst)
00034 {
00035        char *optstr=NULL;
00036        np_arg_list *optltmp;
00037 
00038        /* Put everything as a space-separated string */
00039        asprintf(&optstr, "");
00040        while (optlst) {
00041               asprintf(&optstr, "%s%s ", optstr, optlst->arg);
00042               optltmp=optlst;
00043               optlst=optlst->next;
00044               free(optltmp);
00045        }
00046        /* Strip last whitespace */
00047        if (strlen(optstr)>1) optstr[strlen(optstr)-1]='\0';
00048 
00049        return optstr;
00050 }
00051 
00052 int
00053 main (int argc, char **argv)
00054 {
00055        char *optstr=NULL;
00056 
00057        plan_tests(12);
00058 
00059        optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
00060        ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected");
00061        my_free(optstr);
00062 
00063        optstr=list2str(np_get_defaults("@./config-tiny.ini", "section"));
00064        ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific");
00065        my_free(optstr);
00066 
00067        optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
00068        ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
00069        my_free(optstr);
00070 
00071        optstr=list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk"));
00072        ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
00073        my_free(optstr);
00074 
00075        optstr=list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk"));
00076        ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
00077        my_free(optstr);
00078 
00079        optstr=list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk"));
00080        ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
00081        my_free(optstr);
00082 
00083        optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
00084        ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
00085        my_free(optstr);
00086 
00087        optstr=list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk"));
00088        ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
00089        my_free(optstr);
00090 
00091        optstr=list2str(np_get_defaults("check space_and_flags@./plugin.ini", "check_disk"));
00092        ok( !strcmp(optstr, "--foo=bar -a -b --bar"), "plugin.ini space in stanza and flag arguments");
00093        my_free(optstr);
00094 
00095        optstr=list2str(np_get_defaults("Section Two@./config-dos.ini", "check_disk"));
00096        ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-dos.ini's Section Two as expected");
00097        my_free(optstr);
00098 
00099        optstr=list2str(np_get_defaults("section_twice@./plugin.ini", "check_disk"));
00100        ok( !strcmp(optstr, "--foo=bar --bar=foo"), "plugin.ini's section_twice defined twice in the file");
00101        my_free(optstr);
00102 
00103        optstr=list2str(np_get_defaults("tcp_long_lines@plugins.ini", "check_tcp"));
00104        ok( !strcmp(optstr, "--escape --send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda --expect=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda --jail"), "Long options");
00105        my_free(optstr);
00106 
00107        return exit_status();
00108 }
00109