diff options
Diffstat (limited to 'plugins/tests/test_check_disk.c')
-rw-r--r-- | plugins/tests/test_check_disk.c | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/plugins/tests/test_check_disk.c b/plugins/tests/test_check_disk.c new file mode 100644 index 00000000..32b46c2d --- /dev/null +++ b/plugins/tests/test_check_disk.c | |||
@@ -0,0 +1,213 @@ | |||
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 | * | ||
17 | *****************************************************************************/ | ||
18 | |||
19 | #include "common.h" | ||
20 | #include "../check_disk.d/utils_disk.h" | ||
21 | #include "../../tap/tap.h" | ||
22 | #include "regex.h" | ||
23 | |||
24 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, | ||
25 | int expect, char *desc); | ||
26 | |||
27 | int main(int argc, char **argv) { | ||
28 | plan_tests(35); | ||
29 | |||
30 | struct name_list *exclude_filesystem = NULL; | ||
31 | ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); | ||
32 | np_add_name(&exclude_filesystem, "/var/log"); | ||
33 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); | ||
34 | ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); | ||
35 | np_add_name(&exclude_filesystem, "/home"); | ||
36 | ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); | ||
37 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); | ||
38 | |||
39 | struct name_list *exclude_fstype = NULL; | ||
40 | ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); | ||
41 | np_add_name(&exclude_fstype, "iso9660"); | ||
42 | ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); | ||
43 | |||
44 | ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); | ||
45 | |||
46 | /* | ||
47 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | ||
48 | printf("Name: %s\n", temp_name->name); | ||
49 | } | ||
50 | */ | ||
51 | |||
52 | struct mount_entry *dummy_mount_list; | ||
53 | struct mount_entry **mtail = &dummy_mount_list; | ||
54 | struct mount_entry *me = (struct mount_entry *)malloc(sizeof *me); | ||
55 | me->me_devname = strdup("/dev/c0t0d0s0"); | ||
56 | me->me_mountdir = strdup("/"); | ||
57 | *mtail = me; | ||
58 | mtail = &me->me_next; | ||
59 | |||
60 | me = (struct mount_entry *)malloc(sizeof *me); | ||
61 | me->me_devname = strdup("/dev/c1t0d1s0"); | ||
62 | me->me_mountdir = strdup("/var"); | ||
63 | *mtail = me; | ||
64 | mtail = &me->me_next; | ||
65 | |||
66 | me = (struct mount_entry *)malloc(sizeof *me); | ||
67 | me->me_devname = strdup("/dev/c2t0d0s0"); | ||
68 | me->me_mountdir = strdup("/home"); | ||
69 | *mtail = me; | ||
70 | mtail = &me->me_next; | ||
71 | |||
72 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
73 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); | ||
74 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, | ||
75 | strdup("regex on dev names:")); | ||
76 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, | ||
77 | strdup("regex on non existent dev/path:")); | ||
78 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, | ||
79 | strdup("regi on non existent dev/path:")); | ||
80 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, | ||
81 | strdup("partial devname regex match:")); | ||
82 | np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, | ||
83 | strdup("partial devname regex match:")); | ||
84 | np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, | ||
85 | strdup("partial devname regi match:")); | ||
86 | np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, | ||
87 | strdup("partial pathname regex match:")); | ||
88 | np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, | ||
89 | strdup("partial pathname regi match:")); | ||
90 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, | ||
91 | strdup("grouped regex pathname match:")); | ||
92 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, | ||
93 | strdup("grouped regi pathname match:")); | ||
94 | |||
95 | filesystem_list test_paths = filesystem_list_init(); | ||
96 | mp_int_fs_list_append(&test_paths, "/home/groups"); | ||
97 | mp_int_fs_list_append(&test_paths, "/var"); | ||
98 | mp_int_fs_list_append(&test_paths, "/tmp"); | ||
99 | mp_int_fs_list_append(&test_paths, "/home/tonvoon"); | ||
100 | mp_int_fs_list_append(&test_paths, "/dev/c2t0d0s0"); | ||
101 | ok(test_paths.length == 5, "List counter works correctly with appends"); | ||
102 | |||
103 | mp_int_fs_list_set_best_match(test_paths, dummy_mount_list, false); | ||
104 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
105 | struct mount_entry *temp_me; | ||
106 | temp_me = p->best_match; | ||
107 | if (!strcmp(p->name, "/home/groups")) { | ||
108 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), | ||
109 | "/home/groups got right best match: /home"); | ||
110 | } else if (!strcmp(p->name, "/var")) { | ||
111 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | ||
112 | } else if (!strcmp(p->name, "/tmp")) { | ||
113 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | ||
114 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
115 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), | ||
116 | "/home/tonvoon got right best match: /home"); | ||
117 | } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { | ||
118 | ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), | ||
119 | "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
124 | mp_int_fs_list_del(&test_paths, p); | ||
125 | } | ||
126 | ok(test_paths.length == 0, "List delete sets counter properly"); | ||
127 | |||
128 | mp_int_fs_list_append(&test_paths, "/home/groups"); | ||
129 | mp_int_fs_list_append(&test_paths, "/var"); | ||
130 | mp_int_fs_list_append(&test_paths, "/tmp"); | ||
131 | mp_int_fs_list_append(&test_paths, "/home/tonvoon"); | ||
132 | mp_int_fs_list_append(&test_paths, "/home"); | ||
133 | |||
134 | mp_int_fs_list_set_best_match(test_paths, dummy_mount_list, true); | ||
135 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
136 | if (!strcmp(p->name, "/home/groups")) { | ||
137 | ok(!p->best_match, "/home/groups correctly not found"); | ||
138 | } else if (!strcmp(p->name, "/var")) { | ||
139 | ok(p->best_match, "/var found"); | ||
140 | } else if (!strcmp(p->name, "/tmp")) { | ||
141 | ok(!p->best_match, "/tmp correctly not found"); | ||
142 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
143 | ok(!p->best_match, "/home/tonvoon not found"); | ||
144 | } else if (!strcmp(p->name, "/home")) { | ||
145 | ok(p->best_match, "/home found"); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | bool found = false; | ||
150 | /* test deleting first element in paths */ | ||
151 | mp_int_fs_list_del(&test_paths, NULL); | ||
152 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
153 | if (!strcmp(p->name, "/home/groups")) { | ||
154 | found = true; | ||
155 | } | ||
156 | } | ||
157 | ok(!found, "first element successfully deleted"); | ||
158 | found = false; | ||
159 | |||
160 | parameter_list_elem *prev = NULL; | ||
161 | parameter_list_elem *p = NULL; | ||
162 | for (parameter_list_elem *path = test_paths.first; path; path = mp_int_fs_list_get_next(path)) { | ||
163 | if (!strcmp(path->name, "/tmp")) { | ||
164 | mp_int_fs_list_del(&test_paths, path); | ||
165 | } | ||
166 | p = path; | ||
167 | } | ||
168 | |||
169 | parameter_list_elem *last = NULL; | ||
170 | for (parameter_list_elem *path = test_paths.first; path; path = mp_int_fs_list_get_next(path)) { | ||
171 | if (!strcmp(path->name, "/tmp")) { | ||
172 | found = true; | ||
173 | } | ||
174 | if (path->next) { | ||
175 | prev = path; | ||
176 | } else { | ||
177 | last = path; | ||
178 | } | ||
179 | } | ||
180 | ok(!found, "/tmp element successfully deleted"); | ||
181 | |||
182 | int count = 0; | ||
183 | mp_int_fs_list_del(&test_paths, p); | ||
184 | for (p = test_paths.first; p; p = p->next) { | ||
185 | if (!strcmp(p->name, "/home")) { | ||
186 | found = true; | ||
187 | } | ||
188 | last = p; | ||
189 | count++; | ||
190 | } | ||
191 | ok(!found, "last (/home) element successfully deleted"); | ||
192 | ok(count == 2, "two elements remaining"); | ||
193 | |||
194 | return exit_status(); | ||
195 | } | ||
196 | |||
197 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, | ||
198 | int expect, char *desc) { | ||
199 | regex_t regex; | ||
200 | if (regcomp(®ex, regstr, cflags) == 0) { | ||
201 | int matches = 0; | ||
202 | for (struct mount_entry *me = dummy_mount_list; me; me = me->me_next) { | ||
203 | if (np_regex_match_mount_entry(me, ®ex)) { | ||
204 | matches++; | ||
205 | } | ||
206 | } | ||
207 | ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, | ||
208 | matches); | ||
209 | |||
210 | } else { | ||
211 | ok(false, "regex '%s' not compilable", regstr); | ||
212 | } | ||
213 | } | ||