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