chiark / gitweb /
fix ENV{TEST}="Test: $env{TEST}"
[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    = "../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            => "ignore ATTRS attribute whitespace",
586                 subsys          => "block",
587                 devpath         => "/block/sda",
588                 exp_name        => "ignored",
589                 rules           => <<EOF
590 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE  SPACE", NAME="ignored"
591 EOF
592         },
593         {
594                 desc            => "do not ignore ATTRS attribute whitespace",
595                 subsys          => "block",
596                 devpath         => "/block/sda",
597                 exp_name        => "matched-with-space",
598                 rules           => <<EOF
599 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE  SPACE ", NAME="wrong-to-ignore"
600 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE  SPACE   ", NAME="matched-with-space"
601 EOF
602         },
603         {
604                 desc            => "permissions USER=bad GROUP=name",
605                 subsys          => "tty",
606                 devpath         => "/class/tty/tty33",
607                 exp_name        => "tty33",
608                 exp_perms       => "0:0:0660",
609                 rules           => <<EOF
610 KERNEL=="tty33", NAME="tty33", OWNER="bad", GROUP="name"
611 EOF
612         },
613         {
614                 desc            => "permissions OWNER=5000",
615                 subsys          => "block",
616                 devpath         => "/block/sda",
617                 exp_name        => "node",
618                 exp_perms       => "5000::0660",
619                 rules           => <<EOF
620 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OWNER="5000"
621 EOF
622         },
623         {
624                 desc            => "permissions GROUP=100",
625                 subsys          => "block",
626                 devpath         => "/block/sda",
627                 exp_name        => "node",
628                 exp_perms       => ":100:0660",
629                 rules           => <<EOF
630 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", GROUP="100"
631 EOF
632         },
633         {
634                 desc            => "textual user id",
635                 subsys          => "block",
636                 devpath         => "/block/sda",
637                 exp_name        => "node",
638                 exp_perms       => "nobody::0660",
639                 rules           => <<EOF
640 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OWNER="nobody"
641 EOF
642         },
643         {
644                 desc            => "textual group id",
645                 subsys          => "block",
646                 devpath         => "/block/sda",
647                 exp_name        => "node",
648                 exp_perms       => ":daemon:0660",
649                 rules           => <<EOF
650 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", GROUP="daemon"
651 EOF
652         },
653         {
654                 desc            => "textual user/group id",
655                 subsys          => "block",
656                 devpath         => "/block/sda",
657                 exp_name        => "node",
658                 exp_perms       => "root:mail:0660",
659                 rules           => <<EOF
660 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OWNER="root", GROUP="mail"
661 EOF
662         },
663         {
664                 desc            => "permissions MODE=0777",
665                 subsys          => "block",
666                 devpath         => "/block/sda",
667                 exp_name        => "node",
668                 exp_perms       => "::0777",
669                 rules           => <<EOF
670 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", MODE="0777"
671 EOF
672         },
673         {
674                 desc            => "permissions OWNER=5000 GROUP=100 MODE=0777",
675                 subsys          => "block",
676                 devpath         => "/block/sda",
677                 exp_name        => "node",
678                 exp_perms       => "5000:100:0777",
679                 rules           => <<EOF
680 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OWNER="5000", GROUP="100", MODE="0777"
681 EOF
682         },
683         {
684                 desc            => "permissions OWNER to 5000",
685                 subsys          => "tty",
686                 devpath         => "/class/tty/ttyUSB0",
687                 exp_name        => "ttyUSB0",
688                 exp_perms       => "5000::",
689                 rules           => <<EOF
690 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", OWNER="5000"
691 EOF
692         },
693         {
694                 desc            => "permissions GROUP to 100",
695                 subsys          => "tty",
696                 devpath         => "/class/tty/ttyUSB0",
697                 exp_name        => "ttyUSB0",
698                 exp_perms       => ":100:0660",
699                 rules           => <<EOF
700 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", GROUP="100"
701 EOF
702         },
703         {
704                 desc            => "permissions MODE to 0060",
705                 subsys          => "tty",
706                 devpath         => "/class/tty/ttyUSB0",
707                 exp_name        => "ttyUSB0",
708                 exp_perms       => "::0060",
709                 rules           => <<EOF
710 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", MODE="0060"
711 EOF
712         },
713         {
714                 desc            => "permissions OWNER, GROUP, MODE",
715                 subsys          => "tty",
716                 devpath         => "/class/tty/ttyUSB0",
717                 exp_name        => "ttyUSB0",
718                 exp_perms       => "5000:100:0777",
719                 rules           => <<EOF
720 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", OWNER="5000", GROUP="100", MODE="0777"
721 EOF
722         },
723         {
724                 desc            => "permissions only rule",
725                 subsys          => "tty",
726                 devpath         => "/class/tty/ttyUSB0",
727                 exp_name        => "ttyUSB0",
728                 exp_perms       => "5000:100:0777",
729                 rules           => <<EOF
730 KERNEL=="ttyUSB[0-9]*", OWNER="5000", GROUP="100", MODE="0777"
731 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
732 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n"
733 EOF
734         },
735         {
736                 desc            => "multiple permissions only rule",
737                 subsys          => "tty",
738                 devpath         => "/class/tty/ttyUSB0",
739                 exp_name        => "ttyUSB0",
740                 exp_perms       => "3000:4000:0777",
741                 rules           => <<EOF
742 SUBSYSTEM=="tty", OWNER="3000"
743 SUBSYSTEM=="tty", GROUP="4000"
744 SUBSYSTEM=="tty", MODE="0777"
745 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
746 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n"
747 EOF
748         },
749         {
750                 desc            => "permissions only rule with override at NAME rule",
751                 subsys          => "tty",
752                 devpath         => "/class/tty/ttyUSB0",
753                 exp_name        => "ttyUSB0",
754                 exp_perms       => "3000:8000:0777",
755                 rules           => <<EOF
756 SUBSYSTEM=="tty", OWNER="3000"
757 SUBSYSTEM=="tty", GROUP="4000"
758 SUBSYSTEM=="tty", MODE="0777"
759 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
760 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", GROUP="8000"
761 EOF
762         },
763         {
764                 desc            => "major/minor number test",
765                 subsys          => "block",
766                 devpath         => "/block/sda",
767                 exp_name        => "node",
768                 exp_majorminor  => "8:0",
769                 rules           => <<EOF
770 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node"
771 EOF
772         },
773         {
774                 desc            => "big minor number test",
775                 subsys          => "i2c-dev",
776                 devpath         => "/class/i2c-dev/i2c-300",
777                 exp_name        => "node",
778                 exp_majorminor  => "89:300",
779                 rules           => <<EOF
780 KERNEL=="i2c-300", NAME="node"
781 EOF
782         },
783         {
784                 desc            => "big major number test",
785                 subsys          => "i2c-dev",
786                 devpath         => "/class/i2c-dev/i2c-fake1",
787                 exp_name        => "node",
788                 exp_majorminor  => "4095:1",
789                 rules           => <<EOF
790 KERNEL=="i2c-fake1", NAME="node"
791 EOF
792         },
793         {
794                 desc            => "big major and big minor number test",
795                 subsys          => "i2c-dev",
796                 devpath         => "/class/i2c-dev/i2c-fake2",
797                 exp_name        => "node",
798                 exp_majorminor  => "4094:89999",
799                 rules           => <<EOF
800 KERNEL=="i2c-fake2", NAME="node"
801 EOF
802         },
803         {
804                 desc            => "multiple symlinks with format char",
805                 subsys          => "tty",
806                 devpath         => "/class/tty/ttyUSB0",
807                 exp_name        => "symlink2-ttyUSB0",
808                 exp_target      => "ttyUSB0",
809                 rules           => <<EOF
810 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
811 EOF
812         },
813         {
814                 desc            => "multiple symlinks with a lot of s p a c e s",
815                 subsys          => "tty",
816                 devpath         => "/class/tty/ttyUSB0",
817                 exp_name        => "one",
818                 not_exp_name    => " ",
819                 exp_target      => "ttyUSB0",
820                 rules           => <<EOF
821 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="  one     two        "
822 EOF
823         },
824         {
825                 desc            => "symlink creation (same directory)",
826                 subsys          => "tty",
827                 devpath         => "/class/tty/ttyUSB0",
828                 exp_name        => "visor0",
829                 exp_target      => "ttyUSB0",
830                 rules           => <<EOF
831 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
832 EOF
833         },
834         {
835                 desc            => "symlink creation (relative link forward)",
836                 subsys          => "block",
837                 devpath         => "/block/sda/sda2",
838                 exp_name        => "1/2/symlink" ,
839                 exp_target      => "a/b/node",
840                 rules           => <<EOF
841 SUBSYSTEMS=="scsi", ATTRS{vendor}=="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
842 EOF
843         },
844         {
845                 desc            => "symlink creation (relative link back and forward)",
846                 subsys          => "block",
847                 devpath         => "/block/sda/sda2",
848                 exp_name        => "1/2/c/d/symlink" ,
849                 exp_target      => "../../a/b/node",
850                 rules           => <<EOF
851 SUBSYSTEMS=="scsi", ATTRS{vendor}=="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
852 EOF
853         },
854         {
855                 desc            => "multiple symlinks",
856                 subsys          => "tty",
857                 devpath         => "/class/tty/ttyUSB0",
858                 exp_name        => "second-0" ,
859                 exp_target      => "visor" ,
860                 rules           => <<EOF
861 KERNEL=="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
862 EOF
863         },
864         {
865                 desc            => "symlink only rule",
866                 subsys          => "block",
867                 devpath         => "/block/sda",
868                 exp_name        => "symlink-only2",
869                 exp_target      => "link",
870                 rules           => <<EOF
871 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-only1"
872 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-only2"
873 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="symlink0"
874 EOF
875         },
876         {
877                 desc            => "symlink name '.'",
878                 subsys          => "block",
879                 devpath         => "/block/sda",
880                 exp_name        => ".",
881                 exp_target      => "link",
882                 exp_add_error   => "yes",
883                 exp_rem_error   => "yes",
884                 rules           => <<EOF
885 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="."
886 EOF
887         },
888         {
889                 desc            => "symlink node to itself",
890                 subsys          => "tty",
891                 devpath         => "/class/tty/tty0",
892                 exp_name        => "link",
893                 exp_target      => "link",
894                 exp_add_error   => "yes",
895                 exp_rem_error   => "yes",
896                 option          => "clean",
897                 rules           => <<EOF
898 KERNEL=="tty0", NAME="link", SYMLINK+="link"
899 EOF
900         },
901         {
902                 desc            => "symlink %n substitution",
903                 subsys          => "tty",
904                 devpath         => "/class/tty/ttyUSB0",
905                 exp_name        => "symlink0",
906                 exp_target      => "ttyUSB0",
907                 rules           => <<EOF
908 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="symlink%n"
909 EOF
910         },
911         {
912                 desc            => "symlink %k substitution",
913                 subsys          => "tty",
914                 devpath         => "/class/tty/ttyUSB0",
915                 exp_name        => "symlink-ttyUSB0",
916                 exp_target      => "ttyUSB0",
917                 rules           => <<EOF
918 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="symlink-%k"
919 EOF
920         },
921         {
922                 desc            => "symlink %M:%m substitution",
923                 subsys          => "tty",
924                 devpath         => "/class/tty/ttyUSB0",
925                 exp_name        => "major-188:0",
926                 exp_target      => "ttyUSB0",
927                 rules           => <<EOF
928 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="major-%M:%m"
929 EOF
930         },
931         {
932                 desc            => "symlink %b substitution",
933                 subsys          => "block",
934                 devpath         => "/block/sda",
935                 exp_name        => "symlink-0:0:0:0",
936                 exp_target      => "node",
937                 rules           => <<EOF
938 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", SYMLINK+="symlink-%b"
939 EOF
940         },
941         {
942                 desc            => "symlink %c substitution",
943                 subsys          => "tty",
944                 devpath         => "/class/tty/ttyUSB0",
945                 exp_name        => "test",
946                 exp_target      => "ttyUSB0",
947                 rules           => <<EOF
948 KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo test" NAME="ttyUSB%n", SYMLINK+="%c"
949 EOF
950         },
951         {
952                 desc            => "symlink %c{N} substitution",
953                 subsys          => "tty",
954                 devpath         => "/class/tty/ttyUSB0",
955                 exp_name        => "test",
956                 exp_target      => "ttyUSB0",
957                 rules           => <<EOF
958 KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo symlink test this" NAME="ttyUSB%n", SYMLINK+="%c{2}"
959 EOF
960         },
961         {
962                 desc            => "symlink %c{N+} substitution",
963                 subsys          => "tty",
964                 devpath         => "/class/tty/ttyUSB0",
965                 exp_name        => "this",
966                 exp_target      => "ttyUSB0",
967                 rules           => <<EOF
968 KERNEL=="ttyUSB[0-9]*", PROGRAM=="/bin/echo symlink test this" NAME="ttyUSB%n", SYMLINK+="%c{2+}"
969 EOF
970         },
971         {
972                 desc            => "symlink only rule with %c{N+}",
973                 subsys          => "block",
974                 devpath         => "/block/sda",
975                 exp_name        => "test",
976                 exp_target      => "link",
977                 rules           => <<EOF
978 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
979 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="link", SYMLINK+="symlink0"
980 EOF
981         },
982         {
983                 desc            => "symlink %s{filename} substitution",
984                 subsys          => "tty",
985                 devpath         => "/class/tty/ttyUSB0",
986                 exp_name        => "188:0",
987                 exp_target      => "ttyUSB0",
988                 rules           => <<EOF
989 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%s{dev}"
990 EOF
991         },
992         {
993                 desc            => "symlink %Ns{filename} substitution",
994                 subsys          => "tty",
995                 devpath         => "/class/tty/ttyUSB0",
996                 exp_name        => "188",
997                 exp_target      => "ttyUSB0",
998                 rules           => <<EOF
999 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%3s{dev}"
1000 EOF
1001         },
1002         {
1003                 desc            => "symlink with '%' in name",
1004                 subsys          => "tty",
1005                 devpath         => "/class/tty/ttyUSB0",
1006                 exp_name        => "percent%sign",
1007                 exp_target      => "ttyUSB0",
1008                 rules           => <<EOF
1009 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="percent%%sign"
1010 EOF
1011         },
1012         {
1013                 desc            => "symlink with '%' in name",
1014                 subsys          => "tty",
1015                 devpath         => "/class/tty/ttyUSB0",
1016                 exp_name        => "%ttyUSB0_name",
1017                 exp_target      => "ttyUSB0",
1018                 rules           => <<EOF
1019 KERNEL=="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK+="%%%k_name"
1020 EOF
1021         },
1022         {
1023                 desc            => "program result substitution (numbered part of)",
1024                 subsys          => "block",
1025                 devpath         => "/block/sda/sda3",
1026                 exp_name        => "link1",
1027                 exp_target      => "node",
1028                 rules           => <<EOF
1029 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", NAME="%c{1}", SYMLINK+="%c{2} %c{3}"
1030 EOF
1031         },
1032         {
1033                 desc            => "program result substitution (numbered part of+)",
1034                 subsys          => "block",
1035                 devpath         => "/block/sda/sda3",
1036                 exp_name        => "link4",
1037                 exp_target      => "node",
1038                 rules           => <<EOF
1039 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", NAME="%c{1}", SYMLINK+="%c{2+}"
1040 EOF
1041         },
1042         {
1043                 desc            => "ignore rule test",
1044                 subsys          => "block",
1045                 devpath         => "/block/sda",
1046                 exp_name        => "nothing",
1047                 not_exp_name    => "node",
1048                 exp_add_error   => "yes",
1049                 rules           => <<EOF
1050 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore"
1051 EOF
1052         },
1053         {
1054                 desc            => "all_partitions, option-only rule",
1055                 subsys          => "block",
1056                 devpath         => "/block/sda",
1057                 exp_name        => "node6",
1058                 rules           => <<EOF
1059 SUBSYSTEM=="block", OPTIONS="all_partitions"
1060 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node"
1061 EOF
1062         },
1063         {
1064                 desc            => "all_partitions, option-only rule (fail on partition)",
1065                 subsys          => "block",
1066                 devpath         => "/block/sda/sda1",
1067                 exp_name        => "node6",
1068                 exp_add_error   => "yes",
1069                 rules           => <<EOF
1070 SUBSYSTEM=="block", OPTIONS="all_partitions"
1071 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node"
1072 EOF
1073         },
1074         {
1075                 desc            => "ignore remove event test",
1076                 subsys          => "block",
1077                 devpath         => "/block/sda",
1078                 exp_name        => "node",
1079                 exp_rem_error   => "yes",
1080                 rules           => <<EOF
1081 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove"
1082 EOF
1083         },
1084         {
1085                 desc            => "ignore remove event test (with all partitions)",
1086                 subsys          => "block",
1087                 devpath         => "/block/sda",
1088                 exp_name        => "node14",
1089                 exp_rem_error   => "yes",
1090                 option          => "clean",
1091                 rules           => <<EOF
1092 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove, all_partitions"
1093 EOF
1094         },
1095         {
1096                 desc            => "SUBSYSTEM match test",
1097                 subsys          => "block",
1098                 devpath         => "/block/sda",
1099                 exp_name        => "node",
1100                 rules           => <<EOF
1101 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="should_not_match", SUBSYSTEM=="vc"
1102 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", SUBSYSTEM=="block"
1103 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="should_not_match2", SUBSYSTEM=="vc"
1104 EOF
1105         },
1106         {
1107                 desc            => "DRIVERS match test",
1108                 subsys          => "block",
1109                 devpath         => "/block/sda",
1110                 exp_name        => "node",
1111                 rules           => <<EOF
1112 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="should_not_match", DRIVERS=="sd-wrong"
1113 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", DRIVERS=="sd"
1114 EOF
1115         },
1116         {
1117                 desc            => "temporary node creation test",
1118                 subsys          => "block",
1119                 devpath         => "/block/sda",
1120                 exp_name        => "node",
1121                 rules           => <<EOF
1122 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" NAME="node"
1123 EOF
1124         },
1125         {
1126                 desc            => "devpath substitution test",
1127                 subsys          => "block",
1128                 devpath         => "/block/sda",
1129                 exp_name        => "sda",
1130                 rules           => <<EOF
1131 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo %p", RESULT=="/block/sda" NAME="%k"
1132 EOF
1133         },
1134         {
1135                 desc            => "parent node name substitution test sequence 1/2 (keep)",
1136                 subsys          => "block",
1137                 devpath         => "/block/sda",
1138                 exp_name        => "main_device",
1139                 option          => "keep",
1140                 rules           => <<EOF
1141 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="main_device"
1142 EOF
1143         },
1144         {
1145                 desc            => "parent node name substitution test sequence 2/2 (clean)",
1146                 subsys          => "block",
1147                 devpath         => "/block/sda/sda1",
1148                 exp_name        => "main_device-part-1",
1149                 option          => "clean",
1150                 rules           => <<EOF
1151 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="%P-part-1"
1152 EOF
1153         },
1154         {
1155                 desc            => "udev_root substitution",
1156                 subsys          => "block",
1157                 devpath         => "/block/sda/sda1",
1158                 exp_name        => "start-udev-root-end",
1159                 rules           => <<EOF
1160 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="start-%r-end"
1161 EOF
1162         },
1163         {
1164                 desc            => "last_rule option",
1165                 subsys          => "block",
1166                 devpath         => "/block/sda/sda1",
1167                 exp_name        => "last",
1168                 rules           => <<EOF
1169 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
1170 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="very-last"
1171 EOF
1172         },
1173         {
1174                 desc            => "negation KERNEL!=",
1175                 subsys          => "block",
1176                 devpath         => "/block/sda/sda1",
1177                 exp_name        => "match",
1178                 rules           => <<EOF
1179 SUBSYSTEMS=="scsi", KERNEL!="sda1", NAME="matches-but-is-negated"
1180 SUBSYSTEMS=="scsi", KERNEL!="xsda1", NAME="match"
1181 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="wrong"
1182 EOF
1183         },
1184         {
1185                 desc            => "negation SUBSYSTEM!=",
1186                 subsys          => "block",
1187                 devpath         => "/block/sda/sda1",
1188                 exp_name        => "not-anything",
1189                 rules           => <<EOF
1190 SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", NAME="matches-but-is-negated"
1191 SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", NAME="not-anything"
1192 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="wrong"
1193 EOF
1194         },
1195         {
1196                 desc            => "negation PROGRAM!= exit code",
1197                 subsys          => "block",
1198                 devpath         => "/block/sda/sda1",
1199                 exp_name        => "nonzero-program",
1200                 rules           => <<EOF
1201 KERNEL=="sda1", PROGRAM!="/bin/false", NAME="nonzero-program"
1202 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="wrong"
1203 EOF
1204         },
1205         {
1206                 desc            => "test for whitespace between the operator",
1207                 subsys          => "block",
1208                 devpath         => "/block/sda/sda1",
1209                 exp_name        => "true",
1210                 rules           => <<EOF
1211 KERNEL   ==   "sda1"     ,    NAME   =    "true"
1212 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="wrong"
1213 EOF
1214         },
1215         {
1216                 desc            => "ENV{} test",
1217                 subsys          => "block",
1218                 devpath         => "/block/sda/sda1",
1219                 exp_name        => "true",
1220                 rules           => <<EOF
1221 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", NAME="wrong"
1222 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", NAME="true"
1223 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", NAME="bad"
1224 EOF
1225         },
1226         {
1227                 desc            => "ENV{} test",
1228                 subsys          => "block",
1229                 devpath         => "/block/sda/sda1",
1230                 exp_name        => "true",
1231                 rules           => <<EOF
1232 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", NAME="wrong"
1233 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="/block/sda/sdax1", NAME="no"
1234 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="/block/sda/sda1", NAME="true"
1235 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", NAME="bad"
1236 EOF
1237         },
1238         {
1239                 desc            => "ENV{} test (assign)",
1240                 subsys          => "block",
1241                 devpath         => "/block/sda/sda1",
1242                 exp_name        => "true",
1243                 rules           => <<EOF
1244 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1245 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", NAME="no"
1246 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", NAME="true"
1247 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="bad"
1248 EOF
1249         },
1250         {
1251                 desc            => "ENV{} test (assign 2 times)",
1252                 subsys          => "block",
1253                 devpath         => "/block/sda/sda1",
1254                 exp_name        => "true",
1255                 rules           => <<EOF
1256 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1257 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
1258 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", NAME="no"
1259 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", NAME="true"
1260 SUBSYSTEMS=="scsi", KERNEL=="sda1", NAME="bad"
1261 EOF
1262         },
1263         {
1264                 desc            => "ENV{} test (assign2)",
1265                 subsys          => "block",
1266                 devpath         => "/block/sda/sda1",
1267                 exp_name        => "part",
1268                 rules           => <<EOF
1269 SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1270 SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
1271 ENV{MAINDEVICE}=="true", NAME="disk"
1272 ENV{PARTITION}=="true", NAME="part"
1273 NAME="bad"
1274 EOF
1275         },
1276         {
1277                 desc            => "untrusted string sanitize",
1278                 subsys          => "block",
1279                 devpath         => "/block/sda/sda1",
1280                 exp_name        => "sane",
1281                 rules           => <<EOF
1282 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/sbin/badprogram)", RESULT=="name_ _/sbin/badprogram_", NAME="sane"
1283 EOF
1284         },
1285         {
1286                 desc            => "untrusted string sanitize (don't replace utf8)",
1287                 subsys          => "block",
1288                 devpath         => "/block/sda/sda1",
1289                 exp_name        => "uber",
1290                 rules           => <<EOF
1291 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", NAME="uber"
1292 EOF
1293         },
1294         {
1295                 desc            => "untrusted string sanitize (replace invalid utf8)",
1296                 subsys          => "block",
1297                 devpath         => "/block/sda/sda1",
1298                 exp_name        => "replaced",
1299                 rules           => <<EOF
1300 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", NAME="replaced"
1301 EOF
1302         },
1303         {
1304                 desc            => "read sysfs value from device down in the chain",
1305                 subsys          => "block",
1306                 devpath         => "/class/tty/ttyUSB0",
1307                 exp_name        => "serial-0000:00:09.0",
1308                 rules           => <<EOF
1309 KERNEL=="ttyUSB*", NAME="serial-%s{serial}"
1310 EOF
1311         },
1312         {
1313                 desc            => "match against empty key string",
1314                 subsys          => "block",
1315                 devpath         => "/block/sda",
1316                 exp_name        => "ok",
1317                 rules           => <<EOF
1318 KERNEL=="sda", ATTRS{nothing}!="", NAME="not-1-ok"
1319 KERNEL=="sda", ATTRS{nothing}=="", NAME="not-2-ok"
1320 KERNEL=="sda", ATTRS{vendor}!="", NAME="ok"
1321 KERNEL=="sda", ATTRS{vendor}=="", NAME="not-3-ok"
1322 EOF
1323         },
1324         {
1325                 desc            => "check ACTION value",
1326                 subsys          => "block",
1327                 devpath         => "/block/sda",
1328                 exp_name        => "ok",
1329                 rules           => <<EOF
1330 ACTION=="unknown", KERNEL=="sda", NAME="unknown-not-ok"
1331 ACTION=="add", KERNEL=="sda", NAME="ok"
1332 EOF
1333         },
1334         {
1335                 desc            => "apply NAME only once",
1336                 subsys          => "block",
1337                 devpath         => "/block/sda",
1338                 exp_name        => "link",
1339                 exp_target      => "ok",
1340                 rules           => <<EOF
1341 KERNEL=="sda", NAME="ok"
1342 KERNEL=="sda", NAME="not-ok"
1343 KERNEL=="sda", SYMLINK+="link"
1344 EOF
1345         },
1346         {
1347                 desc            => "test RUN key",
1348                 subsys          => "block",
1349                 devpath         => "/block/sda",
1350                 exp_name        => "testsymlink",
1351                 exp_target      => "ok",
1352                 exp_rem_error   => "yes",
1353                 option          => "clean",
1354                 rules           => <<EOF
1355 KERNEL=="sda", NAME="ok", RUN+="/bin/ln -s ok %r/testsymlink"
1356 KERNEL=="sda", NAME="not-ok"
1357 EOF
1358         },
1359         {
1360                 desc            => "test RUN key and DEVNAME",
1361                 subsys          => "block",
1362                 devpath         => "/block/sda",
1363                 exp_name        => "testsymlink",
1364                 exp_target      => "ok",
1365                 exp_rem_error   => "yes",
1366                 option          => "clean",
1367                 rules           => <<EOF
1368 KERNEL=="sda", NAME="ok", RUN+="/bin/sh -c 'ln -s `basename \$\$DEVNAME` %r/testsymlink'"
1369 KERNEL=="sda", NAME="not-ok"
1370 EOF
1371         },
1372         {
1373                 desc            => "test RUN key remove",
1374                 subsys          => "block",
1375                 devpath         => "/block/sda",
1376                 exp_name        => "testsymlink2",
1377                 exp_target      => "ok2",
1378                 rules           => <<EOF
1379 KERNEL=="sda", NAME="ok2", RUN+="/bin/ln -s ok2 %r/testsymlink2"
1380 KERNEL=="sda", ACTION=="remove", RUN+="/bin/rm -f %r/testsymlink2"
1381 KERNEL=="sda", NAME="not-ok2"
1382 EOF
1383         },
1384         {
1385                 desc            => "final assignment",
1386                 subsys          => "block",
1387                 devpath         => "/block/sda",
1388                 exp_name        => "ok",
1389                 exp_perms       => "root:nobody:0640",
1390                 rules           => <<EOF
1391 KERNEL=="sda", GROUP:="nobody"
1392 KERNEL=="sda", GROUP="not-ok", MODE="0640", NAME="ok"
1393 EOF
1394         },
1395         {
1396                 desc            => "final assignment",
1397                 subsys          => "block",
1398                 devpath         => "/block/sda",
1399                 exp_name        => "ok",
1400                 exp_perms       => "root:nobody:0640",
1401                 rules           => <<EOF
1402 KERNEL=="sda", GROUP:="nobody"
1403 SUBSYSTEM=="block", MODE:="640"
1404 KERNEL=="sda", GROUP="not-ok", MODE="0666", NAME="ok"
1405 EOF
1406         },
1407         {
1408                 desc            => "env substitution",
1409                 subsys          => "block",
1410                 devpath         => "/block/sda",
1411                 exp_name        => "node-add-me",
1412                 rules           => <<EOF
1413 KERNEL=="sda", MODE="0666", NAME="node-\$env{ACTION}-me"
1414 EOF
1415         },
1416         {
1417                 desc            => "reset list to current value",
1418                 subsys          => "tty",
1419                 devpath         => "/class/tty/ttyUSB0",
1420                 exp_name        => "three",
1421                 not_exp_name    => "two",
1422                 exp_target      => "node",
1423                 rules           => <<EOF
1424 KERNEL=="ttyUSB[0-9]*", SYMLINK+="one"
1425 KERNEL=="ttyUSB[0-9]*", SYMLINK+="two"
1426 KERNEL=="ttyUSB[0-9]*", SYMLINK="three"
1427 KERNEL=="ttyUSB[0-9]*", NAME="node"
1428 EOF
1429         },
1430         {
1431                 desc            => "test empty NAME",
1432                 subsys          => "tty",
1433                 devpath         => "/class/tty/ttyUSB0",
1434                 exp_name        => "node",
1435                 not_exp_name    => "wrong",
1436                 exp_add_error   => "yes",
1437                 rules           => <<EOF
1438 KERNEL=="ttyUSB[0-9]*", NAME=""
1439 KERNEL=="ttyUSB[0-9]*", NAME="wrong"
1440 EOF
1441         },
1442         {
1443                 desc            => "test empty NAME 2",
1444                 subsys          => "tty",
1445                 devpath         => "/class/tty/ttyUSB0",
1446                 exp_name        => "right",
1447                 rules           => <<EOF
1448 KERNEL=="ttyUSB[0-9]*", NAME="right"
1449 KERNEL=="ttyUSB[0-9]*", NAME=""
1450 KERNEL=="ttyUSB[0-9]*", NAME="wrong"
1451 EOF
1452         },
1453         {
1454                 desc            => "test multi matches",
1455                 subsys          => "tty",
1456                 devpath         => "/class/tty/ttyUSB0",
1457                 exp_name        => "right",
1458                 rules           => <<EOF
1459 KERNEL=="ttyUSB*|nothing", NAME="right"
1460 KERNEL=="ttyUSB*", NAME="wrong"
1461 EOF
1462         },
1463         {
1464                 desc            => "test multi matches 2",
1465                 subsys          => "tty",
1466                 devpath         => "/class/tty/ttyUSB0",
1467                 exp_name        => "right",
1468                 rules           => <<EOF
1469 KERNEL=="dontknow*|*nothing", NAME="nomatch"
1470 KERNEL=="dontknow*|ttyUSB*|nothing*", NAME="right"
1471 KERNEL=="ttyUSB*", NAME="wrong"
1472 EOF
1473         },
1474         {
1475                 desc            => "IMPORT parent test sequence 1/2 (keep)",
1476                 subsys          => "block",
1477                 devpath         => "/block/sda",
1478                 exp_name        => "parent",
1479                 option          => "keep",
1480                 rules           => <<EOF
1481 KERNEL=="sda", IMPORT="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1482 KERNEL=="sda", NAME="parent"
1483 EOF
1484         },
1485         {
1486                 desc            => "IMPORT parent test sequence 2/2 (keep)",
1487                 subsys          => "block",
1488                 devpath         => "/block/sda/sda1",
1489                 exp_name        => "parentenv-parent_right",
1490                 option          => "clean",
1491                 rules           => <<EOF
1492 KERNEL=="sda1", IMPORT{parent}="PARENT*", NAME="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
1493 EOF
1494         },
1495         {
1496                 desc            => "GOTO test",
1497                 subsys          => "block",
1498                 devpath         => "/block/sda/sda1",
1499                 exp_name        => "right",
1500                 rules           => <<EOF
1501 KERNEL=="sda1", GOTO="TEST"
1502 KERNEL=="sda1", NAME="wrong"
1503 KERNEL=="sda1", NAME="", LABEL="NO"
1504 KERNEL=="sda1", NAME="right", LABEL="TEST"
1505 KERNEL=="sda1", NAME="wrong2"
1506 EOF
1507         },
1508         {
1509                 desc            => "NAME compare test",
1510                 subsys          => "block",
1511                 devpath         => "/block/sda/sda1",
1512                 exp_name        => "link",
1513                 exp_target      => "node",
1514                 not_exp_name    => "wronglink",
1515                 rules           => <<EOF
1516 KERNEL=="sda1", NAME="node"
1517 KERNEL=="sda2", NAME="wrong"
1518 KERNEL=="sda1", NAME=="wrong*", SYMLINK+="wronglink"
1519 KERNEL=="sda1", NAME=="?*", SYMLINK+="link"
1520 KERNEL=="sda1", NAME=="node*", SYMLINK+="link2"
1521 EOF
1522         },
1523         {
1524                 desc            => "NAME compare test 2",
1525                 subsys          => "block",
1526                 devpath         => "/block/sda/sda1",
1527                 exp_name        => "link2",
1528                 exp_target      => "sda1",
1529                 not_exp_name    => "link",
1530                 rules           => <<EOF
1531 KERNEL=="sda1", NAME=="?*", SYMLINK+="link"
1532 KERNEL=="sda1", NAME!="?*", SYMLINK+="link2"
1533 EOF
1534         },
1535         {
1536                 desc            => "invalid key operation",
1537                 subsys          => "block",
1538                 devpath         => "/block/sda/sda1",
1539                 exp_name        => "yes",
1540                 rules           => <<EOF
1541 KERNEL="sda1", NAME=="no"
1542 KERNEL=="sda1", NAME="yes"
1543 EOF
1544         },
1545         {
1546                 desc            => "overlong comment line",
1547                 subsys          => "block",
1548                 devpath         => "/block/sda/sda1",
1549                 exp_name        => "yes",
1550                 rules           => <<EOF
1551 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1552    # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1553 KERNEL="sda1", NAME=="no"
1554 KERNEL=="sda1", NAME="yes"
1555 EOF
1556         },
1557 );
1558
1559 # set env
1560 $ENV{ENV_KEY_TEST} = "test";
1561 $ENV{SYSFS_PATH} = $sysfs;
1562 $ENV{UDEV_CONFIG_FILE} = $udev_conf;
1563
1564 sub udev {
1565         my ($action, $subsys, $devpath, $rules) = @_;
1566
1567         $ENV{DEVPATH} = $devpath;
1568
1569         # create temporary rules
1570         open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1571         print CONF $$rules;
1572         close CONF;
1573
1574         $ENV{ACTION} = $action;
1575         system("$udev_bin $subsys");
1576 }
1577
1578 my $error = 0;
1579
1580 sub permissions_test {
1581         my($rules, $uid, $gid, $mode) = @_;
1582
1583         my $wrong = 0;
1584         my $userid;
1585         my $groupid;
1586
1587         $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1588         if ($1 ne "") {
1589                 if (defined(getpwnam($1))) {
1590                         $userid = int(getpwnam($1));
1591                 } else {
1592                         $userid = $1;
1593                 }
1594                 if ($uid != $userid) { $wrong = 1; }
1595         }
1596         if ($2 ne "") {
1597                 if (defined(getgrnam($2))) {
1598                         $groupid = int(getgrnam($2));
1599                 } else {
1600                         $groupid = $2;
1601                 }
1602                 if ($gid != $groupid) { $wrong = 1; }
1603         }
1604         if ($3 ne "") {
1605                 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1606         }
1607         if ($wrong == 0) {
1608                 print "permissions: ok\n";
1609         } else {
1610                 printf "  expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1611                 printf "  created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1612                 print "permissions: error\n";
1613                 $error++;
1614         }
1615 }
1616
1617 sub major_minor_test {
1618         my($rules, $rdev) = @_;
1619
1620         my $major = ($rdev >> 8) & 0xfff;
1621         my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1622         my $wrong = 0;
1623
1624         $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1625         if ($1 ne "") {
1626                 if ($major != $1) { $wrong = 1; };
1627         }
1628         if ($2 ne "") {
1629                 if ($minor != $2) { $wrong = 1; };
1630         }
1631         if ($wrong == 0) {
1632                 print "major:minor: ok\n";
1633         } else {
1634                 printf "  expected major:minor is: %i:%i\n", $1, $2;
1635                 printf "  created major:minor is : %i:%i\n", $major, $minor;
1636                 print "major:minor: error\n";
1637                 $error++;
1638         }
1639 }
1640
1641 sub symlink_test {
1642         my ($rules) = @_;
1643
1644         my $output = `ls -l $PWD/$udev_root$rules->{exp_name}`;
1645
1646         if ($output =~ m/(.*)-> (.*)/) {
1647                 if ($2 eq $rules->{exp_target}) {
1648                         print "symlink:     ok\n";
1649                 } else {
1650                         print "  expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1651                         print "  created symlink from: \'$rules->{exp_name}\' to \'$2\'\n";
1652                         print "symlink: error";
1653                         if ($rules->{exp_add_error}) {
1654                                 print " as expected\n";
1655                         } else {
1656                                 print "\n";
1657                                 $error++;
1658                         }
1659                 }
1660         } else {
1661                 print "  expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1662                 print "symlink:     not created";
1663                 if ($rules->{exp_add_error}) {
1664                         print " as expected\n";
1665                 } else {
1666                         print "\n";
1667                         $error++;
1668                 }
1669         }
1670 }
1671
1672 sub run_test {
1673         my ($rules, $number) = @_;
1674
1675         print "TEST $number: $rules->{desc}\n";
1676
1677         if ($rules->{exp_target}) {
1678                 print "device \'$rules->{devpath}\' expecting symlink '$rules->{exp_name}' to node \'$rules->{exp_target}\'\n";
1679         } else {
1680                 print "device \'$rules->{devpath}\' expecting node \'$rules->{exp_name}\'\n";
1681         }
1682
1683
1684         udev("add", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1685         if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1686             (-l "$PWD/$udev_root$rules->{exp_name}")) {
1687
1688                 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1689                     $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$rules->{exp_name}");
1690
1691                 if (defined($rules->{not_exp_name})) {
1692                         if ((-e "$PWD/$udev_root$rules->{not_exp_name}") ||
1693                             (-l "$PWD/$udev_root$rules->{not_exp_name}")) {
1694                                 print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1695                                 $error++
1696                         }
1697                 }
1698                 if (defined($rules->{exp_perms})) {
1699                         permissions_test($rules, $uid, $gid, $mode);
1700                 }
1701                 if (defined($rules->{exp_majorminor})) {
1702                         major_minor_test($rules, $rdev);
1703                 }
1704                 if (defined($rules->{exp_target})) {
1705                         symlink_test($rules);
1706                 }
1707                 print "add:         ok\n";
1708         } else {
1709                 print "add:         error";
1710                 if ($rules->{exp_add_error}) {
1711                         print " as expected\n";
1712                 } else {
1713                         print "\n";
1714                         system("tree $udev_root");
1715                         print "\n";
1716                         $error++;
1717                 }
1718         }
1719
1720         if (defined($rules->{option}) && $rules->{option} eq "keep") {
1721                 print "\n\n";
1722                 return;
1723         }
1724
1725         udev("remove", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1726         if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1727             (-l "$PWD/$udev_root$rules->{exp_name}")) {
1728                 print "remove:      error";
1729                 if ($rules->{exp_rem_error}) {
1730                         print " as expected\n";
1731                 } else {
1732                         print "\n";
1733                         system("tree $udev_root");
1734                         print "\n";
1735                         $error++;
1736                 }
1737         } else {
1738                 print "remove:      ok\n";
1739         }
1740
1741         print "\n";
1742
1743         if (defined($rules->{option}) && $rules->{option} eq "clean") {
1744                 system("rm -rf $udev_root");
1745                 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1746         }
1747
1748 }
1749
1750 # only run if we have root permissions
1751 # due to mknod restrictions
1752 if (!($<==0)) {
1753         print "Must have root permissions to run properly.\n";
1754         exit;
1755 }
1756
1757 # prepare
1758 system("rm -rf $udev_root");
1759 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1760
1761 # create config file
1762 open CONF, ">$udev_conf" || die "unable to create config file: $udev_conf";
1763 print CONF "udev_root=\"$udev_root\"\n";
1764 print CONF "udev_rules=\"$udev_rules\"\n";
1765 print CONF "udev_log=\"info\"\n";
1766 close CONF;
1767
1768 my $test_num = 1;
1769
1770 if ($ARGV[0]) {
1771         # only run one test
1772         $test_num = $ARGV[0];
1773
1774         if (defined($tests[$test_num-1]->{desc})) {
1775                 print "udev-test will run test number $test_num only:\n\n";
1776                 run_test($tests[$test_num-1], $test_num);
1777         } else {
1778                 print "test does not exist.\n";
1779         }
1780 } else {
1781         # test all
1782         print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1783
1784         foreach my $rules (@tests) {
1785                 run_test($rules, $test_num);
1786                 $test_num++;
1787         }
1788 }
1789
1790 print "$error errors occured\n\n";
1791
1792 # cleanup
1793 system("rm -rf $udev_root");
1794 unlink($udev_rules);
1795 unlink($udev_conf);
1796