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