]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
[PATCH] add a "old style" SYSFS_attribute test to udev-test.pl
[thirdparty/systemd.git] / test / udev-test.pl
CommitLineData
a367f04e
GKH
1#!/usr/bin/perl
2
3# udev-test
4#
5# Provides automated testing of the udev binary.
6# The whole test is self contained in this file, except the matching sysfs tree.
7# Simply extend the @tests array, to add a new test variant.
8#
9# Every test is driven by its own temporary config file.
10# This program prepares the environment, creates the config and calls udev.
11#
12# udev reads the config, looks at the provided sysfs and
13# first creates and then removes the device node.
14# After creation and removal the result is checked against the
15# expected value and the result is printed.
16#
17# happy testing,
18# Kay Sievers <kay.sievers@vrfy.org>, 2003
19
20
21use warnings;
22use strict;
23
f8f00338 24my $PWD = $ENV{PWD};
a367f04e
GKH
25my $sysfs = "sys/";
26my $udev_bin = "../udev";
27my $udev_root = "udev-root/"; # !!! directory will be removed !!!
36043f84 28my $udev_db = ".udev.tdb";
a367f04e 29my $perm = "udev.permissions";
72ffa78d
GKH
30my $main_conf = "udev-test.conf";
31my $conf_tmp = "udev-test.rules";
a367f04e
GKH
32
33
34my @tests = (
35 {
36 desc => "label test of scsi disc",
37 subsys => "block",
38 devpath => "block/sda",
39 expected => "boot_disk" ,
40 conf => <<EOF
16378373 41BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
ac28b86d 42KERNEL="ttyUSB0", NAME="visor"
a367f04e
GKH
43EOF
44 },
45 {
46 desc => "label test of scsi partition",
47 subsys => "block",
48 devpath => "block/sda/sda1",
49 expected => "boot_disk1" ,
50 conf => <<EOF
16378373 51BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
83be97ba
KS
52EOF
53 },
54 {
55 desc => "label test of pattern match",
56 subsys => "block",
57 devpath => "block/sda/sda1",
58 expected => "boot_disk1" ,
59 conf => <<EOF
16378373
KS
60BUS="scsi", SYSFS{vendor}="?IBM-ESXS", NAME="boot_disk%n-1"
61BUS="scsi", SYSFS{vendor}="IBM-ESXS?", NAME="boot_disk%n-2"
62BUS="scsi", SYSFS{vendor}="IBM-ES??", NAME="boot_disk%n"
63BUS="scsi", SYSFS{vendor}="IBM-ESXSS", NAME="boot_disk%n-3"
358c8c20
GKH
64EOF
65 },
66 {
67 desc => "label test of multiple sysfs files",
68 subsys => "block",
69 devpath => "block/sda/sda1",
70 expected => "boot_disk1" ,
71 conf => <<EOF
16378373
KS
72BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW !#", NAME="boot_diskX%n"
73BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW !#", NAME="boot_disk%n"
358c8c20
GKH
74EOF
75 },
76 {
77 desc => "label test of max sysfs files",
78 subsys => "block",
79 devpath => "block/sda/sda1",
80 expected => "boot_disk1" ,
81 conf => <<EOF
16378373
KS
82BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW !#", SYSFS{scsi_level}="4", SYSFS{rev}="B245", SYSFS{type}="2", SYSFS{queue_depth}="32", NAME="boot_diskXX%n"
83BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW !#", SYSFS{scsi_level}="4", SYSFS{rev}="B245", SYSFS{type}="0", NAME="boot_disk%n"
0db6d4cc
KS
84EOF
85 },
86 {
9f1da361 87 desc => "catch device by *",
0db6d4cc
KS
88 subsys => "tty",
89 devpath => "class/tty/ttyUSB0",
90 expected => "visor/0" ,
91 conf => <<EOF
ac28b86d 92KERNEL="ttyUSB*", NAME="visor/%n"
2e317184
GKH
93EOF
94 },
95 {
96 desc => "catch device by * - take 2",
97 subsys => "tty",
98 devpath => "class/tty/ttyUSB0",
99 expected => "visor/0" ,
100 conf => <<EOF
101KERNEL="*USB1", NAME="bad"
102KERNEL="*USB0", NAME="visor/%n"
9f1da361
KS
103EOF
104 },
105 {
106 desc => "catch device by ?",
107 subsys => "tty",
108 devpath => "class/tty/ttyUSB0",
109 expected => "visor/0" ,
110 conf => <<EOF
ac28b86d
KS
111KERNEL="ttyUSB??*", NAME="visor/%n-1"
112KERNEL="ttyUSB??", NAME="visor/%n-2"
113KERNEL="ttyUSB?", NAME="visor/%n"
9f1da361
KS
114EOF
115 },
116 {
117 desc => "catch device by character class",
118 subsys => "tty",
119 devpath => "class/tty/ttyUSB0",
120 expected => "visor/0" ,
121 conf => <<EOF
ac28b86d
KS
122KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
123KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
124KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
a367f04e
GKH
125EOF
126 },
127 {
128 desc => "replace kernel name",
129 subsys => "tty",
130 devpath => "class/tty/ttyUSB0",
131 expected => "visor" ,
132 conf => <<EOF
ac28b86d 133KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
134EOF
135 },
136 {
137 desc => "Handle comment lines in config file (and replace kernel name)",
138 subsys => "tty",
139 devpath => "class/tty/ttyUSB0",
140 expected => "visor" ,
141 conf => <<EOF
142# this is a comment
ac28b86d 143KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
144
145EOF
146 },
147 {
148 desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
149 subsys => "tty",
150 devpath => "class/tty/ttyUSB0",
151 expected => "visor" ,
152 conf => <<EOF
153 # this is a comment with whitespace before the comment
ac28b86d 154KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
155
156EOF
157 },
158 {
159 desc => "Handle empty lines in config file (and replace kernel name)",
160 subsys => "tty",
161 devpath => "class/tty/ttyUSB0",
162 expected => "visor" ,
163 conf => <<EOF
164
ac28b86d 165KERNEL="ttyUSB0", NAME="visor"
281ff00a 166
5499d319
KS
167EOF
168 },
169 {
170 desc => "subdirectory handling",
171 subsys => "tty",
172 devpath => "class/tty/ttyUSB0",
173 expected => "sub/direct/ory/visor" ,
174 conf => <<EOF
ac28b86d 175KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
a367f04e
GKH
176EOF
177 },
178 {
179 desc => "place on bus of scsi partition",
180 subsys => "block",
181 devpath => "block/sda/sda3",
182 expected => "first_disk3" ,
183 conf => <<EOF
ac28b86d 184BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
a367f04e
GKH
185EOF
186 },
187 {
188 desc => "test NAME substitution chars",
189 subsys => "block",
190 devpath => "block/sda/sda3",
191 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
192 conf => <<EOF
ac28b86d 193BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
59525115
GKH
194EOF
195 },
196 {
197 desc => "old style SYSFS_ attribute",
198 subsys => "block",
199 devpath => "block/sda",
200 expected => "good" ,
201 conf => <<EOF
202BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="good"
a27cd06c
KS
203EOF
204 },
205 {
206 desc => "sustitution of sysfs value (%s{file})",
207 subsys => "block",
208 devpath => "block/sda",
209 expected => "disk-IBM-ESXS-sda" ,
210 conf => <<EOF
16378373 211BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="disk-%s{vendor}-%k"
a27cd06c 212KERNEL="ttyUSB0", NAME="visor"
a367f04e
GKH
213EOF
214 },
215 {
ac28b86d 216 desc => "program result substitution",
a367f04e
GKH
217 subsys => "block",
218 devpath => "block/sda/sda3",
219 expected => "special-device-3" ,
220 conf => <<EOF
ac28b86d
KS
221BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
222BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
223BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
224BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
225BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
f3b04a2e
GKH
226EOF
227 },
228 {
ac28b86d 229 desc => "program result substitution",
f3b04a2e
GKH
230 subsys => "block",
231 devpath => "block/sda/sda3",
232 expected => "test-0:0:0:0" ,
233 conf => <<EOF
ac28b86d 234BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
8ffb636f
KS
235EOF
236 },
237 {
238 desc => "program with escaped format char (tricky: callout returns format char!)",
239 subsys => "block",
240 devpath => "block/sda/sda3",
241 expected => "escape-3" ,
242 conf => <<EOF
243BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
b1c5e333
KS
244EOF
245 },
246 {
ac28b86d 247 desc => "program result substitution (numbered part of)",
b1c5e333
KS
248 subsys => "block",
249 devpath => "block/sda/sda3",
250 expected => "link1" ,
251 conf => <<EOF
88f09368 252BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
8ff8bbba
GKH
253EOF
254 },
255 {
ac28b86d 256 desc => "invalid program for device with no bus",
8ff8bbba
GKH
257 subsys => "tty",
258 devpath => "class/tty/console",
259 expected => "TTY" ,
260 conf => <<EOF
ac28b86d
KS
261BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
262KERNEL="console", NAME="TTY"
1d936fbc
GKH
263EOF
264 },
265 {
ac28b86d 266 desc => "valid program for device with no bus",
1d936fbc
GKH
267 subsys => "tty",
268 devpath => "class/tty/console",
269 expected => "foo" ,
270 conf => <<EOF
ac28b86d
KS
271PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
272KERNEL="console", NAME="TTY"
64682333
GKH
273EOF
274 },
275 {
276 desc => "invalid label for device with no bus",
277 subsys => "tty",
278 devpath => "class/tty/console",
279 expected => "TTY" ,
280 conf => <<EOF
16378373 281BUS="foo", SYSFS{dev}="5:1", NAME="foo"
ac28b86d 282KERNEL="console", NAME="TTY"
64682333
GKH
283EOF
284 },
285 {
286 desc => "valid label for device with no bus",
287 subsys => "tty",
288 devpath => "class/tty/console",
289 expected => "foo" ,
290 conf => <<EOF
16378373 291SYSFS{dev}="5:1", NAME="foo"
ac28b86d 292KERNEL="console", NAME="TTY"
772558f4
GKH
293EOF
294 },
295 {
ac28b86d 296 desc => "program and bus type match",
772558f4
GKH
297 subsys => "block",
298 devpath => "block/sda",
299 expected => "scsi-0:0:0:0" ,
300 conf => <<EOF
ac28b86d
KS
301BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
302BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
303BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
3d150dfb
KS
304EOF
305 },
306 {
307 desc => "symlink creation (same directory)",
308 subsys => "tty",
309 devpath => "class/tty/ttyUSB0",
310 expected => "visor0" ,
311 conf => <<EOF
ac28b86d 312KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
3d150dfb
KS
313EOF
314 },
315 {
316 desc => "symlink creation (relative link back)",
317 subsys => "block",
318 devpath => "block/sda/sda2",
319 expected => "1/2/a/b/symlink" ,
320 conf => <<EOF
16378373 321BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
3d150dfb
KS
322EOF
323 },
324 {
325 desc => "symlink creation (relative link forward)",
326 subsys => "block",
327 devpath => "block/sda/sda2",
328 expected => "1/2/symlink" ,
329 conf => <<EOF
16378373 330BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
3d150dfb
KS
331EOF
332 },
333 {
334 desc => "symlink creation (relative link back and forward)",
335 subsys => "block",
336 devpath => "block/sda/sda2",
337 expected => "1/2/c/d/symlink" ,
338 conf => <<EOF
16378373 339BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
4763256c
KS
340EOF
341 },
342 {
343 desc => "multiple symlinks",
344 subsys => "tty",
345 devpath => "class/tty/ttyUSB0",
346 expected => "second-0" ,
347 conf => <<EOF
ac28b86d 348KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
724257d9
GKH
349EOF
350 },
351 {
50e5de03
KS
352 desc => "create all possible partitions",
353 subsys => "block",
354 devpath => "block/sda",
355 expected => "boot_disk15" ,
356 conf => <<EOF
16378373 357BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME{all_partitions}="boot_disk"
50e5de03
KS
358EOF
359 },
360 {
361 desc => "sysfs parent hierarchy",
724257d9
GKH
362 subsys => "tty",
363 devpath => "class/tty/ttyUSB0",
364 expected => "visor" ,
365 conf => <<EOF
16378373 366SYSFS{idProduct}="2008", NAME="visor"
f0142622
GKH
367EOF
368 },
369 {
370 desc => "name test with ! in the name",
371 subsys => "block",
372 devpath => "block/rd!c0d0",
373 expected => "rd/c0d0" ,
374 conf => <<EOF
375BUS="scsi", NAME="%k"
376KERNEL="ttyUSB0", NAME="visor"
b9fc973b
GKH
377EOF
378 },
379 {
380 desc => "name test with ! in the name, but no matching rule",
381 subsys => "block",
382 devpath => "block/rd!c0d0",
383 expected => "rd/c0d0" ,
384 conf => <<EOF
385KERNEL="ttyUSB0", NAME="visor"
93656247
PM
386EOF
387 },
388 {
389 desc => "ID rule",
390 subsys => "block",
391 devpath => "block/sda",
392 expected => "scsi-0:0:0:0",
393 conf => <<EOF
394BUS="usb", ID="0:0:0:0", NAME="not-scsi"
395BUS="scsi", ID="0:0:0:1", NAME="no-match"
396BUS="scsi", ID=":0", NAME="short-id"
397BUS="scsi", ID="/0:0:0:0", NAME="no-match"
398BUS="scsi", ID="0:0:0:0", NAME="scsi-0:0:0:0"
399EOF
400 },
401 {
402 desc => "ID wildcard all",
403 subsys => "block",
404 devpath => "block/sda",
405 expected => "scsi-0:0:0:0",
406 conf => <<EOF
407BUS="scsi", ID="*:1", NAME="no-match"
408BUS="scsi", ID="*:0:1", NAME="no-match"
409BUS="scsi", ID="*:0:0:1", NAME="no-match"
410BUS="scsi", ID="*", NAME="scsi-0:0:0:0"
411BUS="scsi", ID="0:0:0:0", NAME="bad"
412EOF
413 },
414 {
415 desc => "ID wildcard partial",
416 subsys => "block",
417 devpath => "block/sda",
418 expected => "scsi-0:0:0:0",
419 conf => <<EOF
420BUS="scsi", ID="*:0", NAME="scsi-0:0:0:0"
421BUS="scsi", ID="0:0:0:0", NAME="bad"
422EOF
423 },
424 {
425 desc => "ID wildcard partial 2",
426 subsys => "block",
427 devpath => "block/sda",
428 expected => "scsi-0:0:0:0",
429 conf => <<EOF
430BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
431BUS="scsi", ID="0:0:0:0", NAME="bad"
a367f04e
GKH
432EOF
433 },
434);
435
436# set env
437$ENV{UDEV_TEST} = "yes";
438$ENV{SYSFS_PATH} = $sysfs;
72ffa78d 439$ENV{UDEV_CONFIG_FILE} = $main_conf;
a367f04e
GKH
440
441
442sub udev {
443 my ($action, $subsys, $devpath, $config) = @_;
444
445 $ENV{DEVPATH} = $devpath;
a367f04e
GKH
446
447 # create temporary config
448 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
449 print CONF $$config;
450 close CONF;
451
452 $ENV{ACTION} = $action;
453 system("$udev_bin $subsys");
454}
455
e5fbfe0a 456my $error = 0;
72ffa78d 457
2e317184
GKH
458sub run_test {
459 my ($config, $number) = @_;
460
461 print "TEST $number: $config->{desc}\n";
ac28b86d 462 print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
a367f04e
GKH
463
464 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
465 if (-e "$PWD/$udev_root$config->{expected}") {
466 print "add: ok ";
467 } else {
468 print "add: error\n";
469 system("tree $udev_root");
470 print "\n";
471 $error++;
a367f04e
GKH
472 }
473
474 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
3d150dfb
KS
475 if ((-e "$PWD/$udev_root$config->{expected}") ||
476 (-l "$PWD/$udev_root$config->{expected}")) {
a367f04e
GKH
477 print "remove: error\n\n";
478 system("tree $udev_root");
479 $error++;
480 } else {
481 print "remove: ok\n\n";
482 }
483}
484
2e317184
GKH
485# prepare
486system("rm -rf $udev_root");
487mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
488
489# create initial config file
490open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
491print CONF "udev_root=\"$udev_root\"\n";
492print CONF "udev_db=\"$udev_db\"\n";
493print CONF "udev_rules=\"$conf_tmp\"\n";
494print CONF "udev_permissions=\"$perm\"\n";
495close CONF;
496
497my $test_num = 1;
498
499if ($ARGV[0]) {
500 # only run one test
501 $test_num = $ARGV[0];
502 print "udev-test will run test number $test_num only\n";
503
b9fc973b 504 run_test($tests[$test_num-1], $test_num);
2e317184
GKH
505} else {
506 # test all
507 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
508
509 foreach my $config (@tests) {
510 run_test($config, $test_num);
511 $test_num++;
512
513 }
514}
515
a367f04e
GKH
516print "$error errors occured\n\n";
517
518# cleanup
36043f84 519unlink($udev_db);
a367f04e
GKH
520system("rm -rf $udev_root");
521unlink($conf_tmp);
72ffa78d 522unlink($main_conf);
a367f04e 523