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