diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 14:58:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-31 14:58:50 +0100 |
| commit | 87eb2bef1ee2a6a42793437b2f5d63f41b1e1806 (patch) | |
| tree | 9513fdfe66af00b7332fd1578f50af8403e64371 /lib/tests/test_disk.c | |
| parent | b1d260a821b7d4916d6bf1a026fbc9b4f2b268ae (diff) | |
| parent | 7d90b8200f709d125df19fa6aedf633c64b88ad4 (diff) | |
| download | monitoring-plugins-87eb2bef1ee2a6a42793437b2f5d63f41b1e1806.tar.gz | |
Merge pull request #2034 from RincewindsHat/cleanup/lib
Cleanup/lib
Diffstat (limited to 'lib/tests/test_disk.c')
| -rw-r--r-- | lib/tests/test_disk.c | 180 |
1 files changed, 74 insertions, 106 deletions
diff --git a/lib/tests/test_disk.c b/lib/tests/test_disk.c index e283fe2e..c18db7a4 100644 --- a/lib/tests/test_disk.c +++ b/lib/tests/test_disk.c | |||
| @@ -1,36 +1,31 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify | 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 | 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 | 5 | * the Free Software Foundation, either version 3 of the License, or |
| 6 | * (at your option) any later version. | 6 | * (at your option) any later version. |
| 7 | * | 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, | 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. | 11 | * GNU General Public License for more details. |
| 12 | * | 12 | * |
| 13 | * You should have received a copy of the GNU General Public License | 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/>. | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | * | 15 | * |
| 16 | * | 16 | * |
| 17 | *****************************************************************************/ | 17 | *****************************************************************************/ |
| 18 | 18 | ||
| 19 | #include "common.h" | 19 | #include "common.h" |
| 20 | #include "utils_disk.h" | 20 | #include "utils_disk.h" |
| 21 | #include "tap.h" | 21 | #include "tap.h" |
| 22 | #include "regex.h" | 22 | #include "regex.h" |
| 23 | 23 | ||
| 24 | void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, | 24 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc); |
| 25 | char *regstr, int cflags, int expect, | ||
| 26 | char *desc); | ||
| 27 | 25 | ||
| 28 | 26 | int main(int argc, char **argv) { | |
| 29 | int | 27 | struct name_list *exclude_filesystem = NULL; |
| 30 | main (int argc, char **argv) | 28 | struct name_list *exclude_fstype = NULL; |
| 31 | { | ||
| 32 | struct name_list *exclude_filesystem=NULL; | ||
| 33 | struct name_list *exclude_fstype=NULL; | ||
| 34 | struct name_list *dummy_mountlist = NULL; | 29 | struct name_list *dummy_mountlist = NULL; |
| 35 | struct name_list *temp_name; | 30 | struct name_list *temp_name; |
| 36 | struct parameter_list *paths = NULL; | 31 | struct parameter_list *paths = NULL; |
| @@ -44,19 +39,19 @@ main (int argc, char **argv) | |||
| 44 | 39 | ||
| 45 | plan_tests(33); | 40 | plan_tests(33); |
| 46 | 41 | ||
| 47 | ok( np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); | 42 | ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); |
| 48 | np_add_name(&exclude_filesystem, "/var/log"); | 43 | np_add_name(&exclude_filesystem, "/var/log"); |
| 49 | ok( np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); | 44 | 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"); | 45 | ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); |
| 51 | np_add_name(&exclude_filesystem, "/home"); | 46 | np_add_name(&exclude_filesystem, "/home"); |
| 52 | ok( np_find_name(exclude_filesystem, "/home") == true, "is in list now"); | 47 | 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"); | 48 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); |
| 54 | 49 | ||
| 55 | ok( np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); | 50 | ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); |
| 56 | np_add_name(&exclude_fstype, "iso9660"); | 51 | np_add_name(&exclude_fstype, "iso9660"); |
| 57 | ok( np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); | 52 | ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); |
| 58 | 53 | ||
| 59 | ok( np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); | 54 | ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); |
| 60 | 55 | ||
| 61 | /* | 56 | /* |
| 62 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | 57 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { |
| @@ -64,55 +59,35 @@ main (int argc, char **argv) | |||
| 64 | } | 59 | } |
| 65 | */ | 60 | */ |
| 66 | 61 | ||
| 67 | me = (struct mount_entry *) malloc(sizeof *me); | 62 | me = (struct mount_entry *)malloc(sizeof *me); |
| 68 | me->me_devname = strdup("/dev/c0t0d0s0"); | 63 | me->me_devname = strdup("/dev/c0t0d0s0"); |
| 69 | me->me_mountdir = strdup("/"); | 64 | me->me_mountdir = strdup("/"); |
| 70 | *mtail = me; | 65 | *mtail = me; |
| 71 | mtail = &me->me_next; | 66 | mtail = &me->me_next; |
| 72 | 67 | ||
| 73 | me = (struct mount_entry *) malloc(sizeof *me); | 68 | me = (struct mount_entry *)malloc(sizeof *me); |
| 74 | me->me_devname = strdup("/dev/c1t0d1s0"); | 69 | me->me_devname = strdup("/dev/c1t0d1s0"); |
| 75 | me->me_mountdir = strdup("/var"); | 70 | me->me_mountdir = strdup("/var"); |
| 76 | *mtail = me; | 71 | *mtail = me; |
| 77 | mtail = &me->me_next; | 72 | mtail = &me->me_next; |
| 78 | 73 | ||
| 79 | me = (struct mount_entry *) malloc(sizeof *me); | 74 | me = (struct mount_entry *)malloc(sizeof *me); |
| 80 | me->me_devname = strdup("/dev/c2t0d0s0"); | 75 | me->me_devname = strdup("/dev/c2t0d0s0"); |
| 81 | me->me_mountdir = strdup("/home"); | 76 | me->me_mountdir = strdup("/home"); |
| 82 | *mtail = me; | 77 | *mtail = me; |
| 83 | mtail = &me->me_next; | 78 | mtail = &me->me_next; |
| 84 | 79 | ||
| 85 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), | 80 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); |
| 86 | cflags, 3, strdup("a")); | 81 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, strdup("regex on dev names:")); |
| 87 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), | 82 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:")); |
| 88 | cflags, 3,strdup("regex on dev names:")); | 83 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, strdup("regi on non existent dev/path:")); |
| 89 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), | 84 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, strdup("partial devname regex match:")); |
| 90 | cflags, 0, | 85 | np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, strdup("partial devname regex match:")); |
| 91 | strdup("regex on non existent dev/path:")); | 86 | np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, strdup("partial devname regi match:")); |
| 92 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), | 87 | np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, strdup("partial pathname regex match:")); |
| 93 | cflags | REG_ICASE,0, | 88 | np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, strdup("partial pathname regi match:")); |
| 94 | strdup("regi on non existent dev/path:")); | 89 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); |
| 95 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), | 90 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:")); |
| 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:")); | ||
| 116 | 91 | ||
| 117 | np_add_parameter(&paths, "/home/groups"); | 92 | np_add_parameter(&paths, "/home/groups"); |
| 118 | np_add_parameter(&paths, "/var"); | 93 | np_add_parameter(&paths, "/var"); |
| @@ -124,20 +99,20 @@ main (int argc, char **argv) | |||
| 124 | for (p = paths; p; p = p->name_next) { | 99 | for (p = paths; p; p = p->name_next) { |
| 125 | struct mount_entry *temp_me; | 100 | struct mount_entry *temp_me; |
| 126 | temp_me = p->best_match; | 101 | temp_me = p->best_match; |
| 127 | if (! strcmp(p->name, "/home/groups")) { | 102 | if (!strcmp(p->name, "/home/groups")) { |
| 128 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); | 103 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); |
| 129 | } else if (! strcmp(p->name, "/var")) { | 104 | } else if (!strcmp(p->name, "/var")) { |
| 130 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | 105 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); |
| 131 | } else if (! strcmp(p->name, "/tmp")) { | 106 | } else if (!strcmp(p->name, "/tmp")) { |
| 132 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | 107 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); |
| 133 | } else if (! strcmp(p->name, "/home/tonvoon")) { | 108 | } else if (!strcmp(p->name, "/home/tonvoon")) { |
| 134 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); | 109 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); |
| 135 | } else if (! strcmp(p->name, "/dev/c2t0d0s0")) { | 110 | } 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"); | 111 | ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); |
| 137 | } | 112 | } |
| 138 | } | 113 | } |
| 139 | 114 | ||
| 140 | paths = NULL; /* Bad boy - should free, but this is a test suite */ | 115 | paths = NULL; /* Bad boy - should free, but this is a test suite */ |
| 141 | np_add_parameter(&paths, "/home/groups"); | 116 | np_add_parameter(&paths, "/home/groups"); |
| 142 | np_add_parameter(&paths, "/var"); | 117 | np_add_parameter(&paths, "/var"); |
| 143 | np_add_parameter(&paths, "/tmp"); | 118 | np_add_parameter(&paths, "/tmp"); |
| @@ -146,31 +121,31 @@ main (int argc, char **argv) | |||
| 146 | 121 | ||
| 147 | np_set_best_match(paths, dummy_mount_list, true); | 122 | np_set_best_match(paths, dummy_mount_list, true); |
| 148 | for (p = paths; p; p = p->name_next) { | 123 | for (p = paths; p; p = p->name_next) { |
| 149 | if (! strcmp(p->name, "/home/groups")) { | 124 | if (!strcmp(p->name, "/home/groups")) { |
| 150 | ok( ! p->best_match , "/home/groups correctly not found"); | 125 | ok(!p->best_match, "/home/groups correctly not found"); |
| 151 | } else if (! strcmp(p->name, "/var")) { | 126 | } else if (!strcmp(p->name, "/var")) { |
| 152 | ok( p->best_match, "/var found"); | 127 | ok(p->best_match, "/var found"); |
| 153 | } else if (! strcmp(p->name, "/tmp")) { | 128 | } else if (!strcmp(p->name, "/tmp")) { |
| 154 | ok(! p->best_match, "/tmp correctly not found"); | 129 | ok(!p->best_match, "/tmp correctly not found"); |
| 155 | } else if (! strcmp(p->name, "/home/tonvoon")) { | 130 | } else if (!strcmp(p->name, "/home/tonvoon")) { |
| 156 | ok(! p->best_match, "/home/tonvoon not found"); | 131 | ok(!p->best_match, "/home/tonvoon not found"); |
| 157 | } else if (! strcmp(p->name, "/home")) { | 132 | } else if (!strcmp(p->name, "/home")) { |
| 158 | ok( p->best_match, "/home found"); | 133 | ok(p->best_match, "/home found"); |
| 159 | } | 134 | } |
| 160 | } | 135 | } |
| 161 | 136 | ||
| 162 | /* test deleting first element in paths */ | 137 | /* test deleting first element in paths */ |
| 163 | paths = np_del_parameter(paths, NULL); | 138 | paths = np_del_parameter(paths, NULL); |
| 164 | for (p = paths; p; p = p->name_next) { | 139 | for (p = paths; p; p = p->name_next) { |
| 165 | if (! strcmp(p->name, "/home/groups")) | 140 | if (!strcmp(p->name, "/home/groups")) |
| 166 | found = 1; | 141 | found = 1; |
| 167 | } | 142 | } |
| 168 | ok(found == 0, "first element successfully deleted"); | 143 | ok(found == 0, "first element successfully deleted"); |
| 169 | found = 0; | 144 | found = 0; |
| 170 | 145 | ||
| 171 | p=paths; | 146 | p = paths; |
| 172 | while (p) { | 147 | while (p) { |
| 173 | if (! strcmp(p->name, "/tmp")) | 148 | if (!strcmp(p->name, "/tmp")) |
| 174 | p = np_del_parameter(p, prev); | 149 | p = np_del_parameter(p, prev); |
| 175 | else { | 150 | else { |
| 176 | prev = p; | 151 | prev = p; |
| @@ -179,7 +154,7 @@ main (int argc, char **argv) | |||
| 179 | } | 154 | } |
| 180 | 155 | ||
| 181 | for (p = paths; p; p = p->name_next) { | 156 | for (p = paths; p; p = p->name_next) { |
| 182 | if (! strcmp(p->name, "/tmp")) | 157 | if (!strcmp(p->name, "/tmp")) |
| 183 | found = 1; | 158 | found = 1; |
| 184 | if (p->name_next) | 159 | if (p->name_next) |
| 185 | prev = p; | 160 | prev = p; |
| @@ -190,7 +165,7 @@ main (int argc, char **argv) | |||
| 190 | 165 | ||
| 191 | p = np_del_parameter(last, prev); | 166 | p = np_del_parameter(last, prev); |
| 192 | for (p = paths; p; p = p->name_next) { | 167 | for (p = paths; p; p = p->name_next) { |
| 193 | if (! strcmp(p->name, "/home")) | 168 | if (!strcmp(p->name, "/home")) |
| 194 | found = 1; | 169 | found = 1; |
| 195 | last = p; | 170 | last = p; |
| 196 | count++; | 171 | count++; |
| @@ -198,27 +173,20 @@ main (int argc, char **argv) | |||
| 198 | ok(found == 0, "last (/home) element successfully deleted"); | 173 | ok(found == 0, "last (/home) element successfully deleted"); |
| 199 | ok(count == 2, "two elements remaining"); | 174 | ok(count == 2, "two elements remaining"); |
| 200 | 175 | ||
| 201 | |||
| 202 | return exit_status(); | 176 | return exit_status(); |
| 203 | } | 177 | } |
| 204 | 178 | ||
| 205 | 179 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { | |
| 206 | void | ||
| 207 | np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) | ||
| 208 | { | ||
| 209 | int matches = 0; | 180 | int matches = 0; |
| 210 | regex_t re; | 181 | regex_t re; |
| 211 | struct mount_entry *me; | 182 | struct mount_entry *me; |
| 212 | if (regcomp(&re,regstr, cflags) == 0) { | 183 | if (regcomp(&re, regstr, cflags) == 0) { |
| 213 | for (me = dummy_mount_list; me; me= me->me_next) { | 184 | for (me = dummy_mount_list; me; me = me->me_next) { |
| 214 | if(np_regex_match_mount_entry(me,&re)) | 185 | if (np_regex_match_mount_entry(me, &re)) |
| 215 | matches++; | 186 | matches++; |
| 216 | } | 187 | } |
| 217 | ok( matches == expect, | 188 | ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); |
| 218 | "%s '%s' matched %i/3 entries. ok: %i/3", | ||
| 219 | desc, regstr, expect, matches); | ||
| 220 | 189 | ||
| 221 | } else | 190 | } else |
| 222 | ok ( false, "regex '%s' not compilable", regstr); | 191 | ok(false, "regex '%s' not compilable", regstr); |
| 223 | } | 192 | } |
| 224 | |||
