udev-test.pl revision b1e95455d2683f43f47ea94a7b79af64f0738d36
# 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
#
# Modified April 9, 2004 by Leann Ogasawara <ogasawara@osdl.org>
# - expanded @tests array to add more symlinks and permissions tests
# - some of the symlinks tests also test lack of node creation
# - added symlink_test() function
# - moved permissions and major_minor tests into their own functions
use warnings;
use strict;
my $sysfs = "sys/";
my $udev_bin = "../udev";
my $udev_db = ".udevdb";
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",
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 => "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 test",
subsys => "block",
exp_name => "node",
exp_perms => "5000::0444",
},
{
desc => "permissions ttyUSB0:root:uucp:0660",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "0:14:0660",
},
{
desc => "permissions tty0::root:0444",
subsys => "tty",
exp_name => "tty0",
exp_perms => "0:0:0444",
},
{
desc => "permissions tty1:root::0555",
subsys => "tty",
exp_name => "tty1",
exp_perms => "0:0:0555",
},
{
desc => "permissions tty2:::0777",
subsys => "tty",
exp_name => "tty2",
exp_perms => "0:0:0777",
},
{
desc => "permissions tty3::: (default mode applied)",
subsys => "tty",
exp_name => "tty3",
exp_perms => "0:0:600",
},
{
desc => "permissions i2c-300:root:sys:0744",
subsys => "i2c-dev",
exp_name => "i2c-300",
exp_perms => "0:3:0744",
},
{
desc => "permissions i2c-fake1:root:7:0007",
subsys => "i2c-dev",
exp_name => "i2c-fake1",
exp_perms => "0:7:0007",
},
{
desc => "permissions ttyS[01]:0:5:0700",
subsys => "tty",
exp_name => "ttyS1",
exp_perms => "0:5:0700",
},
{
desc => "permissions ttyS[4-9]:tty:5:0060",
subsys => "tty",
exp_name => "ttyS7",
exp_perms => "0:5:0060",
},
{
desc => "permissions tty4:0:5:0707",
subsys => "ttyS4",
exp_name => "tty4",
exp_perms => "0:5:0707",
},
{
desc => "permissions tty4?:0:5:0007",
subsys => "tty",
exp_name => "tty44",
exp_perms => "0:5:0007",
},
{
desc => "permissions tty3[!3]:::0467",
subsys => "tty",
exp_name => "tty35",
exp_perms => "0:0:0467",
},
{
desc => "permissions tty33:bad:name:0500",
subsys => "tty",
exp_name => "tty33",
exp_perms => "0:0:0500",
},
{
desc => "permissions rtc:0:users:0600",
subsys => "misc",
exp_perms => "0:100:0600",
},
{
desc => "permissions misc:0:users:0600",
subsys => "misc",
exp_perms => "0:100:0600",
},
{
desc => "permissions set OWNER=5000",
subsys => "block",
exp_name => "node",
exp_perms => "5000::0600",
},
{
desc => "permissions set GROUP=100",
subsys => "block",
exp_name => "node",
exp_perms => ":100:0600",
},
{
desc => "permissions set mode=0777",
subsys => "block",
exp_name => "node",
exp_perms => "::0777",
},
{
desc => "permissions set OWNER=5000 GROUP=100 MODE=0777",
subsys => "block",
exp_name => "node",
exp_perms => "5000:100:0777",
},
{
desc => "permissions override OWNER to 5000",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "5000:14:0660",
},
{
desc => "permissions override GROUP to 100",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => ":100:0660",
},
{
desc => "permissions override MODE to 0060",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => ":14:0060",
},
{
desc => "permissions override OWNER, GROUP, MODE",
subsys => "tty",
exp_name => "ttyUSB0",
exp_perms => "5000:100: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 empty",
subsys => "block",
exp_name => "",
exp_target => "link",
exp_error => "yes",
},
{
desc => "symlink name '.'",
subsys => "block",
exp_name => ".",
exp_target => "link",
exp_error => "yes",
},
{
desc => "symlink to empty name",
subsys => "block",
exp_name => "symlink",
exp_target => "",
exp_error => "yes",
},
{
desc => "symlink and name empty",
subsys => "block",
exp_name => "",
exp_target => "",
exp_error => "yes",
},
{
desc => "symlink node to itself",
subsys => "tty",
exp_name => "link",
exp_target => "link",
},
{
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 remove event test",
subsys => "block",
exp_name => "node",
exp_error => "yes",
},
{
desc => "ignore remove event test (with all partitions)",
subsys => "block",
exp_name => "node14",
exp_error => "yes",
option => "clear",
},
{
desc => "SUBSYSTEM match test",
subsys => "block",
exp_name => "node",
},
{
desc => "DRIVER match test",
subsys => "block",
exp_name => "node",
},
);
# 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";
print "as expected ";
} else {
$error++;
}
}
} else {
print "expected symlink from: \'$config->{exp_name}\' to \'$config->{exp_target}\'\n";
print "symlink: not created ";
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 ";
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 ";
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";
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\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);