chiark / gitweb /
9538cba2d10d6bc28afdd6090ff66899ec3ff789
[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 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="boot_disk%n"
42 REPLACE, 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 LABEL, 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 LABEL, BUS="scsi", SYSFS_vendor="?IBM-ESXS", NAME="boot_disk%n-1"
61 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS?", NAME="boot_disk%n-2"
62 LABEL, BUS="scsi", SYSFS_vendor="IBM-ES??", NAME="boot_disk%n"
63 LABEL, 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 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW   !#", NAME="boot_diskX%n"
73 LABEL, 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 LABEL, 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 LABEL, 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 REPLACE, KERNEL="ttyUSB*", NAME="visor/%n"
93 EOF
94         },
95         {
96                 desc     => "catch device by ?",
97                 subsys   => "tty",
98                 devpath  => "class/tty/ttyUSB0",
99                 expected => "visor/0" ,
100                 conf     => <<EOF
101 REPLACE, KERNEL="ttyUSB??*", NAME="visor/%n-1"
102 REPLACE, KERNEL="ttyUSB??", NAME="visor/%n-2"
103 REPLACE, KERNEL="ttyUSB?", NAME="visor/%n"
104 EOF
105         },
106         {
107                 desc     => "catch device by character class",
108                 subsys   => "tty",
109                 devpath  => "class/tty/ttyUSB0",
110                 expected => "visor/0" ,
111                 conf     => <<EOF
112 REPLACE, KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
113 REPLACE, KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
114 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
115 EOF
116         },
117         {
118                 desc     => "replace kernel name",
119                 subsys   => "tty",
120                 devpath  => "class/tty/ttyUSB0",
121                 expected => "visor" ,
122                 conf     => <<EOF
123 REPLACE, KERNEL="ttyUSB0", NAME="visor"
124 EOF
125         },
126         {
127                 desc     => "Handle comment lines in config file (and replace kernel name)",
128                 subsys   => "tty",
129                 devpath  => "class/tty/ttyUSB0",
130                 expected => "visor" ,
131                 conf     => <<EOF
132 # this is a comment
133 REPLACE, KERNEL="ttyUSB0", NAME="visor"
134
135 EOF
136         },
137         {
138                 desc     => "Handle comment lines in config file with whitespace (and replace kernel name)",
139                 subsys   => "tty",
140                 devpath  => "class/tty/ttyUSB0",
141                 expected => "visor" ,
142                 conf     => <<EOF
143  # this is a comment with whitespace before the comment 
144 REPLACE, KERNEL="ttyUSB0", NAME="visor"
145
146 EOF
147         },
148         {
149                 desc     => "Handle empty lines in config file (and replace kernel name)",
150                 subsys   => "tty",
151                 devpath  => "class/tty/ttyUSB0",
152                 expected => "visor" ,
153                 conf     => <<EOF
154
155 REPLACE, KERNEL="ttyUSB0", NAME="visor"
156
157 EOF
158         },
159         {
160                 desc     => "subdirectory handling",
161                 subsys   => "tty",
162                 devpath  => "class/tty/ttyUSB0",
163                 expected => "sub/direct/ory/visor" ,
164                 conf     => <<EOF
165 REPLACE, KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
166 EOF
167         },
168         {
169                 desc     => "place on bus of scsi partition",
170                 subsys   => "block",
171                 devpath  => "block/sda/sda3",
172                 expected => "first_disk3" ,
173                 conf     => <<EOF
174 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
175 EOF
176         },
177         {
178                 desc     => "test NAME substitution chars",
179                 subsys   => "block",
180                 devpath  => "block/sda/sda3",
181                 expected => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
182                 conf     => <<EOF
183 TOPOLOGY, BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
184 EOF
185         },
186         {
187                 desc     => "callout result substitution",
188                 subsys   => "block",
189                 devpath  => "block/sda/sda3",
190                 expected => "special-device-3" ,
191                 conf     => <<EOF
192 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="-special-*", NAME="%c-1-%n"
193 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special--*", NAME="%c-2-%n"
194 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-device-", NAME="%c-3-%n"
195 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-devic", NAME="%c-4-%n"
196 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n special-device", ID="special-*", NAME="%c-%n"
197 EOF
198         },
199         {
200                 desc     => "callout program substitution",
201                 subsys   => "block",
202                 devpath  => "block/sda/sda3",
203                 expected => "test-0:0:0:0" ,
204                 conf     => <<EOF
205 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n test-%b", ID="test-*", NAME="%c"
206 EOF
207         },
208         {
209                 desc     => "callout program substitution (numbered part of)",
210                 subsys   => "block",
211                 devpath  => "block/sda/sda3",
212                 expected => "link1" ,
213                 conf     => <<EOF
214 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", ID="node *", NAME="%1c", SYMLINK="%2c %3c"
215 EOF
216         },
217         {
218                 desc     => "invalid callout for device with no bus",
219                 subsys   => "tty",
220                 devpath  => "class/tty/console",
221                 expected => "TTY" ,
222                 conf     => <<EOF
223 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n foo", ID="foo", NAME="foo"
224 REPLACE, KERNEL="console", NAME="TTY"
225 EOF
226         },
227         {
228                 desc     => "valid callout for device with no bus",
229                 subsys   => "tty",
230                 devpath  => "class/tty/console",
231                 expected => "foo" ,
232                 conf     => <<EOF
233 CALLOUT, PROGRAM="/bin/echo -n foo", ID="foo", NAME="foo"
234 EOF
235         },
236         {
237                 desc     => "devfs disk naming substitution",
238                 subsys   => "block",
239                 devpath  => "block/sda",
240                 expected => "lun0/disc" ,
241                 conf     => <<EOF
242 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="lun0/%D"
243 EOF
244         },
245         {
246                 desc     => "devfs disk naming substitution",
247                 subsys   => "block",
248                 devpath  => "block/sda/sda2",
249                 expected => "lun0/part2" ,
250                 conf     => <<EOF
251 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="lun0/%D"
252 EOF
253         },
254         {
255                 desc     => "callout bus type",
256                 subsys   => "block",
257                 devpath  => "block/sda",
258                 expected => "scsi-0:0:0:0" ,
259                 conf     => <<EOF
260 CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
261 CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
262 CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
263 EOF
264         },
265         {
266                 desc     => "symlink creation (same directory)",
267                 subsys   => "tty",
268                 devpath  => "class/tty/ttyUSB0",
269                 expected => "visor0" ,
270                 conf     => <<EOF
271 REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
272 EOF
273         },
274         {
275                 desc     => "symlink creation (relative link back)",
276                 subsys   => "block",
277                 devpath  => "block/sda/sda2",
278                 expected => "1/2/a/b/symlink" ,
279                 conf     => <<EOF
280 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
281 EOF
282         },
283         {
284                 desc     => "symlink creation (relative link forward)",
285                 subsys   => "block",
286                 devpath  => "block/sda/sda2",
287                 expected => "1/2/symlink" ,
288                 conf     => <<EOF
289 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
290 EOF
291         },
292         {
293                 desc     => "symlink creation (relative link back and forward)",
294                 subsys   => "block",
295                 devpath  => "block/sda/sda2",
296                 expected => "1/2/c/d/symlink" ,
297                 conf     => <<EOF
298 LABEL, BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
299 EOF
300         },
301         {
302                 desc     => "multiple symlinks",
303                 subsys   => "tty",
304                 devpath  => "class/tty/ttyUSB0",
305                 expected => "second-0" ,
306                 conf     => <<EOF
307 REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
308 EOF
309         },
310 );
311
312 # set env
313 $ENV{UDEV_TEST} = "yes";
314 $ENV{SYSFS_PATH} = $sysfs;
315 $ENV{UDEV_CONFIG_FILE} = $main_conf;
316
317
318 sub udev {
319         my ($action, $subsys, $devpath, $config) = @_;
320
321         $ENV{DEVPATH} = $devpath;
322
323         # create temporary config
324         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
325         print CONF $$config;
326         close CONF;
327
328         $ENV{ACTION} = $action;
329         system("$udev_bin $subsys");
330 }
331
332
333 # prepare
334 system("rm -rf $udev_root");
335 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
336
337 # test
338 my $error = 0;
339 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
340
341 # create initial config file
342 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
343 print CONF "udev_root=\"$udev_root\"\n";
344 print CONF "udev_db=\"$udev_db\"\n";
345 print CONF "udev_rules=\"$conf_tmp\"\n";
346 print CONF "udev_permissions=\"$perm\"\n";
347 close CONF;
348
349 foreach my $config (@tests) {
350         $config->{conf} =~ m/([A-Z]+)\s*,/;
351         my $method  = $1;
352
353         print "TEST: $config->{desc}\n";
354         print "method \'$method\' for \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
355
356         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
357         if (-e "$PWD/$udev_root$config->{expected}") {
358                 print "add: ok    ";
359         } else {
360                 print "add: error\n";
361                 system("tree $udev_root");
362                 print "\n";
363                 $error++;
364         }
365
366         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
367         if ((-e "$PWD/$udev_root$config->{expected}") ||
368             (-l "$PWD/$udev_root$config->{expected}")) {
369                 print "remove: error\n\n";
370                 system("tree $udev_root");
371                 $error++;
372         } else {
373                 print "remove: ok\n\n";
374         }
375 }
376
377 print "$error errors occured\n\n";
378
379 # cleanup
380 unlink($udev_db);
381 system("rm -rf $udev_root");
382 unlink($conf_tmp);
383 unlink($main_conf);
384