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