udev-test.pl revision 77313cd0d13ed3682f25c745f71f648292b28215
# 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.
#
# Kay Sievers <kay.sievers@vrfy.org>, 2003
# Leann Ogasawara <ogasawara@osdl.org>, 2004
use warnings;
use strict;
my $sysfs = "sys/";
my $udev_bin = "../udev";
my $udev_db = ".udevdb";
my $main_conf = "udev-test.conf";
my $conf_tmp = "udev-test.rules";
# uncomment following line to run udev with valgrind.
# Should make this a runtime option to the script someday...
#my $udev_bin = "valgrind --tool=memcheck --leak-check=yes ../udev";
my @tests = (
{
desc => "label test of scsi disc",
subsys => "block",
exp_name => "boot_disk" ,
},
{
desc => "label test of scsi partition",
subsys => "block",
exp_name => "boot_disk1" ,
},
{
desc => "label test of pattern match",
subsys => "block",
exp_name => "boot_disk1" ,
},
{
desc => "label test of multiple sysfs files",
subsys => "block",
exp_name => "boot_disk1" ,
},
{
desc => "label test of max sysfs files",
subsys => "block",
exp_name => "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",
exp_name => "visor/0" ,
},
{
desc => "catch device by * - take 2",
subsys => "tty",
exp_name => "visor/0" ,
},
{
desc => "catch device by ?",
subsys => "tty",
exp_name => "visor/0" ,
},
{
desc => "catch device by character class",
subsys => "tty",
exp_name => "visor/0" ,
},
{
desc => "replace kernel name",
subsys => "tty",
exp_name => "visor" ,
},
{
desc => "Handle comment lines in config file (and replace kernel name)",
subsys => "tty",
exp_name => "visor" ,
# this is a comment
},
{
desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
subsys => "tty",
exp_name => "visor" ,
# this is a comment with whitespace before the comment
},
{
desc => "Handle whitespace only lines (and replace kernel name)",
subsys => "tty",
exp_name => "whitespace" ,
# this is a comment with whitespace before the comment
},
{
desc => "Handle empty lines in config file (and replace kernel name)",
subsys => "tty",
exp_name => "visor" ,
},
{
desc => "Handle backslashed multi lines in config file (and replace kernel name)",
subsys => "tty",
exp_name => "visor" ,
KERNEL="ttyUSB0", \\
NAME="visor"
},
{
desc => "preserve backslashes, if they are not for a newline",
subsys => "tty",
exp_name => "aaa",
},
{
desc => "Handle stupid backslashed multi lines in config file (and replace kernel name)",
subsys => "tty",
exp_name => "visor" ,
#
\\
\\\\
#\\
KERNEL="ttyUSB0", \\
NAME="visor"
},
{
desc => "subdirectory handling",
subsys => "tty",
},
{
desc => "place on bus of scsi partition",
subsys => "block",
exp_name => "first_disk3" ,
},
{
desc => "test NAME substitution chars",
subsys => "block",
exp_name => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
},
{
desc => "test NAME substitution chars (with length limit)",
subsys => "block",
exp_name => "M8-m3-n3-b0:0-sIBM" ,
},
{
desc => "old style SYSFS_ attribute",
subsys => "block",
exp_name => "good" ,
},
{
desc => "sustitution of sysfs value (%s{file})",
subsys => "block",
exp_name => "disk-IBM-ESXS-sda" ,
},
{
desc => "program result substitution",
subsys => "block",
exp_name => "special-device-3" ,
},
{
desc => "program result substitution (no argument should be subsystem)",
subsys => "block",
exp_name => "subsys_block" ,
},
{
desc => "program result substitution (newline removal)",
subsys => "block",
exp_name => "newline_removed" ,
},
{
desc => "program result substitution",
subsys => "block",
exp_name => "test-0:0:0:0" ,
},
{
desc => "program with escaped format char (tricky: callout returns format char!)",
subsys => "block",
exp_name => "escape-3" ,
},
{
desc => "program with lots of arguments",
subsys => "block",
exp_name => "foo9" ,
},
{
desc => "program with subshell",
subsys => "block",
exp_name => "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",
exp_name => "foo7" ,
},
{
desc => "characters before the %c{N} substitution",
subsys => "block",
exp_name => "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",
exp_name => "my-foo8" ,
BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}"
},
{
desc => "invalid program for device with no bus",
subsys => "tty",
exp_name => "TTY" ,
},
{
desc => "valid program for device with no bus",
subsys => "tty",
exp_name => "foo" ,
},
{
desc => "invalid label for device with no bus",
subsys => "tty",
exp_name => "TTY" ,
},
{
desc => "valid label for device with no bus",
subsys => "tty",
exp_name => "foo" ,
},
{
desc => "program and bus type match",
subsys => "block",
exp_name => "scsi-0:0:0:0" ,
},
{
desc => "create all possible partitions",
subsys => "block",
exp_name => "boot_disk15" ,
},
{
desc => "sysfs parent hierarchy",
subsys => "tty",
exp_name => "visor" ,
},
{
desc => "name test with ! in the name",
subsys => "block",
},
{
desc => "name test with ! in the name, but no matching rule",
subsys => "block",
},
{
desc => "name test with ! in the name for a partition",
subsys => "block",
},
{
desc => "ID rule",
subsys => "block",
exp_name => "scsi-0:0:0:0",
},
{
desc => "ID wildcard all",
subsys => "block",
exp_name => "scsi-0:0:0:0",
},
{
desc => "ID wildcard partial",
subsys => "block",
exp_name => "scsi-0:0:0:0",
},
{
desc => "ID wildcard partial 2",
subsys => "block",
exp_name => "scsi-0:0:0:0",
},
{
desc => "ignore SYSFS attribute whitespace",
subsys => "block",
exp_name => "ignored",
},
{
desc => "do not ignore SYSFS attribute whitespace",
subsys => "block",
exp_name => "matched-with-space",
},
{
desc => "permissions USER=bad GROUP=name",
subsys => "tty",
exp_name => "tty33",
exp_perms => "0:0:0660",
},
{
desc => "permissions OWNER=5000",
subsys => "block",
exp_name => "node",
exp_perms => "5000::0660",
},
{
desc => "permissions GROUP=100",
subsys => "block",
exp_name => "node",
exp_perms => ":100:0660",
},
{
desc => "permissions MODE=0777",
subsys => "block",
exp_name => "node",
exp_perms => "::0777",
},
{
desc => "permissions OWNER=5000 GROUP=100 MODE=0777",
subsys => "block",
exp_name => "node",
exp_perms => "5000:100:0777",
},
{
desc => "permissions OWNER to 5000",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "5000::",
},
{
desc => "permissions GROUP to 100",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => ":100:0660",
},
{
desc => "permissions MODE to 0060",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "::0060",
},
{
desc => "permissions OWNER, GROUP, MODE",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "5000:100:0777",
},
{
desc => "permissions only rule",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "5000:100:0777",
},
{
desc => "multiple permissions only rule",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "3000:4000:0777",
},
{
desc => "permissions only rule with override at NAME rule",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "3000:8000:0777",
},
{
subsys => "block",
exp_name => "node",
exp_majorminor => "8:0",
},
{
desc => "big minor number test",
subsys => "i2c-dev",
exp_name => "node",
exp_majorminor => "89:300",
},
{
desc => "big major number test",
subsys => "i2c-dev",
exp_name => "node",
exp_majorminor => "4095:1",
},
{
desc => "big major and big minor number test",
subsys => "i2c-dev",
exp_name => "node",
exp_majorminor => "4094:89999",
},
{
desc => "multiple symlinks with format char",
subsys => "tty",
exp_name => "symlink2-ttyUSB0",
exp_target => "ttyUSB0",
},
{
desc => "symlink creation (same directory)",
subsys => "tty",
exp_name => "visor0",
exp_target => "ttyUSB0",
},
{
desc => "symlink creation (relative link forward)",
subsys => "block",
exp_name => "1/2/symlink" ,
exp_target => "a/b/node",
},
{
desc => "symlink creation (relative link back and forward)",
subsys => "block",
exp_target => "../../a/b/node",
},
{
desc => "multiple symlinks",
subsys => "tty",
exp_name => "second-0" ,
exp_target => "visor" ,
},
{
desc => "symlink only rule",
subsys => "block",
exp_name => "symlink-only2",
exp_target => "link",
},
{
desc => "symlink name '.'",
subsys => "block",
exp_name => ".",
exp_target => "link",
exp_add_error => "yes",
exp_rem_error => "yes",
},
{
desc => "symlink to empty name",
subsys => "block",
exp_name => "symlink",
exp_target => "",
exp_add_error => "yes",
},
{
desc => "symlink and name empty",
subsys => "block",
exp_name => "",
exp_target => "",
exp_add_error => "yes",
exp_rem_error => "yes",
},
{
desc => "symlink node to itself",
subsys => "tty",
exp_name => "link",
exp_target => "link",
exp_rem_error => "yes",
option => "clear",
},
{
desc => "symlink %n substitution",
subsys => "tty",
exp_name => "symlink0",
exp_target => "ttyUSB0",
},
{
desc => "symlink %k substitution",
subsys => "tty",
exp_name => "symlink-ttyUSB0",
exp_target => "ttyUSB0",
},
{
desc => "symlink %M:%m substitution",
subsys => "tty",
exp_name => "major-188:0",
exp_target => "ttyUSB0",
},
{
desc => "symlink %b substitution",
subsys => "block",
exp_name => "symlink-0:0:0:0",
exp_target => "node",
},
{
desc => "symlink %c substitution",
subsys => "tty",
exp_name => "test",
exp_target => "ttyUSB0",
},
{
desc => "symlink %c{N} substitution",
subsys => "tty",
exp_name => "test",
exp_target => "ttyUSB0",
},
{
desc => "symlink %c{N+} substitution",
subsys => "tty",
exp_name => "this",
exp_target => "ttyUSB0",
},
{
desc => "symlink only rule with %c{N+}",
subsys => "block",
exp_name => "test",
exp_target => "link",
},
{
desc => "symlink %s{filename} substitution",
subsys => "tty",
exp_name => "188:0",
exp_target => "ttyUSB0",
},
{
desc => "symlink %Ns{filename} substitution",
subsys => "tty",
exp_name => "188",
exp_target => "ttyUSB0",
},
{
desc => "symlink with '%' in name",
subsys => "tty",
exp_name => "percent%sign",
exp_target => "ttyUSB0",
},
{
desc => "symlink with '%' in name",
subsys => "tty",
exp_name => "%ttyUSB0_name",
exp_target => "ttyUSB0",
},
{
desc => "program result substitution (numbered part of)",
subsys => "block",
exp_name => "link1",
exp_target => "node",
BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
},
{
desc => "program result substitution (numbered part of+)",
subsys => "block",
exp_name => "link4",
exp_target => "node",
BUS="scsi", PROGRAM="/bin/echo -n node link1 link2 link3 link4", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2+}"
},
{
desc => "enumeration char test (single test)",
subsys => "block",
exp_name => "cdrom",
},
{
desc => "enumeration char test sequence 1/5 (keep)",
subsys => "block",
exp_name => "cdrom",
option => "keep",
},
{
desc => "enumeration char test sequence 2/5 (keep)",
subsys => "block",
exp_name => "enum",
option => "keep",
},
{
desc => "enumeration char test sequence 3/5 (keep)",
subsys => "block",
exp_name => "cdrom1",
option => "keep",
},
{
desc => "enumeration char test sequence 4/5 (keep)",
subsys => "block",
exp_name => "enum1",
option => "keep",
},
{
desc => "enumeration char test sequence 5/5 (clean)",
subsys => "block",
exp_name => "cdrom2",
option => "clear",
},
{
desc => "enumeration char test after cleanup (single test)",
subsys => "block",
exp_name => "cdrom",
},
{
desc => "ignore rule test",
subsys => "block",
exp_name => "node",
exp_add_error => "yes",
},
{
desc => "all_partitions, option-only rule",
subsys => "block",
exp_name => "node6",
},
{
desc => "all_partitions, option-only rule (fail on partition)",
subsys => "block",
exp_name => "node6",
exp_add_error => "yes",
},
{
desc => "ignore remove event test",
subsys => "block",
exp_name => "node",
exp_rem_error => "yes",
},
{
desc => "ignore remove event test (with all partitions)",
subsys => "block",
exp_name => "node14",
exp_rem_error => "yes",
option => "clear",
},
{
desc => "SUBSYSTEM match test",
subsys => "block",
exp_name => "node",
},
{
desc => "DRIVER match test",
subsys => "block",
exp_name => "node",
},
{
desc => "temporary node creation test",
subsys => "block",
exp_name => "node",
},
{
desc => "devpath substitution test",
subsys => "block",
exp_name => "sda",
},
{
desc => "parent node name substitution test sequence 1/2 (keep)",
subsys => "block",
exp_name => "main_device",
option => "keep",
},
{
desc => "parent node name substitution test sequence 2/2 (clean)",
subsys => "block",
exp_name => "main_device-part-1",
option => "clean",
},
{
desc => "udev_root substitution",
subsys => "block",
exp_name => "start-udev-root-end",
option => "clean",
},
);
# set env
sub udev {
# create temporary config
close CONF;
system("$udev_bin $subsys");
}
my $error = 0;
sub permissions_test {
my $wrong = 0;
if ($1 ne "") {
}
if ($2 ne "") {
}
if ($3 ne "") {
}
if ($wrong == 0) {
print "permissions: ok ";
} else {
printf "expected permissions are: %i:%i:%#o\n", $1, $2, oct($3);
$error++;
}
}
sub major_minor_test {
my $wrong = 0;
$config->{exp_majorminor} =~ m/^(.*):(.*)$/;
if ($1 ne "") {
}
if ($2 ne "") {
}
if ($wrong == 0) {
print "major:minor: ok ";
} else {
printf "expected major:minor is: %i:%i\n", $1, $2;
print "major:minor: error ";
$error++;
}
}
sub symlink_test {
my ($config) = @_;
if ($output =~ m/(.*)-> (.*)/) {
print "symlink: ok ";
} else {
print "expected symlink from: \'$config->{exp_name}\' to \'$config->{exp_target}\'\n";
print "created symlink from: \'$config->{exp_name}\' to \'$2\'\n";
if ($config->{exp_add_error}) {
print "as expected ";
} else {
$error++;
}
}
} else {
print "expected symlink from: \'$config->{exp_name}\' to \'$config->{exp_target}\'\n";
print "symlink: not created ";
if ($config->{exp_add_error}) {
print "as expected ";
} else {
$error++;
}
}
}
sub run_test {
print "TEST $number: $config->{desc}\n";
if ($config->{exp_target}) {
print "device \'$config->{devpath}\' expecting symlink '$config->{exp_name}' to node \'$config->{exp_target}\'\n";
} else {
print "device \'$config->{devpath}\' expecting node \'$config->{exp_name}\'\n";
}
if ((-e "$PWD/$udev_root$config->{exp_name}") ||
(-l "$PWD/$udev_root$config->{exp_name}")) {
}
if (defined($config->{exp_majorminor})) {
}
if (defined($config->{exp_target})) {
}
print "add: ok ";
} else {
print "add: error ";
if ($config->{exp_add_error}) {
print "as expected ";
} else {
print "\n\n";
system("tree $udev_root");
print "\n";
$error++;
}
}
print "\n\n";
return;
}
if ((-e "$PWD/$udev_root$config->{exp_name}") ||
(-l "$PWD/$udev_root$config->{exp_name}")) {
print "remove: error ";
if ($config->{exp_rem_error}) {
print "as expected\n\n";
} else {
print "\n\n";
system("tree $udev_root");
print "\n";
$error++;
}
} else {
print "remove: ok\n\n";
}
system("rm -rf $udev_db");
system("rm -rf $udev_root");
mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
}
}
# only run if we have root permissions
# due to mknod restrictions
if (!($<==0)) {
print "Must have root permissions to run properly.\n";
exit;
}
# 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";
close CONF;
my $test_num = 1;
if ($ARGV[0]) {
# only run one test
print "udev-test will run test number $test_num only:\n\n";
} else {
print "test does not exist.\n";
}
} else {
# test all
print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
$test_num++;
}
}
print "$error errors occured\n\n";
# cleanup
system("rm -rf $udev_db");
system("rm -rf $udev_root");
unlink($conf_tmp);
unlink($main_conf);