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