1.4.15 prep
[monitoring-plugins.git] / lib / tests / test_disk.c
blob58eb8f1dfad27772a65dc24b9efc982597bb486b
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.
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/>.
17 *****************************************************************************/
19 #include "common.h"
20 #include "utils_disk.h"
21 #include "tap.h"
22 #include "regex.h"
24 void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
25 char *regstr, int cflags, int expect,
26 char *desc);
29 int
30 main (int argc, char **argv)
32 struct name_list *exclude_filesystem=NULL;
33 struct name_list *exclude_fstype=NULL;
34 struct name_list *dummy_mountlist = NULL;
35 struct name_list *temp_name;
36 struct parameter_list *paths = NULL;
37 struct parameter_list *p, *prev, *last;
39 struct mount_entry *dummy_mount_list;
40 struct mount_entry *me;
41 struct mount_entry **mtail = &dummy_mount_list;
42 int cflags = REG_NOSUB | REG_EXTENDED;
43 int found = 0, count = 0;
45 plan_tests(33);
47 ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
48 np_add_name(&exclude_filesystem, "/var/log");
49 ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
50 ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
51 np_add_name(&exclude_filesystem, "/home");
52 ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
53 ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
55 ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
56 np_add_name(&exclude_fstype, "iso9660");
57 ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
59 ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
62 for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
63 printf("Name: %s\n", temp_name->name);
67 me = (struct mount_entry *) malloc(sizeof *me);
68 me->me_devname = strdup("/dev/c0t0d0s0");
69 me->me_mountdir = strdup("/");
70 *mtail = me;
71 mtail = &me->me_next;
73 me = (struct mount_entry *) malloc(sizeof *me);
74 me->me_devname = strdup("/dev/c1t0d1s0");
75 me->me_mountdir = strdup("/var");
76 *mtail = me;
77 mtail = &me->me_next;
79 me = (struct mount_entry *) malloc(sizeof *me);
80 me->me_devname = strdup("/dev/c2t0d0s0");
81 me->me_mountdir = strdup("/home");
82 *mtail = me;
83 mtail = &me->me_next;
85 np_test_mount_entry_regex(dummy_mount_list, strdup("/"),
86 cflags, 3, strdup("a"));
87 np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"),
88 cflags, 3,strdup("regex on dev names:"));
89 np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"),
90 cflags, 0,
91 strdup("regex on non existant dev/path:"));
92 np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"),
93 cflags | REG_ICASE,0,
94 strdup("regi on non existant dev/path:"));
95 np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"),
96 cflags, 3,
97 strdup("partial devname regex match:"));
98 np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"),
99 cflags, 1,
100 strdup("partial devname regex match:"));
101 np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"),
102 cflags | REG_ICASE, 1,
103 strdup("partial devname regi match:"));
104 np_test_mount_entry_regex(dummy_mount_list, strdup("home"),
105 cflags, 1,
106 strdup("partial pathname regex match:"));
107 np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"),
108 cflags | REG_ICASE, 1,
109 strdup("partial pathname regi match:"));
110 np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"),
111 cflags, 2,
112 strdup("grouped regex pathname match:"));
113 np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"),
114 cflags | REG_ICASE, 2,
115 strdup("grouped regi pathname match:"));
117 np_add_parameter(&paths, "/home/groups");
118 np_add_parameter(&paths, "/var");
119 np_add_parameter(&paths, "/tmp");
120 np_add_parameter(&paths, "/home/tonvoon");
121 np_add_parameter(&paths, "/dev/c2t0d0s0");
123 np_set_best_match(paths, dummy_mount_list, FALSE);
124 for (p = paths; p; p = p->name_next) {
125 struct mount_entry *temp_me;
126 temp_me = p->best_match;
127 if (! strcmp(p->name, "/home/groups")) {
128 ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
129 } else if (! strcmp(p->name, "/var")) {
130 ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
131 } else if (! strcmp(p->name, "/tmp")) {
132 ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
133 } else if (! strcmp(p->name, "/home/tonvoon")) {
134 ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
135 } else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
136 ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
140 paths = NULL; /* Bad boy - should free, but this is a test suite */
141 np_add_parameter(&paths, "/home/groups");
142 np_add_parameter(&paths, "/var");
143 np_add_parameter(&paths, "/tmp");
144 np_add_parameter(&paths, "/home/tonvoon");
145 np_add_parameter(&paths, "/home");
147 np_set_best_match(paths, dummy_mount_list, TRUE);
148 for (p = paths; p; p = p->name_next) {
149 if (! strcmp(p->name, "/home/groups")) {
150 ok( ! p->best_match , "/home/groups correctly not found");
151 } else if (! strcmp(p->name, "/var")) {
152 ok( p->best_match, "/var found");
153 } else if (! strcmp(p->name, "/tmp")) {
154 ok(! p->best_match, "/tmp correctly not found");
155 } else if (! strcmp(p->name, "/home/tonvoon")) {
156 ok(! p->best_match, "/home/tonvoon not found");
157 } else if (! strcmp(p->name, "/home")) {
158 ok( p->best_match, "/home found");
162 /* test deleting first element in paths */
163 paths = np_del_parameter(paths, NULL);
164 for (p = paths; p; p = p->name_next) {
165 if (! strcmp(p->name, "/home/groups"))
166 found = 1;
168 ok(found == 0, "first element successfully deleted");
169 found = 0;
171 p=paths;
172 while (p) {
173 if (! strcmp(p->name, "/tmp"))
174 p = np_del_parameter(p, prev);
175 else {
176 prev = p;
177 p = p->name_next;
181 for (p = paths; p; p = p->name_next) {
182 if (! strcmp(p->name, "/tmp"))
183 found = 1;
184 if (p->name_next)
185 prev = p;
186 else
187 last = p;
189 ok(found == 0, "/tmp element successfully deleted");
191 p = np_del_parameter(last, prev);
192 for (p = paths; p; p = p->name_next) {
193 if (! strcmp(p->name, "/home"))
194 found = 1;
195 last = p;
196 count++;
198 ok(found == 0, "last (/home) element successfully deleted");
199 ok(count == 2, "two elements remaining");
202 return exit_status();
206 void
207 np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
209 int matches = 0;
210 regex_t re;
211 struct mount_entry *me;
212 if (regcomp(&re,regstr, cflags) == 0) {
213 for (me = dummy_mount_list; me; me= me->me_next) {
214 if(np_regex_match_mount_entry(me,&re))
215 matches++;
217 ok( matches == expect,
218 "%s '%s' matched %i/3 entries. ok: %i/3",
219 desc, regstr, expect, matches);
221 } else
222 ok ( false, "regex '%s' not compilable", regstr);