]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
sd-device,udev: refuse invalid devlink and store in normalized form
[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
YW
1810 rules => <<EOF
1811KERNEL=="sda", TAG=""
1812TAGS=="|foo", SYMLINK+="found"
1813TAGS=="aaa|bbb", SYMLINK+="bad"
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
YW
1838 rules => <<EOF
1839KERNEL=="sda", TAG=""
1840TAGS=="foo||bar", SYMLINK+="found"
1841TAGS=="aaa|bbb", SYMLINK+="bad"
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
YW
1866 rules => <<EOF
1867KERNEL=="sda", TAG=""
1868TAGS=="foo|", SYMLINK+="found"
1869TAGS=="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"
0bfb84e1 1884EOF
912541b0
KS
1885 },
1886 {
a96cd21d 1887 desc => "IMPORT parent test",
255c05b7
MW
1888 devices => [
1889 {
1890 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1891 exp_links => ["parent"],
a96cd21d 1892 },
255c05b7
MW
1893 {
1894 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1895 exp_links => ["parentenv-parent_right"],
255c05b7 1896 }],
a96cd21d 1897 sleep_us => 500000, # Serialized! We need to sleep here after adding sda
912541b0 1898 rules => <<EOF
5754e74c 1899KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
a96cd21d
MW
1900KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1901KERNEL=="sda", SYMLINK+="parent"
594dd610 1902EOF
912541b0
KS
1903 },
1904 {
1905 desc => "GOTO test",
255c05b7
MW
1906 devices => [
1907 {
1908 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1909 exp_links => ["right"],
1910 not_exp_test => ["wrong", "wrong2"],
255c05b7 1911 }],
912541b0 1912 rules => <<EOF
594dd610 1913KERNEL=="sda1", GOTO="TEST"
5754e74c 1914KERNEL=="sda1", SYMLINK+="wrong"
6880b25d 1915KERNEL=="sda1", GOTO="BAD"
5754e74c
KS
1916KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1917KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1918KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
9dae0e89 1919LABEL="end"
0c377989 1920EOF
912541b0
KS
1921 },
1922 {
1923 desc => "GOTO label does not exist",
255c05b7
MW
1924 devices => [
1925 {
1926 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1927 exp_links => ["right"],
255c05b7 1928 }],
912541b0 1929 rules => <<EOF
0c377989 1930KERNEL=="sda1", GOTO="does-not-exist"
5754e74c 1931KERNEL=="sda1", SYMLINK+="right",
0c377989 1932LABEL="exists"
d59c84ef 1933EOF
912541b0
KS
1934 },
1935 {
1936 desc => "SYMLINK+ compare test",
255c05b7
MW
1937 devices => [
1938 {
1939 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1940 exp_links => ["right", "link"],
1941 not_exp_links => ["wrong"],
255c05b7 1942 }],
912541b0 1943 rules => <<EOF
5754e74c
KS
1944KERNEL=="sda1", SYMLINK+="link"
1945KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1946KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
d59c84ef 1947EOF
912541b0
KS
1948 },
1949 {
1950 desc => "invalid key operation",
255c05b7
MW
1951 devices => [
1952 {
1953 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1954 exp_links => ["yes"],
1955 not_exp_links => ["no"],
255c05b7 1956 }],
912541b0 1957 rules => <<EOF
5754e74c
KS
1958KERNEL="sda1", SYMLINK+="no"
1959KERNEL=="sda1", SYMLINK+="yes"
864b9b5e 1960EOF
912541b0
KS
1961 },
1962 {
1963 desc => "operator chars in attribute",
255c05b7
MW
1964 devices => [
1965 {
1966 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1967 exp_links => ["yes"],
255c05b7 1968 }],
912541b0 1969 rules => <<EOF
5754e74c 1970KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
d4ae9925 1971EOF
912541b0
KS
1972 },
1973 {
1974 desc => "overlong comment line",
255c05b7
MW
1975 devices => [
1976 {
1977 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1978 exp_links => ["yes"],
1979 not_exp_links => ["no"],
255c05b7 1980 }],
912541b0 1981 rules => <<EOF
d4ae9925 1982# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
d960ad15 1983 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
5754e74c
KS
1984KERNEL=="sda1", SYMLINK+=="no"
1985KERNEL=="sda1", SYMLINK+="yes"
4ad47b2d 1986EOF
912541b0
KS
1987 },
1988 {
1989 desc => "magic subsys/kernel lookup",
255c05b7
MW
1990 devices => [
1991 {
1992 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1993 exp_links => ["00:16:41:e2:8d:ff"],
255c05b7 1994 }],
912541b0 1995 rules => <<EOF
5754e74c 1996KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
03f65fe6 1997EOF
912541b0
KS
1998 },
1999 {
2000 desc => "TEST absolute path",
255c05b7
MW
2001 devices => [
2002 {
2003 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
2004 exp_links => ["there"],
2005 not_exp_links => ["notthere"],
255c05b7 2006 }],
912541b0 2007 rules => <<EOF
f0289577
LB
2008TEST=="/etc/passwd", SYMLINK+="there"
2009TEST!="/etc/passwd", SYMLINK+="notthere"
03f65fe6 2010EOF
912541b0
KS
2011 },
2012 {
2013 desc => "TEST subsys/kernel lookup",
255c05b7
MW
2014 devices => [
2015 {
2016 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2017 exp_links => ["yes"],
255c05b7 2018 }],
912541b0 2019 rules => <<EOF
5754e74c 2020KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
03f65fe6 2021EOF
912541b0
KS
2022 },
2023 {
2024 desc => "TEST relative path",
255c05b7
MW
2025 devices => [
2026 {
2027 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2028 exp_links => ["relative"],
255c05b7 2029 }],
912541b0 2030 rules => <<EOF
5754e74c 2031KERNEL=="sda", TEST=="size", SYMLINK+="relative"
0ea5e96e 2032EOF
912541b0
KS
2033 },
2034 {
2035 desc => "TEST wildcard substitution (find queue/nr_requests)",
255c05b7
MW
2036 devices => [
2037 {
2038 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2039 exp_links => ["found-subdir"],
255c05b7 2040 }],
912541b0 2041 rules => <<EOF
5754e74c 2042KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
cf100ca7 2043EOF
912541b0
KS
2044 },
2045 {
2046 desc => "TEST MODE=0000",
255c05b7
MW
2047 devices => [
2048 {
2049 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
2050 exp_perms => "0:0:0000",
2051 exp_rem_error => "yes",
2052 }],
912541b0 2053 rules => <<EOF
cf100ca7 2054KERNEL=="sda", MODE="0000"
a367f04e 2055EOF
912541b0
KS
2056 },
2057 {
2058 desc => "TEST PROGRAM feeds OWNER, GROUP, MODE",
255c05b7
MW
2059 devices => [
2060 {
2061 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7 2062 exp_perms => "1:1:0400",
255c05b7 2063 }],
912541b0 2064 rules => <<EOF
6880b25d 2065KERNEL=="sda", MODE="666"
9158d03e 2066KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
ff94cec3 2067EOF
912541b0
KS
2068 },
2069 {
2070 desc => "TEST PROGRAM feeds MODE with overflow",
255c05b7
MW
2071 devices => [
2072 {
2073 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
2074 exp_perms => "0:0:0440",
2075 exp_rem_error => "yes",
2076 }],
912541b0 2077 rules => <<EOF
6880b25d 2078KERNEL=="sda", MODE="440"
d914e445 2079KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
dc4c7e46 2080EOF
912541b0
KS
2081 },
2082 {
2083 desc => "magic [subsys/sysname] attribute substitution",
255c05b7
MW
2084 devices => [
2085 {
2086 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2087 exp_links => ["sda-8741C4G-end"],
255c05b7
MW
2088 exp_perms => "0:0:0600",
2089 }],
912541b0 2090 rules => <<EOF
0f52fdee 2091KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
d7867b31 2092EOF
912541b0
KS
2093 },
2094 {
2095 desc => "builtin path_id",
255c05b7
MW
2096 devices => [
2097 {
2098 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2099 exp_links => ["disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0"],
255c05b7 2100 }],
912541b0 2101 rules => <<EOF
d7867b31
KS
2102KERNEL=="sda", IMPORT{builtin}="path_id"
2103KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/\$env{ID_PATH}"
bf8f7583
MP
2104EOF
2105 },
2106 {
2107 desc => "add and match tag",
255c05b7
MW
2108 devices => [
2109 {
2110 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
2111 exp_links => ["found"],
2112 not_exp_links => ["bad"],
255c05b7 2113 }],
bf8f7583
MP
2114 rules => <<EOF
2115SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", TAG+="green"
2116TAGS=="green", SYMLINK+="found"
2117TAGS=="blue", SYMLINK+="bad"
2118EOF
2119 },
2120 {
2121 desc => "don't crash with lots of tags",
255c05b7
MW
2122 devices => [
2123 {
2124 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2125 exp_links => ["found"],
255c05b7 2126 }],
bf8f7583
MP
2127 rules => $rules_10k_tags . <<EOF
2128TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="found"
1e797cf5
YW
2129EOF
2130 },
2131 {
d35976c6 2132 desc => "continuations",
255c05b7
MW
2133 devices => [
2134 {
2135 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2136 exp_links => ["found"],
255c05b7
MW
2137 not_exp_name => "bad",
2138 }],
1e797cf5
YW
2139 rules => $rules_10k_tags_continuation . <<EOF
2140TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="bad"
d35976c6
YW
2141KERNEL=="sda",\\
2142# comment in continuation
2143TAG+="hoge1",\\
2144 # space before comment
2145TAG+="hoge2",\\
2146# spaces before and after token are dropped
2147 TAG+="hoge3", \\
84a0819c
YW
2148\\
2149 \\
d35976c6
YW
2150TAG+="hoge4"
2151TAGS=="hoge1", TAGS=="hoge2", TAGS=="hoge3", TAGS=="hoge4", SYMLINK+="found"
84a0819c
YW
2152EOF
2153 },
2154 {
2155 desc => "continuations with empty line",
255c05b7
MW
2156 devices => [
2157 {
2158 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2159 exp_links => ["found"],
255c05b7
MW
2160 not_exp_name => "bad",
2161
2162 }],
84a0819c
YW
2163 rules => <<EOF
2164# empty line finishes continuation
2165KERNEL=="sda", TAG+="foo" \\
2166
2167KERNEL=="sdb", TAG+="hoge"
2168KERNEL=="sda", TAG+="aaa" \\
2169KERNEL=="sdb", TAG+="bbb"
2170TAGS=="foo", SYMLINK+="found"
2171TAGS=="aaa", SYMLINK+="bad"
255c05b7 2172 EOF
84a0819c
YW
2173 },
2174 {
2175 desc => "continuations with white only line",
255c05b7
MW
2176 devices => [
2177 {
2178 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2179 exp_links => ["found"],
255c05b7
MW
2180 not_exp_name => "bad",
2181 }],
84a0819c
YW
2182 rules => <<EOF
2183# space only line finishes continuation
2184KERNEL=="sda", TAG+="foo" \\
2185 \t
2186KERNEL=="sdb", TAG+="hoge"
2187KERNEL=="sda", TAG+="aaa" \\
2188KERNEL=="sdb", TAG+="bbb"
2189TAGS=="foo", SYMLINK+="found"
2190TAGS=="aaa", SYMLINK+="bad"
4a0ec82d
MW
2191EOF
2192 },
2193 {
2194 desc => "multiple devices",
2195 devices => [
2196 {
2197 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2198 exp_links => ["part-1"],
2199 },
2200 {
2201 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2202 exp_links => ["part-5"],
2203 },
2204 {
2205 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2206 exp_links => ["part-6"],
2207 },
2208 {
2209 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2210 exp_links => ["part-7"],
2211 },
2212 {
2213 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2214 exp_links => ["part-8"],
2215 },
2216 {
2217 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2218 exp_links => ["part-9"],
2219 },
2220 {
2221 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2222 exp_links => ["part-10"],
2223 },
2224 ],
2225 rules => <<EOF
2226SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2227EOF
2228 },
2229 {
2230 desc => "multiple devices, same link name, positive prio",
2ab0a8d0 2231 repeat => 100,
4a0ec82d
MW
2232 devices => [
2233 {
2234 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2235 exp_links => ["part-1"],
2236 not_exp_links => ["partition"],
2237 },
2238 {
2239 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2240 exp_links => ["part-5"],
2241 not_exp_links => ["partition"],
2242 },
2243 {
2244 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2245 not_exp_links => ["partition"],
2246 exp_links => ["part-6"],
2247 },
2248 {
2249 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2250 exp_links => ["part-7", "partition"],
2251 },
2252 {
2253 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2254 not_exp_links => ["partition"],
2255 exp_links => ["part-8"],
2256 },
2257 {
2258 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2259 not_exp_links => ["partition"],
2260 exp_links => ["part-9"],
2261 },
2262 {
2263 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2264 not_exp_links => ["partition"],
2265 exp_links => ["part-10"],
2266 },
2267 ],
2268 rules => <<EOF
2269SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2270SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2271KERNEL=="*7", OPTIONS+="link_priority=10"
2272EOF
2273 },
2274 {
2275 desc => "multiple devices, same link name, negative prio",
2276 devices => [
2277 {
2278 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2279 exp_links => ["part-1"],
2280 not_exp_links => ["partition"],
2281 },
2282 {
2283 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2284 exp_links => ["part-5"],
2285 not_exp_links => ["partition"],
2286 },
2287 {
2288 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2289 not_exp_links => ["partition"],
2290 exp_links => ["part-6"],
2291 },
2292 {
2293 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2294 exp_links => ["part-7", "partition"],
2295 },
2296 {
2297 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2298 not_exp_links => ["partition"],
2299 exp_links => ["part-8"],
2300 },
2301 {
2302 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2303 not_exp_links => ["partition"],
2304 exp_links => ["part-9"],
2305 },
2306 {
2307 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2308 not_exp_links => ["partition"],
2309 exp_links => ["part-10"],
2310 },
2311 ],
2312 rules => <<EOF
2313SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2314SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2315KERNEL!="*7", OPTIONS+="link_priority=-10"
2316EOF
2317 },
2318 {
2319 desc => "multiple devices, same link name, positive prio, sleep",
2320 devices => [
2321 {
2322 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2323 exp_links => ["part-1"],
2324 not_exp_links => ["partition"],
2325 },
2326 {
2327 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2328 exp_links => ["part-5"],
2329 not_exp_links => ["partition"],
2330 },
2331 {
2332 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2333 not_exp_links => ["partition"],
2334 exp_links => ["part-6"],
2335 },
2336 {
2337 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2338 exp_links => ["part-7", "partition"],
2339 },
2340 {
2341 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2342 not_exp_links => ["partition"],
2343 exp_links => ["part-8"],
2344 },
2345 {
2346 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2347 not_exp_links => ["partition"],
2348 exp_links => ["part-9"],
2349 },
2350 {
2351 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2352 not_exp_links => ["partition"],
2353 exp_links => ["part-10"],
2354 },
2355 ],
2356 sleep_us => 10000,
2357 rules => <<EOF
2358SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2359SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2360KERNEL=="*7", OPTIONS+="link_priority=10"
ff94cec3 2361EOF
912541b0 2362 },
eb44d715
MW
2363 {
2364 desc => 'all_block_devs',
2365 generator => expect_for_some("\\/sda6\$", ["blockdev"]),
2366 repeat => 10,
2367 rules => <<EOF
2368SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sd*", SYMLINK+="blockdev"
2369KERNEL=="sda6", OPTIONS+="link_priority=10"
2370EOF
2371 }
a367f04e
GKH
2372);
2373
af7ee3ea
MW
2374sub create_rules {
2375 my ($rules) = @_;
a367f04e 2376
912541b0 2377 # create temporary rules
6ada823a 2378 system("mkdir", "-p", "$udev_rules_dir");
912541b0
KS
2379 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
2380 print CONF $$rules;
2381 close CONF;
af7ee3ea
MW
2382}
2383
2384sub udev {
2385 my ($action, $devpath) = @_;
a367f04e 2386
912541b0 2387 if ($valgrind > 0) {
efc9f703 2388 return system("$udev_bin_valgrind $action $devpath");
333e07b7 2389 } elsif ($gdb > 0) {
efc9f703 2390 return system("$udev_bin_gdb $action $devpath");
587751eb 2391 } elsif ($strace > 0) {
efc9f703 2392 return system("$udev_bin_strace $action $devpath");
912541b0 2393 } else {
efc9f703 2394 return system("$udev_bin", "$action", "$devpath");
912541b0 2395 }
a367f04e
GKH
2396}
2397
e5fbfe0a 2398my $error = 0;
b95c4398 2399my $good = 0;
cbeb23d8 2400my $exp_good = 0;
72ffa78d 2401
b8669191 2402sub permissions_test {
912541b0 2403 my($rules, $uid, $gid, $mode) = @_;
b8669191 2404
912541b0
KS
2405 my $wrong = 0;
2406 my $userid;
2407 my $groupid;
9b434de1 2408
912541b0
KS
2409 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
2410 if ($1 ne "") {
2411 if (defined(getpwnam($1))) {
2412 $userid = int(getpwnam($1));
2413 } else {
2414 $userid = $1;
2415 }
2416 if ($uid != $userid) { $wrong = 1; }
2417 }
2418 if ($2 ne "") {
2419 if (defined(getgrnam($2))) {
2420 $groupid = int(getgrnam($2));
2421 } else {
2422 $groupid = $2;
2423 }
2424 if ($gid != $groupid) { $wrong = 1; }
2425 }
2426 if ($3 ne "") {
2427 if (($mode & 07777) != oct($3)) { $wrong = 1; };
2428 }
2429 if ($wrong == 0) {
2430 print "permissions: ok\n";
b95c4398 2431 $good++;
912541b0
KS
2432 } else {
2433 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
2434 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
2435 print "permissions: error\n";
2436 $error++;
2437 sleep(1);
2438 }
b8669191
GKH
2439}
2440
2441sub major_minor_test {
912541b0 2442 my($rules, $rdev) = @_;
b8669191 2443
912541b0
KS
2444 my $major = ($rdev >> 8) & 0xfff;
2445 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
2446 my $wrong = 0;
b8669191 2447
912541b0
KS
2448 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
2449 if ($1 ne "") {
2450 if ($major != $1) { $wrong = 1; };
2451 }
2452 if ($2 ne "") {
2453 if ($minor != $2) { $wrong = 1; };
2454 }
2455 if ($wrong == 0) {
2456 print "major:minor: ok\n";
b95c4398 2457 $good++;
912541b0
KS
2458 } else {
2459 printf " expected major:minor is: %i:%i\n", $1, $2;
2460 printf " created major:minor is : %i:%i\n", $major, $minor;
2461 print "major:minor: error\n";
2462 $error++;
2463 sleep(1);
2464 }
b8669191
GKH
2465}
2466
6ada823a 2467sub udev_setup {
f1cb0860 2468 system("umount \"$udev_tmpfs\" 2>/dev/null");
21d9e3f3
EV
2469 rmdir($udev_tmpfs);
2470 mkdir($udev_tmpfs) || die "unable to create udev_tmpfs: $udev_tmpfs\n";
110a1320 2471
9f563f27 2472 if (system("mount", "-o", "rw,mode=0755,nosuid,noexec", "-t", "tmpfs", "tmpfs", $udev_tmpfs)) {
110a1320
EV
2473 warn "unable to mount tmpfs";
2474 return 0;
2475 }
21d9e3f3 2476
6ada823a
KS
2477 mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
2478 # setting group and mode of udev_dev ensures the tests work
912541b0 2479 # even if the parent directory has setgid bit enabled.
6ada823a
KS
2480 chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
2481 chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
110a1320
EV
2482
2483 if (system("mknod", $udev_dev . "/null", "c", "1", "3")) {
2484 warn "unable to create $udev_dev/null";
2485 return 0;
2486 }
6ada823a 2487
dbfbc6c4
AB
2488 # check if we are permitted to create block device nodes
2489 my $block_device_filename = $udev_dev . "/sda";
2490 if (system("mknod", $block_device_filename, "b", "8", "0")) {
2491 warn "unable to create $block_device_filename";
2492 return 0;
2493 }
2494 unlink $block_device_filename;
2495
21d9e3f3 2496 system("cp", "-r", "test/sys/", $udev_sys) && die "unable to copy test/sys";
2ce8d27b 2497
6ada823a 2498 system("rm", "-rf", "$udev_run");
110a1320 2499
1e5548c0
AB
2500 if (!mkdir($udev_run)) {
2501 warn "unable to create directory $udev_run";
2502 return 0;
2503 }
2504
110a1320 2505 return 1;
ff94cec3
EK
2506}
2507
f0dccf01
MW
2508sub get_devnode {
2509 my ($device) = @_;
2510 my $devnode;
2511
2512 if (defined($device->{devnode})) {
2513 $devnode = "$udev_dev/$device->{devnode}";
2514 } else {
2515 $devnode = "$device->{devpath}";
2516 $devnode =~ s!.*/!$udev_dev/!;
2517 }
2518 return $devnode;
2519}
2520
2521sub check_devnode {
2522 my ($device) = @_;
2523 my $devnode = get_devnode($device);
2524
2525 my @st = lstat("$devnode");
2526 if (! (-b _ || -c _)) {
2527 print "add $devnode: error\n";
2528 system("tree", "$udev_dev");
2529 $error++;
2530 return undef;
2531 }
2532
2533 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
2534 $atime, $mtime, $ctime, $blksize, $blocks) = @st;
2535
2536 if (defined($device->{exp_perms})) {
2537 permissions_test($device, $uid, $gid, $mode);
2538 }
2539 if (defined($device->{exp_majorminor})) {
2540 major_minor_test($device, $rdev);
2541 }
2542 print "add $devnode: ok\n";
b95c4398 2543 $good++;
f0dccf01
MW
2544 return $devnode;
2545}
2546
e62acc31
MW
2547sub get_link_target {
2548 my ($link) = @_;
09a4062d 2549
e62acc31
MW
2550 my $cwd = getcwd();
2551 my $dir = "$udev_dev/$link";
2552 my $tgt = readlink("$udev_dev/$link");
2553 $dir =~ s!/[^/]*$!!;
2554 $tgt = abs_path("$dir/$tgt");
2555 $tgt =~ s!^$cwd/!!;
2556 return $tgt;
2557}
f0dccf01 2558
e62acc31
MW
2559sub check_link_add {
2560 my ($link, $devnode, $err_expected) = @_;
f0dccf01 2561
e62acc31 2562 my @st = lstat("$udev_dev/$link");
997683c8 2563 if (-l _) {
e62acc31 2564 my $tgt = get_link_target($link);
997683c8
MW
2565
2566 if ($tgt ne $devnode) {
e62acc31 2567 print "symlink $link: error, found -> $tgt\n";
997683c8
MW
2568 $error++;
2569 system("tree", "$udev_dev");
2570 } else {
e62acc31 2571 print "symlink $link: ok\n";
b95c4398 2572 $good++;
997683c8 2573 }
912541b0 2574 } else {
e62acc31
MW
2575 print "symlink $link: error";
2576 if ($err_expected) {
912541b0 2577 print " as expected\n";
b95c4398 2578 $good++;
912541b0
KS
2579 } else {
2580 print "\n";
6ada823a 2581 system("tree", "$udev_dev");
912541b0
KS
2582 print "\n";
2583 $error++;
2584 sleep(1);
2585 }
2586 }
255c05b7 2587}
a367f04e 2588
e62acc31
MW
2589sub check_link_nonexistent {
2590 my ($link, $devnode, $err_expected) = @_;
2591
2592 if ((-e "$udev_dev/$link") || (-l "$udev_dev/$link")) {
2593 my $tgt = get_link_target($link);
2594
2595 if ($tgt ne $devnode) {
2596 print "nonexistent: '$link' points to other device (ok)\n";
b95c4398 2597 $good++;
e62acc31
MW
2598 } else {
2599 print "nonexistent: error \'$link\' should not be there";
2600 if ($err_expected) {
2601 print " (as expected)\n";
b95c4398 2602 $good++;
e62acc31
MW
2603 } else {
2604 print "\n";
2605 system("tree", "$udev_dev");
2606 print "\n";
2607 $error++;
2608 sleep(1);
2609 }
2610 }
2611 } else {
2612 print "nonexistent $link: ok\n";
b95c4398 2613 $good++;
e62acc31
MW
2614 }
2615}
2616
2617sub check_add {
2618 my ($device) = @_;
2619 my $devnode = check_devnode($device);
2620
2621 if (defined($device->{exp_links})) {
2622 foreach my $link (@{$device->{exp_links}}) {
2623 check_link_add($link, $devnode,
2624 $device->{exp_add_error});
2625 }
2626 }
2627 if (defined $device->{not_exp_links}) {
2628 foreach my $link (@{$device->{not_exp_links}}) {
2629 check_link_nonexistent($link, $devnode,
2630 $device->{exp_nodev_error});
2631 }
2632 }
2633}
2634
f0dccf01
MW
2635sub check_remove_devnode {
2636 my ($device) = @_;
2637 my $devnode = get_devnode($device);
2638
2639 if (-e "$devnode") {
2640 print "remove $devnode: error";
2641 print "\n";
2642 system("tree", "$udev_dev");
2643 print "\n";
2644 $error++;
2645 sleep(1);
2646 } else {
2647 print "remove $devnode: ok\n";
b95c4398 2648 $good++;
f0dccf01
MW
2649 }
2650}
2651
e62acc31
MW
2652sub check_link_remove {
2653 my ($link, $err_expected) = @_;
f0dccf01 2654
e62acc31
MW
2655 if ((-e "$udev_dev/$link") ||
2656 (-l "$udev_dev/$link")) {
2657 print "remove $link: error";
2658 if ($err_expected) {
912541b0 2659 print " as expected\n";
b95c4398 2660 $good++;
912541b0
KS
2661 } else {
2662 print "\n";
6ada823a 2663 system("tree", "$udev_dev");
912541b0
KS
2664 print "\n";
2665 $error++;
2666 sleep(1);
2667 }
2668 } else {
e62acc31 2669 print "remove $link: ok\n";
b95c4398 2670 $good++;
e62acc31
MW
2671 }
2672}
2673
2674sub check_remove {
2675 my ($device) = @_;
2676
2677 check_remove_devnode($device);
2678
2679 return if (!defined($device->{exp_links}));
2680
2681 foreach my $link (@{$device->{exp_links}}) {
2682 check_link_remove($link, $device->{exp_rem_error});
255c05b7
MW
2683 }
2684}
2685
09a4062d
MW
2686sub run_udev {
2687 my ($action, $dev, $sleep_us, $sema) = @_;
2688
2689 # Notify main process that this worker has started
2690 $sema->op(0, 1, 0);
2691
2692 # Wait for start
2693 $sema->op(0, 0, 0);
2694 usleep($sleep_us) if defined ($sleep_us);
2695 my $rc = udev($action, $dev->{devpath});
2696 exit $rc;
2697}
2698
2699sub fork_and_run_udev {
2700 my ($action, $rules, $sema) = @_;
2701 my @devices = @{$rules->{devices}};
2702 my $dev;
2703 my $k = 0;
2704
2705 $sema->setval(0, 1);
2706 foreach $dev (@devices) {
2707 my $pid = fork();
2708
2709 if (!$pid) {
2710 run_udev($action, $dev,
2711 defined($rules->{sleep_us}) ? $k * $rules->{sleep_us} : undef,
2712 $sema);
2713 } else {
2714 $dev->{pid} = $pid;
2715 }
2716 $k++;
2717 }
2718
2719 # This operation waits for all workers to become ready, and
2720 # starts them off when that's the case.
2721 $sema->op(0, -($#devices + 2), 0);
2722
2723 foreach $dev (@devices) {
2724 my $rc;
2725 my $pid;
2726
2727 $pid = waitpid($dev->{pid}, 0);
2728 if ($pid == -1) {
2729 print "error waiting for pid dev->{pid}\n";
09a4062d
MW
2730 }
2731 if (WIFEXITED($?)) {
2732 $rc = WEXITSTATUS($?);
2733
2734 if ($rc) {
2735 print "$udev_bin $action for $dev->{devpath} failed with code $rc\n";
2736 $error += 1;
b95c4398
MW
2737 } else {
2738 $good++;
09a4062d
MW
2739 }
2740 }
2741 }
2742}
2743
255c05b7 2744sub run_test {
09a4062d 2745 my ($rules, $number, $sema) = @_;
255c05b7 2746 my $rc;
eb44d715 2747 my @devices;
cbeb23d8
MW
2748 my $ntests;
2749 my $cur_good = $good;
2750 my $cur_error = $error;
eb44d715
MW
2751
2752 if (!defined $rules->{devices}) {
2753 $rules->{devices} = all_block_devs($rules->{generator});
2754 }
2755 @devices = @{$rules->{devices}};
cbeb23d8
MW
2756 # For each device: exit status and devnode test for add & remove
2757 $ntests += 4 * ($#devices + 1);
255c05b7 2758
cbeb23d8
MW
2759 foreach my $dev (@devices) {
2760 $ntests += 2 * ($#{$dev->{exp_links}} + 1)
2761 + ($#{$dev->{not_exp_links}} + 1)
2762 + (defined $dev->{exp_perms} ? 1 : 0)
2763 + (defined $dev->{exp_majorminor} ? 1 : 0);
2764 }
2765 if (defined $rules->{repeat}) {
2766 $ntests *= $rules->{repeat};
2767 }
2768 $exp_good += $ntests;
255c05b7 2769 print "TEST $number: $rules->{desc}\n";
af7ee3ea 2770 create_rules(\$rules->{rules});
09a4062d 2771
2ab0a8d0 2772 REPEAT:
09a4062d 2773 fork_and_run_udev("add", $rules, $sema);
255c05b7
MW
2774
2775 foreach my $dev (@devices) {
2776 check_add($dev);
2777 }
2778
2779 if (defined($rules->{option}) && $rules->{option} eq "keep") {
2780 print "\n\n";
2781 return;
2782 }
2783
09a4062d
MW
2784 fork_and_run_udev("remove", $rules, $sema);
2785
255c05b7
MW
2786 foreach my $dev (@devices) {
2787 check_remove($dev);
912541b0 2788 }
0345b862 2789
2ab0a8d0
MW
2790 if (defined($rules->{repeat}) && --($rules->{repeat}) > 0) {
2791 goto REPEAT;
2792 }
cbeb23d8
MW
2793 printf "TEST $number: errors: %d good: %d/%d\n\n", $error-$cur_error,
2794 $good-$cur_good, $ntests;
9b434de1 2795
912541b0 2796 if (defined($rules->{option}) && $rules->{option} eq "clean") {
6ada823a 2797 udev_setup();
912541b0 2798 }
0345b862 2799
a367f04e
GKH
2800}
2801
abb9cc50
DS
2802sub cleanup {
2803 system("rm", "-rf", "$udev_run");
2804 system("umount", "$udev_tmpfs");
2805 rmdir($udev_tmpfs);
2806}
2807
800ab95b
GKH
2808# only run if we have root permissions
2809# due to mknod restrictions
2810if (!($<==0)) {
912541b0 2811 print "Must have root permissions to run properly.\n";
4d55fc5b 2812 exit($EXIT_TEST_SKIP);
800ab95b
GKH
2813}
2814
e4d214ef
DC
2815# skip the test when running in a chroot
2816system("systemd-detect-virt", "-r", "-q");
2817if ($? >> 8 == 0) {
ce5fcc69
ZJS
2818 print "Running in a chroot, skipping the test.\n";
2819 exit($EXIT_TEST_SKIP);
e4d214ef
DC
2820}
2821
110a1320
EV
2822if (!udev_setup()) {
2823 warn "Failed to set up the environment, skipping the test";
abb9cc50 2824 cleanup();
110a1320
EV
2825 exit($EXIT_TEST_SKIP);
2826}
2827
7935dae5 2828if (system($udev_bin, "check")) {
110a1320 2829 warn "$udev_bin failed to set up the environment, skipping the test";
abb9cc50 2830 cleanup();
110a1320
EV
2831 exit($EXIT_TEST_SKIP);
2832}
2e317184
GKH
2833
2834my $test_num = 1;
e08109cb 2835my @list;
2e317184 2836
e08109cb 2837foreach my $arg (@ARGV) {
912541b0
KS
2838 if ($arg =~ m/--valgrind/) {
2839 $valgrind = 1;
2840 printf("using valgrind\n");
333e07b7
TG
2841 } elsif ($arg =~ m/--gdb/) {
2842 $gdb = 1;
2843 printf("using gdb\n");
587751eb
ZJS
2844 } elsif ($arg =~ m/--strace/) {
2845 $strace = 1;
2846 printf("using strace\n");
912541b0
KS
2847 } else {
2848 push(@list, $arg);
2849 }
e08109cb 2850}
09a4062d 2851my $sema = IPC::Semaphore->new(IPC_PRIVATE, 1, S_IRUSR | S_IWUSR | IPC_CREAT);
e08109cb
KS
2852
2853if ($list[0]) {
912541b0
KS
2854 foreach my $arg (@list) {
2855 if (defined($tests[$arg-1]->{desc})) {
2856 print "udev-test will run test number $arg:\n\n";
09a4062d 2857 run_test($tests[$arg-1], $arg, $sema);
912541b0
KS
2858 } else {
2859 print "test does not exist.\n";
2860 }
2861 }
2e317184 2862} else {
912541b0
KS
2863 # test all
2864 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
2e317184 2865
912541b0 2866 foreach my $rules (@tests) {
09a4062d 2867 run_test($rules, $test_num, $sema);
912541b0
KS
2868 $test_num++;
2869 }
2e317184
GKH
2870}
2871
09a4062d 2872$sema->remove;
cbeb23d8 2873print "$error errors occurred. $good/$exp_good good results.\n\n";
a367f04e 2874
abb9cc50 2875cleanup();
a367f04e 2876
034b37c8 2877if ($error > 0) {
d3fc8bf4 2878 exit(1);
034b37c8
AJ
2879}
2880exit(0);