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