]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/udev-test.pl
Merge pull request #4832 from rojkov/mdns
[thirdparty/systemd.git] / test / udev-test.pl
1 #!/usr/bin/perl
2
3 # udev test
4 #
5 # Provides automated testing of the udev binary.
6 # The whole test is self contained in this file, except the matching sysfs tree.
7 # Simply extend the @tests array, to add a new test variant.
8 #
9 # Every test is driven by its own temporary config file.
10 # This program prepares the environment, creates the config and calls udev.
11 #
12 # udev parses the rules, looks at the provided sysfs and
13 # first creates and then removes the device node.
14 # After creation and removal the result is checked against the
15 # expected value and the result is printed.
16 #
17 # Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
18 # Copyright (C) 2004 Leann Ogasawara <ogasawara@osdl.org>
19
20 use warnings;
21 use strict;
22
23 my $udev_bin = "./test-udev";
24 my $valgrind = 0;
25 my $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 => "characters before the %c{N} substitution",
337 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
338 exp_name => "my-foo9" ,
339 rules => <<EOF
340 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{7}"
341 EOF
342 },
343 {
344 desc => "substitute the second to last argument",
345 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
346 exp_name => "my-foo8" ,
347 rules => <<EOF
348 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{6}"
349 EOF
350 },
351 {
352 desc => "test substitution by variable name",
353 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
354 exp_name => "Major:8-minor:5-kernelnumber:5-id:0:0:0:0",
355 rules => <<EOF
356 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:\$major-minor:\$minor-kernelnumber:\$number-id:\$id"
357 EOF
358 },
359 {
360 desc => "test substitution by variable name 2",
361 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
362 exp_name => "Major:8-minor:5-kernelnumber:5-id:0:0:0:0",
363 rules => <<EOF
364 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="Major:\$major-minor:%m-kernelnumber:\$number-id:\$id"
365 EOF
366 },
367 {
368 desc => "test substitution by variable name 3",
369 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
370 exp_name => "850:0:0:05" ,
371 rules => <<EOF
372 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="%M%m%b%n"
373 EOF
374 },
375 {
376 desc => "test substitution by variable name 4",
377 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
378 exp_name => "855" ,
379 rules => <<EOF
380 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="\$major\$minor\$number"
381 EOF
382 },
383 {
384 desc => "test substitution by variable name 5",
385 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
386 exp_name => "8550:0:0:0" ,
387 rules => <<EOF
388 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="\$major%m%n\$id"
389 EOF
390 },
391 {
392 desc => "non matching SUBSYSTEMS for device with no parent",
393 devpath => "/devices/virtual/tty/console",
394 exp_name => "TTY",
395 rules => <<EOF
396 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo", RESULT=="foo", SYMLINK+="foo"
397 KERNEL=="console", SYMLINK+="TTY"
398 EOF
399 },
400 {
401 desc => "non matching SUBSYSTEMS",
402 devpath => "/devices/virtual/tty/console",
403 exp_name => "TTY" ,
404 rules => <<EOF
405 SUBSYSTEMS=="foo", ATTRS{dev}=="5:1", SYMLINK+="foo"
406 KERNEL=="console", SYMLINK+="TTY"
407 EOF
408 },
409 {
410 desc => "ATTRS match",
411 devpath => "/devices/virtual/tty/console",
412 exp_name => "foo" ,
413 rules => <<EOF
414 KERNEL=="console", SYMLINK+="TTY"
415 ATTRS{dev}=="5:1", SYMLINK+="foo"
416 EOF
417 },
418 {
419 desc => "ATTR (empty file)",
420 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
421 exp_name => "empty" ,
422 rules => <<EOF
423 KERNEL=="sda", ATTR{test_empty_file}=="?*", SYMLINK+="something"
424 KERNEL=="sda", ATTR{test_empty_file}!="", SYMLINK+="not-empty"
425 KERNEL=="sda", ATTR{test_empty_file}=="", SYMLINK+="empty"
426 KERNEL=="sda", ATTR{test_empty_file}!="?*", SYMLINK+="not-something"
427 EOF
428 },
429 {
430 desc => "ATTR (non-existent file)",
431 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
432 exp_name => "non-existent" ,
433 rules => <<EOF
434 KERNEL=="sda", ATTR{nofile}=="?*", SYMLINK+="something"
435 KERNEL=="sda", ATTR{nofile}!="", SYMLINK+="not-empty"
436 KERNEL=="sda", ATTR{nofile}=="", SYMLINK+="empty"
437 KERNEL=="sda", ATTR{nofile}!="?*", SYMLINK+="not-something"
438 KERNEL=="sda", TEST!="nofile", SYMLINK+="non-existent"
439 KERNEL=="sda", SYMLINK+="wrong"
440 EOF
441 },
442 {
443 desc => "program and bus type match",
444 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
445 exp_name => "scsi-0:0:0:0" ,
446 rules => <<EOF
447 SUBSYSTEMS=="usb", PROGRAM=="/bin/echo -n usb-%b", SYMLINK+="%c"
448 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n scsi-%b", SYMLINK+="%c"
449 SUBSYSTEMS=="foo", PROGRAM=="/bin/echo -n foo-%b", SYMLINK+="%c"
450 EOF
451 },
452 {
453 desc => "sysfs parent hierarchy",
454 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
455 exp_name => "modem" ,
456 rules => <<EOF
457 ATTRS{idProduct}=="007b", SYMLINK+="modem"
458 EOF
459 },
460 {
461 desc => "name test with ! in the name",
462 devpath => "/devices/virtual/block/fake!blockdev0",
463 exp_name => "is/a/fake/blockdev0" ,
464 rules => <<EOF
465 SUBSYSTEMS=="scsi", SYMLINK+="is/not/a/%k"
466 SUBSYSTEM=="block", SYMLINK+="is/a/%k"
467 KERNEL=="ttyACM0", SYMLINK+="modem"
468 EOF
469 },
470 {
471 desc => "name test with ! in the name, but no matching rule",
472 devpath => "/devices/virtual/block/fake!blockdev0",
473 exp_name => "fake/blockdev0" ,
474 exp_rem_error => "yes",
475 rules => <<EOF
476 KERNEL=="ttyACM0", SYMLINK+="modem"
477 EOF
478 },
479 {
480 desc => "KERNELS rule",
481 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
482 exp_name => "scsi-0:0:0:0",
483 rules => <<EOF
484 SUBSYSTEMS=="usb", KERNELS=="0:0:0:0", SYMLINK+="not-scsi"
485 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:1", SYMLINK+="no-match"
486 SUBSYSTEMS=="scsi", KERNELS==":0", SYMLINK+="short-id"
487 SUBSYSTEMS=="scsi", KERNELS=="/0:0:0:0", SYMLINK+="no-match"
488 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="scsi-0:0:0:0"
489 EOF
490 },
491 {
492 desc => "KERNELS wildcard all",
493 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
494 exp_name => "scsi-0:0:0:0",
495 rules => <<EOF
496 SUBSYSTEMS=="scsi", KERNELS=="*:1", SYMLINK+="no-match"
497 SUBSYSTEMS=="scsi", KERNELS=="*:0:1", SYMLINK+="no-match"
498 SUBSYSTEMS=="scsi", KERNELS=="*:0:0:1", SYMLINK+="no-match"
499 SUBSYSTEMS=="scsi", KERNEL=="0:0:0:0", SYMLINK+="before"
500 SUBSYSTEMS=="scsi", KERNELS=="*", SYMLINK+="scsi-0:0:0:0"
501 EOF
502 },
503 {
504 desc => "KERNELS wildcard partial",
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=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
509 SUBSYSTEMS=="scsi", KERNELS=="*:0", SYMLINK+="scsi-0:0:0:0"
510 EOF
511 },
512 {
513 desc => "KERNELS wildcard partial 2",
514 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
515 exp_name => "scsi-0:0:0:0",
516 rules => <<EOF
517 SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
518 SUBSYSTEMS=="scsi", KERNELS=="*:0:0:0", SYMLINK+="scsi-0:0:0:0"
519 EOF
520 },
521 {
522 desc => "substitute attr with link target value (first match)",
523 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
524 exp_name => "driver-is-sd",
525 rules => <<EOF
526 SUBSYSTEMS=="scsi", SYMLINK+="driver-is-\$attr{driver}"
527 EOF
528 },
529 {
530 desc => "substitute attr with link target value (currently selected device)",
531 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
532 exp_name => "driver-is-ahci",
533 rules => <<EOF
534 SUBSYSTEMS=="pci", SYMLINK+="driver-is-\$attr{driver}"
535 EOF
536 },
537 {
538 desc => "ignore ATTRS attribute whitespace",
539 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
540 exp_name => "ignored",
541 rules => <<EOF
542 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE", SYMLINK+="ignored"
543 EOF
544 },
545 {
546 desc => "do not ignore ATTRS attribute whitespace",
547 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
548 exp_name => "matched-with-space",
549 rules => <<EOF
550 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE ", SYMLINK+="wrong-to-ignore"
551 SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE ", SYMLINK+="matched-with-space"
552 EOF
553 },
554 {
555 desc => "permissions USER=bad GROUP=name",
556 devpath => "/devices/virtual/tty/tty33",
557 exp_name => "tty33",
558 exp_perms => "0:0:0600",
559 rules => <<EOF
560 KERNEL=="tty33", OWNER="bad", GROUP="name"
561 EOF
562 },
563 {
564 desc => "permissions OWNER=1",
565 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
566 exp_name => "node",
567 exp_perms => "1::0600",
568 rules => <<EOF
569 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1"
570 EOF
571 },
572 {
573 desc => "permissions GROUP=1",
574 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
575 exp_name => "node",
576 exp_perms => ":1:0660",
577 rules => <<EOF
578 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="1"
579 EOF
580 },
581 {
582 desc => "textual user id",
583 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
584 exp_name => "node",
585 exp_perms => "nobody::0600",
586 rules => <<EOF
587 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="nobody"
588 EOF
589 },
590 {
591 desc => "textual group id",
592 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
593 exp_name => "node",
594 exp_perms => ":daemon:0660",
595 rules => <<EOF
596 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="daemon"
597 EOF
598 },
599 {
600 desc => "textual user/group id",
601 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
602 exp_name => "node",
603 exp_perms => "root:mail:0660",
604 rules => <<EOF
605 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="root", GROUP="mail"
606 EOF
607 },
608 {
609 desc => "permissions MODE=0777",
610 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
611 exp_name => "node",
612 exp_perms => "::0777",
613 rules => <<EOF
614 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", MODE="0777"
615 EOF
616 },
617 {
618 desc => "permissions OWNER=1 GROUP=1 MODE=0777",
619 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
620 exp_name => "node",
621 exp_perms => "1:1:0777",
622 rules => <<EOF
623 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1", GROUP="1", MODE="0777"
624 EOF
625 },
626 {
627 desc => "permissions OWNER to 1",
628 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
629 exp_name => "ttyACM0",
630 exp_perms => "1::",
631 rules => <<EOF
632 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1"
633 EOF
634 },
635 {
636 desc => "permissions GROUP to 1",
637 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
638 exp_name => "ttyACM0",
639 exp_perms => ":1:0660",
640 rules => <<EOF
641 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="1"
642 EOF
643 },
644 {
645 desc => "permissions MODE to 0060",
646 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
647 exp_name => "ttyACM0",
648 exp_perms => "::0060",
649 rules => <<EOF
650 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", MODE="0060"
651 EOF
652 },
653 {
654 desc => "permissions OWNER, GROUP, MODE",
655 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
656 exp_name => "ttyACM0",
657 exp_perms => "1:1:0777",
658 rules => <<EOF
659 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1", GROUP="1", MODE="0777"
660 EOF
661 },
662 {
663 desc => "permissions only rule",
664 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
665 exp_name => "ttyACM0",
666 exp_perms => "1:1:0777",
667 rules => <<EOF
668 KERNEL=="ttyACM[0-9]*", OWNER="1", GROUP="1", MODE="0777"
669 KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
670 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
671 EOF
672 },
673 {
674 desc => "multiple permissions only rule",
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 => "1:1:0777",
678 rules => <<EOF
679 SUBSYSTEM=="tty", OWNER="1"
680 SUBSYSTEM=="tty", GROUP="1"
681 SUBSYSTEM=="tty", MODE="0777"
682 KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
683 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
684 EOF
685 },
686 {
687 desc => "permissions only rule with override at SYMLINK+ 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:2:0777",
691 rules => <<EOF
692 SUBSYSTEM=="tty", OWNER="1"
693 SUBSYSTEM=="tty", GROUP="1"
694 SUBSYSTEM=="tty", MODE="0777"
695 KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
696 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="2"
697 EOF
698 },
699 {
700 desc => "major/minor number test",
701 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
702 exp_name => "node",
703 exp_majorminor => "8:0",
704 rules => <<EOF
705 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node"
706 EOF
707 },
708 {
709 desc => "big major number test",
710 devpath => "/devices/virtual/misc/misc-fake1",
711 exp_name => "node",
712 exp_majorminor => "4095:1",
713 rules => <<EOF
714 KERNEL=="misc-fake1", SYMLINK+="node"
715 EOF
716 },
717 {
718 desc => "big major and big minor number test",
719 devpath => "/devices/virtual/misc/misc-fake89999",
720 exp_name => "node",
721 exp_majorminor => "4095:89999",
722 rules => <<EOF
723 KERNEL=="misc-fake89999", SYMLINK+="node"
724 EOF
725 },
726 {
727 desc => "multiple symlinks with format char",
728 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
729 exp_name => "symlink2-ttyACM0",
730 rules => <<EOF
731 KERNEL=="ttyACM[0-9]*", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
732 EOF
733 },
734 {
735 desc => "multiple symlinks with a lot of s p a c e s",
736 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
737 exp_name => "one",
738 not_exp_name => " ",
739 rules => <<EOF
740 KERNEL=="ttyACM[0-9]*", SYMLINK=" one two "
741 EOF
742 },
743 {
744 desc => "symlink with spaces in substituted variable",
745 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
746 exp_name => "name-one_two_three-end",
747 not_exp_name => " ",
748 rules => <<EOF
749 ENV{WITH_WS}="one two three"
750 SYMLINK="name-\$env{WITH_WS}-end"
751 EOF
752 },
753 {
754 desc => "symlink with leading space in substituted variable",
755 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
756 exp_name => "name-one_two_three-end",
757 not_exp_name => " ",
758 rules => <<EOF
759 ENV{WITH_WS}=" one two three"
760 SYMLINK="name-\$env{WITH_WS}-end"
761 EOF
762 },
763 {
764 desc => "symlink with trailing space in substituted variable",
765 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
766 exp_name => "name-one_two_three-end",
767 not_exp_name => " ",
768 rules => <<EOF
769 ENV{WITH_WS}="one two three "
770 SYMLINK="name-\$env{WITH_WS}-end"
771 EOF
772 },
773 {
774 desc => "symlink with lots of space in substituted variable",
775 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
776 exp_name => "name-one_two_three-end",
777 not_exp_name => " ",
778 rules => <<EOF
779 ENV{WITH_WS}=" one two three "
780 SYMLINK="name-\$env{WITH_WS}-end"
781 EOF
782 },
783 {
784 desc => "symlink with multiple spaces in substituted variable",
785 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
786 exp_name => "name-one_two_three-end",
787 not_exp_name => " ",
788 rules => <<EOF
789 ENV{WITH_WS}=" one two three "
790 SYMLINK="name-\$env{WITH_WS}-end"
791 EOF
792 },
793 {
794 desc => "symlink with space and var with space, part 1",
795 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
796 exp_name => "first",
797 not_exp_name => " ",
798 rules => <<EOF
799 ENV{WITH_WS}=" one two three "
800 SYMLINK=" first name-\$env{WITH_WS}-end another_symlink a b c "
801 EOF
802 },
803 {
804 desc => "symlink with space and var with space, part 2",
805 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
806 exp_name => "name-one_two_three-end",
807 not_exp_name => " ",
808 rules => <<EOF
809 ENV{WITH_WS}=" one two three "
810 SYMLINK=" first name-\$env{WITH_WS}-end another_symlink a b c "
811 EOF
812 },
813 {
814 desc => "symlink with space and var with space, part 3",
815 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
816 exp_name => "another_symlink",
817 not_exp_name => " ",
818 rules => <<EOF
819 ENV{WITH_WS}=" one two three "
820 SYMLINK=" first name-\$env{WITH_WS}-end another_symlink a b c "
821 EOF
822 },
823 {
824 desc => "symlink creation (same directory)",
825 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
826 exp_name => "modem0",
827 rules => <<EOF
828 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK="modem%n"
829 EOF
830 },
831 {
832 desc => "multiple symlinks",
833 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
834 exp_name => "second-0" ,
835 rules => <<EOF
836 KERNEL=="ttyACM0", SYMLINK="first-%n second-%n third-%n"
837 EOF
838 },
839 {
840 desc => "symlink name '.'",
841 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
842 exp_name => ".",
843 exp_add_error => "yes",
844 exp_rem_error => "yes",
845 rules => <<EOF
846 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="."
847 EOF
848 },
849 {
850 desc => "symlink node to itself",
851 devpath => "/devices/virtual/tty/tty0",
852 exp_name => "link",
853 exp_add_error => "yes",
854 exp_rem_error => "yes",
855 option => "clean",
856 rules => <<EOF
857 KERNEL=="tty0", SYMLINK+="tty0"
858 EOF
859 },
860 {
861 desc => "symlink %n substitution",
862 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
863 exp_name => "symlink0",
864 rules => <<EOF
865 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink%n"
866 EOF
867 },
868 {
869 desc => "symlink %k substitution",
870 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
871 exp_name => "symlink-ttyACM0",
872 rules => <<EOF
873 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink-%k"
874 EOF
875 },
876 {
877 desc => "symlink %M:%m substitution",
878 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
879 exp_name => "major-166:0",
880 rules => <<EOF
881 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="major-%M:%m"
882 EOF
883 },
884 {
885 desc => "symlink %b substitution",
886 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
887 exp_name => "symlink-0:0:0:0",
888 rules => <<EOF
889 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-%b"
890 EOF
891 },
892 {
893 desc => "symlink %c substitution",
894 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
895 exp_name => "test",
896 rules => <<EOF
897 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo test", SYMLINK+="%c"
898 EOF
899 },
900 {
901 desc => "symlink %c{N} substitution",
902 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
903 exp_name => "test",
904 rules => <<EOF
905 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2}"
906 EOF
907 },
908 {
909 desc => "symlink %c{N+} substitution",
910 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
911 exp_name => "this",
912 rules => <<EOF
913 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2+}"
914 EOF
915 },
916 {
917 desc => "symlink only rule with %c{N+}",
918 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
919 exp_name => "test",
920 rules => <<EOF
921 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
922 EOF
923 },
924 {
925 desc => "symlink %s{filename} substitution",
926 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
927 exp_name => "166:0",
928 rules => <<EOF
929 KERNEL=="ttyACM[0-9]*", SYMLINK+="%s{dev}"
930 EOF
931 },
932 {
933 desc => "program result substitution (numbered part of)",
934 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
935 exp_name => "link1",
936 rules => <<EOF
937 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
938 EOF
939 },
940 {
941 desc => "program result substitution (numbered part of+)",
942 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
943 exp_name => "link4",
944 rules => <<EOF
945 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
946 EOF
947 },
948 {
949 desc => "SUBSYSTEM match test",
950 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
951 exp_name => "node",
952 rules => <<EOF
953 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", SUBSYSTEM=="vc"
954 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", SUBSYSTEM=="block"
955 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match2", SUBSYSTEM=="vc"
956 EOF
957 },
958 {
959 desc => "DRIVERS match test",
960 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
961 exp_name => "node",
962 rules => <<EOF
963 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", DRIVERS=="sd-wrong"
964 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", DRIVERS=="sd"
965 EOF
966 },
967 {
968 desc => "devnode substitution test",
969 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
970 exp_name => "node",
971 rules => <<EOF
972 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" SYMLINK+="node"
973 EOF
974 },
975 {
976 desc => "parent node name substitution test",
977 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
978 exp_name => "sda-part-1",
979 rules => <<EOF
980 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="%P-part-1"
981 EOF
982 },
983 {
984 desc => "udev_root substitution",
985 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
986 exp_name => "start-/dev-end",
987 rules => <<EOF
988 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
989 EOF
990 },
991 {
992 desc => "last_rule option",
993 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
994 exp_name => "last",
995 rules => <<EOF
996 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
997 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="very-last"
998 EOF
999 },
1000 {
1001 desc => "negation KERNEL!=",
1002 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1003 exp_name => "match",
1004 rules => <<EOF
1005 SUBSYSTEMS=="scsi", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1006 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1007 SUBSYSTEMS=="scsi", KERNEL!="xsda1", SYMLINK+="match"
1008 EOF
1009 },
1010 {
1011 desc => "negation SUBSYSTEM!=",
1012 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1013 exp_name => "not-anything",
1014 rules => <<EOF
1015 SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1016 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1017 SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", SYMLINK+="not-anything"
1018 EOF
1019 },
1020 {
1021 desc => "negation PROGRAM!= exit code",
1022 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1023 exp_name => "nonzero-program",
1024 rules => <<EOF
1025 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1026 KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
1027 EOF
1028 },
1029 {
1030 desc => "ENV{} test",
1031 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1032 exp_name => "true",
1033 rules => <<EOF
1034 ENV{ENV_KEY_TEST}="test"
1035 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1036 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
1037 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
1038 EOF
1039 },
1040 {
1041 desc => "ENV{} test",
1042 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1043 exp_name => "true",
1044 rules => <<EOF
1045 ENV{ENV_KEY_TEST}="test"
1046 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1047 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
1048 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
1049 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
1050 EOF
1051 },
1052 {
1053 desc => "ENV{} test (assign)",
1054 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1055 exp_name => "true",
1056 rules => <<EOF
1057 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1058 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
1059 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1060 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
1061 EOF
1062 },
1063 {
1064 desc => "ENV{} test (assign 2 times)",
1065 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1066 exp_name => "true",
1067 rules => <<EOF
1068 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1069 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
1070 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1071 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
1072 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
1073 EOF
1074 },
1075 {
1076 desc => "ENV{} test (assign2)",
1077 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1078 exp_name => "part",
1079 rules => <<EOF
1080 SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1081 SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
1082 ENV{MAINDEVICE}=="true", SYMLINK+="disk"
1083 SUBSYSTEM=="block", SYMLINK+="before"
1084 ENV{PARTITION}=="true", SYMLINK+="part"
1085 EOF
1086 },
1087 {
1088 desc => "untrusted string sanitize",
1089 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1090 exp_name => "sane",
1091 rules => <<EOF
1092 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
1093 EOF
1094 },
1095 {
1096 desc => "untrusted string sanitize (don't replace utf8)",
1097 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1098 exp_name => "uber",
1099 rules => <<EOF
1100 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
1101 EOF
1102 },
1103 {
1104 desc => "untrusted string sanitize (replace invalid utf8)",
1105 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1106 exp_name => "replaced",
1107 rules => <<EOF
1108 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
1109 EOF
1110 },
1111 {
1112 desc => "read sysfs value from parent device",
1113 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1114 exp_name => "serial-354172020305000",
1115 rules => <<EOF
1116 KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
1117 EOF
1118 },
1119 {
1120 desc => "match against empty key string",
1121 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1122 exp_name => "ok",
1123 rules => <<EOF
1124 KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
1125 KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
1126 KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
1127 KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
1128 EOF
1129 },
1130 {
1131 desc => "check ACTION value",
1132 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1133 exp_name => "ok",
1134 rules => <<EOF
1135 ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
1136 ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
1137 EOF
1138 },
1139 {
1140 desc => "final assignment",
1141 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1142 exp_name => "ok",
1143 exp_perms => "root:tty:0640",
1144 rules => <<EOF
1145 KERNEL=="sda", GROUP:="tty"
1146 KERNEL=="sda", GROUP="not-ok", MODE="0640", SYMLINK+="ok"
1147 EOF
1148 },
1149 {
1150 desc => "final assignment 2",
1151 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1152 exp_name => "ok",
1153 exp_perms => "root:tty:0640",
1154 rules => <<EOF
1155 KERNEL=="sda", GROUP:="tty"
1156 SUBSYSTEM=="block", MODE:="640"
1157 KERNEL=="sda", GROUP="not-ok", MODE="0666", SYMLINK+="ok"
1158 EOF
1159 },
1160 {
1161 desc => "env substitution",
1162 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1163 exp_name => "node-add-me",
1164 rules => <<EOF
1165 KERNEL=="sda", MODE="0666", SYMLINK+="node-\$env{ACTION}-me"
1166 EOF
1167 },
1168 {
1169 desc => "reset list to current value",
1170 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1171 exp_name => "three",
1172 not_exp_name => "two",
1173 rules => <<EOF
1174 KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
1175 KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
1176 KERNEL=="ttyACM[0-9]*", SYMLINK="three"
1177 EOF
1178 },
1179 {
1180 desc => "test empty SYMLINK+ (empty override)",
1181 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1182 exp_name => "right",
1183 not_exp_name => "wrong",
1184 rules => <<EOF
1185 KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
1186 KERNEL=="ttyACM[0-9]*", SYMLINK=""
1187 KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
1188 EOF
1189 },
1190 {
1191 desc => "test multi matches",
1192 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1193 exp_name => "right",
1194 rules => <<EOF
1195 KERNEL=="ttyACM*", SYMLINK+="before"
1196 KERNEL=="ttyACM*|nothing", SYMLINK+="right"
1197 EOF
1198 },
1199 {
1200 desc => "test multi matches 2",
1201 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1202 exp_name => "right",
1203 rules => <<EOF
1204 KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
1205 KERNEL=="ttyACM*", SYMLINK+="before"
1206 KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
1207 EOF
1208 },
1209 {
1210 desc => "test multi matches 3",
1211 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1212 exp_name => "right",
1213 rules => <<EOF
1214 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1215 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1216 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1217 KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
1218 EOF
1219 },
1220 {
1221 desc => "test multi matches 4",
1222 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1223 exp_name => "right",
1224 rules => <<EOF
1225 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1226 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1227 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1228 KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
1229 KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
1230 EOF
1231 },
1232 {
1233 desc => "IMPORT parent test sequence 1/2 (keep)",
1234 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1235 exp_name => "parent",
1236 option => "keep",
1237 rules => <<EOF
1238 KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1239 KERNEL=="sda", SYMLINK+="parent"
1240 EOF
1241 },
1242 {
1243 desc => "IMPORT parent test sequence 2/2 (keep)",
1244 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1245 exp_name => "parentenv-parent_right",
1246 option => "clean",
1247 rules => <<EOF
1248 KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
1249 EOF
1250 },
1251 {
1252 desc => "GOTO test",
1253 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1254 exp_name => "right",
1255 rules => <<EOF
1256 KERNEL=="sda1", GOTO="TEST"
1257 KERNEL=="sda1", SYMLINK+="wrong"
1258 KERNEL=="sda1", GOTO="BAD"
1259 KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1260 KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1261 KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
1262 LABEL="end"
1263 EOF
1264 },
1265 {
1266 desc => "GOTO label does not exist",
1267 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1268 exp_name => "right",
1269 rules => <<EOF
1270 KERNEL=="sda1", GOTO="does-not-exist"
1271 KERNEL=="sda1", SYMLINK+="right",
1272 LABEL="exists"
1273 EOF
1274 },
1275 {
1276 desc => "SYMLINK+ compare 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 not_exp_name => "wrong",
1280 rules => <<EOF
1281 KERNEL=="sda1", SYMLINK+="link"
1282 KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1283 KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
1284 EOF
1285 },
1286 {
1287 desc => "invalid key operation",
1288 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1289 exp_name => "yes",
1290 rules => <<EOF
1291 KERNEL="sda1", SYMLINK+="no"
1292 KERNEL=="sda1", SYMLINK+="yes"
1293 EOF
1294 },
1295 {
1296 desc => "operator chars in attribute",
1297 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1298 exp_name => "yes",
1299 rules => <<EOF
1300 KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
1301 EOF
1302 },
1303 {
1304 desc => "overlong comment line",
1305 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1306 exp_name => "yes",
1307 rules => <<EOF
1308 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1309 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1310 KERNEL=="sda1", SYMLINK+=="no"
1311 KERNEL=="sda1", SYMLINK+="yes"
1312 EOF
1313 },
1314 {
1315 desc => "magic subsys/kernel lookup",
1316 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1317 exp_name => "00:16:41:e2:8d:ff",
1318 rules => <<EOF
1319 KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
1320 EOF
1321 },
1322 {
1323 desc => "TEST absolute path",
1324 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1325 exp_name => "there",
1326 rules => <<EOF
1327 TEST=="/etc/machine-id", SYMLINK+="there"
1328 TEST!="/etc/machine-id", SYMLINK+="notthere"
1329 EOF
1330 },
1331 {
1332 desc => "TEST subsys/kernel lookup",
1333 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1334 exp_name => "yes",
1335 rules => <<EOF
1336 KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
1337 EOF
1338 },
1339 {
1340 desc => "TEST relative path",
1341 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1342 exp_name => "relative",
1343 rules => <<EOF
1344 KERNEL=="sda", TEST=="size", SYMLINK+="relative"
1345 EOF
1346 },
1347 {
1348 desc => "TEST wildcard substitution (find queue/nr_requests)",
1349 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1350 exp_name => "found-subdir",
1351 rules => <<EOF
1352 KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
1353 EOF
1354 },
1355 {
1356 desc => "TEST MODE=0000",
1357 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1358 exp_name => "sda",
1359 exp_perms => "0:0:0000",
1360 exp_rem_error => "yes",
1361 rules => <<EOF
1362 KERNEL=="sda", MODE="0000"
1363 EOF
1364 },
1365 {
1366 desc => "TEST PROGRAM feeds OWNER, GROUP, MODE",
1367 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1368 exp_name => "sda",
1369 exp_perms => "1:1:0400",
1370 exp_rem_error => "yes",
1371 rules => <<EOF
1372 KERNEL=="sda", MODE="666"
1373 KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1374 EOF
1375 },
1376 {
1377 desc => "TEST PROGRAM feeds MODE with overflow",
1378 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1379 exp_name => "sda",
1380 exp_perms => "0:0:0440",
1381 exp_rem_error => "yes",
1382 rules => <<EOF
1383 KERNEL=="sda", MODE="440"
1384 KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1385 EOF
1386 },
1387 {
1388 desc => "magic [subsys/sysname] attribute substitution",
1389 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1390 exp_name => "sda-8741C4G-end",
1391 exp_perms => "0:0:0600",
1392 rules => <<EOF
1393 KERNEL=="sda", PROGRAM="/bin/true create-envp"
1394 KERNEL=="sda", ENV{TESTENV}="change-envp"
1395 KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
1396 EOF
1397 },
1398 {
1399 desc => "builtin path_id",
1400 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1401 exp_name => "disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0",
1402 rules => <<EOF
1403 KERNEL=="sda", IMPORT{builtin}="path_id"
1404 KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/\$env{ID_PATH}"
1405 EOF
1406 },
1407 {
1408 desc => "add and match tag",
1409 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1410 exp_name => "found",
1411 not_exp_name => "bad" ,
1412 rules => <<EOF
1413 SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", TAG+="green"
1414 TAGS=="green", SYMLINK+="found"
1415 TAGS=="blue", SYMLINK+="bad"
1416 EOF
1417 },
1418 {
1419 desc => "don't crash with lots of tags",
1420 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1421 exp_name => "found",
1422 rules => $rules_10k_tags . <<EOF
1423 TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="found"
1424 EOF
1425 },
1426 );
1427
1428 sub udev {
1429 my ($action, $devpath, $rules) = @_;
1430
1431 # create temporary rules
1432 system("mkdir", "-p", "$udev_rules_dir");
1433 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1434 print CONF $$rules;
1435 close CONF;
1436
1437 if ($valgrind > 0) {
1438 return system("$udev_bin_valgrind $action $devpath");
1439 } elsif ($gdb > 0) {
1440 return system("$udev_bin_gdb $action $devpath");
1441 } elsif ($strace > 0) {
1442 return system("$udev_bin_strace $action $devpath");
1443 } else {
1444 return system("$udev_bin", "$action", "$devpath");
1445 }
1446 }
1447
1448 my $error = 0;
1449
1450 sub permissions_test {
1451 my($rules, $uid, $gid, $mode) = @_;
1452
1453 my $wrong = 0;
1454 my $userid;
1455 my $groupid;
1456
1457 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1458 if ($1 ne "") {
1459 if (defined(getpwnam($1))) {
1460 $userid = int(getpwnam($1));
1461 } else {
1462 $userid = $1;
1463 }
1464 if ($uid != $userid) { $wrong = 1; }
1465 }
1466 if ($2 ne "") {
1467 if (defined(getgrnam($2))) {
1468 $groupid = int(getgrnam($2));
1469 } else {
1470 $groupid = $2;
1471 }
1472 if ($gid != $groupid) { $wrong = 1; }
1473 }
1474 if ($3 ne "") {
1475 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1476 }
1477 if ($wrong == 0) {
1478 print "permissions: ok\n";
1479 } else {
1480 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1481 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1482 print "permissions: error\n";
1483 $error++;
1484 sleep(1);
1485 }
1486 }
1487
1488 sub major_minor_test {
1489 my($rules, $rdev) = @_;
1490
1491 my $major = ($rdev >> 8) & 0xfff;
1492 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1493 my $wrong = 0;
1494
1495 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1496 if ($1 ne "") {
1497 if ($major != $1) { $wrong = 1; };
1498 }
1499 if ($2 ne "") {
1500 if ($minor != $2) { $wrong = 1; };
1501 }
1502 if ($wrong == 0) {
1503 print "major:minor: ok\n";
1504 } else {
1505 printf " expected major:minor is: %i:%i\n", $1, $2;
1506 printf " created major:minor is : %i:%i\n", $major, $minor;
1507 print "major:minor: error\n";
1508 $error++;
1509 sleep(1);
1510 }
1511 }
1512
1513 sub udev_setup {
1514 system("umount", $udev_tmpfs);
1515 rmdir($udev_tmpfs);
1516 mkdir($udev_tmpfs) || die "unable to create udev_tmpfs: $udev_tmpfs\n";
1517 system("mount", "-o", "rw,mode=755,nosuid,noexec,nodev", "-t", "tmpfs", "tmpfs", $udev_tmpfs) && die "unable to mount tmpfs";
1518
1519 mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
1520 # setting group and mode of udev_dev ensures the tests work
1521 # even if the parent directory has setgid bit enabled.
1522 chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
1523 chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
1524
1525 system("cp", "-r", "test/sys/", $udev_sys) && die "unable to copy test/sys";
1526
1527 system("rm", "-rf", "$udev_run");
1528 }
1529
1530 sub run_test {
1531 my ($rules, $number) = @_;
1532 my $rc;
1533
1534 print "TEST $number: $rules->{desc}\n";
1535 print "device \'$rules->{devpath}\' expecting node/link \'$rules->{exp_name}\'\n";
1536
1537 $rc = udev("add", $rules->{devpath}, \$rules->{rules});
1538 if ($rc != 0) {
1539 print "$udev_bin add failed with code $rc\n";
1540 $error++;
1541 }
1542 if (defined($rules->{not_exp_name})) {
1543 if ((-e "$udev_dev/$rules->{not_exp_name}") ||
1544 (-l "$udev_dev/$rules->{not_exp_name}")) {
1545 print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1546 $error++;
1547 sleep(1);
1548 }
1549 }
1550
1551 if ((-e "$udev_dev/$rules->{exp_name}") ||
1552 (-l "$udev_dev/$rules->{exp_name}")) {
1553
1554 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1555 $atime, $mtime, $ctime, $blksize, $blocks) = stat("$udev_dev/$rules->{exp_name}");
1556
1557 if (defined($rules->{exp_perms})) {
1558 permissions_test($rules, $uid, $gid, $mode);
1559 }
1560 if (defined($rules->{exp_majorminor})) {
1561 major_minor_test($rules, $rdev);
1562 }
1563 print "add: ok\n";
1564 } else {
1565 print "add: error";
1566 if ($rules->{exp_add_error}) {
1567 print " as expected\n";
1568 } else {
1569 print "\n";
1570 system("tree", "$udev_dev");
1571 print "\n";
1572 $error++;
1573 sleep(1);
1574 }
1575 }
1576
1577 if (defined($rules->{option}) && $rules->{option} eq "keep") {
1578 print "\n\n";
1579 return;
1580 }
1581
1582 $rc = udev("remove", $rules->{devpath}, \$rules->{rules});
1583 if ($rc != 0) {
1584 print "$udev_bin remove failed with code $rc\n";
1585 $error++;
1586 }
1587 if ((-e "$udev_dev/$rules->{exp_name}") ||
1588 (-l "$udev_dev/$rules->{exp_name}")) {
1589 print "remove: error";
1590 if ($rules->{exp_rem_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 } else {
1600 print "remove: ok\n";
1601 }
1602
1603 print "\n";
1604
1605 if (defined($rules->{option}) && $rules->{option} eq "clean") {
1606 udev_setup();
1607 }
1608
1609 }
1610
1611 # only run if we have root permissions
1612 # due to mknod restrictions
1613 if (!($<==0)) {
1614 print "Must have root permissions to run properly.\n";
1615 exit($EXIT_TEST_SKIP);
1616 }
1617
1618 # skip the test when running in a chroot
1619 system("systemd-detect-virt", "-r", "-q");
1620 if ($? >> 8 == 0) {
1621 print "Running in a chroot, skipping the test.\n";
1622 exit($EXIT_TEST_SKIP);
1623 }
1624
1625 # skip the test when running in a container
1626 system("systemd-detect-virt", "-c", "-q");
1627 if ($? >> 8 == 0) {
1628 print "Running in a container, skipping the test.\n";
1629 exit($EXIT_TEST_SKIP);
1630 }
1631
1632 udev_setup();
1633
1634 my $test_num = 1;
1635 my @list;
1636
1637 foreach my $arg (@ARGV) {
1638 if ($arg =~ m/--valgrind/) {
1639 $valgrind = 1;
1640 printf("using valgrind\n");
1641 } elsif ($arg =~ m/--gdb/) {
1642 $gdb = 1;
1643 printf("using gdb\n");
1644 } elsif ($arg =~ m/--strace/) {
1645 $strace = 1;
1646 printf("using strace\n");
1647 } else {
1648 push(@list, $arg);
1649 }
1650 }
1651
1652 if ($list[0]) {
1653 foreach my $arg (@list) {
1654 if (defined($tests[$arg-1]->{desc})) {
1655 print "udev-test will run test number $arg:\n\n";
1656 run_test($tests[$arg-1], $arg);
1657 } else {
1658 print "test does not exist.\n";
1659 }
1660 }
1661 } else {
1662 # test all
1663 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1664
1665 foreach my $rules (@tests) {
1666 run_test($rules, $test_num);
1667 $test_num++;
1668 }
1669 }
1670
1671 print "$error errors occurred\n\n";
1672
1673 # cleanup
1674 system("rm", "-rf", "$udev_run");
1675 system("umount", "$udev_tmpfs");
1676 rmdir($udev_tmpfs);
1677
1678 if ($error > 0) {
1679 exit(1);
1680 }
1681 exit(0);