#!/usr/bin/env perl # 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 © 2004 Leann Ogasawara use warnings; use strict; my $udev_bin = "./test-udev"; my $valgrind = 0; my $gdb = 0; my $strace = 0; my $udev_bin_valgrind = "valgrind --tool=memcheck --leak-check=yes --track-origins=yes --quiet $udev_bin"; my $udev_bin_gdb = "gdb --args $udev_bin"; my $udev_bin_strace = "strace -efile $udev_bin"; my $udev_run = "test/run"; my $udev_tmpfs = "test/tmpfs"; my $udev_sys = "${udev_tmpfs}/sys"; my $udev_dev = "${udev_tmpfs}/dev"; my $udev_rules_dir = "$udev_run/udev/rules.d"; my $udev_rules = "$udev_rules_dir/udev-test.rules"; my $EXIT_TEST_SKIP = 77; my $rules_10k_tags = ""; for (my $i = 1; $i <= 10000; ++$i) { $rules_10k_tags .= 'KERNEL=="sda", TAG+="test' . $i . "\"\n"; } my $rules_10k_tags_continuation = "KERNEL==\"sda\", \\\n"; for (my $i = 1; $i < 10000; ++$i) { $rules_10k_tags_continuation .= 'TAG+="test' . $i . "\",\\\n"; } $rules_10k_tags_continuation .= "TAG+=\"test10000\"\\n"; my @tests = ( { desc => "no rules", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "sda", exp_rem_error => "yes", rules => < "label test of scsi disc", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "boot_disk", rules => < "label test of scsi disc", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "boot_disk", rules => < "label test of scsi disc", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "boot_disk", rules => < "label test of scsi partition", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "boot_disk1", rules => < "label test of pattern match", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "boot_disk1", rules => < "label test of multiple sysfs files", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "boot_disk1", rules => < "label test of max sysfs files (skip invalid rule)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "boot_disk1", rules => < "catch device by *", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem/0", rules => < "catch device by * - take 2", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem/0", rules => < "catch device by ?", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem/0", rules => < "catch device by character class", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem/0", rules => < "replace kernel name", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "Handle comment lines in config file (and replace kernel name)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "Handle comment lines in config file with whitespace (and replace kernel name)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "Handle whitespace only lines (and replace kernel name)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "whitespace", rules => < "Handle empty lines in config file (and replace kernel name)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "Handle backslashed multi lines in config file (and replace kernel name)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "preserve backslashes, if they are not for a newline", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "aaa", rules => < "Handle stupid backslashed multi lines in config file (and replace kernel name)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "subdirectory handling", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "sub/direct/ory/modem", rules => < "parent device name match of scsi partition", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "first_disk5", rules => < "test substitution chars", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "Major:8:minor:5:kernelnumber:5:id:0:0:0:0", rules => < "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", rules => < "sustitution of sysfs value (%s{file})", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "disk-ATA-sda", rules => < "program result substitution", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "special-device-5", not_exp_name => "not", rules => < "program result substitution (newline removal)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "newline_removed", rules => < "program result substitution", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "test-0:0:0:0", rules => < "program with lots of arguments", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "foo9", rules => < "program with subshell", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "bar9", rules => < "program arguments combined with apostrophes", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "foo7", rules => < "program arguments combined with escaped double quotes, part 1", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "foo2", rules => < "program arguments combined with escaped double quotes, part 2", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "foo2", rules => < "program arguments combined with escaped double quotes, part 3", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "foo2", rules => < "characters before the %c{N} substitution", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "my-foo9", rules => < "substitute the second to last argument", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "my-foo8", rules => < "test substitution by variable name", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "Major:8-minor:5-kernelnumber:5-id:0:0:0:0", rules => < "test substitution by variable name 2", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "Major:8-minor:5-kernelnumber:5-id:0:0:0:0", rules => < "test substitution by variable name 3", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "850:0:0:05", rules => < "test substitution by variable name 4", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "855", rules => < "test substitution by variable name 5", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "8550:0:0:0", rules => < "non matching SUBSYSTEMS for device with no parent", devpath => "/devices/virtual/tty/console", exp_name => "TTY", rules => < "non matching SUBSYSTEMS", devpath => "/devices/virtual/tty/console", exp_name => "TTY", rules => < "ATTRS match", devpath => "/devices/virtual/tty/console", exp_name => "foo", rules => < "ATTR (empty file)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "empty", rules => < "ATTR (non-existent file)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "non-existent", rules => < "program and bus type match", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "scsi-0:0:0:0", rules => < "sysfs parent hierarchy", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem", rules => < "name test with ! in the name", devpath => "/devices/virtual/block/fake!blockdev0", exp_name => "is/a/fake/blockdev0", rules => < "name test with ! in the name, but no matching rule", devpath => "/devices/virtual/block/fake!blockdev0", exp_name => "fake/blockdev0", exp_rem_error => "yes", rules => < "KERNELS rule", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "scsi-0:0:0:0", rules => < "KERNELS wildcard all", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "scsi-0:0:0:0", rules => < "KERNELS wildcard partial", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "scsi-0:0:0:0", rules => < "KERNELS wildcard partial 2", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "scsi-0:0:0:0", rules => < "substitute attr with link target value (first match)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "driver-is-sd", rules => < "substitute attr with link target value (currently selected device)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "driver-is-ahci", rules => < "ignore ATTRS attribute whitespace", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "ignored", rules => < "do not ignore ATTRS attribute whitespace", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "matched-with-space", rules => < "permissions USER=bad GROUP=name", devpath => "/devices/virtual/tty/tty33", exp_name => "tty33", exp_perms => "0:0:0600", rules => < "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 => "1::0600", rules => < "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 => ":1:0660", rules => < "textual user id", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", exp_perms => "daemon::0600", rules => < "textual group id", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", exp_perms => ":daemon:0660", rules => < "textual user/group id", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", exp_perms => "root:mail:0660", rules => < "permissions 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 => "::0777", rules => < "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 => "1:1:0777", rules => < "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 => "1::", rules => < "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 => ":1:0660", rules => < "permissions MODE to 0060", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "ttyACM0", exp_perms => "::0060", rules => < "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 => "1:1:0777", rules => < "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 => "1:1:0777", rules => < "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 => "1:1:0777", rules => < "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 => "1:2:0777", rules => < "major/minor number test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", exp_majorminor => "8:0", rules => < "big major number test", devpath => "/devices/virtual/misc/misc-fake1", exp_name => "node", exp_majorminor => "4095:1", rules => < "big major and big minor number test", devpath => "/devices/virtual/misc/misc-fake89999", exp_name => "node", exp_majorminor => "4095:89999", rules => < "multiple symlinks with format char", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "symlink2-ttyACM0", rules => < "multiple symlinks with a lot of s p a c e s", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "one", not_exp_name => " ", rules => < "symlink with spaces in substituted variable", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "name-one_two_three-end", not_exp_name => " ", rules => < "symlink with leading space in substituted variable", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "name-one_two_three-end", not_exp_name => " ", rules => < "symlink with trailing space in substituted variable", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "name-one_two_three-end", not_exp_name => " ", rules => < "symlink with lots of space in substituted variable", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "name-one_two_three-end", not_exp_name => " ", rules => < "symlink with multiple spaces in substituted variable", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "name-one_two_three-end", not_exp_name => " ", rules => < "symlink with space and var with space, part 1", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "first", not_exp_name => " ", rules => < "symlink with space and var with space, part 2", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "name-one_two_three-end", not_exp_name => " ", rules => < "symlink with space and var with space, part 3", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "another_symlink", not_exp_name => " ", rules => < "symlink creation (same directory)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "modem0", rules => < "multiple symlinks", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "second-0", rules => < "symlink name '.'", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => ".", exp_add_error => "yes", exp_rem_error => "yes", rules => < "symlink node to itself", devpath => "/devices/virtual/tty/tty0", exp_name => "link", exp_add_error => "yes", exp_rem_error => "yes", option => "clean", rules => < "symlink %n substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "symlink0", rules => < "symlink %k substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "symlink-ttyACM0", rules => < "symlink %M:%m substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "major-166:0", rules => < "symlink %b substitution", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "symlink-0:0:0:0", rules => < "symlink %c substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "test", rules => < "symlink %c{N} substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "test", rules => < "symlink %c{N+} substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "this", rules => < "symlink only rule with %c{N+}", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "test", rules => < "symlink %s{filename} substitution", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "166:0", rules => < "program result substitution (numbered part of)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "link1", rules => < "program result substitution (numbered part of+)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5", exp_name => "link4", rules => < "SUBSYSTEM match test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", rules => < "DRIVERS match test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", rules => < "devnode substitution test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node", rules => < "parent node name substitution test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "sda-part-1", rules => < "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-/dev-end", rules => < "last_rule option", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "last", rules => < "negation KERNEL!=", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "match", rules => < "negation SUBSYSTEM!=", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "not-anything", rules => < "negation PROGRAM!= exit code", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "nonzero-program", rules => < "ENV{} test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "true", rules => < "ENV{} test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "true", rules => < "ENV{} test (assign)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "true", rules => < "ENV{} test (assign 2 times)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "true", rules => < "ENV{} test (assign2)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "part", rules => < "untrusted string sanitize", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "sane", rules => < "untrusted string sanitize (don't replace utf8)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "uber", rules => < "untrusted string sanitize (replace invalid utf8)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "replaced", rules => < "read sysfs value from parent device", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "serial-354172020305000", rules => < "match against empty key string", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "ok", rules => < "check ACTION value", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "ok", rules => < "final assignment", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "ok", exp_perms => "root:tty:0640", rules => < "final assignment 2", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "ok", exp_perms => "root:tty:0640", rules => < "env substitution", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "node-add-me", rules => < "reset list to current value", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "three", not_exp_name => "two", rules => < "test empty SYMLINK+ (empty override)", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "right", not_exp_name => "wrong", rules => < "test multi matches", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "right", rules => < "test multi matches 2", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "right", rules => < "test multi matches 3", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "right", rules => < "test multi matches 4", devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0", exp_name => "right", rules => < "IMPORT parent test sequence 1/2 (keep)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "parent", option => "keep", rules => < "IMPORT parent test sequence 2/2 (keep)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "parentenv-parent_right", option => "clean", rules => < "GOTO test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "right", rules => < "GOTO label does not exist", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "right", rules => < "SYMLINK+ compare test", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "right", not_exp_name => "wrong", rules => < "invalid key operation", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "yes", rules => < "operator chars in attribute", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "yes", rules => < "overlong comment line", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1", exp_name => "yes", rules => < "magic subsys/kernel lookup", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "00:16:41:e2:8d:ff", rules => < "TEST absolute path", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "there", rules => < "TEST subsys/kernel lookup", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "yes", rules => < "TEST relative path", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "relative", rules => < "TEST wildcard substitution (find queue/nr_requests)", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "found-subdir", rules => < "TEST MODE=0000", 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:0000", exp_rem_error => "yes", rules => < "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 => "1:1:0400", exp_rem_error => "yes", rules => < "TEST PROGRAM feeds MODE with overflow", 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:0440", exp_rem_error => "yes", rules => < "magic [subsys/sysname] attribute substitution", 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:0600", rules => < "builtin path_id", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0", rules => < "add and match tag", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "found", not_exp_name => "bad", rules => < "don't crash with lots of tags", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "found", rules => $rules_10k_tags . < "continuations", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "found", not_exp_name => "bad", rules => $rules_10k_tags_continuation . < "continuations with empty line", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "found", not_exp_name => "bad", rules => < "continuations with white only line", devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda", exp_name => "found", not_exp_name => "bad", rules => <$udev_rules" || die "unable to create rules file: $udev_rules"; print CONF $$rules; close CONF; if ($valgrind > 0) { return system("$udev_bin_valgrind $action $devpath"); } elsif ($gdb > 0) { return system("$udev_bin_gdb $action $devpath"); } elsif ($strace > 0) { return system("$udev_bin_strace $action $devpath"); } else { return system("$udev_bin", "$action", "$devpath"); } } my $error = 0; sub permissions_test { my($rules, $uid, $gid, $mode) = @_; my $wrong = 0; my $userid; my $groupid; $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/; if ($1 ne "") { if (defined(getpwnam($1))) { $userid = int(getpwnam($1)); } else { $userid = $1; } if ($uid != $userid) { $wrong = 1; } } if ($2 ne "") { if (defined(getgrnam($2))) { $groupid = int(getgrnam($2)); } else { $groupid = $2; } if ($gid != $groupid) { $wrong = 1; } } if ($3 ne "") { if (($mode & 07777) != oct($3)) { $wrong = 1; }; } if ($wrong == 0) { print "permissions: ok\n"; } else { printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3); printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777; print "permissions: error\n"; $error++; sleep(1); } } sub major_minor_test { my($rules, $rdev) = @_; my $major = ($rdev >> 8) & 0xfff; my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00); my $wrong = 0; $rules->{exp_majorminor} =~ m/^(.*):(.*)$/; if ($1 ne "") { if ($major != $1) { $wrong = 1; }; } if ($2 ne "") { if ($minor != $2) { $wrong = 1; }; } if ($wrong == 0) { print "major:minor: ok\n"; } else { printf " expected major:minor is: %i:%i\n", $1, $2; printf " created major:minor is : %i:%i\n", $major, $minor; print "major:minor: error\n"; $error++; sleep(1); } } sub udev_setup { system("umount", $udev_tmpfs); rmdir($udev_tmpfs); mkdir($udev_tmpfs) || die "unable to create udev_tmpfs: $udev_tmpfs\n"; if (system("mount", "-o", "rw,mode=755,nosuid,noexec", "-t", "tmpfs", "tmpfs", $udev_tmpfs)) { warn "unable to mount tmpfs"; return 0; } 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_dev) || die "unable to chown $udev_dev\n"; chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n"; if (system("mknod", $udev_dev . "/null", "c", "1", "3")) { warn "unable to create $udev_dev/null"; return 0; } # check if we are permitted to create block device nodes my $block_device_filename = $udev_dev . "/sda"; if (system("mknod", $block_device_filename, "b", "8", "0")) { warn "unable to create $block_device_filename"; return 0; } unlink $block_device_filename; system("cp", "-r", "test/sys/", $udev_sys) && die "unable to copy test/sys"; system("rm", "-rf", "$udev_run"); if (!mkdir($udev_run)) { warn "unable to create directory $udev_run"; return 0; } return 1; } sub run_test { my ($rules, $number) = @_; my $rc; print "TEST $number: $rules->{desc}\n"; print "device \'$rules->{devpath}\' expecting node/link \'$rules->{exp_name}\'\n"; $rc = udev("add", $rules->{devpath}, \$rules->{rules}); if ($rc != 0) { print "$udev_bin add failed with code $rc\n"; $error++; } if (defined($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 "$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("$udev_dev/$rules->{exp_name}"); if (defined($rules->{exp_perms})) { permissions_test($rules, $uid, $gid, $mode); } if (defined($rules->{exp_majorminor})) { major_minor_test($rules, $rdev); } print "add: ok\n"; } else { print "add: error"; if ($rules->{exp_add_error}) { print " as expected\n"; } else { print "\n"; system("tree", "$udev_dev"); print "\n"; $error++; sleep(1); } } if (defined($rules->{option}) && $rules->{option} eq "keep") { print "\n\n"; return; } $rc = udev("remove", $rules->{devpath}, \$rules->{rules}); if ($rc != 0) { print "$udev_bin remove failed with code $rc\n"; $error++; } 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_dev"); print "\n"; $error++; sleep(1); } } else { print "remove: ok\n"; } print "\n"; if (defined($rules->{option}) && $rules->{option} eq "clean") { udev_setup(); } } # only run if we have root permissions # due to mknod restrictions if (!($<==0)) { print "Must have root permissions to run properly.\n"; exit($EXIT_TEST_SKIP); } # skip the test when running in a chroot system("systemd-detect-virt", "-r", "-q"); if ($? >> 8 == 0) { print "Running in a chroot, skipping the test.\n"; exit($EXIT_TEST_SKIP); } if (!udev_setup()) { warn "Failed to set up the environment, skipping the test"; exit($EXIT_TEST_SKIP); } if (system($udev_bin, "check")) { warn "$udev_bin failed to set up the environment, skipping the test"; exit($EXIT_TEST_SKIP); } my $test_num = 1; my @list; foreach my $arg (@ARGV) { if ($arg =~ m/--valgrind/) { $valgrind = 1; printf("using valgrind\n"); } elsif ($arg =~ m/--gdb/) { $gdb = 1; printf("using gdb\n"); } elsif ($arg =~ m/--strace/) { $strace = 1; printf("using strace\n"); } else { push(@list, $arg); } } if ($list[0]) { foreach my $arg (@list) { if (defined($tests[$arg-1]->{desc})) { print "udev-test will run test number $arg:\n\n"; run_test($tests[$arg-1], $arg); } else { print "test does not exist.\n"; } } } else { # test all print "\nudev-test will run ".($#tests + 1)." tests:\n\n"; foreach my $rules (@tests) { run_test($rules, $test_num); $test_num++; } } print "$error errors occurred\n\n"; # cleanup system("rm", "-rf", "$udev_run"); system("umount", "$udev_tmpfs"); rmdir($udev_tmpfs); if ($error > 0) { exit(1); } exit(0);