udev-test.pl revision 35b38379bac87ebf4d0cc6884feff588ee859d5f
# udev-test
#
# Provides automated testing of the udev binary.
# The whole test is self contained in this file, except the matching sysfs tree.
# Simply extend the @tests array, to add a new test variant.
#
# Every test is driven by its own temporary config file.
# This program prepares the environment, creates the config and calls udev.
#
# udev reads the config, looks at the provided sysfs and
# first creates and then removes the device node.
# After creation and removal the result is checked against the
# expected value and the result is printed.
#
# happy testing,
# Kay Sievers <kay.sievers@vrfy.org>, 2003
use warnings;
use strict;
my $sysfs = "sys/";
my $udev_bin = "../udev";
my $udev_db = ".udev.tdb";
my $perm = "udev.permissions";
my $main_conf = "udev-test.conf";
my $conf_tmp = "udev-test.rules";
my @tests = (
{
desc => "label test of scsi disc",
subsys => "block",
expected => "boot_disk" ,
},
{
desc => "label test of scsi partition",
subsys => "block",
expected => "boot_disk1" ,
},
{
desc => "label test of pattern match",
subsys => "block",
expected => "boot_disk1" ,
},
{
desc => "label test of multiple sysfs files",
subsys => "block",
expected => "boot_disk1" ,
},
{
desc => "label test of max sysfs files",
subsys => "block",
expected => "boot_disk1" ,
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"
BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW !#", SYSFS{scsi_level}="4", SYSFS{rev}="B245", SYSFS{type}="0", NAME="boot_disk%n"
},
{
desc => "catch device by *",
subsys => "tty",
expected => "visor/0" ,
},
{
desc => "catch device by * - take 2",
subsys => "tty",
expected => "visor/0" ,
},
{
desc => "catch device by ?",
subsys => "tty",
expected => "visor/0" ,
},
{
desc => "catch device by character class",
subsys => "tty",
expected => "visor/0" ,
},
{
desc => "replace kernel name",
subsys => "tty",
expected => "visor" ,
},
{
desc => "Handle comment lines in config file (and replace kernel name)",
subsys => "tty",
expected => "visor" ,
# this is a comment
},
{
desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
subsys => "tty",
expected => "visor" ,
# this is a comment with whitespace before the comment
},
{
desc => "Handle empty lines in config file (and replace kernel name)",
subsys => "tty",
expected => "visor" ,
},
{
desc => "subdirectory handling",
subsys => "tty",
},
{
desc => "place on bus of scsi partition",
subsys => "block",
expected => "first_disk3" ,
},
{
desc => "test NAME substitution chars",
subsys => "block",
expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
},
{
desc => "test NAME substitution chars (with length limit)",
subsys => "block",
expected => "M8-m3-n3-b0:0-sIBM" ,
},
{
desc => "old style SYSFS_ attribute",
subsys => "block",
expected => "good" ,
},
{
desc => "sustitution of sysfs value (%s{file})",
subsys => "block",
expected => "disk-IBM-ESXS-sda" ,
},
{
desc => "program result substitution",
subsys => "block",
expected => "special-device-3" ,
},
{
desc => "program result substitution",
subsys => "block",
expected => "test-0:0:0:0" ,
},
{
desc => "program with escaped format char (tricky: callout returns format char!)",
subsys => "block",
expected => "escape-3" ,
},
{
desc => "program with lots of arguments",
subsys => "block",
expected => "foo9" ,
},
{
desc => "program with subshell",
subsys => "block",
expected => "bar9" ,
BUS="scsi", PROGRAM="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL="sda3", NAME="%c{7}"
},
{
desc => "program arguments combined with apostrophes",
subsys => "block",
expected => "foo7" ,
},
{
desc => "characters before the %c{N} substitution",
subsys => "block",
expected => "my-foo9" ,
BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{7}"
},
{
desc => "substitute the second to last argument",
subsys => "block",
expected => "my-foo8" ,
BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}"
},
{
desc => "program result substitution (numbered part of)",
subsys => "block",
expected => "link1" ,
BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
},
{
desc => "invalid program for device with no bus",
subsys => "tty",
expected => "TTY" ,
},
{
desc => "valid program for device with no bus",
subsys => "tty",
expected => "foo" ,
},
{
desc => "invalid label for device with no bus",
subsys => "tty",
expected => "TTY" ,
},
{
desc => "valid label for device with no bus",
subsys => "tty",
expected => "foo" ,
},
{
desc => "program and bus type match",
subsys => "block",
expected => "scsi-0:0:0:0" ,
},
{
desc => "symlink creation (same directory)",
subsys => "tty",
expected => "visor0" ,
},
{
desc => "symlink creation (relative link back)",
subsys => "block",
},
{
desc => "symlink creation (relative link forward)",
subsys => "block",
expected => "1/2/symlink" ,
},
{
desc => "symlink creation (relative link back and forward)",
subsys => "block",
},
{
desc => "multiple symlinks",
subsys => "tty",
expected => "second-0" ,
},
{
desc => "create all possible partitions",
subsys => "block",
expected => "boot_disk15" ,
},
{
desc => "sysfs parent hierarchy",
subsys => "tty",
expected => "visor" ,
},
{
desc => "name test with ! in the name",
subsys => "block",
},
{
desc => "name test with ! in the name, but no matching rule",
subsys => "block",
},
{
desc => "ID rule",
subsys => "block",
expected => "scsi-0:0:0:0",
},
{
desc => "ID wildcard all",
subsys => "block",
expected => "scsi-0:0:0:0",
},
{
desc => "ID wildcard partial",
subsys => "block",
expected => "scsi-0:0:0:0",
},
{
desc => "ID wildcard partial 2",
subsys => "block",
expected => "scsi-0:0:0:0",
},
{
desc => "ignore SYSFS attribute whitespace",
subsys => "block",
expected => "ignored",
},
{
desc => "do not ignore SYSFS attribute whitespace",
subsys => "block",
expected => "matched-with-space",
},
);
# set env
sub udev {
# create temporary config
close CONF;
system("$udev_bin $subsys");
}
my $error = 0;
sub run_test {
print "TEST $number: $config->{desc}\n";
print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
if (-e "$PWD/$udev_root$config->{expected}") {
print "add: ok ";
} else {
print "add: error\n";
system("tree $udev_root");
print "\n";
$error++;
}
if ((-e "$PWD/$udev_root$config->{expected}") ||
(-l "$PWD/$udev_root$config->{expected}")) {
print "remove: error\n\n";
system("tree $udev_root");
$error++;
} else {
print "remove: ok\n\n";
}
}
# prepare
system("rm -rf $udev_root");
mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
# create initial config file
print CONF "udev_root=\"$udev_root\"\n";
print CONF "udev_db=\"$udev_db\"\n";
print CONF "udev_rules=\"$conf_tmp\"\n";
print CONF "udev_permissions=\"$perm\"\n";
close CONF;
my $test_num = 1;
if ($ARGV[0]) {
# only run one test
print "udev-test will run test number $test_num only\n";
} else {
# test all
print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
$test_num++;
}
}
print "$error errors occured\n\n";
# cleanup
unlink($udev_db);
system("rm -rf $udev_root");
unlink($conf_tmp);
unlink($main_conf);