5 # Copyright (C) Intel Corp, 2004
7 # Author: Yin Hu <hu.yin@intel.com>
9 # Provides automated testing of the udevd binary.This test script is self-contained.
10 # Before you run this script please modify $sysfs to locate your sysfs filesystem,
11 # modify $udevsend_bin to locate your udevsend binary,
12 # modify $udev_bin to locate dummy udev script,
13 # modify $udev_bin2 to locate another dummy udev script ( amplify the execution time for test),
14 # modify $log_file to locate where udev script have placed the log file,
15 # modify $time_out to decide the time out for events,
16 # modify $udev_exe_time to decide the execution time for dummy udev script.
18 # Detail information of each test case please refer to the header of corresponding
22 # This program is free software; you can redistribute it and/or modify it
23 # under the terms of the GNU General Public License as published by the
24 # Free Software Foundation version 2 of the License.
26 # This program is distributed in the hope that it will be useful, but
27 # WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 # General Public License for more details.
31 # You should have received a copy of the GNU General Public License along
32 # with this program; if not, write to the Free Software Foundation, Inc.,
33 # 675 Mass Ave, Cambridge, MA 02139, USA.
41 my $udevd_bin = "../../udevd";
42 my $udevsend_bin = "../../udevsend";
43 my $udev_bin = "$ENV{'PWD'}/udev-log-script.pl";
44 my $udev_bin2 = "$ENV{'PWD'}/udev-log-amplify.pl";
45 my $log_file = "/tmp/udev_log.txt";
47 my $udev_exe_time = 5;
55 my ($udev_binary) = @_;
57 system("killall udevd");
58 system("rm -f $log_file");
61 if (!defined($udev_binary)) {
62 $udev_binary = $udev_bin;
65 $ENV{'UDEV_BIN'} = $udev_binary;
66 system("/bin/sh -c $udevd_bin&");
71 # This function prepares corresponding environment variables
72 # and then call $udevsend_bin to send event.
74 my ($seqnum, $devpath, $action, $subsystem, $script) = @_;
77 $ENV{'DEVPATH'} = $devpath;
78 $ENV{'ACTION'} = $action;
79 $ENV{'SUBSYSTEM'} = $subsystem;
82 $ENV{SEQNUM} = $seqnum;
85 return system("$udevsend_bin $subsystem");
88 sub check_count_and_time {
97 ($event_recv_time) = @_;
99 print " event receiving time: $event_recv_time\n\n";
101 open(LOGF, $log_file) || die "Opening file $log_file: $!";
103 foreach $line ( @content ) {
104 @line_items = split(/,/,$line);
105 print " device: $line_items[0], action: $line_items[1] \n";
106 print " forking udev time: $line_items[-1]";
107 $diff = $line_items[-1] - $event_recv_time;
108 print " the delay time is: $diff s \n\n";
109 if ( $diff > $time_out+10 ) {
110 print " the delay time is: $diff \n";
111 print " udevd doesn't act properly. \n";
118 return $log_ln_count;
121 sub check_sysfs_device_exist {
122 # check if the designated devices exist
126 foreach $dev (@dev_list) {
128 print "the designated device $dev doesn't exist. please change a device!\n";
142 ($event_recv_time) = @_;
144 print " event receiving time: $event_recv_time\n\n";
146 open(LOGF, $log_file) || die "Opening file $log_file: $!";
148 foreach $line ( @content ) {
149 @line_items = split(/,/,$line);
150 print " device: $line_items[0], action: $line_items[1] \n";
151 print " forking udev time: $line_items[-1]";
152 $diff = $line_items[-1] - $event_recv_time;
153 print " the delay time is: $diff s \n\n";
158 sub show_result_tm_out {
166 ($event_recv_time) = @_;
168 print " event receiving time: $event_recv_time\n\n";
170 open(LOGF, $log_file) || die "Opening file $log_file: $!";
172 foreach $line ( @content ) {
173 @line_items = split(/,/,$line);
174 print " device: $line_items[0], action: $line_items[1] \n";
175 print " forking udev time: $line_items[-1]";
176 $diff = $line_items[-1] - $event_recv_time;
177 print " the delay time is: $diff s \n\n";
178 if ( $diff < $time_out ) {
179 print " the delay time is: $diff \n";
180 print " udevd doesn't act properly. \n";
187 sub show_result_immediate {
195 ($event_recv_time) = @_;
197 print " event receiving time: $event_recv_time\n\n";
199 open(LOGF, $log_file) || die "Opening file $log_file: $!";
201 foreach $line ( @content ) {
202 @line_items = split(/,/,$line);
203 print " device: $line_items[0], action: $line_items[1] \n";
204 print " forking udev time: $line_items[-1]";
205 $diff = $line_items[-1] - $event_recv_time;
206 print " the delay time is: $diff s \n\n";
207 if ( $diff > $time_out ) {
208 print " the delay time is: $diff \n";
209 print " udevd doesn't act properly. \n";
224 open(LOGF, $log_file) || die "Opening file $log_file: $!";
227 foreach $line ( @content ) {
228 @line_items = split(/,/,$line);
229 $exe_time[$i] = $line_items[-1];
232 $diff = $exe_time[1] - $exe_time[0];
233 if ( $diff < $udev_exe_time ) {
234 print " there are more than one udev instance for a single device at the same time. \n";
237 print " there is just one udev instance for a single device at the same time. \n";
241 # test case functions
242 sub run_no_seq_test {
243 print "Test case name: no sequence number test\n";
244 print "Test case purpose: check whether udevd forks udev immediately when environment variable SEQNUM is null.\n";
245 print "Test expected visible results: \n";
246 print " the delay time between event receiving and forking udev for udevd should be negligible, \n";
247 print " that is, udev should be forked at once. please notice the following time...\n\n";
252 # add devices event test
256 # check if devices /block/sda exist
257 check_sysfs_device_exist("$sysfs/block/sda");
259 # log current system date/time
262 udevsend(-1, "/block/sda", "add", "block");
264 # check if execution is successful in time
266 show_result_immediate($time);
267 print " fork udev (add device) at once successfully.\n\n";
270 # remove devices event test
272 system("rm -f $log_file");
274 # log current system date/time
277 udevsend(-1, "/block/sda", "remove", "block");
279 # check if execution is successful in time
281 show_result_immediate($time);
282 print " fork udev (remove device) at once successfully.\n\n";
283 print "this case is ok\n\n";
286 sub run_normal_seq_test {
287 print "Test case name: normal sequence number stress test\n";
288 print "Test case purpose: check whether udevd can fork massive udev instances for \n";
289 print " massive sequential events successfully. \n";
290 print "Test expected visible results: \n";
291 print " Populate all the devices in directory $sysfs/class/tty, fork udved to send add/remove \n";
292 print " event to udev for each device. \n";
293 print " We can see the delay time for each device should be negligible. \n\n";
302 @file_list = glob "$sysfs/class/tty/*";
304 # log current system date/time for device add events
308 # add devices event test
310 print "add device events test: \n";
311 foreach $file (@file_list) {
312 udevsend($seq, substr($file, length($sysfs), length($file)-length($sysfs)), "add", "tty");
313 # check if execution is successful
317 print "add event: error\n\n";
322 # we'd better wait the udev to create all the device for a few seconds
323 print " wait for udevd processing about $time_out s... \n\n";
326 $ret_seq = check_count_and_time($time);
327 if ( $ret_seq != $seq ) {
328 print " add event: failed. some device-adding events fail to execute.\n\n";
331 print " $seq pieces of device-adding events have executed successfully.\n\n";
334 # log current system date/time for device remove events
338 # remove devices event test
340 print "remove device events test: \n";
342 @file_list = glob "$sysfs/class/tty/*";
344 foreach $file (@file_list) {
345 udevsend($seq, substr($file, length($sysfs), length($file)-length($sysfs)), "remove", "tty");
346 # check if execution is successful
350 print "remove event: error\n\n";
355 # we'd better wait the udev to create all the device for a few seconds
356 print " waiting for udev removing devices (about $time_out s)...\n";
360 $ret_seq = check_count_and_time($time);
361 if ( $ret_seq != $seq ) {
362 print " remove event: failed. some device-removing events fail to execute.\n\n";
365 print " $seq pieces of device-removing events have executed successfully.\n\n";
366 print "this case is ok.\n\n";
370 sub run_random_seq_test {
371 print "Test case name: random sequence number test case,\n";
372 print "Test case purpose: check whether udevd can order the events with random sequence number \n";
373 print " and fork udev correctly. \n";
374 print "Test expected visible results: \n";
375 print " We have disordered the events sent to udevd, if udevd can order them correctly, the devices' \n";
376 print " add/remove sequence should be tty0, tty1, tty2. \n\n";
380 # check if devices /class/tty/tty0, tty1, tty2 exist
381 check_sysfs_device_exist("$sysfs/class/tty/tty0", "$sysfs/class/tty/tty1", "$sysfs/class/tty/tty2");
384 # add device events test
386 print "add device events test: \n";
389 # log current system date/time for device remove events
392 # parameters: 1 sequence number, 2 device, 3 action, 4 subsystem
393 udevsend(3, "/class/tty/tty2", "add", "tty");
394 udevsend(1, "/class/tty/tty0", "add", "tty");
395 udevsend(2, "/class/tty/tty1", "add", "tty");
396 print " wait for udevd processing about $time_out s... \n\n";
398 show_result_tm_out($time);
401 # remove device events test
403 print "\nremove device events test: \n";
406 # log current system date/time for device remove events
409 udevsend(3, "/class/tty/tty2", "remove", "tty");
410 udevsend(2, "/class/tty/tty1", "remove", "tty");
411 udevsend(1, "/class/tty/tty0", "remove", "tty");
414 print " wait for udevd processing about $time_out s... \n\n";
416 show_result_tm_out($time);
417 print "this case is ok.\n\n";
420 sub run_expected_seq_test {
421 print "Test case name: expected sequence number test \n";
422 print "Test case purpose: check whether udevd fork udev immediately when the incoming event\n";
423 print " is exactly the expected event sequence number.\n";
424 print "Test expected visible results:\n";
425 print " first, udevd disposes disorder events(sequence number is 3,1,2,5,4,6),\n";
426 print " thus after disposed the expected event number for udevd is 7, when incoming event is 7, udevd\n";
427 print " should fork udev immediately, the delay time should be negligible. \n";
428 print " where: event 7 is (add device /class/tty/tty2) \n\n";
432 # check if devices /class/tty0, tty1, tty2 exist
433 check_sysfs_device_exist("$sysfs/class/tty/tty0", "$sysfs/class/tty/tty1", "$sysfs/class/tty/tty2");
437 # parameters: 1 sequence number, 2 device, 3 action, 4 subsystem
438 udevsend(3, "/class/tty/tty2", "add", "tty");
439 udevsend(1, "/class/tty/tty0", "add", "tty");
440 udevsend(2, "/class/tty/tty1", "add", "tty");
441 udevsend(5, "/class/tty/tty1", "remove", "tty");
442 udevsend(4, "/class/tty/tty0", "remove", "tty");
443 udevsend(6, "/class/tty/tty2", "remove", "tty");
445 print " wait for udevd timing out for disorder events (about $time_out s) \n\n";
446 sleep $udev_exe_time + $time_out+1;
447 system("rm -f $log_file");
449 # log current system date/time for device remove events
453 udevsend(7, "/class/tty/tty2", "add", "tty");
455 print " event sequence number: 7 \n";
456 show_result_immediate($time);
458 print "this case is ok.\n\n";
461 sub run_single_instance_test {
462 print "Test case name: single instance running for a single device test \n";
463 print "Test case purpose: check whether udevd only fork one udev instance for a single\n";
464 print " device at the same time. For each event a udev instance is \n";
465 print " executed in the background. All further events for the same \n";
466 print " device are delayed until the execution is finished. This way \n";
467 print " there will never be more than one instance running for a single \n";
468 print " device at the same time.\n";
469 print "Test expected visible results:\n";
470 print " In this test we amplify the execution time of udev (about 5 seconds), first, \n";
471 print " we send a add event for device /block/sda, and then we send a remove event, so the \n";
472 print " execution of remove event should be delayed until add is finished. \n\n";
476 restart_daemon($udev_bin2);
478 # check if device exists
479 check_sysfs_device_exist("$sysfs/block/sda");
481 # log current system date/time
484 udevsend(-1, "/block/sda", "add", "block");
485 udevsend(-1, "/block/sda", "remove", "block");
488 print " wait for udevd processing about $udev_exe_time s... \n\n";
489 sleep $udev_exe_time+1;
490 show_result_immediate($time);
492 print "this case is ok\n\n";
495 sub run_same_events_test {
496 print "Test case name: event sequence number overlap test \n";
497 print "Test case purpose: check whether udevd doesn't fork udev untill time out\n";
498 print " when encountering a event with sequence number same as the pevious event. \n";
499 print "Test expected visible results:\n";
500 print " event ( remove device /block/sda ) should be no delay, \n";
501 print " event ( add device /class/tty/tty1 ) should be delayed for $time_out s than its previous \n";
502 print " event ( remove device /block/sda ) \n\n";
508 # check if device exist
509 check_sysfs_device_exist("$sysfs/block/sda", "$sysfs/class/tty/tty1");
511 udevsend(0, "/block/sda", "add", "block");
513 # log current system date/time
516 system("rm -f $log_file");
518 udevsend(1, "/block/sda", "remove", "block");
519 udevsend(1, "/class/tty/tty1", "add", "tty");
522 print " wait for udevd processing about $time_out s... \n\n";
525 print "this case is ok\n\n";
528 sub run_missing_seq_test {
529 print "Test case name: missing sequence number test \n";
530 print "Test case purpose: check whether udevd doesn't fork udev untill time out\n";
531 print " when certain event sequence number is missing.\n";
532 print "Test expected visible results:\n";
533 print " the delay time for event(add device /block/sda) should be about $time_out s.\n\n";
539 # check if device exist
540 check_sysfs_device_exist("$sysfs/block/sda", "$sysfs/class/tty/tty1");
542 udevsend(0, "/class/tty/tty1", "add", "tty");
543 udevsend(1, "/class/tty/tty1", "remove", "tty");
546 # log current system date/time
548 system("rm -f $log_file");
550 udevsend(3, "/block/sda", "add", "block");
553 print " wait for udevd processing about $time_out s... \n\n";
556 print "this case is ok\n\n";
559 sub run_all_cases_test {
561 run_normal_seq_test();
562 run_random_seq_test();
563 run_missing_seq_test();
564 run_expected_seq_test();
565 run_same_events_test();
566 run_single_instance_test();
571 $test_case = $ARGV[0];
573 if ($test_case == 1) {
575 } elsif ($test_case == 2) {
576 run_normal_seq_test();
577 } elsif ($test_case == 3) {
578 run_random_seq_test();
579 } elsif ($test_case == 4) {
580 run_missing_seq_test();
581 } elsif ($test_case == 5) {
582 run_expected_seq_test();
583 } elsif ($test_case == 6) {
584 run_single_instance_test();
585 } elsif ($test_case == 7) {
586 run_same_events_test();
588 run_all_cases_test();
592 print "command format: perl udevd-test.pl <case number>\n";
593 print " test case:\n";
594 print " 1: no event sequence number\n";
595 print " 2: sequential event sequence number\n";
596 print " 3: random event sequence number\n";
597 print " 4: missing event sequence number\n";
598 print " 5: the incoming event sequence number is right the expected sequence number\n";
599 print " 6: single udev instance on a single device at the same time\n";
600 print " 7: test event sequence number overlap\n";
601 print " 9: all the cases\n\n";