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