kerneltest/e32test/pccd/t_idrv.cpp
changeset 0 a41df078684a
child 33 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\pccd\t_idrv.cpp
       
    15 // Overview:
       
    16 // Tests for the internal RAM drive
       
    17 // API Information:
       
    18 // TBusLocalDrive
       
    19 // Details:
       
    20 // - Load a Physical Device Driver for the RAM Media Driver.
       
    21 // - Find the internal drive: type == EMediaRam
       
    22 // - Display and adjust various drive capabilities, verify results
       
    23 // are as expected.
       
    24 // - Read and write the drive using various drive sizes, verify results
       
    25 // are as expected.
       
    26 // - Format the drive, verify results.
       
    27 // - Set original size and reformat. 
       
    28 // Platforms/Drives/Compatibility:
       
    29 // All.
       
    30 // Assumptions/Requirement/Pre-requisites:
       
    31 // Failures and causes:
       
    32 // Base Port information:
       
    33 // 
       
    34 //
       
    35 
       
    36 #include <e32test.h>
       
    37 #include <e32svr.h>
       
    38 #include <e32hal.h>
       
    39 #include <e32uid.h>
       
    40 #include "../mmu/mmudetect.h"
       
    41 #include <f32file.h>
       
    42 
       
    43 #define PDD_NAME _L("MEDINT")
       
    44 
       
    45 const TInt KTestDriveLen=0x00040000;	//256K
       
    46 const TInt KSmallDriveInc=0x00000400;	//1K
       
    47 const TInt KBigDriveLen=0x00100000;		//1M - WINS
       
    48 const TInt KTestBufLen=256;
       
    49 
       
    50 
       
    51 RTest test(_L("T_IDRV"));
       
    52 
       
    53 void Format(TInt aDrive, RFs& aFs)
       
    54 //
       
    55 // Format current drive
       
    56 //
       
    57 	{
       
    58 	test.Next(_L("Format"));
       
    59 	TBuf<4> driveBuf=_L("?:\\");
       
    60 	driveBuf[0]=(TText)(aDrive+'A');
       
    61 	RFormat format;
       
    62 	TInt count;
       
    63 	TInt r=format.Open(aFs,driveBuf,EHighDensity,count);
       
    64 	test(r==KErrNone);
       
    65 	while(count)
       
    66 		{
       
    67 		TInt r=format.Next(count);
       
    68 		test(r==KErrNone);
       
    69 		}
       
    70 	format.Close();
       
    71 	}
       
    72 
       
    73 GLDEF_C TInt E32Main()
       
    74     {
       
    75 
       
    76 	test.Title();
       
    77 	if (!HaveVirtMem())
       
    78 		{
       
    79 		test.Printf(_L("Needs MMU\n"));
       
    80 		return 0;
       
    81 		}
       
    82 #if defined(__EPOC32__) && defined(__CPU_X86)
       
    83 	test.Printf(_L("Doesn't run on X86\n"));
       
    84 #else
       
    85 
       
    86 	TBusLocalDrive theInternalDrive;
       
    87     TInt msgHandle = KLocalMessageHandle;
       
    88 	
       
    89 	UserSvr::UnlockRamDrive();
       
    90 	
       
    91 	test.Printf(_L("Warning - this will destroy internal drive.\r\n"));
       
    92 	TChar c= 'C';
       
    93 	c.UpperCase();
       
    94 	if (c!='C')
       
    95 		return(0);
       
    96 
       
    97 	test.Start(_L("Check loader running"));
       
    98 
       
    99 	test.Next(_L("Load Internal Ram Media Driver"));
       
   100 	TInt r=User::LoadPhysicalDevice(PDD_NAME);
       
   101 	test(r==KErrNone || r==KErrAlreadyExists);
       
   102 
       
   103 	test.Next(_L("Find internal drive"));
       
   104 	
       
   105 	TDriveInfoV1Buf driveInfoBuf;
       
   106 	UserHal::DriveInfo(driveInfoBuf);
       
   107 	TDriveInfoV1& driveInfo = driveInfoBuf();
       
   108 
       
   109 	TInt drive = 0;
       
   110 	for ( ; drive < driveInfo.iTotalSupportedDrives; ++drive)
       
   111 		{
       
   112 		TBool changedFlag;
       
   113 		theInternalDrive.Connect(drive, changedFlag);
       
   114 
       
   115 		TLocalDriveCapsV2 info;
       
   116 		TPckg<TLocalDriveCapsV2> infoPckg(info);
       
   117 		theInternalDrive.Caps(infoPckg);
       
   118 
       
   119 		if (info.iType == EMediaRam)
       
   120 			{
       
   121 			break;
       
   122 			}
       
   123 
       
   124 		theInternalDrive.Disconnect();
       
   125 		}
       
   126 	test(drive < driveInfo.iTotalSupportedDrives);
       
   127 
       
   128 	test.Next(_L("Capabilities"));
       
   129 	TLocalDriveCapsV2 info;
       
   130 	TPckg<TLocalDriveCapsV2> infoPckg(info);
       
   131 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   132 	TUint saveSize=I64LOW(info.iSize);
       
   133 	test(info.iType==EMediaRam);
       
   134 	test(info.iBattery==EBatNotSupported);
       
   135 	test(info.iDriveAtt==(KDriveAttLocal|KDriveAttInternal));
       
   136 	test(info.iMediaAtt==(KMediaAttVariableSize|KMediaAttFormattable));
       
   137 	test(info.iFileSystemId==KDriveFileSysFAT);
       
   138 
       
   139 	test.Printf(_L("Current drive size: %lx\n"),info.iSize);
       
   140 
       
   141 	test.Next(_L("Set size to zero"));
       
   142 	test(theInternalDrive.ReduceSize(0,saveSize)==KErrNone);
       
   143 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   144 	test(info.iSize==0);
       
   145 	test(theInternalDrive.ReduceSize(0,-1)==KErrArgument);
       
   146 	test(theInternalDrive.Enlarge(-1)==KErrArgument);
       
   147 
       
   148 	test.Next(_L("Increase to large size"));
       
   149 #if defined (__WINS__)
       
   150 	TUint cSize=KBigDriveLen;
       
   151 #else
       
   152 	TMemoryInfoV1Buf memBuf;
       
   153 	TMemoryInfoV1 &mi=memBuf();
       
   154 	UserHal::MemoryInfo(memBuf);
       
   155 //	TUint cSize=(mi.iTotalRamInBytes-KTestDriveLen); // Leave last 256K - used by Kernel etc.
       
   156 //	TUint cSize=mi.iTotalRamInBytes>>1; 			 // Half ram
       
   157 //	TUint cSize=mi.iTotalRamInBytes>>2; 			 // Quarter ram
       
   158 	TUint cSize=mi.iTotalRamInBytes>>3; 			 // Eighth ram
       
   159 #endif
       
   160 	test.Printf(_L("(Increasing to %dbytes)\r\n"),cSize);
       
   161 	test(theInternalDrive.Enlarge(cSize)==KErrNone);
       
   162 //	test(theInternalDrive.Enlarge(cSize-saveSize)==KErrNone); // ???
       
   163 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   164 	test(I64LOW(info.iSize)==cSize);
       
   165 
       
   166 	test.Next(_L("Increase by 1K"));
       
   167 	cSize+=KSmallDriveInc;
       
   168 	test(theInternalDrive.Enlarge(KSmallDriveInc)==KErrNone);
       
   169 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   170 	test(I64LOW(info.iSize)==cSize);
       
   171 
       
   172 	test.Next(_L("Reduce to 256K"));
       
   173 	test(theInternalDrive.ReduceSize(0,(cSize-KTestDriveLen))==KErrNone);
       
   174 	cSize=KTestDriveLen;
       
   175 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   176 	test(I64LOW(info.iSize)==(TUint)KTestDriveLen);
       
   177 
       
   178 	test.Next(_L("Write/Read"));
       
   179 	TBuf8<KTestBufLen> wrBuf(KTestBufLen),rdBuf;
       
   180 	TUint i,j,len;
       
   181 	for (i=0 ; i<(TUint)KTestBufLen ; i++)
       
   182 		wrBuf[i]=(TUint8)i;
       
   183 	for (i=0,j=0;i<(TUint)KTestDriveLen;i+=len,j++)
       
   184 		{
       
   185 		len=Min(KTestBufLen,(KTestDriveLen-i));
       
   186 		rdBuf.Fill(0,len);
       
   187 		wrBuf[0]=(TUint8)j;
       
   188 		test(theInternalDrive.Write(i,len,&wrBuf,msgHandle,0)==KErrNone);
       
   189  		test(theInternalDrive.Read(i,len,&rdBuf,msgHandle,0)==KErrNone);
       
   190 		wrBuf.SetLength(len);
       
   191   	    test(rdBuf.Compare(wrBuf)==0);
       
   192 		}
       
   193 
       
   194 	test.Next(_L("Reduce size - 256 bytes from start"));
       
   195  	test(theInternalDrive.ReduceSize(0,KTestBufLen)==KErrNone);
       
   196 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   197 	cSize-=KTestBufLen;
       
   198 	test(I64LOW(info.iSize)==(TUint)cSize);
       
   199 	for (i=0,j=1;i<cSize;i+=len,j++)
       
   200 		{
       
   201 		len=Min(KTestBufLen,(cSize-i));
       
   202 		rdBuf.Fill(0,len);
       
   203 		wrBuf[0]=(TUint8)j;
       
   204  		test(theInternalDrive.Read(i,len,&rdBuf,msgHandle,0)==KErrNone);
       
   205 		wrBuf.SetLength(len);
       
   206   	    test(rdBuf.Compare(wrBuf)==0);
       
   207 		}
       
   208 
       
   209 	test.Next(_L("Reduce size - (4K+127) bytes from middle"));
       
   210 	TInt reduction=((KTestBufLen<<4)+((KTestBufLen>>1)-1)); 
       
   211  	test(theInternalDrive.ReduceSize(KTestBufLen,reduction)==KErrNone); 
       
   212 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   213 	cSize-=reduction;
       
   214 	test(I64LOW(info.iSize)==(TUint)cSize);
       
   215 	TBuf8<KTestBufLen> odBuf(KTestBufLen); // To verify new pattern 
       
   216 	for (i=0 ; i<(TUint)KTestBufLen ; i++)
       
   217 		{
       
   218 		if (i<=(KTestBufLen>>1))
       
   219 			odBuf[i]=(TUint8)(i+((KTestBufLen>>1)-1));
       
   220 		else
       
   221 			odBuf[i]=(TUint8)(i-((KTestBufLen>>1)+1));
       
   222 		}
       
   223 	for (i=0,j=1;i<cSize;i+=len,j++)
       
   224 		{
       
   225 		len=Min(KTestBufLen,(cSize-i));
       
   226 		rdBuf.Fill(0,len);
       
   227  		test(theInternalDrive.Read(i,len,&rdBuf,msgHandle,0)==KErrNone);
       
   228 		if (j==2)
       
   229 			j+=17;
       
   230 		if (j==1)
       
   231 			{
       
   232 			wrBuf[0]=(TUint8)j;
       
   233 			wrBuf.SetLength(len);
       
   234   	    	test(rdBuf.Compare(wrBuf)==0);
       
   235 			}
       
   236 		else
       
   237 			{
       
   238 			odBuf.SetLength(KTestBufLen);
       
   239 			odBuf[((KTestBufLen>>1)+1)]=(TUint8)j;
       
   240 			odBuf.SetLength(len);
       
   241   	    	test(rdBuf.Compare(odBuf)==0);
       
   242 			}
       
   243 		}
       
   244 
       
   245 	test.Next(_L("Reduce size - (8K-1) bytes from end"));
       
   246 	reduction=((KTestBufLen<<5)-1); 
       
   247 	test(theInternalDrive.ReduceSize((cSize-reduction),reduction)==KErrNone);
       
   248 	test(theInternalDrive.Caps(infoPckg)==KErrNone);
       
   249 	cSize-=reduction;
       
   250 	test(info.iSize==cSize);
       
   251 	for (i=0,j=1;i<cSize;i+=len,j++)
       
   252 		{
       
   253 		len=Min(KTestBufLen,(cSize-i));
       
   254 		rdBuf.Fill(0,len);
       
   255  		test(theInternalDrive.Read(i,len,&rdBuf,msgHandle,0)==KErrNone);
       
   256 		if (j==2)
       
   257 			j+=17;
       
   258 		if (j==1)
       
   259 			{
       
   260 			wrBuf[0]=(TUint8)j;
       
   261 			wrBuf.SetLength(len);
       
   262   	    	test(rdBuf.Compare(wrBuf)==0);
       
   263 			}
       
   264 		else
       
   265 			{
       
   266 			odBuf.SetLength(KTestBufLen);
       
   267 			odBuf[((KTestBufLen>>1)+1)]=(TUint8)j;
       
   268 			odBuf.SetLength(len);
       
   269   	    	test(rdBuf.Compare(odBuf)==0);
       
   270 			}
       
   271 		}
       
   272 
       
   273 	test.Next(_L("Format"));
       
   274 	wrBuf.Fill(0,KTestBufLen);
       
   275 	TFormatInfo fi;
       
   276 	TInt ret;
       
   277 	while((ret=theInternalDrive.Format(fi))!=KErrEof)
       
   278 		test(ret==KErrNone);
       
   279 	for (i=0;i<cSize;i+=len)
       
   280 		{
       
   281 		len=Min(KTestBufLen,(cSize-i));
       
   282 		rdBuf.Fill(0xAA,len);
       
   283  		test(theInternalDrive.Read(i,len,&rdBuf,msgHandle,0)==KErrNone);
       
   284 		wrBuf.SetLength(len);
       
   285   	    test(rdBuf.Compare(wrBuf)==0);
       
   286 		}
       
   287 
       
   288 	test.Next(_L("Restore original size"));
       
   289 	TInt sizeDif=cSize-saveSize;
       
   290 	if (sizeDif>0)
       
   291 		test(theInternalDrive.ReduceSize(0,sizeDif)==KErrNone);
       
   292 	else
       
   293 		test(theInternalDrive.Enlarge(sizeDif*-1)==KErrNone);
       
   294 
       
   295 	test.Next(_L("Disconnect from internal drive"));
       
   296 	theInternalDrive.Disconnect();
       
   297 
       
   298 	RFs fs;
       
   299 	test(fs.Connect()==KErrNone);
       
   300 	for(drive=25 ; drive>=0; --drive)
       
   301 		{
       
   302 		TDriveInfo info;
       
   303 		if(fs.Drive(info,drive)==KErrNone)
       
   304 			if(info.iType==EMediaRam)
       
   305 				{
       
   306 				TBuf<256> text;
       
   307 				text.Append(_L("Formatting drive "));
       
   308 				text.Append(TText(drive+'A'));
       
   309 				text.Append(_L(": ..."));
       
   310 				test.Next(text);
       
   311 				Format(drive,fs);
       
   312 				break;
       
   313 				}
       
   314 		}
       
   315 
       
   316     test.End();
       
   317 
       
   318 #endif	// x86
       
   319 	return(0);
       
   320 	}
       
   321