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