chiark / gitweb /
[PATCH] callout part selector tweak
[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     => "program result substitution (numbered part of+)",
311                 subsys   => "block",
312                 devpath  => "block/sda/sda3",
313                 expected => "link3" ,
314                 conf     => <<EOF
315 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2 link3 link4", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2+}"
316 EOF
317         },
318         {
319                 desc     => "invalid program for device with no bus",
320                 subsys   => "tty",
321                 devpath  => "class/tty/console",
322                 expected => "TTY" ,
323                 conf     => <<EOF
324 BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
325 KERNEL="console", NAME="TTY"
326 EOF
327         },
328         {
329                 desc     => "valid program for device with no bus",
330                 subsys   => "tty",
331                 devpath  => "class/tty/console",
332                 expected => "foo" ,
333                 conf     => <<EOF
334 PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
335 KERNEL="console", NAME="TTY"
336 EOF
337         },
338         {
339                 desc     => "invalid label for device with no bus",
340                 subsys   => "tty",
341                 devpath  => "class/tty/console",
342                 expected => "TTY" ,
343                 conf     => <<EOF
344 BUS="foo", SYSFS{dev}="5:1", NAME="foo"
345 KERNEL="console", NAME="TTY"
346 EOF
347         },
348         {
349                 desc     => "valid label for device with no bus",
350                 subsys   => "tty",
351                 devpath  => "class/tty/console",
352                 expected => "foo" ,
353                 conf     => <<EOF
354 SYSFS{dev}="5:1", NAME="foo"
355 KERNEL="console", NAME="TTY"
356 EOF
357         },
358         {
359                 desc     => "program and bus type match",
360                 subsys   => "block",
361                 devpath  => "block/sda",
362                 expected => "scsi-0:0:0:0" ,
363                 conf     => <<EOF
364 BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
365 BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
366 BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
367 EOF
368         },
369         {
370                 desc     => "symlink creation (same directory)",
371                 subsys   => "tty",
372                 devpath  => "class/tty/ttyUSB0",
373                 expected => "visor0" ,
374                 conf     => <<EOF
375 KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
376 EOF
377         },
378         {
379                 desc     => "symlink creation (relative link back)",
380                 subsys   => "block",
381                 devpath  => "block/sda/sda2",
382                 expected => "1/2/a/b/symlink" ,
383                 conf     => <<EOF
384 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
385 EOF
386         },
387         {
388                 desc     => "symlink creation (relative link forward)",
389                 subsys   => "block",
390                 devpath  => "block/sda/sda2",
391                 expected => "1/2/symlink" ,
392                 conf     => <<EOF
393 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
394 EOF
395         },
396         {
397                 desc     => "symlink creation (relative link back and forward)",
398                 subsys   => "block",
399                 devpath  => "block/sda/sda2",
400                 expected => "1/2/c/d/symlink" ,
401                 conf     => <<EOF
402 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
403 EOF
404         },
405         {
406                 desc     => "multiple symlinks",
407                 subsys   => "tty",
408                 devpath  => "class/tty/ttyUSB0",
409                 expected => "second-0" ,
410                 conf     => <<EOF
411 KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
412 EOF
413         },
414         {
415                 desc     => "create all possible partitions",
416                 subsys   => "block",
417                 devpath  => "block/sda",
418                 expected => "boot_disk15" ,
419                 conf     => <<EOF
420 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME{all_partitions}="boot_disk"
421 EOF
422         },
423         {
424                 desc     => "sysfs parent hierarchy",
425                 subsys   => "tty",
426                 devpath  => "class/tty/ttyUSB0",
427                 expected => "visor" ,
428                 conf     => <<EOF
429 SYSFS{idProduct}="2008", NAME="visor"
430 EOF
431         },
432         {
433                 desc     => "name test with ! in the name",
434                 subsys   => "block",
435                 devpath  => "block/rd!c0d0",
436                 expected => "rd/c0d0" ,
437                 conf     => <<EOF
438 BUS="scsi", NAME="%k"
439 KERNEL="ttyUSB0", NAME="visor"
440 EOF
441         },
442         {
443                 desc     => "name test with ! in the name, but no matching rule",
444                 subsys   => "block",
445                 devpath  => "block/rd!c0d0",
446                 expected => "rd/c0d0" ,
447                 conf     => <<EOF
448 KERNEL="ttyUSB0", NAME="visor"
449 EOF
450         },
451         {
452                 desc     => "ID rule",
453                 subsys   => "block",
454                 devpath  => "block/sda",
455                 expected => "scsi-0:0:0:0",
456                 conf     => <<EOF
457 BUS="usb", ID="0:0:0:0", NAME="not-scsi"
458 BUS="scsi", ID="0:0:0:1", NAME="no-match"
459 BUS="scsi", ID=":0", NAME="short-id"
460 BUS="scsi", ID="/0:0:0:0", NAME="no-match"
461 BUS="scsi", ID="0:0:0:0", NAME="scsi-0:0:0:0"
462 EOF
463         },
464         {
465                 desc     => "ID wildcard all",
466                 subsys   => "block",
467                 devpath  => "block/sda",
468                 expected => "scsi-0:0:0:0",
469                 conf     => <<EOF
470 BUS="scsi", ID="*:1", NAME="no-match"
471 BUS="scsi", ID="*:0:1", NAME="no-match"
472 BUS="scsi", ID="*:0:0:1", NAME="no-match"
473 BUS="scsi", ID="*", NAME="scsi-0:0:0:0"
474 BUS="scsi", ID="0:0:0:0", NAME="bad"
475 EOF
476         },
477         {
478                 desc     => "ID wildcard partial",
479                 subsys   => "block",
480                 devpath  => "block/sda",
481                 expected => "scsi-0:0:0:0",
482                 conf     => <<EOF
483 BUS="scsi", ID="*:0", NAME="scsi-0:0:0:0"
484 BUS="scsi", ID="0:0:0:0", NAME="bad"
485 EOF
486         },
487         {
488                 desc     => "ID wildcard partial 2",
489                 subsys   => "block",
490                 devpath  => "block/sda",
491                 expected => "scsi-0:0:0:0",
492                 conf     => <<EOF
493 BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
494 BUS="scsi", ID="0:0:0:0", NAME="bad"
495 EOF
496         },
497         {
498                 desc     => "ignore SYSFS attribute whitespace",
499                 subsys   => "block",
500                 devpath  => "block/sda",
501                 expected => "ignored",
502                 conf     => <<EOF
503 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE", NAME="ignored"
504 EOF
505         },
506         {
507                 desc     => "do not ignore SYSFS attribute whitespace",
508                 subsys   => "block",
509                 devpath  => "block/sda",
510                 expected => "matched-with-space",
511                 conf     => <<EOF
512 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE ", NAME="wrong-to-ignore"
513 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE   ", NAME="matched-with-space"
514 EOF
515         },
516 );
517
518 # set env
519 $ENV{UDEV_TEST} = "yes";
520 $ENV{SYSFS_PATH} = $sysfs;
521 $ENV{UDEV_CONFIG_FILE} = $main_conf;
522
523
524 sub udev {
525         my ($action, $subsys, $devpath, $config) = @_;
526
527         $ENV{DEVPATH} = $devpath;
528
529         # create temporary config
530         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
531         print CONF $$config;
532         close CONF;
533
534         $ENV{ACTION} = $action;
535         system("$udev_bin $subsys");
536 }
537
538 my $error = 0;
539
540 sub run_test {
541         my ($config, $number) = @_;
542         
543         print "TEST $number: $config->{desc}\n";
544         print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
545
546         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
547         if (-e "$PWD/$udev_root$config->{expected}") {
548                 print "add: ok    ";
549         } else {
550                 print "add: error\n";
551                 system("tree $udev_root");
552                 print "\n";
553                 $error++;
554         }
555
556         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
557         if ((-e "$PWD/$udev_root$config->{expected}") ||
558             (-l "$PWD/$udev_root$config->{expected}")) {
559                 print "remove: error\n\n";
560                 system("tree $udev_root");
561                 $error++;
562         } else {
563                 print "remove: ok\n\n";
564         }
565 }
566
567 # prepare
568 system("rm -rf $udev_root");
569 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
570
571 # create initial config file
572 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
573 print CONF "udev_root=\"$udev_root\"\n";
574 print CONF "udev_db=\"$udev_db\"\n";
575 print CONF "udev_rules=\"$conf_tmp\"\n";
576 print CONF "udev_permissions=\"$perm\"\n";
577 close CONF;
578
579 my $test_num = 1;
580
581 if ($ARGV[0]) {
582         # only run one test
583         $test_num = $ARGV[0];
584         print "udev-test will run test number $test_num only\n";
585
586         run_test($tests[$test_num-1], $test_num);
587 } else {
588         # test all
589         print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
590
591         foreach my $config (@tests) {
592                 run_test($config, $test_num);
593                 $test_num++;
594
595         }
596 }
597
598 print "$error errors occured\n\n";
599
600 # cleanup
601 unlink($udev_db);
602 system("rm -rf $udev_root");
603 unlink($conf_tmp);
604 unlink($main_conf);
605