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