Code

Adding test for base64 lib
[nagiosplug.git] / lib / tests / test_disk.c
1 /******************************************************************************
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 2 of the License, or
6  (at your option) any later version.
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, write to the Free Software
15  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  $Id$
18  
19 ******************************************************************************/
21 #include "common.h"
22 #include "utils_disk.h"
23 #include "tap.h"
24 #include "regex.h"
26 void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
27                                 char *regstr, int cflags, int expect,
28                                 char *desc);
31 int
32 main (int argc, char **argv)
33 {
34         struct name_list *exclude_filesystem=NULL;
35         struct name_list *exclude_fstype=NULL;
36         struct name_list *dummy_mountlist = NULL;
37         struct name_list *temp_name;
38         struct parameter_list *paths = NULL;
39         struct parameter_list *p, *prev, *last;
41         struct mount_entry *dummy_mount_list;
42         struct mount_entry *me;
43         struct mount_entry **mtail = &dummy_mount_list;
44         int cflags = REG_NOSUB | REG_EXTENDED;
45         int found = 0, count = 0;
47         plan_tests(33);
49         ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
50         np_add_name(&exclude_filesystem, "/var/log");
51         ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
52         ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
53         np_add_name(&exclude_filesystem, "/home");
54         ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
55         ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
57         ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
58         np_add_name(&exclude_fstype, "iso9660");
59         ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
61         ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
63         /*
64         for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
65                 printf("Name: %s\n", temp_name->name);
66         }
67         */
69         me = (struct mount_entry *) malloc(sizeof *me);
70         me->me_devname = strdup("/dev/c0t0d0s0");
71         me->me_mountdir = strdup("/");
72         *mtail = me;
73         mtail = &me->me_next;
75         me = (struct mount_entry *) malloc(sizeof *me);
76         me->me_devname = strdup("/dev/c1t0d1s0");
77         me->me_mountdir = strdup("/var");
78         *mtail = me;
79         mtail = &me->me_next;
81         me = (struct mount_entry *) malloc(sizeof *me);
82         me->me_devname = strdup("/dev/c2t0d0s0");
83         me->me_mountdir = strdup("/home");
84         *mtail = me;
85         mtail = &me->me_next;
87         np_test_mount_entry_regex(dummy_mount_list, strdup("/"),
88                                   cflags, 3, strdup("a"));
89         np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"),
90                                   cflags, 3,strdup("regex on dev names:"));
91         np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"),
92                                   cflags, 0,
93                                   strdup("regex on non existant dev/path:"));
94         np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"),
95                                   cflags | REG_ICASE,0,
96                                   strdup("regi on non existant dev/path:"));
97         np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"),
98                                   cflags, 3,
99                                   strdup("partial devname regex match:"));
100         np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"),
101                                   cflags, 1,
102                                   strdup("partial devname regex match:"));
103         np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"),
104                                   cflags | REG_ICASE, 1,
105                                   strdup("partial devname regi match:"));
106         np_test_mount_entry_regex(dummy_mount_list, strdup("home"),
107                                   cflags, 1,
108                                   strdup("partial pathname regex match:"));
109         np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"),
110                                   cflags | REG_ICASE, 1,
111                                   strdup("partial pathname regi match:"));
112         np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"),
113                                   cflags, 2,
114                                   strdup("grouped regex pathname match:"));
115         np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"),
116                                   cflags | REG_ICASE, 2,
117                                   strdup("grouped regi pathname match:"));
119         np_add_parameter(&paths, "/home/groups");
120         np_add_parameter(&paths, "/var");
121         np_add_parameter(&paths, "/tmp");
122         np_add_parameter(&paths, "/home/tonvoon");
123         np_add_parameter(&paths, "/dev/c2t0d0s0");
125         np_set_best_match(paths, dummy_mount_list, FALSE);
126         for (p = paths; p; p = p->name_next) {
127                 struct mount_entry *temp_me;
128                 temp_me = p->best_match;
129                 if (! strcmp(p->name, "/home/groups")) {
130                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
131                 } else if (! strcmp(p->name, "/var")) {
132                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
133                 } else if (! strcmp(p->name, "/tmp")) {
134                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
135                 } else if (! strcmp(p->name, "/home/tonvoon")) {
136                         ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
137                 } else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
138                         ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
139                 }
140         }
142         paths = NULL;   /* Bad boy - should free, but this is a test suite */
143         np_add_parameter(&paths, "/home/groups");
144         np_add_parameter(&paths, "/var");
145         np_add_parameter(&paths, "/tmp");
146         np_add_parameter(&paths, "/home/tonvoon");
147         np_add_parameter(&paths, "/home");
149         np_set_best_match(paths, dummy_mount_list, TRUE);
150         for (p = paths; p; p = p->name_next) {
151                 if (! strcmp(p->name, "/home/groups")) {
152                         ok( ! p->best_match , "/home/groups correctly not found");
153                 } else if (! strcmp(p->name, "/var")) {
154                         ok( p->best_match, "/var found");
155                 } else if (! strcmp(p->name, "/tmp")) {
156                         ok(! p->best_match, "/tmp correctly not found");
157                 } else if (! strcmp(p->name, "/home/tonvoon")) {
158                         ok(! p->best_match, "/home/tonvoon not found");
159                 } else if (! strcmp(p->name, "/home")) {
160                         ok( p->best_match, "/home found");
161                 }
162         }
164         /* test deleting first element in paths */
165         paths = np_del_parameter(paths, NULL);
166         for (p = paths; p; p = p->name_next) {
167                 if (! strcmp(p->name, "/home/groups"))
168                         found = 1;
169         }
170         ok(found == 0, "first element successfully deleted");
171         found = 0;
172         
173         p=paths;
174         while (p) {
175                 if (! strcmp(p->name, "/tmp"))
176                         p = np_del_parameter(p, prev);
177                 else {
178                         prev = p;
179                         p = p->name_next;
180                 }
181         }
183         for (p = paths; p; p = p->name_next) {
184                 if (! strcmp(p->name, "/tmp"))
185                         found = 1;
186                 if (p->name_next)
187                         prev = p;
188                 else
189                         last = p;
190         }
191         ok(found == 0, "/tmp element successfully deleted");
193         p = np_del_parameter(last, prev);
194         for (p = paths; p; p = p->name_next) {
195                 if (! strcmp(p->name, "/home"))
196                         found = 1;
197                 last = p;
198                 count++;
199         }
200         ok(found == 0, "last (/home) element successfully deleted");
201         ok(count == 2, "two elements remaining");
204         return exit_status();
208 void 
209 np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
210 {       
211         int matches = 0;
212         regex_t re;
213         struct mount_entry *me;
214         if (regcomp(&re,regstr, cflags) == 0) {
215                 for (me = dummy_mount_list; me; me= me->me_next) {
216                         if(np_regex_match_mount_entry(me,&re))
217                                 matches++;
218                 }
219                 ok( matches == expect, 
220                     "%s '%s' matched %i/3 entries. ok: %i/3",
221                     desc, regstr, expect, matches);
223         } else
224                 ok ( false, "regex '%s' not compileable", regstr);