Back to index

nagios-plugins  1.4.16
test_disk.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_disk.h"
00021 #include "tap.h"
00022 #include "regex.h"
00023 
00024 void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
00025                                    char *regstr, int cflags, int expect,
00026                                    char *desc);
00027 
00028 
00029 int
00030 main (int argc, char **argv)
00031 {
00032        struct name_list *exclude_filesystem=NULL;
00033        struct name_list *exclude_fstype=NULL;
00034        struct name_list *dummy_mountlist = NULL;
00035        struct name_list *temp_name;
00036        struct parameter_list *paths = NULL;
00037        struct parameter_list *p, *prev, *last;
00038 
00039        struct mount_entry *dummy_mount_list;
00040        struct mount_entry *me;
00041        struct mount_entry **mtail = &dummy_mount_list;
00042        int cflags = REG_NOSUB | REG_EXTENDED;
00043        int found = 0, count = 0;
00044 
00045        plan_tests(33);
00046 
00047        ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
00048        np_add_name(&exclude_filesystem, "/var/log");
00049        ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
00050        ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
00051        np_add_name(&exclude_filesystem, "/home");
00052        ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
00053        ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
00054 
00055        ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
00056        np_add_name(&exclude_fstype, "iso9660");
00057        ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
00058 
00059        ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
00060 
00061        /*
00062        for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
00063               printf("Name: %s\n", temp_name->name);
00064        }
00065        */
00066 
00067        me = (struct mount_entry *) malloc(sizeof *me);
00068        me->me_devname = strdup("/dev/c0t0d0s0");
00069        me->me_mountdir = strdup("/");
00070        *mtail = me;
00071        mtail = &me->me_next;
00072 
00073        me = (struct mount_entry *) malloc(sizeof *me);
00074        me->me_devname = strdup("/dev/c1t0d1s0");
00075        me->me_mountdir = strdup("/var");
00076        *mtail = me;
00077        mtail = &me->me_next;
00078 
00079        me = (struct mount_entry *) malloc(sizeof *me);
00080        me->me_devname = strdup("/dev/c2t0d0s0");
00081        me->me_mountdir = strdup("/home");
00082        *mtail = me;
00083        mtail = &me->me_next;
00084 
00085        np_test_mount_entry_regex(dummy_mount_list, strdup("/"),
00086                                 cflags, 3, strdup("a"));
00087        np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"),
00088                                 cflags, 3,strdup("regex on dev names:"));
00089        np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"),
00090                                 cflags, 0,
00091                               strdup("regex on non existant dev/path:"));
00092        np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"),
00093                                 cflags | REG_ICASE,0,
00094                               strdup("regi on non existant dev/path:"));
00095        np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"),
00096                                 cflags, 3,
00097                               strdup("partial devname regex match:"));
00098        np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"),
00099                                 cflags, 1,
00100                               strdup("partial devname regex match:"));
00101        np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"),
00102                                 cflags | REG_ICASE, 1,
00103                               strdup("partial devname regi match:"));
00104        np_test_mount_entry_regex(dummy_mount_list, strdup("home"),
00105                                 cflags, 1,
00106                               strdup("partial pathname regex match:"));
00107        np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"),
00108                                 cflags | REG_ICASE, 1,
00109                               strdup("partial pathname regi match:"));
00110        np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"),
00111                                 cflags, 2,
00112                               strdup("grouped regex pathname match:"));
00113        np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"),
00114                                 cflags | REG_ICASE, 2,
00115                               strdup("grouped regi pathname match:"));
00116 
00117        np_add_parameter(&paths, "/home/groups");
00118        np_add_parameter(&paths, "/var");
00119        np_add_parameter(&paths, "/tmp");
00120        np_add_parameter(&paths, "/home/tonvoon");
00121        np_add_parameter(&paths, "/dev/c2t0d0s0");
00122 
00123        np_set_best_match(paths, dummy_mount_list, FALSE);
00124        for (p = paths; p; p = p->name_next) {
00125               struct mount_entry *temp_me;
00126               temp_me = p->best_match;
00127               if (! strcmp(p->name, "/home/groups")) {
00128                      ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
00129               } else if (! strcmp(p->name, "/var")) {
00130                      ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
00131               } else if (! strcmp(p->name, "/tmp")) {
00132                      ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
00133               } else if (! strcmp(p->name, "/home/tonvoon")) {
00134                      ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
00135               } else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
00136                      ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
00137               }
00138        }
00139 
00140        paths = NULL; /* Bad boy - should free, but this is a test suite */
00141        np_add_parameter(&paths, "/home/groups");
00142        np_add_parameter(&paths, "/var");
00143        np_add_parameter(&paths, "/tmp");
00144        np_add_parameter(&paths, "/home/tonvoon");
00145        np_add_parameter(&paths, "/home");
00146 
00147        np_set_best_match(paths, dummy_mount_list, TRUE);
00148        for (p = paths; p; p = p->name_next) {
00149               if (! strcmp(p->name, "/home/groups")) {
00150                      ok( ! p->best_match , "/home/groups correctly not found");
00151               } else if (! strcmp(p->name, "/var")) {
00152                      ok( p->best_match, "/var found");
00153               } else if (! strcmp(p->name, "/tmp")) {
00154                      ok(! p->best_match, "/tmp correctly not found");
00155               } else if (! strcmp(p->name, "/home/tonvoon")) {
00156                      ok(! p->best_match, "/home/tonvoon not found");
00157               } else if (! strcmp(p->name, "/home")) {
00158                      ok( p->best_match, "/home found");
00159               }
00160        }
00161 
00162        /* test deleting first element in paths */
00163        paths = np_del_parameter(paths, NULL);
00164        for (p = paths; p; p = p->name_next) {
00165               if (! strcmp(p->name, "/home/groups"))
00166                      found = 1;
00167        }
00168        ok(found == 0, "first element successfully deleted");
00169        found = 0;
00170        
00171        p=paths;
00172        while (p) {
00173               if (! strcmp(p->name, "/tmp"))
00174                      p = np_del_parameter(p, prev);
00175               else {
00176                      prev = p;
00177                      p = p->name_next;
00178               }
00179        }
00180 
00181        for (p = paths; p; p = p->name_next) {
00182               if (! strcmp(p->name, "/tmp"))
00183                      found = 1;
00184               if (p->name_next)
00185                      prev = p;
00186               else
00187                      last = p;
00188        }
00189        ok(found == 0, "/tmp element successfully deleted");
00190 
00191        p = np_del_parameter(last, prev);
00192        for (p = paths; p; p = p->name_next) {
00193               if (! strcmp(p->name, "/home"))
00194                      found = 1;
00195               last = p;
00196               count++;
00197        }
00198        ok(found == 0, "last (/home) element successfully deleted");
00199        ok(count == 2, "two elements remaining");
00200 
00201 
00202        return exit_status();
00203 }
00204 
00205 
00206 void 
00207 np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
00208 {      
00209        int matches = 0;
00210        regex_t re;
00211        struct mount_entry *me;
00212        if (regcomp(&re,regstr, cflags) == 0) {
00213               for (me = dummy_mount_list; me; me= me->me_next) {
00214                      if(np_regex_match_mount_entry(me,&re))
00215                             matches++;
00216               }
00217               ok( matches == expect, 
00218                   "%s '%s' matched %i/3 entries. ok: %i/3",
00219                   desc, regstr, expect, matches);
00220 
00221        } else
00222               ok ( false, "regex '%s' not compilable", regstr);
00223 }
00224