HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/test-udev.py

86,568 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: c2ee8b1bc1440cefc7a546c7393d72ec854c7e6d

Lines 1001–2000 of 2,471

        ''',
    ),
    Rules.new(
        'permissions MODE=0777',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node'],
            exp_perms='::0777',
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", MODE="0777"
        ''',
    ),
    Rules.new(
        'permissions OWNER=1 GROUP=1 MODE=0777',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node'],
            exp_perms='1:1:0777',
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1", GROUP="1", MODE="0777"
        ''',
    ),
    Rules.new(
        'permissions OWNER to 1',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms='1::',
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1"
        ''',
    ),
    Rules.new(
        'permissions GROUP to 1',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms=':1:0660',
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="1"
        ''',
    ),
    Rules.new(
        'permissions MODE to 0060',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms='::0060',
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", MODE="0060"
        ''',
    ),
    Rules.new(
        'permissions OWNER, GROUP, MODE',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms='1:1:0777',
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1", GROUP="1", MODE="0777"
        ''',
    ),
    Rules.new(
        'permissions only rule',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms='1:1:0777',
        ),
        rules=r'''
        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"
        ''',
    ),
    Rules.new(
        'multiple permissions only rule',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms='1:1:0777',
        ),
        rules=r'''
        SUBSYSTEM=="tty", OWNER="1"
        SUBSYSTEM=="tty", GROUP="1"
        SUBSYSTEM=="tty", MODE="0777"
        KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
        ''',
    ),
    Rules.new(
        'permissions only rule with override at SYMLINK+ rule',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_perms='1:2:0777',
        ),
        rules=r'''
        SUBSYSTEM=="tty", OWNER="1"
        SUBSYSTEM=="tty", GROUP="1"
        SUBSYSTEM=="tty", MODE="0777"
        KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="2"
        ''',
    ),
    Rules.new(
        'major/minor number test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node'],
            exp_major_minor='8:0',
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node"
        ''',
    ),
    Rules.new(
        'big major number test',
        Device(
            '/devices/virtual/misc/misc-fake1',
            exp_links=['node'],
            exp_major_minor='4095:1',
        ),
        rules=r'''
        KERNEL=="misc-fake1", SYMLINK+="node"
        ''',
    ),
    Rules.new(
        'big major and big minor number test',
        Device(
            '/devices/virtual/misc/misc-fake89999',
            exp_links=['node'],
            exp_major_minor='4095:89999',
        ),
        rules=r'''
        KERNEL=="misc-fake89999", SYMLINK+="node"
        ''',
    ),
    Rules.new(
        'multiple symlinks with format char',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['symlink1-0', 'symlink2-ttyACM0', 'symlink3-'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
        ''',
    ),
    Rules.new(
        'multiple symlinks with a lot of s p a c e s',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['one', 'two'],
            not_exp_links=[' '],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK="  one     two        "
        ''',
    ),
    Rules.new(
        'symlink with spaces in substituted variable',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['name-one_two_three-end'],
            not_exp_links=[' '],
        ),
        rules=r'''
        ENV{WITH_WS}="one two three"
        SYMLINK="name-$env{WITH_WS}-end"
        ''',
    ),
    Rules.new(
        'symlink with leading space in substituted variable',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['name-one_two_three-end'],
            not_exp_links=[' '],
        ),
        rules=r'''
        ENV{WITH_WS}="   one two three"
        SYMLINK="name-$env{WITH_WS}-end"
        ''',
    ),
    Rules.new(
        'symlink with trailing space in substituted variable',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['name-one_two_three-end'],
            not_exp_links=[' '],
        ),
        rules=r'''
        ENV{WITH_WS}="one two three   "
        SYMLINK="name-$env{WITH_WS}-end"
        ''',
    ),
    Rules.new(
        'symlink with lots of space in substituted variable',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['name-one_two_three-end'],
            not_exp_links=[' '],
        ),
        rules=r'''
        ENV{WITH_WS}="   one two three   "
        SYMLINK="name-$env{WITH_WS}-end"
        ''',
    ),
    Rules.new(
        'symlink with multiple spaces in substituted variable',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['name-one_two_three-end'],
            not_exp_links=[' '],
        ),
        rules=r'''
        ENV{WITH_WS}="   one  two  three   "
        SYMLINK="name-$env{WITH_WS}-end"
        ''',
    ),
    Rules.new(
        'symlink with space and var with space',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['first', 'name-one_two_three-end', 'another_symlink', 'a', 'b', 'c'],
            not_exp_links=[' '],
        ),
        rules=r'''
        ENV{WITH_WS}="   one  two  three   "
        SYMLINK="  first  name-$env{WITH_WS}-end another_symlink a b c "
        ''',
    ),
    Rules.new(
        'symlink with env which contain slash (see #19309)',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['first', 'name-aaa_bbb_ccc-end', 'another_symlink', 'a', 'b', 'c'],
            not_exp_links=['ame-aaa/bbb/ccc-end'],
        ),
        rules=r'''
        ENV{WITH_SLASH}="aaa/bbb/ccc"
        OPTIONS="string_escape=replace", ENV{REPLACED}="$env{WITH_SLASH}"
        SYMLINK="  first  name-$env{REPLACED}-end another_symlink a b c "
        ''',
    ),
    Rules.new(
        'symlink creation (same directory)',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['modem0'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK="modem%n"
        ''',
    ),
    Rules.new(
        'multiple symlinks',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['first-0', 'second-0', 'third-0'],
        ),
        rules=r'''
        KERNEL=="ttyACM0", SYMLINK="first-%n second-%n third-%n"
        ''',
    ),
    Rules.new(
        "symlink name '.'",
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
        ),
        # we get a warning, but the process does not fail
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="."
        ''',
    ),
    Rules.new(
        'symlink node to itself',
        Device(
            '/devices/virtual/tty/tty0',
        ),
        # we get a warning, but the process does not fail
        rules=r'''
        KERNEL=="tty0", SYMLINK+="tty0"
        ''',
    ),
    Rules.new(
        'symlink %n substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['symlink0'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink%n"
        ''',
    ),
    Rules.new(
        'symlink %k substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['symlink-ttyACM0'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink-%k"
        ''',
    ),
    Rules.new(
        'symlink %M:%m substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['major-166:0'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="major-%M:%m"
        ''',
    ),
    Rules.new(
        'symlink %b substitution',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['symlink-0:0:0:0'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-%b"
        ''',
    ),
    Rules.new(
        'symlink %c substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['test'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo test", SYMLINK+="%c"
        ''',
    ),
    Rules.new(
        'symlink %c{N} substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['test'],
            not_exp_links=['symlink', 'this'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2}"
        ''',
    ),
    Rules.new(
        'symlink %c{N+} substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['test', 'this'],
            not_exp_links=['symlink'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2+}"
        ''',
    ),
    Rules.new(
        'symlink only rule with %c{N+}',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['test', 'this'],
            not_exp_links=['symlink'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
        ''',
    ),
    Rules.new(
        'symlink %s{filename} substitution',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['166:0'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="%s{dev}"
        ''',
    ),
    Rules.new(
        'program result substitution (numbered part of)',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5',
            exp_links=['link1', 'link2'],
            not_exp_links=['node'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
        ''',
    ),
    Rules.new(
        'program result substitution (numbered part of+)',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5',
            exp_links=['link1', 'link2', 'link3', 'link4'],
            not_exp_links=['node'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
        ''',
    ),
    Rules.new(
        'SUBSYSTEM match test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node'],
            not_exp_links=['should_not_match', 'should_not_match2'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", SUBSYSTEM=="vc"
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", SUBSYSTEM=="block"
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match2", SUBSYSTEM=="vc"
        ''',
    ),
    Rules.new(
        'DRIVERS match test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node'],
            not_exp_links=['should_not_match'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", DRIVERS=="sd-wrong"
        SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", DRIVERS=="sd"
        ''',
    ),
    Rules.new(
        'devnode substitution test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" SYMLINK+="node"
        ''',
    ),
    Rules.new(
        'parent node name substitution test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['sda-part-1'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="%P-part-%n"
        ''',
    ),
    Rules.new(
        'udev_root substitution',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['start-/dev-end'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
        ''',
    ),
    Rules.new(
        # This is not supported any more
        'last_rule option',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['last', 'very-last'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="very-last"
        ''',
    ),
    Rules.new(
        'negation KERNEL!=',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['match', 'before'],
            not_exp_links=['matches-but-is-negated'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
        SUBSYSTEMS=="scsi", KERNEL!="xsda1", SYMLINK+="match"
        ''',
    ),
    Rules.new(
        'negation SUBSYSTEM!=',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['before', 'not-anything'],
            not_exp_links=['matches-but-is-negated'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
        SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", SYMLINK+="not-anything"
        ''',
    ),
    Rules.new(
        'negation PROGRAM!= exit code',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['before', 'nonzero-program'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
        KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
        ''',
    ),
    Rules.new(
        'ENV{} test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['true'],
            not_exp_links=['bad', 'wrong'],
        ),
        rules=r'''
        ENV{ENV_KEY_TEST}="test"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'ENV{} test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['true'],
            not_exp_links=['bad', 'wrong', 'no'],
        ),
        rules=r'''
        ENV{ENV_KEY_TEST}="test"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
        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"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'ENV{} test (assign)',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['true', 'before'],
            not_exp_links=['no'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
        ''',
    ),
    Rules.new(
        'ENV{} test (assign 2 times)',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['true', 'before'],
            not_exp_links=['no', 'bad'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-$env{ASSIGN}"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="bad"
        SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
        ''',
    ),
    Rules.new(
        'ENV{} test (assign2)',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['part'],
            not_exp_links=['disk'],
        ),
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['disk'],
            not_exp_links=['part'],
        ),
        rules=r'''
        SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
        SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
        ENV{MAINDEVICE}=="true", SYMLINK+="disk"
        SUBSYSTEM=="block", SYMLINK+="before"
        ENV{PARTITION}=="true", SYMLINK+="part"
        ''',
    ),
    Rules.new(
        'untrusted string sanitize',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['sane'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
        ''',
    ),
    Rules.new(
        "untrusted string sanitize (don't replace utf8)",
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['uber'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \xc3\xbcber" RESULT=="über", SYMLINK+="uber"
        ''',
    ),
    Rules.new(
        'untrusted string sanitize (replace invalid utf8)',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['replaced'],
        ),
        rules=r'''
        SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \xef\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
        ''',
    ),
    Rules.new(
        'read sysfs value from parent device',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['serial-354172020305000'],
        ),
        rules=r'''
        KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
        ''',
    ),
    Rules.new(
        'match against empty key string',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['ok'],
            not_exp_links=['not-1-ok', 'not-2-ok', 'not-3-ok'],
        ),
        rules=r'''
        KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
        KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
        KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
        KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
        ''',
    ),
    Rules.new(
        'check ACTION value',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['ok'],
            not_exp_links=['unknown-not-ok'],
        ),
        rules=r'''
        ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
        ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
        ''',
    ),
    Rules.new(
        'final assignment',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['ok'],
            exp_perms='root:tty:0640',
        ),
        rules=r'''
        KERNEL=="sda", GROUP:="tty"
        KERNEL=="sda", GROUP="root", MODE="0640", SYMLINK+="ok"
        ''',
    ),
    Rules.new(
        'final assignment 2',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['ok'],
            exp_perms='root:tty:0640',
        ),
        rules=r'''
        KERNEL=="sda", GROUP:="tty"
        SUBSYSTEM=="block", MODE:="640"
        KERNEL=="sda", GROUP="root", MODE="0666", SYMLINK+="ok"
        ''',
    ),
    Rules.new(
        'env substitution',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['node-add-me'],
        ),
        rules=r'''
        KERNEL=="sda", MODE="0666", SYMLINK+="node-$env{ACTION}-me"
        ''',
    ),
    Rules.new(
        'reset list to current value',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['three'],
            not_exp_links=['two', 'one'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
        KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
        KERNEL=="ttyACM[0-9]*", SYMLINK="three"
        ''',
    ),
    Rules.new(
        'test empty SYMLINK+ (empty override)',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['right'],
            not_exp_links=['wrong'],
        ),
        rules=r'''
        KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
        KERNEL=="ttyACM[0-9]*", SYMLINK=""
        KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
        ''',
    ),
    Rules.new(
        'test multi matches',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['right', 'before'],
        ),
        rules=r'''
        KERNEL=="ttyACM*", SYMLINK+="before"
        KERNEL=="ttyACM*|nothing", SYMLINK+="right"
        ''',
    ),
    Rules.new(
        'test multi matches 2',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['right', 'before'],
            not_exp_links=['nomatch'],
        ),
        rules=r'''
        KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
        KERNEL=="ttyACM*", SYMLINK+="before"
        KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
        ''',
    ),
    Rules.new(
        'test multi matches 3',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['right'],
            not_exp_links=['nomatch', 'wrong1', 'wrong2'],
        ),
        rules=r'''
        KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
        KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
        KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
        KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
        ''',
    ),
    Rules.new(
        'test multi matches 4',
        Device(
            '/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0',
            exp_links=['right'],
            not_exp_links=['nomatch', 'wrong1', 'wrong2', 'wrong3'],
        ),
        rules=r'''
        KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
        KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
        KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
        KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
        KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
        ''',
    ),
    Rules.new(
        'test multi matches 5',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", TAG="foo"
        TAGS=="|foo", SYMLINK+="found"
        TAGS=="|aaa", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'test multi matches 6',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", ENV{HOGE}=""
        ENV{HOGE}=="|foo", SYMLINK+="found"
        ENV{HOGE}=="aaa|bbb", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'test multi matches 7',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", TAG="foo"
        TAGS=="foo||bar", SYMLINK+="found"
        TAGS=="aaa||bbb", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'test multi matches 8',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", ENV{HOGE}=""
        ENV{HOGE}=="foo||bar", SYMLINK+="found"
        ENV{HOGE}=="aaa|bbb", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'test multi matches 9',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found', 'found2'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", TAG="foo"
        TAGS=="foo|", SYMLINK+="found"
        TAGS=="aaa|", SYMLINK+="bad"
        KERNEL=="sda", TAGS!="hoge", SYMLINK+="found2"
        KERNEL=="sda", TAGS!="foo", SYMLINK+="bad2"
        ''',
    ),
    Rules.new(
        'test multi matches 10',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", ENV{HOGE}=""
        ENV{HOGE}=="foo|", SYMLINK+="found"
        ENV{HOGE}=="aaa|bbb", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'test multi matches 11',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['found'],
            not_exp_links=['bad'],
        ),
        rules=r'''
        KERNEL=="sda", TAG="c"
        TAGS=="foo||bar||c", SYMLINK+="found"
        TAGS=="aaa||bbb||ccc", SYMLINK+="bad"
        ''',
    ),
    Rules.new(
        'TAG refuses invalid string',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['valid', 'found'],
            not_exp_links=['empty', 'invalid_char', 'path', 'bad', 'bad2'],
        ),
        rules=r'''
       KERNEL=="sda", TAG+="", TAG+="invalid.char", TAG+="path/is/also/invalid", TAG+="valid"
       TAGS=="", SYMLINK+="empty"
       TAGS=="invalid.char", SYMLINK+="invalid_char"
       TAGS=="path/is/also/invalid", SYMLINK+="path"
       TAGS=="valid", SYMLINK+="valid"
       TAGS=="valid|", SYMLINK+="found"
       TAGS=="aaa|", SYMLINK+="bad"
       TAGS=="aaa|bbb", SYMLINK+="bad2"
        ''',
    ),
    Rules.new(
        'IMPORT parent test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['parent'],
        ),
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['parentenv-parent_right'],
        ),
        delay=500000,  # Serialized! We need to sleep here after adding sda
        rules=r'''
        KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-$env{PARENT_KEY}$env{WRONG_PARENT_KEY}"
        KERNEL=="sda", IMPORT{program}="/bin/echo -e 'PARENT_KEY=parent_right\nWRONG_PARENT_KEY=parent_wrong'"
        KERNEL=="sda", SYMLINK+="parent"
        ''',
    ),
    Rules.new(
        'GOTO test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['right'],
            not_exp_links=['wrong', 'wrong2'],
        ),
        rules=r'''
        KERNEL=="sda1", GOTO="TEST"
        KERNEL=="sda1", SYMLINK+="wrong"
        KERNEL=="sda1", GOTO="BAD"
        KERNEL=="sda1", SYMLINK+="", LABEL="NO"
        KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
        KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
        LABEL="end"
        ''',
    ),
    Rules.new(
        'GOTO label does not exist',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['right'],
        ),
        rules=r'''
        KERNEL=="sda1", GOTO="does-not-exist"
        KERNEL=="sda1", SYMLINK+="right",
        LABEL="exists"
        ''',
    ),
    Rules.new(
        'SYMLINK+ compare test',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['right', 'link'],
            not_exp_links=['wrong'],
        ),
        rules=r'''
        KERNEL=="sda1", SYMLINK+="link"
        KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
        KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
        ''',
    ),
    Rules.new(
        'invalid key operation',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['yes'],
            not_exp_links=['no'],
        ),
        rules=r'''
        KERNEL="sda1", SYMLINK+="no"
        KERNEL=="sda1", SYMLINK+="yes"
        ''',
    ),
    Rules.new(
        'operator chars in attribute',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['yes'],
        ),
        rules=r'''
        KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
        ''',
    ),
    Rules.new(
        'overlong comment line',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1',
            exp_links=['yes'],
            not_exp_links=['no'],
        ),
        rules=r'''
        # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
           # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
        KERNEL=="sda1", SYMLINK+=="no"
        KERNEL=="sda1", SYMLINK+="yes"
        ''',
    ),
    Rules.new(
        'magic subsys/kernel lookup',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['00:16:41:e2:8d:ff'],
        ),
        rules=r'''
        KERNEL=="sda", SYMLINK+="$attr{[net/eth0]address}"
        ''',
    ),
    Rules.new(
        'TEST absolute path',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['there'],
            not_exp_links=['notthere'],
        ),
        rules=r'''
        TEST=="/etc/passwd", SYMLINK+="there"
        TEST!="/etc/passwd", SYMLINK+="notthere"
        ''',
    ),
    Rules.new(
        'TEST subsys/kernel lookup',
        Device(
            '/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda',
            exp_links=['yes'],
        ),
        rules=r'''
        KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
        ''',
    ),
    Rules.new(