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