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