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