]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/udev-test.pl
allow multiple values to be matched with KEY=="value1|value2"
[thirdparty/systemd.git] / test / udev-test.pl
1 #!/usr/bin/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 # Kay Sievers <kay.sievers@vrfy.org>, 2003
18 # Leann Ogasawara <ogasawara@osdl.org>, 2004
19
20 use warnings;
21 use strict;
22
23 my $PWD = $ENV{PWD};
24 my $sysfs = "sys/";
25 my $udev_bin = "../udev";
26 my $udev_root = "udev-root/"; # !!! directory will be removed !!!
27 my $udev_db = ".udevdb";
28 my $udev_conf = "udev-test.conf";
29 my $udev_rules = "udev-test.rules";
30
31 # uncomment following line to run udev with valgrind.
32 # Should make this a runtime option to the script someday...
33 #my $udev_bin = "valgrind --tool=memcheck --leak-check=yes ../udev";
34
35 my @tests = (
36 {
37 desc => "label test of scsi disc",
38 subsys => "block",
39 devpath => "/block/sda",
40 exp_name => "boot_disk" ,
41 rules => <<EOF
42 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="boot_disk%n"
43 KERNEL=="ttyUSB0", NAME="visor"
44 EOF
45 },
46 {
47 desc => "label test of scsi partition",
48 subsys => "block",
49 devpath => "/block/sda/sda1",
50 exp_name => "boot_disk1" ,
51 rules => <<EOF
52 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="boot_disk%n"
53 EOF
54 },
55 {
56 desc => "label test of pattern match",
57 subsys => "block",
58 devpath => "/block/sda/sda1",
59 exp_name => "boot_disk1" ,
60 rules => <<EOF
61 BUS=="scsi", SYSFS{vendor}=="?IBM-ESXS", NAME="boot_disk%n-1"
62 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS?", NAME="boot_disk%n-2"
63 BUS=="scsi", SYSFS{vendor}=="IBM-ES??", NAME="boot_disk%n"
64 BUS=="scsi", SYSFS{vendor}=="IBM-ESXSS", NAME="boot_disk%n-3"
65 EOF
66 },
67 {
68 desc => "label test of multiple sysfs files",
69 subsys => "block",
70 devpath => "/block/sda/sda1",
71 exp_name => "boot_disk1" ,
72 rules => <<EOF
73 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", NAME="boot_diskX%n"
74 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", NAME="boot_disk%n"
75 EOF
76 },
77 {
78 desc => "label test of max sysfs files",
79 subsys => "block",
80 devpath => "/block/sda/sda1",
81 exp_name => "boot_disk1" ,
82 rules => <<EOF
83 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", SYSFS{scsi_level}=="4", SYSFS{rev}=="B245", SYSFS{type}=="2", SYSFS{queue_depth}=="32", NAME="boot_diskXX%n"
84 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", SYSFS{scsi_level}=="4", SYSFS{rev}=="B245", SYSFS{type}=="0", NAME="boot_disk%n"
85 EOF
86 },
87 {
88 desc => "catch device by *",
89 subsys => "tty",
90 devpath => "/class/tty/ttyUSB0",
91 exp_name => "visor/0" ,
92 rules => <<EOF
93 KERNEL=="ttyUSB*", NAME="visor/%n"
94 EOF
95 },
96 {
97 desc => "catch device by * - take 2",
98 subsys => "tty",
99 devpath => "/class/tty/ttyUSB0",
100 exp_name => "visor/0" ,
101 rules => <<EOF
102 KERNEL=="*USB1", NAME="bad"
103 KERNEL=="*USB0", NAME="visor/%n"
104 EOF
105 },
106 {
107 desc => "catch device by ?",
108 subsys => "tty",
109 devpath => "/class/tty/ttyUSB0",
110 exp_name => "visor/0" ,
111 rules => <<EOF
112 KERNEL=="ttyUSB??*", NAME="visor/%n-1"
113 KERNEL=="ttyUSB??", NAME="visor/%n-2"
114 KERNEL=="ttyUSB?", NAME="visor/%n"
115 EOF
116 },
117 {
118 desc => "catch device by character class",
119 subsys => "tty",
120 devpath => "/class/tty/ttyUSB0",
121 exp_name => "visor/0" ,
122 rules => <<EOF
123 KERNEL=="ttyUSB[A-Z]*", NAME="visor/%n-1"
124 KERNEL=="ttyUSB?[0-9]", NAME="visor/%n-2"
125 KERNEL=="ttyUSB[0-9]*", NAME="visor/%n"
126 EOF
127 },
128 {
129 desc => "replace kernel name",
130 subsys => "tty",
131 devpath => "/class/tty/ttyUSB0",
132 exp_name => "visor" ,
133 rules => <<EOF
134 KERNEL=="ttyUSB0", NAME="visor"
135 EOF
136 },
137 {
138 desc => "Handle comment lines in config file (and replace kernel name)",
139 subsys => "tty",
140 devpath => "/class/tty/ttyUSB0",
141 exp_name => "visor" ,
142 rules => <<EOF
143 # this is a comment
144 KERNEL=="ttyUSB0", NAME="visor"
145
146 EOF
147 },
148 {
149 desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
150 subsys => "tty",
151 devpath => "/class/tty/ttyUSB0",
152 exp_name => "visor" ,
153 rules => <<EOF
154 # this is a comment with whitespace before the comment
155 KERNEL=="ttyUSB0", NAME="visor"
156
157 EOF
158 },
159 {
160 desc => "Handle whitespace only lines (and replace kernel name)",
161 subsys => "tty",
162 devpath => "/class/tty/ttyUSB0",
163 exp_name => "whitespace" ,
164 rules => <<EOF
165
166
167
168 # this is a comment with whitespace before the comment
169 KERNEL=="ttyUSB0", NAME="whitespace"
170
171
172
173 EOF
174 },
175 {
176 desc => "Handle empty lines in config file (and replace kernel name)",
177 subsys => "tty",
178 devpath => "/class/tty/ttyUSB0",
179 exp_name => "visor" ,
180 rules => <<EOF
181
182 KERNEL=="ttyUSB0", NAME="visor"
183
184 EOF
185 },
186 {
187 desc => "Handle backslashed multi lines in config file (and replace kernel name)",
188 subsys => "tty",
189 devpath => "/class/tty/ttyUSB0",
190 exp_name => "visor" ,
191 rules => <<EOF
192 KERNEL=="ttyUSB0", \\
193 NAME="visor"
194
195 EOF
196 },
197 {
198 desc => "preserve backslashes, if they are not for a newline",
199 subsys => "tty",
200 devpath => "/class/tty/ttyUSB0",
201 exp_name => "aaa",
202 rules => <<EOF
203 KERNEL=="ttyUSB0", PROGRAM=="/bin/echo -e \\101", RESULT=="A", NAME="aaa"
204 EOF
205 },
206 {
207 desc => "Handle stupid backslashed multi lines in config file (and replace kernel name)",
208 subsys => "tty",
209 devpath => "/class/tty/ttyUSB0",
210 exp_name => "visor" ,
211 rules => <<EOF
212
213 #
214 \\
215
216 \\\\
217
218 #\\
219
220 KERNEL=="ttyUSB0", \\
221 NAME="visor"
222
223 EOF
224 },
225 {
226 desc => "subdirectory handling",
227 subsys => "tty",
228 devpath => "/class/tty/ttyUSB0",
229 exp_name => "sub/direct/ory/visor" ,
230 rules => <<EOF
231 KERNEL=="ttyUSB0", NAME="sub/direct/ory/visor"
232 EOF
233 },
234 {
235 desc => "place on bus of scsi partition",
236 subsys => "block",
237 devpath => "/block/sda/sda3",
238 exp_name => "first_disk3" ,
239 rules => <<EOF
240 BUS=="scsi", ID=="0:0:0:0", NAME="first_disk%n"
241 EOF
242 },
243 {
244 desc => "test substitution chars",
245 subsys => "block",
246 devpath => "/block/sda/sda3",
247 exp_name => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
248 rules => <<EOF
249 BUS=="scsi", ID=="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
250 EOF
251 },
252 {
253 desc => "test substitution chars (with length limit)",
254 subsys => "block",
255 devpath => "/block/sda/sda3",
256 exp_name => "M8-m3-n3-b0:0-sIBM" ,
257 rules => <<EOF
258 BUS=="scsi", ID=="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}"
259 EOF
260 },
261 {
262 desc => "import of shell-value file",
263 subsys => "block",
264 devpath => "/block/sda",
265 exp_name => "subdir/sys/node" ,
266 rules => <<EOF
267 BUS=="scsi", IMPORT{file}="test.all", NAME="subdir%E{SYSFSDIR}/node"
268 KERNEL=="ttyUSB0", NAME="visor"
269 EOF
270 },
271 {
272 desc => "import of shell-value returned from program",
273 subsys => "block",
274 devpath => "/block/sda",
275 exp_name => "node12345678",
276 rules => <<EOF
277 BUS=="scsi", IMPORT="/bin/echo -e \' TEST_KEY=12345678 \\n TEST_key2=98765 \'", NAME="node\$env{TEST_KEY}"
278 KERNEL=="ttyUSB0", NAME="visor"
279 EOF
280 },
281 {
282 desc => "sustitution of sysfs value (%s{file})",
283 subsys => "block",
284 devpath => "/block/sda",
285 exp_name => "disk-IBM-ESXS-sda" ,
286 rules => <<EOF
287 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="disk-%s{vendor}-%k"
288 KERNEL=="ttyUSB0", NAME="visor"
289 EOF
290 },
291 {
292 desc => "program result substitution",
293 subsys => "block",
294 devpath => "/block/sda/sda3",
295 exp_name => "special-device-3" ,
296 rules => <<EOF
297 BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="-special-*", NAME="%c-1-%n"
298 BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special--*", NAME="%c-2-%n"
299 BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-device-", NAME="%c-3-%n"
300 BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-devic", NAME="%c-4-%n"
301 BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-*", NAME="%c-%n"
302 EOF
303 },
304 {
305 desc => "program result substitution (no argument should be subsystem)",
306 subsys => "block",
307 devpath => "/block/sda/sda3",
308 exp_name => "subsys_block" ,
309 rules => <<EOF
310 BUS=="scsi", PROGRAM=="/bin/echo", RESULT=="block", NAME="subsys_block"
311 EOF
312 },
313 {
314 desc => "program result substitution (newline removal)",
315 subsys => "block",
316 devpath => "/block/sda/sda3",
317 exp_name => "newline_removed" ,
318 rules => <<EOF
319 BUS=="scsi", PROGRAM=="/bin/echo test", RESULT=="test", NAME="newline_removed"
320 EOF
321 },
322 {
323 desc => "program result substitution",
324 subsys => "block",
325 devpath => "/block/sda/sda3",
326 exp_name => "test-0:0:0:0" ,
327 rules => <<EOF
328 BUS=="scsi", PROGRAM=="/bin/echo -n test-%b", RESULT=="test-0:0*", NAME="%c"
329 EOF
330 },
331 {
332 desc => "program with escaped format char (tricky: callout returns format char!)",
333 subsys => "block",
334 devpath => "/block/sda/sda3",
335 exp_name => "escape-3" ,
336 rules => <<EOF
337 BUS=="scsi", PROGRAM=="/bin/echo -n escape-%%n", KERNEL=="sda3", NAME="%c"
338 EOF
339 },
340 {
341 desc => "program with lots of arguments",
342 subsys => "block",
343 devpath => "/block/sda/sda3",
344 exp_name => "foo9" ,
345 rules => <<EOF
346 BUS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda3", NAME="%c{7}"
347 EOF
348 },
349 {
350 desc => "program with subshell",
351 subsys => "block",
352 devpath => "/block/sda/sda3",
353 exp_name => "bar9" ,
354 rules => <<EOF
355 BUS=="scsi", PROGRAM=="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL=="sda3", NAME="%c{7}"
356 EOF
357 },
358 {
359 desc => "program arguments combined with apostrophes",
360 subsys => "block",
361 devpath => "/block/sda/sda3",
362 exp_name => "foo7" ,
363 rules => <<EOF
364 BUS=="scsi", PROGRAM=="/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'", KERNEL=="sda3", NAME="%c{5}"
365 EOF
366 },
367 {
368 desc => "characters before the %c{N} substitution",
369 subsys => "block",
370 devpath => "/block/sda/sda3",
371 exp_name => "my-foo9" ,
372 rules => <<EOF
373 BUS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda3", NAME="my-%c{7}"
374 EOF
375 },
376 {
377 desc => "substitute the second to last argument",
378 subsys => "block",
379 devpath => "/block/sda/sda3",
380 exp_name => "my-foo8" ,
381 rules => <<EOF
382 BUS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda3", NAME="my-%c{6}"
383 EOF
384 },
385 {
386 desc => "test substitution by variable name",
387 subsys => "block",
388 devpath => "/block/sda/sda3",
389 exp_name => "Major:8-minor:3-kernelnumber:3-bus:0:0:0:0" ,
390 rules => <<EOF
391 BUS=="scsi", ID=="0:0:0:0", NAME="Major:\$major-minor:\$minor-kernelnumber:\$number-bus:\$id"
392 EOF
393 },
394 {
395 desc => "test substitution by variable name 2",
396 subsys => "block",
397 devpath => "/block/sda/sda3",
398 exp_name => "Major:8-minor:3-kernelnumber:3-bus:0:0:0:0" ,
399 rules => <<EOF
400 BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="Major:\$major-minor:%m-kernelnumber:\$number-bus:%b"
401 EOF
402 },
403 {
404 desc => "test substitution by variable name 3",
405 subsys => "block",
406 devpath => "/block/sda/sda3",
407 exp_name => "830:0:0:03" ,
408 rules => <<EOF
409 BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="%M%m%b%n"
410 EOF
411 },
412 {
413 desc => "test substitution by variable name 4",
414 subsys => "block",
415 devpath => "/block/sda/sda3",
416 exp_name => "833" ,
417 rules => <<EOF
418 BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="\$major\$minor\$number"
419 EOF
420 },
421 {
422 desc => "test substitution by variable name 5",
423 subsys => "block",
424 devpath => "/block/sda/sda3",
425 exp_name => "8330:0:0:0" ,
426 rules => <<EOF
427 BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="\$major%m%n\$id"
428 EOF
429 },
430 {
431 desc => "invalid program for device with no bus",
432 subsys => "tty",
433 devpath => "/class/tty/console",
434 exp_name => "TTY" ,
435 rules => <<EOF
436 BUS=="scsi", PROGRAM=="/bin/echo -n foo", RESULT=="foo", NAME="foo"
437 KERNEL=="console", NAME="TTY"
438 EOF
439 },
440 {
441 desc => "valid program for device with no bus",
442 subsys => "tty",
443 devpath => "/class/tty/console",
444 exp_name => "foo" ,
445 rules => <<EOF
446 PROGRAM=="/bin/echo -n foo", RESULT=="foo", NAME="foo"
447 KERNEL=="console", NAME="TTY"
448 EOF
449 },
450 {
451 desc => "invalid label for device with no bus",
452 subsys => "tty",
453 devpath => "/class/tty/console",
454 exp_name => "TTY" ,
455 rules => <<EOF
456 BUS=="foo", SYSFS{dev}=="5:1", NAME="foo"
457 KERNEL=="console", NAME="TTY"
458 EOF
459 },
460 {
461 desc => "valid label for device with no bus",
462 subsys => "tty",
463 devpath => "/class/tty/console",
464 exp_name => "foo" ,
465 rules => <<EOF
466 SYSFS{dev}=="5:1", NAME="foo"
467 KERNEL=="console", NAME="TTY"
468 EOF
469 },
470 {
471 desc => "program and bus type match",
472 subsys => "block",
473 devpath => "/block/sda",
474 exp_name => "scsi-0:0:0:0" ,
475 rules => <<EOF
476 BUS=="usb", PROGRAM=="/bin/echo -n usb-%b", NAME="%c"
477 BUS=="scsi", PROGRAM=="/bin/echo -n scsi-%b", NAME="%c"
478 BUS=="foo", PROGRAM=="/bin/echo -n foo-%b", NAME="%c"
479 EOF
480 },
481 {
482 desc => "create all possible partitions",
483 subsys => "block",
484 devpath => "/block/sda",
485 exp_name => "boot_disk15" ,
486 exp_majorminor => "8:15",
487 rules => <<EOF
488 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME{all_partitions}="boot_disk"
489 EOF
490 },
491 {
492 desc => "sysfs parent hierarchy",
493 subsys => "tty",
494 devpath => "/class/tty/ttyUSB0",
495 exp_name => "visor" ,
496 rules => <<EOF
497 SYSFS{idProduct}=="2008", NAME="visor"
498 EOF
499 },
500 {
501 desc => "name test with ! in the name",
502 subsys => "block",
503 devpath => "/block/rd!c0d0",
504 exp_name => "rd/c0d0" ,
505 rules => <<EOF
506 BUS=="scsi", NAME="%k"
507 KERNEL=="ttyUSB0", NAME="visor"
508 EOF
509 },
510 {
511 desc => "name test with ! in the name, but no matching rule",
512 subsys => "block",
513 devpath => "/block/rd!c0d0",
514 exp_name => "rd/c0d0" ,
515 rules => <<EOF
516 KERNEL=="ttyUSB0", NAME="visor"
517 EOF
518 },
519 {
520 desc => "name test with ! in the name for a partition",
521 subsys => "block",
522 devpath => "/block/cciss!c0d0/cciss!c0d0p1",
523 exp_name => "cciss/c0d0p1" ,
524 rules => <<EOF
525 BUS=="scsi", NAME="%k"
526 KERNEL=="ttyUSB0", NAME="visor"
527 EOF
528 },
529 {
530 desc => "ID rule",
531 subsys => "block",
532 devpath => "/block/sda",
533 exp_name => "scsi-0:0:0:0",
534 rules => <<EOF
535 BUS=="usb", ID=="0:0:0:0", NAME="not-scsi"
536 BUS=="scsi", ID=="0:0:0:1", NAME="no-match"
537 BUS=="scsi", ID==":0", NAME="short-id"
538 BUS=="scsi", ID=="/0:0:0:0", NAME="no-match"
539 BUS=="scsi", ID=="0:0:0:0", NAME="scsi-0:0:0:0"
540 EOF
541 },
542 {
543 desc => "ID wildcard all",
544 subsys => "block",
545 devpath => "/block/sda",
546 exp_name => "scsi-0:0:0:0",
547 rules => <<EOF
548 BUS=="scsi", ID=="*:1", NAME="no-match"
549 BUS=="scsi", ID=="*:0:1", NAME="no-match"
550 BUS=="scsi", ID=="*:0:0:1", NAME="no-match"
551 BUS=="scsi", ID=="*", NAME="scsi-0:0:0:0"
552 BUS=="scsi", ID=="0:0:0:0", NAME="bad"
553 EOF
554 },
555 {
556 desc => "ID wildcard partial",
557 subsys => "block",
558 devpath => "/block/sda",
559 exp_name => "scsi-0:0:0:0",
560 rules => <<EOF
561 BUS=="scsi", ID=="*:0", NAME="scsi-0:0:0:0"
562 BUS=="scsi", ID=="0:0:0:0", NAME="bad"
563 EOF
564 },
565 {
566 desc => "ID wildcard partial 2",
567 subsys => "block",
568 devpath => "/block/sda",
569 exp_name => "scsi-0:0:0:0",
570 rules => <<EOF
571 BUS=="scsi", ID=="*:0:0:0", NAME="scsi-0:0:0:0"
572 BUS=="scsi", ID=="0:0:0:0", NAME="bad"
573 EOF
574 },
575 {
576 desc => "ignore SYSFS attribute whitespace",
577 subsys => "block",
578 devpath => "/block/sda",
579 exp_name => "ignored",
580 rules => <<EOF
581 BUS=="scsi", SYSFS{whitespace_test}=="WHITE SPACE", NAME="ignored"
582 EOF
583 },
584 {
585 desc => "do not ignore SYSFS attribute whitespace",
586 subsys => "block",
587 devpath => "/block/sda",
588 exp_name => "matched-with-space",
589 rules => <<EOF
590 BUS=="scsi", SYSFS{whitespace_test}=="WHITE SPACE ", NAME="wrong-to-ignore"
591 BUS=="scsi", SYSFS{whitespace_test}=="WHITE SPACE ", NAME="matched-with-space"
592 EOF
593 },
594 {
595 desc => "permissions USER=bad GROUP=name",
596 subsys => "tty",
597 devpath => "/class/tty/tty33",
598 exp_name => "tty33",
599 exp_perms => "0:0:0660",
600 rules => <<EOF
601 KERNEL=="tty33", NAME="tty33", OWNER="bad", GROUP="name"
602 EOF
603 },
604 {
605 desc => "permissions OWNER=5000",
606 subsys => "block",
607 devpath => "/block/sda",
608 exp_name => "node",
609 exp_perms => "5000::0660",
610 rules => <<EOF
611 BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="5000"
612 EOF
613 },
614 {
615 desc => "permissions GROUP=100",
616 subsys => "block",
617 devpath => "/block/sda",
618 exp_name => "node",
619 exp_perms => ":100:0660",
620 rules => <<EOF
621 BUS=="scsi", KERNEL=="sda", NAME="node", GROUP="100"
622 EOF
623 },
624 {
625 desc => "textual user id",
626 subsys => "block",
627 devpath => "/block/sda",
628 exp_name => "node",
629 exp_perms => "nobody::0660",
630 rules => <<EOF
631 BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="nobody"
632 EOF
633 },
634 {
635 desc => "textual group id",
636 subsys => "block",
637 devpath => "/block/sda",
638 exp_name => "node",
639 exp_perms => ":daemon:0660",
640 rules => <<EOF
641 BUS=="scsi", KERNEL=="sda", NAME="node", GROUP="daemon"
642 EOF
643 },
644 {
645 desc => "textual user/group id",
646 subsys => "block",
647 devpath => "/block/sda",
648 exp_name => "node",
649 exp_perms => "root:mail:0660",
650 rules => <<EOF
651 BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="root", GROUP="mail"
652 EOF
653 },
654 {
655 desc => "permissions MODE=0777",
656 subsys => "block",
657 devpath => "/block/sda",
658 exp_name => "node",
659 exp_perms => "::0777",
660 rules => <<EOF
661 BUS=="scsi", KERNEL=="sda", NAME="node", MODE="0777"
662 EOF
663 },
664 {
665 desc => "permissions OWNER=5000 GROUP=100 MODE=0777",
666 subsys => "block",
667 devpath => "/block/sda",
668 exp_name => "node",
669 exp_perms => "5000:100:0777",
670 rules => <<EOF
671 BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="5000", GROUP="100", MODE="0777"
672 EOF
673 },
674 {
675 desc => "permissions OWNER to 5000",
676 subsys => "tty",
677 devpath => "/class/tty/ttyUSB0",
678 exp_name => "ttyUSB0",
679 exp_perms => "5000::",
680 rules => <<EOF
681 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", OWNER="5000"
682 EOF
683 },
684 {
685 desc => "permissions GROUP to 100",
686 subsys => "tty",
687 devpath => "/class/tty/ttyUSB0",
688 exp_name => "ttyUSB0",
689 exp_perms => ":100:0660",
690 rules => <<EOF
691 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", GROUP="100"
692 EOF
693 },
694 {
695 desc => "permissions MODE to 0060",
696 subsys => "tty",
697 devpath => "/class/tty/ttyUSB0",
698 exp_name => "ttyUSB0",
699 exp_perms => "::0060",
700 rules => <<EOF
701 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", MODE="0060"
702 EOF
703 },
704 {
705 desc => "permissions OWNER, GROUP, MODE",
706 subsys => "tty",
707 devpath => "/class/tty/ttyUSB0",
708 exp_name => "ttyUSB0",
709 exp_perms => "5000:100:0777",
710 rules => <<EOF
711 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", OWNER="5000", GROUP="100", MODE="0777"
712 EOF
713 },
714 {
715 desc => "permissions only rule",
716 subsys => "tty",
717 devpath => "/class/tty/ttyUSB0",
718 exp_name => "ttyUSB0",
719 exp_perms => "5000:100:0777",
720 rules => <<EOF
721 KERNEL=="ttyUSB[0-9]*", OWNER="5000", GROUP="100", MODE="0777"
722 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
723 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n"
724 EOF
725 },
726 {
727 desc => "multiple permissions only rule",
728 subsys => "tty",
729 devpath => "/class/tty/ttyUSB0",
730 exp_name => "ttyUSB0",
731 exp_perms => "3000:4000:0777",
732 rules => <<EOF
733 SUBSYSTEM=="tty", OWNER="3000"
734 SUBSYSTEM=="tty", GROUP="4000"
735 SUBSYSTEM=="tty", MODE="0777"
736 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
737 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n"
738 EOF
739 },
740 {
741 desc => "permissions only rule with override at NAME rule",
742 subsys => "tty",
743 devpath => "/class/tty/ttyUSB0",
744 exp_name => "ttyUSB0",
745 exp_perms => "3000:8000:0777",
746 rules => <<EOF
747 SUBSYSTEM=="tty", OWNER="3000"
748 SUBSYSTEM=="tty", GROUP="4000"
749 SUBSYSTEM=="tty", MODE="0777"
750 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
751 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", GROUP="8000"
752 EOF
753 },
754 {
755 desc => "major/minor number test",
756 subsys => "block",
757 devpath => "/block/sda",
758 exp_name => "node",
759 exp_majorminor => "8:0",
760 rules => <<EOF
761 BUS=="scsi", KERNEL=="sda", NAME="node"
762 EOF
763 },
764 {
765 desc => "big minor number test",
766 subsys => "i2c-dev",
767 devpath => "/class/i2c-dev/i2c-300",
768 exp_name => "node",
769 exp_majorminor => "89:300",
770 rules => <<EOF
771 KERNEL=="i2c-300", NAME="node"
772 EOF
773 },
774 {
775 desc => "big major number test",
776 subsys => "i2c-dev",
777 devpath => "/class/i2c-dev/i2c-fake1",
778 exp_name => "node",
779 exp_majorminor => "4095:1",
780 rules => <<EOF
781 KERNEL=="i2c-fake1", NAME="node"
782 EOF
783 },
784 {
785 desc => "big major and big minor number test",
786 subsys => "i2c-dev",
787 devpath => "/class/i2c-dev/i2c-fake2",
788 exp_name => "node",
789 exp_majorminor => "4094:89999",
790 rules => <<EOF
791 KERNEL=="i2c-fake2", NAME="node"
792 EOF
793 },
794 {
795 desc => "multiple symlinks with format char",
796 subsys => "tty",
797 devpath => "/class/tty/ttyUSB0",
798 exp_name => "symlink2-ttyUSB0",
799 exp_target => "ttyUSB0",
800 rules => <<EOF
801 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
802 EOF
803 },
804 {
805 desc => "multiple symlinks with a lot of s p a c e s",
806 subsys => "tty",
807 devpath => "/class/tty/ttyUSB0",
808 exp_name => "one",
809 not_exp_name => " ",
810 exp_target => "ttyUSB0",
811 rules => <<EOF
812 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK=" one two "
813 EOF
814 },
815 {
816 desc => "symlink creation (same directory)",
817 subsys => "tty",
818 devpath => "/class/tty/ttyUSB0",
819 exp_name => "visor0",
820 exp_target => "ttyUSB0",
821 rules => <<EOF
822 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
823 EOF
824 },
825 {
826 desc => "symlink creation (relative link forward)",
827 subsys => "block",
828 devpath => "/block/sda/sda2",
829 exp_name => "1/2/symlink" ,
830 exp_target => "a/b/node",
831 rules => <<EOF
832 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
833 EOF
834 },
835 {
836 desc => "symlink creation (relative link back and forward)",
837 subsys => "block",
838 devpath => "/block/sda/sda2",
839 exp_name => "1/2/c/d/symlink" ,
840 exp_target => "../../a/b/node",
841 rules => <<EOF
842 BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
843 EOF
844 },
845 {
846 desc => "multiple symlinks",
847 subsys => "tty",
848 devpath => "/class/tty/ttyUSB0",
849 exp_name => "second-0" ,
850 exp_target => "visor" ,
851 rules => <<EOF
852 KERNEL=="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
853 EOF
854 },
855 {
856 desc => "symlink only rule",
857 subsys => "block",
858 devpath => "/block/sda",
859 exp_name => "symlink-only2",
860 exp_target => "link",
861 rules => <<EOF
862 BUS=="scsi", KERNEL=="sda", SYMLINK+="symlink-only1"
863 BUS=="scsi", KERNEL=="sda", SYMLINK+="symlink-only2"
864 BUS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="symlink0"
865 EOF
866 },
867 {
868 desc => "symlink name '.'",
869 subsys => "block",
870 devpath => "/block/sda",
871 exp_name => ".",
872 exp_target => "link",
873 exp_add_error => "yes",
874 exp_rem_error => "yes",
875 rules => <<EOF
876 BUS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="."
877 EOF
878 },
879 {
880 desc => "symlink node to itself",
881 subsys => "tty",
882 devpath => "/class/tty/tty0",
883 exp_name => "link",
884 exp_target => "link",
885 exp_rem_error => "yes",
886 option => "clean",
887 rules => <<EOF
888 KERNEL=="tty0", NAME="link", SYMLINK+="link"
889 EOF
890 },
891 {
892 desc => "symlink %n substitution",
893 subsys => "tty",
894 devpath => "/class/tty/ttyUSB0",
895 exp_name => "symlink0",
896 exp_target => "ttyUSB0",
897 rules => <<EOF
898 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="symlink%n"
899 EOF
900 },
901 {
902 desc => "symlink %k substitution",
903 subsys => "tty",
904 devpath => "/class/tty/ttyUSB0",
905 exp_name => "symlink-ttyUSB0",
906 exp_target => "ttyUSB0",
907 rules => <<EOF
908 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="symlink-%k"
909 EOF
910 },
911 {
912 desc => "symlink %M:%m substitution",
913 subsys => "tty",
914 devpath => "/class/tty/ttyUSB0",
915 exp_name => "major-188:0",
916 exp_target => "ttyUSB0",
917 rules => <<EOF
918 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="major-%M:%m"
919 EOF
920 },
921 {
922 desc => "symlink %b substitution",
923 subsys => "block",
924 devpath => "/block/sda",
925 exp_name => "symlink-0:0:0:0",
926 exp_target => "node",
927 rules => <<EOF
928 BUS=="scsi", KERNEL=="sda", NAME="node", SYMLINK+="symlink-%b"
929 EOF
930 },
931 {
932 desc => "symlink %c substitution",
933 subsys => "tty",
934 devpath => "/class/tty/ttyUSB0",
935 exp_name => "test",
936 exp_target => "ttyUSB0",
937 rules => <<EOF
938 KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo test" NAME="ttyUSB%n", SYMLINK+="%c"
939 EOF
940 },
941 {
942 desc => "symlink %c{N} substitution",
943 subsys => "tty",
944 devpath => "/class/tty/ttyUSB0",
945 exp_name => "test",
946 exp_target => "ttyUSB0",
947 rules => <<EOF
948 KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo symlink test this" NAME="ttyUSB%n", SYMLINK+="%c{2}"
949 EOF
950 },
951 {
952 desc => "symlink %c{N+} substitution",
953 subsys => "tty",
954 devpath => "/class/tty/ttyUSB0",
955 exp_name => "this",
956 exp_target => "ttyUSB0",
957 rules => <<EOF
958 KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo symlink test this" NAME="ttyUSB%n", SYMLINK+="%c{2+}"
959 EOF
960 },
961 {
962 desc => "symlink only rule with %c{N+}",
963 subsys => "block",
964 devpath => "/block/sda",
965 exp_name => "test",
966 exp_target => "link",
967 rules => <<EOF
968 BUS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
969 BUS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="symlink0"
970 EOF
971 },
972 {
973 desc => "symlink %s{filename} substitution",
974 subsys => "tty",
975 devpath => "/class/tty/ttyUSB0",
976 exp_name => "188:0",
977 exp_target => "ttyUSB0",
978 rules => <<EOF
979 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%s{dev}"
980 EOF
981 },
982 {
983 desc => "symlink %Ns{filename} substitution",
984 subsys => "tty",
985 devpath => "/class/tty/ttyUSB0",
986 exp_name => "188",
987 exp_target => "ttyUSB0",
988 rules => <<EOF
989 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%3s{dev}"
990 EOF
991 },
992 {
993 desc => "symlink with '%' in name",
994 subsys => "tty",
995 devpath => "/class/tty/ttyUSB0",
996 exp_name => "percent%sign",
997 exp_target => "ttyUSB0",
998 rules => <<EOF
999 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="percent%%sign"
1000 EOF
1001 },
1002 {
1003 desc => "symlink with '%' in name",
1004 subsys => "tty",
1005 devpath => "/class/tty/ttyUSB0",
1006 exp_name => "%ttyUSB0_name",
1007 exp_target => "ttyUSB0",
1008 rules => <<EOF
1009 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%%%k_name"
1010 EOF
1011 },
1012 {
1013 desc => "program result substitution (numbered part of)",
1014 subsys => "block",
1015 devpath => "/block/sda/sda3",
1016 exp_name => "link1",
1017 exp_target => "node",
1018 rules => <<EOF
1019 BUS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", NAME="%c{1}", SYMLINK+="%c{2} %c{3}"
1020 EOF
1021 },
1022 {
1023 desc => "program result substitution (numbered part of+)",
1024 subsys => "block",
1025 devpath => "/block/sda/sda3",
1026 exp_name => "link4",
1027 exp_target => "node",
1028 rules => <<EOF
1029 BUS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", NAME="%c{1}", SYMLINK+="%c{2+}"
1030 EOF
1031 },
1032 {
1033 desc => "enumeration char test (single test)",
1034 subsys => "block",
1035 devpath => "/block/sda",
1036 exp_name => "cdrom",
1037 rules => <<EOF
1038 KERNEL=="sda", NAME="cdrom%e"
1039 EOF
1040 },
1041 {
1042 desc => "enumeration char test sequence 1/5 (keep)",
1043 subsys => "block",
1044 devpath => "/block/sda",
1045 exp_name => "cdrom",
1046 option => "keep",
1047 rules => <<EOF
1048 KERNEL=="sda", NAME="cdrom%e"
1049 EOF
1050 },
1051 {
1052 desc => "enumeration char test sequence 2/5 (keep)",
1053 subsys => "block",
1054 devpath => "/block/sda/sda1",
1055 exp_name => "enum",
1056 option => "keep",
1057 rules => <<EOF
1058 KERNEL=="sda1", NAME="enum%e"
1059 EOF
1060 },
1061 {
1062 desc => "enumeration char test sequence 3/5 (keep)",
1063 subsys => "block",
1064 devpath => "/block/sda/sda2",
1065 exp_name => "cdrom1",
1066 option => "keep",
1067 rules => <<EOF
1068 KERNEL=="sda2", NAME="cdrom%e"
1069 EOF
1070 },
1071 {
1072 desc => "enumeration char test sequence 4/5 (keep)",
1073 subsys => "block",
1074 devpath => "/block/sda/sda3",
1075 exp_name => "enum1",
1076 option => "keep",
1077 rules => <<EOF
1078 KERNEL=="sda3", NAME="enum%e"
1079 EOF
1080 },
1081 {
1082 desc => "enumeration char test sequence 5/5 (clean)",
1083 subsys => "block",
1084 devpath => "/block/sda/sda4",
1085 exp_name => "cdrom2",
1086 option => "clean",
1087 rules => <<EOF
1088 KERNEL=="sda4", NAME="cdrom%e"
1089 EOF
1090 },
1091 {
1092 desc => "enumeration char test after cleanup (single test)",
1093 subsys => "block",
1094 devpath => "/block/sda",
1095 exp_name => "cdrom",
1096 rules => <<EOF
1097 KERNEL=="sda", NAME="cdrom%e"
1098 EOF
1099 },
1100 {
1101 desc => "ignore rule test",
1102 subsys => "block",
1103 devpath => "/block/sda",
1104 exp_name => "nothing",
1105 not_exp_name => "node",
1106 exp_add_error => "yes",
1107 rules => <<EOF
1108 BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore"
1109 EOF
1110 },
1111 {
1112 desc => "all_partitions, option-only rule",
1113 subsys => "block",
1114 devpath => "/block/sda",
1115 exp_name => "node6",
1116 rules => <<EOF
1117 SUBSYSTEM=="block", OPTIONS="all_partitions"
1118 BUS=="scsi", KERNEL=="sda", NAME="node"
1119 EOF
1120 },
1121 {
1122 desc => "all_partitions, option-only rule (fail on partition)",
1123 subsys => "block",
1124 devpath => "/block/sda/sda1",
1125 exp_name => "node6",
1126 exp_add_error => "yes",
1127 rules => <<EOF
1128 SUBSYSTEM=="block", OPTIONS="all_partitions"
1129 BUS=="scsi", KERNEL=="sda", NAME="node"
1130 EOF
1131 },
1132 {
1133 desc => "ignore remove event test",
1134 subsys => "block",
1135 devpath => "/block/sda",
1136 exp_name => "node",
1137 exp_rem_error => "yes",
1138 rules => <<EOF
1139 BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove"
1140 EOF
1141 },
1142 {
1143 desc => "ignore remove event test (with all partitions)",
1144 subsys => "block",
1145 devpath => "/block/sda",
1146 exp_name => "node14",
1147 exp_rem_error => "yes",
1148 option => "clean",
1149 rules => <<EOF
1150 BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove, all_partitions"
1151 EOF
1152 },
1153 {
1154 desc => "SUBSYSTEM match test",
1155 subsys => "block",
1156 devpath => "/block/sda",
1157 exp_name => "node",
1158 rules => <<EOF
1159 BUS=="scsi", KERNEL=="sda", NAME="should_not_match", SUBSYSTEM=="vc"
1160 BUS=="scsi", KERNEL=="sda", NAME="node", SUBSYSTEM=="block"
1161 BUS=="scsi", KERNEL=="sda", NAME="should_not_match2", SUBSYSTEM=="vc"
1162 EOF
1163 },
1164 {
1165 desc => "DRIVER match test",
1166 subsys => "block",
1167 devpath => "/block/sda",
1168 exp_name => "node",
1169 rules => <<EOF
1170 BUS=="scsi", KERNEL=="sda", NAME="should_not_match", DRIVER=="sd-wrong"
1171 BUS=="scsi", KERNEL=="sda", NAME="node", DRIVER=="sd"
1172 EOF
1173 },
1174 {
1175 desc => "temporary node creation test",
1176 subsys => "block",
1177 devpath => "/block/sda",
1178 exp_name => "node",
1179 rules => <<EOF
1180 BUS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" NAME="node"
1181 EOF
1182 },
1183 {
1184 desc => "devpath substitution test",
1185 subsys => "block",
1186 devpath => "/block/sda",
1187 exp_name => "sda",
1188 rules => <<EOF
1189 BUS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo %p", RESULT=="/block/sda" NAME="%k"
1190 EOF
1191 },
1192 {
1193 desc => "parent node name substitution test sequence 1/2 (keep)",
1194 subsys => "block",
1195 devpath => "/block/sda",
1196 exp_name => "main_device",
1197 option => "keep",
1198 rules => <<EOF
1199 BUS=="scsi", KERNEL=="sda", NAME="main_device"
1200 EOF
1201 },
1202 {
1203 desc => "parent node name substitution test sequence 2/2 (clean)",
1204 subsys => "block",
1205 devpath => "/block/sda/sda1",
1206 exp_name => "main_device-part-1",
1207 option => "clean",
1208 rules => <<EOF
1209 BUS=="scsi", KERNEL=="sda1", NAME="%P-part-1"
1210 EOF
1211 },
1212 {
1213 desc => "udev_root substitution",
1214 subsys => "block",
1215 devpath => "/block/sda/sda1",
1216 exp_name => "start-udev-root-end",
1217 rules => <<EOF
1218 BUS=="scsi", KERNEL=="sda1", NAME="start-%r-end"
1219 EOF
1220 },
1221 {
1222 desc => "last_rule option",
1223 subsys => "block",
1224 devpath => "/block/sda/sda1",
1225 exp_name => "last",
1226 rules => <<EOF
1227 BUS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
1228 BUS=="scsi", KERNEL=="sda1", NAME="very-last"
1229 EOF
1230 },
1231 {
1232 desc => "negation KERNEL!=",
1233 subsys => "block",
1234 devpath => "/block/sda/sda1",
1235 exp_name => "match",
1236 rules => <<EOF
1237 BUS=="scsi", KERNEL!="sda1", NAME="matches-but-is-negated"
1238 BUS=="scsi", KERNEL!="xsda1", NAME="match"
1239 BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1240 EOF
1241 },
1242 {
1243 desc => "negation SUBSYSTEM!=",
1244 subsys => "block",
1245 devpath => "/block/sda/sda1",
1246 exp_name => "not-anything",
1247 rules => <<EOF
1248 BUS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", NAME="matches-but-is-negated"
1249 BUS=="scsi", SUBSYSTEM!="anything", NAME="not-anything"
1250 BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1251 EOF
1252 },
1253 {
1254 desc => "negation PROGRAM!= exit code",
1255 subsys => "block",
1256 devpath => "/block/sda/sda1",
1257 exp_name => "nonzero-program",
1258 rules => <<EOF
1259 KERNEL=="sda1", PROGRAM!="/bin/false", NAME="nonzero-program"
1260 BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1261 EOF
1262 },
1263 {
1264 desc => "test for whitespace between the operator",
1265 subsys => "block",
1266 devpath => "/block/sda/sda1",
1267 exp_name => "true",
1268 rules => <<EOF
1269 KERNEL == "sda1" , NAME = "true"
1270 BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1271 EOF
1272 },
1273 {
1274 desc => "ENV{} test",
1275 subsys => "block",
1276 devpath => "/block/sda/sda1",
1277 exp_name => "true",
1278 rules => <<EOF
1279 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", NAME="wrong"
1280 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", NAME="true"
1281 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", NAME="bad"
1282 EOF
1283 },
1284 {
1285 desc => "ENV{} test",
1286 subsys => "block",
1287 devpath => "/block/sda/sda1",
1288 exp_name => "true",
1289 rules => <<EOF
1290 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", NAME="wrong"
1291 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="/block/sda/sdax1", NAME="no"
1292 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="/block/sda/sda1", NAME="true"
1293 BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", NAME="bad"
1294 EOF
1295 },
1296 {
1297 desc => "untrusted string sanitize",
1298 subsys => "block",
1299 devpath => "/block/sda/sda1",
1300 exp_name => "sane",
1301 rules => <<EOF
1302 BUS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/sbin/badprogram)", RESULT="name_ _/sbin/badprogram_", NAME="sane"
1303 EOF
1304 },
1305 {
1306 desc => "read sysfs value from device down in the chain",
1307 subsys => "block",
1308 devpath => "/class/tty/ttyUSB0",
1309 exp_name => "serial-0000:00:09.0",
1310 rules => <<EOF
1311 KERNEL=="ttyUSB*", NAME="serial-%s{serial}"
1312 EOF
1313 },
1314 {
1315 desc => "match against empty key string",
1316 subsys => "block",
1317 devpath => "/block/sda",
1318 exp_name => "ok",
1319 rules => <<EOF
1320 KERNEL=="sda", SYSFS{nothing}!="", NAME="not-1-ok"
1321 KERNEL=="sda", SYSFS{nothing}=="", NAME="not-2-ok"
1322 KERNEL=="sda", SYSFS{vendor}!="", NAME="ok"
1323 KERNEL=="sda", SYSFS{vendor}=="", NAME="not-3-ok"
1324 EOF
1325 },
1326 {
1327 desc => "check ACTION value",
1328 subsys => "block",
1329 devpath => "/block/sda",
1330 exp_name => "ok",
1331 rules => <<EOF
1332 ACTION=="unknown", KERNEL=="sda", NAME="unknown-not-ok"
1333 ACTION=="add", KERNEL=="sda", NAME="ok"
1334 EOF
1335 },
1336 {
1337 desc => "apply NAME only once",
1338 subsys => "block",
1339 devpath => "/block/sda",
1340 exp_name => "link",
1341 exp_target => "ok",
1342 rules => <<EOF
1343 KERNEL=="sda", NAME="ok"
1344 KERNEL=="sda", NAME="not-ok"
1345 KERNEL=="sda", SYMLINK+="link"
1346 EOF
1347 },
1348 {
1349 desc => "test RUN key",
1350 subsys => "block",
1351 devpath => "/block/sda",
1352 exp_name => "testsymlink",
1353 exp_target => "ok",
1354 exp_rem_error => "yes",
1355 option => "clean",
1356 rules => <<EOF
1357 KERNEL=="sda", NAME="ok", RUN+="/bin/ln -s ok %r/testsymlink"
1358 KERNEL=="sda", NAME="not-ok"
1359 EOF
1360 },
1361 {
1362 desc => "test RUN key and DEVNAME",
1363 subsys => "block",
1364 devpath => "/block/sda",
1365 exp_name => "testsymlink",
1366 exp_target => "ok",
1367 exp_rem_error => "yes",
1368 option => "clean",
1369 rules => <<EOF
1370 KERNEL=="sda", NAME="ok", RUN+="/bin/sh -c 'ln -s `basename \$\$DEVNAME` %r/testsymlink'"
1371 KERNEL=="sda", NAME="not-ok"
1372 EOF
1373 },
1374 {
1375 desc => "test RUN key remove",
1376 subsys => "block",
1377 devpath => "/block/sda",
1378 exp_name => "testsymlink2",
1379 exp_target => "ok2",
1380 rules => <<EOF
1381 KERNEL=="sda", NAME="ok2", RUN+="/bin/ln -s ok2 %r/testsymlink2"
1382 KERNEL=="sda", ACTION=="remove", RUN+="/bin/rm -f %r/testsymlink2"
1383 KERNEL=="sda", NAME="not-ok2"
1384 EOF
1385 },
1386 {
1387 desc => "final assignment",
1388 subsys => "block",
1389 devpath => "/block/sda",
1390 exp_name => "ok",
1391 exp_perms => "root:nobody:0640",
1392 rules => <<EOF
1393 KERNEL=="sda", GROUP:="nobody"
1394 KERNEL=="sda", GROUP="not-ok", MODE="0640", NAME="ok"
1395 EOF
1396 },
1397 {
1398 desc => "final assignment",
1399 subsys => "block",
1400 devpath => "/block/sda",
1401 exp_name => "ok",
1402 exp_perms => "root:nobody:0640",
1403 rules => <<EOF
1404 KERNEL=="sda", GROUP:="nobody"
1405 SUBSYSTEM=="block", MODE:="640"
1406 KERNEL=="sda", GROUP="not-ok", MODE="0666", NAME="ok"
1407 EOF
1408 },
1409 {
1410 desc => "env substitution",
1411 subsys => "block",
1412 devpath => "/block/sda",
1413 exp_name => "node-add-me",
1414 rules => <<EOF
1415 KERNEL=="sda", MODE="0666", NAME="node-\$env{ACTION}-me"
1416 EOF
1417 },
1418 {
1419 desc => "reset list to current value",
1420 subsys => "tty",
1421 devpath => "/class/tty/ttyUSB0",
1422 exp_name => "three",
1423 not_exp_name => "two",
1424 exp_target => "node",
1425 rules => <<EOF
1426 KERNEL=="ttyUSB[0-9]*", SYMLINK+="one"
1427 KERNEL=="ttyUSB[0-9]*", SYMLINK+="two"
1428 KERNEL=="ttyUSB[0-9]*", SYMLINK="three"
1429 KERNEL=="ttyUSB[0-9]*", NAME="node"
1430 EOF
1431 },
1432 {
1433 desc => "test empty NAME",
1434 subsys => "tty",
1435 devpath => "/class/tty/ttyUSB0",
1436 exp_name => "node",
1437 not_exp_name => "wrong",
1438 exp_add_error => "yes",
1439 rules => <<EOF
1440 KERNEL=="ttyUSB[0-9]*", NAME=""
1441 KERNEL=="ttyUSB[0-9]*", NAME="wrong"
1442 EOF
1443 },
1444 {
1445 desc => "test empty NAME 2",
1446 subsys => "tty",
1447 devpath => "/class/tty/ttyUSB0",
1448 exp_name => "right",
1449 rules => <<EOF
1450 KERNEL=="ttyUSB[0-9]*", NAME="right"
1451 KERNEL=="ttyUSB[0-9]*", NAME=""
1452 KERNEL=="ttyUSB[0-9]*", NAME="wrong"
1453 EOF
1454 },
1455 {
1456 desc => "test multi matches",
1457 subsys => "tty",
1458 devpath => "/class/tty/ttyUSB0",
1459 exp_name => "right",
1460 rules => <<EOF
1461 KERNEL=="ttyUSB*|nothing", NAME="right"
1462 KERNEL=="ttyUSB*", NAME="wrong"
1463 EOF
1464 },
1465 {
1466 desc => "test multi matches 2",
1467 subsys => "tty",
1468 devpath => "/class/tty/ttyUSB0",
1469 exp_name => "right",
1470 rules => <<EOF
1471 KERNEL=="dontknow*|*nothing", NAME="nomatch"
1472 KERNEL=="dontknow*|ttyUSB*|nothing*", NAME="right"
1473 KERNEL=="ttyUSB*", NAME="wrong"
1474 EOF
1475 },
1476 );
1477
1478 # set env
1479 $ENV{ENV_KEY_TEST} = "test";
1480 $ENV{SYSFS_PATH} = $sysfs;
1481 $ENV{UDEV_CONFIG_FILE} = $udev_conf;
1482 $ENV{UDEV_NO_DEVD} = "yes";
1483 $ENV{UDEV_NO_HOTPLUGD} = "yes";
1484
1485
1486 sub udev {
1487 my ($action, $subsys, $devpath, $rules) = @_;
1488
1489 $ENV{DEVPATH} = $devpath;
1490
1491 # create temporary rules
1492 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1493 print CONF $$rules;
1494 close CONF;
1495
1496 $ENV{ACTION} = $action;
1497 system("$udev_bin $subsys");
1498 }
1499
1500 my $error = 0;
1501
1502 sub permissions_test {
1503 my($rules, $uid, $gid, $mode) = @_;
1504
1505 my $wrong = 0;
1506 my $userid;
1507 my $groupid;
1508
1509 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1510 if ($1 ne "") {
1511 if (defined(getpwnam($1))) {
1512 $userid = int(getpwnam($1));
1513 } else {
1514 $userid = $1;
1515 }
1516 if ($uid != $userid) { $wrong = 1; }
1517 }
1518 if ($2 ne "") {
1519 if (defined(getgrnam($2))) {
1520 $groupid = int(getgrnam($2));
1521 } else {
1522 $groupid = $2;
1523 }
1524 if ($gid != $groupid) { $wrong = 1; }
1525 }
1526 if ($3 ne "") {
1527 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1528 }
1529 if ($wrong == 0) {
1530 print "permissions: ok\n";
1531 } else {
1532 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1533 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1534 print "permissions: error\n";
1535 $error++;
1536 }
1537 }
1538
1539 sub major_minor_test {
1540 my($rules, $rdev) = @_;
1541
1542 my $major = ($rdev >> 8) & 0xfff;
1543 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1544 my $wrong = 0;
1545
1546 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1547 if ($1 ne "") {
1548 if ($major != $1) { $wrong = 1; };
1549 }
1550 if ($2 ne "") {
1551 if ($minor != $2) { $wrong = 1; };
1552 }
1553 if ($wrong == 0) {
1554 print "major:minor: ok\n";
1555 } else {
1556 printf " expected major:minor is: %i:%i\n", $1, $2;
1557 printf " created major:minor is : %i:%i\n", $major, $minor;
1558 print "major:minor: error\n";
1559 $error++;
1560 }
1561 }
1562
1563 sub symlink_test {
1564 my ($rules) = @_;
1565
1566 my $output = `ls -l $PWD/$udev_root$rules->{exp_name}`;
1567
1568 if ($output =~ m/(.*)-> (.*)/) {
1569 if ($2 eq $rules->{exp_target}) {
1570 print "symlink: ok\n";
1571 } else {
1572 print " expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1573 print " created symlink from: \'$rules->{exp_name}\' to \'$2\'\n";
1574 print "symlink: error";
1575 if ($rules->{exp_add_error}) {
1576 print " as expected\n";
1577 } else {
1578 print "\n";
1579 $error++;
1580 }
1581 }
1582 } else {
1583 print " expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1584 print "symlink: not created";
1585 if ($rules->{exp_add_error}) {
1586 print " as expected\n";
1587 } else {
1588 print "\n";
1589 $error++;
1590 }
1591 }
1592 }
1593
1594 sub run_test {
1595 my ($rules, $number) = @_;
1596
1597 print "TEST $number: $rules->{desc}\n";
1598
1599 if ($rules->{exp_target}) {
1600 print "device \'$rules->{devpath}\' expecting symlink '$rules->{exp_name}' to node \'$rules->{exp_target}\'\n";
1601 } else {
1602 print "device \'$rules->{devpath}\' expecting node \'$rules->{exp_name}\'\n";
1603 }
1604
1605
1606 udev("add", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1607 if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1608 (-l "$PWD/$udev_root$rules->{exp_name}")) {
1609
1610 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1611 $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$rules->{exp_name}");
1612
1613 if (defined($rules->{not_exp_name})) {
1614 if ((-e "$PWD/$udev_root$rules->{not_exp_name}") ||
1615 (-l "$PWD/$udev_root$rules->{not_exp_name}")) {
1616 print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1617 $error++
1618 }
1619 }
1620 if (defined($rules->{exp_perms})) {
1621 permissions_test($rules, $uid, $gid, $mode);
1622 }
1623 if (defined($rules->{exp_majorminor})) {
1624 major_minor_test($rules, $rdev);
1625 }
1626 if (defined($rules->{exp_target})) {
1627 symlink_test($rules);
1628 }
1629 print "add: ok\n";
1630 } else {
1631 print "add: error";
1632 if ($rules->{exp_add_error}) {
1633 print " as expected\n";
1634 } else {
1635 print "\n";
1636 system("tree $udev_root");
1637 print "\n";
1638 $error++;
1639 }
1640 }
1641
1642 if (defined($rules->{option}) && $rules->{option} eq "keep") {
1643 print "\n\n";
1644 return;
1645 }
1646
1647 udev("remove", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1648 if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1649 (-l "$PWD/$udev_root$rules->{exp_name}")) {
1650 print "remove: error";
1651 if ($rules->{exp_rem_error}) {
1652 print " as expected\n";
1653 } else {
1654 print "\n";
1655 system("tree $udev_root");
1656 print "\n";
1657 $error++;
1658 }
1659 } else {
1660 print "remove: ok\n";
1661 }
1662
1663 print "\n";
1664
1665 if (defined($rules->{option}) && $rules->{option} eq "clean") {
1666 system("rm -rf $udev_db");
1667 system("rm -rf $udev_root");
1668 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1669 }
1670
1671 }
1672
1673 # only run if we have root permissions
1674 # due to mknod restrictions
1675 if (!($<==0)) {
1676 print "Must have root permissions to run properly.\n";
1677 exit;
1678 }
1679
1680 # prepare
1681 system("rm -rf $udev_root");
1682 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1683
1684 # create config file
1685 open CONF, ">$udev_conf" || die "unable to create config file: $udev_conf";
1686 print CONF "udev_root=\"$udev_root\"\n";
1687 print CONF "udev_db=\"$udev_db\"\n";
1688 print CONF "udev_rules=\"$udev_rules\"\n";
1689 close CONF;
1690
1691 my $test_num = 1;
1692
1693 if ($ARGV[0]) {
1694 # only run one test
1695 $test_num = $ARGV[0];
1696
1697 if (defined($tests[$test_num-1]->{desc})) {
1698 print "udev-test will run test number $test_num only:\n\n";
1699 run_test($tests[$test_num-1], $test_num);
1700 } else {
1701 print "test does not exist.\n";
1702 }
1703 } else {
1704 # test all
1705 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1706
1707 foreach my $rules (@tests) {
1708 run_test($rules, $test_num);
1709 $test_num++;
1710 }
1711 }
1712
1713 print "$error errors occured\n\n";
1714
1715 # cleanup
1716 system("rm -rf $udev_db");
1717 system("rm -rf $udev_root");
1718 unlink($udev_rules);
1719 unlink($udev_conf);
1720