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.
9 # Every test is driven by its own temporary config file.
10 # This program prepares the environment, creates the config and calls udev.
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.
18 # Kay Sievers <kay.sievers@vrfy.org>, 2003
26 my $udev_bin = "../udev";
27 my $udev_root = "udev-root/"; # !!! directory will be removed !!!
28 my $udev_db = ".udev.tdb";
29 my $perm = "udev.permissions";
30 my $main_conf = "udev-test.conf";
31 my $conf_tmp = "udev-test.rules";
36 desc => "label test of scsi disc",
38 devpath => "block/sda",
39 expected => "boot_disk" ,
41 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
42 KERNEL="ttyUSB0", NAME="visor"
46 desc => "label test of scsi partition",
48 devpath => "block/sda/sda1",
49 expected => "boot_disk1" ,
51 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
55 desc => "label test of pattern match",
57 devpath => "block/sda/sda1",
58 expected => "boot_disk1" ,
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"
67 desc => "label test of multiple sysfs files",
69 devpath => "block/sda/sda1",
70 expected => "boot_disk1" ,
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"
77 desc => "label test of max sysfs files",
79 devpath => "block/sda/sda1",
80 expected => "boot_disk1" ,
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"
87 desc => "catch device by *",
89 devpath => "class/tty/ttyUSB0",
90 expected => "visor/0" ,
92 KERNEL="ttyUSB*", NAME="visor/%n"
96 desc => "catch device by * - take 2",
98 devpath => "class/tty/ttyUSB0",
99 expected => "visor/0" ,
101 KERNEL="*USB1", NAME="bad"
102 KERNEL="*USB0", NAME="visor/%n"
106 desc => "catch device by ?",
108 devpath => "class/tty/ttyUSB0",
109 expected => "visor/0" ,
111 KERNEL="ttyUSB??*", NAME="visor/%n-1"
112 KERNEL="ttyUSB??", NAME="visor/%n-2"
113 KERNEL="ttyUSB?", NAME="visor/%n"
117 desc => "catch device by character class",
119 devpath => "class/tty/ttyUSB0",
120 expected => "visor/0" ,
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"
128 desc => "replace kernel name",
130 devpath => "class/tty/ttyUSB0",
131 expected => "visor" ,
133 KERNEL="ttyUSB0", NAME="visor"
137 desc => "Handle comment lines in config file (and replace kernel name)",
139 devpath => "class/tty/ttyUSB0",
140 expected => "visor" ,
143 KERNEL="ttyUSB0", NAME="visor"
148 desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
150 devpath => "class/tty/ttyUSB0",
151 expected => "visor" ,
153 # this is a comment with whitespace before the comment
154 KERNEL="ttyUSB0", NAME="visor"
159 desc => "Handle empty lines in config file (and replace kernel name)",
161 devpath => "class/tty/ttyUSB0",
162 expected => "visor" ,
165 KERNEL="ttyUSB0", NAME="visor"
170 desc => "subdirectory handling",
172 devpath => "class/tty/ttyUSB0",
173 expected => "sub/direct/ory/visor" ,
175 KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
179 desc => "place on bus of scsi partition",
181 devpath => "block/sda/sda3",
182 expected => "first_disk3" ,
184 BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
188 desc => "test NAME substitution chars",
190 devpath => "block/sda/sda3",
191 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
193 BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
197 desc => "old style SYSFS_ attribute",
199 devpath => "block/sda",
202 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="good"
206 desc => "sustitution of sysfs value (%s{file})",
208 devpath => "block/sda",
209 expected => "disk-IBM-ESXS-sda" ,
211 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="disk-%s{vendor}-%k"
212 KERNEL="ttyUSB0", NAME="visor"
216 desc => "program result substitution",
218 devpath => "block/sda/sda3",
219 expected => "special-device-3" ,
221 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
222 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
223 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
224 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
225 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
229 desc => "program result substitution",
231 devpath => "block/sda/sda3",
232 expected => "test-0:0:0:0" ,
234 BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
238 desc => "program with escaped format char (tricky: callout returns format char!)",
240 devpath => "block/sda/sda3",
241 expected => "escape-3" ,
243 BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
247 desc => "program with lots of arguments",
249 devpath => "block/sda/sda3",
252 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="%c{7}"
256 desc => "program result substitution (numbered part of)",
258 devpath => "block/sda/sda3",
259 expected => "link1" ,
261 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
265 desc => "invalid program for device with no bus",
267 devpath => "class/tty/console",
270 BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
271 KERNEL="console", NAME="TTY"
275 desc => "valid program for device with no bus",
277 devpath => "class/tty/console",
280 PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
281 KERNEL="console", NAME="TTY"
285 desc => "invalid label for device with no bus",
287 devpath => "class/tty/console",
290 BUS="foo", SYSFS{dev}="5:1", NAME="foo"
291 KERNEL="console", NAME="TTY"
295 desc => "valid label for device with no bus",
297 devpath => "class/tty/console",
300 SYSFS{dev}="5:1", NAME="foo"
301 KERNEL="console", NAME="TTY"
305 desc => "program and bus type match",
307 devpath => "block/sda",
308 expected => "scsi-0:0:0:0" ,
310 BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
311 BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
312 BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
316 desc => "symlink creation (same directory)",
318 devpath => "class/tty/ttyUSB0",
319 expected => "visor0" ,
321 KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
325 desc => "symlink creation (relative link back)",
327 devpath => "block/sda/sda2",
328 expected => "1/2/a/b/symlink" ,
330 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
334 desc => "symlink creation (relative link forward)",
336 devpath => "block/sda/sda2",
337 expected => "1/2/symlink" ,
339 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
343 desc => "symlink creation (relative link back and forward)",
345 devpath => "block/sda/sda2",
346 expected => "1/2/c/d/symlink" ,
348 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
352 desc => "multiple symlinks",
354 devpath => "class/tty/ttyUSB0",
355 expected => "second-0" ,
357 KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
361 desc => "create all possible partitions",
363 devpath => "block/sda",
364 expected => "boot_disk15" ,
366 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME{all_partitions}="boot_disk"
370 desc => "sysfs parent hierarchy",
372 devpath => "class/tty/ttyUSB0",
373 expected => "visor" ,
375 SYSFS{idProduct}="2008", NAME="visor"
379 desc => "name test with ! in the name",
381 devpath => "block/rd!c0d0",
382 expected => "rd/c0d0" ,
384 BUS="scsi", NAME="%k"
385 KERNEL="ttyUSB0", NAME="visor"
389 desc => "name test with ! in the name, but no matching rule",
391 devpath => "block/rd!c0d0",
392 expected => "rd/c0d0" ,
394 KERNEL="ttyUSB0", NAME="visor"
400 devpath => "block/sda",
401 expected => "scsi-0:0:0:0",
403 BUS="usb", ID="0:0:0:0", NAME="not-scsi"
404 BUS="scsi", ID="0:0:0:1", NAME="no-match"
405 BUS="scsi", ID=":0", NAME="short-id"
406 BUS="scsi", ID="/0:0:0:0", NAME="no-match"
407 BUS="scsi", ID="0:0:0:0", NAME="scsi-0:0:0:0"
411 desc => "ID wildcard all",
413 devpath => "block/sda",
414 expected => "scsi-0:0:0:0",
416 BUS="scsi", ID="*:1", NAME="no-match"
417 BUS="scsi", ID="*:0:1", NAME="no-match"
418 BUS="scsi", ID="*:0:0:1", NAME="no-match"
419 BUS="scsi", ID="*", NAME="scsi-0:0:0:0"
420 BUS="scsi", ID="0:0:0:0", NAME="bad"
424 desc => "ID wildcard partial",
426 devpath => "block/sda",
427 expected => "scsi-0:0:0:0",
429 BUS="scsi", ID="*:0", NAME="scsi-0:0:0:0"
430 BUS="scsi", ID="0:0:0:0", NAME="bad"
434 desc => "ID wildcard partial 2",
436 devpath => "block/sda",
437 expected => "scsi-0:0:0:0",
439 BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
440 BUS="scsi", ID="0:0:0:0", NAME="bad"
446 $ENV{UDEV_TEST} = "yes";
447 $ENV{SYSFS_PATH} = $sysfs;
448 $ENV{UDEV_CONFIG_FILE} = $main_conf;
452 my ($action, $subsys, $devpath, $config) = @_;
454 $ENV{DEVPATH} = $devpath;
456 # create temporary config
457 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
461 $ENV{ACTION} = $action;
462 system("$udev_bin $subsys");
468 my ($config, $number) = @_;
470 print "TEST $number: $config->{desc}\n";
471 print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
473 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
474 if (-e "$PWD/$udev_root$config->{expected}") {
477 print "add: error\n";
478 system("tree $udev_root");
483 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
484 if ((-e "$PWD/$udev_root$config->{expected}") ||
485 (-l "$PWD/$udev_root$config->{expected}")) {
486 print "remove: error\n\n";
487 system("tree $udev_root");
490 print "remove: ok\n\n";
495 system("rm -rf $udev_root");
496 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
498 # create initial config file
499 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
500 print CONF "udev_root=\"$udev_root\"\n";
501 print CONF "udev_db=\"$udev_db\"\n";
502 print CONF "udev_rules=\"$conf_tmp\"\n";
503 print CONF "udev_permissions=\"$perm\"\n";
510 $test_num = $ARGV[0];
511 print "udev-test will run test number $test_num only\n";
513 run_test($tests[$test_num-1], $test_num);
516 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
518 foreach my $config (@tests) {
519 run_test($config, $test_num);
525 print "$error errors occured\n\n";
529 system("rm -rf $udev_root");