kerneltest/f32test/server/b_open.cpp
changeset 0 a41df078684a
child 175 5af6c74cd793
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 // f32test\server\b_open.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #define __E32TEST_EXTENSION__
       
    19 
       
    20 #include <f32file.h>
       
    21 #include <e32test.h>
       
    22 #include <e32hal.h>
       
    23 #include <f32dbg.h>
       
    24 #include "t_server.h"
       
    25 #include "t_chlffs.h"
       
    26 
       
    27 #ifdef __WINS__
       
    28 #define WIN32_LEAN_AND_MEAN
       
    29 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
       
    30 #include <windows.h>
       
    31 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
       
    32 #endif
       
    33 
       
    34 GLDEF_D RTest test(_L("B_OPEN"));
       
    35 
       
    36 LOCAL_D const TInt KMaxFiles=200;
       
    37 LOCAL_D TBuf<32> nameBuf[KMaxFiles];
       
    38 LOCAL_D TBuf<0x100> nameBuf1;
       
    39 LOCAL_D RFile chan[KMaxFiles];
       
    40 LOCAL_D RFile chan1;
       
    41 LOCAL_D TFileName fBuf;
       
    42 LOCAL_D TVolumeInfo vInfo;
       
    43 LOCAL_D TInt LeaveMemFree;
       
    44 
       
    45 LOCAL_C void testOpenFiles()
       
    46 //
       
    47 // Open files till memory is full
       
    48 // Write, seek, read, seteof, close, check and delete all files.
       
    49 //
       
    50     {
       
    51 	
       
    52 	TChar currentDrive=gSessionPath[0];
       
    53 	TInt driveNum;
       
    54 	TInt r=TheFs.CharToDrive(currentDrive,driveNum);
       
    55 	test_KErrNone(r);
       
    56 
       
    57     TInt i=0;
       
    58 	TInt totalRam;
       
    59     FOREVER
       
    60         {
       
    61 
       
    62 #if defined(__WINS__)
       
    63 		DWORD sectorsPerCluster;
       
    64 		DWORD bytesPerSector;
       
    65 		DWORD freeClusters;
       
    66 		DWORD sizeClusters;
       
    67 		BOOL b=GetDiskFreeSpaceA("C:\\",&sectorsPerCluster,&bytesPerSector,&freeClusters,&sizeClusters);
       
    68 		test(b==TRUE);
       
    69 		totalRam=sizeClusters*sectorsPerCluster*bytesPerSector;
       
    70 #else
       
    71 		TMemoryInfoV1Buf memInfoBuf;
       
    72 		UserHal::MemoryInfo(memInfoBuf);
       
    73 		totalRam=memInfoBuf().iTotalRamInBytes;
       
    74 #endif
       
    75         test.Printf(_L("Open %u\n"),i);
       
    76         nameBuf[i].Format(_L("B_OPEN test file %d"),i);
       
    77 		r=chan[i].Replace(TheFs,nameBuf[i],EFileStream|EFileWrite);
       
    78 		if (r==KErrNone)
       
    79 			{
       
    80 			r=chan[i].Write(_L8("SomeText"));
       
    81 			if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
       
    82 				r = chan[i].Flush();
       
    83 			if (r==KErrDiskFull)
       
    84 				{
       
    85 				chan[i].Close();
       
    86 				test(TheFs.Delete(nameBuf[i])==KErrNone);
       
    87 				}
       
    88 			}
       
    89 		if (r==KErrDiskFull)
       
    90 			break;
       
    91 		if (r==KErrNoMemory)
       
    92 			break;
       
    93 		if (r!=KErrNone)
       
    94 			{
       
    95 			test.Printf(_L("ERROR: RFile::Replace returned %d\n"),r);
       
    96 			test(EFalse);
       
    97 			}
       
    98 
       
    99         r=TheFs.Volume(vInfo);
       
   100 		test_KErrNone(r);
       
   101 		if (driveNum==EDriveC)
       
   102 	        test.Printf(_L("VInfo size=0x%x free=0x%x TotalRam = 0x%x\n"),vInfo.iSize,vInfo.iFree,totalRam);
       
   103         test(vInfo.iFree<=vInfo.iSize);
       
   104 
       
   105 //      r=TheFs.Volume(vInfo,(driveNum==EDriveC) ? EDriveD : EDriveC);
       
   106 //		if (r==KErrNone);
       
   107 //		test(vInfo.iFree<=vInfo.iSize);
       
   108 	    i++;
       
   109         if (i==KMaxFiles)
       
   110             {
       
   111             if (IsTestingLFFS())
       
   112                 break;
       
   113             else
       
   114                 test.Panic(_L("Too many files opened"));
       
   115             }
       
   116         }
       
   117 
       
   118 	test.Printf(_L("Created %d extra files\n"),i);
       
   119 	const TInt n_files=i;
       
   120 	r=TheFs.Volume(vInfo);
       
   121 	test_KErrNone(r);
       
   122 	if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
       
   123 		{
       
   124 		test(vInfo.iSize<=totalRam);
       
   125 		test(vInfo.iFree<=totalRam);
       
   126 		}
       
   127     test(vInfo.iFree<=vInfo.iSize);
       
   128 
       
   129 	test.Next(_L("SetSize to each file"));
       
   130     for (i=0;i<n_files;i++)
       
   131         {
       
   132 		r=chan[i].SetSize(0);
       
   133 		test_KErrNone(r);
       
   134         }
       
   135 
       
   136 	test.Next(_L("Write to each file"));
       
   137     TBuf8<3> numBuf;
       
   138     for (i=0;i<n_files;i++)
       
   139         {
       
   140         test.Printf(_L("Write %u\n"),i);
       
   141 		numBuf.Format(_L8("%d"),i);
       
   142 		r=chan[i].Write(0,numBuf);
       
   143 		test_KErrNone(r);
       
   144         }
       
   145 
       
   146 	test.Next(_L("Seek on each file"));
       
   147     for (i=0;i<n_files;i++)
       
   148         {
       
   149         test.Printf(_L("Seeking %u\n"),i);
       
   150         TInt pos=0;
       
   151         r=chan[i].Seek(ESeekStart,pos);
       
   152 		test_KErrNone(r);
       
   153         }
       
   154 
       
   155 	test.Next(_L("Read from each file"));
       
   156     TBuf8<3> checkBuf;
       
   157 	for (i=0;i<n_files;i++)
       
   158         {
       
   159         test.Printf(_L("Read %u\n"),i);
       
   160         r=chan[i].Read(checkBuf,3);
       
   161 		test_KErrNone(r);
       
   162 		numBuf.Format(_L8("%d"),i);
       
   163 		test(numBuf==checkBuf);
       
   164         }
       
   165 
       
   166 	test.Next(_L("Set size of each file"));
       
   167     for (i=0;i<n_files;i++)
       
   168         {
       
   169         test.Printf(_L("Set size %u\n"),i);
       
   170         r=chan[i].SetSize(i);
       
   171 		test_KErrNone(r);
       
   172         }
       
   173 
       
   174     r=TheFs.Volume(vInfo);
       
   175 	test_KErrNone(r);
       
   176 	if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
       
   177 		{
       
   178 	    test(vInfo.iSize<=totalRam);
       
   179 		test(vInfo.iFree<=totalRam);
       
   180 		}
       
   181     test(vInfo.iFree<=vInfo.iSize);
       
   182 
       
   183 	test.Next(_L("Close each file"));
       
   184     for (i=0;i<n_files;i++)
       
   185         {
       
   186         test.Printf(_L("Close %u\n"),i);
       
   187         chan[i].Close();
       
   188         }
       
   189 
       
   190 	test.Next(_L("Open each file"));
       
   191 
       
   192 	TInt n_files_open;
       
   193     for (n_files_open=0; n_files_open < n_files; n_files_open++)
       
   194         {
       
   195         r = chan[n_files_open].Open(TheFs,nameBuf[n_files_open],EFileRead|EFileStream);
       
   196 		test.Printf(_L("Open(%d) ret %d\n"), n_files_open, r);
       
   197 		if (r != KErrNone)
       
   198 			{
       
   199 			if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
       
   200 				break;
       
   201 			else
       
   202 				test(0);
       
   203 			}
       
   204         }
       
   205 
       
   206 	test.Next(_L("Check size of each file"));
       
   207     for (i=0;i<n_files_open;i++)
       
   208         {
       
   209         test.Printf(_L("Check %u\n"),i);
       
   210         TInt size;
       
   211 		TInt r=chan[i].Size(size);
       
   212 		test.Printf(_L("size of file %u is %d ret %d\n"),i,size, r);
       
   213 		test_KErrNone(r);
       
   214 		test_Equal(i,size);
       
   215         }
       
   216 
       
   217 	test.Next(_L("Close each file"));
       
   218     for (i=0;i<n_files_open;i++)
       
   219         {
       
   220         test.Printf(_L("Close %u\n"),i);
       
   221         chan[i].Close();
       
   222         }
       
   223 	
       
   224 	test.Next(_L("Delete files"));
       
   225     for (i=0;i<n_files;i++)
       
   226         {
       
   227         test.Printf(_L("Delete %u\n"),i);
       
   228         r=TheFs.Delete(nameBuf[i]);
       
   229 		test_KErrNone(r);
       
   230         }
       
   231     }
       
   232 
       
   233 
       
   234 
       
   235 LOCAL_C void InitTest()
       
   236 //
       
   237 // Create a large file
       
   238 //
       
   239 	{
       
   240 	TInt fileNumber = 0;
       
   241 	TBool fileSizeTruncated;
       
   242 
       
   243 	do
       
   244 		{
       
   245 		fileSizeTruncated = EFalse;
       
   246 		nameBuf1.Format(_L("\\Hooge Test File for B_OPEN %d"), fileNumber++);
       
   247 
       
   248 		TInt r=chan1.Replace(TheFs,nameBuf1,EFileStream|EFileWrite);
       
   249 		test_KErrNone(r);
       
   250 		r=TheFs.Volume(vInfo);
       
   251 		test_KErrNone(r);
       
   252 		TInt64 size;
       
   253 		LeaveMemFree = 0x400; // ???
       
   254 		if (vInfo.iFree>LeaveMemFree)
       
   255 			size=vInfo.iFree-LeaveMemFree;
       
   256 		else
       
   257 			size=0;
       
   258 
       
   259 		const TInt KMaxFileSize = 0x40000000;
       
   260 
       
   261 		// test as 64 bit numbers in case size is very large (eg. enough that TInt is
       
   262 		// not large enough to hold it)
       
   263 		TInt64 KMaxFileSize64 = MAKE_TINT64(0, KMaxFileSize);
       
   264 
       
   265 		test.Printf(_L("Free space available = %08x:%08x\n"), I64HIGH(size), I64LOW(size));
       
   266 		if (size > KMaxFileSize64)
       
   267 			{
       
   268 			size = KMaxFileSize;
       
   269 			test.Printf(_L("Truncated to %d to avoid current FAT FSY file size limit !!!\n"), size);
       
   270 			fileSizeTruncated = ETrue;
       
   271 			}
       
   272 
       
   273 		TFileName sessionPath;
       
   274 		TheFs.SessionPath(sessionPath);
       
   275 		TBuf<32> message=_L("?: has %ld bytes free\n");
       
   276 		message[0]=sessionPath[0];
       
   277 		test.Printf(message,vInfo.iFree);
       
   278 		if (((vInfo.iDrive.iMediaAtt)&KMediaAttVariableSize)==0)
       
   279 			{
       
   280 			// Not a variable sized drive, so should be safe to to just create big file
       
   281 			test.Printf(_L("Creating %S, 0x%08lx\n"),&nameBuf1,size);
       
   282 			r=chan1.SetSize((TUint)size);
       
   283 			}
       
   284 		else
       
   285 			{
       
   286 			// Variable sized drive (proabably RAM drive) needs a bit of special treatment
       
   287 			// Use a binary search to allocate largest sized file possible...
       
   288 			test.Printf(_L("Creating %S, 0x%08x\n"),&nameBuf1,size);
       
   289 			TInt lo = 0;
       
   290 			TInt hi = (TInt)size;
       
   291 			const TInt KSizeGranularity = 0x200; // must be power-of-2
       
   292 			while(hi-lo>KSizeGranularity && r==KErrNone)
       
   293 				{
       
   294 				TInt trySize = (lo+hi)/2;
       
   295 				trySize &= ~(KSizeGranularity-1);
       
   296 				r = chan1.SetSize((TUint)trySize);
       
   297 				if(r==KErrNone)
       
   298 					{
       
   299 					size = trySize;
       
   300 					lo = trySize;
       
   301 					}
       
   302 				else if(r==KErrDiskFull)
       
   303 					{
       
   304 					hi = trySize;
       
   305 					r = KErrNone;
       
   306 					}
       
   307 				}
       
   308 			if(r==KErrNone)
       
   309 				{
       
   310 				// reduce size to leave some free for tests...
       
   311 				LeaveMemFree = 4096*4; // best leave several RAM pages worth of space so rest of test has some memory
       
   312 				size -= LeaveMemFree;
       
   313 				r = chan1.SetSize((TUint)size);
       
   314 				}
       
   315 			}
       
   316 
       
   317 		if (r!=KErrNone)
       
   318 			{
       
   319 			test.Printf(_L("ERROR: Creating large file failed %d\n"),r);
       
   320 			test(EFalse);
       
   321 			}
       
   322 		test.Printf(_L("Created %S, 0x%08x\n"),&nameBuf1,size);
       
   323 		chan1.Close();
       
   324 		}
       
   325 	while (fileSizeTruncated);
       
   326 
       
   327 	}
       
   328 
       
   329 LOCAL_C void Cleanup()
       
   330 //
       
   331 // Cleanup test files
       
   332 //
       
   333 	{
       
   334 
       
   335 	TInt r=TheFs.Delete(nameBuf1);
       
   336 	test_KErrNone(r);
       
   337 	r=TheFs.RmDir(gSessionPath);
       
   338 	test_KErrNone(r);
       
   339 	}
       
   340 
       
   341 
       
   342 GLDEF_C void CallTestsL()
       
   343 //
       
   344 // Call tests that may leave
       
   345 //
       
   346 	{
       
   347 
       
   348 #ifdef __WINS__
       
   349 // These tests try to create a huge file to fill up the drive.
       
   350 // This fails on WINS with drives with > 1/2G free because
       
   351 // RFile::SetSize() (among other things) only takes a TInt.
       
   352 //
       
   353 	if (gSessionPath.Left(1).CompareF(_L("C")) == 0)
       
   354 		return;
       
   355 #endif
       
   356 	CreateTestDirectory(_L("\\B_OPEN\\"));
       
   357 	InitTest();
       
   358 	testOpenFiles();
       
   359 	Cleanup();
       
   360 	}
       
   361