]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/udev-test.pl
Merge pull request 3821 from davide125/fix-tests
[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 creation (same directory)",
745 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
746 exp_name => "modem0",
747 rules => <<EOF
748 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK="modem%n"
749 EOF
750 },
751 {
752 desc => "multiple symlinks",
753 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
754 exp_name => "second-0" ,
755 rules => <<EOF
756 KERNEL=="ttyACM0", SYMLINK="first-%n second-%n third-%n"
757 EOF
758 },
759 {
760 desc => "symlink name '.'",
761 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
762 exp_name => ".",
763 exp_add_error => "yes",
764 exp_rem_error => "yes",
765 rules => <<EOF
766 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="."
767 EOF
768 },
769 {
770 desc => "symlink node to itself",
771 devpath => "/devices/virtual/tty/tty0",
772 exp_name => "link",
773 exp_add_error => "yes",
774 exp_rem_error => "yes",
775 option => "clean",
776 rules => <<EOF
777 KERNEL=="tty0", SYMLINK+="tty0"
778 EOF
779 },
780 {
781 desc => "symlink %n substitution",
782 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
783 exp_name => "symlink0",
784 rules => <<EOF
785 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink%n"
786 EOF
787 },
788 {
789 desc => "symlink %k substitution",
790 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
791 exp_name => "symlink-ttyACM0",
792 rules => <<EOF
793 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink-%k"
794 EOF
795 },
796 {
797 desc => "symlink %M:%m substitution",
798 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
799 exp_name => "major-166:0",
800 rules => <<EOF
801 KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="major-%M:%m"
802 EOF
803 },
804 {
805 desc => "symlink %b substitution",
806 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
807 exp_name => "symlink-0:0:0:0",
808 rules => <<EOF
809 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-%b"
810 EOF
811 },
812 {
813 desc => "symlink %c substitution",
814 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
815 exp_name => "test",
816 rules => <<EOF
817 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo test", SYMLINK+="%c"
818 EOF
819 },
820 {
821 desc => "symlink %c{N} substitution",
822 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
823 exp_name => "test",
824 rules => <<EOF
825 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2}"
826 EOF
827 },
828 {
829 desc => "symlink %c{N+} substitution",
830 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
831 exp_name => "this",
832 rules => <<EOF
833 KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2+}"
834 EOF
835 },
836 {
837 desc => "symlink only rule with %c{N+}",
838 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
839 exp_name => "test",
840 rules => <<EOF
841 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
842 EOF
843 },
844 {
845 desc => "symlink %s{filename} substitution",
846 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
847 exp_name => "166:0",
848 rules => <<EOF
849 KERNEL=="ttyACM[0-9]*", SYMLINK+="%s{dev}"
850 EOF
851 },
852 {
853 desc => "program result substitution (numbered part of)",
854 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
855 exp_name => "link1",
856 rules => <<EOF
857 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
858 EOF
859 },
860 {
861 desc => "program result substitution (numbered part of+)",
862 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
863 exp_name => "link4",
864 rules => <<EOF
865 SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
866 EOF
867 },
868 {
869 desc => "SUBSYSTEM match test",
870 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
871 exp_name => "node",
872 rules => <<EOF
873 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", SUBSYSTEM=="vc"
874 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", SUBSYSTEM=="block"
875 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match2", SUBSYSTEM=="vc"
876 EOF
877 },
878 {
879 desc => "DRIVERS match test",
880 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
881 exp_name => "node",
882 rules => <<EOF
883 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", DRIVERS=="sd-wrong"
884 SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", DRIVERS=="sd"
885 EOF
886 },
887 {
888 desc => "devnode substitution test",
889 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
890 exp_name => "node",
891 rules => <<EOF
892 SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" SYMLINK+="node"
893 EOF
894 },
895 {
896 desc => "parent node name substitution test",
897 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
898 exp_name => "sda-part-1",
899 rules => <<EOF
900 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="%P-part-1"
901 EOF
902 },
903 {
904 desc => "udev_root substitution",
905 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
906 exp_name => "start-/dev-end",
907 rules => <<EOF
908 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
909 EOF
910 },
911 {
912 desc => "last_rule option",
913 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
914 exp_name => "last",
915 rules => <<EOF
916 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
917 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="very-last"
918 EOF
919 },
920 {
921 desc => "negation KERNEL!=",
922 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
923 exp_name => "match",
924 rules => <<EOF
925 SUBSYSTEMS=="scsi", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
926 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
927 SUBSYSTEMS=="scsi", KERNEL!="xsda1", SYMLINK+="match"
928 EOF
929 },
930 {
931 desc => "negation SUBSYSTEM!=",
932 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
933 exp_name => "not-anything",
934 rules => <<EOF
935 SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
936 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
937 SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", SYMLINK+="not-anything"
938 EOF
939 },
940 {
941 desc => "negation PROGRAM!= exit code",
942 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
943 exp_name => "nonzero-program",
944 rules => <<EOF
945 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
946 KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
947 EOF
948 },
949 {
950 desc => "ENV{} test",
951 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
952 exp_name => "true",
953 rules => <<EOF
954 ENV{ENV_KEY_TEST}="test"
955 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
956 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
957 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
958 EOF
959 },
960 {
961 desc => "ENV{} test",
962 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
963 exp_name => "true",
964 rules => <<EOF
965 ENV{ENV_KEY_TEST}="test"
966 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
967 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
968 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
969 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
970 EOF
971 },
972 {
973 desc => "ENV{} test (assign)",
974 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
975 exp_name => "true",
976 rules => <<EOF
977 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
978 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
979 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
980 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
981 EOF
982 },
983 {
984 desc => "ENV{} test (assign 2 times)",
985 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
986 exp_name => "true",
987 rules => <<EOF
988 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
989 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
990 SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
991 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
992 SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
993 EOF
994 },
995 {
996 desc => "ENV{} test (assign2)",
997 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
998 exp_name => "part",
999 rules => <<EOF
1000 SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1001 SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
1002 ENV{MAINDEVICE}=="true", SYMLINK+="disk"
1003 SUBSYSTEM=="block", SYMLINK+="before"
1004 ENV{PARTITION}=="true", SYMLINK+="part"
1005 EOF
1006 },
1007 {
1008 desc => "untrusted string sanitize",
1009 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1010 exp_name => "sane",
1011 rules => <<EOF
1012 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
1013 EOF
1014 },
1015 {
1016 desc => "untrusted string sanitize (don't replace utf8)",
1017 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1018 exp_name => "uber",
1019 rules => <<EOF
1020 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
1021 EOF
1022 },
1023 {
1024 desc => "untrusted string sanitize (replace invalid utf8)",
1025 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1026 exp_name => "replaced",
1027 rules => <<EOF
1028 SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
1029 EOF
1030 },
1031 {
1032 desc => "read sysfs value from parent device",
1033 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1034 exp_name => "serial-354172020305000",
1035 rules => <<EOF
1036 KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
1037 EOF
1038 },
1039 {
1040 desc => "match against empty key string",
1041 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1042 exp_name => "ok",
1043 rules => <<EOF
1044 KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
1045 KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
1046 KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
1047 KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
1048 EOF
1049 },
1050 {
1051 desc => "check ACTION value",
1052 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1053 exp_name => "ok",
1054 rules => <<EOF
1055 ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
1056 ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
1057 EOF
1058 },
1059 {
1060 desc => "final assignment",
1061 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1062 exp_name => "ok",
1063 exp_perms => "root:tty:0640",
1064 rules => <<EOF
1065 KERNEL=="sda", GROUP:="tty"
1066 KERNEL=="sda", GROUP="not-ok", MODE="0640", SYMLINK+="ok"
1067 EOF
1068 },
1069 {
1070 desc => "final assignment 2",
1071 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1072 exp_name => "ok",
1073 exp_perms => "root:tty:0640",
1074 rules => <<EOF
1075 KERNEL=="sda", GROUP:="tty"
1076 SUBSYSTEM=="block", MODE:="640"
1077 KERNEL=="sda", GROUP="not-ok", MODE="0666", SYMLINK+="ok"
1078 EOF
1079 },
1080 {
1081 desc => "env substitution",
1082 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1083 exp_name => "node-add-me",
1084 rules => <<EOF
1085 KERNEL=="sda", MODE="0666", SYMLINK+="node-\$env{ACTION}-me"
1086 EOF
1087 },
1088 {
1089 desc => "reset list to current value",
1090 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1091 exp_name => "three",
1092 not_exp_name => "two",
1093 rules => <<EOF
1094 KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
1095 KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
1096 KERNEL=="ttyACM[0-9]*", SYMLINK="three"
1097 EOF
1098 },
1099 {
1100 desc => "test empty SYMLINK+ (empty override)",
1101 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1102 exp_name => "right",
1103 not_exp_name => "wrong",
1104 rules => <<EOF
1105 KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
1106 KERNEL=="ttyACM[0-9]*", SYMLINK=""
1107 KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
1108 EOF
1109 },
1110 {
1111 desc => "test multi matches",
1112 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1113 exp_name => "right",
1114 rules => <<EOF
1115 KERNEL=="ttyACM*", SYMLINK+="before"
1116 KERNEL=="ttyACM*|nothing", SYMLINK+="right"
1117 EOF
1118 },
1119 {
1120 desc => "test multi matches 2",
1121 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1122 exp_name => "right",
1123 rules => <<EOF
1124 KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
1125 KERNEL=="ttyACM*", SYMLINK+="before"
1126 KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
1127 EOF
1128 },
1129 {
1130 desc => "test multi matches 3",
1131 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1132 exp_name => "right",
1133 rules => <<EOF
1134 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1135 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1136 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1137 KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
1138 EOF
1139 },
1140 {
1141 desc => "test multi matches 4",
1142 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
1143 exp_name => "right",
1144 rules => <<EOF
1145 KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1146 KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1147 KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1148 KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
1149 KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
1150 EOF
1151 },
1152 {
1153 desc => "IMPORT parent test sequence 1/2 (keep)",
1154 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1155 exp_name => "parent",
1156 option => "keep",
1157 rules => <<EOF
1158 KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1159 KERNEL=="sda", SYMLINK+="parent"
1160 EOF
1161 },
1162 {
1163 desc => "IMPORT parent test sequence 2/2 (keep)",
1164 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1165 exp_name => "parentenv-parent_right",
1166 option => "clean",
1167 rules => <<EOF
1168 KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
1169 EOF
1170 },
1171 {
1172 desc => "GOTO test",
1173 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1174 exp_name => "right",
1175 rules => <<EOF
1176 KERNEL=="sda1", GOTO="TEST"
1177 KERNEL=="sda1", SYMLINK+="wrong"
1178 KERNEL=="sda1", GOTO="BAD"
1179 KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1180 KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1181 KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
1182 LABEL="end"
1183 EOF
1184 },
1185 {
1186 desc => "GOTO label does not exist",
1187 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1188 exp_name => "right",
1189 rules => <<EOF
1190 KERNEL=="sda1", GOTO="does-not-exist"
1191 KERNEL=="sda1", SYMLINK+="right",
1192 LABEL="exists"
1193 EOF
1194 },
1195 {
1196 desc => "SYMLINK+ compare test",
1197 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1198 exp_name => "right",
1199 not_exp_name => "wrong",
1200 rules => <<EOF
1201 KERNEL=="sda1", SYMLINK+="link"
1202 KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1203 KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
1204 EOF
1205 },
1206 {
1207 desc => "invalid key operation",
1208 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1209 exp_name => "yes",
1210 rules => <<EOF
1211 KERNEL="sda1", SYMLINK+="no"
1212 KERNEL=="sda1", SYMLINK+="yes"
1213 EOF
1214 },
1215 {
1216 desc => "operator chars in attribute",
1217 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1218 exp_name => "yes",
1219 rules => <<EOF
1220 KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
1221 EOF
1222 },
1223 {
1224 desc => "overlong comment line",
1225 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
1226 exp_name => "yes",
1227 rules => <<EOF
1228 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1229 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
1230 KERNEL=="sda1", SYMLINK+=="no"
1231 KERNEL=="sda1", SYMLINK+="yes"
1232 EOF
1233 },
1234 {
1235 desc => "magic subsys/kernel lookup",
1236 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1237 exp_name => "00:16:41:e2:8d:ff",
1238 rules => <<EOF
1239 KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
1240 EOF
1241 },
1242 {
1243 desc => "TEST absolute path",
1244 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1245 exp_name => "there",
1246 rules => <<EOF
1247 TEST=="/etc/machine-id", SYMLINK+="there"
1248 TEST!="/etc/machine-id", SYMLINK+="notthere"
1249 EOF
1250 },
1251 {
1252 desc => "TEST subsys/kernel lookup",
1253 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1254 exp_name => "yes",
1255 rules => <<EOF
1256 KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
1257 EOF
1258 },
1259 {
1260 desc => "TEST relative path",
1261 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1262 exp_name => "relative",
1263 rules => <<EOF
1264 KERNEL=="sda", TEST=="size", SYMLINK+="relative"
1265 EOF
1266 },
1267 {
1268 desc => "TEST wildcard substitution (find queue/nr_requests)",
1269 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1270 exp_name => "found-subdir",
1271 rules => <<EOF
1272 KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
1273 EOF
1274 },
1275 {
1276 desc => "TEST MODE=0000",
1277 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1278 exp_name => "sda",
1279 exp_perms => "0:0:0000",
1280 exp_rem_error => "yes",
1281 rules => <<EOF
1282 KERNEL=="sda", MODE="0000"
1283 EOF
1284 },
1285 {
1286 desc => "TEST PROGRAM feeds OWNER, GROUP, MODE",
1287 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1288 exp_name => "sda",
1289 exp_perms => "1:1:0400",
1290 exp_rem_error => "yes",
1291 rules => <<EOF
1292 KERNEL=="sda", MODE="666"
1293 KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1294 EOF
1295 },
1296 {
1297 desc => "TEST PROGRAM feeds MODE with overflow",
1298 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1299 exp_name => "sda",
1300 exp_perms => "0:0:0440",
1301 exp_rem_error => "yes",
1302 rules => <<EOF
1303 KERNEL=="sda", MODE="440"
1304 KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
1305 EOF
1306 },
1307 {
1308 desc => "magic [subsys/sysname] attribute substitution",
1309 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1310 exp_name => "sda-8741C4G-end",
1311 exp_perms => "0:0:0600",
1312 rules => <<EOF
1313 KERNEL=="sda", PROGRAM="/bin/true create-envp"
1314 KERNEL=="sda", ENV{TESTENV}="change-envp"
1315 KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
1316 EOF
1317 },
1318 {
1319 desc => "builtin path_id",
1320 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1321 exp_name => "disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0",
1322 rules => <<EOF
1323 KERNEL=="sda", IMPORT{builtin}="path_id"
1324 KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/\$env{ID_PATH}"
1325 EOF
1326 },
1327 {
1328 desc => "add and match tag",
1329 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1330 exp_name => "found",
1331 not_exp_name => "bad" ,
1332 rules => <<EOF
1333 SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", TAG+="green"
1334 TAGS=="green", SYMLINK+="found"
1335 TAGS=="blue", SYMLINK+="bad"
1336 EOF
1337 },
1338 {
1339 desc => "don't crash with lots of tags",
1340 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1341 exp_name => "found",
1342 rules => $rules_10k_tags . <<EOF
1343 TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="found"
1344 EOF
1345 },
1346 );
1347
1348 sub udev {
1349 my ($action, $devpath, $rules) = @_;
1350
1351 # create temporary rules
1352 system("mkdir", "-p", "$udev_rules_dir");
1353 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
1354 print CONF $$rules;
1355 close CONF;
1356
1357 if ($valgrind > 0) {
1358 return system("$udev_bin_valgrind $action $devpath");
1359 } elsif ($gdb > 0) {
1360 return system("$udev_bin_gdb $action $devpath");
1361 } elsif ($strace > 0) {
1362 return system("$udev_bin_strace $action $devpath");
1363 } else {
1364 return system("$udev_bin", "$action", "$devpath");
1365 }
1366 }
1367
1368 my $error = 0;
1369
1370 sub permissions_test {
1371 my($rules, $uid, $gid, $mode) = @_;
1372
1373 my $wrong = 0;
1374 my $userid;
1375 my $groupid;
1376
1377 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
1378 if ($1 ne "") {
1379 if (defined(getpwnam($1))) {
1380 $userid = int(getpwnam($1));
1381 } else {
1382 $userid = $1;
1383 }
1384 if ($uid != $userid) { $wrong = 1; }
1385 }
1386 if ($2 ne "") {
1387 if (defined(getgrnam($2))) {
1388 $groupid = int(getgrnam($2));
1389 } else {
1390 $groupid = $2;
1391 }
1392 if ($gid != $groupid) { $wrong = 1; }
1393 }
1394 if ($3 ne "") {
1395 if (($mode & 07777) != oct($3)) { $wrong = 1; };
1396 }
1397 if ($wrong == 0) {
1398 print "permissions: ok\n";
1399 } else {
1400 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
1401 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
1402 print "permissions: error\n";
1403 $error++;
1404 sleep(1);
1405 }
1406 }
1407
1408 sub major_minor_test {
1409 my($rules, $rdev) = @_;
1410
1411 my $major = ($rdev >> 8) & 0xfff;
1412 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
1413 my $wrong = 0;
1414
1415 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
1416 if ($1 ne "") {
1417 if ($major != $1) { $wrong = 1; };
1418 }
1419 if ($2 ne "") {
1420 if ($minor != $2) { $wrong = 1; };
1421 }
1422 if ($wrong == 0) {
1423 print "major:minor: ok\n";
1424 } else {
1425 printf " expected major:minor is: %i:%i\n", $1, $2;
1426 printf " created major:minor is : %i:%i\n", $major, $minor;
1427 print "major:minor: error\n";
1428 $error++;
1429 sleep(1);
1430 }
1431 }
1432
1433 sub udev_setup {
1434 system("umount", $udev_tmpfs);
1435 rmdir($udev_tmpfs);
1436 mkdir($udev_tmpfs) || die "unable to create udev_tmpfs: $udev_tmpfs\n";
1437 system("mount", "-o", "rw,mode=755,nosuid,noexec,nodev", "-t", "tmpfs", "tmpfs", $udev_tmpfs) && die "unable to mount tmpfs";
1438
1439 mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
1440 # setting group and mode of udev_dev ensures the tests work
1441 # even if the parent directory has setgid bit enabled.
1442 chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
1443 chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
1444
1445 system("cp", "-r", "test/sys/", $udev_sys) && die "unable to copy test/sys";
1446
1447 system("rm", "-rf", "$udev_run");
1448 }
1449
1450 sub run_test {
1451 my ($rules, $number) = @_;
1452 my $rc;
1453
1454 print "TEST $number: $rules->{desc}\n";
1455 print "device \'$rules->{devpath}\' expecting node/link \'$rules->{exp_name}\'\n";
1456
1457 $rc = udev("add", $rules->{devpath}, \$rules->{rules});
1458 if ($rc != 0) {
1459 print "$udev_bin add failed with code $rc\n";
1460 $error++;
1461 }
1462 if (defined($rules->{not_exp_name})) {
1463 if ((-e "$udev_dev/$rules->{not_exp_name}") ||
1464 (-l "$udev_dev/$rules->{not_exp_name}")) {
1465 print "nonexistent: error \'$rules->{not_exp_name}\' not expected to be there\n";
1466 $error++;
1467 sleep(1);
1468 }
1469 }
1470
1471 if ((-e "$udev_dev/$rules->{exp_name}") ||
1472 (-l "$udev_dev/$rules->{exp_name}")) {
1473
1474 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
1475 $atime, $mtime, $ctime, $blksize, $blocks) = stat("$udev_dev/$rules->{exp_name}");
1476
1477 if (defined($rules->{exp_perms})) {
1478 permissions_test($rules, $uid, $gid, $mode);
1479 }
1480 if (defined($rules->{exp_majorminor})) {
1481 major_minor_test($rules, $rdev);
1482 }
1483 print "add: ok\n";
1484 } else {
1485 print "add: error";
1486 if ($rules->{exp_add_error}) {
1487 print " as expected\n";
1488 } else {
1489 print "\n";
1490 system("tree", "$udev_dev");
1491 print "\n";
1492 $error++;
1493 sleep(1);
1494 }
1495 }
1496
1497 if (defined($rules->{option}) && $rules->{option} eq "keep") {
1498 print "\n\n";
1499 return;
1500 }
1501
1502 $rc = udev("remove", $rules->{devpath}, \$rules->{rules});
1503 if ($rc != 0) {
1504 print "$udev_bin remove failed with code $rc\n";
1505 $error++;
1506 }
1507 if ((-e "$udev_dev/$rules->{exp_name}") ||
1508 (-l "$udev_dev/$rules->{exp_name}")) {
1509 print "remove: error";
1510 if ($rules->{exp_rem_error}) {
1511 print " as expected\n";
1512 } else {
1513 print "\n";
1514 system("tree", "$udev_dev");
1515 print "\n";
1516 $error++;
1517 sleep(1);
1518 }
1519 } else {
1520 print "remove: ok\n";
1521 }
1522
1523 print "\n";
1524
1525 if (defined($rules->{option}) && $rules->{option} eq "clean") {
1526 udev_setup();
1527 }
1528
1529 }
1530
1531 # only run if we have root permissions
1532 # due to mknod restrictions
1533 if (!($<==0)) {
1534 print "Must have root permissions to run properly.\n";
1535 exit($EXIT_TEST_SKIP);
1536 }
1537
1538 # skip the test when running in a chroot
1539 system("systemd-detect-virt", "-r", "-q");
1540 if ($? >> 8 == 0) {
1541 print "Running in a chroot, skipping the test.\n";
1542 exit($EXIT_TEST_SKIP);
1543 }
1544
1545 # skip the test when running in a container
1546 system("systemd-detect-virt", "-c", "-q");
1547 if ($? >> 8 == 0) {
1548 print "Running in a container, skipping the test.\n";
1549 exit($EXIT_TEST_SKIP);
1550 }
1551
1552 udev_setup();
1553
1554 my $test_num = 1;
1555 my @list;
1556
1557 foreach my $arg (@ARGV) {
1558 if ($arg =~ m/--valgrind/) {
1559 $valgrind = 1;
1560 printf("using valgrind\n");
1561 } elsif ($arg =~ m/--gdb/) {
1562 $gdb = 1;
1563 printf("using gdb\n");
1564 } elsif ($arg =~ m/--strace/) {
1565 $strace = 1;
1566 printf("using strace\n");
1567 } else {
1568 push(@list, $arg);
1569 }
1570 }
1571
1572 if ($list[0]) {
1573 foreach my $arg (@list) {
1574 if (defined($tests[$arg-1]->{desc})) {
1575 print "udev-test will run test number $arg:\n\n";
1576 run_test($tests[$arg-1], $arg);
1577 } else {
1578 print "test does not exist.\n";
1579 }
1580 }
1581 } else {
1582 # test all
1583 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
1584
1585 foreach my $rules (@tests) {
1586 run_test($rules, $test_num);
1587 $test_num++;
1588 }
1589 }
1590
1591 print "$error errors occurred\n\n";
1592
1593 # cleanup
1594 system("rm", "-rf", "$udev_run");
1595 system("umount", "$udev_tmpfs");
1596 rmdir($udev_tmpfs);
1597
1598 if ($error > 0) {
1599 exit(1);
1600 }
1601 exit(0);