kerneltest/e32test/usb/t_usb_win/scripts/massstorage/createlargefile.pl
changeset 0 a41df078684a
child 4 56f325a607ea
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 #!perl -w
       
     2 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 # USBinterop2
       
    16 # 
       
    17 #
       
    18 
       
    19 use strict;
       
    20 use Digest::MD5;
       
    21 use POSIX;
       
    22 use Getopt::Long;
       
    23 use Pod::Usage;
       
    24 
       
    25 my $drive = ".";
       
    26 my $size = 1024;
       
    27 my $reading = 0;
       
    28 my $logfile = "";
       
    29 my $help = 0;
       
    30 my $filename = "file0000.txt";
       
    31 
       
    32 # Error codes
       
    33 my $KErrNone = 0;			# No error - Test passed
       
    34 my $KErrLogFile = -1;		# Can't open log file
       
    35 my $KErrWriteFile = -2;		# Can't open file for writing
       
    36 my $KErrReadFile = -3;		# Can't open file for reading
       
    37 my $KErrRmFile = -4;		# Can't remove file
       
    38 my $KErrRmDir = -5;			# Can't remove directory
       
    39 my $KErrCmp = -6;			# File comparison failed
       
    40 my $KErrOther = -7;			# Other
       
    41 
       
    42 my $returncode = $KErrNone;
       
    43 
       
    44 my %opts = ( 'drive=s' 	=> \$drive,
       
    45 	     'size=i' 	=> \$size,
       
    46 	     'read=i'	=> \$reading,
       
    47 	     'logfile=s'=> \$logfile,
       
    48 	     'help!' 	=> \$help);
       
    49 
       
    50 GetOptions(%opts) || pod2usage(2);
       
    51 pod2usage(-exitval => 1, -verbose => 2) if $help;
       
    52 
       
    53 $drive =~ s/\\/\//g;
       
    54 $drive .= "/" unless ($drive =~ m/\/$/);
       
    55 $size = 1 if $size < 1;
       
    56 
       
    57 # Check OS
       
    58 # ME : "Windows" "4.90"
       
    59 # 2k : "Windows NT" "5.0"
       
    60 # XP : "Windows NT" "5.1"
       
    61 # Mac: "Darwin" "7.4.1"
       
    62 LogPrint("START - usbinterop2.pl\n");
       
    63 LogPrint("drive=$drive size=$size read=$reading\n");
       
    64 LogPrint((uname)[0] . " v" . (uname)[2] . ":" . (uname)[3] . "\n");
       
    65 
       
    66 # Set the random number seed
       
    67 srand(1515 * $size);
       
    68 
       
    69 $filename = "$drive$filename";
       
    70 
       
    71 my $writeDigest = writefile($filename, $size);
       
    72 
       
    73 if ($reading == 1)
       
    74 	{
       
    75 	my $readDigest = readfile($filename);
       
    76 	print "$filename\t$writeDigest\t$readDigest\t" . ($writeDigest eq $readDigest ? "ok" : "ERROR") . "\n";
       
    77 	if ($writeDigest ne $readDigest)
       
    78 		{
       
    79 		$returncode = $KErrCmp;
       
    80 		}
       
    81 	if (not unlink($filename))
       
    82 		{
       
    83 		LogPrint("Can't remove $filename: $!\n");
       
    84 		Leave($KErrRmFile);
       
    85 		}
       
    86 	}
       
    87 
       
    88 Leave($returncode);
       
    89 exit $KErrOther;
       
    90 
       
    91 sub Leave
       
    92 	{
       
    93 	my ($errcode) = @_;
       
    94 	LogPrint("END - usbinterop2.pl - return code:$errcode\n");
       
    95 	exit $errcode
       
    96 	}
       
    97 
       
    98 sub LogPrint
       
    99 	{
       
   100 	if ($logfile ne "")
       
   101 		{
       
   102 		open LOGFILE, ">>$logfile" or exit $KErrLogFile;
       
   103 		print LOGFILE @_;
       
   104 		close LOGFILE;
       
   105 		}
       
   106 	print @_;
       
   107 	}
       
   108 
       
   109 
       
   110 sub makeblock
       
   111 {
       
   112 	my $length = shift;
       
   113 	my @list = ();
       
   114 	for (my $i = 0; $i < $length; $i++)
       
   115 	{
       
   116 		push @list, int((91 - 65) * rand()) + 65;
       
   117 	}
       
   118 	return pack "C$length", @list;
       
   119 }
       
   120 
       
   121 
       
   122 sub writefile
       
   123 {
       
   124 	my $file = shift;
       
   125 	my $length = shift;
       
   126 	my $block = 1024;
       
   127 	if ($reading == 0)
       
   128 		{
       
   129 		if (not open(FILE, ">$file"))
       
   130 			{
       
   131 			LogPrint("Unable to open $file for writing: $!\n");
       
   132 			Leave($KErrWriteFile);
       
   133 			}
       
   134 		}
       
   135 	my $md5 = Digest::MD5->new();
       
   136 	while ($length > 0)
       
   137 	{	
       
   138 		my $data = makeblock(($length > $block) ? $block : $length);
       
   139 		$md5->add($data);
       
   140 		if ($reading == 0)
       
   141 			{
       
   142 			print(FILE $data);
       
   143 			}
       
   144 		$length -= $block;
       
   145 	}
       
   146 	if ($reading == 0)
       
   147 		{
       
   148 		close(FILE);
       
   149 		}
       
   150 	return $md5->hexdigest();
       
   151 }
       
   152 
       
   153 
       
   154 sub readfile 
       
   155 {
       
   156 	my $file = shift;
       
   157     if (!sysopen(FILE, $file, O_RDONLY)) {
       
   158 		LogPrint("Unable to open $file for reading: $!\n");
       
   159 		Leave($KErrReadFile);
       
   160 		};
       
   161 	my $md5 = Digest::MD5->new();
       
   162 	$md5->addfile(*FILE);
       
   163 	close(FILE);
       
   164 	return $md5->hexdigest();
       
   165 }
       
   166 
       
   167 
       
   168 
       
   169 ######################################################################
       
   170 
       
   171 __END__
       
   172 
       
   173 =head1 NAME
       
   174 
       
   175 usbinterop2.pl - Create drive-filling file, read back and compare
       
   176 
       
   177 =head1 SYNOPSIS
       
   178 
       
   179 usage:   usbinterop2.pl [options]
       
   180 
       
   181 =head1 OPTIONS
       
   182 
       
   183 =over 4
       
   184 
       
   185 =item --size=<size of files to create>
       
   186 
       
   187 The size in bytes for the test file.
       
   188 
       
   189 Default value is "1024".
       
   190 
       
   191 =item --drive=<USB drive location>
       
   192 
       
   193 The path to the USB drive in which to write the files.
       
   194 
       
   195 Default value is ".", the current working directory.
       
   196 
       
   197 =item --read=<0/1>
       
   198 
       
   199 0 to write
       
   200 1 to read
       
   201 
       
   202 Default value if no option given is 0 (write).
       
   203 
       
   204 Ensure the card/device has been ejected and thus flushed before you do the
       
   205 reading test after the writing case, otherwise the content will be cached by
       
   206 the local OS and the time measured won't be realistic.
       
   207 
       
   208 'Write' will leave the transferred files on the remote disk, but 'Read' will
       
   209 delete them (moving them to the local PC). So a practical test order is:
       
   210 
       
   211 'Write' test, then disconnect/re-connect USB drive, then 'Read' test.
       
   212 
       
   213 This sequence will start and finish with an empty USB disk.
       
   214 
       
   215 =item --logfile=<file>
       
   216 
       
   217 Append test output to specified file.
       
   218 
       
   219 =item --help=<file>
       
   220 
       
   221 Displays this help.
       
   222 
       
   223 =back
       
   224 
       
   225 =head1 DESCRIPTION
       
   226 
       
   227 This is a simple utility to create a file that fills the USB drive as
       
   228 much as possible, and reads it back to verify its contents.
       
   229 
       
   230 =head2 Test Case Specification
       
   231 
       
   232  TestCaseID:	Interoperability_2
       
   233  TestType: 	IT
       
   234  TestCaseDesc:  Test Mass Storage functionality on different platforms
       
   235 	        (Windows 2000/XP/ME, MacOS) (Manual test)
       
   236  FssID: 	Base/emstore/1.1.1
       
   237  FssID: 	Base/emstore/3.1.1
       
   238 
       
   239  TestActions:
       
   240 	Connect device to a host PC. Enable MS. Start perl script on
       
   241  PC.  This script formats drive and queries size of it.  Than script
       
   242  create file with size close to drive size and check free space.  Then
       
   243  script prompt ask to unplug/plug USB cable (to flash OS read cache)
       
   244  and then read the file back and compare.
       
   245 
       
   246  TestExpectedResults:
       
   247 	File creation should succeed. Read data from file should match
       
   248  with written.  Sum of file size and free space should be close to
       
   249  drive size.
       
   250 
       
   251 =head1 COPYRIGHT
       
   252 
       
   253 Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
   254 
       
   255 =cut