Code

Adding libtap into distribution to help run C based tests
[nagiosplug.git] / lib / tests / test_disk.c
1 /*****************************************************************************
2
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 3 of the License, or
6 * (at your option) any later version.
7
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.
12
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 * $Id$
17
18 *****************************************************************************/
20 #include "common.h"
21 #include "utils_disk.h"
22 #include "tap.h"
23 #include "regex.h"
25 void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
26                                 char *regstr, int cflags, int expect,
27                                 char *desc);
30 int
31 main (int argc, char **argv)
32 {
33         struct name_list *exclude_filesystem=NULL;
34         struct name_list *exclude_fstype=NULL;
35         struct name_list *dummy_mountlist = NULL;
36         struct name_list *temp_name;
37         struct parameter_list *paths = NULL;
38         struct parameter_list *p, *prev, *last;
40         struct mount_entry *dummy_mount_list;
41         struct mount_entry *me;
42         struct mount_entry **mtail = &dummy_mount_list;
43         int cflags = REG_NOSUB | REG_EXTENDED;
44         int found = 0, count = 0;
46         plan_tests(33);
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         /* test deleting first element in paths */
164         paths = np_del_parameter(paths, NULL);
165         for (p = paths; p; p = p->name_next) {
166                 if (! strcmp(p->name, "/home/groups"))
167                         found = 1;
168         }
169         ok(found == 0, "first element successfully deleted");
170         found = 0;
171         
172         p=paths;
173         while (p) {
174                 if (! strcmp(p->name, "/tmp"))
175                         p = np_del_parameter(p, prev);
176                 else {
177                         prev = p;
178                         p = p->name_next;
179                 }
180         }
182         for (p = paths; p; p = p->name_next) {
183                 if (! strcmp(p->name, "/tmp"))
184                         found = 1;
185                 if (p->name_next)
186                         prev = p;
187                 else
188                         last = p;
189         }
190         ok(found == 0, "/tmp element successfully deleted");
192         p = np_del_parameter(last, prev);
193         for (p = paths; p; p = p->name_next) {
194                 if (! strcmp(p->name, "/home"))
195                         found = 1;
196                 last = p;
197                 count++;
198         }
199         ok(found == 0, "last (/home) element successfully deleted");
200         ok(count == 2, "two elements remaining");
203         return exit_status();
207 void 
208 np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
209 {       
210         int matches = 0;
211         regex_t re;
212         struct mount_entry *me;
213         if (regcomp(&re,regstr, cflags) == 0) {
214                 for (me = dummy_mount_list; me; me= me->me_next) {
215                         if(np_regex_match_mount_entry(me,&re))
216                                 matches++;
217                 }
218                 ok( matches == expect, 
219                     "%s '%s' matched %i/3 entries. ok: %i/3",
220                     desc, regstr, expect, matches);
222         } else
223                 ok ( false, "regex '%s' not compileable", regstr);