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 => "catch device by *",
69 devpath => "class/tty/ttyUSB0",
70 expected => "visor/0" ,
72 REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
76 desc => "catch device by ?",
78 devpath => "class/tty/ttyUSB0",
79 expected => "visor/0" ,
81 REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
82 REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
83 REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
87 desc => "catch device by character class",
89 devpath => "class/tty/ttyUSB0",
90 expected => "visor/0" ,
92 REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
93 REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
94 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
98 desc => "replace kernel name",
100 devpath => "class/tty/ttyUSB0",
101 expected => "visor" ,
103 REPLACE, KERNEL="ttyUSB0", NAME="visor"
107 desc => "Handle comment lines in config file (and replace kernel name)",
109 devpath => "class/tty/ttyUSB0",
110 expected => "visor" ,
113 REPLACE, KERNEL="ttyUSB0", NAME="visor"
118 desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
120 devpath => "class/tty/ttyUSB0",
121 expected => "visor" ,
123 # this is a comment with whitespace before the comment
124 REPLACE, KERNEL="ttyUSB0", NAME="visor"
129 desc => "Handle empty lines in config file (and replace kernel name)",
131 devpath => "class/tty/ttyUSB0",
132 expected => "visor" ,
135 REPLACE, KERNEL="ttyUSB0", NAME="visor"
140 desc => "subdirectory handling",
142 devpath => "class/tty/ttyUSB0",
143 expected => "sub/direct/ory/visor" ,
145 REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
149 desc => "place on bus of scsi partition",
151 devpath => "block/sda/sda3",
152 expected => "first_disk3" ,
154 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
158 desc => "test NAME substitution chars",
160 devpath => "block/sda/sda3",
161 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
163 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
167 desc => "callout result substitution",
169 devpath => "block/sda/sda3",
170 expected => "special-device-3" ,
172 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
173 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
174 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
175 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
176 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
180 desc => "callout program substitution",
182 devpath => "block/sda/sda3",
183 expected => "test-0:0:0:0" ,
185 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
189 desc => "callout program substitution (numbered part of)",
191 devpath => "block/sda/sda3",
192 expected => "link1" ,
194 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", ID="node *", NAME="%1c", SYMLINK="%2c %3c"
198 desc => "devfs disk naming substitution",
200 devpath => "block/sda",
201 expected => "lun0/disc" ,
203 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="lun0/%D"
207 desc => "devfs disk naming substitution",
209 devpath => "block/sda/sda2",
210 expected => "lun0/part2" ,
212 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="lun0/%D"
216 desc => "callout bus type",
218 devpath => "block/sda",
219 expected => "scsi-0:0:0:0" ,
221 CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
222 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
223 CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
227 desc => "symlink creation (same directory)",
229 devpath => "class/tty/ttyUSB0",
230 expected => "visor0" ,
232 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
236 desc => "symlink creation (relative link back)",
238 devpath => "block/sda/sda2",
239 expected => "1/2/a/b/symlink" ,
241 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
245 desc => "symlink creation (relative link forward)",
247 devpath => "block/sda/sda2",
248 expected => "1/2/symlink" ,
250 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
254 desc => "symlink creation (relative link back and forward)",
256 devpath => "block/sda/sda2",
257 expected => "1/2/c/d/symlink" ,
259 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
263 desc => "multiple symlinks",
265 devpath => "class/tty/ttyUSB0",
266 expected => "second-0" ,
268 REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
274 $ENV{UDEV_TEST} = "yes";
275 $ENV{SYSFS_PATH} = $sysfs;
276 $ENV{UDEV_CONFIG_FILE} = $main_conf;
280 my ($action, $subsys, $devpath, $config) = @_;
282 $ENV{DEVPATH} = $devpath;
284 # create temporary config
285 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
289 $ENV{ACTION} = $action;
290 system("$udev_bin $subsys");
295 system("rm -rf $udev_root");
296 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
300 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
302 # create initial config file
303 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
304 print CONF "udev_root=\"$udev_root\"\n";
305 print CONF "udev_db=\"$udev_db\"\n";
306 print CONF "udev_rules=\"$conf_tmp\"\n";
307 print CONF "udev_permissions=\"$perm\"\n";
310 foreach my $config (@tests) {
311 $config->{conf} =~ m/^([A-Z]*).*/;
314 print "TEST: $config->{desc}\n";
315 print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
317 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
318 if (-e "$PWD/$udev_root$config->{expected}") {
321 print "add: error\n";
322 system("tree $udev_root");
327 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
328 if ((-e "$PWD/$udev_root$config->{expected}") ||
329 (-l "$PWD/$udev_root$config->{expected}")) {
330 print "remove: error\n\n";
331 system("tree $udev_root");
334 print "remove: ok\n\n";
338 print "$error errors occured\n\n";
342 system("rm -rf $udev_root");