chiark / gitweb /
[PATCH] netdev - udevdb+dev.d changes
[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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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                 exp_name        => "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            => "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
202 BUS="scsi", PLACE="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}"
203 EOF
204         },
205         {
206                 desc            => "old style SYSFS_ attribute",
207                 subsys          => "block",
208                 devpath         => "/block/sda",
209                 exp_name        => "good" ,
210                 conf            => <<EOF
211 BUS="scsi", SYSFS_vendor="IBM-ESXS", NAME="good"
212 EOF
213         },
214         {
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
220 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="disk-%s{vendor}-%k"
221 KERNEL="ttyUSB0", NAME="visor"
222 EOF
223         },
224         {
225                 desc            => "program result substitution",
226                 subsys          => "block",
227                 devpath         => "/block/sda/sda3",
228                 exp_name        => "special-device-3" ,
229                 conf            => <<EOF
230 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="-special-*", NAME="%c-1-%n"
231 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special--*", NAME="%c-2-%n"
232 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-device-", NAME="%c-3-%n"
233 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-devic", NAME="%c-4-%n"
234 BUS="scsi", PROGRAM="/bin/echo -n special-device", RESULT="special-*", NAME="%c-%n"
235 EOF
236         },
237         {
238                 desc            => "program result substitution (newline removal)",
239                 subsys          => "block",
240                 devpath         => "/block/sda/sda3",
241                 exp_name        => "newline_removed" ,
242                 conf            => <<EOF
243 BUS="scsi", PROGRAM="/bin/echo test", RESULT="test", NAME="newline_removed"
244 EOF
245         },
246         {
247                 desc            => "program result substitution",
248                 subsys          => "block",
249                 devpath         => "/block/sda/sda3",
250                 exp_name        => "test-0:0:0:0" ,
251                 conf            => <<EOF
252 BUS="scsi", PROGRAM="/bin/echo -n test-%b", RESULT="test-0:0*", NAME="%c"
253 EOF
254         },
255         {
256                 desc            => "program with escaped format char (tricky: callout returns format char!)",
257                 subsys          => "block",
258                 devpath         => "/block/sda/sda3",
259                 exp_name        => "escape-3" ,
260                 conf            => <<EOF
261 BUS="scsi", PROGRAM="/bin/echo -n escape-%%n", KERNEL="sda3", NAME="%c"
262 EOF
263         },
264         {
265                 desc            => "program with lots of arguments",
266                 subsys          => "block",
267                 devpath         => "/block/sda/sda3",
268                 exp_name        => "foo9" ,
269                 conf            => <<EOF
270 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="%c{7}"
271 EOF
272         },
273         {
274                 desc            => "program with subshell",
275                 subsys          => "block",
276                 devpath         => "/block/sda/sda3",
277                 exp_name        => "bar9" ,
278                 conf            => <<EOF
279 BUS="scsi", PROGRAM="/bin/sh -c 'echo foo3 foo4 foo5 foo6 foo7 foo8 foo9 | sed  s/foo9/bar9/'", KERNEL="sda3", NAME="%c{7}"
280 EOF
281         },
282         {
283                 desc            => "program arguments combined with apostrophes",
284                 subsys          => "block",
285                 devpath         => "/block/sda/sda3",
286                 exp_name        => "foo7" ,
287                 conf            => <<EOF
288 BUS="scsi", PROGRAM="/bin/echo -n 'foo3 foo4'   'foo5   foo6   foo7 foo8'", KERNEL="sda3", NAME="%c{5}"
289 EOF
290         },
291         {
292                 desc            => "characters before the %c{N} substitution",
293                 subsys          => "block",
294                 devpath         => "/block/sda/sda3",
295                 exp_name        => "my-foo9" ,
296                 conf            => <<EOF
297 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{7}"
298 EOF
299         },
300         {
301                 desc            => "substitute the second to last argument",
302                 subsys          => "block",
303                 devpath         => "/block/sda/sda3",
304                 exp_name        => "my-foo8" ,
305                 conf            => <<EOF
306 BUS="scsi", PROGRAM="/bin/echo -n foo3 foo4 foo5 foo6 foo7 foo8 foo9", KERNEL="sda3", NAME="my-%c{6}"
307 EOF
308         },
309         {
310                 desc            => "program result substitution (numbered part of)",
311                 subsys          => "block",
312                 devpath         => "/block/sda/sda3",
313                 exp_name        => "link1" ,
314                 conf            => <<EOF
315 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2} %c{3}"
316 EOF
317         },
318         {
319                 desc            => "program result substitution (numbered part of+)",
320                 subsys          => "block",
321                 devpath         => "/block/sda/sda3",
322                 exp_name        => "link3" ,
323                 conf            => <<EOF
324 BUS="scsi", PROGRAM="/bin/echo -n node link1 link2 link3 link4", RESULT="node *", NAME="%c{1}", SYMLINK="%c{2+}"
325 EOF
326         },
327         {
328                 desc            => "invalid program for device with no bus",
329                 subsys          => "tty",
330                 devpath         => "/class/tty/console",
331                 exp_name        => "TTY" ,
332                 conf            => <<EOF
333 BUS="scsi", PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
334 KERNEL="console", NAME="TTY"
335 EOF
336         },
337         {
338                 desc            => "valid program for device with no bus",
339                 subsys          => "tty",
340                 devpath         => "/class/tty/console",
341                 exp_name        => "foo" ,
342                 conf            => <<EOF
343 PROGRAM="/bin/echo -n foo", RESULT="foo", NAME="foo"
344 KERNEL="console", NAME="TTY"
345 EOF
346         },
347         {
348                 desc            => "invalid label for device with no bus",
349                 subsys          => "tty",
350                 devpath         => "/class/tty/console",
351                 exp_name        => "TTY" ,
352                 conf            => <<EOF
353 BUS="foo", SYSFS{dev}="5:1", NAME="foo"
354 KERNEL="console", NAME="TTY"
355 EOF
356         },
357         {
358                 desc            => "valid label for device with no bus",
359                 subsys          => "tty",
360                 devpath         => "/class/tty/console",
361                 exp_name        => "foo" ,
362                 conf            => <<EOF
363 SYSFS{dev}="5:1", NAME="foo"
364 KERNEL="console", NAME="TTY"
365 EOF
366         },
367         {
368                 desc            => "program and bus type match",
369                 subsys          => "block",
370                 devpath         => "/block/sda",
371                 exp_name        => "scsi-0:0:0:0" ,
372                 conf            => <<EOF
373 BUS="usb", PROGRAM="/bin/echo -n usb-%b", NAME="%c"
374 BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", NAME="%c"
375 BUS="foo", PROGRAM="/bin/echo -n foo-%b", NAME="%c"
376 EOF
377         },
378         {
379                 desc            => "symlink creation (same directory)",
380                 subsys          => "tty",
381                 devpath         => "/class/tty/ttyUSB0",
382                 exp_name        => "visor0" ,
383                 conf            => <<EOF
384 KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="visor%n"
385 EOF
386         },
387         {
388                 desc            => "symlink creation (relative link back)",
389                 subsys          => "block",
390                 devpath         => "/block/sda/sda2",
391                 exp_name        => "1/2/a/b/symlink" ,
392                 conf            => <<EOF
393 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/node", SYMLINK="1/2/a/b/symlink"
394 EOF
395         },
396         {
397                 desc            => "symlink creation (relative link forward)",
398                 subsys          => "block",
399                 devpath         => "/block/sda/sda2",
400                 exp_name        => "1/2/symlink" ,
401                 conf            => <<EOF
402 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/symlink"
403 EOF
404         },
405         {
406                 desc            => "symlink creation (relative link back and forward)",
407                 subsys          => "block",
408                 devpath         => "/block/sda/sda2",
409                 exp_name        => "1/2/c/d/symlink" ,
410                 conf            => <<EOF
411 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
412 EOF
413         },
414         {
415                 desc            => "multiple symlinks",
416                 subsys          => "tty",
417                 devpath         => "/class/tty/ttyUSB0",
418                 exp_name        => "second-0" ,
419                 conf            => <<EOF
420 KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
421 EOF
422         },
423         {
424                 desc            => "create all possible partitions",
425                 subsys          => "block",
426                 devpath         => "/block/sda",
427                 exp_name        => "boot_disk15" ,
428                 conf            => <<EOF
429 BUS="scsi", SYSFS{vendor}="IBM-ESXS", NAME{all_partitions}="boot_disk"
430 EOF
431         },
432         {
433                 desc            => "sysfs parent hierarchy",
434                 subsys          => "tty",
435                 devpath         => "/class/tty/ttyUSB0",
436                 exp_name        => "visor" ,
437                 conf            => <<EOF
438 SYSFS{idProduct}="2008", NAME="visor"
439 EOF
440         },
441         {
442                 desc            => "name test with ! in the name",
443                 subsys          => "block",
444                 devpath         => "/block/rd!c0d0",
445                 exp_name        => "rd/c0d0" ,
446                 conf            => <<EOF
447 BUS="scsi", NAME="%k"
448 KERNEL="ttyUSB0", NAME="visor"
449 EOF
450         },
451         {
452                 desc            => "name test with ! in the name, but no matching rule",
453                 subsys          => "block",
454                 devpath         => "/block/rd!c0d0",
455                 exp_name        => "rd/c0d0" ,
456                 conf            => <<EOF
457 KERNEL="ttyUSB0", NAME="visor"
458 EOF
459         },
460         {
461                 desc            => "ID rule",
462                 subsys          => "block",
463                 devpath         => "/block/sda",
464                 exp_name        => "scsi-0:0:0:0",
465                 conf            => <<EOF
466 BUS="usb", ID="0:0:0:0", NAME="not-scsi"
467 BUS="scsi", ID="0:0:0:1", NAME="no-match"
468 BUS="scsi", ID=":0", NAME="short-id"
469 BUS="scsi", ID="/0:0:0:0", NAME="no-match"
470 BUS="scsi", ID="0:0:0:0", NAME="scsi-0:0:0:0"
471 EOF
472         },
473         {
474                 desc            => "ID wildcard all",
475                 subsys          => "block",
476                 devpath         => "/block/sda",
477                 exp_name        => "scsi-0:0:0:0",
478                 conf            => <<EOF
479 BUS="scsi", ID="*:1", NAME="no-match"
480 BUS="scsi", ID="*:0:1", NAME="no-match"
481 BUS="scsi", ID="*:0:0:1", NAME="no-match"
482 BUS="scsi", ID="*", NAME="scsi-0:0:0:0"
483 BUS="scsi", ID="0:0:0:0", NAME="bad"
484 EOF
485         },
486         {
487                 desc            => "ID wildcard partial",
488                 subsys          => "block",
489                 devpath         => "/block/sda",
490                 exp_name        => "scsi-0:0:0:0",
491                 conf            => <<EOF
492 BUS="scsi", ID="*:0", NAME="scsi-0:0:0:0"
493 BUS="scsi", ID="0:0:0:0", NAME="bad"
494 EOF
495         },
496         {
497                 desc            => "ID wildcard partial 2",
498                 subsys          => "block",
499                 devpath         => "/block/sda",
500                 exp_name        => "scsi-0:0:0:0",
501                 conf            => <<EOF
502 BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
503 BUS="scsi", ID="0:0:0:0", NAME="bad"
504 EOF
505         },
506         {
507                 desc            => "ignore SYSFS attribute whitespace",
508                 subsys          => "block",
509                 devpath         => "/block/sda",
510                 exp_name        => "ignored",
511                 conf            => <<EOF
512 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE", NAME="ignored"
513 EOF
514         },
515         {
516                 desc            => "do not ignore SYSFS attribute whitespace",
517                 subsys          => "block",
518                 devpath         => "/block/sda",
519                 exp_name        => "matched-with-space",
520                 conf            => <<EOF
521 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE ", NAME="wrong-to-ignore"
522 BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE   ", NAME="matched-with-space"
523 EOF
524         },
525         {
526                 desc            => "SYMLINK only rule",
527                 subsys          => "block",
528                 devpath         => "/block/sda",
529                 exp_name        => "symlink-only2",
530                 conf            => <<EOF
531 BUS="scsi", KERNEL="sda", SYMLINK="symlink-only1"
532 BUS="scsi", KERNEL="sda", SYMLINK="symlink-only2"
533 BUS="scsi", KERNEL="sda", NAME="link", SYMLINK="symlink0"
534 EOF
535         },
536         {
537                 desc            => "permissions test",
538                 subsys          => "block",
539                 devpath         => "/block/sda",
540                 exp_name        => "node",
541                 exp_perms       => "5000::0444",
542                 conf            => <<EOF
543 BUS="scsi", KERNEL="sda", NAME="node", OWNER="5000", MODE="0444"
544 EOF
545         },
546         {
547                 desc            => "major/minor number test",
548                 subsys          => "block",
549                 devpath         => "/block/sda",
550                 exp_name        => "node",
551                 exp_majorminor  => "8:0",
552                 conf            => <<EOF
553 BUS="scsi", KERNEL="sda", NAME="node"
554 EOF
555         },
556         {
557                 desc            => "big minor number test",
558                 subsys          => "i2c-dev",
559                 devpath         => "/class/i2c-dev/i2c-300",
560                 exp_name        => "node",
561                 exp_majorminor  => "89:300",
562                 conf            => <<EOF
563 KERNEL="i2c-300", NAME="node"
564 EOF
565         },
566         {
567                 desc            => "big major number test",
568                 subsys          => "i2c-dev",
569                 devpath         => "/class/i2c-dev/i2c-fake1",
570                 exp_name        => "node",
571                 exp_majorminor  => "4095:1",
572                 conf            => <<EOF
573 KERNEL="i2c-fake1", NAME="node"
574 EOF
575         },
576         {
577                 desc            => "big major and big minor number test",
578                 subsys          => "i2c-dev",
579                 devpath         => "/class/i2c-dev/i2c-fake2",
580                 exp_name        => "node",
581                 exp_majorminor  => "4094:89999",
582                 conf            => <<EOF
583 KERNEL="i2c-fake2", NAME="node"
584 EOF
585         },
586 );
587
588 # set env
589 $ENV{UDEV_TEST} = "yes";
590 $ENV{SYSFS_PATH} = $sysfs;
591 $ENV{UDEV_CONFIG_FILE} = $main_conf;
592 $ENV{UDEV_NO_SLEEP} = "yes";
593 $ENV{UDEV_NO_DEVD} = "yes";
594
595
596 sub udev {
597         my ($action, $subsys, $devpath, $config) = @_;
598
599         $ENV{DEVPATH} = $devpath;
600
601         # create temporary config
602         open CONF, ">$conf_tmp" || die "unable to create config file: $conf_tmp";
603         print CONF $$config;
604         close CONF;
605
606         $ENV{ACTION} = $action;
607         system("$udev_bin $subsys");
608 }
609
610 my $error = 0;
611
612 sub run_test {
613         my ($config, $number) = @_;
614
615         print "TEST $number: $config->{desc}\n";
616         print "device \'$config->{devpath}\' expecting node \'$config->{exp_name}\'\n";
617
618         udev("add", $config->{subsys}, $config->{devpath}, \$config->{conf});
619         if (-e "$PWD/$udev_root$config->{exp_name}") {
620
621                 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
622                     $atime, $mtime, $ctime, $blksize, $blocks) = stat("$PWD/$udev_root$config->{exp_name}");
623
624                 if (defined($config->{exp_perms})) {
625                         my $wrong = 0;
626                         $config->{exp_perms} =~ m/^(.*):(.*):(.*)$/;
627                         if ($1 ne "") {
628                                 if ($uid != $1) { $wrong = 1; };
629                         }
630                         if ($2 ne "") {
631                                 if ($gid != $2) { $wrong = 1; };
632                         }
633                         if ($3 ne "") {
634                                 if (($mode & 07777) != oct($3)) { $wrong = 1; };
635                         }
636                         if ($wrong == 0) {
637                                 print "permissions: ok    ";
638                         } else {
639                                 printf "expected permissions are: %i:%i:%#o\n", $1, $2, oct($3);
640                                 printf "created permissions are : %i:%i:%#o\n", $uid, $gid, $mode & 07777;
641                                 $error++;
642                         }
643                 }
644
645                 if (defined($config->{exp_majorminor})) {
646                         my $major = ($rdev >> 8) & 0xfff;
647                         my $minor = ($rdev & 0xff) | (($rdev >> 12) & 0xfff00);
648
649                         my $wrong = 0;
650                         $config->{exp_majorminor} =~ m/^(.*):(.*)$/;
651                         if ($1 ne "") {
652                                 if ($major != $1) { $wrong = 1; };
653                         }
654                         if ($2 ne "") {
655                                 if ($minor != $2) { $wrong = 1; };
656                         }
657                         if ($wrong == 0) {
658                                 print "major:minor: ok    ";
659                         } else {
660                                 printf "expected major:minor is: %i:%i\n", $1, $2;
661                                 printf "created major:minor is : %i:%i\n", $major, $minor;
662                                 $error++;
663                         }
664                 }
665
666                 print "add: ok    ";
667         } else {
668                 print "add: error\n";
669                 system("tree $udev_root");
670                 print "\n";
671                 $error++;
672         }
673
674         udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
675         if ((-e "$PWD/$udev_root$config->{exp_name}") ||
676             (-l "$PWD/$udev_root$config->{exp_name}")) {
677                 print "remove: error\n\n";
678                 system("tree $udev_root");
679                 $error++;
680         } else {
681                 print "remove: ok\n\n";
682         }
683 }
684
685 # prepare
686 system("rm -rf $udev_root");
687 mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
688
689 # create initial config file
690 open CONF, ">$main_conf" || die "unable to create config file: $main_conf";
691 print CONF "udev_root=\"$udev_root\"\n";
692 print CONF "udev_db=\"$udev_db\"\n";
693 print CONF "udev_rules=\"$conf_tmp\"\n";
694 print CONF "udev_permissions=\"$perm\"\n";
695 close CONF;
696
697 my $test_num = 1;
698
699 if ($ARGV[0]) {
700         # only run one test
701         $test_num = $ARGV[0];
702         print "udev-test will run test number $test_num only\n";
703
704         run_test($tests[$test_num-1], $test_num);
705 } else {
706         # test all
707         print "\nudev-test will run ".($#tests + 1)." tests:\n\n";
708
709         foreach my $config (@tests) {
710                 run_test($config, $test_num);
711                 $test_num++;
712
713         }
714 }
715
716 print "$error errors occured\n\n";
717
718 # cleanup
719 unlink($udev_db);
720 system("rm -rf $udev_root");
721 unlink($conf_tmp);
722 unlink($main_conf);
723