]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/udev-test.pl
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
=> "program result substitution",
199 devpath
=> "block/sda/sda3",
200 expected
=> "special-device-3" ,
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"
210 desc
=> "program result substitution",
212 devpath
=> "block/sda/sda3",
213 expected
=> "test-0:0:0:0" ,
215 BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
219 desc
=> "program with escaped format char (tricky: callout returns format char!)",
221 devpath
=> "block/sda/sda3",
222 expected
=> "escape-3" ,
224 BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
228 desc
=> "program result substitution (numbered part of)",
230 devpath
=> "block/sda/sda3",
231 expected
=> "link1" ,
233 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%1c", SYMLINK="%2c %3c"
237 desc
=> "invalid program for device with no bus",
239 devpath
=> "class/tty/console",
242 BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
243 KERNEL="console", NAME="TTY"
247 desc
=> "valid program for device with no bus",
249 devpath
=> "class/tty/console",
252 PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
253 KERNEL="console", NAME="TTY"
257 desc
=> "invalid label for device with no bus",
259 devpath
=> "class/tty/console",
262 BUS="foo", SYSFS_dev="5:1", NAME="foo"
263 KERNEL="console", NAME="TTY"
267 desc
=> "valid label for device with no bus",
269 devpath
=> "class/tty/console",
272 SYSFS_dev="5:1", NAME="foo"
273 KERNEL="console", NAME="TTY"
277 desc
=> "program and bus type match",
279 devpath
=> "block/sda",
280 expected
=> "scsi-0:0:0:0" ,
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"
288 desc
=> "symlink creation (same directory)",
290 devpath
=> "class/tty/ttyUSB0",
291 expected
=> "visor0" ,
293 KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
297 desc
=> "symlink creation (relative link back)",
299 devpath
=> "block/sda/sda2",
300 expected
=> "1/2/a/b/symlink" ,
302 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
306 desc
=> "symlink creation (relative link forward)",
308 devpath
=> "block/sda/sda2",
309 expected
=> "1/2/symlink" ,
311 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
315 desc
=> "symlink creation (relative link back and forward)",
317 devpath
=> "block/sda/sda2",
318 expected
=> "1/2/c/d/symlink" ,
320 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
324 desc
=> "multiple symlinks",
326 devpath
=> "class/tty/ttyUSB0",
327 expected
=> "second-0" ,
329 KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
333 desc
=> "sysfs parent heirachy",
335 devpath
=> "class/tty/ttyUSB0",
336 expected
=> "visor" ,
338 SYSFS_idProduct="2008", NAME="visor"
342 desc
=> "name test with ! in the name",
344 devpath
=> "block/rd!c0d0",
345 expected
=> "rd/c0d0" ,
347 BUS="scsi", NAME="%k"
348 KERNEL="ttyUSB0", NAME="visor"
352 desc
=> "name test with ! in the name, but no matching rule",
354 devpath
=> "block/rd!c0d0",
355 expected
=> "rd/c0d0" ,
357 KERNEL="ttyUSB0", NAME="visor"
363 $ENV{UDEV_TEST
} = "yes";
364 $ENV{SYSFS_PATH
} = $sysfs;
365 $ENV{UDEV_CONFIG_FILE
} = $main_conf;
369 my ($action, $subsys, $devpath, $config) = @_;
371 $ENV{DEVPATH
} = $devpath;
373 # create temporary config
374 open CONF
, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
378 $ENV{ACTION
} = $action;
379 system("$udev_bin $subsys");
385 my ($config, $number) = @_;
387 print "TEST $number: $config->{desc}\n";
388 print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
390 udev
("add", $config->{subsys
}, $config->{devpath
}, \
$config->{conf
});
391 if (-e
"$PWD/$udev_root$config->{expected}") {
394 print "add: error\n";
395 system("tree $udev_root");
400 udev
("remove", $config->{subsys
}, $config->{devpath
}, \
$config->{conf
});
401 if ((-e
"$PWD/$udev_root$config->{expected}") ||
402 (-l
"$PWD/$udev_root$config->{expected}")) {
403 print "remove: error\n\n";
404 system("tree $udev_root");
407 print "remove: ok\n\n";
412 system("rm -rf $udev_root");
413 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
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";
427 $test_num = $ARGV[0];
428 print "udev-test will run test number $test_num only\n";
430 run_test
($tests[$test_num-1], $test_num);
433 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
435 foreach my $config (@tests) {
436 run_test
($config, $test_num);
442 print "$error errors occured\n\n";
446 system("rm -rf $udev_root");