]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - test/udev-test.pl
add uft8 validation for safe volume label exporting
[thirdparty/systemd.git] / test / udev-test.pl
... / ...
CommitLineData
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
20use warnings;
21use strict;
22
23my $PWD = $ENV{PWD};
24my $sysfs = "sys/";
25my $udev_bin = "../udev";
26my $udev_root = "udev-root/"; # !!! directory will be removed !!!
27my $udev_db = ".udevdb";
28my $udev_conf = "udev-test.conf";
29my $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
35my @tests = (
36 {
37 desc => "label test of scsi disc",
38 subsys => "block",
39 devpath => "/block/sda",
40 exp_name => "boot_disk" ,
41 rules => <<EOF
42BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="boot_disk%n"
43KERNEL=="ttyUSB0", NAME="visor"
44EOF
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
52BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="boot_disk%n"
53EOF
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
61BUS=="scsi", SYSFS{vendor}=="?IBM-ESXS", NAME="boot_disk%n-1"
62BUS=="scsi", SYSFS{vendor}=="IBM-ESXS?", NAME="boot_disk%n-2"
63BUS=="scsi", SYSFS{vendor}=="IBM-ES??", NAME="boot_disk%n"
64BUS=="scsi", SYSFS{vendor}=="IBM-ESXSS", NAME="boot_disk%n-3"
65EOF
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
73BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", NAME="boot_diskX%n"
74BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", NAME="boot_disk%n"
75EOF
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
83BUS=="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"
84BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", SYSFS{model}=="ST336605LW !#", SYSFS{scsi_level}=="4", SYSFS{rev}=="B245", SYSFS{type}=="0", NAME="boot_disk%n"
85EOF
86 },
87 {
88 desc => "catch device by *",
89 subsys => "tty",
90 devpath => "/class/tty/ttyUSB0",
91 exp_name => "visor/0" ,
92 rules => <<EOF
93KERNEL=="ttyUSB*", NAME="visor/%n"
94EOF
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
102KERNEL=="*USB1", NAME="bad"
103KERNEL=="*USB0", NAME="visor/%n"
104EOF
105 },
106 {
107 desc => "catch device by ?",
108 subsys => "tty",
109 devpath => "/class/tty/ttyUSB0",
110 exp_name => "visor/0" ,
111 rules => <<EOF
112KERNEL=="ttyUSB??*", NAME="visor/%n-1"
113KERNEL=="ttyUSB??", NAME="visor/%n-2"
114KERNEL=="ttyUSB?", NAME="visor/%n"
115EOF
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
123KERNEL=="ttyUSB[A-Z]*", NAME="visor/%n-1"
124KERNEL=="ttyUSB?[0-9]", NAME="visor/%n-2"
125KERNEL=="ttyUSB[0-9]*", NAME="visor/%n"
126EOF
127 },
128 {
129 desc => "replace kernel name",
130 subsys => "tty",
131 devpath => "/class/tty/ttyUSB0",
132 exp_name => "visor" ,
133 rules => <<EOF
134KERNEL=="ttyUSB0", NAME="visor"
135EOF
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
144KERNEL=="ttyUSB0", NAME="visor"
145
146EOF
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
155KERNEL=="ttyUSB0", NAME="visor"
156
157EOF
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
169KERNEL=="ttyUSB0", NAME="whitespace"
170
171
172
173EOF
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
182KERNEL=="ttyUSB0", NAME="visor"
183
184EOF
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
192KERNEL=="ttyUSB0", \\
193NAME="visor"
194
195EOF
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
203KERNEL=="ttyUSB0", PROGRAM=="/bin/echo -e \\101", RESULT=="A", NAME="aaa"
204EOF
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
220KERNEL=="ttyUSB0", \\
221 NAME="visor"
222
223EOF
224 },
225 {
226 desc => "subdirectory handling",
227 subsys => "tty",
228 devpath => "/class/tty/ttyUSB0",
229 exp_name => "sub/direct/ory/visor" ,
230 rules => <<EOF
231KERNEL=="ttyUSB0", NAME="sub/direct/ory/visor"
232EOF
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
240BUS=="scsi", ID=="0:0:0:0", NAME="first_disk%n"
241EOF
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
249BUS=="scsi", ID=="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
250EOF
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
258BUS=="scsi", ID=="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}"
259EOF
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
267BUS=="scsi", IMPORT{file}="test.all", NAME="subdir%E{SYSFSDIR}/node"
268KERNEL=="ttyUSB0", NAME="visor"
269EOF
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
277BUS=="scsi", IMPORT="/bin/echo -e \' TEST_KEY=12345678 \\n TEST_key2=98765 \'", NAME="node\$env{TEST_KEY}"
278KERNEL=="ttyUSB0", NAME="visor"
279EOF
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
287BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="disk-%s{vendor}-%k"
288KERNEL=="ttyUSB0", NAME="visor"
289EOF
290 },
291 {
292 desc => "program result substitution",
293 subsys => "block",
294 devpath => "/block/sda/sda3",
295 exp_name => "special-device-3" ,
296 rules => <<EOF
297BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="-special-*", NAME="%c-1-%n"
298BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special--*", NAME="%c-2-%n"
299BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-device-", NAME="%c-3-%n"
300BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-devic", NAME="%c-4-%n"
301BUS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-*", NAME="%c-%n"
302EOF
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
310BUS=="scsi", PROGRAM=="/bin/echo", RESULT=="block", NAME="subsys_block"
311EOF
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
319BUS=="scsi", PROGRAM=="/bin/echo test", RESULT=="test", NAME="newline_removed"
320EOF
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
328BUS=="scsi", PROGRAM=="/bin/echo -n test-%b", RESULT=="test-0:0*", NAME="%c"
329EOF
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
337BUS=="scsi", PROGRAM=="/bin/echo -n escape-%%n", KERNEL=="sda3", NAME="%c"
338EOF
339 },
340 {
341 desc => "program with lots of arguments",
342 subsys => "block",
343 devpath => "/block/sda/sda3",
344 exp_name => "foo9" ,
345 rules => <<EOF
346BUS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda3", NAME="%c{7}"
347EOF
348 },
349 {
350 desc => "program with subshell",
351 subsys => "block",
352 devpath => "/block/sda/sda3",
353 exp_name => "bar9" ,
354 rules => <<EOF
355BUS=="scsi", PROGRAM=="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL=="sda3", NAME="%c{7}"
356EOF
357 },
358 {
359 desc => "program arguments combined with apostrophes",
360 subsys => "block",
361 devpath => "/block/sda/sda3",
362 exp_name => "foo7" ,
363 rules => <<EOF
364BUS=="scsi", PROGRAM=="/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'", KERNEL=="sda3", NAME="%c{5}"
365EOF
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
373BUS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda3", NAME="my-%c{7}"
374EOF
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
382BUS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda3", NAME="my-%c{6}"
383EOF
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
391BUS=="scsi", ID=="0:0:0:0", NAME="Major:\$major-minor:\$minor-kernelnumber:\$number-bus:\$id"
392EOF
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
400BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="Major:\$major-minor:%m-kernelnumber:\$number-bus:%b"
401EOF
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
409BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="%M%m%b%n"
410EOF
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
418BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="\$major\$minor\$number"
419EOF
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
427BUS=="scsi", ID=="0:0:0:0", DEVPATH="*/sda/*", NAME="\$major%m%n\$id"
428EOF
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
436BUS=="scsi", PROGRAM=="/bin/echo -n foo", RESULT=="foo", NAME="foo"
437KERNEL=="console", NAME="TTY"
438EOF
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
446PROGRAM=="/bin/echo -n foo", RESULT=="foo", NAME="foo"
447KERNEL=="console", NAME="TTY"
448EOF
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
456BUS=="foo", SYSFS{dev}=="5:1", NAME="foo"
457KERNEL=="console", NAME="TTY"
458EOF
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
466SYSFS{dev}=="5:1", NAME="foo"
467KERNEL=="console", NAME="TTY"
468EOF
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
476BUS=="usb", PROGRAM=="/bin/echo -n usb-%b", NAME="%c"
477BUS=="scsi", PROGRAM=="/bin/echo -n scsi-%b", NAME="%c"
478BUS=="foo", PROGRAM=="/bin/echo -n foo-%b", NAME="%c"
479EOF
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
488BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME{all_partitions}="boot_disk"
489EOF
490 },
491 {
492 desc => "sysfs parent hierarchy",
493 subsys => "tty",
494 devpath => "/class/tty/ttyUSB0",
495 exp_name => "visor" ,
496 rules => <<EOF
497SYSFS{idProduct}=="2008", NAME="visor"
498EOF
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
506BUS=="scsi", NAME="%k"
507KERNEL=="ttyUSB0", NAME="visor"
508EOF
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
516KERNEL=="ttyUSB0", NAME="visor"
517EOF
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
525BUS=="scsi", NAME="%k"
526KERNEL=="ttyUSB0", NAME="visor"
527EOF
528 },
529 {
530 desc => "ID rule",
531 subsys => "block",
532 devpath => "/block/sda",
533 exp_name => "scsi-0:0:0:0",
534 rules => <<EOF
535BUS=="usb", ID=="0:0:0:0", NAME="not-scsi"
536BUS=="scsi", ID=="0:0:0:1", NAME="no-match"
537BUS=="scsi", ID==":0", NAME="short-id"
538BUS=="scsi", ID=="/0:0:0:0", NAME="no-match"
539BUS=="scsi", ID=="0:0:0:0", NAME="scsi-0:0:0:0"
540EOF
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
548BUS=="scsi", ID=="*:1", NAME="no-match"
549BUS=="scsi", ID=="*:0:1", NAME="no-match"
550BUS=="scsi", ID=="*:0:0:1", NAME="no-match"
551BUS=="scsi", ID=="*", NAME="scsi-0:0:0:0"
552BUS=="scsi", ID=="0:0:0:0", NAME="bad"
553EOF
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
561BUS=="scsi", ID=="*:0", NAME="scsi-0:0:0:0"
562BUS=="scsi", ID=="0:0:0:0", NAME="bad"
563EOF
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
571BUS=="scsi", ID=="*:0:0:0", NAME="scsi-0:0:0:0"
572BUS=="scsi", ID=="0:0:0:0", NAME="bad"
573EOF
574 },
575 {
576 desc => "ignore SYSFS attribute whitespace",
577 subsys => "block",
578 devpath => "/block/sda",
579 exp_name => "ignored",
580 rules => <<EOF
581BUS=="scsi", SYSFS{whitespace_test}=="WHITE SPACE", NAME="ignored"
582EOF
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
590BUS=="scsi", SYSFS{whitespace_test}=="WHITE SPACE ", NAME="wrong-to-ignore"
591BUS=="scsi", SYSFS{whitespace_test}=="WHITE SPACE ", NAME="matched-with-space"
592EOF
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
601KERNEL=="tty33", NAME="tty33", OWNER="bad", GROUP="name"
602EOF
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
611BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="5000"
612EOF
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
621BUS=="scsi", KERNEL=="sda", NAME="node", GROUP="100"
622EOF
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
631BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="nobody"
632EOF
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
641BUS=="scsi", KERNEL=="sda", NAME="node", GROUP="daemon"
642EOF
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
651BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="root", GROUP="mail"
652EOF
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
661BUS=="scsi", KERNEL=="sda", NAME="node", MODE="0777"
662EOF
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
671BUS=="scsi", KERNEL=="sda", NAME="node", OWNER="5000", GROUP="100", MODE="0777"
672EOF
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
681KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", OWNER="5000"
682EOF
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
691KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", GROUP="100"
692EOF
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
701KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", MODE="0060"
702EOF
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
711KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", OWNER="5000", GROUP="100", MODE="0777"
712EOF
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
721KERNEL=="ttyUSB[0-9]*", OWNER="5000", GROUP="100", MODE="0777"
722KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
723KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n"
724EOF
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
733SUBSYSTEM=="tty", OWNER="3000"
734SUBSYSTEM=="tty", GROUP="4000"
735SUBSYSTEM=="tty", MODE="0777"
736KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
737KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n"
738EOF
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
747SUBSYSTEM=="tty", OWNER="3000"
748SUBSYSTEM=="tty", GROUP="4000"
749SUBSYSTEM=="tty", MODE="0777"
750KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
751KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", GROUP="8000"
752EOF
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
761BUS=="scsi", KERNEL=="sda", NAME="node"
762EOF
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
771KERNEL=="i2c-300", NAME="node"
772EOF
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
781KERNEL=="i2c-fake1", NAME="node"
782EOF
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
791KERNEL=="i2c-fake2", NAME="node"
792EOF
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
801KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
802EOF
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
812KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK=" one two "
813EOF
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
822KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
823EOF
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
832BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
833EOF
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
842BUS=="scsi", SYSFS{vendor}=="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
843EOF
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
852KERNEL=="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
853EOF
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
862BUS=="scsi", KERNEL=="sda", SYMLINK+="symlink-only1"
863BUS=="scsi", KERNEL=="sda", SYMLINK+="symlink-only2"
864BUS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="symlink0"
865EOF
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
876BUS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="."
877EOF
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
888KERNEL=="tty0", NAME="link", SYMLINK+="link"
889EOF
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
898KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="symlink%n"
899EOF
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
908KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="symlink-%k"
909EOF
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
918KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="major-%M:%m"
919EOF
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
928BUS=="scsi", KERNEL=="sda", NAME="node", SYMLINK+="symlink-%b"
929EOF
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
938KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo test" NAME="ttyUSB%n", SYMLINK+="%c"
939EOF
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
948KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo symlink test this" NAME="ttyUSB%n", SYMLINK+="%c{2}"
949EOF
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
958KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo symlink test this" NAME="ttyUSB%n", SYMLINK+="%c{2+}"
959EOF
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
968BUS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
969BUS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="symlink0"
970EOF
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
979KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%s{dev}"
980EOF
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
989KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%3s{dev}"
990EOF
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
999KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="percent%%sign"
1000EOF
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
1009KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%%%k_name"
1010EOF
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
1019BUS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", NAME="%c{1}", SYMLINK+="%c{2} %c{3}"
1020EOF
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
1029BUS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", NAME="%c{1}", SYMLINK+="%c{2+}"
1030EOF
1031 },
1032 {
1033 desc => "enumeration char test (single test)",
1034 subsys => "block",
1035 devpath => "/block/sda",
1036 exp_name => "cdrom",
1037 rules => <<EOF
1038KERNEL=="sda", NAME="cdrom%e"
1039EOF
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
1048KERNEL=="sda", NAME="cdrom%e"
1049EOF
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
1058KERNEL=="sda1", NAME="enum%e"
1059EOF
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
1068KERNEL=="sda2", NAME="cdrom%e"
1069EOF
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
1078KERNEL=="sda3", NAME="enum%e"
1079EOF
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
1088KERNEL=="sda4", NAME="cdrom%e"
1089EOF
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
1097KERNEL=="sda", NAME="cdrom%e"
1098EOF
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
1108BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore"
1109EOF
1110 },
1111 {
1112 desc => "all_partitions, option-only rule",
1113 subsys => "block",
1114 devpath => "/block/sda",
1115 exp_name => "node6",
1116 rules => <<EOF
1117SUBSYSTEM=="block", OPTIONS="all_partitions"
1118BUS=="scsi", KERNEL=="sda", NAME="node"
1119EOF
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
1128SUBSYSTEM=="block", OPTIONS="all_partitions"
1129BUS=="scsi", KERNEL=="sda", NAME="node"
1130EOF
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
1139BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove"
1140EOF
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
1150BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove, all_partitions"
1151EOF
1152 },
1153 {
1154 desc => "SUBSYSTEM match test",
1155 subsys => "block",
1156 devpath => "/block/sda",
1157 exp_name => "node",
1158 rules => <<EOF
1159BUS=="scsi", KERNEL=="sda", NAME="should_not_match", SUBSYSTEM=="vc"
1160BUS=="scsi", KERNEL=="sda", NAME="node", SUBSYSTEM=="block"
1161BUS=="scsi", KERNEL=="sda", NAME="should_not_match2", SUBSYSTEM=="vc"
1162EOF
1163 },
1164 {
1165 desc => "DRIVER match test",
1166 subsys => "block",
1167 devpath => "/block/sda",
1168 exp_name => "node",
1169 rules => <<EOF
1170BUS=="scsi", KERNEL=="sda", NAME="should_not_match", DRIVER=="sd-wrong"
1171BUS=="scsi", KERNEL=="sda", NAME="node", DRIVER=="sd"
1172EOF
1173 },
1174 {
1175 desc => "temporary node creation test",
1176 subsys => "block",
1177 devpath => "/block/sda",
1178 exp_name => "node",
1179 rules => <<EOF
1180BUS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" NAME="node"
1181EOF
1182 },
1183 {
1184 desc => "devpath substitution test",
1185 subsys => "block",
1186 devpath => "/block/sda",
1187 exp_name => "sda",
1188 rules => <<EOF
1189BUS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo %p", RESULT=="/block/sda" NAME="%k"
1190EOF
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
1199BUS=="scsi", KERNEL=="sda", NAME="main_device"
1200EOF
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
1209BUS=="scsi", KERNEL=="sda1", NAME="%P-part-1"
1210EOF
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
1218BUS=="scsi", KERNEL=="sda1", NAME="start-%r-end"
1219EOF
1220 },
1221 {
1222 desc => "last_rule option",
1223 subsys => "block",
1224 devpath => "/block/sda/sda1",
1225 exp_name => "last",
1226 rules => <<EOF
1227BUS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
1228BUS=="scsi", KERNEL=="sda1", NAME="very-last"
1229EOF
1230 },
1231 {
1232 desc => "negation KERNEL!=",
1233 subsys => "block",
1234 devpath => "/block/sda/sda1",
1235 exp_name => "match",
1236 rules => <<EOF
1237BUS=="scsi", KERNEL!="sda1", NAME="matches-but-is-negated"
1238BUS=="scsi", KERNEL!="xsda1", NAME="match"
1239BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1240EOF
1241 },
1242 {
1243 desc => "negation SUBSYSTEM!=",
1244 subsys => "block",
1245 devpath => "/block/sda/sda1",
1246 exp_name => "not-anything",
1247 rules => <<EOF
1248BUS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", NAME="matches-but-is-negated"
1249BUS=="scsi", SUBSYSTEM!="anything", NAME="not-anything"
1250BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1251EOF
1252 },
1253 {
1254 desc => "negation PROGRAM!= exit code",
1255 subsys => "block",
1256 devpath => "/block/sda/sda1",
1257 exp_name => "nonzero-program",
1258 rules => <<EOF
1259KERNEL=="sda1", PROGRAM!="/bin/false", NAME="nonzero-program"
1260BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1261EOF
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
1269KERNEL == "sda1" , NAME = "true"
1270BUS=="scsi", KERNEL=="sda1", NAME="wrong"
1271EOF
1272 },
1273 {
1274 desc => "ENV{} test",
1275 subsys => "block",
1276 devpath => "/block/sda/sda1",
1277 exp_name => "true",
1278 rules => <<EOF
1279BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", NAME="wrong"
1280BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", NAME="true"
1281BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", NAME="bad"
1282EOF
1283 },
1284 {
1285 desc => "ENV{} test",
1286 subsys => "block",
1287 devpath => "/block/sda/sda1",
1288 exp_name => "true",
1289 rules => <<EOF
1290BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", NAME="wrong"
1291BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="/block/sda/sdax1", NAME="no"
1292BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="/block/sda/sda1", NAME="true"
1293BUS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", NAME="bad"
1294EOF
1295 },
1296 {
1297 desc => "ENV{} test (assign)",
1298 subsys => "block",
1299 devpath => "/block/sda/sda1",
1300 exp_name => "true",
1301 rules => <<EOF
1302BUS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1303BUS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", NAME="no"
1304BUS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", NAME="true"
1305BUS=="scsi", KERNEL=="sda1", NAME="bad"
1306EOF
1307 },
1308 {
1309 desc => "ENV{} test (assign2)",
1310 subsys => "block",
1311 devpath => "/block/sda/sda1",
1312 exp_name => "part",
1313 rules => <<EOF
1314SUBSYSTEM=="block", KERNEL="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1315SUBSYSTEM=="block", KERNEL="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
1316ENV{MAINDEVICE}=="true", NAME="disk"
1317ENV{PARTITION}=="true", NAME="part"
1318NAME="bad"
1319EOF
1320 },
1321 {
1322 desc => "untrusted string sanitize",
1323 subsys => "block",
1324 devpath => "/block/sda/sda1",
1325 exp_name => "sane",
1326 rules => <<EOF
1327BUS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/sbin/badprogram)", RESULT="name_ _/sbin/badprogram_", NAME="sane"
1328EOF
1329 },
1330 {
1331 desc => "read sysfs value from device down in the chain",
1332 subsys => "block",
1333 devpath => "/class/tty/ttyUSB0",
1334 exp_name => "serial-0000:00:09.0",
1335 rules => <<EOF
1336KERNEL=="ttyUSB*", NAME="serial-%s{serial}"
1337EOF
1338 },
1339 {
1340 desc => "match against empty key string",
1341 subsys => "block",
1342 devpath => "/block/sda",
1343 exp_name => "ok",
1344 rules => <<EOF
1345KERNEL=="sda", SYSFS{nothing}!="", NAME="not-1-ok"
1346KERNEL=="sda", SYSFS{nothing}=="", NAME="not-2-ok"
1347KERNEL=="sda", SYSFS{vendor}!="", NAME="ok"
1348KERNEL=="sda", SYSFS{vendor}=="", NAME="not-3-ok"
1349EOF
1350 },
1351 {
1352 desc => "check ACTION value",
1353 subsys => "block",
1354 devpath => "/block/sda",
1355 exp_name => "ok",
1356 rules => <<EOF
1357ACTION=="unknown", KERNEL=="sda", NAME="unknown-not-ok"
1358ACTION=="add", KERNEL=="sda", NAME="ok"
1359EOF
1360 },
1361 {
1362 desc => "apply NAME only once",
1363 subsys => "block",
1364 devpath => "/block/sda",
1365 exp_name => "link",
1366 exp_target => "ok",
1367 rules => <<EOF
1368KERNEL=="sda", NAME="ok"
1369KERNEL=="sda", NAME="not-ok"
1370KERNEL=="sda", SYMLINK+="link"
1371EOF
1372 },
1373 {
1374 desc => "test RUN key",
1375 subsys => "block",
1376 devpath => "/block/sda",
1377 exp_name => "testsymlink",
1378 exp_target => "ok",
1379 exp_rem_error => "yes",
1380 option => "clean",
1381 rules => <<EOF
1382KERNEL=="sda", NAME="ok", RUN+="/bin/ln -s ok %r/testsymlink"
1383KERNEL=="sda", NAME="not-ok"
1384EOF
1385 },
1386 {
1387 desc => "test RUN key and DEVNAME",
1388 subsys => "block",
1389 devpath => "/block/sda",
1390 exp_name => "testsymlink",
1391 exp_target => "ok",
1392 exp_rem_error => "yes",
1393 option => "clean",
1394 rules => <<EOF
1395KERNEL=="sda", NAME="ok", RUN+="/bin/sh -c 'ln -s `basename \$\$DEVNAME` %r/testsymlink'"
1396KERNEL=="sda", NAME="not-ok"
1397EOF
1398 },
1399 {
1400 desc => "test RUN key remove",
1401 subsys => "block",
1402 devpath => "/block/sda",
1403 exp_name => "testsymlink2",
1404 exp_target => "ok2",
1405 rules => <<EOF
1406KERNEL=="sda", NAME="ok2", RUN+="/bin/ln -s ok2 %r/testsymlink2"
1407KERNEL=="sda", ACTION=="remove", RUN+="/bin/rm -f %r/testsymlink2"
1408KERNEL=="sda", NAME="not-ok2"
1409EOF
1410 },
1411 {
1412 desc => "final assignment",
1413 subsys => "block",
1414 devpath => "/block/sda",
1415 exp_name => "ok",
1416 exp_perms => "root:nobody:0640",
1417 rules => <<EOF
1418KERNEL=="sda", GROUP:="nobody"
1419KERNEL=="sda", GROUP="not-ok", MODE="0640", NAME="ok"
1420EOF
1421 },
1422 {
1423 desc => "final assignment",
1424 subsys => "block",
1425 devpath => "/block/sda",
1426 exp_name => "ok",
1427 exp_perms => "root:nobody:0640",
1428 rules => <<EOF
1429KERNEL=="sda", GROUP:="nobody"
1430SUBSYSTEM=="block", MODE:="640"
1431KERNEL=="sda", GROUP="not-ok", MODE="0666", NAME="ok"
1432EOF
1433 },
1434 {
1435 desc => "env substitution",
1436 subsys => "block",
1437 devpath => "/block/sda",
1438 exp_name => "node-add-me",
1439 rules => <<EOF
1440KERNEL=="sda", MODE="0666", NAME="node-\$env{ACTION}-me"
1441EOF
1442 },
1443 {
1444 desc => "reset list to current value",
1445 subsys => "tty",
1446 devpath => "/class/tty/ttyUSB0",
1447 exp_name => "three",
1448 not_exp_name => "two",
1449 exp_target => "node",
1450 rules => <<EOF
1451KERNEL=="ttyUSB[0-9]*", SYMLINK+="one"
1452KERNEL=="ttyUSB[0-9]*", SYMLINK+="two"
1453KERNEL=="ttyUSB[0-9]*", SYMLINK="three"
1454KERNEL=="ttyUSB[0-9]*", NAME="node"
1455EOF
1456 },
1457 {
1458 desc => "test empty NAME",
1459 subsys => "tty",
1460 devpath => "/class/tty/ttyUSB0",
1461 exp_name => "node",
1462 not_exp_name => "wrong",
1463 exp_add_error => "yes",
1464 rules => <<EOF
1465KERNEL=="ttyUSB[0-9]*", NAME=""
1466KERNEL=="ttyUSB[0-9]*", NAME="wrong"
1467EOF
1468 },
1469 {
1470 desc => "test empty NAME 2",
1471 subsys => "tty",
1472 devpath => "/class/tty/ttyUSB0",
1473 exp_name => "right",
1474 rules => <<EOF
1475KERNEL=="ttyUSB[0-9]*", NAME="right"
1476KERNEL=="ttyUSB[0-9]*", NAME=""
1477KERNEL=="ttyUSB[0-9]*", NAME="wrong"
1478EOF
1479 },
1480 {
1481 desc => "test multi matches",
1482 subsys => "tty",
1483 devpath => "/class/tty/ttyUSB0",
1484 exp_name => "right",
1485 rules => <<EOF
1486KERNEL=="ttyUSB*|nothing", NAME="right"
1487KERNEL=="ttyUSB*", NAME="wrong"
1488EOF
1489 },
1490 {
1491 desc => "test multi matches 2",
1492 subsys => "tty",
1493 devpath => "/class/tty/ttyUSB0",
1494 exp_name => "right",
1495 rules => <<EOF
1496KERNEL=="dontknow*|*nothing", NAME="nomatch"
1497KERNEL=="dontknow*|ttyUSB*|nothing*", NAME="right"
1498KERNEL=="ttyUSB*", NAME="wrong"
1499EOF
1500 },
1501 {
1502 desc => "IMPORT parent test sequence 1/2 (keep)",
1503 subsys => "block",
1504 devpath => "/block/sda",
1505 exp_name => "parent",
1506 option => "keep",
1507 rules => <<EOF
1508KERNEL=="sda", IMPORT="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1509KERNEL=="sda", NAME="parent"
1510EOF
1511 },
1512 {
1513 desc => "IMPORT parent test sequence 2/2 (keep)",
1514 subsys => "block",
1515 devpath => "/block/sda/sda1",
1516 exp_name => "parentenv-parent_right",
1517 option => "clean",
1518 rules => <<EOF
1519KERNEL=="sda1", IMPORT{parent}="PARENT*", NAME="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
1520EOF
1521 },
1522 {
1523 desc => "GOTO test",
1524 subsys => "block",
1525 devpath => "/block/sda/sda1",
1526 exp_name => "right",
1527 rules => <<EOF
1528KERNEL=="sda1", GOTO="TEST"
1529KERNEL=="sda1", NAME="wrong"
1530KERNEL=="sda1", NAME="", LABEL="NO"
1531KERNEL=="sda1", NAME="right", LABEL="TEST"
1532KERNEL=="sda1", NAME="wrong2"
1533EOF
1534 },
1535);
1536
1537# set env
1538$ENV{ENV_KEY_TEST} = "test";
1539$ENV{SYSFS_PATH} = $sysfs;
1540$ENV{UDEV_CONFIG_FILE} = $udev_conf;
1541$ENV{UDEV_NO_DEVD} = "yes";
1542$ENV{UDEV_NO_HOTPLUGD} = "yes";
1543
1544
1545sub udev {
1546 my ($action, $subsys, $devpath, $rules) = @_;
1547
1548 $ENV{DEVPATH} = $devpath;
1549
1550 # create temporary rules
1551 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1552 print CONF $$rules;
1553 close CONF;
1554
1555 $ENV{ACTION} = $action;
1556 system("$udev_bin $subsys");
1557}
1558
1559my $error = 0;
1560
1561sub permissions_test {
1562 my($rules, $uid, $gid, $mode) = @_;
1563
1564 my $wrong = 0;
1565 my $userid;
1566 my $groupid;
1567
1568 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1569 if ($1 ne "") {
1570 if (defined(getpwnam($1))) {
1571 $userid = int(getpwnam($1));
1572 } else {
1573 $userid = $1;
1574 }
1575 if ($uid != $userid) { $wrong = 1; }
1576 }
1577 if ($2 ne "") {
1578 if (defined(getgrnam($2))) {
1579 $groupid = int(getgrnam($2));
1580 } else {
1581 $groupid = $2;
1582 }
1583 if ($gid != $groupid) { $wrong = 1; }
1584 }
1585 if ($3 ne "") {
1586 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1587 }
1588 if ($wrong == 0) {
1589 print "permissions: ok\n";
1590 } else {
1591 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1592 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1593 print "permissions: error\n";
1594 $error++;
1595 }
1596}
1597
1598sub major_minor_test {
1599 my($rules, $rdev) = @_;
1600
1601 my $major = ($rdev >> 8) & 0xfff;
1602 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1603 my $wrong = 0;
1604
1605 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1606 if ($1 ne "") {
1607 if ($major != $1) { $wrong = 1; };
1608 }
1609 if ($2 ne "") {
1610 if ($minor != $2) { $wrong = 1; };
1611 }
1612 if ($wrong == 0) {
1613 print "major:minor: ok\n";
1614 } else {
1615 printf " expected major:minor is: %i:%i\n", $1, $2;
1616 printf " created major:minor is : %i:%i\n", $major, $minor;
1617 print "major:minor: error\n";
1618 $error++;
1619 }
1620}
1621
1622sub symlink_test {
1623 my ($rules) = @_;
1624
1625 my $output = `ls -l $PWD/$udev_root$rules->{exp_name}`;
1626
1627 if ($output =~ m/(.*)-> (.*)/) {
1628 if ($2 eq $rules->{exp_target}) {
1629 print "symlink: ok\n";
1630 } else {
1631 print " expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1632 print " created symlink from: \'$rules->{exp_name}\' to \'$2\'\n";
1633 print "symlink: error";
1634 if ($rules->{exp_add_error}) {
1635 print " as expected\n";
1636 } else {
1637 print "\n";
1638 $error++;
1639 }
1640 }
1641 } else {
1642 print " expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1643 print "symlink: not created";
1644 if ($rules->{exp_add_error}) {
1645 print " as expected\n";
1646 } else {
1647 print "\n";
1648 $error++;
1649 }
1650 }
1651}
1652
1653sub run_test {
1654 my ($rules, $number) = @_;
1655
1656 print "TEST $number: $rules->{desc}\n";
1657
1658 if ($rules->{exp_target}) {
1659 print "device \'$rules->{devpath}\' expecting symlink '$rules->{exp_name}' to node \'$rules->{exp_target}\'\n";
1660 } else {
1661 print "device \'$rules->{devpath}\' expecting node \'$rules->{exp_name}\'\n";
1662 }
1663
1664
1665 udev("add", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1666 if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1667 (-l "$PWD/$udev_root$rules->{exp_name}")) {
1668
1669 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1670 $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$rules->{exp_name}");
1671
1672 if (defined($rules->{not_exp_name})) {
1673 if ((-e "$PWD/$udev_root$rules->{not_exp_name}") ||
1674 (-l "$PWD/$udev_root$rules->{not_exp_name}")) {
1675 print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1676 $error++
1677 }
1678 }
1679 if (defined($rules->{exp_perms})) {
1680 permissions_test($rules, $uid, $gid, $mode);
1681 }
1682 if (defined($rules->{exp_majorminor})) {
1683 major_minor_test($rules, $rdev);
1684 }
1685 if (defined($rules->{exp_target})) {
1686 symlink_test($rules);
1687 }
1688 print "add: ok\n";
1689 } else {
1690 print "add: error";
1691 if ($rules->{exp_add_error}) {
1692 print " as expected\n";
1693 } else {
1694 print "\n";
1695 system("tree $udev_root");
1696 print "\n";
1697 $error++;
1698 }
1699 }
1700
1701 if (defined($rules->{option}) && $rules->{option} eq "keep") {
1702 print "\n\n";
1703 return;
1704 }
1705
1706 udev("remove", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1707 if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1708 (-l "$PWD/$udev_root$rules->{exp_name}")) {
1709 print "remove: error";
1710 if ($rules->{exp_rem_error}) {
1711 print " as expected\n";
1712 } else {
1713 print "\n";
1714 system("tree $udev_root");
1715 print "\n";
1716 $error++;
1717 }
1718 } else {
1719 print "remove: ok\n";
1720 }
1721
1722 print "\n";
1723
1724 if (defined($rules->{option}) && $rules->{option} eq "clean") {
1725 system("rm -rf $udev_db");
1726 system("rm -rf $udev_root");
1727 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1728 }
1729
1730}
1731
1732# only run if we have root permissions
1733# due to mknod restrictions
1734if (!($<==0)) {
1735 print "Must have root permissions to run properly.\n";
1736 exit;
1737}
1738
1739# prepare
1740system("rm -rf $udev_root");
1741mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1742
1743# create config file
1744open CONF, ">$udev_conf" || die "unable to create config file: $udev_conf";
1745print CONF "udev_root=\"$udev_root\"\n";
1746print CONF "udev_db=\"$udev_db\"\n";
1747print CONF "udev_rules=\"$udev_rules\"\n";
1748close CONF;
1749
1750my $test_num = 1;
1751
1752if ($ARGV[0]) {
1753 # only run one test
1754 $test_num = $ARGV[0];
1755
1756 if (defined($tests[$test_num-1]->{desc})) {
1757 print "udev-test will run test number $test_num only:\n\n";
1758 run_test($tests[$test_num-1], $test_num);
1759 } else {
1760 print "test does not exist.\n";
1761 }
1762} else {
1763 # test all
1764 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1765
1766 foreach my $rules (@tests) {
1767 run_test($rules, $test_num);
1768 $test_num++;
1769 }
1770}
1771
1772print "$error errors occured\n\n";
1773
1774# cleanup
1775system("rm -rf $udev_db");
1776system("rm -rf $udev_root");
1777unlink($udev_rules);
1778unlink($udev_conf);
1779