]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/udev-test.pl
[PATCH] added RFC-dev.d document detailing how /etc/dev.d/ works.
[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 {
fa19f181
KS
36 desc => "label test of scsi disc",
37 subsys => "block",
38 devpath => "block/sda",
c013b034 39 exp_name => "boot_disk" ,
fa19f181 40 conf => <<EOF
16378373 41BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
ac28b86d 42KERNEL="ttyUSB0", NAME="visor"
a367f04e
GKH
43EOF
44 },
45 {
fa19f181
KS
46 desc => "label test of scsi partition",
47 subsys => "block",
48 devpath => "block/sda/sda1",
c013b034 49 exp_name => "boot_disk1" ,
fa19f181 50 conf => <<EOF
16378373 51BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="boot_disk%n"
83be97ba
KS
52EOF
53 },
54 {
fa19f181
KS
55 desc => "label test of pattern match",
56 subsys => "block",
57 devpath => "block/sda/sda1",
c013b034 58 exp_name => "boot_disk1" ,
fa19f181 59 conf => <<EOF
16378373
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 {
fa19f181
KS
67 desc => "label test of multiple sysfs files",
68 subsys => "block",
69 devpath => "block/sda/sda1",
70 exp_name => "boot_disk1" ,
71 conf => <<EOF
16378373
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 {
fa19f181
KS
77 desc => "label test of max sysfs files",
78 subsys => "block",
79 devpath => "block/sda/sda1",
80 exp_name => "boot_disk1" ,
81 conf => <<EOF
16378373
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 {
fa19f181
KS
87 desc => "catch device by *",
88 subsys => "tty",
89 devpath => "class/tty/ttyUSB0",
90 exp_name => "visor/0" ,
91 conf => <<EOF
ac28b86d 92KERNEL="ttyUSB*", NAME="visor/%n"
2e317184
GKH
93EOF
94 },
95 {
fa19f181
KS
96 desc => "catch device by * - take 2",
97 subsys => "tty",
98 devpath => "class/tty/ttyUSB0",
99 exp_name => "visor/0" ,
100 conf => <<EOF
2e317184
GKH
101KERNEL="*USB1", NAME="bad"
102KERNEL="*USB0", NAME="visor/%n"
9f1da361
KS
103EOF
104 },
105 {
fa19f181
KS
106 desc => "catch device by ?",
107 subsys => "tty",
108 devpath => "class/tty/ttyUSB0",
109 exp_name => "visor/0" ,
110 conf => <<EOF
ac28b86d
KS
111KERNEL="ttyUSB??*", NAME="visor/%n-1"
112KERNEL="ttyUSB??", NAME="visor/%n-2"
113KERNEL="ttyUSB?", NAME="visor/%n"
9f1da361
KS
114EOF
115 },
116 {
fa19f181
KS
117 desc => "catch device by character class",
118 subsys => "tty",
119 devpath => "class/tty/ttyUSB0",
120 exp_name => "visor/0" ,
121 conf => <<EOF
ac28b86d
KS
122KERNEL="ttyUSB[A-Z]*", NAME="visor/%n-1"
123KERNEL="ttyUSB?[0-9]", NAME="visor/%n-2"
124KERNEL="ttyUSB[0-9]*", NAME="visor/%n"
a367f04e
GKH
125EOF
126 },
127 {
fa19f181
KS
128 desc => "replace kernel name",
129 subsys => "tty",
130 devpath => "class/tty/ttyUSB0",
131 exp_name => "visor" ,
132 conf => <<EOF
ac28b86d 133KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
134EOF
135 },
136 {
fa19f181
KS
137 desc => "Handle comment lines in config file (and replace kernel name)",
138 subsys => "tty",
139 devpath => "class/tty/ttyUSB0",
140 exp_name => "visor" ,
141 conf => <<EOF
281ff00a 142# this is a comment
ac28b86d 143KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
144
145EOF
146 },
147 {
fa19f181
KS
148 desc => "Handle comment lines in config file with whitespace (and replace kernel name)",
149 subsys => "tty",
150 devpath => "class/tty/ttyUSB0",
151 exp_name => "visor" ,
152 conf => <<EOF
281ff00a 153 # this is a comment with whitespace before the comment
ac28b86d 154KERNEL="ttyUSB0", NAME="visor"
281ff00a
GKH
155
156EOF
157 },
158 {
fa19f181
KS
159 desc => "Handle empty lines in config file (and replace kernel name)",
160 subsys => "tty",
161 devpath => "class/tty/ttyUSB0",
162 exp_name => "visor" ,
163 conf => <<EOF
281ff00a 164
ac28b86d 165KERNEL="ttyUSB0", NAME="visor"
281ff00a 166
5499d319
KS
167EOF
168 },
169 {
fa19f181
KS
170 desc => "subdirectory handling",
171 subsys => "tty",
172 devpath => "class/tty/ttyUSB0",
173 exp_name => "sub/direct/ory/visor" ,
174 conf => <<EOF
ac28b86d 175KERNEL="ttyUSB0", NAME="sub/direct/ory/visor"
a367f04e
GKH
176EOF
177 },
178 {
fa19f181
KS
179 desc => "place on bus of scsi partition",
180 subsys => "block",
181 devpath => "block/sda/sda3",
182 exp_name => "first_disk3" ,
183 conf => <<EOF
ac28b86d 184BUS="scsi", PLACE="0:0:0:0", NAME="first_disk%n"
a367f04e
GKH
185EOF
186 },
187 {
fa19f181
KS
188 desc => "test NAME substitution chars",
189 subsys => "block",
190 devpath => "block/sda/sda3",
191 exp_name => "Major:8:minor:3:kernelnumber:3:bus:0:0:0:0" ,
192 conf => <<EOF
ac28b86d 193BUS="scsi", PLACE="0:0:0:0", NAME="Major:%M:minor:%m:kernelnumber:%n:bus:%b"
63ead27c
KS
194EOF
195 },
196 {
fa19f181
KS
197 desc => "test NAME substitution chars (with length limit)",
198 subsys => "block",
199 devpath => "block/sda/sda3",
200 exp_name => "M8-m3-n3-b0:0-sIBM" ,
201 conf => <<EOF
63ead27c 202BUS="scsi", PLACE="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}"
59525115
GKH
203EOF
204 },
205 {
fa19f181
KS
206 desc => "old style SYSFS_ attribute",
207 subsys => "block",
208 devpath => "block/sda",
209 exp_name => "good" ,
210 conf => <<EOF
59525115 211BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="good"
a27cd06c
KS
212EOF
213 },
214 {
fa19f181
KS
215 desc => "sustitution of sysfs value (%s{file})",
216 subsys => "block",
217 devpath => "block/sda",
218 exp_name => "disk-IBM-ESXS-sda" ,
219 conf => <<EOF
16378373 220BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="disk-%s{vendor}-%k"
a27cd06c 221KERNEL="ttyUSB0", NAME="visor"
a367f04e
GKH
222EOF
223 },
224 {
fa19f181
KS
225 desc => "program result substitution",
226 subsys => "block",
227 devpath => "block/sda/sda3",
228 exp_name => "special-device-3" ,
229 conf => <<EOF
ac28b86d
KS
230BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
231BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
232BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
233BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
234BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
f3b04a2e
GKH
235EOF
236 },
237 {
fa19f181
KS
238 desc => "program result substitution",
239 subsys => "block",
240 devpath => "block/sda/sda3",
241 exp_name => "test-0:0:0:0" ,
242 conf => <<EOF
ac28b86d 243BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
8ffb636f
KS
244EOF
245 },
246 {
fa19f181
KS
247 desc => "program with escaped format char (tricky: callout returns format char!)",
248 subsys => "block",
249 devpath => "block/sda/sda3",
250 exp_name => "escape-3" ,
251 conf => <<EOF
8ffb636f 252BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
dde05ccb
GKH
253EOF
254 },
255 {
fa19f181
KS
256 desc => "program with lots of arguments",
257 subsys => "block",
258 devpath => "block/sda/sda3",
259 exp_name => "foo9" ,
260 conf => <<EOF
dde05ccb 261BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="%c{7}"
35b38379
KS
262EOF
263 },
264 {
fa19f181
KS
265 desc => "program with subshell",
266 subsys => "block",
267 devpath => "block/sda/sda3",
268 exp_name => "bar9" ,
269 conf => <<EOF
35b38379
KS
270BUS="scsi", PROGRAM="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed s/foo9/bar9/'", KERNEL="sda3", NAME="%c{7}"
271EOF
272 },
273 {
fa19f181
KS
274 desc => "program arguments combined with apostrophes",
275 subsys => "block",
276 devpath => "block/sda/sda3",
277 exp_name => "foo7" ,
278 conf => <<EOF
35b38379 279BUS="scsi", PROGRAM="/bin/echo -n 'foo3 foo4' 'foo5 foo6 foo7 foo8'", KERNEL="sda3", NAME="%c{5}"
56c963dc
PM
280EOF
281 },
282 {
fa19f181
KS
283 desc => "characters before the %c{N} substitution",
284 subsys => "block",
285 devpath => "block/sda/sda3",
286 exp_name => "my-foo9" ,
287 conf => <<EOF
56c963dc
PM
288BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{7}"
289EOF
290 },
291 {
fa19f181
KS
292 desc => "substitute the second to last argument",
293 subsys => "block",
294 devpath => "block/sda/sda3",
295 exp_name => "my-foo8" ,
296 conf => <<EOF
56c963dc 297BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}"
b1c5e333
KS
298EOF
299 },
300 {
fa19f181
KS
301 desc => "program result substitution (numbered part of)",
302 subsys => "block",
303 devpath => "block/sda/sda3",
304 exp_name => "link1" ,
305 conf => <<EOF
88f09368 306BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
558f80ba
KS
307EOF
308 },
309 {
fa19f181
KS
310 desc => "program result substitution (numbered part of+)",
311 subsys => "block",
312 devpath => "block/sda/sda3",
313 exp_name => "link3" ,
314 conf => <<EOF
558f80ba 315BUS="scsi", PROGRAM="/bin/echo -n node link1 link2 link3 link4", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2+}"
8ff8bbba
GKH
316EOF
317 },
318 {
fa19f181
KS
319 desc => "invalid program for device with no bus",
320 subsys => "tty",
321 devpath => "class/tty/console",
322 exp_name => "TTY" ,
323 conf => <<EOF
ac28b86d
KS
324BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
325KERNEL="console", NAME="TTY"
1d936fbc
GKH
326EOF
327 },
328 {
fa19f181
KS
329 desc => "valid program for device with no bus",
330 subsys => "tty",
331 devpath => "class/tty/console",
332 exp_name => "foo" ,
333 conf => <<EOF
ac28b86d
KS
334PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
335KERNEL="console", NAME="TTY"
64682333
GKH
336EOF
337 },
338 {
fa19f181
KS
339 desc => "invalid label for device with no bus",
340 subsys => "tty",
341 devpath => "class/tty/console",
342 exp_name => "TTY" ,
343 conf => <<EOF
16378373 344BUS="foo", SYSFS{dev}="5:1", NAME="foo"
ac28b86d 345KERNEL="console", NAME="TTY"
64682333
GKH
346EOF
347 },
348 {
fa19f181
KS
349 desc => "valid label for device with no bus",
350 subsys => "tty",
351 devpath => "class/tty/console",
352 exp_name => "foo" ,
353 conf => <<EOF
16378373 354SYSFS{dev}="5:1", NAME="foo"
ac28b86d 355KERNEL="console", NAME="TTY"
772558f4
GKH
356EOF
357 },
358 {
fa19f181
KS
359 desc => "program and bus type match",
360 subsys => "block",
361 devpath => "block/sda",
362 exp_name => "scsi-0:0:0:0" ,
363 conf => <<EOF
ac28b86d
KS
364BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
365BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
366BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
3d150dfb
KS
367EOF
368 },
369 {
fa19f181
KS
370 desc => "symlink creation (same directory)",
371 subsys => "tty",
372 devpath => "class/tty/ttyUSB0",
373 exp_name => "visor0" ,
374 conf => <<EOF
ac28b86d 375KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
3d150dfb
KS
376EOF
377 },
378 {
fa19f181
KS
379 desc => "symlink creation (relative link back)",
380 subsys => "block",
381 devpath => "block/sda/sda2",
382 exp_name => "1/2/a/b/symlink" ,
383 conf => <<EOF
16378373 384BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
3d150dfb
KS
385EOF
386 },
387 {
fa19f181
KS
388 desc => "symlink creation (relative link forward)",
389 subsys => "block",
390 devpath => "block/sda/sda2",
391 exp_name => "1/2/symlink" ,
392 conf => <<EOF
16378373 393BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
3d150dfb
KS
394EOF
395 },
396 {
fa19f181
KS
397 desc => "symlink creation (relative link back and forward)",
398 subsys => "block",
399 devpath => "block/sda/sda2",
400 exp_name => "1/2/c/d/symlink" ,
401 conf => <<EOF
16378373 402BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
4763256c
KS
403EOF
404 },
405 {
fa19f181
KS
406 desc => "multiple symlinks",
407 subsys => "tty",
408 devpath => "class/tty/ttyUSB0",
409 exp_name => "second-0" ,
410 conf => <<EOF
ac28b86d 411KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
724257d9
GKH
412EOF
413 },
414 {
fa19f181
KS
415 desc => "create all possible partitions",
416 subsys => "block",
417 devpath => "block/sda",
418 exp_name => "boot_disk15" ,
419 conf => <<EOF
16378373 420BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME{all_partitions}="boot_disk"
50e5de03
KS
421EOF
422 },
423 {
fa19f181
KS
424 desc => "sysfs parent hierarchy",
425 subsys => "tty",
426 devpath => "class/tty/ttyUSB0",
427 exp_name => "visor" ,
428 conf => <<EOF
16378373 429SYSFS{idProduct}="2008", NAME="visor"
f0142622
GKH
430EOF
431 },
432 {
fa19f181
KS
433 desc => "name test with ! in the name",
434 subsys => "block",
435 devpath => "block/rd!c0d0",
436 exp_name => "rd/c0d0" ,
437 conf => <<EOF
f0142622
GKH
438BUS="scsi", NAME="%k"
439KERNEL="ttyUSB0", NAME="visor"
b9fc973b
GKH
440EOF
441 },
442 {
fa19f181
KS
443 desc => "name test with ! in the name, but no matching rule",
444 subsys => "block",
445 devpath => "block/rd!c0d0",
446 exp_name => "rd/c0d0" ,
447 conf => <<EOF
b9fc973b 448KERNEL="ttyUSB0", NAME="visor"
93656247
PM
449EOF
450 },
451 {
fa19f181
KS
452 desc => "ID rule",
453 subsys => "block",
454 devpath => "block/sda",
455 exp_name => "scsi-0:0:0:0",
456 conf => <<EOF
93656247
PM
457BUS="usb", ID="0:0:0:0", NAME="not-scsi"
458BUS="scsi", ID="0:0:0:1", NAME="no-match"
459BUS="scsi", ID=":0", NAME="short-id"
460BUS="scsi", ID="/0:0:0:0", NAME="no-match"
461BUS="scsi", ID="0:0:0:0", NAME="scsi-0:0:0:0"
462EOF
463 },
464 {
fa19f181
KS
465 desc => "ID wildcard all",
466 subsys => "block",
467 devpath => "block/sda",
468 exp_name => "scsi-0:0:0:0",
469 conf => <<EOF
93656247
PM
470BUS="scsi", ID="*:1", NAME="no-match"
471BUS="scsi", ID="*:0:1", NAME="no-match"
472BUS="scsi", ID="*:0:0:1", NAME="no-match"
473BUS="scsi", ID="*", NAME="scsi-0:0:0:0"
474BUS="scsi", ID="0:0:0:0", NAME="bad"
475EOF
476 },
477 {
fa19f181
KS
478 desc => "ID wildcard partial",
479 subsys => "block",
480 devpath => "block/sda",
481 exp_name => "scsi-0:0:0:0",
482 conf => <<EOF
93656247
PM
483BUS="scsi", ID="*:0", NAME="scsi-0:0:0:0"
484BUS="scsi", ID="0:0:0:0", NAME="bad"
485EOF
486 },
487 {
fa19f181
KS
488 desc => "ID wildcard partial 2",
489 subsys => "block",
490 devpath => "block/sda",
491 exp_name => "scsi-0:0:0:0",
492 conf => <<EOF
93656247
PM
493BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
494BUS="scsi", ID="0:0:0:0", NAME="bad"
d5f91372
KS
495EOF
496 },
497 {
fa19f181
KS
498 desc => "ignore SYSFS attribute whitespace",
499 subsys => "block",
500 devpath => "block/sda",
501 exp_name => "ignored",
502 conf => <<EOF
d5f91372
KS
503BUS="scsi", SYSFS{whitespace_test}="WHITE SPACE", NAME="ignored"
504EOF
505 },
506 {
fa19f181
KS
507 desc => "do not ignore SYSFS attribute whitespace",
508 subsys => "block",
509 devpath => "block/sda",
510 exp_name => "matched-with-space",
511 conf => <<EOF
d5f91372
KS
512BUS="scsi", SYSFS{whitespace_test}="WHITE SPACE ", NAME="wrong-to-ignore"
513BUS="scsi", SYSFS{whitespace_test}="WHITE SPACE ", NAME="matched-with-space"
0a5417a0
KS
514EOF
515 },
516 {
fa19f181
KS
517 desc => "SYMLINK only rule",
518 subsys => "block",
519 devpath => "block/sda",
520 exp_name => "symlink-only2",
521 conf => <<EOF
0a5417a0
KS
522BUS="scsi", KERNEL="sda", SYMLINK="symlink-only1"
523BUS="scsi", KERNEL="sda", SYMLINK="symlink-only2"
524BUS="scsi", KERNEL="sda", NAME="link", SYMLINK="symlink0"
c612a0ac
KS
525EOF
526 },
527 {
fa19f181
KS
528 desc => "permissions test",
529 subsys => "block",
530 devpath => "block/sda",
531 exp_name => "node",
c013b034 532 exp_perms => "5000::0444",
fa19f181
KS
533 conf => <<EOF
534BUS="scsi", KERNEL="sda", NAME="node", OWNER="5000", MODE="0444"
535EOF
536 },
537 {
538 desc => "major/minor number test",
539 subsys => "block",
540 devpath => "block/sda",
541 exp_name => "node",
fa19f181
KS
542 exp_majorminor => "8:0",
543 conf => <<EOF
7ddb6f47 544BUS="scsi", KERNEL="sda", NAME="node"
a5e8d2b4
GKH
545EOF
546 },
547 {
548 desc => "big minor number test",
549 subsys => "i2c-dev",
550 devpath => "class/i2c-dev/i2c-300",
551 exp_name => "node",
552 exp_majorminor => "89:300",
553 conf => <<EOF
554KERNEL="i2c-300", NAME="node"
7d12d4e1
GKH
555EOF
556 },
557 {
558 desc => "big major number test",
559 subsys => "i2c-dev",
560 devpath => "class/i2c-dev/i2c-fake1",
561 exp_name => "node",
562 exp_majorminor => "4095:1",
563 conf => <<EOF
564KERNEL="i2c-fake1", NAME="node"
565EOF
566 },
567 {
568 desc => "big major and big minor number test",
569 subsys => "i2c-dev",
570 devpath => "class/i2c-dev/i2c-fake2",
571 exp_name => "node",
572 exp_majorminor => "4094:89999",
573 conf => <<EOF
574KERNEL="i2c-fake2", NAME="node"
a367f04e
GKH
575EOF
576 },
577);
578
579# set env
580$ENV{UDEV_TEST} = "yes";
581$ENV{SYSFS_PATH} = $sysfs;
72ffa78d 582$ENV{UDEV_CONFIG_FILE} = $main_conf;
a367f04e
GKH
583
584
585sub udev {
586 my ($action, $subsys, $devpath, $config) = @_;
587
588 $ENV{DEVPATH} = $devpath;
a367f04e
GKH
589
590 # create temporary config
591 open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
592 print CONF $$config;
593 close CONF;
594
595 $ENV{ACTION} = $action;
596 system("$udev_bin $subsys");
597}
598
e5fbfe0a 599my $error = 0;
72ffa78d 600
2e317184
GKH
601sub run_test {
602 my ($config, $number) = @_;
fa19f181 603
2e317184 604 print "TEST $number: $config->{desc}\n";
fa19f181 605 print "device \'$config->{devpath}\' expecting node \'$config->{exp_name}\'\n";
a367f04e
GKH
606
607 udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
fa19f181
KS
608 if (-e "$PWD/$udev_root$config->{exp_name}") {
609
610 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
611 $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$config->{exp_name}");
612
c013b034 613 if (defined($config->{exp_perms})) {
c612a0ac 614 my $wrong = 0;
c013b034 615 $config->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
c612a0ac
KS
616 if ($1 ne "") {
617 if ($uid != $1) { $wrong = 1; };
618 }
619 if ($2 ne "") {
620 if ($gid != $2) { $wrong = 1; };
621 }
622 if ($3 ne "") {
623 if (($mode & 07777) != oct($3)) { $wrong = 1; };
624 }
7ddb6f47
GKH
625 if ($wrong == 0) {
626 print "permissions: ok ";
627 } else {
c612a0ac
KS
628 printf "expected permissions are: %i:%i:%#o\n", $1, $2, oct($3);
629 printf "created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
b9a88fe7 630 $error++;
c612a0ac
KS
631 }
632 }
fa19f181
KS
633
634 if (defined($config->{exp_majorminor})) {
635 my $major = ($rdev >> 8) & 0xfff;
636 my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
637
638 my $wrong = 0;
639 $config->{exp_majorminor} =~ m/^(.*):(.*)$/;
640 if ($1 ne "") {
641 if ($major != $1) { $wrong = 1; };
642 }
643 if ($2 ne "") {
644 if ($minor != $2) { $wrong = 1; };
645 }
7ddb6f47
GKH
646 if ($wrong == 0) {
647 print "major:minor: ok ";
648 } else {
fa19f181
KS
649 printf "expected major:minor is: %i:%i\n", $1, $2;
650 printf "created major:minor is : %i:%i\n", $major, $minor;
b9a88fe7 651 $error++;
fa19f181
KS
652 }
653 }
654
a367f04e
GKH
655 print "add: ok ";
656 } else {
657 print "add: error\n";
658 system("tree $udev_root");
659 print "\n";
660 $error++;
a367f04e
GKH
661 }
662
663 udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
fa19f181
KS
664 if ((-e "$PWD/$udev_root$config->{exp_name}") ||
665 (-l "$PWD/$udev_root$config->{exp_name}")) {
a367f04e
GKH
666 print "remove: error\n\n";
667 system("tree $udev_root");
668 $error++;
669 } else {
670 print "remove: ok\n\n";
671 }
672}
673
2e317184
GKH
674# prepare
675system("rm -rf $udev_root");
676mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
677
678# create initial config file
679open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
680print CONF "udev_root=\"$udev_root\"\n";
681print CONF "udev_db=\"$udev_db\"\n";
682print CONF "udev_rules=\"$conf_tmp\"\n";
683print CONF "udev_permissions=\"$perm\"\n";
684close CONF;
685
686my $test_num = 1;
687
688if ($ARGV[0]) {
689 # only run one test
690 $test_num = $ARGV[0];
691 print "udev-test will run test number $test_num only\n";
692
b9fc973b 693 run_test($tests[$test_num-1], $test_num);
2e317184
GKH
694} else {
695 # test all
696 print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
697
698 foreach my $config (@tests) {
699 run_test($config, $test_num);
700 $test_num++;
701
702 }
703}
704
a367f04e
GKH
705print "$error errors occured\n\n";
706
707# cleanup
36043f84 708unlink($udev_db);
a367f04e
GKH
709system("rm -rf $udev_root");
710unlink($conf_tmp);
72ffa78d 711unlink($main_conf);
a367f04e 712