chiark / gitweb /
[PATCH] cleanup callout fork
[elogind.git] / test / udev-test.pl
1 #!/usr/bin/perl
2
3 # udev-test
4 #
5 # Provides automated testing of the udev binary.
6 # The whole test is self contained in this file, except the matching sysfs tree.
7 # Simply extend the @tests array, to add a new test variant.
8 #
9 # Every test is driven by its own temporary config file.
10 # This program prepares the environment, creates the config and calls udev.
11 #
12 # udev 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
21 use warnings;
22 use strict;
23
24 my $PWD = $ENV{PWD};
25 my $sysfs     = "sys/";
26 my $udev_bin  = "../udev";
27 my $udev_root = "udev-root/"; # !!! directory will be removed !!!
28 my $udev_db   = ".udev.tdb";
29 my $perm      = "udev.permissions";
30 my $main_conf = "udev-test.conf";
31 my $conf_tmp  = "udev-test.rules";
32
33
34 my @tests = (
35         {
36                 desc     => "label test of scsi disc",
37                 subsys   => "block",
38                 devpath  => "block/sda",
39                 expected => "boot_disk" ,
40                 conf     => <<EOF
41 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
42 KERNEL="ttyUSB0", NAME="visor"
43 EOF
44         },
45         {
46                 desc     => "label test of scsi partition",
47                 subsys   => "block",
48                 devpath  => "block/sda/sda1",
49                 expected => "boot_disk1" ,
50                 conf     => <<EOF
51 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
52 EOF
53         },
54         {
55                 desc     => "label test of pattern match",
56                 subsys   => "block",
57                 devpath  => "block/sda/sda1",
58                 expected => "boot_disk1" ,
59                 conf     => <<EOF
60 BUS="scsi", SYSFS{vendor}="?IBM-ESXS", NAME="boot_disk%n-1"
61 BUS="scsi", SYSFS{vendor}="IBM-ESXS?", NAME="boot_disk%n-2"
62 BUS="scsi", SYSFS{vendor}="IBM-ES??", NAME="boot_disk%n"
63 BUS="scsi", SYSFS{vendor}="IBM-ESXSS", NAME="boot_disk%n-3"
64 EOF
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
72 BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW   !#", NAME="boot_diskX%n"
73 BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW    !#", NAME="boot_disk%n"
74 EOF
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
82 BUS="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"
83 BUS="scsi", SYSFS{vendor}="IBM-ESXS", SYSFS{model}="ST336605LW    !#", SYSFS{scsi_level}="4", SYSFS{rev}="B245", SYSFS{type}="0", NAME="boot_disk%n"
84 EOF
85         },
86         {
87                 desc     => "catch device by *",
88                 subsys   => "tty",
89                 devpath  => "class/tty/ttyUSB0",
90                 expected => "visor/0" ,
91                 conf     => <<EOF
92 KERNEL="ttyUSB*", NAME="visor/%n"
93 EOF
94         },
95         {
96                 desc     => "catch device by * - take 2",
97                 subsys   => "tty",
98                 devpath  => "class/tty/ttyUSB0",
99                 expected => "visor/0" ,
100                 conf     => <<EOF
101 KERNEL="*USB1", NAME="bad"
102 KERNEL="*USB0", NAME="visor/%n"
103 EOF
104         },
105         {
106                 desc     => "catch device by ?",
107                 subsys   => "tty",
108                 devpath  => "class/tty/ttyUSB0",
109                 expected => "visor/0" ,
110                 conf     => <<EOF
111 KERNEL="ttyUSB??*", NAME="visor/%n-1"
112 KERNEL="ttyUSB??", NAME="visor/%n-2"
113 KERNEL="ttyUSB?", NAME="visor/%n"
114 EOF
115         },
116         {
117                 desc     => "catch device by character class",
118                 subsys   => "tty",
119                 devpath  => "class/tty/ttyUSB0",
120                 expected => "visor/0" ,
121                 conf     => <<EOF
122 KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
123 KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
124 KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
125 EOF
126         },
127         {
128                 desc     => "replace kernel name",
129                 subsys   => "tty",
130                 devpath  => "class/tty/ttyUSB0",
131                 expected => "visor" ,
132                 conf     => <<EOF
133 KERNEL="ttyUSB0", NAME="visor"
134 EOF
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
143 KERNEL="ttyUSB0", NAME="visor"
144
145 EOF
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 
154 KERNEL="ttyUSB0", NAME="visor"
155
156 EOF
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
165 KERNEL="ttyUSB0", NAME="visor"
166
167 EOF
168         },
169         {
170                 desc     => "subdirectory handling",
171                 subsys   => "tty",
172                 devpath  => "class/tty/ttyUSB0",
173                 expected => "sub/direct/ory/visor" ,
174                 conf     => <<EOF
175 KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
176 EOF
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
184 BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
185 EOF
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
193 BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
194 EOF
195         },
196         {
197                 desc     => "test NAME substitution chars (with length limit)",
198                 subsys   => "block",
199                 devpath  => "block/sda/sda3",
200                 expected => "M8-m3-n3-b0:0-sIBM" ,
201                 conf     => <<EOF
202 BUS="scsi", PLACE="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}"
203 EOF
204         },
205         {
206                 desc     => "old style SYSFS_ attribute",
207                 subsys   => "block",
208                 devpath  => "block/sda",
209                 expected => "good" ,
210                 conf     => <<EOF
211 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="good"
212 EOF
213         },
214         {
215                 desc     => "sustitution of sysfs value (%s{file})",
216                 subsys   => "block",
217                 devpath  => "block/sda",
218                 expected => "disk-IBM-ESXS-sda" ,
219                 conf     => <<EOF
220 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="disk-%s{vendor}-%k"
221 KERNEL="ttyUSB0", NAME="visor"
222 EOF
223         },
224         {
225                 desc     => "program result substitution",
226                 subsys   => "block",
227                 devpath  => "block/sda/sda3",
228                 expected => "special-device-3" ,
229                 conf     => <<EOF
230 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
231 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
232 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
233 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
234 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
235 EOF
236         },
237         {
238                 desc     => "program result substitution",
239                 subsys   => "block",
240                 devpath  => "block/sda/sda3",
241                 expected => "test-0:0:0:0" ,
242                 conf     => <<EOF
243 BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
244 EOF
245         },
246         {
247                 desc     => "program with escaped format char (tricky: callout returns format char!)",
248                 subsys   => "block",
249                 devpath  => "block/sda/sda3",
250                 expected => "escape-3" ,
251                 conf     => <<EOF
252 BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
253 EOF
254         },
255         {
256                 desc     => "program with lots of arguments",
257                 subsys   => "block",
258                 devpath  => "block/sda/sda3",
259                 expected => "foo9" ,
260                 conf     => <<EOF
261 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="%c{7}"
262 EOF
263         },
264         {
265                 desc     => "program with subshell",
266                 subsys   => "block",
267                 devpath  => "block/sda/sda3",
268                 expected => "bar9" ,
269                 conf     => <<EOF
270 BUS="scsi", PROGRAM="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed  s/foo9/bar9/'", KERNEL="sda3", NAME="%c{7}"
271 EOF
272         },
273         {
274                 desc     => "program arguments combined with apostrophes",
275                 subsys   => "block",
276                 devpath  => "block/sda/sda3",
277                 expected => "foo7" ,
278                 conf     => <<EOF
279 BUS="scsi", PROGRAM="/bin/echo -n 'foo3 foo4'   'foo5   foo6   foo7 foo8'", KERNEL="sda3", NAME="%c{5}"
280 EOF
281         },
282         {
283                 desc     => "characters before the %c{N} substitution",
284                 subsys   => "block",
285                 devpath  => "block/sda/sda3",
286                 expected => "my-foo9" ,
287                 conf     => <<EOF
288 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{7}"
289 EOF
290         },
291         {
292                 desc     => "substitute the second to last argument",
293                 subsys   => "block",
294                 devpath  => "block/sda/sda3",
295                 expected => "my-foo8" ,
296                 conf     => <<EOF
297 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}"
298 EOF
299         },
300         {
301                 desc     => "program result substitution (numbered part of)",
302                 subsys   => "block",
303                 devpath  => "block/sda/sda3",
304                 expected => "link1" ,
305                 conf     => <<EOF
306 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
307 EOF
308         },
309         {
310                 desc     => "invalid program for device with no bus",
311                 subsys   => "tty",
312                 devpath  => "class/tty/console",
313                 expected => "TTY" ,
314                 conf     => <<EOF
315 BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
316 KERNEL="console", NAME="TTY"
317 EOF
318         },
319         {
320                 desc     => "valid program for device with no bus",
321                 subsys   => "tty",
322                 devpath  => "class/tty/console",
323                 expected => "foo" ,
324                 conf     => <<EOF
325 PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
326 KERNEL="console", NAME="TTY"
327 EOF
328         },
329         {
330                 desc     => "invalid label for device with no bus",
331                 subsys   => "tty",
332                 devpath  => "class/tty/console",
333                 expected => "TTY" ,
334                 conf     => <<EOF
335 BUS="foo", SYSFS{dev}="5:1", NAME="foo"
336 KERNEL="console", NAME="TTY"
337 EOF
338         },
339         {
340                 desc     => "valid label for device with no bus",
341                 subsys   => "tty",
342                 devpath  => "class/tty/console",
343                 expected => "foo" ,
344                 conf     => <<EOF
345 SYSFS{dev}="5:1", NAME="foo"
346 KERNEL="console", NAME="TTY"
347 EOF
348         },
349         {
350                 desc     => "program and bus type match",
351                 subsys   => "block",
352                 devpath  => "block/sda",
353                 expected => "scsi-0:0:0:0" ,
354                 conf     => <<EOF
355 BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
356 BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
357 BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
358 EOF
359         },
360         {
361                 desc     => "symlink creation (same directory)",
362                 subsys   => "tty",
363                 devpath  => "class/tty/ttyUSB0",
364                 expected => "visor0" ,
365                 conf     => <<EOF
366 KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
367 EOF
368         },
369         {
370                 desc     => "symlink creation (relative link back)",
371                 subsys   => "block",
372                 devpath  => "block/sda/sda2",
373                 expected => "1/2/a/b/symlink" ,
374                 conf     => <<EOF
375 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
376 EOF
377         },
378         {
379                 desc     => "symlink creation (relative link forward)",
380                 subsys   => "block",
381                 devpath  => "block/sda/sda2",
382                 expected => "1/2/symlink" ,
383                 conf     => <<EOF
384 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
385 EOF
386         },
387         {
388                 desc     => "symlink creation (relative link back and forward)",
389                 subsys   => "block",
390                 devpath  => "block/sda/sda2",
391                 expected => "1/2/c/d/symlink" ,
392                 conf     => <<EOF
393 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
394 EOF
395         },
396         {
397                 desc     => "multiple symlinks",
398                 subsys   => "tty",
399                 devpath  => "class/tty/ttyUSB0",
400                 expected => "second-0" ,
401                 conf     => <<EOF
402 KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
403 EOF
404         },
405         {
406                 desc     => "create all possible partitions",
407                 subsys   => "block",
408                 devpath  => "block/sda",
409                 expected => "boot_disk15" ,
410                 conf     => <<EOF
411 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME{all_partitions}="boot_disk"
412 EOF
413         },
414         {
415                 desc     => "sysfs parent hierarchy",
416                 subsys   => "tty",
417                 devpath  => "class/tty/ttyUSB0",
418                 expected => "visor" ,
419                 conf     => <<EOF
420 SYSFS{idProduct}="2008", NAME="visor"
421 EOF
422         },
423         {
424                 desc     => "name test with ! in the name",
425                 subsys   => "block",
426                 devpath  => "block/rd!c0d0",
427                 expected => "rd/c0d0" ,
428                 conf     => <<EOF
429 BUS="scsi", NAME="%k"
430 KERNEL="ttyUSB0", NAME="visor"
431 EOF
432         },
433         {
434                 desc     => "name test with ! in the name, but no matching rule",
435                 subsys   => "block",
436                 devpath  => "block/rd!c0d0",
437                 expected => "rd/c0d0" ,
438                 conf     => <<EOF
439 KERNEL="ttyUSB0", NAME="visor"
440 EOF
441         },
442         {
443                 desc     => "ID rule",
444                 subsys   => "block",
445                 devpath  => "block/sda",
446                 expected => "scsi-0:0:0:0",
447                 conf     => <<EOF
448 BUS="usb", ID="0:0:0:0", NAME="not-scsi"
449 BUS="scsi", ID="0:0:0:1", NAME="no-match"
450 BUS="scsi", ID=":0", NAME="short-id"
451 BUS="scsi", ID="/0:0:0:0", NAME="no-match"
452 BUS="scsi", ID="0:0:0:0", NAME="scsi-0:0:0:0"
453 EOF
454         },
455         {
456                 desc     => "ID wildcard all",
457                 subsys   => "block",
458                 devpath  => "block/sda",
459                 expected => "scsi-0:0:0:0",
460                 conf     => <<EOF
461 BUS="scsi", ID="*:1", NAME="no-match"
462 BUS="scsi", ID="*:0:1", NAME="no-match"
463 BUS="scsi", ID="*:0:0:1", NAME="no-match"
464 BUS="scsi", ID="*", NAME="scsi-0:0:0:0"
465 BUS="scsi", ID="0:0:0:0", NAME="bad"
466 EOF
467         },
468         {
469                 desc     => "ID wildcard partial",
470                 subsys   => "block",
471                 devpath  => "block/sda",
472                 expected => "scsi-0:0:0:0",
473                 conf     => <<EOF
474 BUS="scsi", ID="*:0", NAME="scsi-0:0:0:0"
475 BUS="scsi", ID="0:0:0:0", NAME="bad"
476 EOF
477         },
478         {
479                 desc     => "ID wildcard partial 2",
480                 subsys   => "block",
481                 devpath  => "block/sda",
482                 expected => "scsi-0:0:0:0",
483                 conf     => <<EOF
484 BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
485 BUS="scsi", ID="0:0:0:0", NAME="bad"
486 EOF
487         },
488         {
489                 desc     => "ignore SYSFS attribute whitespace",
490                 subsys   => "block",
491                 devpath  => "block/sda",
492                 expected => "ignored",
493                 conf     => <<EOF
494 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE", NAME="ignored"
495 EOF
496         },
497         {
498                 desc     => "do not ignore SYSFS attribute whitespace",
499                 subsys   => "block",
500                 devpath  => "block/sda",
501                 expected => "matched-with-space",
502                 conf     => <<EOF
503 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE ", NAME="wrong-to-ignore"
504 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE   ", NAME="matched-with-space"
505 EOF
506         },
507 );
508
509 # set env
510 $ENV{UDEV_TEST} = "yes";
511 $ENV{SYSFS_PATH} = $sysfs;
512 $ENV{UDEV_CONFIG_FILE} = $main_conf;
513
514
515 sub udev {
516         my ($action, $subsys, $devpath, $config) = @_;
517
518         $ENV{DEVPATH} = $devpath;
519
520         # create temporary config
521         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
522         print CONF $$config;
523         close CONF;
524
525         $ENV{ACTION} = $action;
526         system("$udev_bin $subsys");
527 }
528
529 my $error = 0;
530
531 sub run_test {
532         my ($config, $number) = @_;
533         
534         print "TEST $number: $config->{desc}\n";
535         print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
536
537         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
538         if (-e "$PWD/$udev_root$config->{expected}") {
539                 print "add: ok    ";
540         } else {
541                 print "add: error\n";
542                 system("tree $udev_root");
543                 print "\n";
544                 $error++;
545         }
546
547         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
548         if ((-e "$PWD/$udev_root$config->{expected}") ||
549             (-l "$PWD/$udev_root$config->{expected}")) {
550                 print "remove: error\n\n";
551                 system("tree $udev_root");
552                 $error++;
553         } else {
554                 print "remove: ok\n\n";
555         }
556 }
557
558 # prepare
559 system("rm -rf $udev_root");
560 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
561
562 # create initial config file
563 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
564 print CONF "udev_root=\"$udev_root\"\n";
565 print CONF "udev_db=\"$udev_db\"\n";
566 print CONF "udev_rules=\"$conf_tmp\"\n";
567 print CONF "udev_permissions=\"$perm\"\n";
568 close CONF;
569
570 my $test_num = 1;
571
572 if ($ARGV[0]) {
573         # only run one test
574         $test_num = $ARGV[0];
575         print "udev-test will run test number $test_num only\n";
576
577         run_test($tests[$test_num-1], $test_num);
578 } else {
579         # test all
580         print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
581
582         foreach my $config (@tests) {
583                 run_test($config, $test_num);
584                 $test_num++;
585
586         }
587 }
588
589 print "$error errors occured\n\n";
590
591 # cleanup
592 unlink($udev_db);
593 system("rm -rf $udev_root");
594 unlink($conf_tmp);
595 unlink($main_conf);
596