chiark / gitweb /
libudev: allow to store negative values in the udev database
[elogind.git] / test / udev-test.pl
index 9725aff5ba0a245ca7881787e1f68d0f2792e26e..47cadc5db35768f64f6a3165d90cdf291ba4d414 100755 (executable)
@@ -21,8 +21,8 @@ use warnings;
 use strict;
 
 my $PWD                        = $ENV{PWD};
-my $sysfs              = "sys/";
-my $udev_bin           = "../udev/test-udev";
+my $sysfs              = "test/sys/";
+my $udev_bin           = "udev/test-udev";
 my $valgrind           = 0;
 my $udev_bin_valgrind  = "valgrind --tool=memcheck --leak-check=yes --quiet $udev_bin";
 my $udev_root          = "udev-root/";
@@ -30,13 +30,21 @@ my $udev_conf               = "udev-test.conf";
 my $udev_rules         = "udev-test.rules";
 
 my @tests = (
+       {
+               desc            => "no rules",
+               subsys          => "block",
+               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
+               exp_name        => "sda" ,
+               rules           => <<EOF
+EOF
+       },
        {
                desc            => "label test of scsi disc (old key names)",
                subsys          => "block",
                devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                exp_name        => "boot_disk" ,
                rules           => <<EOF
-SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", NAME="boot_disk%n"
+SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", NAME="boot_disk%n", RUN+="socket:@/org/kernel/udev/monitor"
 KERNEL=="ttyACM0", NAME="modem"
 EOF
        },
@@ -273,15 +281,6 @@ EOF
                exp_name        => "Major:8:minor:5:kernelnumber:5:id:0:0:0:0" ,
                rules           => <<EOF
 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:id:%b"
-EOF
-       },
-       {
-               desc            => "test substitution chars (with length limit)",
-               subsys          => "block",
-               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
-               exp_name        => "M8-m5-n5-b0:0-xAT" ,
-               rules           => <<EOF
-SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-x%2s{vendor}"
 EOF
        },
        {
@@ -343,15 +342,6 @@ EOF
                exp_name        => "test-0:0:0:0" ,
                rules           => <<EOF
 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n test-%b", RESULT=="test-0:0*", NAME="%c"
-EOF
-       },
-       {
-               desc            => "program with escaped format char (tricky: callout returns format char!)",
-               subsys          => "block",
-               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
-               exp_name        => "escape-5" ,
-               rules           => <<EOF
-SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n escape-%%n", KERNEL=="sda5", NAME="%c"
 EOF
        },
        {
@@ -472,6 +462,33 @@ EOF
                rules           => <<EOF
 KERNEL=="console", NAME="TTY"
 ATTRS{dev}=="5:1", NAME="foo"
+EOF
+       },
+       {
+               desc            => "ATTR (empty file)",
+               subsys          => "tty",
+               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
+               exp_name        => "empty" ,
+               rules           => <<EOF
+KERNEL=="sda", ATTR{test_empty_file}=="?*", NAME:="something"
+KERNEL=="sda", ATTR{test_empty_file}!="", NAME:="not-empty"
+KERNEL=="sda", ATTR{test_empty_file}=="", NAME:="empty"
+KERNEL=="sda", ATTR{test_empty_file}!="?*", NAME:="not-something"
+KERNEL=="sda", NAME="wrong"
+EOF
+       },
+       {
+               desc            => "ATTR (non-existent file)",
+               subsys          => "tty",
+               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
+               exp_name        => "non-existent" ,
+               rules           => <<EOF
+KERNEL=="sda", ATTR{nofile}=="?*", NAME:="something"
+KERNEL=="sda", ATTR{nofile}!="", NAME:="not-empty"
+KERNEL=="sda", ATTR{nofile}=="", NAME:="empty"
+KERNEL=="sda", ATTR{nofile}!="?*", NAME:="not-something"
+KERNEL=="sda", TEST!="nofile", NAME:="non-existent"
+KERNEL=="sda", NAME="wrong"
 EOF
        },
        {
@@ -802,9 +819,9 @@ EOF
                subsys          => "tty",
                devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                exp_name        => "symlink2-ttyACM0",
-               exp_target      => "ttyACM0",
+               exp_target      => "ttyACM-0",
                rules           => <<EOF
-KERNEL=="ttyACM[0-9]*", NAME="ttyACM%n", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
+KERNEL=="ttyACM[0-9]*", NAME="ttyACM-%n", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
 EOF
        },
        {
@@ -984,16 +1001,6 @@ EOF
                exp_target      => "ttyACM0",
                rules           => <<EOF
 KERNEL=="ttyACM[0-9]*", NAME="ttyACM%n", SYMLINK+="%s{dev}"
-EOF
-       },
-       {
-               desc            => "symlink %Ns{filename} substitution",
-               subsys          => "tty",
-               devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
-               exp_name        => "166",
-               exp_target      => "ttyACM0",
-               rules           => <<EOF
-KERNEL=="ttyACM[0-9]*", NAME="ttyACM%n", SYMLINK+="%3s{dev}"
 EOF
        },
        {
@@ -1024,7 +1031,7 @@ EOF
                not_exp_name    => "node",
                exp_add_error   => "yes",
                rules           => <<EOF
-SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore"
+SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_device"
 EOF
        },
        {
@@ -1411,6 +1418,17 @@ EOF
                subsys          => "tty",
                devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                exp_name        => "<none>",
+               not_exp_name    => "ttyACM0",
+               exp_add_error   => "yes",
+               rules           => <<EOF
+KERNEL=="ttyACM[0-9]*", NAME=""
+EOF
+       },
+       {
+               desc            => "test empty NAME (empty override)",
+               subsys          => "tty",
+               devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
+               exp_name        => "<none>",
                not_exp_name    => "wrong",
                exp_add_error   => "yes",
                rules           => <<EOF
@@ -1419,7 +1437,7 @@ KERNEL=="ttyACM[0-9]*", NAME=""
 EOF
        },
        {
-               desc            => "test empty NAME 2",
+               desc            => "test empty NAME (non-empty override)",
                subsys          => "tty",
                devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                exp_name        => "right",
@@ -1448,6 +1466,31 @@ EOF
 KERNEL=="dontknow*|*nothing", NAME="nomatch"
 KERNEL=="ttyACM*", NAME="before"
 KERNEL=="dontknow*|ttyACM*|nothing*", NAME="right"
+EOF
+       },
+       {
+               desc            => "test multi matches 3",
+               subsys          => "tty",
+               devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
+               exp_name        => "right",
+               rules           => <<EOF
+KERNEL=="dontknow|nothing", NAME="nomatch"
+KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", NAME="wrong1"
+KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", NAME="wrong2"
+KERNEL=="dontknow|ttyACM0|nothing", NAME="right"
+EOF
+       },
+       {
+               desc            => "test multi matches 4",
+               subsys          => "tty",
+               devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
+               exp_name        => "right",
+               rules           => <<EOF
+KERNEL=="dontknow|nothing", NAME="nomatch"
+KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", NAME="wrong1"
+KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", NAME="wrong2"
+KERNEL=="all|dontknow|ttyACM0", NAME="right"
+KERNEL=="ttyACM0a|nothing", NAME="wrong3"
 EOF
        },
        {
@@ -1481,8 +1524,20 @@ KERNEL=="sda1", GOTO="TEST"
 KERNEL=="sda1", NAME="wrong"
 KERNEL=="sda1", GOTO="BAD"
 KERNEL=="sda1", NAME="", LABEL="NO"
-KERNEL=="sda1", NAME="right", LABEL="TEST"
-KERNEL=="sda1", LABEL="BAD"
+KERNEL=="sda1", NAME="right", LABEL="TEST", GOTO="end"
+KERNEL=="sda1", NAME="wrong2", LABEL="BAD"
+LABEL="end"
+EOF
+       },
+       {
+               desc            => "GOTO label does not exist",
+               subsys          => "block",
+               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
+               exp_name        => "right",
+               rules           => <<EOF
+KERNEL=="sda1", GOTO="does-not-exist"
+KERNEL=="sda1", NAME="right",
+LABEL="exists"
 EOF
        },
        {
@@ -1560,16 +1615,6 @@ EOF
                rules           => <<EOF
 TEST=="/etc/hosts", NAME="there"
 TEST!="/etc/hosts", NAME="notthere"
-EOF
-       },
-       {
-               desc            => "TEST invalid NAME= only (skip invalid rule)",
-               subsys          => "block",
-               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
-               exp_name        => "yes",
-               rules           => <<EOF
-SUBSYSTEM=="block", NAME="yes"
-NAME="no"
 EOF
        },
        {
@@ -1610,14 +1655,14 @@ KERNEL=="sda", MODE="0000"
 EOF
        },
        {
-               desc            => "TEST PROGRAM feeds MODE",
+               desc            => "TEST PROGRAM feeds OWNER, GROUP, MODE",
                subsys          => "block",
                devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                exp_name        => "sda",
-               exp_perms       => "0:0:0400",
+               exp_perms       => "5000:100:0400",
                rules           => <<EOF
 KERNEL=="sda", MODE="666"
-KERNEL=="sda", PROGRAM=="/bin/echo 0 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
+KERNEL=="sda", PROGRAM=="/bin/echo 5000 100 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
 EOF
        },
        {
@@ -1631,7 +1676,18 @@ KERNEL=="sda", MODE="440"
 KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
 EOF
        },
-
+       {
+               desc            => "magic [subsys/sysname] attribute substitution",
+               subsys          => "block",
+               devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
+               exp_name        => "sda-8741C4G-end",
+               exp_perms       => "0:0:0660",
+               rules           => <<EOF
+KERNEL=="sda", PROGRAM="/bin/true create-envp"
+KERNEL=="sda", ENV{TESTENV}="change-envp"
+KERNEL=="sda", NAME="%k-%s{[dmi/id]product_name}-end", RUN+="socket:@/org/kernel/udev/monitor"
+EOF
+       },
 );
 
 # set env
@@ -1773,19 +1829,20 @@ sub run_test {
 
 
        udev("add", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
+       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++
+               }
+       }
+
        if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
            (-l "$PWD/$udev_root$rules->{exp_name}")) {
 
                my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
                    $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$rules->{exp_name}");
 
-               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++
-                       }
-               }
                if (defined($rules->{exp_perms})) {
                        permissions_test($rules, $uid, $gid, $mode);
                }