6136c1
From 5f34ea55a8c6723240eb1641a655db7df3c428a2 Mon Sep 17 00:00:00 2001
6136c1
From: Martin Wilck <mwilck@suse.com>
6136c1
Date: Tue, 24 Apr 2018 09:38:26 +0200
6136c1
Subject: [PATCH] test/udev-test.pl: use computed devnode name
6136c1
6136c1
More often than not, the created devnode is the basename of the
6136c1
sysfs entry. The "devnode" device may be used to override the
6136c1
auto-detected node name.
6136c1
6136c1
Permissions and major/minor number are now verified on the devnode
6136c1
itself, not on symlinks.
6136c1
6136c1
For those tests where exp_name is set to the computed devnode name,
6136c1
the explicit "exp_name" can be removed. "exp_name" is only required for
6136c1
symlinks.
6136c1
6136c1
This allows separate testing for devnodes and symlinks an a follow-up
6136c1
patch.
6136c1
6136c1
(cherry picked from commit f0dccf01a7b4e72278e14effd74782ea83d0a73b)
6136c1
6136c1
Related: #1642728
6136c1
---
6136c1
 test/udev-test.pl | 92 +++++++++++++++++++++++++++++++++--------------
6136c1
 1 file changed, 66 insertions(+), 26 deletions(-)
6136c1
6136c1
diff --git a/test/udev-test.pl b/test/udev-test.pl
6136c1
index db25ef13c1..aa9a8dc2ff 100755
6136c1
--- a/test/udev-test.pl
6136c1
+++ b/test/udev-test.pl
6136c1
@@ -55,12 +55,10 @@ my @tests = (
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
6136c1
-                                exp_name        => "sda" ,
6136c1
                                 exp_rem_error   => "yes",
6136c1
                         },
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
6136c1
-                                exp_name        => "sda1" ,
6136c1
                                 exp_rem_error   => "yes",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -644,6 +642,7 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/virtual/block/fake!blockdev0",
6136c1
+                                devnode         => "fake/blockdev0",
6136c1
                                 exp_name        => "is/a/fake/blockdev0" ,
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -657,7 +656,7 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/virtual/block/fake!blockdev0",
6136c1
-                                exp_name        => "fake/blockdev0" ,
6136c1
+                                devnode         => "fake/blockdev0",
6136c1
                                 exp_rem_error   => "yes",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -768,7 +767,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/virtual/tty/tty33",
6136c1
-                                exp_name        => "tty33",
6136c1
                                 exp_perms       => "0:0:0600",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -864,7 +862,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => "1::",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -876,7 +873,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => ":1:0660",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -888,7 +884,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => "::0060",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -900,7 +895,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => "1:1:0777",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -912,7 +906,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => "1:1:0777",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -926,7 +919,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => "1:1:0777",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -942,7 +934,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
6136c1
-                                exp_name        => "ttyACM0",
6136c1
                                 exp_perms       => "1:2:0777",
6136c1
                         }],
6136c1
                 rules           => <
6136c1
@@ -1922,7 +1913,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
6136c1
-                                exp_name        => "sda",
6136c1
                                 exp_perms       => "0:0:0000",
6136c1
                                 exp_rem_error   => "yes",
6136c1
                         }],
6136c1
@@ -1935,7 +1925,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
6136c1
-                                exp_name        => "sda",
6136c1
                                 exp_perms       => "1:1:0400",
6136c1
                                 exp_rem_error   => "yes",
6136c1
                         }],
6136c1
@@ -1949,7 +1938,6 @@ EOF
6136c1
                 devices => [
6136c1
                         {
6136c1
                                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
6136c1
-                                exp_name        => "sda",
6136c1
                                 exp_perms       => "0:0:0440",
6136c1
                                 exp_rem_error   => "yes",
6136c1
                         }],
6136c1
@@ -2203,6 +2191,44 @@ sub udev_setup {
6136c1
         return 1;
6136c1
 }
6136c1
 
