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