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 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="boot_disk%n"
42 REPLACE, KERNEL="ttyUSB0", NAME="visor"
46 desc => "label test of scsi partition",
48 devpath => "block/sda/sda1",
49 expected => "boot_disk1" ,
51 LABEL, 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 LABEL, BUS="scsi", SYSFS_vendor="?IBM-ESXS", NAME="boot_disk%n-1"
61 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS?", NAME="boot_disk%n-2"
62 LABEL, BUS="scsi", SYSFS_vendor="IBM-ES??", NAME="boot_disk%n"
63 LABEL, 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 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", NAME="boot_diskX%n"
73 LABEL, 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 LABEL, 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 LABEL, 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 REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
96 desc => "catch device by ?",
98 devpath => "class/tty/ttyUSB0",
99 expected => "visor/0" ,
101 REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
102 REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
103 REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
107 desc => "catch device by character class",
109 devpath => "class/tty/ttyUSB0",
110 expected => "visor/0" ,
112 REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
113 REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
114 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
118 desc => "replace kernel name",
120 devpath => "class/tty/ttyUSB0",
121 expected => "visor" ,
123 REPLACE, KERNEL="ttyUSB0", NAME="visor"
127 desc => "Handle comment lines in config file (and replace kernel name)",
129 devpath => "class/tty/ttyUSB0",
130 expected => "visor" ,
133 REPLACE, KERNEL="ttyUSB0", NAME="visor"
138 desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
140 devpath => "class/tty/ttyUSB0",
141 expected => "visor" ,
143 # this is a comment with whitespace before the comment
144 REPLACE, KERNEL="ttyUSB0", NAME="visor"
149 desc => "Handle empty lines in config file (and replace kernel name)",
151 devpath => "class/tty/ttyUSB0",
152 expected => "visor" ,
155 REPLACE, KERNEL="ttyUSB0", NAME="visor"
160 desc => "subdirectory handling",
162 devpath => "class/tty/ttyUSB0",
163 expected => "sub/direct/ory/visor" ,
165 REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
169 desc => "place on bus of scsi partition",
171 devpath => "block/sda/sda3",
172 expected => "first_disk3" ,
174 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
178 desc => "test NAME substitution chars",
180 devpath => "block/sda/sda3",
181 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
183 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
187 desc => "callout result substitution",
189 devpath => "block/sda/sda3",
190 expected => "special-device-3" ,
192 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
193 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
194 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
195 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
196 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
200 desc => "callout program substitution",
202 devpath => "block/sda/sda3",
203 expected => "test-0:0:0:0" ,
205 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
209 desc => "callout program substitution (numbered part of)",
211 devpath => "block/sda/sda3",
212 expected => "link1" ,
214 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", ID="node *", NAME="%1c", SYMLINK="%2c %3c"
218 desc => "invalid callout for device with no bus",
220 devpath => "class/tty/console",
223 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n foo", ID="foo", NAME="foo"
224 REPLACE, KERNEL="console", NAME="TTY"
228 desc => "valid callout for device with no bus",
230 devpath => "class/tty/console",
233 CALLOUT, PROGRAM="/bin/echo -n foo", ID="foo", NAME="foo"
234 REPLACE, KERNEL="console", NAME="TTY"
238 desc => "invalid label for device with no bus",
240 devpath => "class/tty/console",
243 LABEL, BUS="foo", SYSFS_dev="5:1", NAME="foo"
244 REPLACE, KERNEL="console", NAME="TTY"
248 desc => "valid label for device with no bus",
250 devpath => "class/tty/console",
253 LABEL, SYSFS_dev="5:1", NAME="foo"
254 REPLACE, KERNEL="console", NAME="TTY"
258 desc => "devfs disk naming substitution",
260 devpath => "block/sda",
261 expected => "lun0/disc" ,
263 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="lun0/%D"
267 desc => "devfs disk naming substitution",
269 devpath => "block/sda/sda2",
270 expected => "lun0/part2" ,
272 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="lun0/%D"
276 desc => "callout bus type",
278 devpath => "block/sda",
279 expected => "scsi-0:0:0:0" ,
281 CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
282 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
283 CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
287 desc => "symlink creation (same directory)",
289 devpath => "class/tty/ttyUSB0",
290 expected => "visor0" ,
292 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
296 desc => "symlink creation (relative link back)",
298 devpath => "block/sda/sda2",
299 expected => "1/2/a/b/symlink" ,
301 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
305 desc => "symlink creation (relative link forward)",
307 devpath => "block/sda/sda2",
308 expected => "1/2/symlink" ,
310 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
314 desc => "symlink creation (relative link back and forward)",
316 devpath => "block/sda/sda2",
317 expected => "1/2/c/d/symlink" ,
319 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
323 desc => "multiple symlinks",
325 devpath => "class/tty/ttyUSB0",
326 expected => "second-0" ,
328 REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
334 $ENV{UDEV_TEST} = "yes";
335 $ENV{SYSFS_PATH} = $sysfs;
336 $ENV{UDEV_CONFIG_FILE} = $main_conf;
340 my ($action, $subsys, $devpath, $config) = @_;
342 $ENV{DEVPATH} = $devpath;
344 # create temporary config
345 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
349 $ENV{ACTION} = $action;
350 system("$udev_bin $subsys");
355 system("rm -rf $udev_root");
356 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
360 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
362 # create initial config file
363 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
364 print CONF "udev_root=\"$udev_root\"\n";
365 print CONF "udev_db=\"$udev_db\"\n";
366 print CONF "udev_rules=\"$conf_tmp\"\n";
367 print CONF "udev_permissions=\"$perm\"\n";
370 foreach my $config (@tests) {
371 $config->{conf} =~ m/([A-Z]+)\s*,/;
374 print "TEST: $config->{desc}\n";
375 print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
377 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
378 if (-e "$PWD/$udev_root$config->{expected}") {
381 print "add: error\n";
382 system("tree $udev_root");
387 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
388 if ((-e "$PWD/$udev_root$config->{expected}") ||
389 (-l "$PWD/$udev_root$config->{expected}")) {
390 print "remove: error\n\n";
391 system("tree $udev_root");
394 print "remove: ok\n\n";
398 print "$error errors occured\n\n";
402 system("rm -rf $udev_root");