6136c1
+sub get_devnode {
6136c1
+        my ($device) = @_;
6136c1
+        my $devnode;
6136c1
+
6136c1
+        if (defined($device->{devnode})) {
6136c1
+                $devnode = "$udev_dev/$device->{devnode}";
6136c1
+        } else {
6136c1
+                $devnode = "$device->{devpath}";
6136c1
+                $devnode =~ s!.*/!$udev_dev/!;
6136c1
+        }
6136c1
+        return $devnode;
6136c1
+}
6136c1
+
6136c1
+sub check_devnode {
6136c1
+        my ($device) = @_;
6136c1
+        my $devnode = get_devnode($device);
6136c1
+
6136c1
+        my @st = lstat("$devnode");
6136c1
+        if (! (-b _  || -c _)) {
6136c1
+                print "add $devnode:         error\n";
6136c1
+                system("tree", "$udev_dev");
6136c1
+                $error++;
6136c1
+                return undef;
6136c1
+        }
6136c1
+
6136c1
+        my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
6136c1
+            $atime, $mtime, $ctime, $blksize, $blocks) = @st;
6136c1
+
6136c1
+        if (defined($device->{exp_perms})) {
6136c1
+                permissions_test($device, $uid, $gid, $mode);
6136c1
+        }
6136c1
+        if (defined($device->{exp_majorminor})) {
6136c1
+                major_minor_test($device, $rdev);
6136c1
+        }
6136c1
+        print "add $devnode:         ok\n";
6136c1
+        return $devnode;
6136c1
+}
6136c1
+
6136c1
 sub check_add {
6136c1
         my ($device) = @_;
6136c1
 
6136c1
@@ -2215,19 +2241,13 @@ sub check_add {
6136c1
                 }
6136c1
         }
6136c1
 
6136c1
+        my $devnode = check_devnode($device);
6136c1
+
6136c1
         print "device \'$device->{devpath}\' expecting node/link \'$device->{exp_name}\'\n";
6136c1
+        return if (!defined($device->{exp_name}));
6136c1
+
6136c1
         if ((-e "$udev_dev/$device->{exp_name}") ||
6136c1
             (-l "$udev_dev/$device->{exp_name}")) {
6136c1
-
6136c1
-                my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
6136c1
-                    $atime, $mtime, $ctime, $blksize, $blocks) = stat("$udev_dev/$device->{exp_name}");
6136c1
-
6136c1
-                if (defined($device->{exp_perms})) {
6136c1
-                        permissions_test($device, $uid, $gid, $mode);
6136c1
-                }
6136c1
-                if (defined($device->{exp_majorminor})) {
6136c1
-                        major_minor_test($device, $rdev);
6136c1
-                }
6136c1
                 print "add $device->{devpath}:         ok\n";
6136c1
         } else {
6136c1
                 print "add  $device->{devpath}:         error";
6136c1
@@ -2243,12 +2263,32 @@ sub check_add {
6136c1
         }
6136c1
 }
6136c1
 
6136c1
+sub check_remove_devnode {
6136c1
+        my ($device) = @_;
6136c1
+        my $devnode = get_devnode($device);
6136c1
+
6136c1
+        if (-e "$devnode") {
6136c1
+                print "remove  $devnode:      error";
6136c1
+                print "\n";
6136c1
+                system("tree", "$udev_dev");
6136c1
+                print "\n";
6136c1
+                $error++;
6136c1
+                sleep(1);
6136c1
+        } else {
6136c1
+                print "remove $devnode:         ok\n";
6136c1
+        }
6136c1
+}
6136c1
+
6136c1
 sub check_remove {
6136c1
         my ($device) = @_;
6136c1
 
6136c1
+        check_remove_devnode($device);
6136c1
+
6136c1
+        return if (!defined($device->{exp_name}));
6136c1
+
6136c1
         if ((-e "$udev_dev/$device->{exp_name}") ||
6136c1
             (-l "$udev_dev/$device->{exp_name}")) {
6136c1
-                print "remove  $device->{devpath}:      error";
6136c1
+                print "remove  $device->{exp_name}:      error";
6136c1
                 if ($device->{exp_rem_error}) {
6136c1
                         print " as expected\n";
6136c1
                 } else {
6136c1
@@ -2259,7 +2299,7 @@ sub check_remove {
6136c1
                         sleep(1);
6136c1
                 }
6136c1
         } else {
6136c1
-                print "remove  $device->{devpath}:      ok\n";
6136c1
+                print "remove  $device->{exp_name}:      ok\n";
6136c1
         }
6136c1
 }
6136c1