Back to index

nagios-plugins  1.4.16
test_opts2.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 #include "common.h"
00019 #include "utils_base.h"
00020 #include "extra_opts.h"
00021 
00022 #include "tap.h"
00023 
00024 void my_free(int *argc, char **newargv, char **argv) {
00025        /* Free stuff (and print while we're at it) */
00026        int i, freeflag=1;
00027        printf ("    Arg(%i): ", *argc+1);
00028        printf ("'%s' ", newargv[0]);
00029        for (i=1; i<*argc; i++) {
00030               printf ("'%s' ", newargv[i]);
00031               /* Stop freeing when we get to the start of the original array */
00032               if (freeflag) {
00033                      if (newargv[i] == argv[1])
00034                             freeflag=0;
00035                      else
00036                             free(newargv[i]);
00037               }
00038        }
00039        printf ("\n");
00040        /* Free only if it's a different array */
00041        if (newargv != argv) free(newargv);
00042        *argc=0;
00043 }
00044 
00045 int array_diff(int i1, char **a1, int i2, char **a2) {
00046        int i;
00047 
00048        if (i1 != i2) {
00049               printf("    Argument count doesn't match!\n");
00050               return 0;
00051        }
00052        for (i=0; i<=i1; i++) {
00053               if (a1[i]==NULL && a2[i]==NULL) continue;
00054               if (a1[i]==NULL || a2[i]==NULL) {
00055                      printf("    Argument # %i null in one array!\n", i);
00056                      return 0;
00057               }
00058               if (strcmp(a1[i], a2[i])) {
00059                      printf("    Argument # %i doesn't match!\n", i);
00060                      return 0;
00061               }
00062        }
00063        return 1;
00064 }
00065 
00066 int
00067 main (int argc, char **argv)
00068 {
00069        char **argv_new=NULL;
00070        int i, argc_test;
00071 
00072        plan_tests(5);
00073 
00074        {
00075               char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "--arg3", "val2", (char *) NULL};
00076               argc_test=5;
00077               char *argv_known[] = {"prog_name", "--foo=bar", "arg1", "--arg3", "val2", (char *) NULL};
00078               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00079               ok(array_diff(argc_test, argv_new, 5, argv_known), "Default section 1");
00080               my_free(&argc_test, argv_new, argv_test);
00081        }
00082 
00083        {
00084               char *argv_test[] = {"prog_name", "--extra-opts", (char *) NULL};
00085               argc_test=2;
00086               char *argv_known[] = {"prog_name", "--foo=bar", (char *) NULL};
00087               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00088               ok(array_diff(argc_test, argv_new, 2, argv_known), "Default section 2");
00089               my_free(&argc_test, argv_new, argv_test);
00090        }
00091 
00092        {
00093               char *argv_test[] = {"prog_name", "arg1", "--extra-opts=section1", "--arg3", "val2", (char *) NULL};
00094               argc_test=5;
00095               char *argv_known[] = {"prog_name", "--foobar=baz", "arg1", "--arg3", "val2", (char *) NULL};
00096               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00097               ok(array_diff(argc_test, argv_new, 5, argv_known), "Default section 3");
00098               my_free(&argc_test, argv_new, argv_test);
00099        }
00100 
00101        {
00102               char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "-arg3", "val2", (char *) NULL};
00103               argc_test=5;
00104               char *argv_known[] = {"prog_name", "--foo=bar", "arg1", "-arg3", "val2", (char *) NULL};
00105               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00106               ok(array_diff(argc_test, argv_new, 5, argv_known), "Default section 4");
00107               my_free(&argc_test, argv_new, argv_test);
00108        }
00109 
00110        {
00111               char *argv_test[] = {"check_tcp", "--extra-opts", "--extra-opts=tcp_long_lines", (char *) NULL};
00112               argc_test=3;
00113               char *argv_known[] = {"check_tcp", "--timeout=10", "--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", (char *) NULL};
00114               argv_new=np_extra_opts(&argc_test, argv_test, "check_tcp");
00115               ok(array_diff(argc_test, argv_new, 6, argv_known), "Long lines test");
00116               my_free(&argc_test, argv_new, argv_test);
00117        }
00118 
00119        return exit_status();
00120 }
00121