]>
Commit | Line | Data |
---|---|---|
a367f04e GKH |
1 | #!/usr/bin/perl |
2 | ||
3 | # udev-test | |
4 | # | |
5 | # Provides automated testing of the udev binary. | |
6 | # The whole test is self contained in this file, except the matching sysfs tree. | |
7 | # Simply extend the @tests array, to add a new test variant. | |
8 | # | |
9 | # Every test is driven by its own temporary config file. | |
10 | # This program prepares the environment, creates the config and calls udev. | |
11 | # | |
12 | # udev reads the config, looks at the provided sysfs and | |
13 | # first creates and then removes the device node. | |
14 | # After creation and removal the result is checked against the | |
15 | # expected value and the result is printed. | |
16 | # | |
17 | # happy testing, | |
18 | # Kay Sievers <kay.sievers@vrfy.org>, 2003 | |
19 | ||
20 | ||
21 | use warnings; | |
22 | use strict; | |
23 | ||
f8f00338 | 24 | my $PWD = $ENV{PWD}; |
a367f04e GKH |
25 | my $sysfs = "sys/"; |
26 | my $udev_bin = "../udev"; | |
27 | my $udev_root = "udev-root/"; # !!! directory will be removed !!! | |
36043f84 | 28 | my $udev_db = ".udev.tdb"; |
a367f04e | 29 | my $perm = "udev.permissions"; |
72ffa78d GKH |
30 | my $main_conf = "udev-test.conf"; |
31 | my $conf_tmp = "udev-test.rules"; | |
a367f04e GKH |
32 | |
33 | ||
34 | my @tests = ( | |
35 | { | |
36 | desc => "label test of scsi disc", | |
37 | subsys => "block", | |
38 | devpath => "block/sda", | |
39 | expected => "boot_disk" , | |
40 | conf => <<EOF | |
ac28b86d KS |
41 | BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="boot_disk%n" |
42 | KERNEL="ttyUSB0", NAME="visor" | |
a367f04e GKH |
43 | EOF |
44 | }, | |
45 | { | |
46 | desc => "label test of scsi partition", | |
47 | subsys => "block", | |
48 | devpath => "block/sda/sda1", | |
49 | expected => "boot_disk1" , | |
50 | conf => <<EOF | |
ac28b86d | 51 | BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="boot_disk%n" |
83be97ba KS |
52 | EOF |
53 | }, | |
54 | { | |
55 | desc => "label test of pattern match", | |
56 | subsys => "block", | |
57 | devpath => "block/sda/sda1", | |
58 | expected => "boot_disk1" , | |
59 | conf => <<EOF | |
ac28b86d KS |
60 | BUS="scsi", SYSFS_vendor="?IBM-ESXS", NAME="boot_disk%n-1" |
61 | BUS="scsi", SYSFS_vendor="IBM-ESXS?", NAME="boot_disk%n-2" | |
62 | BUS="scsi", SYSFS_vendor="IBM-ES??", NAME="boot_disk%n" | |
63 | BUS="scsi", SYSFS_vendor="IBM-ESXSS", NAME="boot_disk%n-3" | |
358c8c20 GKH |
64 | EOF |
65 | }, | |
66 | { | |
67 | desc => "label test of multiple sysfs files", | |
68 | subsys => "block", | |
69 | devpath => "block/sda/sda1", | |
70 | expected => "boot_disk1" , | |
71 | conf => <<EOF | |
ac28b86d KS |
72 | BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", NAME="boot_diskX%n" |
73 | BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", NAME="boot_disk%n" | |
358c8c20 GKH |
74 | EOF |
75 | }, | |
76 | { | |
77 | desc => "label test of max sysfs files", | |
78 | subsys => "block", | |
79 | devpath => "block/sda/sda1", | |
80 | expected => "boot_disk1" , | |
81 | conf => <<EOF | |
ac28b86d KS |
82 | BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", SYSFS_scsi_level="4", SYSFS_rev="B245", SYSFS_type="2", SYSFS_queue_depth="32", NAME="boot_diskXX%n" |
83 | BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", SYSFS_scsi_level="4", SYSFS_rev="B245", SYSFS_type="0", NAME="boot_disk%n" | |
0db6d4cc KS |
84 | EOF |
85 | }, | |
86 | { | |
9f1da361 | 87 | desc => "catch device by *", |
0db6d4cc KS |
88 | subsys => "tty", |
89 | devpath => "class/tty/ttyUSB0", | |
90 | expected => "visor/0" , | |
91 | conf => <<EOF | |
ac28b86d | 92 | KERNEL="ttyUSB*", NAME="visor/%n" |
2e317184 GKH |
93 | EOF |
94 | }, | |
95 | { | |
96 | desc => "catch device by * - take 2", | |
97 | subsys => "tty", | |
98 | devpath => "class/tty/ttyUSB0", | |
99 | expected => "visor/0" , | |
100 | conf => <<EOF | |
101 | KERNEL="*USB1", NAME="bad" | |
102 | KERNEL="*USB0", NAME="visor/%n" | |
9f1da361 KS |
103 | EOF |
104 | }, | |
105 | { | |
106 | desc => "catch device by ?", | |
107 | subsys => "tty", | |
108 | devpath => "class/tty/ttyUSB0", | |
109 | expected => "visor/0" , | |
110 | conf => <<EOF | |
ac28b86d KS |
111 | KERNEL="ttyUSB??*", NAME="visor/%n-1" |
112 | KERNEL="ttyUSB??", NAME="visor/%n-2" | |
113 | KERNEL="ttyUSB?", NAME="visor/%n" | |
9f1da361 KS |
114 | EOF |
115 | }, | |
116 | { | |
117 | desc => "catch device by character class", | |
118 | subsys => "tty", | |
119 | devpath => "class/tty/ttyUSB0", | |
120 | expected => "visor/0" , | |
121 | conf => <<EOF | |
ac28b86d KS |
122 | KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1" |
123 | KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2" | |
124 | KERNEL="ttyUSB[0-9]*", NAME="visor/%n" | |
a367f04e GKH |
125 | EOF |
126 | }, | |
127 | { | |
128 | desc => "replace kernel name", | |
129 | subsys => "tty", | |
130 | devpath => "class/tty/ttyUSB0", | |
131 | expected => "visor" , | |
132 | conf => <<EOF | |
ac28b86d | 133 | KERNEL="ttyUSB0", NAME="visor" |
281ff00a GKH |
134 | EOF |
135 | }, | |
136 | { | |
137 | desc => "Handle comment lines in config file (and replace kernel name)", | |
138 | subsys => "tty", | |
139 | devpath => "class/tty/ttyUSB0", | |
140 | expected => "visor" , | |
141 | conf => <<EOF | |
142 | # this is a comment | |
ac28b86d | 143 | KERNEL="ttyUSB0", NAME="visor" |
281ff00a GKH |
144 | |
145 | EOF | |
146 | }, | |
147 | { | |
148 | desc => "Handle comment lines in config file with whitespace (and replace kernel name)", | |
149 | subsys => "tty", | |
150 | devpath => "class/tty/ttyUSB0", | |
151 | expected => "visor" , | |
152 | conf => <<EOF | |
153 | # this is a comment with whitespace before the comment | |
ac28b86d | 154 | KERNEL="ttyUSB0", NAME="visor" |
281ff00a GKH |
155 | |
156 | EOF | |
157 | }, | |
158 | { | |
159 | desc => "Handle empty lines in config file (and replace kernel name)", | |
160 | subsys => "tty", | |
161 | devpath => "class/tty/ttyUSB0", | |
162 | expected => "visor" , | |
163 | conf => <<EOF | |
164 | ||
ac28b86d | 165 | KERNEL="ttyUSB0", NAME="visor" |
281ff00a | 166 | |
5499d319 KS |
167 | EOF |
168 | }, | |
169 | { | |
170 | desc => "subdirectory handling", | |
171 | subsys => "tty", | |
172 | devpath => "class/tty/ttyUSB0", | |
173 | expected => "sub/direct/ory/visor" , | |
174 | conf => <<EOF | |
ac28b86d | 175 | KERNEL="ttyUSB0", NAME="sub/direct/ory/visor" |
a367f04e GKH |
176 | EOF |
177 | }, | |
178 | { | |
179 | desc => "place on bus of scsi partition", | |
180 | subsys => "block", | |
181 | devpath => "block/sda/sda3", | |
182 | expected => "first_disk3" , | |
183 | conf => <<EOF | |
ac28b86d | 184 | BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n" |
a367f04e GKH |
185 | EOF |
186 | }, | |
187 | { | |
188 | desc => "test NAME substitution chars", | |
189 | subsys => "block", | |
190 | devpath => "block/sda/sda3", | |
191 | expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" , | |
192 | conf => <<EOF | |
ac28b86d | 193 | BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b" |
a367f04e GKH |
194 | EOF |
195 | }, | |
196 | { | |
ac28b86d | 197 | desc => "program result substitution", |
a367f04e GKH |
198 | subsys => "block", |
199 | devpath => "block/sda/sda3", | |
200 | expected => "special-device-3" , | |
201 | conf => <<EOF | |
ac28b86d KS |
202 | BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n" |
203 | BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n" | |
204 | BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n" | |
205 | BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n" | |
206 | BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n" | |
f3b04a2e GKH |
207 | EOF |
208 | }, | |
209 | { | |
ac28b86d | 210 | desc => "program result substitution", |
f3b04a2e GKH |
211 | subsys => "block", |
212 | devpath => "block/sda/sda3", | |
213 | expected => "test-0:0:0:0" , | |
214 | conf => <<EOF | |
ac28b86d | 215 | BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c" |
8ffb636f KS |
216 | EOF |
217 | }, | |
218 | { | |
219 | desc => "program with escaped format char (tricky: callout returns format char!)", | |
220 | subsys => "block", | |
221 | devpath => "block/sda/sda3", | |
222 | expected => "escape-3" , | |
223 | conf => <<EOF | |
224 | BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c" | |
b1c5e333 KS |
225 | EOF |
226 | }, | |
227 | { | |
ac28b86d | 228 | desc => "program result substitution (numbered part of)", |
b1c5e333 KS |
229 | subsys => "block", |
230 | devpath => "block/sda/sda3", | |
231 | expected => "link1" , | |
232 | conf => <<EOF | |
ac28b86d | 233 | BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%1c", SYMLINK="%2c %3c" |
8ff8bbba GKH |
234 | EOF |
235 | }, | |
236 | { | |
ac28b86d | 237 | desc => "invalid program for device with no bus", |
8ff8bbba GKH |
238 | subsys => "tty", |
239 | devpath => "class/tty/console", | |
240 | expected => "TTY" , | |
241 | conf => <<EOF | |
ac28b86d KS |
242 | BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo" |
243 | KERNEL="console", NAME="TTY" | |
1d936fbc GKH |
244 | EOF |
245 | }, | |
246 | { | |
ac28b86d | 247 | desc => "valid program for device with no bus", |
1d936fbc GKH |
248 | subsys => "tty", |
249 | devpath => "class/tty/console", | |
250 | expected => "foo" , | |
251 | conf => <<EOF | |
ac28b86d KS |
252 | PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo" |
253 | KERNEL="console", NAME="TTY" | |
64682333 GKH |
254 | EOF |
255 | }, | |
256 | { | |
257 | desc => "invalid label for device with no bus", | |
258 | subsys => "tty", | |
259 | devpath => "class/tty/console", | |
260 | expected => "TTY" , | |
261 | conf => <<EOF | |
ac28b86d KS |
262 | BUS="foo", SYSFS_dev="5:1", NAME="foo" |
263 | KERNEL="console", NAME="TTY" | |
64682333 GKH |
264 | EOF |
265 | }, | |
266 | { | |
267 | desc => "valid label for device with no bus", | |
268 | subsys => "tty", | |
269 | devpath => "class/tty/console", | |
270 | expected => "foo" , | |
271 | conf => <<EOF | |
ac28b86d KS |
272 | SYSFS_dev="5:1", NAME="foo" |
273 | KERNEL="console", NAME="TTY" | |
772558f4 GKH |
274 | EOF |
275 | }, | |
276 | { | |
ac28b86d | 277 | desc => "program and bus type match", |
772558f4 GKH |
278 | subsys => "block", |
279 | devpath => "block/sda", | |
280 | expected => "scsi-0:0:0:0" , | |
281 | conf => <<EOF | |
ac28b86d KS |
282 | BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c" |
283 | BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c" | |
284 | BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c" | |
3d150dfb KS |
285 | EOF |
286 | }, | |
287 | { | |
288 | desc => "symlink creation (same directory)", | |
289 | subsys => "tty", | |
290 | devpath => "class/tty/ttyUSB0", | |
291 | expected => "visor0" , | |
292 | conf => <<EOF | |
ac28b86d | 293 | KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n" |
3d150dfb KS |
294 | EOF |
295 | }, | |
296 | { | |
297 | desc => "symlink creation (relative link back)", | |
298 | subsys => "block", | |
299 | devpath => "block/sda/sda2", | |
300 | expected => "1/2/a/b/symlink" , | |
301 | conf => <<EOF | |
ac28b86d | 302 | BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink" |
3d150dfb KS |
303 | EOF |
304 | }, | |
305 | { | |
306 | desc => "symlink creation (relative link forward)", | |
307 | subsys => "block", | |
308 | devpath => "block/sda/sda2", | |
309 | expected => "1/2/symlink" , | |
310 | conf => <<EOF | |
ac28b86d | 311 | BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink" |
3d150dfb KS |
312 | EOF |
313 | }, | |
314 | { | |
315 | desc => "symlink creation (relative link back and forward)", | |
316 | subsys => "block", | |
317 | devpath => "block/sda/sda2", | |
318 | expected => "1/2/c/d/symlink" , | |
319 | conf => <<EOF | |
ac28b86d | 320 | BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink" |
4763256c KS |
321 | EOF |
322 | }, | |
323 | { | |
324 | desc => "multiple symlinks", | |
325 | subsys => "tty", | |
326 | devpath => "class/tty/ttyUSB0", | |
327 | expected => "second-0" , | |
328 | conf => <<EOF | |
ac28b86d | 329 | KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n" |
724257d9 GKH |
330 | EOF |
331 | }, | |
332 | { | |
333 | desc => "sysfs parent heirachy", | |
334 | subsys => "tty", | |
335 | devpath => "class/tty/ttyUSB0", | |
336 | expected => "visor" , | |
337 | conf => <<EOF | |
338 | SYSFS_idProduct="2008", NAME="visor" | |
f0142622 GKH |
339 | EOF |
340 | }, | |
341 | { | |
342 | desc => "name test with ! in the name", | |
343 | subsys => "block", | |
344 | devpath => "block/rd!c0d0", | |
345 | expected => "rd/c0d0" , | |
346 | conf => <<EOF | |
347 | BUS="scsi", NAME="%k" | |
348 | KERNEL="ttyUSB0", NAME="visor" | |
b9fc973b GKH |
349 | EOF |
350 | }, | |
351 | { | |
352 | desc => "name test with ! in the name, but no matching rule", | |
353 | subsys => "block", | |
354 | devpath => "block/rd!c0d0", | |
355 | expected => "rd/c0d0" , | |
356 | conf => <<EOF | |
357 | KERNEL="ttyUSB0", NAME="visor" | |
a367f04e GKH |
358 | EOF |
359 | }, | |
360 | ); | |
361 | ||
362 | # set env | |
363 | $ENV{UDEV_TEST} = "yes"; | |
364 | $ENV{SYSFS_PATH} = $sysfs; | |
72ffa78d | 365 | $ENV{UDEV_CONFIG_FILE} = $main_conf; |
a367f04e GKH |
366 | |
367 | ||
368 | sub udev { | |
369 | my ($action, $subsys, $devpath, $config) = @_; | |
370 | ||
371 | $ENV{DEVPATH} = $devpath; | |
a367f04e GKH |
372 | |
373 | # create temporary config | |
374 | open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp"; | |
375 | print CONF $$config; | |
376 | close CONF; | |
377 | ||
378 | $ENV{ACTION} = $action; | |
379 | system("$udev_bin $subsys"); | |
380 | } | |
381 | ||
e5fbfe0a | 382 | my $error = 0; |
72ffa78d | 383 | |
2e317184 GKH |
384 | sub run_test { |
385 | my ($config, $number) = @_; | |
386 | ||
387 | print "TEST $number: $config->{desc}\n"; | |
ac28b86d | 388 | print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n"; |
a367f04e GKH |
389 | |
390 | udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf}); | |
391 | if (-e "$PWD/$udev_root$config->{expected}") { | |
392 | print "add: ok "; | |
393 | } else { | |
394 | print "add: error\n"; | |
395 | system("tree $udev_root"); | |
396 | print "\n"; | |
397 | $error++; | |
a367f04e GKH |
398 | } |
399 | ||
400 | udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf}); | |
3d150dfb KS |
401 | if ((-e "$PWD/$udev_root$config->{expected}") || |
402 | (-l "$PWD/$udev_root$config->{expected}")) { | |
a367f04e GKH |
403 | print "remove: error\n\n"; |
404 | system("tree $udev_root"); | |
405 | $error++; | |
406 | } else { | |
407 | print "remove: ok\n\n"; | |
408 | } | |
409 | } | |
410 | ||
2e317184 GKH |
411 | # prepare |
412 | system("rm -rf $udev_root"); | |
413 | mkdir($udev_root) || die "unable to create udev_root: $udev_root\n"; | |
414 | ||
415 | # create initial config file | |
416 | open CONF, ">$main_conf" || die "unable to create config file: $main_conf"; | |
417 | print CONF "udev_root=\"$udev_root\"\n"; | |
418 | print CONF "udev_db=\"$udev_db\"\n"; | |
419 | print CONF "udev_rules=\"$conf_tmp\"\n"; | |
420 | print CONF "udev_permissions=\"$perm\"\n"; | |
421 | close CONF; | |
422 | ||
423 | my $test_num = 1; | |
424 | ||
425 | if ($ARGV[0]) { | |
426 | # only run one test | |
427 | $test_num = $ARGV[0]; | |
428 | print "udev-test will run test number $test_num only\n"; | |
429 | ||
b9fc973b | 430 | run_test($tests[$test_num-1], $test_num); |
2e317184 GKH |
431 | } else { |
432 | # test all | |
433 | print "\nudev-test will run ".($#tests + 1)." tests:\n\n"; | |
434 | ||
435 | foreach my $config (@tests) { | |
436 | run_test($config, $test_num); | |
437 | $test_num++; | |
438 | ||
439 | } | |
440 | } | |
441 | ||
a367f04e GKH |
442 | print "$error errors occured\n\n"; |
443 | ||
444 | # cleanup | |
36043f84 | 445 | unlink($udev_db); |
a367f04e GKH |
446 | system("rm -rf $udev_root"); |
447 | unlink($conf_tmp); | |
72ffa78d | 448 | unlink($main_conf); |
a367f04e | 449 |