]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
network: Allow to configure VLan egress qos maps
[thirdparty/systemd.git] / test / udev-test.pl
CommitLineData
2a5fcfae 1#!/usr/bin/env perl
a367f04e 2
438d4c3c 3# udev test
a367f04e
GKH
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#
65005a7f 12# udev parses the rules, looks at the provided sysfs and
a367f04e
GKH
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#
810adae9 17# Copyright © 2004 Leann Ogasawara <ogasawara@osdl.org>
a367f04e
GKH
18
19use warnings;
20use strict;
d4076383
ZJS
21
22BEGIN {
23 my $EXIT_TEST_SKIP = 77;
24
25 unless (eval "use POSIX qw(WIFEXITED WEXITSTATUS);
26 use Cwd qw(getcwd abs_path);
27 use IPC::Semaphore;
28 use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR IPC_CREAT);
29 use Time::HiRes qw(usleep); 1") {
30 warn "Failed to import dependencies, skipping the test: $@";
31 exit($EXIT_TEST_SKIP);
32 }
33}
a367f04e 34
9b80f05f 35my $udev_bin = "./test-udev";
912541b0 36my $valgrind = 0;
333e07b7 37my $gdb = 0;
587751eb 38my $strace = 0;
b3f24900 39my $udev_bin_valgrind = "valgrind --tool=memcheck --leak-check=yes --track-origins=yes --quiet $udev_bin";
333e07b7 40my $udev_bin_gdb = "gdb --args $udev_bin";
587751eb 41my $udev_bin_strace = "strace -efile $udev_bin";
6ada823a 42my $udev_run = "test/run";
2ce8d27b 43my $udev_tmpfs = "test/tmpfs";
21d9e3f3
EV
44my $udev_sys = "${udev_tmpfs}/sys";
45my $udev_dev = "${udev_tmpfs}/dev";
6ada823a
KS
46my $udev_rules_dir = "$udev_run/udev/rules.d";
47my $udev_rules = "$udev_rules_dir/udev-test.rules";
0eb3cc88 48my $EXIT_TEST_SKIP = 77;
a367f04e 49
bf8f7583
MP
50my $rules_10k_tags = "";
51for (my $i = 1; $i <= 10000; ++$i) {
d3fc8bf4 52 $rules_10k_tags .= 'KERNEL=="sda", TAG+="test' . $i . "\"\n";
bf8f7583
MP
53}
54
e37a5d90
YW
55my $rules_10k_tags_continuation = "KERNEL==\"sda\", \\\n";
56for (my $i = 1; $i < 10000; ++$i) {
57 $rules_10k_tags_continuation .= 'TAG+="test' . $i . "\",\\\n";
1e797cf5 58}
e37a5d90 59$rules_10k_tags_continuation .= "TAG+=\"test10000\"\\n";
1e797cf5 60
eb44d715
MW
61# Create a device list with all block devices under /sys
62# (except virtual devices and cd-roms)
63# the optional argument exp_func returns expected and non-expected
64# symlinks for the device.
65sub all_block_devs {
66 my ($exp_func) = @_;
67 my @devices;
68
69 foreach my $bd (glob "$udev_sys/dev/block/*") {
70 my $tgt = readlink($bd);
71 my ($exp, $notexp) = (undef, undef);
72
73 next if ($tgt =~ m!/virtual/! || $tgt =~ m!/sr[0-9]*$!);
74
75 $tgt =~ s!^\.\./\.\.!!;
76 ($exp, $notexp) = $exp_func->($tgt) if defined($exp_func);
77 my $device = {
78 devpath => $tgt,
79 exp_links => $exp,
80 not_exp_links => $notexp,
81 };
82 push(@devices, $device);
83 }
84 return \@devices;
85}
86
87# This generator returns a suitable exp_func for use with
88# all_block_devs().
89sub expect_for_some {
90 my ($pattern, $links, $donot) = @_;
91 my $_expect = sub {
92 my ($name) = @_;
93
94 if ($name =~ /$pattern/) {
95 return ($links, undef);
96 } elsif ($donot) {
97 return (undef, $links);
98 } else {
99 return (undef, undef);
100 }
101 };
102 return $_expect;
103}
104
a367f04e 105my @tests = (
912541b0
KS
106 {
107 desc => "no rules",
255c05b7
MW
108 devices => [
109 {
110 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
111 exp_rem_error => "yes",
112 },
113 {
114 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
255c05b7
MW
115 exp_rem_error => "yes",
116 }],
912541b0 117 rules => <<EOF
5754e74c 118#
bcf44d55 119EOF
912541b0
KS
120 },
121 {
122 desc => "label test of scsi disc",
255c05b7
MW
123 devices => [
124 {
125 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 126 exp_links => ["boot_disk"],
255c05b7 127 }],
912541b0 128 rules => <<EOF
0f52fdee 129SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
5754e74c 130KERNEL=="ttyACM0", SYMLINK+="modem"
c4edd0ad 131EOF
912541b0
KS
132 },
133 {
134 desc => "label test of scsi disc",
255c05b7
MW
135 devices => [
136 {
137 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 138 exp_links => ["boot_disk"],
255c05b7 139 }],
912541b0 140 rules => <<EOF
5754e74c
KS
141SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
142KERNEL=="ttyACM0", SYMLINK+="modem"
c4edd0ad 143EOF
912541b0
KS
144 },
145 {
146 desc => "label test of scsi disc",
255c05b7
MW
147 devices => [
148 {
149 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 150 exp_links => ["boot_disk"],
255c05b7 151 }],
912541b0 152 rules => <<EOF
5754e74c
KS
153SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
154KERNEL=="ttyACM0", SYMLINK+="modem"
a367f04e 155EOF
912541b0
KS
156 },
157 {
158 desc => "label test of scsi partition",
255c05b7
MW
159 devices => [
160 {
161 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 162 exp_links => ["boot_disk1"],
255c05b7 163 }],
912541b0 164 rules => <<EOF
5754e74c 165SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="boot_disk%n"
83be97ba 166EOF
912541b0
KS
167 },
168 {
169 desc => "label test of pattern match",
255c05b7
MW
170 devices => [
171 {
172 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
173 exp_links => ["boot_disk1", "boot_disk1-4", "boot_disk1-5"],
174 not_exp_links => ["boot_disk1-1", "boot_disk1-2", "boot_disk1-3"]
255c05b7 175 }],
912541b0 176 rules => <<EOF
5754e74c
KS
177SUBSYSTEMS=="scsi", ATTRS{vendor}=="?ATA", SYMLINK+="boot_disk%n-1"
178SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA?", SYMLINK+="boot_disk%n-2"
179SUBSYSTEMS=="scsi", ATTRS{vendor}=="A??", SYMLINK+="boot_disk%n"
180SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATAS", SYMLINK+="boot_disk%n-3"
e62acc31
MW
181SUBSYSTEMS=="scsi", ATTRS{vendor}=="AT?", SYMLINK+="boot_disk%n-4"
182SUBSYSTEMS=="scsi", ATTRS{vendor}=="??A", SYMLINK+="boot_disk%n-5"
358c8c20 183EOF
912541b0
KS
184 },
185 {
186 desc => "label test of multiple sysfs files",
255c05b7
MW
187 devices => [
188 {
189 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
190 exp_links => ["boot_disk1"],
191 not_exp_links => ["boot_diskX1"],
255c05b7 192 }],
912541b0 193 rules => <<EOF
5754e74c
KS
194SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS X ", SYMLINK+="boot_diskX%n"
195SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", SYMLINK+="boot_disk%n"
358c8c20 196EOF
912541b0
KS
197 },
198 {
199 desc => "label test of max sysfs files (skip invalid rule)",
255c05b7
MW
200 devices => [
201 {
202 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
203 exp_links => ["boot_disk1", "boot_diskXY1"],
204 not_exp_links => ["boot_diskXX1"],
255c05b7 205 }],
912541b0 206 rules => <<EOF
5754e74c 207SUBSYSTEMS=="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"
e62acc31 208SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", ATTRS{queue_depth}=="1", SYMLINK+="boot_diskXY%n"
5754e74c 209SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", ATTRS{model}=="ST910021AS", ATTRS{scsi_level}=="6", ATTRS{rev}=="4.06", ATTRS{type}=="0", SYMLINK+="boot_disk%n"
0db6d4cc 210EOF
912541b0
KS
211 },
212 {
213 desc => "catch device by *",
255c05b7
MW
214 devices => [
215 {
216 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 217 exp_links => ["modem/0", "catch-all"],
255c05b7 218 }],
912541b0 219 rules => <<EOF
5754e74c 220KERNEL=="ttyACM*", SYMLINK+="modem/%n"
e62acc31 221KERNEL=="*", SYMLINK+="catch-all"
2e317184 222EOF
912541b0 223 },
e62acc31 224 # 10
912541b0
KS
225 {
226 desc => "catch device by * - take 2",
255c05b7
MW
227 devices => [
228 {
229 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
230 exp_links => ["modem/0"],
231 not_exp_links => ["bad"],
255c05b7 232 }],
912541b0 233 rules => <<EOF
5754e74c
KS
234KERNEL=="*ACM1", SYMLINK+="bad"
235KERNEL=="*ACM0", SYMLINK+="modem/%n"
9f1da361 236EOF
912541b0
KS
237 },
238 {
239 desc => "catch device by ?",
255c05b7
MW
240 devices => [
241 {
242 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
243 exp_links => ["modem/0"],
244 not_exp_links => ["modem/0-1", "modem/0-2"],
255c05b7 245 }],
912541b0 246 rules => <<EOF
5754e74c
KS
247KERNEL=="ttyACM??*", SYMLINK+="modem/%n-1"
248KERNEL=="ttyACM??", SYMLINK+="modem/%n-2"
249KERNEL=="ttyACM?", SYMLINK+="modem/%n"
9f1da361 250EOF
912541b0
KS
251 },
252 {
253 desc => "catch device by character class",
255c05b7
MW
254 devices => [
255 {
256 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
257 exp_links => ["modem/0"],
258 not_exp_links => ["modem/0-1", "modem/0-2"],
255c05b7 259 }],
912541b0 260 rules => <<EOF
5754e74c
KS
261KERNEL=="ttyACM[A-Z]*", SYMLINK+="modem/%n-1"
262KERNEL=="ttyACM?[0-9]", SYMLINK+="modem/%n-2"
263KERNEL=="ttyACM[0-9]*", SYMLINK+="modem/%n"
a367f04e 264EOF
912541b0
KS
265 },
266 {
46bc71b2 267 desc => "don't replace kernel name",
255c05b7
MW
268 devices => [
269 {
270 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 271 exp_links => ["modem"],
255c05b7 272 }],
912541b0 273 rules => <<EOF
5754e74c 274KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a 275EOF
912541b0
KS
276 },
277 {
46bc71b2 278 desc => "Handle comment lines in config file (and don't replace kernel name)",
255c05b7
MW
279 devices => [
280 {
281 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 282 exp_links => ["modem"],
255c05b7 283 }],
912541b0 284 rules => <<EOF
281ff00a 285# this is a comment
5754e74c 286KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a
GKH
287
288EOF
912541b0
KS
289 },
290 {
46bc71b2 291 desc => "Handle comment lines in config file with whitespace (and don't replace kernel name)",
255c05b7
MW
292 devices => [
293 {
294 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 295 exp_links => ["modem"],
255c05b7 296 }],
912541b0 297 rules => <<EOF
d914e445 298 # this is a comment with whitespace before the comment
5754e74c 299KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a 300
3db7fa27 301EOF
912541b0
KS
302 },
303 {
46bc71b2 304 desc => "Handle whitespace only lines (and don't replace kernel name)",
255c05b7
MW
305 devices => [
306 {
307 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 308 exp_links => ["whitespace"],
255c05b7 309 }],
912541b0 310 rules => <<EOF
3db7fa27 311
3db7fa27 312
d914e445
KS
313
314 # this is a comment with whitespace before the comment
5754e74c 315KERNEL=="ttyACM0", SYMLINK+="whitespace"
3db7fa27 316
d914e445 317
3db7fa27 318
281ff00a 319EOF
912541b0
KS
320 },
321 {
46bc71b2 322 desc => "Handle empty lines in config file (and don't replace kernel name)",
255c05b7
MW
323 devices => [
324 {
325 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 326 exp_links => ["modem"],
255c05b7 327 }],
912541b0 328 rules => <<EOF
281ff00a 329
5754e74c 330KERNEL=="ttyACM0", SYMLINK+="modem"
281ff00a 331
9f8dfa19 332EOF
912541b0
KS
333 },
334 {
46bc71b2 335 desc => "Handle backslashed multi lines in config file (and don't replace kernel name)",
255c05b7
MW
336 devices => [
337 {
338 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 339 exp_links => ["modem"],
255c05b7 340 }],
912541b0 341 rules => <<EOF
c7fcba1b 342KERNEL=="ttyACM0", \\
5754e74c 343SYMLINK+="modem"
9f8dfa19 344
77313cd0 345EOF
912541b0
KS
346 },
347 {
348 desc => "preserve backslashes, if they are not for a newline",
255c05b7
MW
349 devices => [
350 {
351 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 352 exp_links => ["aaa"],
255c05b7 353 }],
912541b0 354 rules => <<EOF
d914e445 355KERNEL=="ttyACM0", PROGRAM=="/bin/echo -e \\101", RESULT=="A", SYMLINK+="aaa"
9f8dfa19 356EOF
912541b0 357 },
46bc71b2 358 # 20
912541b0 359 {
46bc71b2 360 desc => "Handle stupid backslashed multi lines in config file (and don't replace kernel name)",
255c05b7
MW
361 devices => [
362 {
363 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 364 exp_links => ["modem"],
255c05b7 365 }],
912541b0 366 rules => <<EOF
9f8dfa19
KS
367
368#
369\\
370
d960ad15 371\\
9f8dfa19
KS
372
373#\\
374
c7fcba1b 375KERNEL=="ttyACM0", \\
912541b0 376 SYMLINK+="modem"
9f8dfa19 377
5499d319 378EOF
912541b0
KS
379 },
380 {
381 desc => "subdirectory handling",
255c05b7
MW
382 devices => [
383 {
384 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 385 exp_links => ["sub/direct/ory/modem"],
255c05b7 386 }],
912541b0 387 rules => <<EOF
5754e74c 388KERNEL=="ttyACM0", SYMLINK+="sub/direct/ory/modem"
a367f04e 389EOF
912541b0
KS
390 },
391 {
392 desc => "parent device name match of scsi partition",
255c05b7
MW
393 devices => [
394 {
395 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 396 exp_links => ["first_disk5"],
255c05b7 397 }],
912541b0 398 rules => <<EOF
5754e74c 399SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="first_disk%n"
c4edd0ad 400EOF
912541b0
KS
401 },
402 {
403 desc => "test substitution chars",
255c05b7
MW
404 devices => [
405 {
406 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 407 exp_links => ["Major:8:minor:5:kernelnumber:5:id:0:0:0:0"],
255c05b7 408 }],
912541b0 409 rules => <<EOF
5754e74c 410SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:%M:minor:%m:kernelnumber:%n:id:%b"
319c6700 411EOF
912541b0
KS
412 },
413 {
414 desc => "import of shell-value returned from program",
255c05b7
MW
415 devices => [
416 {
417 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 418 exp_links => ["node12345678"],
255c05b7 419 }],
912541b0 420 rules => <<EOF
d914e445 421SUBSYSTEMS=="scsi", IMPORT{program}="/bin/echo -e \' TEST_KEY=12345678\\n TEST_key2=98765\'", SYMLINK+="node\$env{TEST_KEY}"
5754e74c 422KERNEL=="ttyACM0", SYMLINK+="modem"
a27cd06c 423EOF
912541b0
KS
424 },
425 {
d3f044dd 426 desc => "substitution of sysfs value (%s{file})",
255c05b7
MW
427 devices => [
428 {
429 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
430 exp_links => ["disk-ATA-sda"],
431 not_exp_links => ["modem"],
255c05b7 432 }],
912541b0 433 rules => <<EOF
5754e74c
KS
434SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", SYMLINK+="disk-%s{vendor}-%k"
435KERNEL=="ttyACM0", SYMLINK+="modem"
a367f04e 436EOF
912541b0
KS
437 },
438 {
439 desc => "program result substitution",
255c05b7
MW
440 devices => [
441 {
442 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
443 exp_links => ["special-device-5"],
444 not_exp_links => ["not"],
255c05b7 445 }],
912541b0 446 rules => <<EOF
d914e445
KS
447SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="-special-*", SYMLINK+="not"
448SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n special-device", RESULT=="special-*", SYMLINK+="%c-%n"
bbbe503e 449EOF
912541b0
KS
450 },
451 {
452 desc => "program result substitution (newline removal)",
255c05b7
MW
453 devices => [
454 {
455 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 456 exp_links => ["newline_removed"],
255c05b7 457 }],
912541b0 458 rules => <<EOF
d914e445 459SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo test", RESULT=="test", SYMLINK+="newline_removed"
f3b04a2e 460EOF
912541b0
KS
461 },
462 {
463 desc => "program result substitution",
255c05b7
MW
464 devices => [
465 {
466 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 467 exp_links => ["test-0:0:0:0"],
255c05b7 468 }],
912541b0 469 rules => <<EOF
d914e445 470SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n test-%b", RESULT=="test-0:0*", SYMLINK+="%c"
dde05ccb 471EOF
912541b0
KS
472 },
473 {
474 desc => "program with lots of arguments",
255c05b7
MW
475 devices => [
476 {
477 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
478 exp_links => ["foo9"],
479 not_exp_links => ["foo3", "foo4", "foo5", "foo6", "foo7", "foo8"],
255c05b7 480 }],
912541b0 481 rules => <<EOF
d914e445 482SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="%c{7}"
35b38379 483EOF
912541b0
KS
484 },
485 {
486 desc => "program with subshell",
255c05b7
MW
487 devices => [
488 {
489 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
490 exp_links => ["bar9"],
491 not_exp_links => ["foo3", "foo4", "foo5", "foo6", "foo7", "foo8"],
255c05b7 492 }],
912541b0 493 rules => <<EOF
d914e445 494SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL=="sda5", SYMLINK+="%c{7}"
35b38379 495EOF
912541b0
KS
496 },
497 {
498 desc => "program arguments combined with apostrophes",
255c05b7
MW
499 devices => [
500 {
501 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
502 exp_links => ["foo7"],
503 not_exp_links => ["foo3", "foo4", "foo5", "foo6", "foo8"],
255c05b7 504 }],
912541b0 505 rules => <<EOF
d914e445 506SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'", KERNEL=="sda5", SYMLINK+="%c{5}"
7e760b79
FB
507EOF
508 },
509 {
510 desc => "program arguments combined with escaped double quotes, part 1",
255c05b7
MW
511 devices => [
512 {
513 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
514 exp_links => ["foo2"],
515 not_exp_links => ["foo1"],
255c05b7 516 }],
7e760b79
FB
517 rules => <<EOF
518SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'printf %%s \\\"foo1 foo2\\\" | grep \\\"foo1 foo2\\\"'", KERNEL=="sda5", SYMLINK+="%c{2}"
519EOF
520 },
521 {
522 desc => "program arguments combined with escaped double quotes, part 2",
255c05b7
MW
523 devices => [
524 {
525 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
526 exp_links => ["foo2"],
527 not_exp_links => ["foo1"],
255c05b7 528 }],
7e760b79
FB
529 rules => <<EOF
530SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c \\\"printf %%s 'foo1 foo2' | grep 'foo1 foo2'\\\"", KERNEL=="sda5", SYMLINK+="%c{2}"
531EOF
532 },
533 {
534 desc => "program arguments combined with escaped double quotes, part 3",
255c05b7
MW
535 devices => [
536 {
537 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
538 exp_links => ["foo2"],
539 not_exp_links => ["foo1", "foo3"],
255c05b7 540 }],
7e760b79
FB
541 rules => <<EOF
542SUBSYSTEMS=="scsi", PROGRAM=="/bin/sh -c 'printf \\\"%%s %%s\\\" \\\"foo1 foo2\\\" \\\"foo3\\\"| grep \\\"foo1 foo2\\\"'", KERNEL=="sda5", SYMLINK+="%c{2}"
56c963dc 543EOF
912541b0
KS
544 },
545 {
546 desc => "characters before the %c{N} substitution",
255c05b7
MW
547 devices => [
548 {
549 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 550 exp_links => ["my-foo9"],
255c05b7 551 }],
912541b0 552 rules => <<EOF
d914e445 553SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{7}"
56c963dc 554EOF
912541b0
KS
555 },
556 {
557 desc => "substitute the second to last argument",
255c05b7
MW
558 devices => [
559 {
560 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
561 exp_links => ["my-foo8"],
562 not_exp_links => ["my-foo3", "my-foo4", "my-foo5", "my-foo6", "my-foo7", "my-foo9"],
255c05b7 563 }],
912541b0 564 rules => <<EOF
d914e445 565SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL=="sda5", SYMLINK+="my-%c{6}"
bf5d2964 566EOF
912541b0
KS
567 },
568 {
569 desc => "test substitution by variable name",
255c05b7
MW
570 devices => [
571 {
572 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 573 exp_links => ["Major:8-minor:5-kernelnumber:5-id:0:0:0:0"],
255c05b7 574 }],
912541b0 575 rules => <<EOF
5754e74c 576SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="Major:\$major-minor:\$minor-kernelnumber:\$number-id:\$id"
bf5d2964 577EOF
912541b0
KS
578 },
579 {
580 desc => "test substitution by variable name 2",
255c05b7
MW
581 devices => [
582 {
583 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 584 exp_links => ["Major:8-minor:5-kernelnumber:5-id:0:0:0:0"],
255c05b7 585 }],
912541b0 586 rules => <<EOF
5754e74c 587SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="Major:\$major-minor:%m-kernelnumber:\$number-id:\$id"
bf5d2964 588EOF
912541b0
KS
589 },
590 {
591 desc => "test substitution by variable name 3",
255c05b7
MW
592 devices => [
593 {
594 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 595 exp_links => ["850:0:0:05"],
255c05b7 596 }],
912541b0 597 rules => <<EOF
5754e74c 598SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="%M%m%b%n"
bf5d2964 599EOF
912541b0
KS
600 },
601 {
602 desc => "test substitution by variable name 4",
255c05b7
MW
603 devices => [
604 {
605 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 606 exp_links => ["855"],
255c05b7 607 }],
912541b0 608 rules => <<EOF
5754e74c 609SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="\$major\$minor\$number"
bf5d2964 610EOF
912541b0
KS
611 },
612 {
613 desc => "test substitution by variable name 5",
255c05b7
MW
614 devices => [
615 {
616 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31 617 exp_links => ["8550:0:0:0"],
255c05b7 618 }],
912541b0 619 rules => <<EOF
5754e74c 620SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", DEVPATH=="*/sda/*", SYMLINK+="\$major%m%n\$id"
8ff8bbba 621EOF
912541b0
KS
622 },
623 {
624 desc => "non matching SUBSYSTEMS for device with no parent",
255c05b7
MW
625 devices => [
626 {
627 devpath => "/devices/virtual/tty/console",
e62acc31
MW
628 exp_links => ["TTY"],
629 not_exp_links => ["foo"],
255c05b7 630 }],
912541b0 631 rules => <<EOF
d914e445 632SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n foo", RESULT=="foo", SYMLINK+="foo"
5754e74c 633KERNEL=="console", SYMLINK+="TTY"
1d936fbc 634EOF
912541b0
KS
635 },
636 {
637 desc => "non matching SUBSYSTEMS",
255c05b7
MW
638 devices => [
639 {
640 devpath => "/devices/virtual/tty/console",
e62acc31
MW
641 exp_links => ["TTY"],
642 not_exp_links => ["foo"],
255c05b7 643 }],
912541b0 644 rules => <<EOF
5754e74c
KS
645SUBSYSTEMS=="foo", ATTRS{dev}=="5:1", SYMLINK+="foo"
646KERNEL=="console", SYMLINK+="TTY"
64682333 647EOF
912541b0
KS
648 },
649 {
650 desc => "ATTRS match",
255c05b7
MW
651 devices => [
652 {
653 devpath => "/devices/virtual/tty/console",
e62acc31 654 exp_links => ["foo", "TTY"],
255c05b7 655 }],
912541b0 656 rules => <<EOF
5754e74c
KS
657KERNEL=="console", SYMLINK+="TTY"
658ATTRS{dev}=="5:1", SYMLINK+="foo"
a402404f 659EOF
912541b0
KS
660 },
661 {
662 desc => "ATTR (empty file)",
255c05b7
MW
663 devices => [
664 {
665 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
666 exp_links => ["empty", "not-something"],
667 not_exp_links => ["something", "not-empty"],
255c05b7 668 }],
912541b0 669 rules => <<EOF
5754e74c
KS
670KERNEL=="sda", ATTR{test_empty_file}=="?*", SYMLINK+="something"
671KERNEL=="sda", ATTR{test_empty_file}!="", SYMLINK+="not-empty"
672KERNEL=="sda", ATTR{test_empty_file}=="", SYMLINK+="empty"
673KERNEL=="sda", ATTR{test_empty_file}!="?*", SYMLINK+="not-something"
a402404f 674EOF
912541b0
KS
675 },
676 {
677 desc => "ATTR (non-existent file)",
255c05b7
MW
678 devices => [
679 {
680 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
681 exp_links => ["non-existent", "wrong"],
682 not_exp_links => ["something", "empty", "not-empty",
683 "not-something", "something"],
255c05b7 684 }],
912541b0 685 rules => <<EOF
5754e74c
KS
686KERNEL=="sda", ATTR{nofile}=="?*", SYMLINK+="something"
687KERNEL=="sda", ATTR{nofile}!="", SYMLINK+="not-empty"
688KERNEL=="sda", ATTR{nofile}=="", SYMLINK+="empty"
689KERNEL=="sda", ATTR{nofile}!="?*", SYMLINK+="not-something"
690KERNEL=="sda", TEST!="nofile", SYMLINK+="non-existent"
691KERNEL=="sda", SYMLINK+="wrong"
772558f4 692EOF
912541b0
KS
693 },
694 {
695 desc => "program and bus type match",
255c05b7
MW
696 devices => [
697 {
698 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 699 exp_links => ["scsi-0:0:0:0"],
255c05b7 700 }],
912541b0 701 rules => <<EOF
d914e445
KS
702SUBSYSTEMS=="usb", PROGRAM=="/bin/echo -n usb-%b", SYMLINK+="%c"
703SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n scsi-%b", SYMLINK+="%c"
704SUBSYSTEMS=="foo", PROGRAM=="/bin/echo -n foo-%b", SYMLINK+="%c"
50e5de03 705EOF
912541b0
KS
706 },
707 {
708 desc => "sysfs parent hierarchy",
255c05b7
MW
709 devices => [
710 {
711 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 712 exp_links => ["modem"],
255c05b7 713 }],
912541b0 714 rules => <<EOF
5754e74c 715ATTRS{idProduct}=="007b", SYMLINK+="modem"
f0142622 716EOF
912541b0
KS
717 },
718 {
719 desc => "name test with ! in the name",
255c05b7
MW
720 devices => [
721 {
722 devpath => "/devices/virtual/block/fake!blockdev0",
f0dccf01 723 devnode => "fake/blockdev0",
e62acc31
MW
724 exp_links => ["is/a/fake/blockdev0"],
725 not_exp_links => ["is/not/a/fake/blockdev0", "modem"],
255c05b7 726 }],
912541b0 727 rules => <<EOF
5754e74c
KS
728SUBSYSTEMS=="scsi", SYMLINK+="is/not/a/%k"
729SUBSYSTEM=="block", SYMLINK+="is/a/%k"
730KERNEL=="ttyACM0", SYMLINK+="modem"
b9fc973b 731EOF
912541b0
KS
732 },
733 {
734 desc => "name test with ! in the name, but no matching rule",
255c05b7
MW
735 devices => [
736 {
737 devpath => "/devices/virtual/block/fake!blockdev0",
f0dccf01 738 devnode => "fake/blockdev0",
e62acc31 739 not_exp_links => ["modem"],
255c05b7 740 }],
912541b0 741 rules => <<EOF
5754e74c 742KERNEL=="ttyACM0", SYMLINK+="modem"
93656247 743EOF
912541b0
KS
744 },
745 {
746 desc => "KERNELS rule",
255c05b7
MW
747 devices => [
748 {
749 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
750 exp_links => ["scsi-0:0:0:0"],
751 not_exp_links => ["no-match", "short-id", "not-scsi"],
255c05b7 752 }],
912541b0 753 rules => <<EOF
5754e74c
KS
754SUBSYSTEMS=="usb", KERNELS=="0:0:0:0", SYMLINK+="not-scsi"
755SUBSYSTEMS=="scsi", KERNELS=="0:0:0:1", SYMLINK+="no-match"
756SUBSYSTEMS=="scsi", KERNELS==":0", SYMLINK+="short-id"
757SUBSYSTEMS=="scsi", KERNELS=="/0:0:0:0", SYMLINK+="no-match"
758SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="scsi-0:0:0:0"
93656247 759EOF
912541b0
KS
760 },
761 {
762 desc => "KERNELS wildcard all",
255c05b7
MW
763 devices => [
764 {
765 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
766 exp_links => ["scsi-0:0:0:0"],
767 not_exp_links => ["no-match", "before"],
255c05b7 768 }],
912541b0 769 rules => <<EOF
5754e74c
KS
770SUBSYSTEMS=="scsi", KERNELS=="*:1", SYMLINK+="no-match"
771SUBSYSTEMS=="scsi", KERNELS=="*:0:1", SYMLINK+="no-match"
772SUBSYSTEMS=="scsi", KERNELS=="*:0:0:1", SYMLINK+="no-match"
773SUBSYSTEMS=="scsi", KERNEL=="0:0:0:0", SYMLINK+="before"
774SUBSYSTEMS=="scsi", KERNELS=="*", SYMLINK+="scsi-0:0:0:0"
93656247 775EOF
912541b0
KS
776 },
777 {
778 desc => "KERNELS wildcard partial",
255c05b7
MW
779 devices => [
780 {
781 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 782 exp_links => ["scsi-0:0:0:0", "before"],
255c05b7 783 }],
912541b0 784 rules => <<EOF
5754e74c
KS
785SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
786SUBSYSTEMS=="scsi", KERNELS=="*:0", SYMLINK+="scsi-0:0:0:0"
93656247 787EOF
912541b0
KS
788 },
789 {
790 desc => "KERNELS wildcard partial 2",
255c05b7
MW
791 devices => [
792 {
793 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 794 exp_links => ["scsi-0:0:0:0", "before"],
255c05b7
MW
795 }],
796 rules => <<EOF
5754e74c
KS
797SUBSYSTEMS=="scsi", KERNELS=="0:0:0:0", SYMLINK+="before"
798SUBSYSTEMS=="scsi", KERNELS=="*:0:0:0", SYMLINK+="scsi-0:0:0:0"
eef54479 799EOF
912541b0
KS
800 },
801 {
802 desc => "substitute attr with link target value (first match)",
255c05b7
MW
803 devices => [
804 {
805 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 806 exp_links => ["driver-is-sd"],
255c05b7 807 }],
912541b0 808 rules => <<EOF
5754e74c 809SUBSYSTEMS=="scsi", SYMLINK+="driver-is-\$attr{driver}"
eef54479 810EOF
912541b0
KS
811 },
812 {
813 desc => "substitute attr with link target value (currently selected device)",
255c05b7
MW
814 devices => [
815 {
816 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 817 exp_links => ["driver-is-ahci"],
255c05b7 818 }],
912541b0 819 rules => <<EOF
5754e74c 820SUBSYSTEMS=="pci", SYMLINK+="driver-is-\$attr{driver}"
d5f91372 821EOF
912541b0
KS
822 },
823 {
824 desc => "ignore ATTRS attribute whitespace",
255c05b7
MW
825 devices => [
826 {
827 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 828 exp_links => ["ignored"],
255c05b7 829 }],
912541b0 830 rules => <<EOF
5754e74c 831SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE", SYMLINK+="ignored"
d5f91372 832EOF
912541b0
KS
833 },
834 {
835 desc => "do not ignore ATTRS attribute whitespace",
255c05b7
MW
836 devices => [
837 {
838 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
839 exp_links => ["matched-with-space"],
840 not_exp_links => ["wrong-to-ignore"],
255c05b7 841 }],
912541b0 842 rules => <<EOF
5754e74c
KS
843SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE ", SYMLINK+="wrong-to-ignore"
844SUBSYSTEMS=="scsi", ATTRS{whitespace_test}=="WHITE SPACE ", SYMLINK+="matched-with-space"
0a5417a0 845EOF
912541b0
KS
846 },
847 {
848 desc => "permissions USER=bad GROUP=name",
255c05b7
MW
849 devices => [
850 {
851 devpath => "/devices/virtual/tty/tty33",
255c05b7
MW
852 exp_perms => "0:0:0600",
853 }],
912541b0 854 rules => <<EOF
6ada823a 855KERNEL=="tty33", OWNER="bad", GROUP="name"
b8669191 856EOF
912541b0
KS
857 },
858 {
9158d03e 859 desc => "permissions OWNER=1",
255c05b7
MW
860 devices => [
861 {
862 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 863 exp_links => ["node"],
255c05b7
MW
864 exp_perms => "1::0600",
865 }],
912541b0 866 rules => <<EOF
9158d03e 867SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1"
b8669191 868EOF
912541b0
KS
869 },
870 {
9158d03e 871 desc => "permissions GROUP=1",
255c05b7
MW
872 devices => [
873 {
874 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 875 exp_links => ["node"],
255c05b7
MW
876 exp_perms => ":1:0660",
877 }],
912541b0 878 rules => <<EOF
9158d03e 879SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="1"
9b434de1 880EOF
912541b0
KS
881 },
882 {
883 desc => "textual user id",
255c05b7
MW
884 devices => [
885 {
886 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 887 exp_links => ["node"],
255c05b7
MW
888 exp_perms => "daemon::0600",
889 }],
912541b0 890 rules => <<EOF
24a01950 891SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="daemon"
9b434de1 892EOF
912541b0
KS
893 },
894 {
895 desc => "textual group id",
255c05b7
MW
896 devices => [
897 {
898 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 899 exp_links => ["node"],
255c05b7
MW
900 exp_perms => ":daemon:0660",
901 }],
912541b0 902 rules => <<EOF
5754e74c 903SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", GROUP="daemon"
c8278763 904EOF
912541b0
KS
905 },
906 {
907 desc => "textual user/group id",
255c05b7
MW
908 devices => [
909 {
910 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 911 exp_links => ["node"],
255c05b7
MW
912 exp_perms => "root:audio:0660",
913 }],
912541b0 914 rules => <<EOF
a9030b81 915SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="root", GROUP="audio"
b8669191 916EOF
912541b0
KS
917 },
918 {
919 desc => "permissions MODE=0777",
255c05b7
MW
920 devices => [
921 {
922 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 923 exp_links => ["node"],
255c05b7
MW
924 exp_perms => "::0777",
925 }],
912541b0 926 rules => <<EOF
5754e74c 927SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", MODE="0777"
b8669191 928EOF
912541b0
KS
929 },
930 {
9158d03e 931 desc => "permissions OWNER=1 GROUP=1 MODE=0777",
255c05b7
MW
932 devices => [
933 {
934 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 935 exp_links => ["node"],
255c05b7
MW
936 exp_perms => "1:1:0777",
937 }],
912541b0 938 rules => <<EOF
9158d03e 939SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", OWNER="1", GROUP="1", MODE="0777"
b8669191 940EOF
912541b0
KS
941 },
942 {
9158d03e 943 desc => "permissions OWNER to 1",
255c05b7
MW
944 devices => [
945 {
946 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
947 exp_perms => "1::",
948 }],
912541b0 949 rules => <<EOF
9158d03e 950KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1"
b8669191 951EOF
912541b0
KS
952 },
953 {
9158d03e 954 desc => "permissions GROUP to 1",
255c05b7
MW
955 devices => [
956 {
957 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
958 exp_perms => ":1:0660",
959 }],
912541b0 960 rules => <<EOF
9158d03e 961KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="1"
b8669191 962EOF
912541b0
KS
963 },
964 {
965 desc => "permissions MODE to 0060",
255c05b7
MW
966 devices => [
967 {
968 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
969 exp_perms => "::0060",
970 }],
912541b0 971 rules => <<EOF
5754e74c 972KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", MODE="0060"
b8669191 973EOF
912541b0
KS
974 },
975 {
976 desc => "permissions OWNER, GROUP, MODE",
255c05b7
MW
977 devices => [
978 {
979 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
980 exp_perms => "1:1:0777",
981 }],
912541b0 982 rules => <<EOF
9158d03e 983KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", OWNER="1", GROUP="1", MODE="0777"
e9390146 984EOF
912541b0
KS
985 },
986 {
987 desc => "permissions only rule",
255c05b7
MW
988 devices => [
989 {
990 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
991 exp_perms => "1:1:0777",
992 }],
912541b0 993 rules => <<EOF
9158d03e
TG
994KERNEL=="ttyACM[0-9]*", OWNER="1", GROUP="1", MODE="0777"
995KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
5754e74c 996KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
eb870090 997EOF
912541b0
KS
998 },
999 {
1000 desc => "multiple permissions only rule",
255c05b7
MW
1001 devices => [
1002 {
1003 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
1004 exp_perms => "1:1:0777",
1005 }],
912541b0 1006 rules => <<EOF
9158d03e
TG
1007SUBSYSTEM=="tty", OWNER="1"
1008SUBSYSTEM=="tty", GROUP="1"
28ce66de 1009SUBSYSTEM=="tty", MODE="0777"
9158d03e 1010KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
5754e74c 1011KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n"
eb870090 1012EOF
912541b0
KS
1013 },
1014 {
1015 desc => "permissions only rule with override at SYMLINK+ rule",
255c05b7
MW
1016 devices => [
1017 {
1018 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
255c05b7
MW
1019 exp_perms => "1:2:0777",
1020 }],
912541b0 1021 rules => <<EOF
9158d03e
TG
1022SUBSYSTEM=="tty", OWNER="1"
1023SUBSYSTEM=="tty", GROUP="1"
28ce66de 1024SUBSYSTEM=="tty", MODE="0777"
9158d03e
TG
1025KERNEL=="ttyUSX[0-9]*", OWNER="2", GROUP="2", MODE="0444"
1026KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", GROUP="2"
fa19f181 1027EOF
912541b0
KS
1028 },
1029 {
1030 desc => "major/minor number test",
255c05b7
MW
1031 devices => [
1032 {
1033 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1034 exp_links => ["node"],
255c05b7
MW
1035 exp_majorminor => "8:0",
1036 }],
912541b0 1037 rules => <<EOF
5754e74c 1038SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node"
7d12d4e1 1039EOF
912541b0
KS
1040 },
1041 {
1042 desc => "big major number test",
255c05b7
MW
1043 devices => [
1044 {
1045 devpath => "/devices/virtual/misc/misc-fake1",
e62acc31 1046 exp_links => ["node"],
255c05b7
MW
1047 exp_majorminor => "4095:1",
1048 }],
1049 rules => <<EOF
5754e74c 1050KERNEL=="misc-fake1", SYMLINK+="node"
7d12d4e1 1051EOF
912541b0
KS
1052 },
1053 {
1054 desc => "big major and big minor number test",
255c05b7
MW
1055 devices => [
1056 {
1057 devpath => "/devices/virtual/misc/misc-fake89999",
e62acc31 1058 exp_links => ["node"],
255c05b7
MW
1059 exp_majorminor => "4095:89999",
1060 }],
912541b0 1061 rules => <<EOF
5754e74c 1062KERNEL=="misc-fake89999", SYMLINK+="node"
2b0f835c 1063EOF
912541b0
KS
1064 },
1065 {
1066 desc => "multiple symlinks with format char",
255c05b7
MW
1067 devices => [
1068 {
1069 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1070 exp_links => ["symlink1-0", "symlink2-ttyACM0", "symlink3-"],
255c05b7 1071 }],
912541b0 1072 rules => <<EOF
5754e74c 1073KERNEL=="ttyACM[0-9]*", SYMLINK="symlink1-%n symlink2-%k symlink3-%b"
7b2bdb4b 1074EOF
912541b0
KS
1075 },
1076 {
1077 desc => "multiple symlinks with a lot of s p a c e s",
255c05b7
MW
1078 devices => [
1079 {
1080 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1081 exp_links => ["one", "two"],
1082 not_exp_links => [" "],
255c05b7 1083 }],
912541b0 1084 rules => <<EOF
5754e74c 1085KERNEL=="ttyACM[0-9]*", SYMLINK=" one two "
9cd7b128
DS
1086EOF
1087 },
1088 {
1089 desc => "symlink with spaces in substituted variable",
255c05b7
MW
1090 devices => [
1091 {
1092 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1093 exp_links => ["name-one_two_three-end"],
1094 not_exp_links => [" "],
255c05b7 1095 }],
9cd7b128
DS
1096 rules => <<EOF
1097ENV{WITH_WS}="one two three"
1098SYMLINK="name-\$env{WITH_WS}-end"
1099EOF
1100 },
1101 {
1102 desc => "symlink with leading space in substituted variable",
255c05b7
MW
1103 devices => [
1104 {
1105 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1106 exp_links => ["name-one_two_three-end"],
1107 not_exp_links => [" "],
255c05b7 1108 }],
9cd7b128
DS
1109 rules => <<EOF
1110ENV{WITH_WS}=" one two three"
1111SYMLINK="name-\$env{WITH_WS}-end"
1112EOF
1113 },
1114 {
1115 desc => "symlink with trailing space in substituted variable",
255c05b7
MW
1116 devices => [
1117 {
1118 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1119 exp_links => ["name-one_two_three-end"],
1120 not_exp_links => [" "],
255c05b7 1121 }],
9cd7b128
DS
1122 rules => <<EOF
1123ENV{WITH_WS}="one two three "
1124SYMLINK="name-\$env{WITH_WS}-end"
1125EOF
1126 },
1127 {
1128 desc => "symlink with lots of space in substituted variable",
255c05b7
MW
1129 devices => [
1130 {
1131 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1132 exp_links => ["name-one_two_three-end"],
1133 not_exp_links => [" "],
255c05b7 1134 }],
9cd7b128
DS
1135 rules => <<EOF
1136ENV{WITH_WS}=" one two three "
1137SYMLINK="name-\$env{WITH_WS}-end"
1138EOF
1139 },
1140 {
1141 desc => "symlink with multiple spaces in substituted variable",
255c05b7
MW
1142 devices => [
1143 {
1144 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1145 exp_links => ["name-one_two_three-end"],
1146 not_exp_links => [" "],
255c05b7 1147 }],
9cd7b128
DS
1148 rules => <<EOF
1149ENV{WITH_WS}=" one two three "
1150SYMLINK="name-\$env{WITH_WS}-end"
1151EOF
1152 },
1153 {
e62acc31 1154 desc => "symlink with space and var with space",
255c05b7
MW
1155 devices => [
1156 {
1157 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
2084fe0d
MW
1158 exp_links => ["first", "name-one_two_three-end",
1159 "another_symlink", "a", "b", "c"],
1160 not_exp_links => [" "],
255c05b7 1161 }],
9cd7b128
DS
1162 rules => <<EOF
1163ENV{WITH_WS}=" one two three "
1164SYMLINK=" first name-\$env{WITH_WS}-end another_symlink a b c "
b8669191 1165EOF
912541b0
KS
1166 },
1167 {
1168 desc => "symlink creation (same directory)",
255c05b7
MW
1169 devices => [
1170 {
1171 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1172 exp_links => ["modem0"],
255c05b7 1173 }],
912541b0 1174 rules => <<EOF
5754e74c 1175KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK="modem%n"
b8669191 1176EOF
912541b0
KS
1177 },
1178 {
1179 desc => "multiple symlinks",
255c05b7
MW
1180 devices => [
1181 {
1182 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1183 exp_links => ["first-0", "second-0", "third-0"],
255c05b7 1184 }],
912541b0 1185 rules => <<EOF
5754e74c 1186KERNEL=="ttyACM0", SYMLINK="first-%n second-%n third-%n"
b8669191 1187EOF
912541b0
KS
1188 },
1189 {
1190 desc => "symlink name '.'",
255c05b7
MW
1191 devices => [
1192 {
1193 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1194 exp_links => ["."],
255c05b7
MW
1195 exp_add_error => "yes",
1196 exp_rem_error => "yes",
1197 }],
912541b0 1198 rules => <<EOF
5754e74c 1199SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="."
b8669191 1200EOF
912541b0
KS
1201 },
1202 {
1203 desc => "symlink node to itself",
255c05b7
MW
1204 devices => [
1205 {
1206 devpath => "/devices/virtual/tty/tty0",
e62acc31 1207 exp_links => ["link"],
255c05b7
MW
1208 exp_add_error => "yes",
1209 exp_rem_error => "yes",
1210 }],
1211 option => "clean",
912541b0 1212 rules => <<EOF
5754e74c 1213KERNEL=="tty0", SYMLINK+="tty0"
b8669191 1214EOF
912541b0
KS
1215 },
1216 {
1217 desc => "symlink %n substitution",
255c05b7
MW
1218 devices => [
1219 {
1220 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1221 exp_links => ["symlink0"],
255c05b7 1222 }],
912541b0 1223 rules => <<EOF
5754e74c 1224KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink%n"
b8669191 1225EOF
912541b0
KS
1226 },
1227 {
1228 desc => "symlink %k substitution",
255c05b7
MW
1229 devices => [
1230 {
1231 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1232 exp_links => ["symlink-ttyACM0"],
255c05b7 1233 }],
912541b0 1234 rules => <<EOF
5754e74c 1235KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="symlink-%k"
b8669191 1236EOF
912541b0
KS
1237 },
1238 {
1239 desc => "symlink %M:%m substitution",
255c05b7
MW
1240 devices => [
1241 {
1242 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1243 exp_links => ["major-166:0"],
255c05b7 1244 }],
912541b0 1245 rules => <<EOF
5754e74c 1246KERNEL=="ttyACM[0-9]*", SYMLINK+="ttyACM%n", SYMLINK+="major-%M:%m"
b8669191 1247EOF
912541b0
KS
1248 },
1249 {
1250 desc => "symlink %b substitution",
255c05b7
MW
1251 devices => [
1252 {
1253 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1254 exp_links => ["symlink-0:0:0:0"],
255c05b7 1255 }],
912541b0 1256 rules => <<EOF
220893b3 1257SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="symlink-%b"
b8669191 1258EOF
912541b0
KS
1259 },
1260 {
1261 desc => "symlink %c substitution",
255c05b7
MW
1262 devices => [
1263 {
1264 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1265 exp_links => ["test"],
255c05b7 1266 }],
912541b0 1267 rules => <<EOF
d914e445 1268KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo test", SYMLINK+="%c"
b8669191 1269EOF
912541b0
KS
1270 },
1271 {
1272 desc => "symlink %c{N} substitution",
255c05b7
MW
1273 devices => [
1274 {
1275 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1276 exp_links => ["test"],
1277 not_exp_links => ["symlink", "this"],
255c05b7 1278 }],
912541b0 1279 rules => <<EOF
d914e445 1280KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2}"
b8669191 1281EOF
912541b0
KS
1282 },
1283 {
1284 desc => "symlink %c{N+} substitution",
255c05b7
MW
1285 devices => [
1286 {
1287 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1288 exp_links => ["test", "this"],
1289 not_exp_links => ["symlink"],
255c05b7 1290 }],
912541b0 1291 rules => <<EOF
d914e445 1292KERNEL=="ttyACM[0-9]*", PROGRAM=="/bin/echo symlink test this", SYMLINK+="%c{2+}"
b8669191 1293EOF
912541b0
KS
1294 },
1295 {
1296 desc => "symlink only rule with %c{N+}",
255c05b7
MW
1297 devices => [
1298 {
1299 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1300 exp_links => ["test", "this"],
1301 not_exp_links => ["symlink"],
255c05b7 1302 }],
912541b0 1303 rules => <<EOF
d914e445 1304SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/bin/echo link test this" SYMLINK+="%c{2+}"
b8669191 1305EOF
912541b0
KS
1306 },
1307 {
1308 desc => "symlink %s{filename} substitution",
255c05b7
MW
1309 devices => [
1310 {
1311 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1312 exp_links => ["166:0"],
255c05b7 1313 }],
912541b0 1314 rules => <<EOF
5754e74c 1315KERNEL=="ttyACM[0-9]*", SYMLINK+="%s{dev}"
b8669191 1316EOF
912541b0
KS
1317 },
1318 {
1319 desc => "program result substitution (numbered part of)",
255c05b7
MW
1320 devices => [
1321 {
1322 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
1323 exp_links => ["link1", "link2"],
1324 not_exp_links => ["node"],
255c05b7 1325 }],
912541b0 1326 rules => <<EOF
d914e445 1327SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2", RESULT=="node *", SYMLINK+="%c{2} %c{3}"
b8669191 1328EOF
912541b0
KS
1329 },
1330 {
1331 desc => "program result substitution (numbered part of+)",
255c05b7
MW
1332 devices => [
1333 {
1334 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
e62acc31
MW
1335 exp_links => ["link1", "link2", "link3", "link4"],
1336 not_exp_links => ["node"],
255c05b7 1337 }],
912541b0 1338 rules => <<EOF
d914e445 1339SUBSYSTEMS=="scsi", PROGRAM=="/bin/echo -n node link1 link2 link3 link4", RESULT=="node *", SYMLINK+="%c{2+}"
7efa217d 1340EOF
912541b0
KS
1341 },
1342 {
1343 desc => "SUBSYSTEM match test",
255c05b7
MW
1344 devices => [
1345 {
1346 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1347 exp_links => ["node"],
1348 not_exp_links => ["should_not_match", "should_not_match2"],
255c05b7 1349 }],
912541b0 1350 rules => <<EOF
5754e74c
KS
1351SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", SUBSYSTEM=="vc"
1352SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", SUBSYSTEM=="block"
1353SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match2", SUBSYSTEM=="vc"
2092fbcd 1354EOF
912541b0
KS
1355 },
1356 {
1357 desc => "DRIVERS match test",
255c05b7
MW
1358 devices => [
1359 {
1360 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1361 exp_links => ["node"],
1362 not_exp_links => ["should_not_match"]
255c05b7 1363 }],
912541b0 1364 rules => <<EOF
5754e74c
KS
1365SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="should_not_match", DRIVERS=="sd-wrong"
1366SUBSYSTEMS=="scsi", KERNEL=="sda", SYMLINK+="node", DRIVERS=="sd"
c1ab0461 1367EOF
912541b0
KS
1368 },
1369 {
1370 desc => "devnode substitution test",
255c05b7
MW
1371 devices => [
1372 {
1373 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1374 exp_links => ["node"],
255c05b7 1375 }],
912541b0 1376 rules => <<EOF
220893b3 1377SUBSYSTEMS=="scsi", KERNEL=="sda", PROGRAM=="/usr/bin/test -b %N" SYMLINK+="node"
69aa6dfb 1378EOF
912541b0
KS
1379 },
1380 {
1381 desc => "parent node name substitution test",
255c05b7
MW
1382 devices => [
1383 {
1384 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1385 exp_links => ["sda-part-1"],
255c05b7 1386 }],
912541b0 1387 rules => <<EOF
06d4d4e2 1388SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="%P-part-%n"
69aa6dfb 1389EOF
912541b0
KS
1390 },
1391 {
1392 desc => "udev_root substitution",
255c05b7
MW
1393 devices => [
1394 {
1395 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1396 exp_links => ["start-/dev-end"],
255c05b7 1397 }],
912541b0 1398 rules => <<EOF
5754e74c 1399SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="start-%r-end"
3b6ed8bb 1400EOF
912541b0
KS
1401 },
1402 {
17cce031 1403 # This is not supported any more
912541b0 1404 desc => "last_rule option",
255c05b7
MW
1405 devices => [
1406 {
1407 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1408 exp_links => ["last"],
17cce031
MW
1409 not_exp_links => ["very-last"],
1410 exp_nodev_error => "yes",
255c05b7 1411 }],
912541b0 1412 rules => <<EOF
c4edd0ad 1413SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="last", OPTIONS="last_rule"
5754e74c 1414SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="very-last"
28ce66de 1415EOF
912541b0
KS
1416 },
1417 {
1418 desc => "negation KERNEL!=",
255c05b7
MW
1419 devices => [
1420 {
1421 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1422 exp_links => ["match", "before"],
1423 not_exp_links => ["matches-but-is-negated"],
255c05b7 1424 }],
912541b0 1425 rules => <<EOF
5754e74c
KS
1426SUBSYSTEMS=="scsi", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1427SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1428SUBSYSTEMS=="scsi", KERNEL!="xsda1", SYMLINK+="match"
28ce66de 1429EOF
912541b0
KS
1430 },
1431 {
1432 desc => "negation SUBSYSTEM!=",
255c05b7
MW
1433 devices => [
1434 {
1435 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1436 exp_links => ["before", "not-anything"],
1437 not_exp_links => ["matches-but-is-negated"],
255c05b7 1438 }],
912541b0 1439 rules => <<EOF
5754e74c
KS
1440SUBSYSTEMS=="scsi", SUBSYSTEM=="block", KERNEL!="sda1", SYMLINK+="matches-but-is-negated"
1441SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1442SUBSYSTEMS=="scsi", SUBSYSTEM!="anything", SYMLINK+="not-anything"
28ce66de 1443EOF
912541b0
KS
1444 },
1445 {
1446 desc => "negation PROGRAM!= exit code",
255c05b7
MW
1447 devices => [
1448 {
1449 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1450 exp_links => ["before", "nonzero-program"],
255c05b7 1451 }],
912541b0 1452 rules => <<EOF
5754e74c 1453SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
d914e445 1454KERNEL=="sda1", PROGRAM!="/bin/false", SYMLINK+="nonzero-program"
3e5958de 1455EOF
912541b0
KS
1456 },
1457 {
1458 desc => "ENV{} test",
255c05b7
MW
1459 devices => [
1460 {
1461 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1462 exp_links => ["true"],
1463 not_exp_links => ["bad", "wrong"],
255c05b7 1464 }],
912541b0 1465 rules => <<EOF
a1af6b04 1466ENV{ENV_KEY_TEST}="test"
5754e74c
KS
1467SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1468SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", SYMLINK+="true"
1469SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
3e5958de 1470EOF
912541b0
KS
1471 },
1472 {
1473 desc => "ENV{} test",
255c05b7
MW
1474 devices => [
1475 {
1476 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1477 exp_links => ["true"],
1478 not_exp_links => ["bad", "wrong", "no"],
255c05b7 1479 }],
912541b0 1480 rules => <<EOF
a1af6b04 1481ENV{ENV_KEY_TEST}="test"
5754e74c
KS
1482SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="go", SYMLINK+="wrong"
1483SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="yes", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sdax1", SYMLINK+="no"
1484SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="test", ENV{ACTION}=="add", ENV{DEVPATH}=="*/block/sda/sda1", SYMLINK+="true"
1485SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ENV_KEY_TEST}=="bad", SYMLINK+="bad"
5618b561 1486EOF
912541b0
KS
1487 },
1488 {
1489 desc => "ENV{} test (assign)",
255c05b7
MW
1490 devices => [
1491 {
1492 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1493 exp_links => ["true", "before"],
1494 not_exp_links => ["no"],
255c05b7 1495 }],
912541b0 1496 rules => <<EOF
c4edd0ad 1497SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
5754e74c
KS
1498SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
1499SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1500SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="true"
ac528431 1501EOF
912541b0
KS
1502 },
1503 {
1504 desc => "ENV{} test (assign 2 times)",
255c05b7
MW
1505 devices => [
1506 {
1507 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1508 exp_links => ["true", "before"],
1509 not_exp_links => ["no", "bad"],
255c05b7 1510 }],
912541b0 1511 rules => <<EOF
ac528431
KS
1512SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="true"
1513SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}="absolutely-\$env{ASSIGN}"
5754e74c
KS
1514SUBSYSTEMS=="scsi", KERNEL=="sda1", SYMLINK+="before"
1515SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="yes", SYMLINK+="no"
06d4d4e2 1516SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="true", SYMLINK+="bad"
5754e74c 1517SUBSYSTEMS=="scsi", KERNEL=="sda1", ENV{ASSIGN}=="absolutely-true", SYMLINK+="true"
5618b561 1518EOF
912541b0
KS
1519 },
1520 {
1521 desc => "ENV{} test (assign2)",
255c05b7
MW
1522 devices => [
1523 {
1524 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1525 exp_links => ["part"],
1526 not_exp_links => ["disk"],
1527 },
06d4d4e2
MW
1528 {
1529 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
1530 exp_links => ["disk"],
1531 not_exp_links => ["part"],
1532 },
e62acc31 1533 ],
912541b0 1534 rules => <<EOF
d59c84ef
KS
1535SUBSYSTEM=="block", KERNEL=="*[0-9]", ENV{PARTITION}="true", ENV{MAINDEVICE}="false"
1536SUBSYSTEM=="block", KERNEL=="*[!0-9]", ENV{PARTITION}="false", ENV{MAINDEVICE}="true"
5754e74c
KS
1537ENV{MAINDEVICE}=="true", SYMLINK+="disk"
1538SUBSYSTEM=="block", SYMLINK+="before"
1539ENV{PARTITION}=="true", SYMLINK+="part"
18614ab2 1540EOF
912541b0
KS
1541 },
1542 {
1543 desc => "untrusted string sanitize",
255c05b7
MW
1544 devices => [
1545 {
1546 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1547 exp_links => ["sane"],
255c05b7 1548 }],
912541b0 1549 rules => <<EOF
d914e445 1550SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e name; (/usr/bin/badprogram)", RESULT=="name_ _/usr/bin/badprogram_", SYMLINK+="sane"
764ce7f2 1551EOF
912541b0
KS
1552 },
1553 {
1554 desc => "untrusted string sanitize (don't replace utf8)",
255c05b7
MW
1555 devices => [
1556 {
1557 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1558 exp_links => ["uber"],
255c05b7 1559 }],
912541b0 1560 rules => <<EOF
d914e445 1561SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xc3\\xbcber" RESULT=="\xc3\xbcber", SYMLINK+="uber"
764ce7f2 1562EOF
912541b0
KS
1563 },
1564 {
1565 desc => "untrusted string sanitize (replace invalid utf8)",
255c05b7
MW
1566 devices => [
1567 {
1568 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1569 exp_links => ["replaced"],
255c05b7 1570 }],
912541b0 1571 rules => <<EOF
d914e445 1572SUBSYSTEMS=="scsi", KERNEL=="sda1", PROGRAM=="/bin/echo -e \\xef\\xe8garbage", RESULT=="__garbage", SYMLINK+="replaced"
98bbc835 1573EOF
912541b0
KS
1574 },
1575 {
1576 desc => "read sysfs value from parent device",
255c05b7
MW
1577 devices => [
1578 {
1579 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1580 exp_links => ["serial-354172020305000"],
255c05b7 1581 }],
912541b0 1582 rules => <<EOF
5754e74c 1583KERNEL=="ttyACM*", ATTRS{serial}=="?*", SYMLINK+="serial-%s{serial}"
db949b02 1584EOF
912541b0
KS
1585 },
1586 {
1587 desc => "match against empty key string",
255c05b7
MW
1588 devices => [
1589 {
1590 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1591 exp_links => ["ok"],
1592 not_exp_links => ["not-1-ok", "not-2-ok", "not-3-ok"],
255c05b7 1593 }],
912541b0 1594 rules => <<EOF
5754e74c
KS
1595KERNEL=="sda", ATTRS{nothing}!="", SYMLINK+="not-1-ok"
1596KERNEL=="sda", ATTRS{nothing}=="", SYMLINK+="not-2-ok"
1597KERNEL=="sda", ATTRS{vendor}!="", SYMLINK+="ok"
1598KERNEL=="sda", ATTRS{vendor}=="", SYMLINK+="not-3-ok"
821d0ec8 1599EOF
912541b0
KS
1600 },
1601 {
1602 desc => "check ACTION value",
255c05b7
MW
1603 devices => [
1604 {
1605 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1606 exp_links => ["ok"],
1607 not_exp_links => ["unknown-not-ok"],
255c05b7 1608 }],
912541b0 1609 rules => <<EOF
5754e74c
KS
1610ACTION=="unknown", KERNEL=="sda", SYMLINK+="unknown-not-ok"
1611ACTION=="add", KERNEL=="sda", SYMLINK+="ok"
c974742b 1612EOF
912541b0
KS
1613 },
1614 {
1615 desc => "final assignment",
255c05b7
MW
1616 devices => [
1617 {
1618 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1619 exp_links => ["ok"],
255c05b7
MW
1620 exp_perms => "root:tty:0640",
1621 }],
912541b0 1622 rules => <<EOF
d960ad15 1623KERNEL=="sda", GROUP:="tty"
06d4d4e2 1624KERNEL=="sda", GROUP="root", MODE="0640", SYMLINK+="ok"
c974742b 1625EOF
912541b0
KS
1626 },
1627 {
1628 desc => "final assignment 2",
255c05b7
MW
1629 devices => [
1630 {
1631 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1632 exp_links => ["ok"],
255c05b7
MW
1633 exp_perms => "root:tty:0640",
1634 }],
912541b0 1635 rules => <<EOF
d960ad15 1636KERNEL=="sda", GROUP:="tty"
c974742b 1637SUBSYSTEM=="block", MODE:="640"
06d4d4e2 1638KERNEL=="sda", GROUP="root", MODE="0666", SYMLINK+="ok"
bd0ed2ff 1639EOF
912541b0
KS
1640 },
1641 {
1642 desc => "env substitution",
255c05b7
MW
1643 devices => [
1644 {
1645 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1646 exp_links => ["node-add-me"],
255c05b7 1647 }],
912541b0 1648 rules => <<EOF
5754e74c 1649KERNEL=="sda", MODE="0666", SYMLINK+="node-\$env{ACTION}-me"
995aec87 1650EOF
912541b0
KS
1651 },
1652 {
1653 desc => "reset list to current value",
255c05b7
MW
1654 devices => [
1655 {
1656 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1657 exp_links => ["three"],
1658 not_exp_links => ["two", "one"],
255c05b7 1659 }],
912541b0 1660 rules => <<EOF
c7fcba1b
KS
1661KERNEL=="ttyACM[0-9]*", SYMLINK+="one"
1662KERNEL=="ttyACM[0-9]*", SYMLINK+="two"
1663KERNEL=="ttyACM[0-9]*", SYMLINK="three"
647f7c49 1664EOF
912541b0
KS
1665 },
1666 {
1667 desc => "test empty SYMLINK+ (empty override)",
255c05b7
MW
1668 devices => [
1669 {
1670 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1671 exp_links => ["right"],
1672 not_exp_links => ["wrong"],
255c05b7 1673 }],
912541b0 1674 rules => <<EOF
5754e74c
KS
1675KERNEL=="ttyACM[0-9]*", SYMLINK+="wrong"
1676KERNEL=="ttyACM[0-9]*", SYMLINK=""
1677KERNEL=="ttyACM[0-9]*", SYMLINK+="right"
0cd4ac47 1678EOF
912541b0
KS
1679 },
1680 {
1681 desc => "test multi matches",
255c05b7
MW
1682 devices => [
1683 {
1684 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31 1685 exp_links => ["right", "before"],
255c05b7 1686 }],
912541b0 1687 rules => <<EOF
5754e74c
KS
1688KERNEL=="ttyACM*", SYMLINK+="before"
1689KERNEL=="ttyACM*|nothing", SYMLINK+="right"
0cd4ac47 1690EOF
912541b0
KS
1691 },
1692 {
1693 desc => "test multi matches 2",
255c05b7
MW
1694 devices => [
1695 {
1696 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1697 exp_links => ["right", "before"],
1698 not_exp_links => ["nomatch"],
255c05b7 1699 }],
912541b0 1700 rules => <<EOF
5754e74c
KS
1701KERNEL=="dontknow*|*nothing", SYMLINK+="nomatch"
1702KERNEL=="ttyACM*", SYMLINK+="before"
1703KERNEL=="dontknow*|ttyACM*|nothing*", SYMLINK+="right"
91a75e4a 1704EOF
912541b0
KS
1705 },
1706 {
1707 desc => "test multi matches 3",
255c05b7
MW
1708 devices => [
1709 {
1710 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1711 exp_links => ["right"],
1712 not_exp_links => ["nomatch", "wrong1", "wrong2"],
255c05b7 1713 }],
912541b0 1714 rules => <<EOF
5754e74c
KS
1715KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1716KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1717KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1718KERNEL=="dontknow|ttyACM0|nothing", SYMLINK+="right"
91a75e4a 1719EOF
912541b0
KS
1720 },
1721 {
1722 desc => "test multi matches 4",
255c05b7
MW
1723 devices => [
1724 {
1725 devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
e62acc31
MW
1726 exp_links => ["right"],
1727 not_exp_links => ["nomatch", "wrong1", "wrong2", "wrong3"],
255c05b7 1728 }],
912541b0 1729 rules => <<EOF
5754e74c
KS
1730KERNEL=="dontknow|nothing", SYMLINK+="nomatch"
1731KERNEL=="dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong1"
1732KERNEL=="X|attyACM0|dontknow|ttyACM0a|nothing|attyACM0", SYMLINK+="wrong2"
1733KERNEL=="all|dontknow|ttyACM0", SYMLINK+="right"
1734KERNEL=="ttyACM0a|nothing", SYMLINK+="wrong3"
48d26c90
YW
1735EOF
1736 },
1737 {
255c05b7
MW
1738 desc => "test multi matches 5",
1739 devices => [
1740 {
1741 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1742 exp_links => ["found"],
255c05b7
MW
1743 not_exp_name => "bad",
1744 }],
48d26c90
YW
1745 rules => <<EOF
1746KERNEL=="sda", TAG="foo"
1747TAGS=="|foo", SYMLINK+="found"
1748TAGS=="|aaa", SYMLINK+="bad"
1749EOF
1750 },
1751 {
1752 desc => "test multi matches 6",
255c05b7
MW
1753 devices => [
1754 {
1755 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1756 exp_links => ["found"],
255c05b7
MW
1757 not_exp_name => "bad",
1758 }],
48d26c90
YW
1759 rules => <<EOF
1760KERNEL=="sda", TAG=""
1761TAGS=="|foo", SYMLINK+="found"
1762TAGS=="aaa|bbb", SYMLINK+="bad"
1763EOF
1764 },
1765 {
1766 desc => "test multi matches 7",
255c05b7
MW
1767 devices => [
1768 {
1769 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1770 exp_links => ["found"],
255c05b7
MW
1771 not_exp_name => "bad",
1772 }],
48d26c90
YW
1773 rules => <<EOF
1774KERNEL=="sda", TAG="foo"
1775TAGS=="foo||bar", SYMLINK+="found"
1776TAGS=="aaa||bbb", SYMLINK+="bad"
1777EOF
1778 },
1779 {
1780 desc => "test multi matches 8",
255c05b7
MW
1781 devices => [
1782 {
1783 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1784 exp_links => ["found"],
255c05b7
MW
1785 not_exp_name => "bad",
1786 }],
48d26c90
YW
1787 rules => <<EOF
1788KERNEL=="sda", TAG=""
1789TAGS=="foo||bar", SYMLINK+="found"
1790TAGS=="aaa|bbb", SYMLINK+="bad"
1791EOF
1792 },
1793 {
1794 desc => "test multi matches 9",
255c05b7
MW
1795 devices => [
1796 {
1797 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1798 exp_links => ["found"],
255c05b7
MW
1799 not_exp_name => "bad",
1800 }],
48d26c90
YW
1801 rules => <<EOF
1802KERNEL=="sda", TAG="foo"
1803TAGS=="foo|", SYMLINK+="found"
1804TAGS=="aaa|", SYMLINK+="bad"
1805EOF
1806 },
1807 {
1808 desc => "test multi matches 10",
255c05b7
MW
1809 devices => [
1810 {
1811 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1812 exp_links => ["found"],
255c05b7
MW
1813 not_exp_name => "bad",
1814 }],
48d26c90
YW
1815 rules => <<EOF
1816KERNEL=="sda", TAG=""
1817TAGS=="foo|", SYMLINK+="found"
1818TAGS=="aaa|bbb", SYMLINK+="bad"
0d3a8bc7
YG
1819EOF
1820 },
1821 {
1822 desc => "test multi matches 11",
255c05b7
MW
1823 devices => [
1824 {
1825 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1826 exp_links => ["found"],
255c05b7
MW
1827 not_exp_name => "bad",
1828 }],
0d3a8bc7
YG
1829 rules => <<EOF
1830KERNEL=="sda", TAG="c"
1831TAGS=="foo||bar||c", SYMLINK+="found"
1832TAGS=="aaa||bbb||ccc", SYMLINK+="bad"
0bfb84e1 1833EOF
912541b0
KS
1834 },
1835 {
a96cd21d 1836 desc => "IMPORT parent test",
255c05b7
MW
1837 devices => [
1838 {
1839 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1840 exp_links => ["parent"],
a96cd21d 1841 },
255c05b7
MW
1842 {
1843 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1844 exp_links => ["parentenv-parent_right"],
255c05b7 1845 }],
a96cd21d 1846 sleep_us => 500000, # Serialized! We need to sleep here after adding sda
912541b0 1847 rules => <<EOF
5754e74c 1848KERNEL=="sda1", IMPORT{parent}="PARENT*", SYMLINK+="parentenv-\$env{PARENT_KEY}\$env{WRONG_PARENT_KEY}"
a96cd21d
MW
1849KERNEL=="sda", IMPORT{program}="/bin/echo -e \'PARENT_KEY=parent_right\\nWRONG_PARENT_KEY=parent_wrong'"
1850KERNEL=="sda", SYMLINK+="parent"
594dd610 1851EOF
912541b0
KS
1852 },
1853 {
1854 desc => "GOTO test",
255c05b7
MW
1855 devices => [
1856 {
1857 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1858 exp_links => ["right"],
1859 not_exp_test => ["wrong", "wrong2"],
255c05b7 1860 }],
912541b0 1861 rules => <<EOF
594dd610 1862KERNEL=="sda1", GOTO="TEST"
5754e74c 1863KERNEL=="sda1", SYMLINK+="wrong"
6880b25d 1864KERNEL=="sda1", GOTO="BAD"
5754e74c
KS
1865KERNEL=="sda1", SYMLINK+="", LABEL="NO"
1866KERNEL=="sda1", SYMLINK+="right", LABEL="TEST", GOTO="end"
1867KERNEL=="sda1", SYMLINK+="wrong2", LABEL="BAD"
9dae0e89 1868LABEL="end"
0c377989 1869EOF
912541b0
KS
1870 },
1871 {
1872 desc => "GOTO label does not exist",
255c05b7
MW
1873 devices => [
1874 {
1875 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31 1876 exp_links => ["right"],
255c05b7 1877 }],
912541b0 1878 rules => <<EOF
0c377989 1879KERNEL=="sda1", GOTO="does-not-exist"
5754e74c 1880KERNEL=="sda1", SYMLINK+="right",
0c377989 1881LABEL="exists"
d59c84ef 1882EOF
912541b0
KS
1883 },
1884 {
1885 desc => "SYMLINK+ compare test",
255c05b7
MW
1886 devices => [
1887 {
1888 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1889 exp_links => ["right", "link"],
1890 not_exp_links => ["wrong"],
255c05b7 1891 }],
912541b0 1892 rules => <<EOF
5754e74c
KS
1893KERNEL=="sda1", SYMLINK+="link"
1894KERNEL=="sda1", SYMLINK=="link*", SYMLINK+="right"
1895KERNEL=="sda1", SYMLINK=="nolink*", SYMLINK+="wrong"
d59c84ef 1896EOF
912541b0
KS
1897 },
1898 {
1899 desc => "invalid key operation",
255c05b7
MW
1900 devices => [
1901 {
1902 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1903 exp_links => ["yes"],
1904 not_exp_links => ["no"],
255c05b7 1905 }],
912541b0 1906 rules => <<EOF
5754e74c
KS
1907KERNEL="sda1", SYMLINK+="no"
1908KERNEL=="sda1", SYMLINK+="yes"
864b9b5e 1909EOF
912541b0
KS
1910 },
1911 {
1912 desc => "operator chars in attribute",
255c05b7
MW
1913 devices => [
1914 {
1915 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1916 exp_links => ["yes"],
255c05b7 1917 }],
912541b0 1918 rules => <<EOF
5754e74c 1919KERNEL=="sda", ATTR{test:colon+plus}=="?*", SYMLINK+="yes"
d4ae9925 1920EOF
912541b0
KS
1921 },
1922 {
1923 desc => "overlong comment line",
255c05b7
MW
1924 devices => [
1925 {
1926 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
e62acc31
MW
1927 exp_links => ["yes"],
1928 not_exp_links => ["no"],
255c05b7 1929 }],
912541b0 1930 rules => <<EOF
d4ae9925 1931# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
d960ad15 1932 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
5754e74c
KS
1933KERNEL=="sda1", SYMLINK+=="no"
1934KERNEL=="sda1", SYMLINK+="yes"
4ad47b2d 1935EOF
912541b0
KS
1936 },
1937 {
1938 desc => "magic subsys/kernel lookup",
255c05b7
MW
1939 devices => [
1940 {
1941 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1942 exp_links => ["00:16:41:e2:8d:ff"],
255c05b7 1943 }],
912541b0 1944 rules => <<EOF
5754e74c 1945KERNEL=="sda", SYMLINK+="\$attr{[net/eth0]address}"
03f65fe6 1946EOF
912541b0
KS
1947 },
1948 {
1949 desc => "TEST absolute path",
255c05b7
MW
1950 devices => [
1951 {
1952 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
1953 exp_links => ["there"],
1954 not_exp_links => ["notthere"],
255c05b7 1955 }],
912541b0 1956 rules => <<EOF
02cd084d
TG
1957TEST=="/etc/machine-id", SYMLINK+="there"
1958TEST!="/etc/machine-id", SYMLINK+="notthere"
03f65fe6 1959EOF
912541b0
KS
1960 },
1961 {
1962 desc => "TEST subsys/kernel lookup",
255c05b7
MW
1963 devices => [
1964 {
1965 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1966 exp_links => ["yes"],
255c05b7 1967 }],
912541b0 1968 rules => <<EOF
5754e74c 1969KERNEL=="sda", TEST=="[net/eth0]", SYMLINK+="yes"
03f65fe6 1970EOF
912541b0
KS
1971 },
1972 {
1973 desc => "TEST relative path",
255c05b7
MW
1974 devices => [
1975 {
1976 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1977 exp_links => ["relative"],
255c05b7 1978 }],
912541b0 1979 rules => <<EOF
5754e74c 1980KERNEL=="sda", TEST=="size", SYMLINK+="relative"
0ea5e96e 1981EOF
912541b0
KS
1982 },
1983 {
1984 desc => "TEST wildcard substitution (find queue/nr_requests)",
255c05b7
MW
1985 devices => [
1986 {
1987 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 1988 exp_links => ["found-subdir"],
255c05b7 1989 }],
912541b0 1990 rules => <<EOF
5754e74c 1991KERNEL=="sda", TEST=="*/nr_requests", SYMLINK+="found-subdir"
cf100ca7 1992EOF
912541b0
KS
1993 },
1994 {
1995 desc => "TEST MODE=0000",
255c05b7
MW
1996 devices => [
1997 {
1998 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
1999 exp_perms => "0:0:0000",
2000 exp_rem_error => "yes",
2001 }],
912541b0 2002 rules => <<EOF
cf100ca7 2003KERNEL=="sda", MODE="0000"
a367f04e 2004EOF
912541b0
KS
2005 },
2006 {
2007 desc => "TEST PROGRAM feeds OWNER, GROUP, MODE",
255c05b7
MW
2008 devices => [
2009 {
2010 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7 2011 exp_perms => "1:1:0400",
255c05b7 2012 }],
912541b0 2013 rules => <<EOF
6880b25d 2014KERNEL=="sda", MODE="666"
9158d03e 2015KERNEL=="sda", PROGRAM=="/bin/echo 1 1 0400", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
ff94cec3 2016EOF
912541b0
KS
2017 },
2018 {
2019 desc => "TEST PROGRAM feeds MODE with overflow",
255c05b7
MW
2020 devices => [
2021 {
2022 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
255c05b7
MW
2023 exp_perms => "0:0:0440",
2024 exp_rem_error => "yes",
2025 }],
912541b0 2026 rules => <<EOF
6880b25d 2027KERNEL=="sda", MODE="440"
d914e445 2028KERNEL=="sda", PROGRAM=="/bin/echo 0 0 0400letsdoabuffferoverflow0123456789012345789012345678901234567890", OWNER="%c{1}", GROUP="%c{2}", MODE="%c{3}"
dc4c7e46 2029EOF
912541b0
KS
2030 },
2031 {
2032 desc => "magic [subsys/sysname] attribute substitution",
255c05b7
MW
2033 devices => [
2034 {
2035 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2036 exp_links => ["sda-8741C4G-end"],
255c05b7
MW
2037 exp_perms => "0:0:0600",
2038 }],
912541b0 2039 rules => <<EOF
0f52fdee 2040KERNEL=="sda", SYMLINK+="%k-%s{[dmi/id]product_name}-end"
d7867b31 2041EOF
912541b0
KS
2042 },
2043 {
2044 desc => "builtin path_id",
255c05b7
MW
2045 devices => [
2046 {
2047 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2048 exp_links => ["disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0"],
255c05b7 2049 }],
912541b0 2050 rules => <<EOF
d7867b31
KS
2051KERNEL=="sda", IMPORT{builtin}="path_id"
2052KERNEL=="sda", ENV{ID_PATH}=="?*", SYMLINK+="disk/by-path/\$env{ID_PATH}"
bf8f7583
MP
2053EOF
2054 },
2055 {
2056 desc => "add and match tag",
255c05b7
MW
2057 devices => [
2058 {
2059 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31
MW
2060 exp_links => ["found"],
2061 not_exp_links => ["bad"],
255c05b7 2062 }],
bf8f7583
MP
2063 rules => <<EOF
2064SUBSYSTEMS=="scsi", ATTRS{vendor}=="ATA", TAG+="green"
2065TAGS=="green", SYMLINK+="found"
2066TAGS=="blue", SYMLINK+="bad"
2067EOF
2068 },
2069 {
2070 desc => "don't crash with lots of tags",
255c05b7
MW
2071 devices => [
2072 {
2073 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2074 exp_links => ["found"],
255c05b7 2075 }],
bf8f7583
MP
2076 rules => $rules_10k_tags . <<EOF
2077TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="found"
1e797cf5
YW
2078EOF
2079 },
2080 {
d35976c6 2081 desc => "continuations",
255c05b7
MW
2082 devices => [
2083 {
2084 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2085 exp_links => ["found"],
255c05b7
MW
2086 not_exp_name => "bad",
2087 }],
1e797cf5
YW
2088 rules => $rules_10k_tags_continuation . <<EOF
2089TAGS=="test1", TAGS=="test500", TAGS=="test1234", TAGS=="test9999", TAGS=="test10000", SYMLINK+="bad"
d35976c6
YW
2090KERNEL=="sda",\\
2091# comment in continuation
2092TAG+="hoge1",\\
2093 # space before comment
2094TAG+="hoge2",\\
2095# spaces before and after token are dropped
2096 TAG+="hoge3", \\
84a0819c
YW
2097\\
2098 \\
d35976c6
YW
2099TAG+="hoge4"
2100TAGS=="hoge1", TAGS=="hoge2", TAGS=="hoge3", TAGS=="hoge4", SYMLINK+="found"
84a0819c
YW
2101EOF
2102 },
2103 {
2104 desc => "continuations with empty line",
255c05b7
MW
2105 devices => [
2106 {
2107 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2108 exp_links => ["found"],
255c05b7
MW
2109 not_exp_name => "bad",
2110
2111 }],
84a0819c
YW
2112 rules => <<EOF
2113# empty line finishes continuation
2114KERNEL=="sda", TAG+="foo" \\
2115
2116KERNEL=="sdb", TAG+="hoge"
2117KERNEL=="sda", TAG+="aaa" \\
2118KERNEL=="sdb", TAG+="bbb"
2119TAGS=="foo", SYMLINK+="found"
2120TAGS=="aaa", SYMLINK+="bad"
255c05b7 2121 EOF
84a0819c
YW
2122 },
2123 {
2124 desc => "continuations with white only line",
255c05b7
MW
2125 devices => [
2126 {
2127 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda",
e62acc31 2128 exp_links => ["found"],
255c05b7
MW
2129 not_exp_name => "bad",
2130 }],
84a0819c
YW
2131 rules => <<EOF
2132# space only line finishes continuation
2133KERNEL=="sda", TAG+="foo" \\
2134 \t
2135KERNEL=="sdb", TAG+="hoge"
2136KERNEL=="sda", TAG+="aaa" \\
2137KERNEL=="sdb", TAG+="bbb"
2138TAGS=="foo", SYMLINK+="found"
2139TAGS=="aaa", SYMLINK+="bad"
4a0ec82d
MW
2140EOF
2141 },
2142 {
2143 desc => "multiple devices",
2144 devices => [
2145 {
2146 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2147 exp_links => ["part-1"],
2148 },
2149 {
2150 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2151 exp_links => ["part-5"],
2152 },
2153 {
2154 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2155 exp_links => ["part-6"],
2156 },
2157 {
2158 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2159 exp_links => ["part-7"],
2160 },
2161 {
2162 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2163 exp_links => ["part-8"],
2164 },
2165 {
2166 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2167 exp_links => ["part-9"],
2168 },
2169 {
2170 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2171 exp_links => ["part-10"],
2172 },
2173 ],
2174 rules => <<EOF
2175SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2176EOF
2177 },
2178 {
2179 desc => "multiple devices, same link name, positive prio",
2ab0a8d0 2180 repeat => 100,
4a0ec82d
MW
2181 devices => [
2182 {
2183 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2184 exp_links => ["part-1"],
2185 not_exp_links => ["partition"],
2186 },
2187 {
2188 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2189 exp_links => ["part-5"],
2190 not_exp_links => ["partition"],
2191 },
2192 {
2193 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2194 not_exp_links => ["partition"],
2195 exp_links => ["part-6"],
2196 },
2197 {
2198 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2199 exp_links => ["part-7", "partition"],
2200 },
2201 {
2202 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2203 not_exp_links => ["partition"],
2204 exp_links => ["part-8"],
2205 },
2206 {
2207 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2208 not_exp_links => ["partition"],
2209 exp_links => ["part-9"],
2210 },
2211 {
2212 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2213 not_exp_links => ["partition"],
2214 exp_links => ["part-10"],
2215 },
2216 ],
2217 rules => <<EOF
2218SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2219SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2220KERNEL=="*7", OPTIONS+="link_priority=10"
2221EOF
2222 },
2223 {
2224 desc => "multiple devices, same link name, negative prio",
2225 devices => [
2226 {
2227 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2228 exp_links => ["part-1"],
2229 not_exp_links => ["partition"],
2230 },
2231 {
2232 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2233 exp_links => ["part-5"],
2234 not_exp_links => ["partition"],
2235 },
2236 {
2237 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2238 not_exp_links => ["partition"],
2239 exp_links => ["part-6"],
2240 },
2241 {
2242 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2243 exp_links => ["part-7", "partition"],
2244 },
2245 {
2246 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2247 not_exp_links => ["partition"],
2248 exp_links => ["part-8"],
2249 },
2250 {
2251 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2252 not_exp_links => ["partition"],
2253 exp_links => ["part-9"],
2254 },
2255 {
2256 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2257 not_exp_links => ["partition"],
2258 exp_links => ["part-10"],
2259 },
2260 ],
2261 rules => <<EOF
2262SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2263SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2264KERNEL!="*7", OPTIONS+="link_priority=-10"
2265EOF
2266 },
2267 {
2268 desc => "multiple devices, same link name, positive prio, sleep",
2269 devices => [
2270 {
2271 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1",
2272 exp_links => ["part-1"],
2273 not_exp_links => ["partition"],
2274 },
2275 {
2276 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5",
2277 exp_links => ["part-5"],
2278 not_exp_links => ["partition"],
2279 },
2280 {
2281 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6",
2282 not_exp_links => ["partition"],
2283 exp_links => ["part-6"],
2284 },
2285 {
2286 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7",
2287 exp_links => ["part-7", "partition"],
2288 },
2289 {
2290 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8",
2291 not_exp_links => ["partition"],
2292 exp_links => ["part-8"],
2293 },
2294 {
2295 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9",
2296 not_exp_links => ["partition"],
2297 exp_links => ["part-9"],
2298 },
2299 {
2300 devpath => "/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10",
2301 not_exp_links => ["partition"],
2302 exp_links => ["part-10"],
2303 },
2304 ],
2305 sleep_us => 10000,
2306 rules => <<EOF
2307SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="part-%n"
2308SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sda?*", ENV{DEVTYPE}=="partition", SYMLINK+="partition"
2309KERNEL=="*7", OPTIONS+="link_priority=10"
ff94cec3 2310EOF
912541b0 2311 },
eb44d715
MW
2312 {
2313 desc => 'all_block_devs',
2314 generator => expect_for_some("\\/sda6\$", ["blockdev"]),
2315 repeat => 10,
2316 rules => <<EOF
2317SUBSYSTEM=="block", SUBSYSTEMS=="scsi", KERNEL=="sd*", SYMLINK+="blockdev"
2318KERNEL=="sda6", OPTIONS+="link_priority=10"
2319EOF
2320 }
a367f04e
GKH
2321);
2322
af7ee3ea
MW
2323sub create_rules {
2324 my ($rules) = @_;
a367f04e 2325
912541b0 2326 # create temporary rules
6ada823a 2327 system("mkdir", "-p", "$udev_rules_dir");
912541b0
KS
2328 open CONF, ">$udev_rules" || die "unable to create rules file: $udev_rules";
2329 print CONF $$rules;
2330 close CONF;
af7ee3ea
MW
2331}
2332
2333sub udev {
2334 my ($action, $devpath) = @_;
a367f04e 2335
912541b0 2336 if ($valgrind > 0) {
efc9f703 2337 return system("$udev_bin_valgrind $action $devpath");
333e07b7 2338 } elsif ($gdb > 0) {
efc9f703 2339 return system("$udev_bin_gdb $action $devpath");
587751eb 2340 } elsif ($strace > 0) {
efc9f703 2341 return system("$udev_bin_strace $action $devpath");
912541b0 2342 } else {
efc9f703 2343 return system("$udev_bin", "$action", "$devpath");
912541b0 2344 }
a367f04e
GKH
2345}
2346
e5fbfe0a 2347my $error = 0;
b95c4398 2348my $good = 0;
cbeb23d8 2349my $exp_good = 0;
72ffa78d 2350
b8669191 2351sub permissions_test {
912541b0 2352 my($rules, $uid, $gid, $mode) = @_;
b8669191 2353
912541b0
KS
2354 my $wrong = 0;
2355 my $userid;
2356 my $groupid;
9b434de1 2357
912541b0
KS
2358 $rules->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
2359 if ($1 ne "") {
2360 if (defined(getpwnam($1))) {
2361 $userid = int(getpwnam($1));
2362 } else {
2363 $userid = $1;
2364 }
2365 if ($uid != $userid) { $wrong = 1; }
2366 }
2367 if ($2 ne "") {
2368 if (defined(getgrnam($2))) {
2369 $groupid = int(getgrnam($2));
2370 } else {
2371 $groupid = $2;
2372 }
2373 if ($gid != $groupid) { $wrong = 1; }
2374 }
2375 if ($3 ne "") {
2376 if (($mode & 07777) != oct($3)) { $wrong = 1; };
2377 }
2378 if ($wrong == 0) {
2379 print "permissions: ok\n";
b95c4398 2380 $good++;
912541b0
KS
2381 } else {
2382 printf " expected permissions are: %s:%s:%#o\n", $1, $2, oct($3);
2383 printf " created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
2384 print "permissions: error\n";
2385 $error++;
2386 sleep(1);
2387 }
b8669191
GKH
2388}
2389
2390sub major_minor_test {
912541b0 2391 my($rules, $rdev) = @_;
b8669191 2392
912541b0
KS
2393 my $major = ($rdev >> 8) & 0xfff;
2394 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
2395 my $wrong = 0;
b8669191 2396
912541b0
KS
2397 $rules->{exp_majorminor} =~ m/^(.*):(.*)$/;
2398 if ($1 ne "") {
2399 if ($major != $1) { $wrong = 1; };
2400 }
2401 if ($2 ne "") {
2402 if ($minor != $2) { $wrong = 1; };
2403 }
2404 if ($wrong == 0) {
2405 print "major:minor: ok\n";
b95c4398 2406 $good++;
912541b0
KS
2407 } else {
2408 printf " expected major:minor is: %i:%i\n", $1, $2;
2409 printf " created major:minor is : %i:%i\n", $major, $minor;
2410 print "major:minor: error\n";
2411 $error++;
2412 sleep(1);
2413 }
b8669191
GKH
2414}
2415
6ada823a 2416sub udev_setup {
f1cb0860 2417 system("umount \"$udev_tmpfs\" 2>/dev/null");
21d9e3f3
EV
2418 rmdir($udev_tmpfs);
2419 mkdir($udev_tmpfs) || die "unable to create udev_tmpfs: $udev_tmpfs\n";
110a1320
EV
2420
2421 if (system("mount", "-o", "rw,mode=755,nosuid,noexec", "-t", "tmpfs", "tmpfs", $udev_tmpfs)) {
2422 warn "unable to mount tmpfs";
2423 return 0;
2424 }
21d9e3f3 2425
6ada823a
KS
2426 mkdir($udev_dev) || die "unable to create udev_dev: $udev_dev\n";
2427 # setting group and mode of udev_dev ensures the tests work
912541b0 2428 # even if the parent directory has setgid bit enabled.
6ada823a
KS
2429 chown (0, 0, $udev_dev) || die "unable to chown $udev_dev\n";
2430 chmod (0755, $udev_dev) || die "unable to chmod $udev_dev\n";
110a1320
EV
2431
2432 if (system("mknod", $udev_dev . "/null", "c", "1", "3")) {
2433 warn "unable to create $udev_dev/null";
2434 return 0;
2435 }
6ada823a 2436
dbfbc6c4
AB
2437 # check if we are permitted to create block device nodes
2438 my $block_device_filename = $udev_dev . "/sda";
2439 if (system("mknod", $block_device_filename, "b", "8", "0")) {
2440 warn "unable to create $block_device_filename";
2441 return 0;
2442 }
2443 unlink $block_device_filename;
2444
21d9e3f3 2445 system("cp", "-r", "test/sys/", $udev_sys) && die "unable to copy test/sys";
2ce8d27b 2446
6ada823a 2447 system("rm", "-rf", "$udev_run");
110a1320 2448
1e5548c0
AB
2449 if (!mkdir($udev_run)) {
2450 warn "unable to create directory $udev_run";
2451 return 0;
2452 }
2453
110a1320 2454 return 1;
ff94cec3
EK
2455}
2456
f0dccf01
MW
2457sub get_devnode {
2458 my ($device) = @_;
2459 my $devnode;
2460
2461 if (defined($device->{devnode})) {
2462 $devnode = "$udev_dev/$device->{devnode}";
2463 } else {
2464 $devnode = "$device->{devpath}";
2465 $devnode =~ s!.*/!$udev_dev/!;
2466 }
2467 return $devnode;
2468}
2469
2470sub check_devnode {
2471 my ($device) = @_;
2472 my $devnode = get_devnode($device);
2473
2474 my @st = lstat("$devnode");
2475 if (! (-b _ || -c _)) {
2476 print "add $devnode: error\n";
2477 system("tree", "$udev_dev");
2478 $error++;
2479 return undef;
2480 }
2481
2482 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
2483 $atime, $mtime, $ctime, $blksize, $blocks) = @st;
2484
2485 if (defined($device->{exp_perms})) {
2486 permissions_test($device, $uid, $gid, $mode);
2487 }
2488 if (defined($device->{exp_majorminor})) {
2489 major_minor_test($device, $rdev);
2490 }
2491 print "add $devnode: ok\n";
b95c4398 2492 $good++;
f0dccf01
MW
2493 return $devnode;
2494}
2495
e62acc31
MW
2496sub get_link_target {
2497 my ($link) = @_;
09a4062d 2498
e62acc31
MW
2499 my $cwd = getcwd();
2500 my $dir = "$udev_dev/$link";
2501 my $tgt = readlink("$udev_dev/$link");
2502 $dir =~ s!/[^/]*$!!;
2503 $tgt = abs_path("$dir/$tgt");
2504 $tgt =~ s!^$cwd/!!;
2505 return $tgt;
2506}
f0dccf01 2507
e62acc31
MW
2508sub check_link_add {
2509 my ($link, $devnode, $err_expected) = @_;
f0dccf01 2510
e62acc31 2511 my @st = lstat("$udev_dev/$link");
997683c8 2512 if (-l _) {
e62acc31 2513 my $tgt = get_link_target($link);
997683c8
MW
2514
2515 if ($tgt ne $devnode) {
e62acc31 2516 print "symlink $link: error, found -> $tgt\n";
997683c8
MW
2517 $error++;
2518 system("tree", "$udev_dev");
2519 } else {
e62acc31 2520 print "symlink $link: ok\n";
b95c4398 2521 $good++;
997683c8 2522 }
912541b0 2523 } else {
e62acc31
MW
2524 print "symlink $link: error";
2525 if ($err_expected) {
912541b0 2526 print " as expected\n";
b95c4398 2527 $good++;
912541b0
KS
2528 } else {
2529 print "\n";
6ada823a 2530 system("tree", "$udev_dev");
912541b0
KS
2531 print "\n";
2532 $error++;
2533 sleep(1);
2534 }
2535 }
255c05b7 2536}
a367f04e 2537
e62acc31
MW
2538sub check_link_nonexistent {
2539 my ($link, $devnode, $err_expected) = @_;
2540
2541 if ((-e "$udev_dev/$link") || (-l "$udev_dev/$link")) {
2542 my $tgt = get_link_target($link);
2543
2544 if ($tgt ne $devnode) {
2545 print "nonexistent: '$link' points to other device (ok)\n";
b95c4398 2546 $good++;
e62acc31
MW
2547 } else {
2548 print "nonexistent: error \'$link\' should not be there";
2549 if ($err_expected) {
2550 print " (as expected)\n";
b95c4398 2551 $good++;
e62acc31
MW
2552 } else {
2553 print "\n";
2554 system("tree", "$udev_dev");
2555 print "\n";
2556 $error++;
2557 sleep(1);
2558 }
2559 }
2560 } else {
2561 print "nonexistent $link: ok\n";
b95c4398 2562 $good++;
e62acc31
MW
2563 }
2564}
2565
2566sub check_add {
2567 my ($device) = @_;
2568 my $devnode = check_devnode($device);
2569
2570 if (defined($device->{exp_links})) {
2571 foreach my $link (@{$device->{exp_links}}) {
2572 check_link_add($link, $devnode,
2573 $device->{exp_add_error});
2574 }
2575 }
2576 if (defined $device->{not_exp_links}) {
2577 foreach my $link (@{$device->{not_exp_links}}) {
2578 check_link_nonexistent($link, $devnode,
2579 $device->{exp_nodev_error});
2580 }
2581 }
2582}
2583
f0dccf01
MW
2584sub check_remove_devnode {
2585 my ($device) = @_;
2586 my $devnode = get_devnode($device);
2587
2588 if (-e "$devnode") {
2589 print "remove $devnode: error";
2590 print "\n";
2591 system("tree", "$udev_dev");
2592 print "\n";
2593 $error++;
2594 sleep(1);
2595 } else {
2596 print "remove $devnode: ok\n";
b95c4398 2597 $good++;
f0dccf01
MW
2598 }
2599}
2600
e62acc31
MW
2601sub check_link_remove {
2602 my ($link, $err_expected) = @_;
f0dccf01 2603
e62acc31
MW
2604 if ((-e "$udev_dev/$link") ||
2605 (-l "$udev_dev/$link")) {
2606 print "remove $link: error";
2607 if ($err_expected) {
912541b0 2608 print " as expected\n";
b95c4398 2609 $good++;
912541b0
KS
2610 } else {
2611 print "\n";
6ada823a 2612 system("tree", "$udev_dev");
912541b0
KS
2613 print "\n";
2614 $error++;
2615 sleep(1);
2616 }
2617 } else {
e62acc31 2618 print "remove $link: ok\n";
b95c4398 2619 $good++;
e62acc31
MW
2620 }
2621}
2622
2623sub check_remove {
2624 my ($device) = @_;
2625
2626 check_remove_devnode($device);
2627
2628 return if (!defined($device->{exp_links}));
2629
2630 foreach my $link (@{$device->{exp_links}}) {
2631 check_link_remove($link, $device->{exp_rem_error});
255c05b7
MW
2632 }
2633}
2634
09a4062d
MW
2635sub run_udev {
2636 my ($action, $dev, $sleep_us, $sema) = @_;
2637
2638 # Notify main process that this worker has started
2639 $sema->op(0, 1, 0);
2640
2641 # Wait for start
2642 $sema->op(0, 0, 0);
2643 usleep($sleep_us) if defined ($sleep_us);
2644 my $rc = udev($action, $dev->{devpath});
2645 exit $rc;
2646}
2647
2648sub fork_and_run_udev {
2649 my ($action, $rules, $sema) = @_;
2650 my @devices = @{$rules->{devices}};
2651 my $dev;
2652 my $k = 0;
2653
2654 $sema->setval(0, 1);
2655 foreach $dev (@devices) {
2656 my $pid = fork();
2657
2658 if (!$pid) {
2659 run_udev($action, $dev,
2660 defined($rules->{sleep_us}) ? $k * $rules->{sleep_us} : undef,
2661 $sema);
2662 } else {
2663 $dev->{pid} = $pid;
2664 }
2665 $k++;
2666 }
2667
2668 # This operation waits for all workers to become ready, and
2669 # starts them off when that's the case.
2670 $sema->op(0, -($#devices + 2), 0);
2671
2672 foreach $dev (@devices) {
2673 my $rc;
2674 my $pid;
2675
2676 $pid = waitpid($dev->{pid}, 0);
2677 if ($pid == -1) {
2678 print "error waiting for pid dev->{pid}\n";
09a4062d
MW
2679 }
2680 if (WIFEXITED($?)) {
2681 $rc = WEXITSTATUS($?);
2682
2683 if ($rc) {
2684 print "$udev_bin $action for $dev->{devpath} failed with code $rc\n";
2685 $error += 1;
b95c4398
MW
2686 } else {
2687 $good++;
09a4062d
MW
2688 }
2689 }
2690 }
2691}
2692
255c05b7 2693sub run_test {
09a4062d 2694 my ($rules, $number, $sema) = @_;
255c05b7 2695 my $rc;
eb44d715 2696 my @devices;
cbeb23d8
MW
2697 my $ntests;
2698 my $cur_good = $good;
2699 my $cur_error = $error;
eb44d715
MW
2700
2701 if (!defined $rules->{devices}) {
2702 $rules->{devices} = all_block_devs($rules->{generator});
2703 }
2704 @devices = @{$rules->{devices}};
cbeb23d8
MW
2705 # For each device: exit status and devnode test for add & remove
2706 $ntests += 4 * ($#devices + 1);
255c05b7 2707
cbeb23d8
MW
2708 foreach my $dev (@devices) {
2709 $ntests += 2 * ($#{$dev->{exp_links}} + 1)
2710 + ($#{$dev->{not_exp_links}} + 1)
2711 + (defined $dev->{exp_perms} ? 1 : 0)
2712 + (defined $dev->{exp_majorminor} ? 1 : 0);
2713 }
2714 if (defined $rules->{repeat}) {
2715 $ntests *= $rules->{repeat};
2716 }
2717 $exp_good += $ntests;
255c05b7 2718 print "TEST $number: $rules->{desc}\n";
af7ee3ea 2719 create_rules(\$rules->{rules});
09a4062d 2720
2ab0a8d0 2721 REPEAT:
09a4062d 2722 fork_and_run_udev("add", $rules, $sema);
255c05b7
MW
2723
2724 foreach my $dev (@devices) {
2725 check_add($dev);
2726 }
2727
2728 if (defined($rules->{option}) && $rules->{option} eq "keep") {
2729 print "\n\n";
2730 return;
2731 }
2732
09a4062d
MW
2733 fork_and_run_udev("remove", $rules, $sema);
2734
255c05b7
MW
2735 foreach my $dev (@devices) {
2736 check_remove($dev);
912541b0 2737 }
0345b862 2738
2ab0a8d0
MW
2739 if (defined($rules->{repeat}) && --($rules->{repeat}) > 0) {
2740 goto REPEAT;
2741 }
cbeb23d8
MW
2742 printf "TEST $number: errors: %d good: %d/%d\n\n", $error-$cur_error,
2743 $good-$cur_good, $ntests;
9b434de1 2744
912541b0 2745 if (defined($rules->{option}) && $rules->{option} eq "clean") {
6ada823a 2746 udev_setup();
912541b0 2747 }
0345b862 2748
a367f04e
GKH
2749}
2750
abb9cc50
DS
2751sub cleanup {
2752 system("rm", "-rf", "$udev_run");
2753 system("umount", "$udev_tmpfs");
2754 rmdir($udev_tmpfs);
2755}
2756
800ab95b
GKH
2757# only run if we have root permissions
2758# due to mknod restrictions
2759if (!($<==0)) {
912541b0 2760 print "Must have root permissions to run properly.\n";
4d55fc5b 2761 exit($EXIT_TEST_SKIP);
800ab95b
GKH
2762}
2763
e4d214ef
DC
2764# skip the test when running in a chroot
2765system("systemd-detect-virt", "-r", "-q");
2766if ($? >> 8 == 0) {
ce5fcc69
ZJS
2767 print "Running in a chroot, skipping the test.\n";
2768 exit($EXIT_TEST_SKIP);
e4d214ef
DC
2769}
2770
110a1320
EV
2771if (!udev_setup()) {
2772 warn "Failed to set up the environment, skipping the test";
abb9cc50 2773 cleanup();
110a1320
EV
2774 exit($EXIT_TEST_SKIP);
2775}
2776
7935dae5 2777if (system($udev_bin, "check")) {
110a1320 2778 warn "$udev_bin failed to set up the environment, skipping the test";
abb9cc50 2779 cleanup();
110a1320
EV
2780 exit($EXIT_TEST_SKIP);
2781}
2e317184
GKH
2782
2783my $test_num = 1;
e08109cb 2784my @list;
2e317184 2785
e08109cb 2786foreach my $arg (@ARGV) {
912541b0
KS
2787 if ($arg =~ m/--valgrind/) {
2788 $valgrind = 1;
2789 printf("using valgrind\n");
333e07b7
TG
2790 } elsif ($arg =~ m/--gdb/) {
2791 $gdb = 1;
2792 printf("using gdb\n");
587751eb
ZJS
2793 } elsif ($arg =~ m/--strace/) {
2794 $strace = 1;
2795 printf("using strace\n");
912541b0
KS
2796 } else {
2797 push(@list, $arg);
2798 }
e08109cb 2799}
09a4062d 2800my $sema = IPC::Semaphore->new(IPC_PRIVATE, 1, S_IRUSR | S_IWUSR | IPC_CREAT);
e08109cb
KS
2801
2802if ($list[0]) {
912541b0
KS
2803 foreach my $arg (@list) {
2804 if (defined($tests[$arg-1]->{desc})) {
2805 print "udev-test will run test number $arg:\n\n";
09a4062d 2806 run_test($tests[$arg-1], $arg, $sema);
912541b0
KS
2807 } else {
2808 print "test does not exist.\n";
2809 }
2810 }
2e317184 2811} else {
912541b0
KS
2812 # test all
2813 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
2e317184 2814
912541b0 2815 foreach my $rules (@tests) {
09a4062d 2816 run_test($rules, $test_num, $sema);
912541b0
KS
2817 $test_num++;
2818 }
2e317184
GKH
2819}
2820
09a4062d 2821$sema->remove;
cbeb23d8 2822print "$error errors occurred. $good/$exp_good good results.\n\n";
a367f04e 2823
abb9cc50 2824cleanup();
a367f04e 2825
034b37c8 2826if ($error > 0) {
d3fc8bf4 2827 exit(1);
034b37c8
AJ
2828}
2829exit(0);