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
, char *regstr
, int cflags
, int expect
, char *desc
);
26 int main(int argc
, char **argv
) {
27 struct name_list
*exclude_filesystem
= NULL
;
28 struct name_list
*exclude_fstype
= NULL
;
29 struct name_list
*dummy_mountlist
= NULL
;
30 struct name_list
*temp_name
;
31 struct parameter_list
*paths
= NULL
;
32 struct parameter_list
*p
, *prev
= NULL
, *last
= NULL
;
34 struct mount_entry
*dummy_mount_list
;
35 struct mount_entry
*me
;
36 struct mount_entry
**mtail
= &dummy_mount_list
;
37 int cflags
= REG_NOSUB
| REG_EXTENDED
;
38 int found
= 0, count
= 0;
42 ok(np_find_name(exclude_filesystem
, "/var/log") == false, "/var/log not in list");
43 np_add_name(&exclude_filesystem
, "/var/log");
44 ok(np_find_name(exclude_filesystem
, "/var/log") == true, "is in list now");
45 ok(np_find_name(exclude_filesystem
, "/home") == false, "/home not in list");
46 np_add_name(&exclude_filesystem
, "/home");
47 ok(np_find_name(exclude_filesystem
, "/home") == true, "is in list now");
48 ok(np_find_name(exclude_filesystem
, "/var/log") == true, "/var/log still in list");
50 ok(np_find_name(exclude_fstype
, "iso9660") == false, "iso9660 not in list");
51 np_add_name(&exclude_fstype
, "iso9660");
52 ok(np_find_name(exclude_fstype
, "iso9660") == true, "is in list now");
54 ok(np_find_name(exclude_filesystem
, "iso9660") == false, "Make sure no clashing in variables");
57 for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
58 printf("Name: %s\n", temp_name->name);
62 me
= (struct mount_entry
*)malloc(sizeof *me
);
63 me
->me_devname
= strdup("/dev/c0t0d0s0");
64 me
->me_mountdir
= strdup("/");
68 me
= (struct mount_entry
*)malloc(sizeof *me
);
69 me
->me_devname
= strdup("/dev/c1t0d1s0");
70 me
->me_mountdir
= strdup("/var");
74 me
= (struct mount_entry
*)malloc(sizeof *me
);
75 me
->me_devname
= strdup("/dev/c2t0d0s0");
76 me
->me_mountdir
= strdup("/home");
80 np_test_mount_entry_regex(dummy_mount_list
, strdup("/"), cflags
, 3, strdup("a"));
81 np_test_mount_entry_regex(dummy_mount_list
, strdup("/dev"), cflags
, 3, strdup("regex on dev names:"));
82 np_test_mount_entry_regex(dummy_mount_list
, strdup("/foo"), cflags
, 0, strdup("regex on non existent dev/path:"));
83 np_test_mount_entry_regex(dummy_mount_list
, strdup("/Foo"), cflags
| REG_ICASE
, 0, strdup("regi on non existent dev/path:"));
84 np_test_mount_entry_regex(dummy_mount_list
, strdup("/c.t0"), cflags
, 3, strdup("partial devname regex match:"));
85 np_test_mount_entry_regex(dummy_mount_list
, strdup("c0t0"), cflags
, 1, strdup("partial devname regex match:"));
86 np_test_mount_entry_regex(dummy_mount_list
, strdup("C0t0"), cflags
| REG_ICASE
, 1, strdup("partial devname regi match:"));
87 np_test_mount_entry_regex(dummy_mount_list
, strdup("home"), cflags
, 1, strdup("partial pathname regex match:"));
88 np_test_mount_entry_regex(dummy_mount_list
, strdup("hOme"), cflags
| REG_ICASE
, 1, strdup("partial pathname regi match:"));
89 np_test_mount_entry_regex(dummy_mount_list
, strdup("(/home)|(/var)"), cflags
, 2, strdup("grouped regex pathname match:"));
90 np_test_mount_entry_regex(dummy_mount_list
, strdup("(/homE)|(/Var)"), cflags
| REG_ICASE
, 2, strdup("grouped regi pathname match:"));
92 np_add_parameter(&paths
, "/home/groups");
93 np_add_parameter(&paths
, "/var");
94 np_add_parameter(&paths
, "/tmp");
95 np_add_parameter(&paths
, "/home/tonvoon");
96 np_add_parameter(&paths
, "/dev/c2t0d0s0");
98 np_set_best_match(paths
, dummy_mount_list
, false);
99 for (p
= paths
; p
; p
= p
->name_next
) {
100 struct mount_entry
*temp_me
;
101 temp_me
= p
->best_match
;
102 if (!strcmp(p
->name
, "/home/groups")) {
103 ok(temp_me
&& !strcmp(temp_me
->me_mountdir
, "/home"), "/home/groups got right best match: /home");
104 } else if (!strcmp(p
->name
, "/var")) {
105 ok(temp_me
&& !strcmp(temp_me
->me_mountdir
, "/var"), "/var got right best match: /var");
106 } else if (!strcmp(p
->name
, "/tmp")) {
107 ok(temp_me
&& !strcmp(temp_me
->me_mountdir
, "/"), "/tmp got right best match: /");
108 } else if (!strcmp(p
->name
, "/home/tonvoon")) {
109 ok(temp_me
&& !strcmp(temp_me
->me_mountdir
, "/home"), "/home/tonvoon got right best match: /home");
110 } else if (!strcmp(p
->name
, "/dev/c2t0d0s0")) {
111 ok(temp_me
&& !strcmp(temp_me
->me_devname
, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
115 paths
= NULL
; /* Bad boy - should free, but this is a test suite */
116 np_add_parameter(&paths
, "/home/groups");
117 np_add_parameter(&paths
, "/var");
118 np_add_parameter(&paths
, "/tmp");
119 np_add_parameter(&paths
, "/home/tonvoon");
120 np_add_parameter(&paths
, "/home");
122 np_set_best_match(paths
, dummy_mount_list
, true);
123 for (p
= paths
; p
; p
= p
->name_next
) {
124 if (!strcmp(p
->name
, "/home/groups")) {
125 ok(!p
->best_match
, "/home/groups correctly not found");
126 } else if (!strcmp(p
->name
, "/var")) {
127 ok(p
->best_match
, "/var found");
128 } else if (!strcmp(p
->name
, "/tmp")) {
129 ok(!p
->best_match
, "/tmp correctly not found");
130 } else if (!strcmp(p
->name
, "/home/tonvoon")) {
131 ok(!p
->best_match
, "/home/tonvoon not found");
132 } else if (!strcmp(p
->name
, "/home")) {
133 ok(p
->best_match
, "/home found");
137 /* test deleting first element in paths */
138 paths
= np_del_parameter(paths
, NULL
);
139 for (p
= paths
; p
; p
= p
->name_next
) {
140 if (!strcmp(p
->name
, "/home/groups"))
143 ok(found
== 0, "first element successfully deleted");
148 if (!strcmp(p
->name
, "/tmp"))
149 p
= np_del_parameter(p
, prev
);
156 for (p
= paths
; p
; p
= p
->name_next
) {
157 if (!strcmp(p
->name
, "/tmp"))
164 ok(found
== 0, "/tmp element successfully deleted");
166 p
= np_del_parameter(last
, prev
);
167 for (p
= paths
; p
; p
= p
->name_next
) {
168 if (!strcmp(p
->name
, "/home"))
173 ok(found
== 0, "last (/home) element successfully deleted");
174 ok(count
== 2, "two elements remaining");
176 return exit_status();
179 void np_test_mount_entry_regex(struct mount_entry
*dummy_mount_list
, char *regstr
, int cflags
, int expect
, char *desc
) {
182 struct mount_entry
*me
;
183 if (regcomp(&re
, regstr
, cflags
) == 0) {
184 for (me
= dummy_mount_list
; me
; me
= me
->me_next
) {
185 if (np_regex_match_mount_entry(me
, &re
))
188 ok(matches
== expect
, "%s '%s' matched %i/3 entries. ok: %i/3", desc
, regstr
, expect
, matches
);
191 ok(false, "regex '%s' not compilable", regstr
);