]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
[PATCH] udevd - next round of fixes
[thirdparty/systemd.git] / test / udev-test.pl
CommitLineData
a367f04e
GKH
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
21use warnings;
22use strict;
23
f8f00338 24my $PWD = $ENV{PWD};
a367f04e
GKH
25my $sysfs = "sys/";
26my $udev_bin = "../udev";
27my $udev_root = "udev-root/"; # !!! directory will be removed !!!
36043f84 28my $udev_db = ".udev.tdb";
a367f04e 29my $perm = "udev.permissions";
72ffa78d
GKH
30my $main_conf = "udev-test.conf";
31my $conf_tmp = "udev-test.rules";
a367f04e
GKH
32
33
34my @tests = (
35 {
36 desc => "label test of scsi disc",
37 subsys => "block",
38 devpath => "block/sda",
39 expected => "boot_disk" ,
40 conf => <<EOF
ac28b86d
KS
41BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="boot_disk%n"
42KERNEL="ttyUSB0", NAME="visor"
a367f04e
GKH
43EOF
44 },
45 {
46 desc => "label test of scsi partition",
47 subsys => "block",
48 devpath => "block/sda/sda1",
49 expected => "boot_disk1" ,
50 conf => <<EOF
ac28b86d 51BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="boot_disk%n"
83be97ba
KS
52EOF
53 },
54 {
55 desc => "label test of pattern match",
56 subsys => "block",
57 devpath => "block/sda/sda1",
58 expected => "boot_disk1" ,
59 conf => <<EOF
ac28b86d
KS
60BUS="scsi", SYSFS_vendor="?IBM-ESXS", NAME="boot_disk%n-1"
61BUS="scsi", SYSFS_vendor="IBM-ESXS?", NAME="boot_disk%n-2"
62BUS="scsi", SYSFS_vendor="IBM-ES??", NAME="boot_disk%n"
63BUS="scsi", SYSFS_vendor="IBM-ESXSS", NAME="boot_disk%n-3"
358c8c20
GKH
64EOF
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
ac28b86d
KS
72BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", NAME="boot_diskX%n"
73BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", NAME="boot_disk%n"
358c8c20
GKH
74EOF
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
ac28b86d
KS
82BUS="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"
83BUS="scsi", SYSFS_vendor="IBM-ESXS", SYSFS_model="ST336605LW !#", SYSFS_scsi_level="4", SYSFS_rev="B245", SYSFS_type="0", NAME="boot_disk%n"
0db6d4cc
KS
84EOF
85 },
86 {
9f1da361 87 desc => "catch device by *",
0db6d4cc
KS
88 subsys => "tty",
89 devpath => "class/tty/ttyUSB0",
90 expected => "visor/0" ,
91 conf => <<EOF
ac28b86d 92KERNEL="ttyUSB*", NAME="visor/%n"
9f1da361
KS
93EOF
94 },
95 {
96 desc => "catch device by ?",
97 subsys => "tty",
98 devpath => "class/tty/ttyUSB0",
99 expected => "visor/0" ,
100 conf => <<EOF
ac28b86d
KS
101KERNEL="ttyUSB??*", NAME="visor/%n-1"
102KERNEL="ttyUSB??", NAME="visor/%n-2"
103KERNEL="ttyUSB?", NAME="visor/%n"
9f1da361
KS
104EOF
105 },
106 {
107 desc => "catch device by character class",
108 subsys => "tty",
109 devpath => "class/tty/ttyUSB0",
110 expected => "visor/0" ,
111 conf => <<EOF
ac28b86d
KS
112KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
113KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
114KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
a367f04e
GKH
115EOF
116 },
117 {
118 desc => "replace kernel name",
119 subsys => "tty",
120 devpath => "class/tty/ttyUSB0",
121 expected => "visor" ,
122 conf => <<EOF
ac28b86d 123KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
124EOF
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
ac28b86d 133KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
134
135EOF
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
ac28b86d 144KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
145
146EOF
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
ac28b86d 155KERNEL="ttyUSB0", NAME="visor"
281ff00a 156
5499d319
KS
157EOF
158 },
159 {
160 desc => "subdirectory handling",
161 subsys => "tty",
162 devpath => "class/tty/ttyUSB0",
163 expected => "sub/direct/ory/visor" ,
164 conf => <<EOF
ac28b86d 165KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
a367f04e
GKH
166EOF
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
ac28b86d 174BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
a367f04e
GKH
175EOF
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
ac28b86d 183BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
a367f04e
GKH
184EOF
185 },
186 {
ac28b86d 187 desc => "program result substitution",
a367f04e
GKH
188 subsys => "block",
189 devpath => "block/sda/sda3",
190 expected => "special-device-3" ,
191 conf => <<EOF
ac28b86d
KS
192BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
193BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
194BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
195BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
196BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
f3b04a2e
GKH
197EOF
198 },
199 {
ac28b86d 200 desc => "program result substitution",
f3b04a2e
GKH
201 subsys => "block",
202 devpath => "block/sda/sda3",
203 expected => "test-0:0:0:0" ,
204 conf => <<EOF
ac28b86d 205BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
8ffb636f
KS
206EOF
207 },
208 {
209 desc => "program with escaped format char (tricky: callout returns format char!)",
210 subsys => "block",
211 devpath => "block/sda/sda3",
212 expected => "escape-3" ,
213 conf => <<EOF
214BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
b1c5e333
KS
215EOF
216 },
217 {
ac28b86d 218 desc => "program result substitution (numbered part of)",
b1c5e333
KS
219 subsys => "block",
220 devpath => "block/sda/sda3",
221 expected => "link1" ,
222 conf => <<EOF
ac28b86d 223BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%1c", SYMLINK="%2c %3c"
8ff8bbba
GKH
224EOF
225 },
226 {
ac28b86d 227 desc => "invalid program for device with no bus",
8ff8bbba
GKH
228 subsys => "tty",
229 devpath => "class/tty/console",
230 expected => "TTY" ,
231 conf => <<EOF
ac28b86d
KS
232BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
233KERNEL="console", NAME="TTY"
1d936fbc
GKH
234EOF
235 },
236 {
ac28b86d 237 desc => "valid program for device with no bus",
1d936fbc
GKH
238 subsys => "tty",
239 devpath => "class/tty/console",
240 expected => "foo" ,
241 conf => <<EOF
ac28b86d
KS
242PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
243KERNEL="console", NAME="TTY"
64682333
GKH
244EOF
245 },
246 {
247 desc => "invalid label for device with no bus",
248 subsys => "tty",
249 devpath => "class/tty/console",
250 expected => "TTY" ,
251 conf => <<EOF
ac28b86d
KS
252BUS="foo", SYSFS_dev="5:1", NAME="foo"
253KERNEL="console", NAME="TTY"
64682333
GKH
254EOF
255 },
256 {
257 desc => "valid label for device with no bus",
258 subsys => "tty",
259 devpath => "class/tty/console",
260 expected => "foo" ,
261 conf => <<EOF
ac28b86d
KS
262SYSFS_dev="5:1", NAME="foo"
263KERNEL="console", NAME="TTY"
772558f4
GKH
264EOF
265 },
266 {
ac28b86d 267 desc => "program and bus type match",
772558f4
GKH
268 subsys => "block",
269 devpath => "block/sda",
270 expected => "scsi-0:0:0:0" ,
271 conf => <<EOF
ac28b86d
KS
272BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
273BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
274BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
3d150dfb
KS
275EOF
276 },
277 {
278 desc => "symlink creation (same directory)",
279 subsys => "tty",
280 devpath => "class/tty/ttyUSB0",
281 expected => "visor0" ,
282 conf => <<EOF
ac28b86d 283KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
3d150dfb
KS
284EOF
285 },
286 {
287 desc => "symlink creation (relative link back)",
288 subsys => "block",
289 devpath => "block/sda/sda2",
290 expected => "1/2/a/b/symlink" ,
291 conf => <<EOF
ac28b86d 292BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
3d150dfb
KS
293EOF
294 },
295 {
296 desc => "symlink creation (relative link forward)",
297 subsys => "block",
298 devpath => "block/sda/sda2",
299 expected => "1/2/symlink" ,
300 conf => <<EOF
ac28b86d 301BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
3d150dfb
KS
302EOF
303 },
304 {
305 desc => "symlink creation (relative link back and forward)",
306 subsys => "block",
307 devpath => "block/sda/sda2",
308 expected => "1/2/c/d/symlink" ,
309 conf => <<EOF
ac28b86d 310BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
4763256c
KS
311EOF
312 },
313 {
314 desc => "multiple symlinks",
315 subsys => "tty",
316 devpath => "class/tty/ttyUSB0",
317 expected => "second-0" ,
318 conf => <<EOF
ac28b86d 319KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
724257d9
GKH
320EOF
321 },
322 {
323 desc => "sysfs parent heirachy",
324 subsys => "tty",
325 devpath => "class/tty/ttyUSB0",
326 expected => "visor" ,
327 conf => <<EOF
328SYSFS_idProduct="2008", NAME="visor"
a367f04e
GKH
329EOF
330 },
331);
332
333# set env
334$ENV{UDEV_TEST} = "yes";
335$ENV{SYSFS_PATH} = $sysfs;
72ffa78d 336$ENV{UDEV_CONFIG_FILE} = $main_conf;
a367f04e
GKH
337
338
339sub udev {
340 my ($action, $subsys, $devpath, $config) = @_;
341
342 $ENV{DEVPATH} = $devpath;
a367f04e
GKH
343
344 # create temporary config
345 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
346 print CONF $$config;
347 close CONF;
348
349 $ENV{ACTION} = $action;
350 system("$udev_bin $subsys");
351}
352
353
354# prepare
355system("rm -rf $udev_root");
356mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
357
358# test
e5fbfe0a 359my $error = 0;
a367f04e
GKH
360print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
361
72ffa78d
GKH
362# create initial config file
363open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
364print CONF "udev_root=\"$udev_root\"\n";
365print CONF "udev_db=\"$udev_db\"\n";
366print CONF "udev_rules=\"$conf_tmp\"\n";
367print CONF "udev_permissions=\"$perm\"\n";
368close CONF;
369
a367f04e 370foreach my $config (@tests) {
a367f04e 371 print "TEST: $config->{desc}\n";
ac28b86d 372 print "device \'$config->{devpath}\' expecting node \'$config->{expected}\'\n";
a367f04e
GKH
373
374 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
375 if (-e "$PWD/$udev_root$config->{expected}") {
376 print "add: ok ";
377 } else {
378 print "add: error\n";
379 system("tree $udev_root");
380 print "\n";
381 $error++;
a367f04e
GKH
382 }
383
384 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
3d150dfb
KS
385 if ((-e "$PWD/$udev_root$config->{expected}") ||
386 (-l "$PWD/$udev_root$config->{expected}")) {
a367f04e
GKH
387 print "remove: error\n\n";
388 system("tree $udev_root");
389 $error++;
390 } else {
391 print "remove: ok\n\n";
392 }
393}
394
395print "$error errors occured\n\n";
396
397# cleanup
36043f84 398unlink($udev_db);
a367f04e
GKH
399system("rm -rf $udev_root");
400unlink($conf_tmp);
72ffa78d 401unlink($main_conf);
a367f04e 402