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 3 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, see <http://www.gnu.org/licenses/>.
17 *****************************************************************************/
20 #include "utils_disk.h"
24 void np_test_mount_entry_regex (struct mount_entry
*dummy_mount_list
,
25 char *regstr
, int cflags
, int expect
,
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;
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("/");
73 me
= (struct mount_entry
*) malloc(sizeof *me
);
74 me
->me_devname
= strdup("/dev/c1t0d1s0");
75 me
->me_mountdir
= strdup("/var");
79 me
= (struct mount_entry
*) malloc(sizeof *me
);
80 me
->me_devname
= strdup("/dev/c2t0d0s0");
81 me
->me_mountdir
= strdup("/home");
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"),
91 strdup("regex on non existant dev/path:"));
92 np_test_mount_entry_regex(dummy_mount_list
, strdup("/Foo"),
94 strdup("regi on non existant dev/path:"));
95 np_test_mount_entry_regex(dummy_mount_list
, strdup("/c.t0"),
97 strdup("partial devname regex match:"));
98 np_test_mount_entry_regex(dummy_mount_list
, strdup("c0t0"),
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"),
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)"),
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"))
168 ok(found
== 0, "first element successfully deleted");
173 if (! strcmp(p
->name
, "/tmp"))
174 p
= np_del_parameter(p
, prev
);
181 for (p
= paths
; p
; p
= p
->name_next
) {
182 if (! strcmp(p
->name
, "/tmp"))
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"))
198 ok(found
== 0, "last (/home) element successfully deleted");
199 ok(count
== 2, "two elements remaining");
202 return exit_status();
207 np_test_mount_entry_regex (struct mount_entry
*dummy_mount_list
, char *regstr
, int cflags
, int expect
, char *desc
)
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
))
217 ok( matches
== expect
,
218 "%s '%s' matched %i/3 entries. ok: %i/3",
219 desc
, regstr
, expect
, matches
);
222 ok ( false, "regex '%s' not compilable", regstr
);