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