udev-test.pl revision 0f52fdee378796df26021208dc684f77be1f877f
# 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 parses the rules, 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.
#
# Copyright (C) 2004-2011 Kay Sievers <kay.sievers@vrfy.org>
# Copyright (C) 2004 Leann Ogasawara <ogasawara@osdl.org>
use warnings;
use strict;
my $valgrind = 0;
my $udev_bin_valgrind = "valgrind --tool=memcheck --leak-check=yes --quiet $udev_bin";
my $udev_root = "udev-root";
my $udev_conf = "udev-test.conf";
my $udev_rules = "udev-test.rules";
my @tests = (
{
desc => "no rules",
exp_name => "sda" ,
exp_rem_error => "yes",
#
},
{
desc => "label test of scsi disc",
exp_name => "boot_disk" ,
},
{
desc => "label test of scsi disc",
exp_name => "boot_disk" ,
},
{
desc => "label test of scsi disc",
exp_name => "boot_disk" ,
},
{
desc => "label test of scsi partition",
exp_name => "boot_disk1" ,
},
{
desc => "label test of pattern match",
exp_name => "boot_disk1" ,
},
{
desc => "label test of multiple sysfs files",
exp_name => "boot_disk1" ,
},
{
desc => "label test of max sysfs files (skip invalid rule)",
exp_name => "boot_disk1" ,
SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", ATTRS{queue_depth}=="32", SYMLINK+="boot_diskXX%n"
SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", SYMLINK+="boot_disk%n"
},
{
desc => "catch device by *",
exp_name => "modem/0" ,
},
{
desc => "catch device by * - take 2",
exp_name => "modem/0" ,
},
{
desc => "catch device by ?",
exp_name => "modem/0" ,
},
{
desc => "catch device by character class",
exp_name => "modem/0" ,
},
{
desc => "replace kernel name",
exp_name => "modem" ,
},
{
desc => "Handle comment lines in config file (and replace kernel name)",
exp_name => "modem" ,
# this is a comment
},
{
desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
exp_name => "modem" ,
# this is a comment with whitespace before the comment
},
{
desc => "Handle whitespace only lines (and replace kernel name)",
exp_name => "whitespace" ,
# this is a comment with whitespace before the comment
},
{
desc => "Handle empty lines in config file (and replace kernel name)",
exp_name => "modem" ,
},
{
desc => "Handle backslashed multi lines in config file (and replace kernel name)",
exp_name => "modem" ,
KERNEL=="ttyACM0", \\
SYMLINK+="modem"
},
{
desc => "preserve backslashes, if they are not for a newline",
exp_name => "aaa",
},
{
desc => "Handle stupid backslashed multi lines in config file (and replace kernel name)",
exp_name => "modem" ,
#
\\
\\
#\\
KERNEL=="ttyACM0", \\
SYMLINK+="modem"
},
{
desc => "subdirectory handling",
},
{
desc => "parent device name match of scsi partition",
exp_name => "first_disk5" ,
},
{
desc => "test substitution chars",
exp_name => "Major:8:minor:5:kernelnumber:5:id:0:0:0:0" ,
},
{
desc => "import of shell-value file",
},
{
desc => "import of shell-value returned from program",
exp_name => "node12345678",
SUBSYSTEMS=="scsi", IMPORT{program}="/bin/echo -e \' TEST_KEY=12345678\\n TEST_key2=98765\'", SYMLINK+="node\$env{TEST_KEY}"
},
{
desc => "sustitution of sysfs value (%s{file})",
exp_name => "disk-ATA-sda" ,
},
{
desc => "program result substitution",
exp_name => "special-device-5" ,
not_exp_name => "not" ,
},
{
desc => "program result substitution (newline removal)",
exp_name => "newline_removed" ,
},
{
desc => "program result substitution",
exp_name => "test-0:0:0:0" ,
},
{
desc => "program with lots of arguments",
exp_name => "foo9" ,
SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="%c{7}"
},
{
desc => "program with subshell",
exp_name => "bar9" ,
SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL=="sda5", SYMLINK+="%c{7}"
},
{
desc => "program arguments combined with apostrophes",
exp_name => "foo7" ,
SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'", KERNEL=="sda5", SYMLINK+="%c{5}"
},
{
desc => "characters before the %c{N} substitution",
exp_name => "my-foo9" ,
SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{7}"
},
{
desc => "substitute the second to last argument",
exp_name => "my-foo8" ,
SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{6}"
},
{
desc => "test substitution by variable name",
exp_name => "Major:8-minor:5-kernelnumber:5-id:0:0:0:0",
SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:\$major-minor:\$minor-kernelnumber:\$number-id:\$id"
},
{
desc => "test substitution by variable name 2",
exp_name => "Major:8-minor:5-kernelnumber:5-id:0:0:0:0",
SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="Major:\$major-minor:%m-kernelnumber:\$number-id:\$id"
},
{
desc => "test substitution by variable name 3",
exp_name => "850:0:0:05" ,
},
{
desc => "test substitution by variable name 4",
exp_name => "855" ,
},
{
desc => "test substitution by variable name 5",
exp_name => "8550:0:0:0" ,
},
{
desc => "non matching SUBSYSTEMS for device with no parent",
exp_name => "TTY",
},
{
desc => "non matching SUBSYSTEMS",
exp_name => "TTY" ,
},
{
desc => "ATTRS match",
exp_name => "foo" ,
},
{
desc => "ATTR (empty file)",
exp_name => "empty" ,
},
{
desc => "ATTR (non-existent file)",
exp_name => "non-existent" ,
},
{
desc => "program and bus type match",
exp_name => "scsi-0:0:0:0" ,
},
{
desc => "sysfs parent hierarchy",
exp_name => "modem" ,
},
{
desc => "name test with ! in the name",
},
{
desc => "name test with ! in the name, but no matching rule",
exp_rem_error => "yes",
},
{
desc => "KERNELS rule",
exp_name => "scsi-0:0:0:0",
},
{
desc => "KERNELS wildcard all",
exp_name => "scsi-0:0:0:0",
},
{
desc => "KERNELS wildcard partial",
exp_name => "scsi-0:0:0:0",
},
{
desc => "KERNELS wildcard partial 2",
exp_name => "scsi-0:0:0:0",
},
{
desc => "substitute attr with link target value (first match)",
exp_name => "driver-is-sd",
},
{
desc => "substitute attr with link target value (currently selected device)",
exp_name => "driver-is-ahci",
},
{
desc => "ignore ATTRS attribute whitespace",
exp_name => "ignored",
},
{
desc => "do not ignore ATTRS attribute whitespace",
exp_name => "matched-with-space",
},
{
desc => "permissions USER=bad GROUP=name",
exp_name => "tty33",
exp_perms => "0:0:0600",
},
{
desc => "permissions OWNER=5000",
exp_name => "node",
exp_perms => "5000::0600",
},
{
desc => "permissions GROUP=100",
exp_name => "node",
exp_perms => ":100:0660",
},
{
desc => "textual user id",
exp_name => "node",
exp_perms => "nobody::0600",
},
{
desc => "textual group id",
exp_name => "node",
exp_perms => ":daemon:0660",
},
{
exp_name => "node",
exp_perms => "root:mail:0660",
},
{
desc => "permissions MODE=0777",
exp_name => "node",
exp_perms => "::0777",
},
{
desc => "permissions OWNER=5000 GROUP=100 MODE=0777",
exp_name => "node",
exp_perms => "5000:100:0777",
},
{
desc => "permissions OWNER to 5000",
exp_name => "ttyACM0",
exp_perms => "5000::",
},
{
desc => "permissions GROUP to 100",
exp_name => "ttyACM0",
exp_perms => ":100:0660",
},
{
desc => "permissions MODE to 0060",
exp_name => "ttyACM0",
exp_perms => "::0060",
},
{
desc => "permissions OWNER, GROUP, MODE",
exp_name => "ttyACM0",
exp_perms => "5000:100:0777",
},
{
desc => "permissions only rule",
exp_name => "ttyACM0",
exp_perms => "5000:100:0777",
},
{
desc => "multiple permissions only rule",
exp_name => "ttyACM0",
exp_perms => "3000:4000:0777",
},
{
desc => "permissions only rule with override at SYMLINK+ rule",
exp_name => "ttyACM0",
exp_perms => "3000:8000:0777",
},
{
exp_name => "node",
exp_majorminor => "8:0",
},
{
desc => "big major number test",
devpath => "/devices/virtual/misc/misc-fake1",
exp_name => "node",
exp_majorminor => "4095:1",
},
{
desc => "big major and big minor number test",
devpath => "/devices/virtual/misc/misc-fake89999",
exp_name => "node",
exp_majorminor => "4095:89999",
},
{
desc => "multiple symlinks with format char",
exp_name => "symlink2-ttyACM0",
},
{
desc => "multiple symlinks with a lot of s p a c e s",
exp_name => "one",
not_exp_name => " ",
},
{
desc => "symlink creation (same directory)",
exp_name => "modem0",
},
{
desc => "multiple symlinks",
exp_name => "second-0" ,
},
{
desc => "symlink name '.'",
exp_name => ".",
exp_add_error => "yes",
exp_rem_error => "yes",
},
{
desc => "symlink node to itself",
exp_name => "link",
exp_add_error => "yes",
exp_rem_error => "yes",
option => "clean",
},
{
desc => "symlink %n substitution",
exp_name => "symlink0",
},
{
desc => "symlink %k substitution",
exp_name => "symlink-ttyACM0",
},
{
desc => "symlink %M:%m substitution",
exp_name => "major-166:0",
},
{
desc => "symlink %b substitution",
exp_name => "symlink-0:0:0:0",
},
{
desc => "symlink %c substitution",
exp_name => "test",
},
{
desc => "symlink %c{N} substitution",
exp_name => "test",
},
{
desc => "symlink %c{N+} substitution",
exp_name => "this",
},
{
desc => "symlink only rule with %c{N+}",
exp_name => "test",
},
{
desc => "symlink %s{filename} substitution",
exp_name => "166:0",
},
{
desc => "program result substitution (numbered part of)",
exp_name => "link1",
SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
},
{
desc => "program result substitution (numbered part of+)",
exp_name => "link4",
SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
},
{
desc => "SUBSYSTEM match test",
exp_name => "node",
},
{
desc => "DRIVERS match test",
exp_name => "node",
},
{
desc => "devnode substitution test",
exp_name => "node",
},
{
desc => "parent node name substitution test",
exp_name => "sda-part-1",
},
{
desc => "udev_root substitution",
exp_name => "start-udev-root-end",
},
{
desc => "last_rule option",
exp_name => "last",
},
{
desc => "negation KERNEL!=",
exp_name => "match",
},
{
desc => "negation SUBSYSTEM!=",
exp_name => "not-anything",
},
{
desc => "negation PROGRAM!= exit code",
exp_name => "nonzero-program",
},
{
desc => "test for whitespace between the operator",
exp_name => "true",
},
{
desc => "ENV{} test",
exp_name => "true",
},
{
desc => "ENV{} test",
exp_name => "true",
SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
},
{
desc => "ENV{} test (assign)",
exp_name => "true",
},
{
desc => "ENV{} test (assign 2 times)",
exp_name => "true",
},
{
desc => "ENV{} test (assign2)",
exp_name => "part",
},
{
desc => "untrusted string sanitize",
exp_name => "sane",
SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
},
{
desc => "untrusted string sanitize (don't replace utf8)",
exp_name => "uber",
SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
},
{
desc => "untrusted string sanitize (replace invalid utf8)",
exp_name => "replaced",
SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
},
{
desc => "read sysfs value from parent device",
exp_name => "serial-354172020305000",
},
{
desc => "match against empty key string",
exp_name => "ok",
},
{
desc => "check ACTION value",
exp_name => "ok",
},
{
desc => "final assignment",
exp_name => "ok",
exp_perms => "root:tty:0640",
},
{
desc => "final assignment 2",
exp_name => "ok",
exp_perms => "root:tty:0640",
},
{
desc => "env substitution",
exp_name => "node-add-me",
},
{
desc => "reset list to current value",
exp_name => "three",
not_exp_name => "two",
},
{
desc => "test empty SYMLINK+ (empty override)",
exp_name => "right",
not_exp_name => "wrong",
},
{
desc => "test multi matches",
exp_name => "right",
},
{
desc => "test multi matches 2",
exp_name => "right",
},
{
desc => "test multi matches 3",
exp_name => "right",
},
{
desc => "test multi matches 4",
exp_name => "right",
},
{
desc => "IMPORT parent test sequence 1/2 (keep)",
exp_name => "parent",
option => "keep",
KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
},
{
desc => "IMPORT parent test sequence 2/2 (keep)",
exp_name => "parentenv-parent_right",
option => "clean",
KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
},
{
desc => "GOTO test",
exp_name => "right",
LABEL="end"
},
{
desc => "GOTO label does not exist",
exp_name => "right",
LABEL="exists"
},
{
desc => "SYMLINK+ compare test",
exp_name => "right",
not_exp_name => "wrong",
},
{
desc => "invalid key operation",
exp_name => "yes",
},
{
desc => "operator chars in attribute",
exp_name => "yes",
},
{
desc => "overlong comment line",
exp_name => "yes",
# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
},
{
exp_name => "00:16:41:e2:8d:ff",
},
{
desc => "TEST absolute path",
exp_name => "there",
},
{
exp_name => "yes",
},
{
desc => "TEST relative path",
exp_name => "relative",
},
{
desc => "TEST wildcard substitution (find queue/nr_requests)",
exp_name => "found-subdir",
},
{
desc => "TEST MODE=0000",
exp_name => "sda",
exp_perms => "0:0:0000",
exp_rem_error => "yes",
},
{
desc => "TEST PROGRAM feeds OWNER, GROUP, MODE",
exp_name => "sda",
exp_perms => "5000:100:0400",
exp_rem_error => "yes",
},
{
desc => "TEST PROGRAM feeds MODE with overflow",
exp_name => "sda",
exp_perms => "0:0:0440",
exp_rem_error => "yes",
KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
},
{
exp_name => "sda-8741C4G-end",
exp_perms => "0:0:0600",
},
{
desc => "builtin path_id",
},
);
# set env
sub udev {
# create temporary rules
close CONF;
if ($valgrind > 0) {
system("$udev_bin_valgrind $action $devpath");
} else {
system("$udev_bin $action $devpath");
}
}
my $error = 0;
sub permissions_test {
my $wrong = 0;
my $userid;
my $groupid;
if ($1 ne "") {
if (defined(getpwnam($1))) {
$userid = int(getpwnam($1));
} else {
$userid = $1;
}
}
if ($2 ne "") {
if (defined(getgrnam($2))) {
$groupid = int(getgrnam($2));
} else {
$groupid = $2;
}
}
if ($3 ne "") {
}
if ($wrong == 0) {
print "permissions: ok\n";
} else {
printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
print "permissions: error\n";
$error++;
sleep(1);
}
}
sub major_minor_test {
my $wrong = 0;
$rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
if ($1 ne "") {
}
if ($2 ne "") {
}
if ($wrong == 0) {
print "major:minor: ok\n";
} else {
printf " expected major:minor is: %i:%i\n", $1, $2;
print "major:minor: error\n";
$error++;
sleep(1);
}
}
sub make_udev_root {
system("rm -rf $udev_root");
mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
# setting group and mode of udev_root ensures the tests work
# even if the parent directory has setgid bit enabled.
}
sub run_test {
print "TEST $number: $rules->{desc}\n";
if (defined($rules->{not_exp_name})) {
if ((-e "$PWD/$udev_root/$rules->{not_exp_name}") ||
(-l "$PWD/$udev_root/$rules->{not_exp_name}")) {
print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
$error++;
sleep(1);
}
}
if ((-e "$PWD/$udev_root/$rules->{exp_name}") ||
(-l "$PWD/$udev_root/$rules->{exp_name}")) {
}
if (defined($rules->{exp_majorminor})) {
}
print "add: ok\n";
} else {
print "add: error";
if ($rules->{exp_add_error}) {
print " as expected\n";
} else {
print "\n";
system("tree $udev_root");
print "\n";
$error++;
sleep(1);
}
}
print "\n\n";
return;
}
if ((-e "$PWD/$udev_root/$rules->{exp_name}") ||
(-l "$PWD/$udev_root/$rules->{exp_name}")) {
print "remove: error";
if ($rules->{exp_rem_error}) {
print " as expected\n";
} else {
print "\n";
system("tree $udev_root");
print "\n";
$error++;
sleep(1);
}
} else {
print "remove: ok\n";
}
print "\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
# create config file
print CONF "udev_root=\"$udev_root\"\n";
print CONF "udev_run=\"$udev_root/.udev\"\n";
print CONF "udev_sys=\"$sysfs\"\n";
print CONF "udev_rules=\"$PWD\"\n";
print CONF "udev_log=\"err\"\n";
close CONF;
my $test_num = 1;
my @list;
$valgrind = 1;
printf("using valgrind\n");
} else {
}
}
if ($list[0]) {
print "udev-test will run test number $arg:\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_root");
unlink($udev_rules);
unlink($udev_conf);
if ($error > 0) {
exit(1);
}
exit(0);