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