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