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