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