]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
Merge pull request #25168 from valentindavid/valentindavid/umount-move-recursive...
[thirdparty/systemd.git] / test / udev-test.pl
CommitLineData
2a5fcfae 1#!/usr/bin/env perl
8f5bcd61 2# SPDX-License-Identifier: LGPL-2.1-or-later
a367f04e 3
438d4c3c 4# udev test
a367f04e
GKH
5#
6# Provides automated testing of the udev binary.
7# The whole test is self contained in this file, except the matching sysfs tree.
8# Simply extend the @tests array, to add a new test variant.
9#
10# Every test is driven by its own temporary config file.
11# This program prepares the environment, creates the config and calls udev.
12#
65005a7f 13# udev parses the rules, looks at the provided sysfs and
a367f04e
GKH
14# first creates and then removes the device node.
15# After creation and removal the result is checked against the
16# expected value and the result is printed.
17#
810adae9 18# Copyright © 2004 Leann Ogasawara <ogasawara@osdl.org>
a367f04e
GKH
19
20use warnings;
21use strict;
d4076383
ZJS
22
23BEGIN {
24 my $EXIT_TEST_SKIP = 77;
25
26 unless (eval "use POSIX qw(WIFEXITED WEXITSTATUS);
27 use Cwd qw(getcwd abs_path);
28 use IPC::Semaphore;
29 use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR IPC_CREAT);
30 use Time::HiRes qw(usleep); 1") {
31 warn "Failed to import dependencies, skipping the test: $@";
32 exit($EXIT_TEST_SKIP);
33 }
34}
a367f04e 35
a7910612
LP
36# Relax sd-device's sysfs verification, since we want to provide a fake sysfs
37# here that actually is a tmpfs.
38$ENV{"SYSTEMD_DEVICE_VERIFY_SYSFS"}="0";
39
9b80f05f 40my $udev_bin = "./test-udev";
912541b0 41my $valgrind = 0;
333e07b7 42my $gdb = 0;
587751eb 43my $strace = 0;
b3f24900 44my $udev_bin_valgrind = "valgrind --tool=memcheck --leak-check=yes --track-origins=yes --quiet $udev_bin";
333e07b7 45my $udev_bin_gdb = "gdb --args $udev_bin";
587751eb 46my $udev_bin_strace = "strace -efile $udev_bin";
6ada823a 47my $udev_run = "test/run";
2ce8d27b 48my $udev_tmpfs = "test/tmpfs";
21d9e3f3
EV
49my $udev_sys = "${udev_tmpfs}/sys";
50my $udev_dev = "${udev_tmpfs}/dev";
6ada823a
KS
51my $udev_rules_dir = "$udev_run/udev/rules.d";
52my $udev_rules = "$udev_rules_dir/udev-test.rules";
0eb3cc88 53my $EXIT_TEST_SKIP = 77;
a367f04e 54
bf8f7583
MP
55my $rules_10k_tags = "";
56for (my $i = 1; $i <= 10000; ++$i) {
d3fc8bf4 57 $rules_10k_tags .= 'KERNEL=="sda", TAG+="test' . $i . "\"\n";
bf8f7583
MP
58}
59
e37a5d90
YW
60my $rules_10k_tags_continuation = "KERNEL==\"sda\", \\\n";
61for (my $i = 1; $i < 10000; ++$i) {
62 $rules_10k_tags_continuation .= 'TAG+="test' . $i . "\",\\\n";
1e797cf5 63}
e37a5d90 64$rules_10k_tags_continuation .= "TAG+=\"test10000\"\\n";
1e797cf5 65
eb44d715
MW
66# Create a device list with all block devices under /sys
67# (except virtual devices and cd-roms)
68# the optional argument exp_func returns expected and non-expected
69# symlinks for the device.
70sub all_block_devs {
71 my ($exp_func) = @_;
72 my @devices;
73
74 foreach my $bd (glob "$udev_sys/dev/block/*") {
75 my $tgt = readlink($bd);
76 my ($exp, $notexp) = (undef, undef);
77
78 next if ($tgt =~ m!/virtual/! || $tgt =~ m!/sr[0-9]*$!);
79
80 $tgt =~ s!^\.\./\.\.!!;
81 ($exp, $notexp) = $exp_func->($tgt) if defined($exp_func);
82 my $device = {
83 devpath => $tgt,
84 exp_links => $exp,
85 not_exp_links => $notexp,
86 };
87 push(@devices, $device);
88 }
89 return \@devices;
90}
91
92# This generator returns a suitable exp_func for use with
93# all_block_devs().
94sub expect_for_some {
95 my ($pattern, $links, $donot) = @_;
96 my $_expect = sub {
97 my ($name) = @_;
98
99 if ($name =~ /$pattern/) {
100 return ($links, undef);
101 } elsif ($donot) {
102 return (undef, $links);
103 } else {
104 return (undef, undef);
105 }
106 };
107 return $_expect;
108}
109
a367f04e 110my @tests = (
912541b0
KS
111 {
112 desc => "no rules",
255c05b7
MW
113 devices => [
114 {
115 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
116 exp_rem_error => "yes",
117 },
118 {
119 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
255c05b7
MW
120 exp_rem_error => "yes",
121 }],
912541b0 122 rules => <<EOF
5754e74c 123#
bcf44d55 124EOF
912541b0
KS
125 },
126 {
127 desc => "label test of scsi disc",
255c05b7
MW
128 devices => [
129 {
130 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 131 exp_links => ["boot_disk"],
255c05b7 132 }],
912541b0 133 rules => <<EOF
0f52fdee 134SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
5754e74c 135KERNEL=="ttyACM0", SYMLINK+="modem"
c4edd0ad 136EOF
912541b0
KS
137 },
138 {
139 desc => "label test of scsi disc",
255c05b7
MW
140 devices => [
141 {
142 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 143 exp_links => ["boot_disk"],
255c05b7 144 }],
912541b0 145 rules => <<EOF
5754e74c
KS
146SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
147KERNEL=="ttyACM0", SYMLINK+="modem"
c4edd0ad 148EOF
912541b0
KS
149 },
150 {
151 desc => "label test of scsi disc",
255c05b7
MW
152 devices => [
153 {
154 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 155 exp_links => ["boot_disk"],
255c05b7 156 }],
912541b0 157 rules => <<EOF
5754e74c
KS
158SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
159KERNEL=="ttyACM0", SYMLINK+="modem"
a367f04e 160EOF
912541b0
KS
161 },
162 {
163 desc => "label test of scsi partition",
255c05b7
MW
164 devices => [
165 {
166 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 167 exp_links => ["boot_disk1"],
255c05b7 168 }],
912541b0 169 rules => <<EOF
5754e74c 170SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
83be97ba 171EOF
912541b0
KS
172 },
173 {
174 desc => "label test of pattern match",
255c05b7
MW
175 devices => [
176 {
177 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 178 exp_links => ["boot_disk1", "boot_disk1-4", "boot_disk1-5"],
d94802e9 179 not_exp_links => ["boot_disk1-1", "boot_disk1-2", "boot_disk1-3", "boot_disk1-6", "boot_disk1-7"]
255c05b7 180 }],
912541b0 181 rules => <<EOF
5754e74c
KS
182SUBSYSTEMS=="scsi", ATTRS{vendor}=="?ATA", SYMLINK+="boot_disk%n-1"
183SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA?", SYMLINK+="boot_disk%n-2"
184SUBSYSTEMS=="scsi", ATTRS{vendor}=="A??", SYMLINK+="boot_disk%n"
185SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATAS", SYMLINK+="boot_disk%n-3"
e62acc31
MW
186SUBSYSTEMS=="scsi", ATTRS{vendor}=="AT?", SYMLINK+="boot_disk%n-4"
187SUBSYSTEMS=="scsi", ATTRS{vendor}=="??A", SYMLINK+="boot_disk%n-5"
d94802e9
YW
188SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", GOTO="skip-6"
189SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n-6"
190LABEL="skip-6"
191SUBSYSTEMS=="scsi", GOTO="skip-7"
192SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n-7"
193LABEL="skip-7"
358c8c20 194EOF
912541b0
KS
195 },
196 {
197 desc => "label test of multiple sysfs files",
255c05b7
MW
198 devices => [
199 {
200 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
201 exp_links => ["boot_disk1"],
202 not_exp_links => ["boot_diskX1"],
255c05b7 203 }],
912541b0 204 rules => <<EOF
5754e74c
KS
205SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS X ", SYMLINK+="boot_diskX%n"
206SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", SYMLINK+="boot_disk%n"
358c8c20 207EOF
912541b0
KS
208 },
209 {
210 desc => "label test of max sysfs files (skip invalid rule)",
255c05b7
MW
211 devices => [
212 {
213 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 214 exp_links => ["boot_disk1", "boot_diskXY1"],
d314345a 215 not_exp_links => ["boot_diskXX1", "hoge"],
255c05b7 216 }],
912541b0 217 rules => <<EOF
5754e74c 218SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", ATTRS{queue_depth}=="32", SYMLINK+="boot_diskXX%n"
e62acc31 219SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", ATTRS{queue_depth}=="1", SYMLINK+="boot_diskXY%n"
d314345a
YW
220SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", SYMLINK+="boot_disk%n", SYMLINK+="hoge"
221SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", SYMLINK-="hoge"
0db6d4cc 222EOF
912541b0
KS
223 },
224 {
225 desc => "catch device by *",
255c05b7
MW
226 devices => [
227 {
228 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 229 exp_links => ["modem/0", "catch-all"],
255c05b7 230 }],
912541b0 231 rules => <<EOF
5754e74c 232KERNEL=="ttyACM*", SYMLINK+="modem/%n"
e62acc31 233KERNEL=="*", SYMLINK+="catch-all"
2e317184 234EOF
912541b0 235 },
e62acc31 236 # 10
912541b0
KS
237 {
238 desc => "catch device by * - take 2",
255c05b7
MW
239 devices => [
240 {
241 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
242 exp_links => ["modem/0"],
243 not_exp_links => ["bad"],
255c05b7 244 }],
912541b0 245 rules => <<EOF
5754e74c
KS
246KERNEL=="*ACM1", SYMLINK+="bad"
247KERNEL=="*ACM0", SYMLINK+="modem/%n"
9f1da361 248EOF
912541b0
KS
249 },
250 {
251 desc => "catch device by ?",
255c05b7
MW
252 devices => [
253 {
254 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
255 exp_links => ["modem/0"],
256 not_exp_links => ["modem/0-1", "modem/0-2"],
255c05b7 257 }],
912541b0 258 rules => <<EOF
5754e74c
KS
259KERNEL=="ttyACM??*", SYMLINK+="modem/%n-1"
260KERNEL=="ttyACM??", SYMLINK+="modem/%n-2"
261KERNEL=="ttyACM?", SYMLINK+="modem/%n"
9f1da361 262EOF
912541b0
KS
263 },
264 {
265 desc => "catch device by character class",
255c05b7
MW
266 devices => [
267 {
268 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
269 exp_links => ["modem/0"],
270 not_exp_links => ["modem/0-1", "modem/0-2"],
255c05b7 271 }],
912541b0 272 rules => <<EOF
5754e74c
KS
273KERNEL=="ttyACM[A-Z]*", SYMLINK+="modem/%n-1"
274KERNEL=="ttyACM?[0-9]", SYMLINK+="modem/%n-2"
275KERNEL=="ttyACM[0-9]*", SYMLINK+="modem/%n"
a367f04e 276EOF
912541b0
KS
277 },
278 {
46bc71b2 279 desc => "don't replace kernel name",
255c05b7
MW
280 devices => [
281 {
282 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 283 exp_links => ["modem"],
255c05b7 284 }],
912541b0 285 rules => <<EOF
5754e74c 286KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a 287EOF
912541b0
KS
288 },
289 {
46bc71b2 290 desc => "Handle comment lines in config file (and don't replace kernel name)",
255c05b7
MW
291 devices => [
292 {
293 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 294 exp_links => ["modem"],
255c05b7 295 }],
912541b0 296 rules => <<EOF
281ff00a 297# this is a comment
5754e74c 298KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a
GKH
299
300EOF
912541b0
KS
301 },
302 {
46bc71b2 303 desc => "Handle comment lines in config file with whitespace (and don't replace kernel name)",
255c05b7
MW
304 devices => [
305 {
306 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 307 exp_links => ["modem"],
255c05b7 308 }],
912541b0 309 rules => <<EOF
d914e445 310 # this is a comment with whitespace before the comment
5754e74c 311KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a 312
3db7fa27 313EOF
912541b0
KS
314 },
315 {
46bc71b2 316 desc => "Handle whitespace only lines (and don't replace kernel name)",
255c05b7
MW
317 devices => [
318 {
319 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 320 exp_links => ["whitespace"],
255c05b7 321 }],
912541b0 322 rules => <<EOF
3db7fa27 323
3db7fa27 324
d914e445
KS
325
326 # this is a comment with whitespace before the comment
5754e74c 327KERNEL=="ttyACM0", SYMLINK+="whitespace"
3db7fa27 328
d914e445 329
3db7fa27 330
281ff00a 331EOF
912541b0
KS
332 },
333 {
46bc71b2 334 desc => "Handle empty lines in config file (and don't replace kernel name)",
255c05b7
MW
335 devices => [
336 {
337 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 338 exp_links => ["modem"],
255c05b7 339 }],
912541b0 340 rules => <<EOF
281ff00a 341
5754e74c 342KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a 343
9f8dfa19 344EOF
912541b0
KS
345 },
346 {
46bc71b2 347 desc => "Handle backslashed multi lines in config file (and don't replace kernel name)",
255c05b7
MW
348 devices => [
349 {
350 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 351 exp_links => ["modem"],
255c05b7 352 }],
912541b0 353 rules => <<EOF
c7fcba1b 354KERNEL=="ttyACM0", \\
5754e74c 355SYMLINK+="modem"
9f8dfa19 356
77313cd0 357EOF
912541b0
KS
358 },
359 {
360 desc => "preserve backslashes, if they are not for a newline",
255c05b7
MW
361 devices => [
362 {
363 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 364 exp_links => ["aaa"],
255c05b7 365 }],
912541b0 366 rules => <<EOF
d914e445 367KERNEL=="ttyACM0", PROGRAM=="/bin/echo -e \\101", RESULT=="A", SYMLINK+="aaa"
9f8dfa19 368EOF
912541b0 369 },
46bc71b2 370 # 20
912541b0 371 {
46bc71b2 372 desc => "Handle stupid backslashed multi lines in config file (and don't replace kernel name)",
255c05b7
MW
373 devices => [
374 {
375 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 376 exp_links => ["modem"],
255c05b7 377 }],
912541b0 378 rules => <<EOF
9f8dfa19
KS
379
380#
381\\
382
d960ad15 383\\
9f8dfa19
KS
384
385#\\
386
c7fcba1b 387KERNEL=="ttyACM0", \\
912541b0 388 SYMLINK+="modem"
9f8dfa19 389
5499d319 390EOF
912541b0
KS
391 },
392 {
393 desc => "subdirectory handling",
255c05b7
MW
394 devices => [
395 {
396 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 397 exp_links => ["sub/direct/ory/modem"],
255c05b7 398 }],
912541b0 399 rules => <<EOF
5754e74c 400KERNEL=="ttyACM0", SYMLINK+="sub/direct/ory/modem"
a367f04e 401EOF
912541b0
KS
402 },
403 {
404 desc => "parent device name match of scsi partition",
255c05b7
MW
405 devices => [
406 {
407 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 408 exp_links => ["first_disk5"],
255c05b7 409 }],
912541b0 410 rules => <<EOF
5754e74c 411SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="first_disk%n"
c4edd0ad 412EOF
912541b0
KS
413 },
414 {
415 desc => "test substitution chars",
255c05b7
MW
416 devices => [
417 {
418 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 419 exp_links => ["Major:8:minor:5:kernelnumber:5:id:0:0:0:0"],
255c05b7 420 }],
912541b0 421 rules => <<EOF
5754e74c 422SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:%M:minor:%m:kernelnumber:%n:id:%b"
319c6700 423EOF
912541b0
KS
424 },
425 {
426 desc => "import of shell-value returned from program",
255c05b7
MW
427 devices => [
428 {
429 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 430 exp_links => ["node12345678"],
255c05b7 431 }],
912541b0 432 rules => <<EOF
d914e445 433SUBSYSTEMS=="scsi", IMPORT{program}="/bin/echo -e \' TEST_KEY=12345678\\n TEST_key2=98765\'", SYMLINK+="node\$env{TEST_KEY}"
5754e74c 434KERNEL=="ttyACM0", SYMLINK+="modem"
a27cd06c 435EOF
912541b0
KS
436 },
437 {
d3f044dd 438 desc => "substitution of sysfs value (%s{file})",
255c05b7
MW
439 devices => [
440 {
441 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
442 exp_links => ["disk-ATA-sda"],
443 not_exp_links => ["modem"],
255c05b7 444 }],
912541b0 445 rules => <<EOF
5754e74c
KS
446SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="disk-%s{vendor}-%k"
447KERNEL=="ttyACM0", SYMLINK+="modem"
a367f04e 448EOF
912541b0
KS
449 },
450 {
451 desc => "program result substitution",
255c05b7
MW
452 devices => [
453 {
454 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
455 exp_links => ["special-device-5"],
456 not_exp_links => ["not"],
255c05b7 457 }],
912541b0 458 rules => <<EOF
d914e445
KS
459SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="-special-*", SYMLINK+="not"
460SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-*", SYMLINK+="%c-%n"
bbbe503e 461EOF
912541b0
KS
462 },
463 {
464 desc => "program result substitution (newline removal)",
255c05b7
MW
465 devices => [
466 {
467 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 468 exp_links => ["newline_removed"],
255c05b7 469 }],
912541b0 470 rules => <<EOF
d914e445 471SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo test", RESULT=="test", SYMLINK+="newline_removed"
f3b04a2e 472EOF
912541b0
KS
473 },
474 {
475 desc => "program result substitution",
255c05b7
MW
476 devices => [
477 {
478 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 479 exp_links => ["test-0:0:0:0"],
255c05b7 480 }],
912541b0 481 rules => <<EOF
d914e445 482SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n test-%b", RESULT=="test-0:0*", SYMLINK+="%c"
dde05ccb 483EOF
912541b0
KS
484 },
485 {
486 desc => "program with lots of arguments",
255c05b7
MW
487 devices => [
488 {
489 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
490 exp_links => ["foo9"],
491 not_exp_links => ["foo3", "foo4", "foo5", "foo6", "foo7", "foo8"],
255c05b7 492 }],
912541b0 493 rules => <<EOF
d914e445 494SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="%c{7}"
35b38379 495EOF
912541b0
KS
496 },
497 {
498 desc => "program with subshell",
255c05b7
MW
499 devices => [
500 {
501 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
502 exp_links => ["bar9"],
503 not_exp_links => ["foo3", "foo4", "foo5", "foo6", "foo7", "foo8"],
255c05b7 504 }],
912541b0 505 rules => <<EOF
d914e445 506SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL=="sda5", SYMLINK+="%c{7}"
35b38379 507EOF
912541b0
KS
508 },
509 {
510 desc => "program arguments combined with apostrophes",
255c05b7
MW
511 devices => [
512 {
513 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
514 exp_links => ["foo7"],
515 not_exp_links => ["foo3", "foo4", "foo5", "foo6", "foo8"],
255c05b7 516 }],
912541b0 517 rules => <<EOF
d914e445 518SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'", KERNEL=="sda5", SYMLINK+="%c{5}"
7e760b79
FB
519EOF
520 },
521 {
522 desc => "program arguments combined with escaped double quotes, part 1",
255c05b7
MW
523 devices => [
524 {
525 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
526 exp_links => ["foo2"],
527 not_exp_links => ["foo1"],
255c05b7 528 }],
7e760b79
FB
529 rules => <<EOF
530SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'printf %%s \\\"foo1 foo2\\\" | grep \\\"foo1 foo2\\\"'", KERNEL=="sda5", SYMLINK+="%c{2}"
531EOF
532 },
533 {
534 desc => "program arguments combined with escaped double quotes, part 2",
255c05b7
MW
535 devices => [
536 {
537 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
538 exp_links => ["foo2"],
539 not_exp_links => ["foo1"],
255c05b7 540 }],
7e760b79
FB
541 rules => <<EOF
542SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c \\\"printf %%s 'foo1 foo2' | grep 'foo1 foo2'\\\"", KERNEL=="sda5", SYMLINK+="%c{2}"
543EOF
544 },
545 {
546 desc => "program arguments combined with escaped double quotes, part 3",
255c05b7
MW
547 devices => [
548 {
549 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
550 exp_links => ["foo2"],
551 not_exp_links => ["foo1", "foo3"],
255c05b7 552 }],
7e760b79
FB
553 rules => <<EOF
554SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'printf \\\"%%s %%s\\\" \\\"foo1 foo2\\\" \\\"foo3\\\"| grep \\\"foo1 foo2\\\"'", KERNEL=="sda5", SYMLINK+="%c{2}"
56c963dc 555EOF
912541b0
KS
556 },
557 {
558 desc => "characters before the %c{N} substitution",
255c05b7
MW
559 devices => [
560 {
561 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 562 exp_links => ["my-foo9"],
255c05b7 563 }],
912541b0 564 rules => <<EOF
d914e445 565SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{7}"
56c963dc 566EOF
912541b0
KS
567 },
568 {
569 desc => "substitute the second to last argument",
255c05b7
MW
570 devices => [
571 {
572 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
573 exp_links => ["my-foo8"],
574 not_exp_links => ["my-foo3", "my-foo4", "my-foo5", "my-foo6", "my-foo7", "my-foo9"],
255c05b7 575 }],
912541b0 576 rules => <<EOF
d914e445 577SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{6}"
bf5d2964 578EOF
912541b0
KS
579 },
580 {
581 desc => "test substitution by variable name",
255c05b7
MW
582 devices => [
583 {
584 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 585 exp_links => ["Major:8-minor:5-kernelnumber:5-id:0:0:0:0"],
255c05b7 586 }],
912541b0 587 rules => <<EOF
5754e74c 588SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:\$major-minor:\$minor-kernelnumber:\$number-id:\$id"
bf5d2964 589EOF
912541b0
KS
590 },
591 {
592 desc => "test substitution by variable name 2",
255c05b7
MW
593 devices => [
594 {
595 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 596 exp_links => ["Major:8-minor:5-kernelnumber:5-id:0:0:0:0"],
255c05b7 597 }],
912541b0 598 rules => <<EOF
5754e74c 599SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="Major:\$major-minor:%m-kernelnumber:\$number-id:\$id"
bf5d2964 600EOF
912541b0
KS
601 },
602 {
603 desc => "test substitution by variable name 3",
255c05b7
MW
604 devices => [
605 {
606 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 607 exp_links => ["850:0:0:05"],
255c05b7 608 }],
912541b0 609 rules => <<EOF
5754e74c 610SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="%M%m%b%n"
bf5d2964 611EOF
912541b0
KS
612 },
613 {
614 desc => "test substitution by variable name 4",
255c05b7
MW
615 devices => [
616 {
617 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 618 exp_links => ["855"],
255c05b7 619 }],
912541b0 620 rules => <<EOF
5754e74c 621SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="\$major\$minor\$number"
bf5d2964 622EOF
912541b0
KS
623 },
624 {
625 desc => "test substitution by variable name 5",
255c05b7
MW
626 devices => [
627 {
628 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 629 exp_links => ["8550:0:0:0"],
255c05b7 630 }],
912541b0 631 rules => <<EOF
5754e74c 632SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="\$major%m%n\$id"
8ff8bbba 633EOF
912541b0
KS
634 },
635 {
636 desc => "non matching SUBSYSTEMS for device with no parent",
255c05b7
MW
637 devices => [
638 {
639 devpath => "/devices/virtual/tty/console",
e62acc31
MW
640 exp_links => ["TTY"],
641 not_exp_links => ["foo"],
255c05b7 642 }],
912541b0 643 rules => <<EOF
d914e445 644SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo", RESULT=="foo", SYMLINK+="foo"
5754e74c 645KERNEL=="console", SYMLINK+="TTY"
1d936fbc 646EOF
912541b0
KS
647 },
648 {
649 desc => "non matching SUBSYSTEMS",
255c05b7
MW
650 devices => [
651 {
652 devpath => "/devices/virtual/tty/console",
e62acc31
MW
653 exp_links => ["TTY"],
654 not_exp_links => ["foo"],
255c05b7 655 }],
912541b0 656 rules => <<EOF
5754e74c
KS
657SUBSYSTEMS=="foo", ATTRS{dev}=="5:1", SYMLINK+="foo"
658KERNEL=="console", SYMLINK+="TTY"
64682333 659EOF
912541b0
KS
660 },
661 {
662 desc => "ATTRS match",
255c05b7
MW
663 devices => [
664 {
665 devpath => "/devices/virtual/tty/console",
e62acc31 666 exp_links => ["foo", "TTY"],
255c05b7 667 }],
912541b0 668 rules => <<EOF
5754e74c
KS
669KERNEL=="console", SYMLINK+="TTY"
670ATTRS{dev}=="5:1", SYMLINK+="foo"
a402404f 671EOF
912541b0
KS
672 },
673 {
674 desc => "ATTR (empty file)",
255c05b7
MW
675 devices => [
676 {
677 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
678 exp_links => ["empty", "not-something"],
679 not_exp_links => ["something", "not-empty"],
255c05b7 680 }],
912541b0 681 rules => <<EOF
5754e74c
KS
682KERNEL=="sda", ATTR{test_empty_file}=="?*", SYMLINK+="something"
683KERNEL=="sda", ATTR{test_empty_file}!="", SYMLINK+="not-empty"
684KERNEL=="sda", ATTR{test_empty_file}=="", SYMLINK+="empty"
685KERNEL=="sda", ATTR{test_empty_file}!="?*", SYMLINK+="not-something"
a402404f 686EOF
912541b0
KS
687 },
688 {
689 desc => "ATTR (non-existent file)",
255c05b7
MW
690 devices => [
691 {
692 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
693 exp_links => ["non-existent", "wrong"],
694 not_exp_links => ["something", "empty", "not-empty",
695 "not-something", "something"],
255c05b7 696 }],
912541b0 697 rules => <<EOF
5754e74c
KS
698KERNEL=="sda", ATTR{nofile}=="?*", SYMLINK+="something"
699KERNEL=="sda", ATTR{nofile}!="", SYMLINK+="not-empty"
700KERNEL=="sda", ATTR{nofile}=="", SYMLINK+="empty"
701KERNEL=="sda", ATTR{nofile}!="?*", SYMLINK+="not-something"
702KERNEL=="sda", TEST!="nofile", SYMLINK+="non-existent"
703KERNEL=="sda", SYMLINK+="wrong"
772558f4 704EOF
912541b0
KS
705 },
706 {
707 desc => "program and bus type match",
255c05b7
MW
708 devices => [
709 {
710 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 711 exp_links => ["scsi-0:0:0:0"],
255c05b7 712 }],
912541b0 713 rules => <<EOF
d914e445
KS
714SUBSYSTEMS=="usb", PROGRAM=="/bin/echo -n usb-%b", SYMLINK+="%c"
715SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n scsi-%b", SYMLINK+="%c"
716SUBSYSTEMS=="foo", PROGRAM=="/bin/echo -n foo-%b", SYMLINK+="%c"
50e5de03 717EOF
912541b0
KS
718 },
719 {
720 desc => "sysfs parent hierarchy",
255c05b7
MW
721 devices => [
722 {
723 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 724 exp_links => ["modem"],
255c05b7 725 }],
912541b0 726 rules => <<EOF
5754e74c 727ATTRS{idProduct}=="007b", SYMLINK+="modem"
f0142622 728EOF
912541b0
KS
729 },
730 {
731 desc => "name test with ! in the name",
255c05b7
MW
732 devices => [
733 {
734 devpath => "/devices/virtual/block/fake!blockdev0",
f0dccf01 735 devnode => "fake/blockdev0",
e62acc31
MW
736 exp_links => ["is/a/fake/blockdev0"],
737 not_exp_links => ["is/not/a/fake/blockdev0", "modem"],
255c05b7 738 }],
912541b0 739 rules => <<EOF
5754e74c
KS
740SUBSYSTEMS=="scsi", SYMLINK+="is/not/a/%k"
741SUBSYSTEM=="block", SYMLINK+="is/a/%k"
742KERNEL=="ttyACM0", SYMLINK+="modem"
b9fc973b 743EOF
912541b0
KS
744 },
745 {
746 desc => "name test with ! in the name, but no matching rule",
255c05b7
MW
747 devices => [
748 {
749 devpath => "/devices/virtual/block/fake!blockdev0",
f0dccf01 750 devnode => "fake/blockdev0",
e62acc31 751 not_exp_links => ["modem"],
255c05b7 752 }],
912541b0 753 rules => <<EOF
5754e74c 754KERNEL=="ttyACM0", SYMLINK+="modem"
93656247 755EOF
912541b0
KS
756 },
757 {
758 desc => "KERNELS rule",
255c05b7
MW
759 devices => [
760 {
761 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
762 exp_links => ["scsi-0:0:0:0"],
763 not_exp_links => ["no-match", "short-id", "not-scsi"],
255c05b7 764 }],
912541b0 765 rules => <<EOF
5754e74c
KS
766SUBSYSTEMS=="usb", KERNELS=="0:0:0:0", SYMLINK+="not-scsi"
767SUBSYSTEMS=="scsi", KERNELS=="0:0:0:1", SYMLINK+="no-match"
768SUBSYSTEMS=="scsi", KERNELS==":0", SYMLINK+="short-id"
769SUBSYSTEMS=="scsi", KERNELS=="/0:0:0:0", SYMLINK+="no-match"
770SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="scsi-0:0:0:0"
93656247 771EOF
912541b0
KS
772 },
773 {
774 desc => "KERNELS wildcard all",
255c05b7
MW
775 devices => [
776 {
777 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
778 exp_links => ["scsi-0:0:0:0"],
779 not_exp_links => ["no-match", "before"],
255c05b7 780 }],
912541b0 781 rules => <<EOF
5754e74c
KS
782SUBSYSTEMS=="scsi", KERNELS=="*:1", SYMLINK+="no-match"
783SUBSYSTEMS=="scsi", KERNELS=="*:0:1", SYMLINK+="no-match"
784SUBSYSTEMS=="scsi", KERNELS=="*:0:0:1", SYMLINK+="no-match"
785SUBSYSTEMS=="scsi", KERNEL=="0:0:0:0", SYMLINK+="before"
786SUBSYSTEMS=="scsi", KERNELS=="*", SYMLINK+="scsi-0:0:0:0"
93656247 787EOF
912541b0
KS
788 },
789 {
790 desc => "KERNELS wildcard partial",
255c05b7
MW
791 devices => [
792 {
793 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 794 exp_links => ["scsi-0:0:0:0", "before"],
255c05b7 795 }],
912541b0 796 rules => <<EOF
5754e74c
KS
797SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
798SUBSYSTEMS=="scsi", KERNELS=="*:0", SYMLINK+="scsi-0:0:0:0"
93656247 799EOF
912541b0
KS
800 },
801 {
802 desc => "KERNELS wildcard partial 2",
255c05b7
MW
803 devices => [
804 {
805 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 806 exp_links => ["scsi-0:0:0:0", "before"],
255c05b7
MW
807 }],
808 rules => <<EOF
5754e74c
KS
809SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
810SUBSYSTEMS=="scsi", KERNELS=="*:0:0:0", SYMLINK+="scsi-0:0:0:0"
eef54479 811EOF
912541b0
KS
812 },
813 {
814 desc => "substitute attr with link target value (first match)",
255c05b7
MW
815 devices => [
816 {
817 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 818 exp_links => ["driver-is-sd"],
255c05b7 819 }],
912541b0 820 rules => <<EOF
5754e74c 821SUBSYSTEMS=="scsi", SYMLINK+="driver-is-\$attr{driver}"
eef54479 822EOF
912541b0
KS
823 },
824 {
825 desc => "substitute attr with link target value (currently selected device)",
255c05b7
MW
826 devices => [
827 {
828 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 829 exp_links => ["driver-is-ahci"],
255c05b7 830 }],
912541b0 831 rules => <<EOF
5754e74c 832SUBSYSTEMS=="pci", SYMLINK+="driver-is-\$attr{driver}"
d5f91372 833EOF
912541b0
KS
834 },
835 {
836 desc => "ignore ATTRS attribute whitespace",
255c05b7
MW
837 devices => [
838 {
839 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 840 exp_links => ["ignored"],
255c05b7 841 }],
912541b0 842 rules => <<EOF
5754e74c 843SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE", SYMLINK+="ignored"
d5f91372 844EOF
912541b0
KS
845 },
846 {
847 desc => "do not ignore ATTRS attribute whitespace",
255c05b7
MW
848 devices => [
849 {
850 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
851 exp_links => ["matched-with-space"],
852 not_exp_links => ["wrong-to-ignore"],
255c05b7 853 }],
912541b0 854 rules => <<EOF
5754e74c
KS
855SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE ", SYMLINK+="wrong-to-ignore"
856SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE ", SYMLINK+="matched-with-space"
0a5417a0 857EOF
912541b0
KS
858 },
859 {
860 desc => "permissions USER=bad GROUP=name",
255c05b7
MW
861 devices => [
862 {
863 devpath => "/devices/virtual/tty/tty33",
255c05b7
MW
864 exp_perms => "0:0:0600",
865 }],
912541b0 866 rules => <<EOF
6ada823a 867KERNEL=="tty33", OWNER="bad", GROUP="name"
b8669191 868EOF
912541b0
KS
869 },
870 {
9158d03e 871 desc => "permissions OWNER=1",
255c05b7
MW
872 devices => [
873 {
874 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 875 exp_links => ["node"],
255c05b7
MW
876 exp_perms => "1::0600",
877 }],
912541b0 878 rules => <<EOF
9158d03e 879SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1"
b8669191 880EOF
912541b0
KS
881 },
882 {
9158d03e 883 desc => "permissions GROUP=1",
255c05b7
MW
884 devices => [
885 {
886 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 887 exp_links => ["node"],
255c05b7
MW
888 exp_perms => ":1:0660",
889 }],
912541b0 890 rules => <<EOF
9158d03e 891SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="1"
9b434de1 892EOF
912541b0
KS
893 },
894 {
895 desc => "textual user id",
255c05b7
MW
896 devices => [
897 {
898 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 899 exp_links => ["node"],
255c05b7
MW
900 exp_perms => "daemon::0600",
901 }],
912541b0 902 rules => <<EOF
24a01950 903SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="daemon"
9b434de1 904EOF
912541b0
KS
905 },
906 {
907 desc => "textual group id",
255c05b7
MW
908 devices => [
909 {
910 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 911 exp_links => ["node"],
255c05b7
MW
912 exp_perms => ":daemon:0660",
913 }],
912541b0 914 rules => <<EOF
5754e74c 915SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="daemon"
c8278763 916EOF
912541b0
KS
917 },
918 {
919 desc => "textual user/group id",
255c05b7
MW
920 devices => [
921 {
922 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 923 exp_links => ["node"],
255c05b7
MW
924 exp_perms => "root:audio:0660",
925 }],
912541b0 926 rules => <<EOF
a9030b81 927SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="root", GROUP="audio"
b8669191 928EOF
912541b0
KS
929 },
930 {
931 desc => "permissions MODE=0777",
255c05b7
MW
932 devices => [
933 {
934 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 935 exp_links => ["node"],
255c05b7
MW
936 exp_perms => "::0777",
937 }],
912541b0 938 rules => <<EOF
5754e74c 939SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", MODE="0777"
b8669191 940EOF
912541b0
KS
941 },
942 {
9158d03e 943 desc => "permissions OWNER=1 GROUP=1 MODE=0777",
255c05b7
MW
944 devices => [
945 {
946 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 947 exp_links => ["node"],
255c05b7
MW
948 exp_perms => "1:1:0777",
949 }],
912541b0 950 rules => <<EOF
9158d03e 951SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1", GROUP="1", MODE="0777"
b8669191 952EOF
912541b0
KS
953 },
954 {
9158d03e 955 desc => "permissions OWNER to 1",
255c05b7
MW
956 devices => [
957 {
958 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
959 exp_perms => "1::",
960 }],
912541b0 961 rules => <<EOF
9158d03e 962KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1"
b8669191 963EOF
912541b0
KS
964 },
965 {
9158d03e 966 desc => "permissions GROUP to 1",
255c05b7
MW
967 devices => [
968 {
969 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
970 exp_perms => ":1:0660",
971 }],
912541b0 972 rules => <<EOF
9158d03e 973KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="1"
b8669191 974EOF
912541b0
KS
975 },
976 {
977 desc => "permissions MODE to 0060",
255c05b7
MW
978 devices => [
979 {
980 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
981 exp_perms => "::0060",
982 }],
912541b0 983 rules => <<EOF
5754e74c 984KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", MODE="0060"
b8669191 985EOF
912541b0
KS
986 },
987 {
988 desc => "permissions OWNER, GROUP, MODE",
255c05b7
MW
989 devices => [
990 {
991 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
992 exp_perms => "1:1:0777",
993 }],
912541b0 994 rules => <<EOF
9158d03e 995KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1", GROUP="1", MODE="0777"
e9390146 996EOF
912541b0
KS
997 },
998 {
999 desc => "permissions only rule",
255c05b7
MW
1000 devices => [
1001 {
1002 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
1003 exp_perms => "1:1:0777",
1004 }],
912541b0 1005 rules => <<EOF
9158d03e
TG
1006KERNEL=="ttyACM[0-9]*", OWNER="1", GROUP="1", MODE="0777"
1007KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
5754e74c 1008KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
eb870090 1009EOF
912541b0
KS
1010 },
1011 {
1012 desc => "multiple permissions only rule",
255c05b7
MW
1013 devices => [
1014 {
1015 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
1016 exp_perms => "1:1:0777",
1017 }],
912541b0 1018 rules => <<EOF
9158d03e
TG
1019SUBSYSTEM=="tty", OWNER="1"
1020SUBSYSTEM=="tty", GROUP="1"
28ce66de 1021SUBSYSTEM=="tty", MODE="0777"
9158d03e 1022KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
5754e74c 1023KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
eb870090 1024EOF
912541b0
KS
1025 },
1026 {
1027 desc => "permissions only rule with override at SYMLINK+ rule",
255c05b7
MW
1028 devices => [
1029 {
1030 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
1031 exp_perms => "1:2:0777",
1032 }],
912541b0 1033 rules => <<EOF
9158d03e
TG
1034SUBSYSTEM=="tty", OWNER="1"
1035SUBSYSTEM=="tty", GROUP="1"
28ce66de 1036SUBSYSTEM=="tty", MODE="0777"
9158d03e
TG
1037KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
1038KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="2"
fa19f181 1039EOF
912541b0
KS
1040 },
1041 {
1042 desc => "major/minor number test",
255c05b7
MW
1043 devices => [
1044 {
1045 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1046 exp_links => ["node"],
255c05b7
MW
1047 exp_majorminor => "8:0",
1048 }],
912541b0 1049 rules => <<EOF
5754e74c 1050SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node"
7d12d4e1 1051EOF
912541b0
KS
1052 },
1053 {
1054 desc => "big major number test",
255c05b7
MW
1055 devices => [
1056 {
1057 devpath => "/devices/virtual/misc/misc-fake1",
e62acc31 1058 exp_links => ["node"],
255c05b7
MW
1059 exp_majorminor => "4095:1",
1060 }],
1061 rules => <<EOF
5754e74c 1062KERNEL=="misc-fake1", SYMLINK+="node"
7d12d4e1 1063EOF
912541b0
KS
1064 },
1065 {
1066 desc => "big major and big minor number test",
255c05b7
MW
1067 devices => [
1068 {
1069 devpath => "/devices/virtual/misc/misc-fake89999",
e62acc31 1070 exp_links => ["node"],
255c05b7
MW
1071 exp_majorminor => "4095:89999",
1072 }],
912541b0 1073 rules => <<EOF
5754e74c 1074KERNEL=="misc-fake89999", SYMLINK+="node"
2b0f835c 1075EOF
912541b0
KS
1076 },
1077 {
1078 desc => "multiple symlinks with format char",
255c05b7
MW
1079 devices => [
1080 {
1081 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1082 exp_links => ["symlink1-0", "symlink2-ttyACM0", "symlink3-"],
255c05b7 1083 }],
912541b0 1084 rules => <<EOF
5754e74c 1085KERNEL=="ttyACM[0-9]*", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
7b2bdb4b 1086EOF
912541b0
KS
1087 },
1088 {
1089 desc => "multiple symlinks with a lot of s p a c e s",
255c05b7
MW
1090 devices => [
1091 {
1092 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1093 exp_links => ["one", "two"],
1094 not_exp_links => [" "],
255c05b7 1095 }],
912541b0 1096 rules => <<EOF
5754e74c 1097KERNEL=="ttyACM[0-9]*", SYMLINK=" one two "
9cd7b128
DS
1098EOF
1099 },
1100 {
1101 desc => "symlink with spaces in substituted variable",
255c05b7
MW
1102 devices => [
1103 {
1104 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1105 exp_links => ["name-one_two_three-end"],
1106 not_exp_links => [" "],
255c05b7 1107 }],
9cd7b128
DS
1108 rules => <<EOF
1109ENV{WITH_WS}="one two three"
1110SYMLINK="name-\$env{WITH_WS}-end"
1111EOF
1112 },
1113 {
1114 desc => "symlink with leading space in substituted variable",
255c05b7
MW
1115 devices => [
1116 {
1117 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1118 exp_links => ["name-one_two_three-end"],
1119 not_exp_links => [" "],
255c05b7 1120 }],
9cd7b128
DS
1121 rules => <<EOF
1122ENV{WITH_WS}=" one two three"
1123SYMLINK="name-\$env{WITH_WS}-end"
1124EOF
1125 },
1126 {
1127 desc => "symlink with trailing space in substituted variable",
255c05b7
MW
1128 devices => [
1129 {
1130 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1131 exp_links => ["name-one_two_three-end"],
1132 not_exp_links => [" "],
255c05b7 1133 }],
9cd7b128
DS
1134 rules => <<EOF
1135ENV{WITH_WS}="one two three "
1136SYMLINK="name-\$env{WITH_WS}-end"
1137EOF
1138 },
1139 {
1140 desc => "symlink with lots of space in substituted variable",
255c05b7
MW
1141 devices => [
1142 {
1143 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1144 exp_links => ["name-one_two_three-end"],
1145 not_exp_links => [" "],
255c05b7 1146 }],
9cd7b128
DS
1147 rules => <<EOF
1148ENV{WITH_WS}=" one two three "
1149SYMLINK="name-\$env{WITH_WS}-end"
1150EOF
1151 },
1152 {
1153 desc => "symlink with multiple spaces in substituted variable",
255c05b7
MW
1154 devices => [
1155 {
1156 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1157 exp_links => ["name-one_two_three-end"],
1158 not_exp_links => [" "],
255c05b7 1159 }],
9cd7b128
DS
1160 rules => <<EOF
1161ENV{WITH_WS}=" one two three "
1162SYMLINK="name-\$env{WITH_WS}-end"
1163EOF
1164 },
1165 {
e62acc31 1166 desc => "symlink with space and var with space",
255c05b7
MW
1167 devices => [
1168 {
1169 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
2084fe0d
MW
1170 exp_links => ["first", "name-one_two_three-end",
1171 "another_symlink", "a", "b", "c"],
1172 not_exp_links => [" "],
255c05b7 1173 }],
9cd7b128
DS
1174 rules => <<EOF
1175ENV{WITH_WS}=" one two three "
1176SYMLINK=" first name-\$env{WITH_WS}-end another_symlink a b c "
33989b96
YW
1177EOF
1178 },
1179 {
1180 desc => "symlink with env which contain slash (see #19309)",
1181 devices => [
1182 {
1183 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1184 exp_links => ["first", "name-aaa_bbb_ccc-end",
1185 "another_symlink", "a", "b", "c"],
1186 not_exp_links => ["ame-aaa/bbb/ccc-end"],
1187 }],
1188 rules => <<EOF
1189ENV{WITH_SLASH}="aaa/bbb/ccc"
1190OPTIONS="string_escape=replace", ENV{REPLACED}="\$env{WITH_SLASH}"
1191SYMLINK=" first name-\$env{REPLACED}-end another_symlink a b c "
b8669191 1192EOF
912541b0
KS
1193 },
1194 {
1195 desc => "symlink creation (same directory)",
255c05b7
MW
1196 devices => [
1197 {
1198 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1199 exp_links => ["modem0"],
255c05b7 1200 }],
912541b0 1201 rules => <<EOF
5754e74c 1202KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK="modem%n"
b8669191 1203EOF
912541b0
KS
1204 },
1205 {
1206 desc => "multiple symlinks",
255c05b7
MW
1207 devices => [
1208 {
1209 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1210 exp_links => ["first-0", "second-0", "third-0"],
255c05b7 1211 }],
912541b0 1212 rules => <<EOF
5754e74c 1213KERNEL=="ttyACM0", SYMLINK="first-%n second-%n third-%n"
b8669191 1214EOF
912541b0
KS
1215 },
1216 {
1217 desc => "symlink name '.'",
255c05b7
MW
1218 devices => [
1219 {
1220 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1221 exp_links => ["."],
255c05b7
MW
1222 exp_add_error => "yes",
1223 exp_rem_error => "yes",
1224 }],
912541b0 1225 rules => <<EOF
5754e74c 1226SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="."
b8669191 1227EOF
912541b0
KS
1228 },
1229 {
1230 desc => "symlink node to itself",
255c05b7
MW
1231 devices => [
1232 {
1233 devpath => "/devices/virtual/tty/tty0",
e62acc31 1234 exp_links => ["link"],
255c05b7
MW
1235 exp_add_error => "yes",
1236 exp_rem_error => "yes",
1237 }],
1238 option => "clean",
912541b0 1239 rules => <<EOF
5754e74c 1240KERNEL=="tty0", SYMLINK+="tty0"
b8669191 1241EOF
912541b0
KS
1242 },
1243 {
1244 desc => "symlink %n substitution",
255c05b7
MW
1245 devices => [
1246 {
1247 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1248 exp_links => ["symlink0"],
255c05b7 1249 }],
912541b0 1250 rules => <<EOF
5754e74c 1251KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink%n"
b8669191 1252EOF
912541b0
KS
1253 },
1254 {
1255 desc => "symlink %k substitution",
255c05b7
MW
1256 devices => [
1257 {
1258 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1259 exp_links => ["symlink-ttyACM0"],
255c05b7 1260 }],
912541b0 1261 rules => <<EOF
5754e74c 1262KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink-%k"
b8669191 1263EOF
912541b0
KS
1264 },
1265 {
1266 desc => "symlink %M:%m substitution",
255c05b7
MW
1267 devices => [
1268 {
1269 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1270 exp_links => ["major-166:0"],
255c05b7 1271 }],
912541b0 1272 rules => <<EOF
5754e74c 1273KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="major-%M:%m"
b8669191 1274EOF
912541b0
KS
1275 },
1276 {
1277 desc => "symlink %b substitution",
255c05b7
MW
1278 devices => [
1279 {
1280 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1281 exp_links => ["symlink-0:0:0:0"],
255c05b7 1282 }],
912541b0 1283 rules => <<EOF
220893b3 1284SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-%b"
b8669191 1285EOF
912541b0
KS
1286 },
1287 {
1288 desc => "symlink %c substitution",
255c05b7
MW
1289 devices => [
1290 {
1291 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1292 exp_links => ["test"],
255c05b7 1293 }],
912541b0 1294 rules => <<EOF
d914e445 1295KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo test", SYMLINK+="%c"
b8669191 1296EOF
912541b0
KS
1297 },
1298 {
1299 desc => "symlink %c{N} substitution",
255c05b7
MW
1300 devices => [
1301 {
1302 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1303 exp_links => ["test"],
1304 not_exp_links => ["symlink", "this"],
255c05b7 1305 }],
912541b0 1306 rules => <<EOF
d914e445 1307KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2}"
b8669191 1308EOF
912541b0
KS
1309 },
1310 {
1311 desc => "symlink %c{N+} substitution",
255c05b7
MW
1312 devices => [
1313 {
1314 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1315 exp_links => ["test", "this"],
1316 not_exp_links => ["symlink"],
255c05b7 1317 }],
912541b0 1318 rules => <<EOF
d914e445 1319KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2+}"
b8669191 1320EOF
912541b0
KS
1321 },
1322 {
1323 desc => "symlink only rule with %c{N+}",
255c05b7
MW
1324 devices => [
1325 {
1326 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1327 exp_links => ["test", "this"],
1328 not_exp_links => ["symlink"],
255c05b7 1329 }],
912541b0 1330 rules => <<EOF
d914e445 1331SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
b8669191 1332EOF
912541b0
KS
1333 },
1334 {
1335 desc => "symlink %s{filename} substitution",
255c05b7
MW
1336 devices => [
1337 {
1338 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1339 exp_links => ["166:0"],
255c05b7 1340 }],
912541b0 1341 rules => <<EOF
5754e74c 1342KERNEL=="ttyACM[0-9]*", SYMLINK+="%s{dev}"
b8669191 1343EOF
912541b0
KS
1344 },
1345 {
1346 desc => "program result substitution (numbered part of)",
255c05b7
MW
1347 devices => [
1348 {
1349 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
1350 exp_links => ["link1", "link2"],
1351 not_exp_links => ["node"],
255c05b7 1352 }],
912541b0 1353 rules => <<EOF
d914e445 1354SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
b8669191 1355EOF
912541b0
KS
1356 },
1357 {
1358 desc => "program result substitution (numbered part of+)",
255c05b7
MW
1359 devices => [
1360 {
1361 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
1362 exp_links => ["link1", "link2", "link3", "link4"],
1363 not_exp_links => ["node"],
255c05b7 1364 }],
912541b0 1365 rules => <<EOF
d914e445 1366SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
7efa217d 1367EOF
912541b0
KS
1368 },
1369 {
1370 desc => "SUBSYSTEM match test",
255c05b7
MW
1371 devices => [
1372 {
1373 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1374 exp_links => ["node"],
1375 not_exp_links => ["should_not_match", "should_not_match2"],
255c05b7 1376 }],
912541b0 1377 rules => <<EOF
5754e74c
KS
1378SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", SUBSYSTEM=="vc"
1379SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", SUBSYSTEM=="block"
1380SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match2", SUBSYSTEM=="vc"
2092fbcd 1381EOF
912541b0
KS
1382 },
1383 {
1384 desc => "DRIVERS match test",
255c05b7
MW
1385 devices => [
1386 {
1387 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1388 exp_links => ["node"],
1389 not_exp_links => ["should_not_match"]
255c05b7 1390 }],
912541b0 1391 rules => <<EOF
5754e74c
KS
1392SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", DRIVERS=="sd-wrong"
1393SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", DRIVERS=="sd"
c1ab0461 1394EOF
912541b0
KS
1395 },
1396 {
1397 desc => "devnode substitution test",
255c05b7
MW
1398 devices => [
1399 {
1400 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1401 exp_links => ["node"],
255c05b7 1402 }],
912541b0 1403 rules => <<EOF
220893b3 1404SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" SYMLINK+="node"
69aa6dfb 1405EOF
912541b0
KS
1406 },
1407 {
1408 desc => "parent node name substitution test",
255c05b7
MW
1409 devices => [
1410 {
1411 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1412 exp_links => ["sda-part-1"],
255c05b7 1413 }],
912541b0 1414 rules => <<EOF
06d4d4e2 1415SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="%P-part-%n"
69aa6dfb 1416EOF
912541b0
KS
1417 },
1418 {
1419 desc => "udev_root substitution",
255c05b7
MW
1420 devices => [
1421 {
1422 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1423 exp_links => ["start-/dev-end"],
255c05b7 1424 }],
912541b0 1425 rules => <<EOF
5754e74c 1426SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
3b6ed8bb 1427EOF
912541b0
KS
1428 },
1429 {
17cce031 1430 # This is not supported any more
912541b0 1431 desc => "last_rule option",
255c05b7
MW
1432 devices => [
1433 {
1434 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1435 exp_links => ["last"],
17cce031
MW
1436 not_exp_links => ["very-last"],
1437 exp_nodev_error => "yes",
255c05b7 1438 }],
912541b0 1439 rules => <<EOF
c4edd0ad 1440SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
5754e74c 1441SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="very-last"
28ce66de 1442EOF
912541b0
KS
1443 },
1444 {
1445 desc => "negation KERNEL!=",
255c05b7
MW
1446 devices => [
1447 {
1448 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1449 exp_links => ["match", "before"],
1450 not_exp_links => ["matches-but-is-negated"],
255c05b7 1451 }],
912541b0 1452 rules => <<EOF
5754e74c
KS
1453SUBSYSTEMS=="scsi", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1454SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1455SUBSYSTEMS=="scsi", KERNEL!="xsda1", SYMLINK+="match"
28ce66de 1456EOF
912541b0
KS
1457 },
1458 {
1459 desc => "negation SUBSYSTEM!=",
255c05b7
MW
1460 devices => [
1461 {
1462 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1463 exp_links => ["before", "not-anything"],
1464 not_exp_links => ["matches-but-is-negated"],
255c05b7 1465 }],
912541b0 1466 rules => <<EOF
5754e74c
KS
1467SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1468SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1469SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", SYMLINK+="not-anything"
28ce66de 1470EOF
912541b0
KS
1471 },
1472 {
1473 desc => "negation PROGRAM!= exit code",
255c05b7
MW
1474 devices => [
1475 {
1476 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1477 exp_links => ["before", "nonzero-program"],
255c05b7 1478 }],
912541b0 1479 rules => <<EOF
5754e74c 1480SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
d914e445 1481KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
3e5958de 1482EOF
912541b0
KS
1483 },
1484 {
1485 desc => "ENV{} test",
255c05b7
MW
1486 devices => [
1487 {
1488 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1489 exp_links => ["true"],
1490 not_exp_links => ["bad", "wrong"],
255c05b7 1491 }],
912541b0 1492 rules => <<EOF
a1af6b04 1493ENV{ENV_KEY_TEST}="test"
5754e74c
KS
1494SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1495SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
1496SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
3e5958de 1497EOF
912541b0
KS
1498 },
1499 {
1500 desc => "ENV{} test",
255c05b7
MW
1501 devices => [
1502 {
1503 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1504 exp_links => ["true"],
1505 not_exp_links => ["bad", "wrong", "no"],
255c05b7 1506 }],
912541b0 1507 rules => <<EOF
a1af6b04 1508ENV{ENV_KEY_TEST}="test"
5754e74c
KS
1509SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1510SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
1511SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
1512SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
5618b561 1513EOF
912541b0
KS
1514 },
1515 {
1516 desc => "ENV{} test (assign)",
255c05b7
MW
1517 devices => [
1518 {
1519 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1520 exp_links => ["true", "before"],
1521 not_exp_links => ["no"],
255c05b7 1522 }],
912541b0 1523 rules => <<EOF
c4edd0ad 1524SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
5754e74c
KS
1525SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
1526SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1527SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
ac528431 1528EOF
912541b0
KS
1529 },
1530 {
1531 desc => "ENV{} test (assign 2 times)",
255c05b7
MW
1532 devices => [
1533 {
1534 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1535 exp_links => ["true", "before"],
1536 not_exp_links => ["no", "bad"],
255c05b7 1537 }],
912541b0 1538 rules => <<EOF
ac528431
KS
1539SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1540SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
5754e74c
KS
1541SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1542SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
06d4d4e2 1543SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="bad"
5754e74c 1544SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
5618b561 1545EOF
912541b0
KS
1546 },
1547 {
1548 desc => "ENV{} test (assign2)",
255c05b7
MW
1549 devices => [
1550 {
1551 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1552 exp_links => ["part"],
1553 not_exp_links => ["disk"],
1554 },
06d4d4e2
MW
1555 {
1556 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1557 exp_links => ["disk"],
1558 not_exp_links => ["part"],
1559 },
e62acc31 1560 ],
912541b0 1561 rules => <<EOF
d59c84ef
KS
1562SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1563SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
5754e74c
KS
1564ENV{MAINDEVICE}=="true", SYMLINK+="disk"
1565SUBSYSTEM=="block", SYMLINK+="before"
1566ENV{PARTITION}=="true", SYMLINK+="part"
18614ab2 1567EOF
912541b0
KS
1568 },
1569 {
1570 desc => "untrusted string sanitize",
255c05b7
MW
1571 devices => [
1572 {
1573 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1574 exp_links => ["sane"],
255c05b7 1575 }],
912541b0 1576 rules => <<EOF
d914e445 1577SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
764ce7f2 1578EOF
912541b0
KS
1579 },
1580 {
1581 desc => "untrusted string sanitize (don't replace utf8)",
255c05b7
MW
1582 devices => [
1583 {
1584 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1585 exp_links => ["uber"],
255c05b7 1586 }],
912541b0 1587 rules => <<EOF
d914e445 1588SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
764ce7f2 1589EOF
912541b0
KS
1590 },
1591 {
1592 desc => "untrusted string sanitize (replace invalid utf8)",
255c05b7
MW
1593 devices => [
1594 {
1595 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1596 exp_links => ["replaced"],
255c05b7 1597 }],
912541b0 1598 rules => <<EOF
d914e445 1599SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
98bbc835 1600EOF
912541b0
KS
1601 },
1602 {
1603 desc => "read sysfs value from parent device",
255c05b7
MW
1604 devices => [
1605 {
1606 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1607 exp_links => ["serial-354172020305000"],
255c05b7 1608 }],
912541b0 1609 rules => <<EOF
5754e74c 1610KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
db949b02 1611EOF
912541b0
KS
1612 },
1613 {
1614 desc => "match against empty key string",
255c05b7
MW
1615 devices => [
1616 {
1617 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1618 exp_links => ["ok"],
1619 not_exp_links => ["not-1-ok", "not-2-ok", "not-3-ok"],
255c05b7 1620 }],
912541b0 1621 rules => <<EOF
5754e74c
KS
1622KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
1623KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
1624KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
1625KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
821d0ec8 1626EOF
912541b0
KS
1627 },
1628 {
1629 desc => "check ACTION value",
255c05b7
MW
1630 devices => [
1631 {
1632 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1633 exp_links => ["ok"],
1634 not_exp_links => ["unknown-not-ok"],
255c05b7 1635 }],
912541b0 1636 rules => <<EOF
5754e74c
KS
1637ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
1638ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
c974742b 1639EOF
912541b0
KS
1640 },
1641 {
1642 desc => "final assignment",
255c05b7
MW
1643 devices => [
1644 {
1645 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1646 exp_links => ["ok"],
255c05b7
MW
1647 exp_perms => "root:tty:0640",
1648 }],
912541b0 1649 rules => <<EOF
d960ad15 1650KERNEL=="sda", GROUP:="tty"
06d4d4e2 1651KERNEL=="sda", GROUP="root", MODE="0640", SYMLINK+="ok"
c974742b 1652EOF
912541b0
KS
1653 },
1654 {
1655 desc => "final assignment 2",
255c05b7
MW
1656 devices => [
1657 {
1658 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1659 exp_links => ["ok"],
255c05b7
MW
1660 exp_perms => "root:tty:0640",
1661 }],
912541b0 1662 rules => <<EOF
d960ad15 1663KERNEL=="sda", GROUP:="tty"
c974742b 1664SUBSYSTEM=="block", MODE:="640"
06d4d4e2 1665KERNEL=="sda", GROUP="root", MODE="0666", SYMLINK+="ok"
bd0ed2ff 1666EOF
912541b0
KS
1667 },
1668 {
1669 desc => "env substitution",
255c05b7
MW
1670 devices => [
1671 {
1672 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1673 exp_links => ["node-add-me"],
255c05b7 1674 }],
912541b0 1675 rules => <<EOF
5754e74c 1676KERNEL=="sda", MODE="0666", SYMLINK+="node-\$env{ACTION}-me"
995aec87 1677EOF
912541b0
KS
1678 },
1679 {
1680 desc => "reset list to current value",
255c05b7
MW
1681 devices => [
1682 {
1683 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1684 exp_links => ["three"],
1685 not_exp_links => ["two", "one"],
255c05b7 1686 }],
912541b0 1687 rules => <<EOF
c7fcba1b
KS
1688KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
1689KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
1690KERNEL=="ttyACM[0-9]*", SYMLINK="three"
647f7c49 1691EOF
912541b0
KS
1692 },
1693 {
1694 desc => "test empty SYMLINK+ (empty override)",
255c05b7
MW
1695 devices => [
1696 {
1697 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1698 exp_links => ["right"],
1699 not_exp_links => ["wrong"],
255c05b7 1700 }],
912541b0 1701 rules => <<EOF
5754e74c
KS
1702KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
1703KERNEL=="ttyACM[0-9]*", SYMLINK=""
1704KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
0cd4ac47 1705EOF
912541b0
KS
1706 },
1707 {
1708 desc => "test multi matches",
255c05b7
MW
1709 devices => [
1710 {
1711 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1712 exp_links => ["right", "before"],
255c05b7 1713 }],
912541b0 1714 rules => <<EOF
5754e74c
KS
1715KERNEL=="ttyACM*", SYMLINK+="before"
1716KERNEL=="ttyACM*|nothing", SYMLINK+="right"
0cd4ac47 1717EOF
912541b0
KS
1718 },
1719 {
1720 desc => "test multi matches 2",
255c05b7
MW
1721 devices => [
1722 {
1723 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1724 exp_links => ["right", "before"],
1725 not_exp_links => ["nomatch"],
255c05b7 1726 }],
912541b0 1727 rules => <<EOF
5754e74c
KS
1728KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
1729KERNEL=="ttyACM*", SYMLINK+="before"
1730KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
91a75e4a 1731EOF
912541b0
KS
1732 },
1733 {
1734 desc => "test multi matches 3",
255c05b7
MW
1735 devices => [
1736 {
1737 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1738 exp_links => ["right"],
1739 not_exp_links => ["nomatch", "wrong1", "wrong2"],
255c05b7 1740 }],
912541b0 1741 rules => <<EOF
5754e74c
KS
1742KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1743KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1744KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1745KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
91a75e4a 1746EOF
912541b0
KS
1747 },
1748 {
1749 desc => "test multi matches 4",
255c05b7
MW
1750 devices => [
1751 {
1752 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1753 exp_links => ["right"],
1754 not_exp_links => ["nomatch", "wrong1", "wrong2", "wrong3"],
255c05b7 1755 }],
912541b0 1756 rules => <<EOF
5754e74c
KS
1757KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1758KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1759KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1760KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
1761KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
48d26c90
YW
1762EOF
1763 },
1764 {
255c05b7
MW
1765 desc => "test multi matches 5",
1766 devices => [
1767 {
1768 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1769 exp_links => ["found"],
255c05b7
MW
1770 not_exp_name => "bad",
1771 }],
48d26c90
YW
1772 rules => <<EOF
1773KERNEL=="sda", TAG="foo"
1774TAGS=="|foo", SYMLINK+="found"
1775TAGS=="|aaa", SYMLINK+="bad"
1776EOF
1777 },
1778 {
1779 desc => "test multi matches 6",
255c05b7
MW
1780 devices => [
1781 {
1782 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1783 exp_links => ["found"],
255c05b7
MW
1784 not_exp_name => "bad",
1785 }],
48d26c90
YW
1786 rules => <<EOF
1787KERNEL=="sda", TAG=""
1788TAGS=="|foo", SYMLINK+="found"
1789TAGS=="aaa|bbb", SYMLINK+="bad"
1790EOF
1791 },
1792 {
1793 desc => "test multi matches 7",
255c05b7
MW
1794 devices => [
1795 {
1796 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1797 exp_links => ["found"],
255c05b7
MW
1798 not_exp_name => "bad",
1799 }],
48d26c90
YW
1800 rules => <<EOF
1801KERNEL=="sda", TAG="foo"
1802TAGS=="foo||bar", SYMLINK+="found"
1803TAGS=="aaa||bbb", SYMLINK+="bad"
1804EOF
1805 },
1806 {
1807 desc => "test multi matches 8",
255c05b7
MW
1808 devices => [
1809 {
1810 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1811 exp_links => ["found"],
255c05b7
MW
1812 not_exp_name => "bad",
1813 }],
48d26c90
YW
1814 rules => <<EOF
1815KERNEL=="sda", TAG=""
1816TAGS=="foo||bar", SYMLINK+="found"
1817TAGS=="aaa|bbb", SYMLINK+="bad"
1818EOF
1819 },
1820 {
1821 desc => "test multi matches 9",
255c05b7
MW
1822 devices => [
1823 {
1824 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1825 exp_links => ["found"],
255c05b7
MW
1826 not_exp_name => "bad",
1827 }],
48d26c90
YW
1828 rules => <<EOF
1829KERNEL=="sda", TAG="foo"
1830TAGS=="foo|", SYMLINK+="found"
1831TAGS=="aaa|", SYMLINK+="bad"
1832EOF
1833 },
1834 {
1835 desc => "test multi matches 10",
255c05b7
MW
1836 devices => [
1837 {
1838 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1839 exp_links => ["found"],
255c05b7
MW
1840 not_exp_name => "bad",
1841 }],
48d26c90
YW
1842 rules => <<EOF
1843KERNEL=="sda", TAG=""
1844TAGS=="foo|", SYMLINK+="found"
1845TAGS=="aaa|bbb", SYMLINK+="bad"
0d3a8bc7
YG
1846EOF
1847 },
1848 {
1849 desc => "test multi matches 11",
255c05b7
MW
1850 devices => [
1851 {
1852 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1853 exp_links => ["found"],
255c05b7
MW
1854 not_exp_name => "bad",
1855 }],
0d3a8bc7
YG
1856 rules => <<EOF
1857KERNEL=="sda", TAG="c"
1858TAGS=="foo||bar||c", SYMLINK+="found"
1859TAGS=="aaa||bbb||ccc", SYMLINK+="bad"
0bfb84e1 1860EOF
912541b0
KS
1861 },
1862 {
a96cd21d 1863 desc => "IMPORT parent test",
255c05b7
MW
1864 devices => [
1865 {
1866 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1867 exp_links => ["parent"],
a96cd21d 1868 },
255c05b7
MW
1869 {
1870 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1871 exp_links => ["parentenv-parent_right"],
255c05b7 1872 }],
a96cd21d 1873 sleep_us => 500000, # Serialized! We need to sleep here after adding sda
912541b0 1874 rules => <<EOF
5754e74c 1875KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
a96cd21d
MW
1876KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1877KERNEL=="sda", SYMLINK+="parent"
594dd610 1878EOF
912541b0
KS
1879 },
1880 {
1881 desc => "GOTO test",
255c05b7
MW
1882 devices => [
1883 {
1884 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1885 exp_links => ["right"],
1886 not_exp_test => ["wrong", "wrong2"],
255c05b7 1887 }],
912541b0 1888 rules => <<EOF
594dd610 1889KERNEL=="sda1", GOTO="TEST"
5754e74c 1890KERNEL=="sda1", SYMLINK+="wrong"
6880b25d 1891KERNEL=="sda1", GOTO="BAD"
5754e74c
KS
1892KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1893KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1894KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
9dae0e89 1895LABEL="end"
0c377989 1896EOF
912541b0
KS
1897 },
1898 {
1899 desc => "GOTO label does not exist",
255c05b7
MW
1900 devices => [
1901 {
1902 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1903 exp_links => ["right"],
255c05b7 1904 }],
912541b0 1905 rules => <<EOF
0c377989 1906KERNEL=="sda1", GOTO="does-not-exist"
5754e74c 1907KERNEL=="sda1", SYMLINK+="right",
0c377989 1908LABEL="exists"
d59c84ef 1909EOF
912541b0
KS
1910 },
1911 {
1912 desc => "SYMLINK+ compare test",
255c05b7
MW
1913 devices => [
1914 {
1915 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1916 exp_links => ["right", "link"],
1917 not_exp_links => ["wrong"],
255c05b7 1918 }],
912541b0 1919 rules => <<EOF
5754e74c
KS
1920KERNEL=="sda1", SYMLINK+="link"
1921KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1922KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
d59c84ef 1923EOF
912541b0
KS
1924 },
1925 {
1926 desc => "invalid key operation",
255c05b7
MW
1927 devices => [
1928 {
1929 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1930 exp_links => ["yes"],
1931 not_exp_links => ["no"],
255c05b7 1932 }],
912541b0 1933 rules => <<EOF
5754e74c
KS
1934KERNEL="sda1", SYMLINK+="no"
1935KERNEL=="sda1", SYMLINK+="yes"
864b9b5e 1936EOF
912541b0
KS
1937 },
1938 {
1939 desc => "operator chars in attribute",
255c05b7
MW
1940 devices => [
1941 {
1942 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1943 exp_links => ["yes"],
255c05b7 1944 }],
912541b0 1945 rules => <<EOF
5754e74c 1946KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
d4ae9925 1947EOF
912541b0
KS
1948 },
1949 {
1950 desc => "overlong comment line",
255c05b7
MW
1951 devices => [
1952 {
1953 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1954 exp_links => ["yes"],
1955 not_exp_links => ["no"],
255c05b7 1956 }],
912541b0 1957 rules => <<EOF
d4ae9925 1958# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
d960ad15 1959 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
5754e74c
KS
1960KERNEL=="sda1", SYMLINK+=="no"
1961KERNEL=="sda1", SYMLINK+="yes"
4ad47b2d 1962EOF
912541b0
KS
1963 },
1964 {
1965 desc => "magic subsys/kernel lookup",
255c05b7
MW
1966 devices => [
1967 {
1968 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1969 exp_links => ["00:16:41:e2:8d:ff"],
255c05b7 1970 }],
912541b0 1971 rules => <<EOF
5754e74c 1972KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
03f65fe6 1973EOF
912541b0
KS
1974 },
1975 {
1976 desc => "TEST absolute path",
255c05b7
MW
1977 devices => [
1978 {
1979 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1980 exp_links => ["there"],
1981 not_exp_links => ["notthere"],
255c05b7 1982 }],
912541b0 1983 rules => <<EOF
f0289577
LB
1984TEST=="/etc/passwd", SYMLINK+="there"
1985TEST!="/etc/passwd", SYMLINK+="notthere"
03f65fe6 1986EOF
912541b0
KS
1987 },
1988 {
1989 desc => "TEST subsys/kernel lookup",
255c05b7
MW
1990 devices => [
1991 {
1992 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1993 exp_links => ["yes"],
255c05b7 1994 }],
912541b0 1995 rules => <<EOF
5754e74c 1996KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
03f65fe6 1997EOF
912541b0
KS
1998 },
1999 {
2000 desc => "TEST relative path",
255c05b7
MW
2001 devices => [
2002 {
2003 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2004 exp_links => ["relative"],
255c05b7 2005 }],
912541b0 2006 rules => <<EOF
5754e74c 2007KERNEL=="sda", TEST=="size", SYMLINK+="relative"
0ea5e96e 2008EOF
912541b0
KS
2009 },
2010 {
2011 desc => "TEST wildcard substitution (find queue/nr_requests)",
255c05b7
MW
2012 devices => [
2013 {
2014 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2015 exp_links => ["found-subdir"],
255c05b7 2016 }],
912541b0 2017 rules => <<EOF
5754e74c 2018KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
cf100ca7 2019EOF
912541b0
KS
2020 },
2021 {
2022 desc => "TEST MODE=0000",
255c05b7
MW
2023 devices => [
2024 {
2025 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
2026 exp_perms => "0:0:0000",
2027 exp_rem_error => "yes",
2028 }],
912541b0 2029 rules => <<EOF
cf100ca7 2030KERNEL=="sda", MODE="0000"
a367f04e 2031EOF
912541b0
KS
2032 },
2033 {
2034 desc => "TEST PROGRAM feeds OWNER, GROUP, MODE",
255c05b7
MW
2035 devices => [
2036 {
2037 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7 2038 exp_perms => "1:1:0400",
255c05b7 2039 }],
912541b0 2040 rules => <<EOF
6880b25d 2041KERNEL=="sda", MODE="666"
9158d03e 2042KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
ff94cec3 2043EOF
912541b0
KS
2044 },
2045 {
2046 desc => "TEST PROGRAM feeds MODE with overflow",
255c05b7
MW
2047 devices => [
2048 {
2049 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
2050 exp_perms => "0:0:0440",
2051 exp_rem_error => "yes",
2052 }],
912541b0 2053 rules => <<EOF
6880b25d 2054KERNEL=="sda", MODE="440"
d914e445 2055KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
dc4c7e46 2056EOF
912541b0
KS
2057 },
2058 {
2059 desc => "magic [subsys/sysname] attribute substitution",
255c05b7
MW
2060 devices => [
2061 {
2062 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2063 exp_links => ["sda-8741C4G-end"],
255c05b7
MW
2064 exp_perms => "0:0:0600",
2065 }],
912541b0 2066 rules => <<EOF
0f52fdee 2067KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
d7867b31 2068EOF
912541b0
KS
2069 },
2070 {
2071 desc => "builtin path_id",
255c05b7
MW
2072 devices => [
2073 {
2074 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2075 exp_links => ["disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0"],
255c05b7 2076 }],
912541b0 2077 rules => <<EOF
d7867b31
KS
2078KERNEL=="sda", IMPORT{builtin}="path_id"
2079KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/\$env{ID_PATH}"
bf8f7583
MP
2080EOF
2081 },
2082 {
2083 desc => "add and match tag",
255c05b7
MW
2084 devices => [
2085 {
2086 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
2087 exp_links => ["found"],
2088 not_exp_links => ["bad"],
255c05b7 2089 }],
bf8f7583
MP
2090 rules => <<EOF
2091SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", TAG+="green"
2092TAGS=="green", SYMLINK+="found"
2093TAGS=="blue", SYMLINK+="bad"
2094EOF
2095 },
2096 {
2097 desc => "don't crash with lots of tags",
255c05b7
MW
2098 devices => [
2099 {
2100 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2101 exp_links => ["found"],
255c05b7 2102 }],
bf8f7583
MP
2103 rules => $rules_10k_tags . <<EOF
2104TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="found"
1e797cf5
YW
2105EOF
2106 },
2107 {
d35976c6 2108 desc => "continuations",
255c05b7
MW
2109 devices => [
2110 {
2111 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2112 exp_links => ["found"],
255c05b7
MW
2113 not_exp_name => "bad",
2114 }],
1e797cf5
YW
2115 rules => $rules_10k_tags_continuation . <<EOF
2116TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="bad"
d35976c6
YW
2117KERNEL=="sda",\\
2118# comment in continuation
2119TAG+="hoge1",\\
2120 # space before comment
2121TAG+="hoge2",\\
2122# spaces before and after token are dropped
2123 TAG+="hoge3", \\
84a0819c
YW
2124\\
2125 \\
d35976c6
YW
2126TAG+="hoge4"
2127TAGS=="hoge1", TAGS=="hoge2", TAGS=="hoge3", TAGS=="hoge4", SYMLINK+="found"
84a0819c
YW
2128EOF
2129 },
2130 {
2131 desc => "continuations with empty line",
255c05b7
MW
2132 devices => [
2133 {
2134 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2135 exp_links => ["found"],
255c05b7
MW
2136 not_exp_name => "bad",
2137
2138 }],
84a0819c
YW
2139 rules => <<EOF
2140# empty line finishes continuation
2141KERNEL=="sda", TAG+="foo" \\
2142
2143KERNEL=="sdb", TAG+="hoge"
2144KERNEL=="sda", TAG+="aaa" \\
2145KERNEL=="sdb", TAG+="bbb"
2146TAGS=="foo", SYMLINK+="found"
2147TAGS=="aaa", SYMLINK+="bad"
255c05b7 2148 EOF
84a0819c
YW
2149 },
2150 {
2151 desc => "continuations with white only line",
255c05b7
MW
2152 devices => [
2153 {
2154 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2155 exp_links => ["found"],
255c05b7
MW
2156 not_exp_name => "bad",
2157 }],
84a0819c
YW
2158 rules => <<EOF
2159# space only line finishes continuation
2160KERNEL=="sda", TAG+="foo" \\
2161 \t
2162KERNEL=="sdb", TAG+="hoge"
2163KERNEL=="sda", TAG+="aaa" \\
2164KERNEL=="sdb", TAG+="bbb"
2165TAGS=="foo", SYMLINK+="found"
2166TAGS=="aaa", SYMLINK+="bad"
4a0ec82d
MW
2167EOF
2168 },
2169 {
2170 desc => "multiple devices",
2171 devices => [
2172 {
2173 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2174 exp_links => ["part-1"],
2175 },
2176 {
2177 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2178 exp_links => ["part-5"],
2179 },
2180 {
2181 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2182 exp_links => ["part-6"],
2183 },
2184 {
2185 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2186 exp_links => ["part-7"],
2187 },
2188 {
2189 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2190 exp_links => ["part-8"],
2191 },
2192 {
2193 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2194 exp_links => ["part-9"],
2195 },
2196 {
2197 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2198 exp_links => ["part-10"],
2199 },
2200 ],
2201 rules => <<EOF
2202SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2203EOF
2204 },
2205 {
2206 desc => "multiple devices, same link name, positive prio",
2ab0a8d0 2207 repeat => 100,
4a0ec82d
MW
2208 devices => [
2209 {
2210 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2211 exp_links => ["part-1"],
2212 not_exp_links => ["partition"],
2213 },
2214 {
2215 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2216 exp_links => ["part-5"],
2217 not_exp_links => ["partition"],
2218 },
2219 {
2220 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2221 not_exp_links => ["partition"],
2222 exp_links => ["part-6"],
2223 },
2224 {
2225 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2226 exp_links => ["part-7", "partition"],
2227 },
2228 {
2229 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2230 not_exp_links => ["partition"],
2231 exp_links => ["part-8"],
2232 },
2233 {
2234 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2235 not_exp_links => ["partition"],
2236 exp_links => ["part-9"],
2237 },
2238 {
2239 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2240 not_exp_links => ["partition"],
2241 exp_links => ["part-10"],
2242 },
2243 ],
2244 rules => <<EOF
2245SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2246SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2247KERNEL=="*7", OPTIONS+="link_priority=10"
2248EOF
2249 },
2250 {
2251 desc => "multiple devices, same link name, negative prio",
2252 devices => [
2253 {
2254 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2255 exp_links => ["part-1"],
2256 not_exp_links => ["partition"],
2257 },
2258 {
2259 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2260 exp_links => ["part-5"],
2261 not_exp_links => ["partition"],
2262 },
2263 {
2264 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2265 not_exp_links => ["partition"],
2266 exp_links => ["part-6"],
2267 },
2268 {
2269 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2270 exp_links => ["part-7", "partition"],
2271 },
2272 {
2273 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2274 not_exp_links => ["partition"],
2275 exp_links => ["part-8"],
2276 },
2277 {
2278 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2279 not_exp_links => ["partition"],
2280 exp_links => ["part-9"],
2281 },
2282 {
2283 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2284 not_exp_links => ["partition"],
2285 exp_links => ["part-10"],
2286 },
2287 ],
2288 rules => <<EOF
2289SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2290SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2291KERNEL!="*7", OPTIONS+="link_priority=-10"
2292EOF
2293 },
2294 {
2295 desc => "multiple devices, same link name, positive prio, sleep",
2296 devices => [
2297 {
2298 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2299 exp_links => ["part-1"],
2300 not_exp_links => ["partition"],
2301 },
2302 {
2303 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2304 exp_links => ["part-5"],
2305 not_exp_links => ["partition"],
2306 },
2307 {
2308 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2309 not_exp_links => ["partition"],
2310 exp_links => ["part-6"],
2311 },
2312 {
2313 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2314 exp_links => ["part-7", "partition"],
2315 },
2316 {
2317 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2318 not_exp_links => ["partition"],
2319 exp_links => ["part-8"],
2320 },
2321 {
2322 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2323 not_exp_links => ["partition"],
2324 exp_links => ["part-9"],
2325 },
2326 {
2327 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2328 not_exp_links => ["partition"],
2329 exp_links => ["part-10"],
2330 },
2331 ],
2332 sleep_us => 10000,
2333 rules => <<EOF
2334SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2335SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2336KERNEL=="*7", OPTIONS+="link_priority=10"
ff94cec3 2337EOF
912541b0 2338 },
eb44d715
MW
2339 {
2340 desc => 'all_block_devs',
2341 generator => expect_for_some("\\/sda6\$", ["blockdev"]),
2342 repeat => 10,
2343 rules => <<EOF
2344SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sd*", SYMLINK+="blockdev"
2345KERNEL=="sda6", OPTIONS+="link_priority=10"
2346EOF
2347 }
a367f04e
GKH
2348);
2349
af7ee3ea
MW
2350sub create_rules {
2351 my ($rules) = @_;
a367f04e 2352
912541b0 2353 # create temporary rules
6ada823a 2354 system("mkdir", "-p", "$udev_rules_dir");
912541b0
KS
2355 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
2356 print CONF $$rules;
2357 close CONF;
af7ee3ea
MW
2358}
2359
2360sub udev {
2361 my ($action, $devpath) = @_;
a367f04e 2362
912541b0 2363 if ($valgrind > 0) {
efc9f703 2364 return system("$udev_bin_valgrind $action $devpath");
333e07b7 2365 } elsif ($gdb > 0) {
efc9f703 2366 return system("$udev_bin_gdb $action $devpath");
587751eb 2367 } elsif ($strace > 0) {
efc9f703 2368 return system("$udev_bin_strace $action $devpath");
912541b0 2369 } else {
efc9f703 2370 return system("$udev_bin", "$action", "$devpath");
912541b0 2371 }
a367f04e
GKH
2372}
2373
e5fbfe0a 2374my $error = 0;
b95c4398 2375my $good = 0;
cbeb23d8 2376my $exp_good = 0;
72ffa78d 2377
b8669191 2378sub permissions_test {
912541b0 2379 my($rules, $uid, $gid, $mode) = @_;
b8669191 2380
912541b0
KS
2381 my $wrong = 0;
2382 my $userid;
2383 my $groupid;
9b434de1 2384
912541b0
KS
2385 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
2386 if ($1 ne "") {
2387 if (defined(getpwnam($1))) {
2388 $userid = int(getpwnam($1));
2389 } else {
2390 $userid = $1;
2391 }
2392 if ($uid != $userid) { $wrong = 1; }
2393 }
2394 if ($2 ne "") {
2395 if (defined(getgrnam($2))) {
2396 $groupid = int(getgrnam($2));
2397 } else {
2398 $groupid = $2;
2399 }
2400 if ($gid != $groupid) { $wrong = 1; }
2401 }
2402 if ($3 ne "") {
2403 if (($mode & 07777) != oct($3)) { $wrong = 1; };
2404 }
2405 if ($wrong == 0) {
2406 print "permissions: ok\n";
b95c4398 2407 $good++;
912541b0
KS
2408 } else {
2409 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
2410 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
2411 print "permissions: error\n";
2412 $error++;
2413 sleep(1);
2414 }
b8669191
GKH
2415}
2416
2417sub major_minor_test {
912541b0 2418 my($rules, $rdev) = @_;
b8669191 2419
912541b0
KS
2420 my $major = ($rdev >> 8) & 0xfff;
2421 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
2422 my $wrong = 0;
b8669191 2423
912541b0
KS
2424 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
2425 if ($1 ne "") {
2426 if ($major != $1) { $wrong = 1; };
2427 }
2428 if ($2 ne "") {
2429 if ($minor != $2) { $wrong = 1; };
2430 }
2431 if ($wrong == 0) {
2432 print "major:minor: ok\n";
b95c4398 2433 $good++;
912541b0
KS
2434 } else {
2435 printf " expected major:minor is: %i:%i\n", $1, $2;
2436 printf " created major:minor is : %i:%i\n", $major, $minor;
2437 print "major:minor: error\n";
2438 $error++;
2439 sleep(1);
2440 }
b8669191
GKH
2441}
2442
6ada823a 2443sub udev_setup {
f1cb0860 2444 system("umount \"$udev_tmpfs\" 2>/dev/null");
21d9e3f3
EV
2445 rmdir($udev_tmpfs);
2446 mkdir($udev_tmpfs) || die "unable to create udev_tmpfs: $udev_tmpfs\n";
110a1320 2447
9f563f27 2448 if (system("mount", "-o", "rw,mode=0755,nosuid,noexec", "-t", "tmpfs", "tmpfs", $udev_tmpfs)) {
110a1320
EV
2449 warn "unable to mount tmpfs";
2450 return 0;
2451 }
21d9e3f3 2452
6ada823a
KS
2453 mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
2454 # setting group and mode of udev_dev ensures the tests work
912541b0 2455 # even if the parent directory has setgid bit enabled.
6ada823a
KS
2456 chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
2457 chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
110a1320
EV
2458
2459 if (system("mknod", $udev_dev . "/null", "c", "1", "3")) {
2460 warn "unable to create $udev_dev/null";
2461 return 0;
2462 }
6ada823a 2463
dbfbc6c4
AB
2464 # check if we are permitted to create block device nodes
2465 my $block_device_filename = $udev_dev . "/sda";
2466 if (system("mknod", $block_device_filename, "b", "8", "0")) {
2467 warn "unable to create $block_device_filename";
2468 return 0;
2469 }
2470 unlink $block_device_filename;
2471
21d9e3f3 2472 system("cp", "-r", "test/sys/", $udev_sys) && die "unable to copy test/sys";
2ce8d27b 2473
6ada823a 2474 system("rm", "-rf", "$udev_run");
110a1320 2475
1e5548c0
AB
2476 if (!mkdir($udev_run)) {
2477 warn "unable to create directory $udev_run";
2478 return 0;
2479 }
2480
110a1320 2481 return 1;
ff94cec3
EK
2482}
2483
f0dccf01
MW
2484sub get_devnode {
2485 my ($device) = @_;
2486 my $devnode;
2487
2488 if (defined($device->{devnode})) {
2489 $devnode = "$udev_dev/$device->{devnode}";
2490 } else {
2491 $devnode = "$device->{devpath}";
2492 $devnode =~ s!.*/!$udev_dev/!;
2493 }
2494 return $devnode;
2495}
2496
2497sub check_devnode {
2498 my ($device) = @_;
2499 my $devnode = get_devnode($device);
2500
2501 my @st = lstat("$devnode");
2502 if (! (-b _ || -c _)) {
2503 print "add $devnode: error\n";
2504 system("tree", "$udev_dev");
2505 $error++;
2506 return undef;
2507 }
2508
2509 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
2510 $atime, $mtime, $ctime, $blksize, $blocks) = @st;
2511
2512 if (defined($device->{exp_perms})) {
2513 permissions_test($device, $uid, $gid, $mode);
2514 }
2515 if (defined($device->{exp_majorminor})) {
2516 major_minor_test($device, $rdev);
2517 }
2518 print "add $devnode: ok\n";
b95c4398 2519 $good++;
f0dccf01
MW
2520 return $devnode;
2521}
2522
e62acc31
MW
2523sub get_link_target {
2524 my ($link) = @_;
09a4062d 2525
e62acc31
MW
2526 my $cwd = getcwd();
2527 my $dir = "$udev_dev/$link";
2528 my $tgt = readlink("$udev_dev/$link");
2529 $dir =~ s!/[^/]*$!!;
2530 $tgt = abs_path("$dir/$tgt");
2531 $tgt =~ s!^$cwd/!!;
2532 return $tgt;
2533}
f0dccf01 2534
e62acc31
MW
2535sub check_link_add {
2536 my ($link, $devnode, $err_expected) = @_;
f0dccf01 2537
e62acc31 2538 my @st = lstat("$udev_dev/$link");
997683c8 2539 if (-l _) {
e62acc31 2540 my $tgt = get_link_target($link);
997683c8
MW
2541
2542 if ($tgt ne $devnode) {
e62acc31 2543 print "symlink $link: error, found -> $tgt\n";
997683c8
MW
2544 $error++;
2545 system("tree", "$udev_dev");
2546 } else {
e62acc31 2547 print "symlink $link: ok\n";
b95c4398 2548 $good++;
997683c8 2549 }
912541b0 2550 } else {
e62acc31
MW
2551 print "symlink $link: error";
2552 if ($err_expected) {
912541b0 2553 print " as expected\n";
b95c4398 2554 $good++;
912541b0
KS
2555 } else {
2556 print "\n";
6ada823a 2557 system("tree", "$udev_dev");
912541b0
KS
2558 print "\n";
2559 $error++;
2560 sleep(1);
2561 }
2562 }
255c05b7 2563}
a367f04e 2564
e62acc31
MW
2565sub check_link_nonexistent {
2566 my ($link, $devnode, $err_expected) = @_;
2567
2568 if ((-e "$udev_dev/$link") || (-l "$udev_dev/$link")) {
2569 my $tgt = get_link_target($link);
2570
2571 if ($tgt ne $devnode) {
2572 print "nonexistent: '$link' points to other device (ok)\n";
b95c4398 2573 $good++;
e62acc31
MW
2574 } else {
2575 print "nonexistent: error \'$link\' should not be there";
2576 if ($err_expected) {
2577 print " (as expected)\n";
b95c4398 2578 $good++;
e62acc31
MW
2579 } else {
2580 print "\n";
2581 system("tree", "$udev_dev");
2582 print "\n";
2583 $error++;
2584 sleep(1);
2585 }
2586 }
2587 } else {
2588 print "nonexistent $link: ok\n";
b95c4398 2589 $good++;
e62acc31
MW
2590 }
2591}
2592
2593sub check_add {
2594 my ($device) = @_;
2595 my $devnode = check_devnode($device);
2596
2597 if (defined($device->{exp_links})) {
2598 foreach my $link (@{$device->{exp_links}}) {
2599 check_link_add($link, $devnode,
2600 $device->{exp_add_error});
2601 }
2602 }
2603 if (defined $device->{not_exp_links}) {
2604 foreach my $link (@{$device->{not_exp_links}}) {
2605 check_link_nonexistent($link, $devnode,
2606 $device->{exp_nodev_error});
2607 }
2608 }
2609}
2610
f0dccf01
MW
2611sub check_remove_devnode {
2612 my ($device) = @_;
2613 my $devnode = get_devnode($device);
2614
2615 if (-e "$devnode") {
2616 print "remove $devnode: error";
2617 print "\n";
2618 system("tree", "$udev_dev");
2619 print "\n";
2620 $error++;
2621 sleep(1);
2622 } else {
2623 print "remove $devnode: ok\n";
b95c4398 2624 $good++;
f0dccf01
MW
2625 }
2626}
2627
e62acc31
MW
2628sub check_link_remove {
2629 my ($link, $err_expected) = @_;
f0dccf01 2630
e62acc31
MW
2631 if ((-e "$udev_dev/$link") ||
2632 (-l "$udev_dev/$link")) {
2633 print "remove $link: error";
2634 if ($err_expected) {
912541b0 2635 print " as expected\n";
b95c4398 2636 $good++;
912541b0
KS
2637 } else {
2638 print "\n";
6ada823a 2639 system("tree", "$udev_dev");
912541b0
KS
2640 print "\n";
2641 $error++;
2642 sleep(1);
2643 }
2644 } else {
e62acc31 2645 print "remove $link: ok\n";
b95c4398 2646 $good++;
e62acc31
MW
2647 }
2648}
2649
2650sub check_remove {
2651 my ($device) = @_;
2652
2653 check_remove_devnode($device);
2654
2655 return if (!defined($device->{exp_links}));
2656
2657 foreach my $link (@{$device->{exp_links}}) {
2658 check_link_remove($link, $device->{exp_rem_error});
255c05b7
MW
2659 }
2660}
2661
09a4062d
MW
2662sub run_udev {
2663 my ($action, $dev, $sleep_us, $sema) = @_;
2664
2665 # Notify main process that this worker has started
2666 $sema->op(0, 1, 0);
2667
2668 # Wait for start
2669 $sema->op(0, 0, 0);
2670 usleep($sleep_us) if defined ($sleep_us);
2671 my $rc = udev($action, $dev->{devpath});
2672 exit $rc;
2673}
2674
2675sub fork_and_run_udev {
2676 my ($action, $rules, $sema) = @_;
2677 my @devices = @{$rules->{devices}};
2678 my $dev;
2679 my $k = 0;
2680
2681 $sema->setval(0, 1);
2682 foreach $dev (@devices) {
2683 my $pid = fork();
2684
2685 if (!$pid) {
2686 run_udev($action, $dev,
2687 defined($rules->{sleep_us}) ? $k * $rules->{sleep_us} : undef,
2688 $sema);
2689 } else {
2690 $dev->{pid} = $pid;
2691 }
2692 $k++;
2693 }
2694
2695 # This operation waits for all workers to become ready, and
2696 # starts them off when that's the case.
2697 $sema->op(0, -($#devices + 2), 0);
2698
2699 foreach $dev (@devices) {
2700 my $rc;
2701 my $pid;
2702
2703 $pid = waitpid($dev->{pid}, 0);
2704 if ($pid == -1) {
2705 print "error waiting for pid dev->{pid}\n";
09a4062d
MW
2706 }
2707 if (WIFEXITED($?)) {
2708 $rc = WEXITSTATUS($?);
2709
2710 if ($rc) {
2711 print "$udev_bin $action for $dev->{devpath} failed with code $rc\n";
2712 $error += 1;
b95c4398
MW
2713 } else {
2714 $good++;
09a4062d
MW
2715 }
2716 }
2717 }
2718}
2719
255c05b7 2720sub run_test {
09a4062d 2721 my ($rules, $number, $sema) = @_;
255c05b7 2722 my $rc;
eb44d715 2723 my @devices;
cbeb23d8
MW
2724 my $ntests;
2725 my $cur_good = $good;
2726 my $cur_error = $error;
eb44d715
MW
2727
2728 if (!defined $rules->{devices}) {
2729 $rules->{devices} = all_block_devs($rules->{generator});
2730 }
2731 @devices = @{$rules->{devices}};
cbeb23d8
MW
2732 # For each device: exit status and devnode test for add & remove
2733 $ntests += 4 * ($#devices + 1);
255c05b7 2734
cbeb23d8
MW
2735 foreach my $dev (@devices) {
2736 $ntests += 2 * ($#{$dev->{exp_links}} + 1)
2737 + ($#{$dev->{not_exp_links}} + 1)
2738 + (defined $dev->{exp_perms} ? 1 : 0)
2739 + (defined $dev->{exp_majorminor} ? 1 : 0);
2740 }
2741 if (defined $rules->{repeat}) {
2742 $ntests *= $rules->{repeat};
2743 }
2744 $exp_good += $ntests;
255c05b7 2745 print "TEST $number: $rules->{desc}\n";
af7ee3ea 2746 create_rules(\$rules->{rules});
09a4062d 2747
2ab0a8d0 2748 REPEAT:
09a4062d 2749 fork_and_run_udev("add", $rules, $sema);
255c05b7
MW
2750
2751 foreach my $dev (@devices) {
2752 check_add($dev);
2753 }
2754
2755 if (defined($rules->{option}) && $rules->{option} eq "keep") {
2756 print "\n\n";
2757 return;
2758 }
2759
09a4062d
MW
2760 fork_and_run_udev("remove", $rules, $sema);
2761
255c05b7
MW
2762 foreach my $dev (@devices) {
2763 check_remove($dev);
912541b0 2764 }
0345b862 2765
2ab0a8d0
MW
2766 if (defined($rules->{repeat}) && --($rules->{repeat}) > 0) {
2767 goto REPEAT;
2768 }
cbeb23d8
MW
2769 printf "TEST $number: errors: %d good: %d/%d\n\n", $error-$cur_error,
2770 $good-$cur_good, $ntests;
9b434de1 2771
912541b0 2772 if (defined($rules->{option}) && $rules->{option} eq "clean") {
6ada823a 2773 udev_setup();
912541b0 2774 }
0345b862 2775
a367f04e
GKH
2776}
2777
abb9cc50
DS
2778sub cleanup {
2779 system("rm", "-rf", "$udev_run");
2780 system("umount", "$udev_tmpfs");
2781 rmdir($udev_tmpfs);
2782}
2783
800ab95b
GKH
2784# only run if we have root permissions
2785# due to mknod restrictions
2786if (!($<==0)) {
912541b0 2787 print "Must have root permissions to run properly.\n";
4d55fc5b 2788 exit($EXIT_TEST_SKIP);
800ab95b
GKH
2789}
2790
e4d214ef
DC
2791# skip the test when running in a chroot
2792system("systemd-detect-virt", "-r", "-q");
2793if ($? >> 8 == 0) {
ce5fcc69
ZJS
2794 print "Running in a chroot, skipping the test.\n";
2795 exit($EXIT_TEST_SKIP);
e4d214ef
DC
2796}
2797
110a1320
EV
2798if (!udev_setup()) {
2799 warn "Failed to set up the environment, skipping the test";
abb9cc50 2800 cleanup();
110a1320
EV
2801 exit($EXIT_TEST_SKIP);
2802}
2803
7935dae5 2804if (system($udev_bin, "check")) {
110a1320 2805 warn "$udev_bin failed to set up the environment, skipping the test";
abb9cc50 2806 cleanup();
110a1320
EV
2807 exit($EXIT_TEST_SKIP);
2808}
2e317184
GKH
2809
2810my $test_num = 1;
e08109cb 2811my @list;
2e317184 2812
e08109cb 2813foreach my $arg (@ARGV) {
912541b0
KS
2814 if ($arg =~ m/--valgrind/) {
2815 $valgrind = 1;
2816 printf("using valgrind\n");
333e07b7
TG
2817 } elsif ($arg =~ m/--gdb/) {
2818 $gdb = 1;
2819 printf("using gdb\n");
587751eb
ZJS
2820 } elsif ($arg =~ m/--strace/) {
2821 $strace = 1;
2822 printf("using strace\n");
912541b0
KS
2823 } else {
2824 push(@list, $arg);
2825 }
e08109cb 2826}
09a4062d 2827my $sema = IPC::Semaphore->new(IPC_PRIVATE, 1, S_IRUSR | S_IWUSR | IPC_CREAT);
e08109cb
KS
2828
2829if ($list[0]) {
912541b0
KS
2830 foreach my $arg (@list) {
2831 if (defined($tests[$arg-1]->{desc})) {
2832 print "udev-test will run test number $arg:\n\n";
09a4062d 2833 run_test($tests[$arg-1], $arg, $sema);
912541b0
KS
2834 } else {
2835 print "test does not exist.\n";
2836 }
2837 }
2e317184 2838} else {
912541b0
KS
2839 # test all
2840 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
2e317184 2841
912541b0 2842 foreach my $rules (@tests) {
09a4062d 2843 run_test($rules, $test_num, $sema);
912541b0
KS
2844 $test_num++;
2845 }
2e317184
GKH
2846}
2847
09a4062d 2848$sema->remove;
cbeb23d8 2849print "$error errors occurred. $good/$exp_good good results.\n\n";
a367f04e 2850
abb9cc50 2851cleanup();
a367f04e 2852
034b37c8 2853if ($error > 0) {
d3fc8bf4 2854 exit(1);
034b37c8
AJ
2855}
2856exit(0);