chiark / gitweb /
nspawn: introduce new --capabilities= flag and make use of it in the nspawn test...
[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.sievers@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-test/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            => "test for whitespace between the operator",
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 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
943 KERNEL   ==   "sda1"     ,    SYMLINK+   =    "true"
944 EOF
945         },
946         {
947                 desc            => "ENV{} test",
948                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
949                 exp_name        => "true",
950                 rules           => <<EOF
951 ENV{ENV_KEY_TEST}="test"
952 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
953 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
954 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
955 EOF
956         },
957         {
958                 desc            => "ENV{} test",
959                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
960                 exp_name        => "true",
961                 rules           => <<EOF
962 ENV{ENV_KEY_TEST}="test"
963 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
964 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
965 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
966 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
967 EOF
968         },
969         {
970                 desc            => "ENV{} test (assign)",
971                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
972                 exp_name        => "true",
973                 rules           => <<EOF
974 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
975 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
976 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
977 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
978 EOF
979         },
980         {
981                 desc            => "ENV{} test (assign 2 times)",
982                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
983                 exp_name        => "true",
984                 rules           => <<EOF
985 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
986 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
987 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
988 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
989 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
990 EOF
991         },
992         {
993                 desc            => "ENV{} test (assign2)",
994                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
995                 exp_name        => "part",
996                 rules           => <<EOF
997 SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
998 SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
999 ENV{MAINDEVICE}=="true", SYMLINK+="disk"
1000 SUBSYSTEM=="block", SYMLINK+="before"
1001 ENV{PARTITION}=="true", SYMLINK+="part"
1002 EOF
1003         },
1004         {
1005                 desc            => "untrusted string sanitize",
1006                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1007                 exp_name        => "sane",
1008                 rules           => <<EOF
1009 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
1010 EOF
1011         },
1012         {
1013                 desc            => "untrusted string sanitize (don't replace utf8)",
1014                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1015                 exp_name        => "uber",
1016                 rules           => <<EOF
1017 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
1018 EOF
1019         },
1020         {
1021                 desc            => "untrusted string sanitize (replace invalid utf8)",
1022                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1023                 exp_name        => "replaced",
1024                 rules           => <<EOF
1025 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
1026 EOF
1027         },
1028         {
1029                 desc            => "read sysfs value from parent device",
1030                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1031                 exp_name        => "serial-354172020305000",
1032                 rules           => <<EOF
1033 KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
1034 EOF
1035         },
1036         {
1037                 desc            => "match against empty key string",
1038                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1039                 exp_name        => "ok",
1040                 rules           => <<EOF
1041 KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
1042 KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
1043 KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
1044 KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
1045 EOF
1046         },
1047         {
1048                 desc            => "check ACTION value",
1049                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1050                 exp_name        => "ok",
1051                 rules           => <<EOF
1052 ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
1053 ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
1054 EOF
1055         },
1056         {
1057                 desc            => "final assignment",
1058                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1059                 exp_name        => "ok",
1060                 exp_perms       => "root:tty:0640",
1061                 rules           => <<EOF
1062 KERNEL=="sda", GROUP:="tty"
1063 KERNEL=="sda", GROUP="not-ok", MODE="0640", SYMLINK+="ok"
1064 EOF
1065         },
1066         {
1067                 desc            => "final assignment 2",
1068                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1069                 exp_name        => "ok",
1070                 exp_perms       => "root:tty:0640",
1071                 rules           => <<EOF
1072 KERNEL=="sda", GROUP:="tty"
1073 SUBSYSTEM=="block", MODE:="640"
1074 KERNEL=="sda", GROUP="not-ok", MODE="0666", SYMLINK+="ok"
1075 EOF
1076         },
1077         {
1078                 desc            => "env substitution",
1079                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1080                 exp_name        => "node-add-me",
1081                 rules           => <<EOF
1082 KERNEL=="sda", MODE="0666", SYMLINK+="node-\$env{ACTION}-me"
1083 EOF
1084         },
1085         {
1086                 desc            => "reset list to current value",
1087                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1088                 exp_name        => "three",
1089                 not_exp_name    => "two",
1090                 rules           => <<EOF
1091 KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
1092 KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
1093 KERNEL=="ttyACM[0-9]*", SYMLINK="three"
1094 EOF
1095         },
1096         {
1097                 desc            => "test empty SYMLINK+ (empty override)",
1098                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1099                 exp_name        => "right",
1100                 not_exp_name    => "wrong",
1101                 rules           => <<EOF
1102 KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
1103 KERNEL=="ttyACM[0-9]*", SYMLINK=""
1104 KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
1105 EOF
1106         },
1107         {
1108                 desc            => "test multi matches",
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=="ttyACM*", SYMLINK+="before"
1113 KERNEL=="ttyACM*|nothing", SYMLINK+="right"
1114 EOF
1115         },
1116         {
1117                 desc            => "test multi matches 2",
1118                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1119                 exp_name        => "right",
1120                 rules           => <<EOF
1121 KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
1122 KERNEL=="ttyACM*", SYMLINK+="before"
1123 KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
1124 EOF
1125         },
1126         {
1127                 desc            => "test multi matches 3",
1128                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1129                 exp_name        => "right",
1130                 rules           => <<EOF
1131 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1132 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1133 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1134 KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
1135 EOF
1136         },
1137         {
1138                 desc            => "test multi matches 4",
1139                 devpath         => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1140                 exp_name        => "right",
1141                 rules           => <<EOF
1142 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1143 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1144 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1145 KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
1146 KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
1147 EOF
1148         },
1149         {
1150                 desc            => "IMPORT parent test sequence 1/2 (keep)",
1151                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1152                 exp_name        => "parent",
1153                 option          => "keep",
1154                 rules           => <<EOF
1155 KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1156 KERNEL=="sda", SYMLINK+="parent"
1157 EOF
1158         },
1159         {
1160                 desc            => "IMPORT parent test sequence 2/2 (keep)",
1161                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1162                 exp_name        => "parentenv-parent_right",
1163                 option          => "clean",
1164                 rules           => <<EOF
1165 KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
1166 EOF
1167         },
1168         {
1169                 desc            => "GOTO test",
1170                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1171                 exp_name        => "right",
1172                 rules           => <<EOF
1173 KERNEL=="sda1", GOTO="TEST"
1174 KERNEL=="sda1", SYMLINK+="wrong"
1175 KERNEL=="sda1", GOTO="BAD"
1176 KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1177 KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1178 KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
1179 LABEL="end"
1180 EOF
1181         },
1182         {
1183                 desc            => "GOTO label does not exist",
1184                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1185                 exp_name        => "right",
1186                 rules           => <<EOF
1187 KERNEL=="sda1", GOTO="does-not-exist"
1188 KERNEL=="sda1", SYMLINK+="right",
1189 LABEL="exists"
1190 EOF
1191         },
1192         {
1193                 desc            => "SYMLINK+ compare test",
1194                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1195                 exp_name        => "right",
1196                 not_exp_name    => "wrong",
1197                 rules           => <<EOF
1198 KERNEL=="sda1", SYMLINK+="link"
1199 KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1200 KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
1201 EOF
1202         },
1203         {
1204                 desc            => "invalid key operation",
1205                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1206                 exp_name        => "yes",
1207                 rules           => <<EOF
1208 KERNEL="sda1", SYMLINK+="no"
1209 KERNEL=="sda1", SYMLINK+="yes"
1210 EOF
1211         },
1212         {
1213                 desc            => "operator chars in attribute",
1214                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1215                 exp_name        => "yes",
1216                 rules           => <<EOF
1217 KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
1218 EOF
1219         },
1220         {
1221                 desc            => "overlong comment line",
1222                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1223                 exp_name        => "yes",
1224                 rules           => <<EOF
1225 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1226    # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1227 KERNEL=="sda1", SYMLINK+=="no"
1228 KERNEL=="sda1", SYMLINK+="yes"
1229 EOF
1230         },
1231         {
1232                 desc            => "magic subsys/kernel lookup",
1233                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1234                 exp_name        => "00:16:41:e2:8d:ff",
1235                 rules           => <<EOF
1236 KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
1237 EOF
1238         },
1239         {
1240                 desc            => "TEST absolute path",
1241                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1242                 exp_name        => "there",
1243                 rules           => <<EOF
1244 TEST=="/etc/hosts", SYMLINK+="there"
1245 TEST!="/etc/hosts", SYMLINK+="notthere"
1246 EOF
1247         },
1248         {
1249                 desc            => "TEST subsys/kernel lookup",
1250                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1251                 exp_name        => "yes",
1252                 rules           => <<EOF
1253 KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
1254 EOF
1255         },
1256         {
1257                 desc            => "TEST relative path",
1258                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1259                 exp_name        => "relative",
1260                 rules           => <<EOF
1261 KERNEL=="sda", TEST=="size", SYMLINK+="relative"
1262 EOF
1263         },
1264         {
1265                 desc            => "TEST wildcard substitution (find queue/nr_requests)",
1266                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1267                 exp_name        => "found-subdir",
1268                 rules           => <<EOF
1269 KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
1270 EOF
1271         },
1272         {
1273                 desc            => "TEST MODE=0000",
1274                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1275                 exp_name        => "sda",
1276                 exp_perms       => "0:0:0000",
1277                 exp_rem_error   => "yes",
1278                 rules           => <<EOF
1279 KERNEL=="sda", MODE="0000"
1280 EOF
1281         },
1282         {
1283                 desc            => "TEST PROGRAM feeds OWNER, GROUP, MODE",
1284                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1285                 exp_name        => "sda",
1286                 exp_perms       => "5000:100:0400",
1287                 exp_rem_error   => "yes",
1288                 rules           => <<EOF
1289 KERNEL=="sda", MODE="666"
1290 KERNEL=="sda", PROGRAM=="/bin/echo 5000 100 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1291 EOF
1292         },
1293         {
1294                 desc            => "TEST PROGRAM feeds MODE with overflow",
1295                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1296                 exp_name        => "sda",
1297                 exp_perms       => "0:0:0440",
1298                 exp_rem_error   => "yes",
1299                 rules           => <<EOF
1300 KERNEL=="sda", MODE="440"
1301 KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1302 EOF
1303         },
1304         {
1305                 desc            => "magic [subsys/sysname] attribute substitution",
1306                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1307                 exp_name        => "sda-8741C4G-end",
1308                 exp_perms       => "0:0:0600",
1309                 rules           => <<EOF
1310 KERNEL=="sda", PROGRAM="/bin/true create-envp"
1311 KERNEL=="sda", ENV{TESTENV}="change-envp"
1312 KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
1313 EOF
1314         },
1315         {
1316                 desc            => "builtin path_id",
1317                 devpath         => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1318                 exp_name        => "disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0",
1319                 rules           => <<EOF
1320 KERNEL=="sda", IMPORT{builtin}="path_id"
1321 KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/\$env{ID_PATH}"
1322 EOF
1323         },
1324 );
1325
1326 sub udev {
1327         my ($action, $devpath, $rules) = @_;
1328
1329         # create temporary rules
1330         system("mkdir", "-p", "$udev_rules_dir");
1331         open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1332         print CONF $$rules;
1333         close CONF;
1334
1335         if ($valgrind > 0) {
1336                 system("$udev_bin_valgrind $action $devpath");
1337         } else {
1338                 system("$udev_bin", "$action", "$devpath");
1339         }
1340 }
1341
1342 my $error = 0;
1343
1344 sub permissions_test {
1345         my($rules, $uid, $gid, $mode) = @_;
1346
1347         my $wrong = 0;
1348         my $userid;
1349         my $groupid;
1350
1351         $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1352         if ($1 ne "") {
1353                 if (defined(getpwnam($1))) {
1354                         $userid = int(getpwnam($1));
1355                 } else {
1356                         $userid = $1;
1357                 }
1358                 if ($uid != $userid) { $wrong = 1; }
1359         }
1360         if ($2 ne "") {
1361                 if (defined(getgrnam($2))) {
1362                         $groupid = int(getgrnam($2));
1363                 } else {
1364                         $groupid = $2;
1365                 }
1366                 if ($gid != $groupid) { $wrong = 1; }
1367         }
1368         if ($3 ne "") {
1369                 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1370         }
1371         if ($wrong == 0) {
1372                 print "permissions: ok\n";
1373         } else {
1374                 printf "  expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1375                 printf "  created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1376                 print "permissions: error\n";
1377                 $error++;
1378                 sleep(1);
1379         }
1380 }
1381
1382 sub major_minor_test {
1383         my($rules, $rdev) = @_;
1384
1385         my $major = ($rdev >> 8) & 0xfff;
1386         my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1387         my $wrong = 0;
1388
1389         $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1390         if ($1 ne "") {
1391                 if ($major != $1) { $wrong = 1; };
1392         }
1393         if ($2 ne "") {
1394                 if ($minor != $2) { $wrong = 1; };
1395         }
1396         if ($wrong == 0) {
1397                 print "major:minor: ok\n";
1398         } else {
1399                 printf "  expected major:minor is: %i:%i\n", $1, $2;
1400                 printf "  created major:minor is : %i:%i\n", $major, $minor;
1401                 print "major:minor: error\n";
1402                 $error++;
1403                 sleep(1);
1404         }
1405 }
1406
1407 sub udev_setup {
1408         system("rm", "-rf", "$udev_dev");
1409         mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
1410         # setting group and mode of udev_dev ensures the tests work
1411         # even if the parent directory has setgid bit enabled.
1412         chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
1413         chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
1414
1415         system("rm", "-rf", "$udev_run");
1416 }
1417
1418 sub run_test {
1419         my ($rules, $number) = @_;
1420
1421         print "TEST $number: $rules->{desc}\n";
1422         print "device \'$rules->{devpath}\' expecting node/link \'$rules->{exp_name}\'\n";
1423
1424         udev("add", $rules->{devpath}, \$rules->{rules});
1425         if (defined($rules->{not_exp_name})) {
1426                 if ((-e "$udev_dev/$rules->{not_exp_name}") ||
1427                     (-l "$udev_dev/$rules->{not_exp_name}")) {
1428                         print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1429                         $error++;
1430                         sleep(1);
1431                 }
1432         }
1433
1434         if ((-e "$udev_dev/$rules->{exp_name}") ||
1435             (-l "$udev_dev/$rules->{exp_name}")) {
1436
1437                 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1438                     $atime, $mtime, $ctime, $blksize, $blocks) = stat("$udev_dev/$rules->{exp_name}");
1439
1440                 if (defined($rules->{exp_perms})) {
1441                         permissions_test($rules, $uid, $gid, $mode);
1442                 }
1443                 if (defined($rules->{exp_majorminor})) {
1444                         major_minor_test($rules, $rdev);
1445                 }
1446                 print "add:         ok\n";
1447         } else {
1448                 print "add:         error";
1449                 if ($rules->{exp_add_error}) {
1450                         print " as expected\n";
1451                 } else {
1452                         print "\n";
1453                         system("tree", "$udev_dev");
1454                         print "\n";
1455                         $error++;
1456                         sleep(1);
1457                 }
1458         }
1459
1460         if (defined($rules->{option}) && $rules->{option} eq "keep") {
1461                 print "\n\n";
1462                 return;
1463         }
1464
1465         udev("remove", $rules->{devpath}, \$rules->{rules});
1466         if ((-e "$udev_dev/$rules->{exp_name}") ||
1467             (-l "$udev_dev/$rules->{exp_name}")) {
1468                 print "remove:      error";
1469                 if ($rules->{exp_rem_error}) {
1470                         print " as expected\n";
1471                 } else {
1472                         print "\n";
1473                         system("tree", "$udev_dev");
1474                         print "\n";
1475                         $error++;
1476                         sleep(1);
1477                 }
1478         } else {
1479                 print "remove:      ok\n";
1480         }
1481
1482         print "\n";
1483
1484         if (defined($rules->{option}) && $rules->{option} eq "clean") {
1485                 udev_setup();
1486         }
1487
1488 }
1489
1490 # only run if we have root permissions
1491 # due to mknod restrictions
1492 if (!($<==0)) {
1493         print "Must have root permissions to run properly.\n";
1494         exit;
1495 }
1496
1497 udev_setup();
1498
1499 my $test_num = 1;
1500 my @list;
1501
1502 foreach my $arg (@ARGV) {
1503         if ($arg =~ m/--valgrind/) {
1504                 $valgrind = 1;
1505                 printf("using valgrind\n");
1506         } else {
1507                 push(@list, $arg);
1508         }
1509 }
1510
1511 if ($list[0]) {
1512         foreach my $arg (@list) {
1513                 if (defined($tests[$arg-1]->{desc})) {
1514                         print "udev-test will run test number $arg:\n\n";
1515                         run_test($tests[$arg-1], $arg);
1516                 } else {
1517                         print "test does not exist.\n";
1518                 }
1519         }
1520 } else {
1521         # test all
1522         print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1523
1524         foreach my $rules (@tests) {
1525                 run_test($rules, $test_num);
1526                 $test_num++;
1527         }
1528 }
1529
1530 print "$error errors occured\n\n";
1531
1532 # cleanup
1533 system("rm", "-rf", "$udev_dev");
1534 system("rm", "-rf", "$udev_run");
1535
1536 if ($error > 0) {
1537     exit(1);
1538 }
1539 exit(0);