]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/udev-test.pl
[PATCH] Adding '%s' format specifier to NAME and SYMLINK
[thirdparty/systemd.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 => "sustitution of sysfs value (%s{file})",
198 subsys => "block",
199 devpath => "block/sda",
200 expected => "disk-IBM-ESXS-sda" ,
201 conf => <<EOF
202 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="disk-%s{vendor}-%k"
203 KERNEL="ttyUSB0", NAME="visor"
204 EOF
205 },
206 {
207 desc => "program result substitution",
208 subsys => "block",
209 devpath => "block/sda/sda3",
210 expected => "special-device-3" ,
211 conf => <<EOF
212 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
213 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
214 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
215 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
216 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
217 EOF
218 },
219 {
220 desc => "program result substitution",
221 subsys => "block",
222 devpath => "block/sda/sda3",
223 expected => "test-0:0:0:0" ,
224 conf => <<EOF
225 BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
226 EOF
227 },
228 {
229 desc => "program with escaped format char (tricky: callout returns format char!)",
230 subsys => "block",
231 devpath => "block/sda/sda3",
232 expected => "escape-3" ,
233 conf => <<EOF
234 BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
235 EOF
236 },
237 {
238 desc => "program result substitution (numbered part of)",
239 subsys => "block",
240 devpath => "block/sda/sda3",
241 expected => "link1" ,
242 conf => <<EOF
243 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%1c", SYMLINK="%2c %3c"
244 EOF
245 },
246 {
247 desc => "invalid program for device with no bus",
248 subsys => "tty",
249 devpath => "class/tty/console",
250 expected => "TTY" ,
251 conf => <<EOF
252 BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
253 KERNEL="console", NAME="TTY"
254 EOF
255 },
256 {
257 desc => "valid program for device with no bus",
258 subsys => "tty",
259 devpath => "class/tty/console",
260 expected => "foo" ,
261 conf => <<EOF
262 PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
263 KERNEL="console", NAME="TTY"
264 EOF
265 },
266 {
267 desc => "invalid label for device with no bus",
268 subsys => "tty",
269 devpath => "class/tty/console",
270 expected => "TTY" ,
271 conf => <<EOF
272 BUS="foo", SYSFS_dev="5:1", NAME="foo"
273 KERNEL="console", NAME="TTY"
274 EOF
275 },
276 {
277 desc => "valid label for device with no bus",
278 subsys => "tty",
279 devpath => "class/tty/console",
280 expected => "foo" ,
281 conf => <<EOF
282 SYSFS_dev="5:1", NAME="foo"
283 KERNEL="console", NAME="TTY"
284 EOF
285 },
286 {
287 desc => "program and bus type match",
288 subsys => "block",
289 devpath => "block/sda",
290 expected => "scsi-0:0:0:0" ,
291 conf => <<EOF
292 BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
293 BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
294 BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
295 EOF
296 },
297 {
298 desc => "symlink creation (same directory)",
299 subsys => "tty",
300 devpath => "class/tty/ttyUSB0",
301 expected => "visor0" ,
302 conf => <<EOF
303 KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
304 EOF
305 },
306 {
307 desc => "symlink creation (relative link back)",
308 subsys => "block",
309 devpath => "block/sda/sda2",
310 expected => "1/2/a/b/symlink" ,
311 conf => <<EOF
312 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
313 EOF
314 },
315 {
316 desc => "symlink creation (relative link forward)",
317 subsys => "block",
318 devpath => "block/sda/sda2",
319 expected => "1/2/symlink" ,
320 conf => <<EOF
321 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
322 EOF
323 },
324 {
325 desc => "symlink creation (relative link back and forward)",
326 subsys => "block",
327 devpath => "block/sda/sda2",
328 expected => "1/2/c/d/symlink" ,
329 conf => <<EOF
330 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
331 EOF
332 },
333 {
334 desc => "multiple symlinks",
335 subsys => "tty",
336 devpath => "class/tty/ttyUSB0",
337 expected => "second-0" ,
338 conf => <<EOF
339 KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
340 EOF
341 },
342 {
343 desc => "sysfs parent heirachy",
344 subsys => "tty",
345 devpath => "class/tty/ttyUSB0",
346 expected => "visor" ,
347 conf => <<EOF
348 SYSFS_idProduct="2008", NAME="visor"
349 EOF
350 },
351 {
352 desc => "name test with ! in the name",
353 subsys => "block",
354 devpath => "block/rd!c0d0",
355 expected => "rd/c0d0" ,
356 conf => <<EOF
357 BUS="scsi", NAME="%k"
358 KERNEL="ttyUSB0", NAME="visor"
359 EOF
360 },
361 {
362 desc => "name test with ! in the name, but no matching rule",
363 subsys => "block",
364 devpath => "block/rd!c0d0",
365 expected => "rd/c0d0" ,
366 conf => <<EOF
367 KERNEL="ttyUSB0", NAME="visor"
368 EOF
369 },
370 );
371
372 # set env
373 $ENV{UDEV_TEST} = "yes";
374 $ENV{SYSFS_PATH} = $sysfs;
375 $ENV{UDEV_CONFIG_FILE} = $main_conf;
376
377
378 sub udev {
379 my ($action, $subsys, $devpath, $config) = @_;
380
381 $ENV{DEVPATH} = $devpath;
382
383 # create temporary config
384 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
385 print CONF $$config;
386 close CONF;
387
388 $ENV{ACTION} = $action;
389 system("$udev_bin $subsys");
390 }
391
392 my $error = 0;
393
394 sub run_test {
395 my ($config, $number) = @_;
396
397 print "TEST $number: $config->{desc}\n";
398 print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
399
400 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
401 if (-e "$PWD/$udev_root$config->{expected}") {
402 print "add: ok ";
403 } else {
404 print "add: error\n";
405 system("tree $udev_root");
406 print "\n";
407 $error++;
408 }
409
410 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
411 if ((-e "$PWD/$udev_root$config->{expected}") ||
412 (-l "$PWD/$udev_root$config->{expected}")) {
413 print "remove: error\n\n";
414 system("tree $udev_root");
415 $error++;
416 } else {
417 print "remove: ok\n\n";
418 }
419 }
420
421 # prepare
422 system("rm -rf $udev_root");
423 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
424
425 # create initial config file
426 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
427 print CONF "udev_root=\"$udev_root\"\n";
428 print CONF "udev_db=\"$udev_db\"\n";
429 print CONF "udev_rules=\"$conf_tmp\"\n";
430 print CONF "udev_permissions=\"$perm\"\n";
431 close CONF;
432
433 my $test_num = 1;
434
435 if ($ARGV[0]) {
436 # only run one test
437 $test_num = $ARGV[0];
438 print "udev-test will run test number $test_num only\n";
439
440 run_test($tests[$test_num-1], $test_num);
441 } else {
442 # test all
443 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
444
445 foreach my $config (@tests) {
446 run_test($config, $test_num);
447 $test_num++;
448
449 }
450 }
451
452 print "$error errors occured\n\n";
453
454 # cleanup
455 unlink($udev_db);
456 system("rm -rf $udev_root");
457 unlink($conf_tmp);
458 unlink($main_conf);
459