os/kernelhwsrv/kerneltest/f32test/smassstorage/scripts/usbinterop1.pl
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/smassstorage/scripts/usbinterop1.pl	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,286 @@
     1.4 +#!perl -w
     1.5 +# Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +# All rights reserved.
     1.7 +# This component and the accompanying materials are made available
     1.8 +# under the terms of the License "Eclipse Public License v1.0"
     1.9 +# which accompanies this distribution, and is available
    1.10 +# at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +#
    1.12 +# Initial Contributors:
    1.13 +# Nokia Corporation - initial contribution.
    1.14 +#
    1.15 +# Contributors:
    1.16 +#
    1.17 +# Description:
    1.18 +# USBinterop1
    1.19 +# 
    1.20 +#
    1.21 +
    1.22 +use strict;
    1.23 +use Digest::MD5;
    1.24 +use POSIX;
    1.25 +use File::Path;
    1.26 +use Getopt::Long;
    1.27 +use Pod::Usage;
    1.28 +
    1.29 +my $spread = 3;
    1.30 +my $depth = 1;
    1.31 +my $drive = "";
    1.32 +my $size = 1024;
    1.33 +my $pause = 1;
    1.34 +my $help = 0;
    1.35 +
    1.36 +my %opts = ( 'spread=i' => \$spread,
    1.37 +	     'depth=i' 	=> \$depth,
    1.38 +	     'drive=s' 	=> \$drive,
    1.39 +	     'size=i' 	=> \$size,
    1.40 +	     'pause!' 	=> \$pause,
    1.41 +	     'help!' 	=> \$help);
    1.42 +
    1.43 +GetOptions(%opts) || pod2usage(2);
    1.44 +pod2usage(-exitval => 1, -verbose => 2) if $help;
    1.45 +
    1.46 +$drive =~ s/\\/\//g;
    1.47 +$drive .= "/" unless ($drive =~ m/\/$/);
    1.48 +$spread = 1 if $spread < 1;
    1.49 +$depth = 1 if $depth < 1;
    1.50 +$size = 1 if $size < 1;
    1.51 +
    1.52 +# Check OS
    1.53 +# ME : "Windows" "4.90"
    1.54 +# 2k : "Windows NT" "5.0"
    1.55 +# XP : "Windows NT" "5.1"
    1.56 +# Mac: "Darwin" "7.4.1"
    1.57 +print((uname)[0] . " v" . (uname)[2] . ":" . (uname)[3] . "\n");
    1.58 +
    1.59 +my @folders = createfolders($drive, $spread, $depth);
    1.60 +mkpath(\@folders);
    1.61 +my %digests = createfiles(\@folders, $size, $spread);
    1.62 +
    1.63 +if ($pause)
    1.64 +{
    1.65 +	print "Unplug and replug the USB cable, then press enter...";
    1.66 +	$pause = <STDIN>;
    1.67 +}
    1.68 +
    1.69 +checkfiles(\%digests);
    1.70 +removefiles(\%digests);
    1.71 +removefolders(\@folders);
    1.72 +
    1.73 +
    1.74 +
    1.75 +sub createfolder
    1.76 +{
    1.77 +	my $dirlist = shift;
    1.78 +	my $fbase = shift;
    1.79 +	my $spread = shift;
    1.80 +	my $depth = shift;
    1.81 +
    1.82 +	return unless $depth > 0;
    1.83 +
    1.84 +	for (my $i = 0; $i < $spread; $i++)
    1.85 +	{	
    1.86 +		my $dir = sprintf("%sdir%05d/", $fbase, $i + 1);
    1.87 +		push @$dirlist, $dir;
    1.88 +		createfolder($dirlist, $dir, $spread, $depth - 1);
    1.89 +	}
    1.90 +}
    1.91 +
    1.92 +
    1.93 +sub createfolders
    1.94 +{
    1.95 +	my $drive = shift;
    1.96 +	$drive = "" unless defined $drive;
    1.97 +	my $spread = shift;
    1.98 +	$spread = 1 unless defined $spread;
    1.99 +	my $depth = shift;
   1.100 +	$depth = 1 unless defined $depth;
   1.101 +
   1.102 +	my @dirlist = ();
   1.103 +	createfolder(\@dirlist, $drive, $spread, $depth);
   1.104 +	return @dirlist;
   1.105 +}
   1.106 +
   1.107 +sub makeblock
   1.108 +{
   1.109 +	my $length = shift;
   1.110 +	my @list = ();
   1.111 +	for (my $i = 0; $i < $length; $i++)
   1.112 +	{
   1.113 +		push @list, int((91 - 65) * rand()) + 65;
   1.114 +	}
   1.115 +	return pack "C$length", @list;
   1.116 +}
   1.117 +
   1.118 +
   1.119 +sub writefile
   1.120 +{
   1.121 +	my $file = shift;
   1.122 +	my $length = shift;
   1.123 +	my $block = 1024;
   1.124 +	open(FILE, ">$file") or warn ("Unable to open $file for writing: $!\n");
   1.125 +	my $md5 = Digest::MD5->new();
   1.126 +	while ($length > 0)
   1.127 +	{	
   1.128 +		my $data = makeblock(($length > $block) ? $block : $length);
   1.129 +		$md5->add($data);
   1.130 +		print(FILE $data);
   1.131 +		$length -= $block;
   1.132 +	}
   1.133 +	close(FILE);
   1.134 +	return $md5->hexdigest();
   1.135 +}
   1.136 +
   1.137 +
   1.138 +sub readfile 
   1.139 +{
   1.140 +	my $file = shift;
   1.141 +	open(FILE, $file) or warn ("Unable to open $file for reading: $!\n");
   1.142 +	my $md5 = Digest::MD5->new();
   1.143 +	$md5->addfile(*FILE);
   1.144 +	close(FILE);
   1.145 +	return $md5->hexdigest();
   1.146 +}
   1.147 +
   1.148 +
   1.149 +sub createfiles
   1.150 +{
   1.151 +	my $dirlist = shift;
   1.152 +	my $size = shift;
   1.153 +	$size = 1024 unless defined $size;
   1.154 +	my $nfiles = shift;
   1.155 +	$nfiles = 1 unless defined $nfiles;
   1.156 +
   1.157 +	my %digest;
   1.158 +
   1.159 +	foreach (@$dirlist)
   1.160 +	{
   1.161 +		for (my $i = 0; $i < $nfiles; $i++)
   1.162 +		{	
   1.163 +			my $file = sprintf("${_}file%04d.txt", $i + 1);
   1.164 +			$digest{$file} = writefile($file, $size);
   1.165 +		}
   1.166 +	}
   1.167 +	return %digest;
   1.168 +}
   1.169 +
   1.170 +sub checkfiles
   1.171 +{
   1.172 +	my $digests = shift;
   1.173 +	
   1.174 +	foreach (sort keys %$digests)
   1.175 +	{
   1.176 +		my $readDigest = readfile($_);
   1.177 +		print "$_\t$digests->{$_}\t$readDigest\t" . ($digests->{$_} eq $readDigest ? "ok" : "ERROR") . "\n";
   1.178 +	}
   1.179 +}
   1.180 +
   1.181 +
   1.182 +sub removefiles
   1.183 +{
   1.184 +	my $digests = shift;
   1.185 +	my @cant = grep {not unlink} (keys %$digests);
   1.186 +	warn "Unable to remove @cant\n" if @cant;
   1.187 +}
   1.188 +	
   1.189 +
   1.190 +sub removefolders
   1.191 +{
   1.192 +	my $folders = shift;
   1.193 +	foreach (@$folders)
   1.194 +	{
   1.195 +		if (-e)
   1.196 +		{
   1.197 +			rmtree($_) or warn "Unable to remove $_: $!\n";
   1.198 +		}
   1.199 +	}
   1.200 +}
   1.201 +
   1.202 +
   1.203 +######################################################################
   1.204 +
   1.205 +__END__
   1.206 +
   1.207 +=head1 NAME
   1.208 +
   1.209 +usbinterop1.pl - Create directories and files, read back and compare
   1.210 +
   1.211 +=head1 SYNOPSIS
   1.212 +
   1.213 +usage:   usbinterop1.pl [options]
   1.214 +
   1.215 +=head1 OPTIONS
   1.216 +
   1.217 +=over 4
   1.218 +
   1.219 +=item --spread=<number of directories and files>
   1.220 +
   1.221 +Spread is the number of directories and files that are created at each
   1.222 +level of the created tree.  For example, a spread of three would
   1.223 +create three directories and three files in each of the directories.
   1.224 +Spread is a measure of the "bushiness" of the directory tree.
   1.225 +
   1.226 +Default value is "3".
   1.227 +
   1.228 +=item --depth=<directory nesting level>
   1.229 +
   1.230 +Each directory can have subdirectories up to the limit of the depth
   1.231 +parameter.  Depth is a measure of the "height" of the directory tree.
   1.232 +
   1.233 +Default value is "1".
   1.234 +
   1.235 +=item --size=<size of files to create>
   1.236 +
   1.237 +The size in bytes for each test file.  Be careful as the disk space
   1.238 +used is a function of this parameter as well as the depth and spread
   1.239 +parameters: 
   1.240 +
   1.241 +total size = size*(sp^(dep+1)+sp^(dep)+sp^(dep-1)+..+sp^2)
   1.242 +
   1.243 +Default value is "1024".
   1.244 +
   1.245 +=item --drive=<USB drive location>
   1.246 +
   1.247 +The path to the USB drive in which to write the files.
   1.248 +
   1.249 +Default value is ".", the current working directory.
   1.250 +
   1.251 +=item --help=<file>
   1.252 +
   1.253 +Displays this help.
   1.254 +
   1.255 +=back
   1.256 +
   1.257 +=head1 DESCRIPTION
   1.258 +
   1.259 +This is a simple utility to create folders and files with varying
   1.260 +levels of nesting and sizes on a USB drive, and read them back to
   1.261 +verify their contents.
   1.262 +
   1.263 +=head2 Test Case Specification
   1.264 +
   1.265 + TestCaseID: 	Interoperability_1
   1.266 + TestType: 	IT
   1.267 + TestCaseDesc:  Test Mass Storage functionality on different platforms
   1.268 +	        (Windows 2000/XP/ME, MacOS) (Manual test)
   1.269 + FssID:		Base/emstore/1.1.1
   1.270 + FssID:		Base/emstore/3.1.1
   1.271 +
   1.272 + TestActions: 
   1.273 +	Connect device to a host PC. Enable MS. Start perl script on
   1.274 + PC.  This script formats drive and creates several folders with
   1.275 + levels of nested folders and writes set of files to them.  File sizes
   1.276 + varied from several kilobytes to several megabytes.  The number of
   1.277 + folders, nest depth, number of files placed in each folder and there
   1.278 + sizes should be configurable.  Then script prompt ask to unplug/plug
   1.279 + USB cable (to flash OS read cache) and then read all files back and
   1.280 + compare.
   1.281 +
   1.282 + TestExpectedResults:  
   1.283 +	Read data from files should match with written.
   1.284 +
   1.285 +=head1 COPYRIGHT
   1.286 +
   1.287 +Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
   1.288 +
   1.289 +=cut