Back to index

nagios-plugins  1.4.16
test_opts1.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 /* This my_free would be the most efficient way of freeing an extra-opts array,
00025  * provided as an example */
00026 #if 0
00027 void my_free(int *argc, char **newargv, char **argv) {
00028        int i;
00029        /* Return if both arrays are the same */
00030        if (newargv == argv) return;
00031 
00032        for (i=1; i<*argc; i++) {
00033               /* Free array items until we reach the start of the original argv */
00034               if (newargv[i] == argv[1]) break;
00035               free(newargv[i]);
00036        }
00037        /* Free the array itself */
00038        free(newargv);
00039 }
00040 #else
00041 void my_free(int *argc, char **newargv, char **argv) {
00042        /* Free stuff (and print while we're at it) */
00043        int i, freeflag=1;
00044        printf ("    Arg(%i): ", *argc+1);
00045        printf ("'%s' ", newargv[0]);
00046        for (i=1; i<*argc; i++) {
00047               printf ("'%s' ", newargv[i]);
00048               /* Stop freeing when we get to the start of the original array */
00049               if (freeflag) {
00050                      if (newargv[i] == argv[1])
00051                             freeflag=0;
00052                      else
00053                             free(newargv[i]);
00054               }
00055        }
00056        printf ("\n");
00057        /* Free only if it's a different array */
00058        if (newargv != argv) free(newargv);
00059        *argc=0;
00060 }
00061 #endif
00062 
00063 int array_diff(int i1, char **a1, int i2, char **a2) {
00064        int i;
00065 
00066        if (i1 != i2) {
00067               printf("    Argument count doesn't match!\n");
00068               return 0;
00069        }
00070        for (i=0; i<=i1; i++) {
00071               if (a1[i]==NULL && a2[i]==NULL) continue;
00072               if (a1[i]==NULL || a2[i]==NULL) {
00073                      printf("    Argument # %i null in one array!\n", i);
00074                      return 0;
00075               }
00076               if (strcmp(a1[i], a2[i])) {
00077                      printf("    Argument # %i doesn't match!\n", i);
00078                      return 0;
00079               }
00080        }
00081        return 1;
00082 }
00083 
00084 int
00085 main (int argc, char **argv)
00086 {
00087        char **argv_new=NULL;
00088        int i, argc_test;
00089 
00090        plan_tests(5);
00091 
00092        {
00093               char *argv_test[] = {"prog_name", (char *) NULL};
00094               argc_test=1;
00095               char *argv_known[] = {"prog_name", (char *) NULL};
00096               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00097               ok(array_diff(argc_test, argv_new, 1, argv_known), "No opts, returns correct argv/argc");
00098               my_free(&argc_test, argv_new, argv_test);
00099        }
00100 
00101        {
00102               char *argv_test[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL};
00103               argc_test=5;
00104               char *argv_known[] = {"prog_name", "arg1", "--arg2=val1", "--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), "No extra opts, verbatim copy of argv");
00107               my_free(&argc_test, argv_new, argv_test);
00108        }
00109 
00110        {
00111               char *argv_test[] = {"prog_name", "--extra-opts=@./config-opts.ini", (char *) NULL};
00112               argc_test=2;
00113               char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", (char *) NULL};
00114               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00115               ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts using default section");
00116               my_free(&argc_test, argv_new, argv_test);
00117        }
00118 
00119        {
00120               char *argv_test[] = {"prog_name", "--extra-opts=sect1@./config-opts.ini", "--extra-opts", "sect2@./config-opts.ini", (char *) NULL};
00121               argc_test=4;
00122               char *argv_known[] = {"prog_name", "--one=two", "--something else=oops", "--this=that", (char *) NULL};
00123               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00124               ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts specified twice");
00125               my_free(&argc_test, argv_new, argv_test);
00126        }
00127 
00128        {
00129               char *argv_test[] = {"prog_name", "--arg1=val1", "--extra-opts=@./config-opts.ini", "--extra-opts", "sect1@./config-opts.ini", "--arg2", (char *) NULL};
00130               argc_test=6;
00131               char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", "--one=two", "--arg1=val1", "--arg2", (char *) NULL};
00132               argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
00133               ok(array_diff(argc_test, argv_new, 7, argv_known), "twice extra opts using two sections");
00134               my_free(&argc_test, argv_new, argv_test);
00135        }
00136 
00137        return exit_status();
00138 }
00139