11 my @unexpected_downloads = ();
14 my %_attr_data = ( # DEFAULT
16 _cwd => Cwd::getcwd(),
25 my ($self, $attr) = @_;
37 my ($caller, %args) = @_;
38 my $caller_is_obj = ref($caller);
39 my $class = $caller_is_obj || $caller;
40 #print STDERR "class = ", $class, "\n";
41 #print STDERR "_attr_data {cwd} = ", $Test::_attr_data{_cwd}, "\n";
42 my $self = bless {}, $class;
43 foreach my $attrname ($self->_standard_keys()) {
44 #print STDERR "attrname = ", $attrname, " value = ";
45 my ($argname) = ($attrname =~ /^_(.*)/);
46 if (exists $args{$argname}) {
47 #printf STDERR "Setting up $attrname\n";
48 $self->{$attrname} = $args{$argname};
49 } elsif ($caller_is_obj) {
50 #printf STDERR "Copying $attrname\n";
51 $self->{$attrname} = $caller->{$attrname};
53 #printf STDERR "Using default for $attrname\n";
54 $self->{$attrname} = $self->_default_for($attrname);
56 #print STDERR $attrname, '=', $self->{$attrname}, "\n";
58 #printf STDERR "_cwd default = ", $self->_default_for("_cwd");
65 my $result_message = "Test successful.\n";
67 printf "Running test $self->{_name}\n";
71 chdir ("$self->{_cwd}/$self->{_name}/input");
76 $self->_launch_server();
78 # print STDERR "Spawned server with pid: $pid\n";
81 chdir ("$self->{_cwd}/$self->{_name}/output");
82 # print "Calling $self->{_cmdline}\n";
83 my $errcode = system ("$self->{_cwd}/../src/$self->{_cmdline}");
87 # print "Killed server\n";
90 unless ($errcode == $self->{_errcode}) {
91 $result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
93 if (my $error_str = $self->_verify_download()) {
94 $result_message = $error_str;
100 print $result_message;
107 #print $self->{_name}, "\n";
108 chdir ($self->{_cwd});
110 # Create temporary directory
111 mkdir ($self->{_name});
112 chdir ($self->{_name});
117 $self->_setup_server();
119 chdir ($self->{_cwd});
126 chdir ($self->{_cwd});
127 File::Path::rmtree ($self->{_name});
131 sub _verify_download {
134 chdir ("$self->{_cwd}/$self->{_name}/output");
136 # use slurp mode to read file content
137 my $old_input_record_separator = $/;
140 while (my ($filename, $filedata) = each %{$self->{_output}}) {
141 open (FILE, $filename)
142 or return "Test failed: file $filename not downloaded\n";
144 my $content = <FILE>;
145 $content eq $filedata->{'content'}
146 or return "Test failed: wrong content for file $filename\n";
148 if (exists($filedata->{'timestamp'})) {
149 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
150 $atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
152 $mtime == $filedata->{'timestamp'}
153 or return "Test failed: wrong timestamp for file $filename\n";
159 $/ = $old_input_record_separator;
161 # make sure no unexpected files were downloaded
162 chdir ("$self->{_cwd}/$self->{_name}/output");
164 __dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[0]}) }, sub { shift; return @_ } );
165 if (@unexpected_downloads) {
166 return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
174 my ($top, $filefunc, $dirfunc) = @_;
180 unless (opendir $DIR, $top) {
181 warn "Couldn't open directory $DIR: $!; skipping.\n";
186 while ($file = readdir $DIR) {
187 next if $file eq '.' || $file eq '..';
188 my $nextdir = $top eq '.' ? $file : "$top/$file";
189 push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
192 return $dirfunc ? $dirfunc->($top, @results) : () ;
194 return $filefunc ? $filefunc->($top) : () ;