chiark / gitweb /
update TODO
[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            => "sysfs parent hierarchy",
497                 subsys          => "tty",
498                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
499                 exp_name        => "modem" ,
500                 rules           => <<EOF
501 ATTRS{idProduct}=="007b", SYMLINK+="modem"
502 EOF
503         },
504         {
505                 desc            => "name test with ! in the name",
506                 subsys          => "block",
507                 devpath         => "/devices/virtual/block/fake!blockdev0",
508                 exp_name        => "is/a/fake/blockdev0" ,
509                 rules           => <<EOF
510 SUBSYSTEMS=="scsi", SYMLINK+="is/not/a/%k"
511 SUBSYSTEM=="block", SYMLINK+="is/a/%k"
512 KERNEL=="ttyACM0", SYMLINK+="modem"
513 EOF
514         },
515         {
516                 desc            => "name test with ! in the name, but no matching rule",
517                 subsys          => "block",
518                 devpath         => "/devices/virtual/block/fake!blockdev0",
519                 exp_name        => "fake/blockdev0" ,
520                 exp_rem_error   => "yes",
521                 rules           => <<EOF
522 KERNEL=="ttyACM0", SYMLINK+="modem"
523 EOF
524         },
525         {
526                 desc            => "KERNELS rule",
527                 subsys          => "block",
528                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
529                 exp_name        => "scsi-0:0:0:0",
530                 rules           => <<EOF
531 SUBSYSTEMS=="usb", KERNELS=="0:0:0:0", SYMLINK+="not-scsi"
532 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:1", SYMLINK+="no-match"
533 SUBSYSTEMS=="scsi", KERNELS==":0", SYMLINK+="short-id"
534 SUBSYSTEMS=="scsi", KERNELS=="/0:0:0:0", SYMLINK+="no-match"
535 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="scsi-0:0:0:0"
536 EOF
537         },
538         {
539                 desc            => "KERNELS wildcard all",
540                 subsys          => "block",
541                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
542                 exp_name        => "scsi-0:0:0:0",
543                 rules           => <<EOF
544 SUBSYSTEMS=="scsi", KERNELS=="*:1", SYMLINK+="no-match"
545 SUBSYSTEMS=="scsi", KERNELS=="*:0:1", SYMLINK+="no-match"
546 SUBSYSTEMS=="scsi", KERNELS=="*:0:0:1", SYMLINK+="no-match"
547 SUBSYSTEMS=="scsi", KERNEL=="0:0:0:0", SYMLINK+="before"
548 SUBSYSTEMS=="scsi", KERNELS=="*", SYMLINK+="scsi-0:0:0:0"
549 EOF
550         },
551         {
552                 desc            => "KERNELS wildcard partial",
553                 subsys          => "block",
554                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
555                 exp_name        => "scsi-0:0:0:0",
556                 rules           => <<EOF
557 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
558 SUBSYSTEMS=="scsi", KERNELS=="*:0", SYMLINK+="scsi-0:0:0:0"
559 EOF
560         },
561         {
562                 desc            => "KERNELS wildcard partial 2",
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:0:0", SYMLINK+="scsi-0:0:0:0"
569 EOF
570         },
571         {
572                 desc            => "substitute attr with link target value (first match)",
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        => "driver-is-sd",
576                 rules           => <<EOF
577 SUBSYSTEMS=="scsi", SYMLINK+="driver-is-\$attr{driver}"
578 EOF
579         },
580         {
581                 desc            => "substitute attr with link target value (currently selected device)",
582                 subsys          => "block",
583                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
584                 exp_name        => "driver-is-ahci",
585                 rules           => <<EOF
586 SUBSYSTEMS=="pci", SYMLINK+="driver-is-\$attr{driver}"
587 EOF
588         },
589         {
590                 desc            => "ignore ATTRS attribute whitespace",
591                 subsys          => "block",
592                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
593                 exp_name        => "ignored",
594                 rules           => <<EOF
595 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE  SPACE", SYMLINK+="ignored"
596 EOF
597         },
598         {
599                 desc            => "do not ignore ATTRS attribute whitespace",
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        => "matched-with-space",
603                 rules           => <<EOF
604 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE  SPACE ", SYMLINK+="wrong-to-ignore"
605 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE  SPACE   ", SYMLINK+="matched-with-space"
606 EOF
607         },
608         {
609                 desc            => "permissions USER=bad GROUP=name",
610                 subsys          => "tty",
611                 devpath         => "/devices/virtual/tty/tty33",
612                 exp_name        => "tty33",
613                 exp_perms       => "0:0:0600",
614                 rules           => <<EOF
615 KERNEL=="tty33", SYMLINK+="tty33", OWNER="bad", GROUP="name"
616 EOF
617         },
618         {
619                 desc            => "permissions OWNER=5000",
620                 subsys          => "block",
621                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
622                 exp_name        => "node",
623                 exp_perms       => "5000::0600",
624                 rules           => <<EOF
625 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="5000"
626 EOF
627         },
628         {
629                 desc            => "permissions GROUP=100",
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       => ":100:0660",
634                 rules           => <<EOF
635 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="100"
636 EOF
637         },
638         {
639                 desc            => "textual user id",
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       => "nobody::0600",
644                 rules           => <<EOF
645 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="nobody"
646 EOF
647         },
648         {
649                 desc            => "textual group 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       => ":daemon:0660",
654                 rules           => <<EOF
655 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="daemon"
656 EOF
657         },
658         {
659                 desc            => "textual user/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       => "root:mail:0660",
664                 rules           => <<EOF
665 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="root", GROUP="mail"
666 EOF
667         },
668         {
669                 desc            => "permissions MODE=0777",
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       => "::0777",
674                 rules           => <<EOF
675 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", MODE="0777"
676 EOF
677         },
678         {
679                 desc            => "permissions OWNER=5000 GROUP=100 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       => "5000:100:0777",
684                 rules           => <<EOF
685 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="5000", GROUP="100", MODE="0777"
686 EOF
687         },
688         {
689                 desc            => "permissions OWNER to 5000",
690                 subsys          => "tty",
691                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
692                 exp_name        => "ttyACM0",
693                 exp_perms       => "5000::",
694                 rules           => <<EOF
695 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="5000"
696 EOF
697         },
698         {
699                 desc            => "permissions GROUP to 100",
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       => ":100:0660",
704                 rules           => <<EOF
705 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="100"
706 EOF
707         },
708         {
709                 desc            => "permissions MODE to 0060",
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       => "::0060",
714                 rules           => <<EOF
715 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", MODE="0060"
716 EOF
717         },
718         {
719                 desc            => "permissions OWNER, GROUP, MODE",
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       => "5000:100:0777",
724                 rules           => <<EOF
725 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="5000", GROUP="100", MODE="0777"
726 EOF
727         },
728         {
729                 desc            => "permissions only rule",
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]*", OWNER="5000", GROUP="100", MODE="0777"
736 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
737 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
738 EOF
739         },
740         {
741                 desc            => "multiple permissions only rule",
742                 subsys          => "tty",
743                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
744                 exp_name        => "ttyACM0",
745                 exp_perms       => "3000:4000:0777",
746                 rules           => <<EOF
747 SUBSYSTEM=="tty", OWNER="3000"
748 SUBSYSTEM=="tty", GROUP="4000"
749 SUBSYSTEM=="tty", MODE="0777"
750 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
751 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
752 EOF
753         },
754         {
755                 desc            => "permissions only rule with override at SYMLINK+ rule",
756                 subsys          => "tty",
757                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
758                 exp_name        => "ttyACM0",
759                 exp_perms       => "3000:8000:0777",
760                 rules           => <<EOF
761 SUBSYSTEM=="tty", OWNER="3000"
762 SUBSYSTEM=="tty", GROUP="4000"
763 SUBSYSTEM=="tty", MODE="0777"
764 KERNEL=="ttyUSX[0-9]*", OWNER="5001", GROUP="101", MODE="0444"
765 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="8000"
766 EOF
767         },
768         {
769                 desc            => "major/minor number test",
770                 subsys          => "block",
771                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
772                 exp_name        => "node",
773                 exp_majorminor  => "8:0",
774                 rules           => <<EOF
775 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node"
776 EOF
777         },
778         {
779                 desc            => "big major number test",
780                 subsys          => "misc",
781                 devpath         => "/devices/virtual/misc/misc-fake1",
782                 exp_name        => "node",
783                 exp_majorminor  => "4095:1",
784                 rules           => <<EOF
785 KERNEL=="misc-fake1", SYMLINK+="node"
786 EOF
787         },
788         {
789                 desc            => "big major and big minor number test",
790                 subsys          => "misc",
791                 devpath         => "/devices/virtual/misc/misc-fake89999",
792                 exp_name        => "node",
793                 exp_majorminor  => "4095:89999",
794                 rules           => <<EOF
795 KERNEL=="misc-fake89999", SYMLINK+="node"
796 EOF
797         },
798         {
799                 desc            => "multiple symlinks with format char",
800                 subsys          => "tty",
801                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
802                 exp_name        => "symlink2-ttyACM0",
803                 rules           => <<EOF
804 KERNEL=="ttyACM[0-9]*", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
805 EOF
806         },
807         {
808                 desc            => "multiple symlinks with a lot of s p a c e s",
809                 subsys          => "tty",
810                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
811                 exp_name        => "one",
812                 not_exp_name    => " ",
813                 rules           => <<EOF
814 KERNEL=="ttyACM[0-9]*", SYMLINK="  one     two        "
815 EOF
816         },
817         {
818                 desc            => "symlink creation (same directory)",
819                 subsys          => "tty",
820                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
821                 exp_name        => "modem0",
822                 rules           => <<EOF
823 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK="modem%n"
824 EOF
825         },
826         {
827                 desc            => "symlink creation (relative link forward)",
828                 subsys          => "block",
829                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
830                 exp_name        => "1/2/symlink" ,
831                 exp_target      => "a/b/node",
832                 exp_rem_error   => "yes",
833                 rules           => <<EOF
834 SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", NAME="1/2/a/b/node", SYMLINK+="1/2/symlink"
835 EOF
836         },
837         {
838                 desc            => "symlink creation (relative link back and forward)",
839                 subsys          => "block",
840                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
841                 exp_name        => "1/2/c/d/symlink" ,
842                 exp_target      => "../../a/b/node",
843                 exp_rem_error   => "yes",
844                 rules           => <<EOF
845 SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", NAME="1/2/a/b/node", SYMLINK+="1/2/c/d/symlink"
846 EOF
847         },
848         {
849                 desc            => "multiple symlinks",
850                 subsys          => "tty",
851                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
852                 exp_name        => "second-0" ,
853                 rules           => <<EOF
854 KERNEL=="ttyACM0", SYMLINK="first-%n second-%n third-%n"
855 EOF
856         },
857         {
858                 desc            => "symlink name '.'",
859                 subsys          => "block",
860                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
861                 exp_name        => ".",
862                 exp_add_error   => "yes",
863                 exp_rem_error   => "yes",
864                 rules           => <<EOF
865 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="."
866 EOF
867         },
868         {
869                 desc            => "symlink node to itself",
870                 subsys          => "tty",
871                 devpath         => "/devices/virtual/tty/tty0",
872                 exp_name        => "link",
873                 exp_add_error   => "yes",
874                 exp_rem_error   => "yes",
875                 option          => "clean",
876                 rules           => <<EOF
877 KERNEL=="tty0", SYMLINK+="tty0"
878 EOF
879         },
880         {
881                 desc            => "symlink %n substitution",
882                 subsys          => "tty",
883                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
884                 exp_name        => "symlink0",
885                 rules           => <<EOF
886 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink%n"
887 EOF
888         },
889         {
890                 desc            => "symlink %k substitution",
891                 subsys          => "tty",
892                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
893                 exp_name        => "symlink-ttyACM0",
894                 rules           => <<EOF
895 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink-%k"
896 EOF
897         },
898         {
899                 desc            => "symlink %M:%m substitution",
900                 subsys          => "tty",
901                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
902                 exp_name        => "major-166:0",
903                 rules           => <<EOF
904 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="major-%M:%m"
905 EOF
906         },
907         {
908                 desc            => "symlink %b substitution",
909                 subsys          => "block",
910                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
911                 exp_name        => "symlink-0:0:0:0",
912                 exp_target      => "node",
913                 rules           => <<EOF
914 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="node", SYMLINK+="symlink-%b"
915 EOF
916         },
917         {
918                 desc            => "symlink %c substitution",
919                 subsys          => "tty",
920                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
921                 exp_name        => "test",
922                 rules           => <<EOF
923 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo test", SYMLINK+="%c"
924 EOF
925         },
926         {
927                 desc            => "symlink %c{N} substitution",
928                 subsys          => "tty",
929                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
930                 exp_name        => "test",
931                 rules           => <<EOF
932 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2}"
933 EOF
934         },
935         {
936                 desc            => "symlink %c{N+} substitution",
937                 subsys          => "tty",
938                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
939                 exp_name        => "this",
940                 rules           => <<EOF
941 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2+}"
942 EOF
943         },
944         {
945                 desc            => "symlink only rule with %c{N+}",
946                 subsys          => "block",
947                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
948                 exp_name        => "test",
949                 rules           => <<EOF
950 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
951 EOF
952         },
953         {
954                 desc            => "symlink %s{filename} 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        => "166:0",
958                 rules           => <<EOF
959 KERNEL=="ttyACM[0-9]*", SYMLINK+="%s{dev}"
960 EOF
961         },
962         {
963                 desc            => "program result substitution (numbered part of)",
964                 subsys          => "block",
965                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
966                 exp_name        => "link1",
967                 rules           => <<EOF
968 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
969 EOF
970         },
971         {
972                 desc            => "program result substitution (numbered part of+)",
973                 subsys          => "block",
974                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
975                 exp_name        => "link4",
976                 rules           => <<EOF
977 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
978 EOF
979         },
980         {
981                 desc            => "SUBSYSTEM match test",
982                 subsys          => "block",
983                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
984                 exp_name        => "node",
985                 rules           => <<EOF
986 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", SUBSYSTEM=="vc"
987 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", SUBSYSTEM=="block"
988 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match2", SUBSYSTEM=="vc"
989 EOF
990         },
991         {
992                 desc            => "DRIVERS match test",
993                 subsys          => "block",
994                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
995                 exp_name        => "node",
996                 rules           => <<EOF
997 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", DRIVERS=="sd-wrong"
998 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", DRIVERS=="sd"
999 EOF
1000         },
1001         {
1002                 desc            => "temporary node creation test",
1003                 subsys          => "block",
1004                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1005                 exp_name        => "node",
1006                 rules           => <<EOF
1007 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" SYMLINK+="node"
1008 EOF
1009         },
1010         {
1011                 desc            => "parent node name substitution test sequence 1/2 (keep)",
1012                 subsys          => "block",
1013                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1014                 exp_name        => "main_device",
1015                 option          => "keep",
1016                 exp_rem_error   => "yes",
1017                 rules           => <<EOF
1018 SUBSYSTEMS=="scsi", KERNEL=="sda", NAME="main_device"
1019 EOF
1020         },
1021         {
1022                 desc            => "parent node name substitution test sequence 2/2 (clean)",
1023                 subsys          => "block",
1024                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1025                 exp_name        => "main_device-part-1",
1026                 option          => "clean",
1027                 rules           => <<EOF
1028 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="%P-part-1"
1029 EOF
1030         },
1031         {
1032                 desc            => "udev_root substitution",
1033                 subsys          => "block",
1034                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1035                 exp_name        => "start-udev-root-end",
1036                 rules           => <<EOF
1037 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
1038 EOF
1039         },
1040         {
1041                 desc            => "last_rule option",
1042                 subsys          => "block",
1043                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1044                 exp_name        => "last",
1045                 rules           => <<EOF
1046 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
1047 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="very-last"
1048 EOF
1049         },
1050         {
1051                 desc            => "negation KERNEL!=",
1052                 subsys          => "block",
1053                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1054                 exp_name        => "match",
1055                 rules           => <<EOF
1056 SUBSYSTEMS=="scsi", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1057 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1058 SUBSYSTEMS=="scsi", KERNEL!="xsda1", SYMLINK+="match"
1059 EOF
1060         },
1061         {
1062                 desc            => "negation SUBSYSTEM!=",
1063                 subsys          => "block",
1064                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1065                 exp_name        => "not-anything",
1066                 rules           => <<EOF
1067 SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1068 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1069 SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", SYMLINK+="not-anything"
1070 EOF
1071         },
1072         {
1073                 desc            => "negation PROGRAM!= exit code",
1074                 subsys          => "block",
1075                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1076                 exp_name        => "nonzero-program",
1077                 rules           => <<EOF
1078 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1079 KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
1080 EOF
1081         },
1082         {
1083                 desc            => "test for whitespace between the operator",
1084                 subsys          => "block",
1085                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1086                 exp_name        => "true",
1087                 rules           => <<EOF
1088 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1089 KERNEL   ==   "sda1"     ,    SYMLINK+   =    "true"
1090 EOF
1091         },
1092         {
1093                 desc            => "ENV{} test",
1094                 subsys          => "block",
1095                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1096                 exp_name        => "true",
1097                 rules           => <<EOF
1098 ENV{ENV_KEY_TEST}="test"
1099 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1100 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
1101 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
1102 EOF
1103         },
1104         {
1105                 desc            => "ENV{} test",
1106                 subsys          => "block",
1107                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1108                 exp_name        => "true",
1109                 rules           => <<EOF
1110 ENV{ENV_KEY_TEST}="test"
1111 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1112 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
1113 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
1114 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
1115 EOF
1116         },
1117         {
1118                 desc            => "ENV{} test (assign)",
1119                 subsys          => "block",
1120                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1121                 exp_name        => "true",
1122                 rules           => <<EOF
1123 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1124 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
1125 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1126 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
1127 EOF
1128         },
1129         {
1130                 desc            => "ENV{} test (assign 2 times)",
1131                 subsys          => "block",
1132                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1133                 exp_name        => "true",
1134                 rules           => <<EOF
1135 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1136 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
1137 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1138 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
1139 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
1140 EOF
1141         },
1142         {
1143                 desc            => "ENV{} test (assign2)",
1144                 subsys          => "block",
1145                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1146                 exp_name        => "part",
1147                 rules           => <<EOF
1148 SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1149 SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
1150 ENV{MAINDEVICE}=="true", SYMLINK+="disk"
1151 SUBSYSTEM=="block", SYMLINK+="before"
1152 ENV{PARTITION}=="true", SYMLINK+="part"
1153 EOF
1154         },
1155         {
1156                 desc            => "untrusted string sanitize",
1157                 subsys          => "block",
1158                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1159                 exp_name        => "sane",
1160                 rules           => <<EOF
1161 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/sbin/badprogram)", RESULT=="name_ _/sbin/badprogram_", SYMLINK+="sane"
1162 EOF
1163         },
1164         {
1165                 desc            => "untrusted string sanitize (don't replace utf8)",
1166                 subsys          => "block",
1167                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1168                 exp_name        => "uber",
1169                 rules           => <<EOF
1170 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
1171 EOF
1172         },
1173         {
1174                 desc            => "untrusted string sanitize (replace invalid utf8)",
1175                 subsys          => "block",
1176                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1177                 exp_name        => "replaced",
1178                 rules           => <<EOF
1179 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
1180 EOF
1181         },
1182         {
1183                 desc            => "read sysfs value from parent device",
1184                 subsys          => "block",
1185                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1186                 exp_name        => "serial-354172020305000",
1187                 rules           => <<EOF
1188 KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
1189 EOF
1190         },
1191         {
1192                 desc            => "match against empty key string",
1193                 subsys          => "block",
1194                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1195                 exp_name        => "ok",
1196                 rules           => <<EOF
1197 KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
1198 KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
1199 KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
1200 KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
1201 EOF
1202         },
1203         {
1204                 desc            => "check ACTION value",
1205                 subsys          => "block",
1206                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1207                 exp_name        => "ok",
1208                 rules           => <<EOF
1209 ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
1210 ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
1211 EOF
1212         },
1213         {
1214                 desc            => "test RUN key remove",
1215                 subsys          => "block",
1216                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1217                 exp_name        => "testsymlink2",
1218                 exp_target      => "ok2",
1219                 rules           => <<EOF
1220 KERNEL=="sda", NAME="ok2", RUN+="/bin/ln -s ok2 %r/testsymlink2"
1221 KERNEL=="sda", ACTION=="remove", RUN+="/bin/rm -f %r/testsymlink2"
1222 EOF
1223         },
1224         {
1225                 desc            => "final assignment",
1226                 subsys          => "block",
1227                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1228                 exp_name        => "ok",
1229                 exp_perms       => "root:tty:0640",
1230                 rules           => <<EOF
1231 KERNEL=="sda", GROUP:="tty"
1232 KERNEL=="sda", GROUP="not-ok", MODE="0640", SYMLINK+="ok"
1233 EOF
1234         },
1235         {
1236                 desc            => "final assignment 2",
1237                 subsys          => "block",
1238                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1239                 exp_name        => "ok",
1240                 exp_perms       => "root:tty:0640",
1241                 rules           => <<EOF
1242 KERNEL=="sda", GROUP:="tty"
1243 SUBSYSTEM=="block", MODE:="640"
1244 KERNEL=="sda", GROUP="not-ok", MODE="0666", SYMLINK+="ok"
1245 EOF
1246         },
1247         {
1248                 desc            => "env substitution",
1249                 subsys          => "block",
1250                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1251                 exp_name        => "node-add-me",
1252                 rules           => <<EOF
1253 KERNEL=="sda", MODE="0666", SYMLINK+="node-\$env{ACTION}-me"
1254 EOF
1255         },
1256         {
1257                 desc            => "reset list to current value",
1258                 subsys          => "tty",
1259                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1260                 exp_name        => "three",
1261                 not_exp_name    => "two",
1262                 rules           => <<EOF
1263 KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
1264 KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
1265 KERNEL=="ttyACM[0-9]*", SYMLINK="three"
1266 EOF
1267         },
1268         {
1269                 desc            => "test empty SYMLINK+ (empty override)",
1270                 subsys          => "tty",
1271                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1272                 exp_name        => "right",
1273                 not_exp_name    => "wrong",
1274                 rules           => <<EOF
1275 KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
1276 KERNEL=="ttyACM[0-9]*", SYMLINK=""
1277 KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
1278 EOF
1279         },
1280         {
1281                 desc            => "test multi matches",
1282                 subsys          => "tty",
1283                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1284                 exp_name        => "right",
1285                 rules           => <<EOF
1286 KERNEL=="ttyACM*", SYMLINK+="before"
1287 KERNEL=="ttyACM*|nothing", SYMLINK+="right"
1288 EOF
1289         },
1290         {
1291                 desc            => "test multi matches 2",
1292                 subsys          => "tty",
1293                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1294                 exp_name        => "right",
1295                 rules           => <<EOF
1296 KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
1297 KERNEL=="ttyACM*", SYMLINK+="before"
1298 KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
1299 EOF
1300         },
1301         {
1302                 desc            => "test multi matches 3",
1303                 subsys          => "tty",
1304                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1305                 exp_name        => "right",
1306                 rules           => <<EOF
1307 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1308 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1309 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1310 KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
1311 EOF
1312         },
1313         {
1314                 desc            => "test multi matches 4",
1315                 subsys          => "tty",
1316                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1317                 exp_name        => "right",
1318                 rules           => <<EOF
1319 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1320 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1321 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1322 KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
1323 KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
1324 EOF
1325         },
1326         {
1327                 desc            => "IMPORT parent test sequence 1/2 (keep)",
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        => "parent",
1331                 option          => "keep",
1332                 rules           => <<EOF
1333 KERNEL=="sda", IMPORT="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1334 KERNEL=="sda", SYMLINK+="parent"
1335 EOF
1336         },
1337         {
1338                 desc            => "IMPORT parent test sequence 2/2 (keep)",
1339                 subsys          => "block",
1340                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1341                 exp_name        => "parentenv-parent_right",
1342                 option          => "clean",
1343                 rules           => <<EOF
1344 KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
1345 EOF
1346         },
1347         {
1348                 desc            => "GOTO test",
1349                 subsys          => "block",
1350                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1351                 exp_name        => "right",
1352                 rules           => <<EOF
1353 KERNEL=="sda1", GOTO="TEST"
1354 KERNEL=="sda1", SYMLINK+="wrong"
1355 KERNEL=="sda1", GOTO="BAD"
1356 KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1357 KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1358 KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
1359 LABEL="end"
1360 EOF
1361         },
1362         {
1363                 desc            => "GOTO label does not exist",
1364                 subsys          => "block",
1365                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1366                 exp_name        => "right",
1367                 rules           => <<EOF
1368 KERNEL=="sda1", GOTO="does-not-exist"
1369 KERNEL=="sda1", SYMLINK+="right",
1370 LABEL="exists"
1371 EOF
1372         },
1373         {
1374                 desc            => "SYMLINK+ compare test",
1375                 subsys          => "block",
1376                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1377                 exp_name        => "right",
1378                 not_exp_name    => "wrong",
1379                 rules           => <<EOF
1380 KERNEL=="sda1", SYMLINK+="link"
1381 KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1382 KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
1383 EOF
1384         },
1385         {
1386                 desc            => "invalid key operation",
1387                 subsys          => "block",
1388                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1389                 exp_name        => "yes",
1390                 rules           => <<EOF
1391 KERNEL="sda1", SYMLINK+="no"
1392 KERNEL=="sda1", SYMLINK+="yes"
1393 EOF
1394         },
1395         {
1396                 desc            => "operator chars in attribute",
1397                 subsys          => "block",
1398                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1399                 exp_name        => "yes",
1400                 rules           => <<EOF
1401 KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
1402 EOF
1403         },
1404         {
1405                 desc            => "overlong comment line",
1406                 subsys          => "block",
1407                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1408                 exp_name        => "yes",
1409                 rules           => <<EOF
1410 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1411    # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1412 KERNEL=="sda1", SYMLINK+=="no"
1413 KERNEL=="sda1", SYMLINK+="yes"
1414 EOF
1415         },
1416         {
1417                 desc            => "magic subsys/kernel lookup",
1418                 subsys          => "block",
1419                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1420                 exp_name        => "00:16:41:e2:8d:ff",
1421                 rules           => <<EOF
1422 KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
1423 EOF
1424         },
1425         {
1426                 desc            => "TEST absolute path",
1427                 subsys          => "block",
1428                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1429                 exp_name        => "there",
1430                 rules           => <<EOF
1431 TEST=="/etc/hosts", SYMLINK+="there"
1432 TEST!="/etc/hosts", SYMLINK+="notthere"
1433 EOF
1434         },
1435         {
1436                 desc            => "TEST subsys/kernel lookup",
1437                 subsys          => "block",
1438                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1439                 exp_name        => "yes",
1440                 rules           => <<EOF
1441 KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
1442 EOF
1443         },
1444         {
1445                 desc            => "TEST relative path",
1446                 subsys          => "block",
1447                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1448                 exp_name        => "relative",
1449                 rules           => <<EOF
1450 KERNEL=="sda", TEST=="size", SYMLINK+="relative"
1451 EOF
1452         },
1453         {
1454                 desc            => "TEST wildcard substitution (find queue/nr_requests)",
1455                 subsys          => "block",
1456                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1457                 exp_name        => "found-subdir",
1458                 rules           => <<EOF
1459 KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
1460 EOF
1461         },
1462         {
1463                 desc            => "TEST MODE=0000",
1464                 subsys          => "block",
1465                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1466                 exp_name        => "sda",
1467                 exp_perms       => "0:0:0000",
1468                 exp_rem_error   => "yes",
1469                 rules           => <<EOF
1470 KERNEL=="sda", MODE="0000"
1471 EOF
1472         },
1473         {
1474                 desc            => "TEST PROGRAM feeds OWNER, GROUP, MODE",
1475                 subsys          => "block",
1476                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1477                 exp_name        => "sda",
1478                 exp_perms       => "5000:100:0400",
1479                 exp_rem_error   => "yes",
1480                 rules           => <<EOF
1481 KERNEL=="sda", MODE="666"
1482 KERNEL=="sda", PROGRAM=="/bin/echo 5000 100 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1483 EOF
1484         },
1485         {
1486                 desc            => "TEST PROGRAM feeds MODE with overflow",
1487                 subsys          => "block",
1488                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1489                 exp_name        => "sda",
1490                 exp_perms       => "0:0:0440",
1491                 exp_rem_error   => "yes",
1492                 rules           => <<EOF
1493 KERNEL=="sda", MODE="440"
1494 KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1495 EOF
1496         },
1497         {
1498                 desc            => "magic [subsys/sysname] attribute substitution",
1499                 subsys          => "block",
1500                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1501                 exp_name        => "sda-8741C4G-end",
1502                 exp_perms       => "0:0:0600",
1503                 rules           => <<EOF
1504 KERNEL=="sda", PROGRAM="/bin/true create-envp"
1505 KERNEL=="sda", ENV{TESTENV}="change-envp"
1506 KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end", RUN+="socket:@/org/kernel/udev/monitor"
1507 EOF
1508         },
1509 );
1510
1511 # set env
1512 $ENV{SYSFS_PATH} = $sysfs;
1513 $ENV{UDEV_CONFIG_FILE} = $udev_conf;
1514
1515 sub udev {
1516         my ($action, $subsys, $devpath, $rules) = @_;
1517
1518         $ENV{DEVPATH} = $devpath;
1519
1520         # create temporary rules
1521         open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1522         print CONF $$rules;
1523         close CONF;
1524
1525         $ENV{ACTION} = $action;
1526         $ENV{SUBSYSTEM} = $subsys;
1527         if ($valgrind > 0) {
1528                 system("$udev_bin_valgrind");
1529         } else {
1530                 system("$udev_bin");
1531         }
1532 }
1533
1534 my $error = 0;
1535
1536 sub permissions_test {
1537         my($rules, $uid, $gid, $mode) = @_;
1538
1539         my $wrong = 0;
1540         my $userid;
1541         my $groupid;
1542
1543         $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1544         if ($1 ne "") {
1545                 if (defined(getpwnam($1))) {
1546                         $userid = int(getpwnam($1));
1547                 } else {
1548                         $userid = $1;
1549                 }
1550                 if ($uid != $userid) { $wrong = 1; }
1551         }
1552         if ($2 ne "") {
1553                 if (defined(getgrnam($2))) {
1554                         $groupid = int(getgrnam($2));
1555                 } else {
1556                         $groupid = $2;
1557                 }
1558                 if ($gid != $groupid) { $wrong = 1; }
1559         }
1560         if ($3 ne "") {
1561                 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1562         }
1563         if ($wrong == 0) {
1564                 print "permissions: ok\n";
1565         } else {
1566                 printf "  expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1567                 printf "  created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1568                 print "permissions: error\n";
1569                 $error++;
1570                 sleep(1);
1571         }
1572 }
1573
1574 sub major_minor_test {
1575         my($rules, $rdev) = @_;
1576
1577         my $major = ($rdev >> 8) & 0xfff;
1578         my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1579         my $wrong = 0;
1580
1581         $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1582         if ($1 ne "") {
1583                 if ($major != $1) { $wrong = 1; };
1584         }
1585         if ($2 ne "") {
1586                 if ($minor != $2) { $wrong = 1; };
1587         }
1588         if ($wrong == 0) {
1589                 print "major:minor: ok\n";
1590         } else {
1591                 printf "  expected major:minor is: %i:%i\n", $1, $2;
1592                 printf "  created major:minor is : %i:%i\n", $major, $minor;
1593                 print "major:minor: error\n";
1594                 $error++;
1595                 sleep(1);
1596         }
1597 }
1598
1599 sub symlink_test {
1600         my ($rules) = @_;
1601
1602         my $output = `ls -l $PWD/$udev_root$rules->{exp_name}`;
1603
1604         if ($output =~ m/(.*)-> (.*)/) {
1605                 if ($2 eq $rules->{exp_target}) {
1606                         print "symlink:     ok\n";
1607                 } else {
1608                         print "  expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1609                         print "  created symlink from: \'$rules->{exp_name}\' to \'$2\'\n";
1610                         print "symlink: error";
1611                         if ($rules->{exp_add_error}) {
1612                                 print " as expected\n";
1613                         } else {
1614                                 print "\n";
1615                                 $error++;
1616                                 sleep(1);
1617                         }
1618                 }
1619         } else {
1620                 print "  expected symlink from: \'$rules->{exp_name}\' to \'$rules->{exp_target}\'\n";
1621                 print "symlink:     not created";
1622                 if ($rules->{exp_add_error}) {
1623                         print " as expected\n";
1624                 } else {
1625                         print "\n";
1626                         $error++;
1627                         sleep(1);
1628                 }
1629         }
1630 }
1631
1632 sub make_udev_root {
1633         system("rm -rf $udev_root");
1634         mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
1635         # setting group and mode of udev_root ensures the tests work
1636         # even if the parent directory has setgid bit enabled.
1637         chown (0, 0, $udev_root) || die "unable to chown $udev_root\n";
1638         chmod (0755, $udev_root) || die "unable to chmod $udev_root\n";
1639 }
1640
1641 sub run_test {
1642         my ($rules, $number) = @_;
1643
1644         print "TEST $number: $rules->{desc}\n";
1645
1646         if ($rules->{exp_target}) {
1647                 print "device \'$rules->{devpath}\' expecting link '$rules->{exp_name}' to node \'$rules->{exp_target}\'\n";
1648         } else {
1649                 print "device \'$rules->{devpath}\' expecting node/link \'$rules->{exp_name}\'\n";
1650         }
1651
1652
1653         udev("add", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1654         if (defined($rules->{not_exp_name})) {
1655                 if ((-e "$PWD/$udev_root$rules->{not_exp_name}") ||
1656                     (-l "$PWD/$udev_root$rules->{not_exp_name}")) {
1657                         print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1658                         $error++;
1659                         sleep(1);
1660                 }
1661         }
1662
1663         if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1664             (-l "$PWD/$udev_root$rules->{exp_name}")) {
1665
1666                 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1667                     $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$rules->{exp_name}");
1668
1669                 if (defined($rules->{exp_perms})) {
1670                         permissions_test($rules, $uid, $gid, $mode);
1671                 }
1672                 if (defined($rules->{exp_majorminor})) {
1673                         major_minor_test($rules, $rdev);
1674                 }
1675                 if (defined($rules->{exp_target})) {
1676                         symlink_test($rules);
1677                 }
1678                 print "add:         ok\n";
1679         } else {
1680                 print "add:         error";
1681                 if ($rules->{exp_add_error}) {
1682                         print " as expected\n";
1683                 } else {
1684                         print "\n";
1685                         system("tree $udev_root");
1686                         print "\n";
1687                         $error++;
1688                         sleep(1);
1689                 }
1690         }
1691
1692         if (defined($rules->{option}) && $rules->{option} eq "keep") {
1693                 print "\n\n";
1694                 return;
1695         }
1696
1697         udev("remove", $rules->{subsys}, $rules->{devpath}, \$rules->{rules});
1698         if ((-e "$PWD/$udev_root$rules->{exp_name}") ||
1699             (-l "$PWD/$udev_root$rules->{exp_name}")) {
1700                 print "remove:      error";
1701                 if ($rules->{exp_rem_error}) {
1702                         print " as expected\n";
1703                 } else {
1704                         print "\n";
1705                         system("tree $udev_root");
1706                         print "\n";
1707                         $error++;
1708                         sleep(1);
1709                 }
1710         } else {
1711                 print "remove:      ok\n";
1712         }
1713
1714         print "\n";
1715
1716         if (defined($rules->{option}) && $rules->{option} eq "clean") {
1717                 make_udev_root ();
1718         }
1719
1720 }
1721
1722 # only run if we have root permissions
1723 # due to mknod restrictions
1724 if (!($<==0)) {
1725         print "Must have root permissions to run properly.\n";
1726         exit;
1727 }
1728
1729 # prepare
1730 make_udev_root();
1731
1732 # create config file
1733 open CONF, ">$udev_conf" || die "unable to create config file: $udev_conf";
1734 print CONF "udev_root=\"$udev_root\"\n";
1735 print CONF "udev_run=\"$udev_root/.udev\"\n";
1736 print CONF "udev_rules=\"$PWD\"\n";
1737 print CONF "udev_log=\"err\"\n";
1738 close CONF;
1739
1740 my $test_num = 1;
1741 my @list;
1742
1743 foreach my $arg (@ARGV) {
1744         if ($arg =~ m/--valgrind/) {
1745                 $valgrind = 1;
1746                 printf("using valgrind\n");
1747         } else {
1748                 push(@list, $arg);
1749         }
1750 }
1751
1752 if ($list[0]) {
1753         foreach my $arg (@list) {
1754                 if (defined($tests[$arg-1]->{desc})) {
1755                         print "udev-test will run test number $arg:\n\n";
1756                         run_test($tests[$arg-1], $arg);
1757                 } else {
1758                         print "test does not exist.\n";
1759                 }
1760         }
1761 } else {
1762         # test all
1763         print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1764
1765         foreach my $rules (@tests) {
1766                 run_test($rules, $test_num);
1767                 $test_num++;
1768         }
1769 }
1770
1771 print "$error errors occured\n\n";
1772
1773 # cleanup
1774 system("rm -rf $udev_root");
1775 unlink($udev_rules);
1776 unlink($udev_conf);
1777
1778 if ($error > 0) {
1779     exit(1);
1780 }
1781 exit(0);