chiark / gitweb /
sysv-generator: Replace Provides: symlinks with real units
[elogind.git] / test / udev-test.pl
index 0706c7a..d9b7967 100755 (executable)
 # 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-2012 Kay Sievers <kay@vrfy.org>
 # Copyright (C) 2004 Leann Ogasawara <ogasawara@osdl.org>
 
 use warnings;
 use strict;
 
-my $PWD                 = $ENV{PWD};
-my $sysfs               = "test/sys";
-my $udev_bin            = "src/test-udev";
+my $udev_bin            = "./test-udev";
 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 $udev_dev            = "test/dev";
+my $udev_run            = "test/run";
+my $udev_rules_dir      = "$udev_run/udev/rules.d";
+my $udev_rules          = "$udev_rules_dir/udev-test.rules";
+my $EXIT_TEST_SKIP      = 77;
 
 my @tests = (
         {
@@ -254,15 +254,6 @@ SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:%M:minor:%m:kernelnumber
 EOF
         },
         {
-                desc            => "import of shell-value file",
-                devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
-                exp_name        => "subdir/err/node" ,
-                rules           => <<EOF
-SUBSYSTEMS=="scsi", IMPORT{file}="udev-test.conf", SYMLINK+="subdir/%E{udev_log}/node"
-KERNEL=="ttyACM0", SYMLINK+="modem"
-EOF
-        },
-        {
                 desc            => "import of shell-value returned from program",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "node12345678",
@@ -555,25 +546,25 @@ EOF
                 exp_name        => "tty33",
                 exp_perms       => "0:0:0600",
                 rules           => <<EOF
-KERNEL=="tty33", SYMLINK+="tty33", OWNER="bad", GROUP="name"
+KERNEL=="tty33", OWNER="bad", GROUP="name"
 EOF
         },
         {
-                desc            => "permissions OWNER=5000",
+                desc            => "permissions OWNER=1",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "node",
-                exp_perms       => "5000::0600",
+                exp_perms       => "1::0600",
                 rules           => <<EOF
-SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="5000"
+SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1"
 EOF
         },
         {
-                desc            => "permissions GROUP=100",
+                desc            => "permissions GROUP=1",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "node",
-                exp_perms       => ":100:0660",
+                exp_perms       => ":1:0660",
                 rules           => <<EOF
-SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="100"
+SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="1"
 EOF
         },
         {
@@ -613,30 +604,30 @@ SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", MODE="0777"
 EOF
         },
         {
-                desc            => "permissions OWNER=5000 GROUP=100 MODE=0777",
+                desc            => "permissions OWNER=1 GROUP=1 MODE=0777",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "node",
-                exp_perms       => "5000:100:0777",
+                exp_perms       => "1:1:0777",
                 rules           => <<EOF
-SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="5000", GROUP="100", MODE="0777"
+SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1", GROUP="1", MODE="0777"
 EOF
         },
         {
-                desc            => "permissions OWNER to 5000",
+                desc            => "permissions OWNER to 1",
                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                 exp_name        => "ttyACM0",
-                exp_perms       => "5000::",
+                exp_perms       => "1::",
                 rules           => <<EOF
-KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="5000"
+KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1"
 EOF
         },
         {
-                desc            => "permissions GROUP to 100",
+                desc            => "permissions GROUP to 1",
                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                 exp_name        => "ttyACM0",
-                exp_perms       => ":100:0660",
+                exp_perms       => ":1:0660",
                 rules           => <<EOF
-KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="100"
+KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="1"
 EOF
         },
         {
@@ -652,19 +643,19 @@ EOF
                 desc            => "permissions OWNER, GROUP, MODE",
                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                 exp_name        => "ttyACM0",
-                exp_perms       => "5000:100:0777",
+                exp_perms       => "1:1:0777",
                 rules           => <<EOF
-KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="5000", GROUP="100", MODE="0777"
+KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1", GROUP="1", MODE="0777"
 EOF
         },
         {
                 desc            => "permissions only rule",
                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                 exp_name        => "ttyACM0",
-                exp_perms       => "5000:100:0777",
+                exp_perms       => "1:1:0777",
                 rules           => <<EOF
-KERNEL=="ttyACM[0-9]*", OWNER="5000", GROUP="100", MODE="0777"
-KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
+KERNEL=="ttyACM[0-9]*", OWNER="1", GROUP="1", MODE="0777"
+KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
 EOF
         },
@@ -672,12 +663,12 @@ EOF
                 desc            => "multiple permissions only rule",
                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                 exp_name        => "ttyACM0",
-                exp_perms       => "3000:4000:0777",
+                exp_perms       => "1:1:0777",
                 rules           => <<EOF
-SUBSYSTEM=="tty", OWNER="3000"
-SUBSYSTEM=="tty", GROUP="4000"
+SUBSYSTEM=="tty", OWNER="1"
+SUBSYSTEM=="tty", GROUP="1"
 SUBSYSTEM=="tty", MODE="0777"
-KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
+KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
 EOF
         },
@@ -685,13 +676,13 @@ EOF
                 desc            => "permissions only rule with override at SYMLINK+ rule",
                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
                 exp_name        => "ttyACM0",
-                exp_perms       => "3000:8000:0777",
+                exp_perms       => "1:2:0777",
                 rules           => <<EOF
-SUBSYSTEM=="tty", OWNER="3000"
-SUBSYSTEM=="tty", GROUP="4000"
+SUBSYSTEM=="tty", OWNER="1"
+SUBSYSTEM=="tty", GROUP="1"
 SUBSYSTEM=="tty", MODE="0777"
-KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
-KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="8000"
+KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
+KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="2"
 EOF
         },
         {
@@ -901,7 +892,7 @@ EOF
         {
                 desc            => "udev_root substitution",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
-                exp_name        => "start-udev-root-end",
+                exp_name        => "start-/dev-end",
                 rules           => <<EOF
 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
 EOF
@@ -945,15 +936,6 @@ KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
 EOF
         },
         {
-                desc            => "test for whitespace between the operator",
-                devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
-                exp_name        => "true",
-                rules           => <<EOF
-SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
-KERNEL   ==   "sda1"     ,    SYMLINK+   =    "true"
-EOF
-        },
-        {
                 desc            => "ENV{} test",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
                 exp_name        => "true",
@@ -1251,8 +1233,8 @@ EOF
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "there",
                 rules           => <<EOF
-TEST=="/etc/hosts", SYMLINK+="there"
-TEST!="/etc/hosts", SYMLINK+="notthere"
+TEST=="/etc/machine-id", SYMLINK+="there"
+TEST!="/etc/machine-id", SYMLINK+="notthere"
 EOF
         },
         {
@@ -1293,11 +1275,11 @@ EOF
                 desc            => "TEST PROGRAM feeds OWNER, GROUP, MODE",
                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
                 exp_name        => "sda",
-                exp_perms       => "5000:100:0400",
+                exp_perms       => "1:1:0400",
                 exp_rem_error   => "yes",
                 rules           => <<EOF
 KERNEL=="sda", MODE="666"
-KERNEL=="sda", PROGRAM=="/bin/echo 5000 100 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
+KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
 EOF
         },
         {
@@ -1333,13 +1315,11 @@ EOF
         },
 );
 
-# set env
-$ENV{UDEV_CONFIG_FILE} = $udev_conf;
-
 sub udev {
         my ($action, $devpath, $rules) = @_;
 
         # create temporary rules
+        system("mkdir", "-p", "$udev_rules_dir");
         open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
         print CONF $$rules;
         close CONF;
@@ -1347,7 +1327,7 @@ sub udev {
         if ($valgrind > 0) {
                 system("$udev_bin_valgrind $action $devpath");
         } else {
-                system("$udev_bin $action $devpath");
+                system("$udev_bin", "$action", "$devpath");
         }
 }
 
@@ -1416,13 +1396,15 @@ sub major_minor_test {
         }
 }
 
-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
+sub udev_setup {
+        system("rm", "-rf", "$udev_dev");
+        mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
+        # setting group and mode of udev_dev ensures the tests work
         # even if the parent directory has setgid bit enabled.
-        chown (0, 0, $udev_root) || die "unable to chown $udev_root\n";
-        chmod (0755, $udev_root) || die "unable to chmod $udev_root\n";
+        chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
+        chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
+
+        system("rm", "-rf", "$udev_run");
 }
 
 sub run_test {
@@ -1433,19 +1415,19 @@ sub run_test {
 
         udev("add", $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}")) {
+                if ((-e "$udev_dev/$rules->{not_exp_name}") ||
+                    (-l "$udev_dev/$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 ((-e "$udev_dev/$rules->{exp_name}") ||
+            (-l "$udev_dev/$rules->{exp_name}")) {
 
                 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
-                    $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root/$rules->{exp_name}");
+                    $atime, $mtime, $ctime, $blksize, $blocks) = stat("$udev_dev/$rules->{exp_name}");
 
                 if (defined($rules->{exp_perms})) {
                         permissions_test($rules, $uid, $gid, $mode);
@@ -1460,7 +1442,7 @@ sub run_test {
                         print " as expected\n";
                 } else {
                         print "\n";
-                        system("tree $udev_root");
+                        system("tree", "$udev_dev");
                         print "\n";
                         $error++;
                         sleep(1);
@@ -1473,14 +1455,14 @@ sub run_test {
         }
 
         udev("remove", $rules->{devpath}, \$rules->{rules});
-        if ((-e "$PWD/$udev_root/$rules->{exp_name}") ||
-            (-l "$PWD/$udev_root/$rules->{exp_name}")) {
+        if ((-e "$udev_dev/$rules->{exp_name}") ||
+            (-l "$udev_dev/$rules->{exp_name}")) {
                 print "remove:      error";
                 if ($rules->{exp_rem_error}) {
                         print " as expected\n";
                 } else {
                         print "\n";
-                        system("tree $udev_root");
+                        system("tree", "$udev_dev");
                         print "\n";
                         $error++;
                         sleep(1);
@@ -1492,7 +1474,7 @@ sub run_test {
         print "\n";
 
         if (defined($rules->{option}) && $rules->{option} eq "clean") {
-                make_udev_root();
+                udev_setup();
         }
 
 }
@@ -1504,17 +1486,14 @@ if (!($<==0)) {
         exit;
 }
 
-# prepare
-make_udev_root();
+# skip the test when running in a container
+system("systemd-detect-virt", "-c", "-q");
+if ($? >> 8 == 0) {
+    print "Running in a container, skipping the test.\n";
+    exit($EXIT_TEST_SKIP);
+}
 
-# create config file
-open CONF, ">$udev_conf" || die "unable to create config file: $udev_conf";
-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;
+udev_setup();
 
 my $test_num = 1;
 my @list;
@@ -1547,12 +1526,11 @@ if ($list[0]) {
         }
 }
 
-print "$error errors occured\n\n";
+print "$error errors occurred\n\n";
 
 # cleanup
-system("rm -rf $udev_root");
-unlink($udev_rules);
-unlink($udev_conf);
+system("rm", "-rf", "$udev_dev");
+system("rm", "-rf", "$udev_run");
 
 if ($error > 0) {
     exit(1);