Code

Minor corrections to test_tcp.c
[nagiosplug.git] / lib / tests / test_disk.c
1 /******************************************************************************
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 2 of the License, or
6  (at your option) any later version.
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.
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  $Id$
18  
19 ******************************************************************************/
21 #include "common.h"
22 #include "utils_disk.h"
23 #include "tap.h"
24 #include "regex.h"
26 void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
27                                 char *regstr, int cflags, int expect,
28                                 char *desc);
31 int
32 main (int argc, char **argv)
33 {
34         struct name_list *exclude_filesystem=NULL;
35         struct name_list *exclude_fstype=NULL;
36         struct name_list *dummy_mountlist = NULL;
37         struct name_list *temp_name;
38         struct parameter_list *paths = NULL;
39         struct parameter_list *p;
41         struct mount_entry *dummy_mount_list;
42         struct mount_entry *me;
43         struct mount_entry **mtail = &dummy_mount_list;
44         int cflags = REG_NOSUB | REG_EXTENDED;
46         plan_tests(29);
48         ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
49         np_add_name(&exclude_filesystem, "/var/log");
50         ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
51         ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
52         np_add_name(&exclude_filesystem, "/home");
53         ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
54         ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
56         ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
57         np_add_name(&exclude_fstype, "iso9660");
58         ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
60         ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
62         /*
63         for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
64                 printf("Name: %s\n", temp_name->name);
65         }
66         */
68         me = (struct mount_entry *) malloc(sizeof *me);
69         me->me_devname = strdup("/dev/c0t0d0s0");
70         me->me_mountdir = strdup("/");
71         *mtail = me;
72         mtail = &me->me_next;
74         me = (struct mount_entry *) malloc(sizeof *me);
75         me->me_devname = strdup("/dev/c1t0d1s0");
76         me->me_mountdir = strdup("/var");
77         *mtail = me;
78         mtail = &me->me_next;
80         me = (struct mount_entry *) malloc(sizeof *me);
81         me->me_devname = strdup("/dev/c2t0d0s0");
82         me->me_mountdir = strdup("/home");
83         *mtail = me;
84         mtail = &me->me_next;
86         np_test_mount_entry_regex(dummy_mount_list, strdup("/"),
87                                   cflags, 3, strdup("a"));
88         np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"),
89                                   cflags, 3,strdup("regex on dev names:"));
90         np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"),
91                                   cflags, 0,
92                                   strdup("regex on non existant dev/path:"));
93         np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"),
94                                   cflags | REG_ICASE,0,
95                                   strdup("regi on non existant dev/path:"));
96         np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"),
97                                   cflags, 3,
98                                   strdup("partial devname regex match:"));
99         np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"),
100                                   cflags, 1,
101                                   strdup("partial devname regex match:"));
102         np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"),
103                                   cflags | REG_ICASE, 1,
104                                   strdup("partial devname regi match:"));
105         np_test_mount_entry_regex(dummy_mount_list, strdup("home"),
106                                   cflags, 1,
107                                   strdup("partial pathname regex match:"));
108         np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"),
109                                   cflags | REG_ICASE, 1,
110                                   strdup("partial pathname regi match:"));
111         np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"),
112                                   cflags, 2,
113                                   strdup("grouped regex pathname match:"));
114         np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"),
115                                   cflags | REG_ICASE, 2,
116                                   strdup("grouped regi pathname match:"));
118         np_add_parameter(&paths, "/home/groups");
119         np_add_parameter(&paths, "/var");
120         np_add_parameter(&paths, "/tmp");
121         np_add_parameter(&paths, "/home/tonvoon");
122         np_add_parameter(&paths, "/dev/c2t0d0s0");
124         np_set_best_match(paths, dummy_mount_list, FALSE);
125         for (p = paths; p; p = p->name_next) {
126                 struct mount_entry *temp_me;
127                 temp_me = p->best_match;
128                 if (! strcmp(p->name, "/home/groups")) {
129                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
130                 } else if (! strcmp(p->name, "/var")) {
131                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
132                 } else if (! strcmp(p->name, "/tmp")) {
133                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
134                 } else if (! strcmp(p->name, "/home/tonvoon")) {
135                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
136                 } else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
137                         ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
138                 }
139         }
141         paths = NULL;   /* Bad boy - should free, but this is a test suite */
142         np_add_parameter(&paths, "/home/groups");
143         np_add_parameter(&paths, "/var");
144         np_add_parameter(&paths, "/tmp");
145         np_add_parameter(&paths, "/home/tonvoon");
146         np_add_parameter(&paths, "/home");
148         np_set_best_match(paths, dummy_mount_list, TRUE);
149         for (p = paths; p; p = p->name_next) {
150                 if (! strcmp(p->name, "/home/groups")) {
151                         ok( ! p->best_match , "/home/groups correctly not found");
152                 } else if (! strcmp(p->name, "/var")) {
153                         ok( p->best_match, "/var found");
154                 } else if (! strcmp(p->name, "/tmp")) {
155                         ok(! p->best_match, "/tmp correctly not found");
156                 } else if (! strcmp(p->name, "/home/tonvoon")) {
157                         ok(! p->best_match, "/home/tonvoon not found");
158                 } else if (! strcmp(p->name, "/home")) {
159                         ok( p->best_match, "/home found");
160                 }
161         }
163         return exit_status();
167 void 
168 np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
169 {       
170         int matches = 0;
171         regex_t re;
172         struct mount_entry *me;
173         if (regcomp(&re,regstr, cflags) == 0) {
174                 for (me = dummy_mount_list; me; me= me->me_next) {
175                         if(np_regex_match_mount_entry(me,&re))
176                                 matches++;
177                 }
178                 ok( matches == expect, 
179                     "%s '%s' matched %i/3 entries. ok: %i/3",
180                     desc, regstr, expect, matches);
182         } else
183                 ok ( false, "regex '%s' not compileable", regstr);