kerneltest/f32test/server/t_falsespace.cpp
changeset 43 96e5fb8b040d
child 44 36bfc973b146
equal deleted inserted replaced
-1:000000000000 43:96e5fb8b040d
       
     1 // Copyright (c) 1997-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 //
       
    15 
       
    16 #include <f32file.h>
       
    17 #include <e32test.h>
       
    18 #include <e32math.h>
       
    19 
       
    20 #include "fat_utils.h"
       
    21 #include "t_server.h"
       
    22 #include "t_chlffs.h"
       
    23 
       
    24 using namespace Fat_Test_Utils;
       
    25 
       
    26 RTest test(_L("t_falsespace"));
       
    27 
       
    28 const TInt KNumberThreads=2;
       
    29 const TInt KHeapSize=0x2000;
       
    30 
       
    31 static TInt RsrvSpaceThread(TAny* aArg);
       
    32 static TInt SessCloseThread(TAny* aArg);
       
    33 static void GetFreeDiskSpace(TInt64 &aFree);
       
    34 
       
    35 
       
    36 TInt gCount;		//count of files used to fill up the disk
       
    37 TInt gTestDrive;	//drive number of the drive currently being tested
       
    38 
       
    39 TChar gCh;
       
    40 
       
    41 _LIT(KBasePath,"\\F32-TST\\FILLDIR\\");
       
    42 _LIT(KBaseName,"\\F32-TST\\FILLDIR\\FILE");
       
    43 
       
    44 _LIT(KTestFile,"?:\\test.txt");
       
    45 _LIT8(KTestData, "12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678");
       
    46 _LIT(KDir, "?:\\adodgydir\\");
       
    47 _LIT(KDrv,"?:\\");	
       
    48 _LIT(KNewName,"?:\\newname.txt");
       
    49 
       
    50 
       
    51 void FormatDrive()
       
    52 {
       
    53     TInt nRes;
       
    54 
       
    55     
       
    56     #if 0
       
    57     //-- FAT32 SPC:1; for the FAT32 testing on the emulator 
       
    58     TFatFormatParam fp;
       
    59     fp.iFatType = EFat32;
       
    60     fp.iSecPerCluster = 1;
       
    61 
       
    62 	nRes = FormatFatDrive(TheFs, gTestDrive, ETrue, &fp);	
       
    63     #else
       
    64 
       
    65     nRes = FormatFatDrive(TheFs, gTestDrive, ETrue);	
       
    66 
       
    67     #endif
       
    68 
       
    69     test(nRes == KErrNone);
       
    70 }
       
    71 
       
    72 void SynchronousClose(RFs &aSession)
       
    73 	{
       
    74 	TRequestStatus s;
       
    75 	aSession.NotifyDestruction(s);
       
    76 	test(s.Int()==KRequestPending);
       
    77 	aSession.Close();
       
    78 	User::WaitForRequest(s);
       
    79 	}
       
    80 
       
    81 
       
    82 static TInt CreateFileX(const TDesC& aBaseName,TInt aX, TInt aFileSize)
       
    83 //
       
    84 // Create a large file. Return KErrEof or KErrNone
       
    85 //
       
    86 	{
       
    87 
       
    88 	TBuf<128> fileName=aBaseName;
       
    89 	fileName.AppendNum(aX);
       
    90 	RFile file;
       
    91 
       
    92 	TInt r=file.Replace(TheFs,fileName,EFileWrite);
       
    93 	if (r==KErrDiskFull)
       
    94 		return(r);
       
    95 	if (r!=KErrNone)
       
    96 		{
       
    97 		test.Printf(_L("ERROR:: Replace returned %d\n"),r);
       
    98 		test(0);
       
    99 		return(KErrDiskFull);
       
   100 		}
       
   101 
       
   102 	if (!IsTestingLFFS())
       
   103 		r=file.SetSize(aFileSize);
       
   104 	else
       
   105 		{
       
   106     	TBuf8<1024> testdata(1024);
       
   107     	TInt count=(aFileSize/testdata.Length());
       
   108     	r=KErrNone;
       
   109     	while (count-- && r==KErrNone) 
       
   110         	r=file.Write(testdata);
       
   111 		}
       
   112 	if (r==KErrDiskFull)
       
   113 		{
       
   114 		file.Close();
       
   115 		return(r);
       
   116 		}
       
   117 	if (r!=KErrNone)
       
   118 		{
       
   119 		test.Printf(_L("ERROR:: SetSize/Write returned %d\n"),r);
       
   120 		test(0);
       
   121 		//test.Getch();
       
   122 		file.Close();
       
   123 		return(KErrDiskFull);
       
   124 		}
       
   125 
       
   126 	file.Close();
       
   127 
       
   128 	test.Printf(_L("Created file %d size %d\n"),aX,aFileSize);
       
   129 	return(KErrNone);
       
   130 	}
       
   131 
       
   132 LOCAL_C TInt DeleteFileX(const TDesC& aBaseName,TInt aX)
       
   133 //
       
   134 // Delete a large file
       
   135 //
       
   136 	{
       
   137 	TBuf<128> fileName=aBaseName;
       
   138 	fileName.AppendNum(aX);
       
   139 	return TheFs.Delete(fileName);
       
   140 	}
       
   141 
       
   142 
       
   143 static void FillUpDisk()
       
   144 //
       
   145 // Test that a full disk is ok
       
   146 //
       
   147 	{
       
   148 
       
   149 	test.Start(_L("Fill disk to capacity"));
       
   150 	TInt r=TheFs.MkDirAll(KBasePath);
       
   151 	test(r==KErrNone || r==KErrAlreadyExists);
       
   152 	gCount=0;
       
   153 	TFileName sessionPath;
       
   154 	r=TheFs.SessionPath(sessionPath);
       
   155 	test(r==KErrNone);
       
   156 	TBuf<128> fileName=KBaseName();
       
   157 	
       
   158 	TInt64 freespace=0;
       
   159 	TInt64 freespaceBeforeScanDrive = 0;
       
   160 	TInt64 freespaceAfterScanDrive = 0;
       
   161 	
       
   162 	do
       
   163 		{
       
   164 		GetFreeDiskSpace(freespace);
       
   165 		TInt fillfilesize=0;
       
   166 		if (I64HIGH(freespace))
       
   167 			fillfilesize=KMaxTInt;
       
   168 		else
       
   169 			fillfilesize=I64LOW(freespace)* 7/8;
       
   170 
       
   171 		FOREVER
       
   172 			{
       
   173 			TInt r=CreateFileX(fileName,gCount,fillfilesize);
       
   174 			if (r==KErrDiskFull)
       
   175 				{
       
   176 				if(fillfilesize <= 2)
       
   177 					break;
       
   178 				else
       
   179 					fillfilesize=fillfilesize/2;
       
   180 				}
       
   181 			test(r==KErrNone || r==KErrDiskFull);
       
   182 			if(r==KErrNone)
       
   183 				gCount++;
       
   184 			}
       
   185 
       
   186 		r=TheFs.CheckDisk(fileName);
       
   187 		if (r!=KErrNone && r!=KErrNotSupported)
       
   188 			{
       
   189 			test.Printf(_L("ERROR:: CheckDisk returned %d\n"),r);
       
   190 			test(0);
       
   191 
       
   192 			}
       
   193 
       
   194 		// Test that scan drive passes on a full disk
       
   195 		// DEF071696 - KErrCorrupt on Scan Drive 
       
   196 		GetFreeDiskSpace(freespaceBeforeScanDrive);
       
   197 		test.Printf(_L("Before ScanDrive freeSpace = %08X:%08X\n"),
       
   198 			I64HIGH(freespaceBeforeScanDrive), I64LOW(freespaceBeforeScanDrive));
       
   199 		r = TheFs.ScanDrive(fileName);
       
   200 		if (r!=KErrNone && r!=KErrNotSupported)
       
   201 			{
       
   202 			test.Printf(_L("ScanDrive returned %d\n"), r);
       
   203 			test(0);
       
   204 			}
       
   205 		GetFreeDiskSpace(freespaceAfterScanDrive);
       
   206 		test.Printf(_L("After ScanDrive freeSpace = %08X:%08X\n"),
       
   207 			I64HIGH(freespaceAfterScanDrive), I64LOW(freespaceAfterScanDrive));
       
   208 		}
       
   209 		while (freespaceBeforeScanDrive != freespaceAfterScanDrive );
       
   210 
       
   211 	gCount--;
       
   212 
       
   213 	test.End();
       
   214 	}
       
   215 
       
   216 static void GetFreeDiskSpace(TInt64 &aFree)
       
   217 //
       
   218 //	Get free disk space
       
   219 //
       
   220 	{
       
   221 	TVolumeInfo v;
       
   222 
       
   223 	TInt r=TheFs.Volume(v,gTestDrive);
       
   224 	test(r==KErrNone);
       
   225 	aFree=v.iFree;
       
   226 	}
       
   227 
       
   228 
       
   229 static void Test1()
       
   230 //
       
   231 //	Test the API fundamentaly works for one session
       
   232 //
       
   233 	{
       
   234 	test.Next(_L("Test Disk Space reserve APIs"));
       
   235 	TInt r=0;
       
   236 	
       
   237     FormatDrive();
       
   238 	
       
   239 	TInt64 free2;
       
   240 	TInt64 free1; 
       
   241 	TInt64 diff;
       
   242 
       
   243 	r=TheFs.GetReserveAccess(gTestDrive);
       
   244 	test(r==KErrPermissionDenied);
       
   245 	
       
   246 	//make sure nothing odd happens if we didnt already have access
       
   247 	r=TheFs.ReleaseReserveAccess(gTestDrive);
       
   248 	test(r==KErrNone);
       
   249 
       
   250 	
       
   251 	GetFreeDiskSpace(free2);
       
   252 
       
   253 	r=TheFs.ReserveDriveSpace(gTestDrive,0x1000);
       
   254 	test(r==KErrNone);
       
   255 
       
   256 	GetFreeDiskSpace(free1);
       
   257 	diff = free2 - free1;
       
   258 	test(I64INT(diff) > 0xfe0 && I64INT(diff) < 0x1100); 
       
   259 	
       
   260 	r=TheFs.GetReserveAccess(gTestDrive);
       
   261 	test(r==KErrNone);
       
   262 
       
   263 	GetFreeDiskSpace(free1);
       
   264 	TInt64 temp = free2-free1;
       
   265 	test(I64INT(temp)>(-0x90) && I64INT(temp)<0x90);
       
   266 	
       
   267 	r=TheFs.ReleaseReserveAccess(gTestDrive);
       
   268 	test(r==KErrNone);
       
   269 	GetFreeDiskSpace(free1);
       
   270 
       
   271 	diff = free2 - free1;
       
   272 	test(I64INT(diff) > 0xfe0 && I64INT(diff) < 0x1100);
       
   273 	
       
   274 	
       
   275 	//test reallocation of reserved space is possible
       
   276 	r=TheFs.ReserveDriveSpace(gTestDrive,0x2000);
       
   277 	test(r==KErrNone);
       
   278 
       
   279 	//test upper limit of reserved space 
       
   280 	r=TheFs.ReserveDriveSpace(gTestDrive,0x2000000);
       
   281 	test(r==KErrArgument);
       
   282 
       
   283 	r=TheFs.ReserveDriveSpace(gTestDrive,0);
       
   284 	test(r==KErrNone);
       
   285 	
       
   286 	r=TheFs.GetReserveAccess(gTestDrive);
       
   287 	test(r==KErrPermissionDenied);
       
   288 
       
   289 	//make sure nothing odd happens if we didnt already have access
       
   290 	r=TheFs.ReleaseReserveAccess(gTestDrive);
       
   291 	test(r==KErrNone);
       
   292 	
       
   293 	r=TheFs.ReserveDriveSpace(gTestDrive,-45);
       
   294 	test(r==KErrArgument);
       
   295 	}
       
   296 
       
   297 
       
   298 static void Test2()
       
   299 //
       
   300 //	Test multiple sessions, ensure the drive limit is not exceeded
       
   301 //	
       
   302 	{
       
   303 
       
   304 	test.Next(_L("Test Session and total reserve limits"));
       
   305 	
       
   306     FormatDrive();
       
   307 	
       
   308 	TInt i=0;
       
   309 	TInt r=0;
       
   310 	RFs sessions[17];
       
   311 	TVolumeInfo v;
       
   312 
       
   313 	//Connect Sessions
       
   314 	for(i=0; i<17; i++)
       
   315 		{
       
   316 		r = sessions[i].Connect();
       
   317 		test(r==KErrNone);
       
   318 		}
       
   319 
       
   320 	test.Next(_L("Test breaching sesson reserve limit"));
       
   321 	r=sessions[0].ReserveDriveSpace(gTestDrive,0x10001);
       
   322 	test(r==KErrArgument);
       
   323 
       
   324 	//Get Volume Free Space
       
   325 	r = sessions[0].Volume(v, gTestDrive);
       
   326 
       
   327 	if(v.iFree > 0x100000)
       
   328 		{
       
   329 		test.Next(_L("Test breaching drive reserve limit"));
       
   330 
       
   331 		for (i=0; i<16; i++)
       
   332 			{
       
   333 			r=sessions[i].ReserveDriveSpace(gTestDrive,0x10000);
       
   334 			test(r==KErrNone);
       
   335 			}
       
   336 
       
   337 		//The straw
       
   338 		r=sessions[16].ReserveDriveSpace(gTestDrive,0x10);
       
   339 		test(r==KErrTooBig);
       
   340 		}
       
   341 	else
       
   342 		{
       
   343 		test.Printf(_L("Drive too small: breaching drive reserve limit test skipped\n"));
       
   344 		test.Next(_L("Testing exhausting available drive free space instead"));
       
   345 
       
   346 		for(i=0; (v.iFree -= 0x10000) >= 0; i++)
       
   347 			{
       
   348 			r=sessions[i].ReserveDriveSpace(gTestDrive,0x10000);
       
   349 			test(r==KErrNone);
       
   350 			}
       
   351 
       
   352 		//The straw
       
   353 		r=sessions[i].ReserveDriveSpace(gTestDrive,0x10000);
       
   354 		test(r==KErrDiskFull);
       
   355 		}
       
   356 
       
   357 	//Close Sessions
       
   358 	for(i=0; i<17; i++)
       
   359 		{
       
   360 		SynchronousClose(sessions[i]);
       
   361 		}
       
   362 	}
       
   363 
       
   364 static void Test3()
       
   365 //
       
   366 //	Test session cleanup
       
   367 //		
       
   368 	{
       
   369 	test.Next(_L("Test session close and clean up of resrved space"));
       
   370 
       
   371 	FormatDrive();
       
   372 	
       
   373 	RFs fs1;
       
   374 	RFs fs2;
       
   375 	TInt64 free2(0);
       
   376 	TInt64 free1(0); 
       
   377 	TInt64 diff(0);
       
   378 	
       
   379 	TInt r=0;
       
   380 	r = fs1.Connect();
       
   381 	test(r==KErrNone);
       
   382 	r = fs2.Connect();
       
   383 	test(r==KErrNone);
       
   384 
       
   385 	GetFreeDiskSpace(free1);
       
   386 
       
   387 	r=fs1.ReserveDriveSpace(gTestDrive,0x10000);
       
   388 	test(r==KErrNone);
       
   389 	r=fs2.ReserveDriveSpace(gTestDrive,0x10000);
       
   390 	test(r==KErrNone);
       
   391 
       
   392 	GetFreeDiskSpace(free2);
       
   393 	diff = free1 - free2;
       
   394 	test(I64INT(diff)>0x1FBD0 && I64INT(diff)<0x21000); 
       
   395 
       
   396 	SynchronousClose(fs1);
       
   397 
       
   398 	GetFreeDiskSpace(free2);
       
   399 	diff = free1-free2;
       
   400 	test(I64INT(diff)>0xFA00 && I64INT(diff)<0x103C4); 
       
   401 
       
   402 	r = fs1.Connect();
       
   403 	test(r==KErrNone);
       
   404 
       
   405 	GetFreeDiskSpace(free1);
       
   406 	diff= free1-free2;
       
   407 	test(I64INT(diff)== 0 || I64INT(diff)<0xFA0 ); 
       
   408 
       
   409 	r=fs1.ReserveDriveSpace(gTestDrive,0x10000);
       
   410 	test(r==KErrNone);
       
   411 
       
   412 	GetFreeDiskSpace(free2);
       
   413 	diff = free1 - free2;
       
   414 	test(I64INT(diff)>0xFA00 && I64INT(diff)<0x103C4); 
       
   415 
       
   416 	// Make sure no reserve space is allocated
       
   417 	r=fs1.ReserveDriveSpace(gTestDrive,0);
       
   418 	test(r==KErrNone);
       
   419 	r=fs2.ReserveDriveSpace(gTestDrive,0);
       
   420 	test(r==KErrNone);
       
   421 
       
   422 	// Now fill up the disk
       
   423 	FillUpDisk();
       
   424 	
       
   425 	// Should fail as there is no space
       
   426 	r=fs1.ReserveDriveSpace(gTestDrive,0x10000);
       
   427 	test(r==KErrDiskFull);
       
   428 
       
   429 	SynchronousClose(fs1);
       
   430 	SynchronousClose(fs2);
       
   431 	}
       
   432 
       
   433 
       
   434 static void Test4()
       
   435 //
       
   436 //	Test real out of disk space conditions i.e. properly run out of disk space and try to 
       
   437 //	reserve an area etc
       
   438 //	
       
   439 	{
       
   440 	test.Next(_L("Test Filling disk and using APIs"));
       
   441 
       
   442 	if(IsTestingLFFS())
       
   443 		{
       
   444 		//-- This test is not valid for LFFS, because free space on this FS can change itself because of some 
       
   445         //-- internal FS activities
       
   446 		test.Printf(_L("This test is inconsistent on LFFS\n"));
       
   447 		return;
       
   448 		}
       
   449 	
       
   450     FormatDrive();
       
   451 
       
   452 	RFs fs;
       
   453 	TInt r=fs.Connect();
       
   454 	test(r==KErrNone);
       
   455 	TInt64 freeA(0);
       
   456 	TInt64 freeB(0);
       
   457 	RFile file;
       
   458 
       
   459 									//start with known amount of space
       
   460 
       
   461 	//create a single file to use for futher tests
       
   462 	TBuf<20> buf;
       
   463 	buf=KTestFile;
       
   464 	buf[0]=(TUint16)gCh;
       
   465 
       
   466 	r=file.Replace(fs, buf, EFileWrite);
       
   467 	test(r==KErrNone);
       
   468 
       
   469 	r=file.Write(KTestData());
       
   470 	test(r==KErrNone);
       
   471 
       
   472 	file.Close();
       
   473 
       
   474 	r=fs.ReserveDriveSpace(gTestDrive,0x10000);		//reserve some disk space
       
   475 	test(r==KErrNone);
       
   476 		
       
   477 	FillUpDisk();									//fill up the disk
       
   478 
       
   479 	TVolumeInfo v;									//get disk space
       
   480 	r=fs.Volume(v,gTestDrive);
       
   481 	test(r==KErrNone);
       
   482 	freeA=v.iFree;
       
   483 
       
   484 	r=fs.GetReserveAccess(gTestDrive);				//get access to reserve space
       
   485 	test(r==KErrNone);
       
   486 
       
   487 	r=fs.Volume(v,gTestDrive);						//get disk space
       
   488 	test(r==KErrNone);
       
   489 	freeB=v.iFree;
       
   490 	
       
   491 	r=fs.ReleaseReserveAccess(gTestDrive);			//release reserve space
       
   492 	test(r==KErrNone);
       
   493 	
       
   494 	test(freeA == (freeB - 0x10000));				//test difference in space is equal to the amount reserved
       
   495 
       
   496 	r=fs.Volume(v,gTestDrive);						//get disk space
       
   497 	test(r==KErrNone);
       
   498 	freeB=v.iFree;
       
   499 	test(freeA == freeB);							//check reading is still correct
       
   500 	
       
   501 	TBuf <20> dir = KDir();
       
   502 	dir[0]=(TUint16)gCh;
       
   503 	r=fs.MkDir(dir);
       
   504 	test(r==KErrDiskFull);
       
   505 
       
   506 	r=fs.MkDirAll(dir);
       
   507 	test(r==KErrDiskFull);
       
   508 
       
   509 	TFileName temp;
       
   510 	TBuf<5> drv = KDrv();
       
   511 	drv[0]=(TUint16)gCh;
       
   512 	r=file.Temp(fs, drv, temp, EFileWrite);
       
   513 	test(r==KErrDiskFull);
       
   514 
       
   515 	r=file.Replace(fs, buf, EFileWrite);
       
   516 	test(r==KErrDiskFull);
       
   517 
       
   518 	r=file.Create(fs, buf, EFileWrite);
       
   519 	test(r==KErrDiskFull);
       
   520 
       
   521 	r=file.Open(fs, buf, EFileWrite);
       
   522 	test(r==KErrNone);
       
   523 
       
   524 	r=file.Write(128, KTestData());
       
   525 
       
   526 	if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
       
   527 		r = file.Flush();
       
   528 	
       
   529 	test(r==KErrDiskFull);
       
   530 
       
   531 	r=file.SetSize(0x1000);
       
   532 	test(r==KErrDiskFull);
       
   533 
       
   534 	r=file.SetAtt(KEntryAttHidden,0); 
       
   535 	test(r==KErrDiskFull);
       
   536 
       
   537 	TTime dtime;
       
   538 	r=file.SetModified(dtime); 
       
   539 	test(r==KErrDiskFull);
       
   540 
       
   541 	r=file.Set(dtime,KEntryAttHidden,0);
       
   542 	test(r==KErrDiskFull);
       
   543 
       
   544 	r=file.Rename(buf);
       
   545 	test(r==KErrDiskFull);
       
   546 
       
   547 	file.Close();
       
   548 
       
   549 
       
   550 	// Test that we can create a temporary file & write to it after acquiring reserved access, 
       
   551 	r=fs.GetReserveAccess(gTestDrive);				//get access to reserve space
       
   552 	test(r==KErrNone);
       
   553 
       
   554 	r=fs.Volume(v,gTestDrive);						//get disk space
       
   555 	test(r==KErrNone);
       
   556 	freeA = v.iFree;
       
   557 
       
   558 	r=file.Temp(fs, drv, temp, EFileWrite);
       
   559 	test(r==KErrNone);
       
   560 
       
   561 	r = file.Write(KTestData());
       
   562 	test (r == KErrNone);
       
   563 
       
   564 	// If write caching is enabled, call RFs::Entry() to flush the file "anonymously"
       
   565 	if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
       
   566 		{
       
   567 		r = file.Flush();
       
   568 		test (r == KErrNone);
       
   569 		}
       
   570 
       
   571 	r=fs.Volume(v,gTestDrive);						//get disk space
       
   572 	test(r==KErrNone);
       
   573 	freeB = v.iFree;
       
   574 	test (freeB < freeA);
       
   575 
       
   576 	file.Close();
       
   577 
       
   578 	r=fs.ReleaseReserveAccess(gTestDrive);			//release reserve space
       
   579 	test(r==KErrNone);
       
   580 
       
   581 
       
   582 	TBuf<20> newname =KNewName();
       
   583 	newname[0]=(TUint16)gCh;
       
   584 	r=fs.Rename(buf, newname);
       
   585 	test(r==KErrDiskFull);
       
   586 
       
   587 	r=fs.Replace(buf, newname);
       
   588 	test(r==KErrDiskFull);
       
   589 
       
   590 	r=fs.SetEntry(buf, dtime, KEntryAttHidden, 0);
       
   591 	test(r==KErrDiskFull);
       
   592 
       
   593 	r=fs.CreatePrivatePath(gTestDrive);
       
   594 	test(r==KErrDiskFull);
       
   595 
       
   596 	r=fs.SetVolumeLabel(_L("Moooo"), gTestDrive);
       
   597 	test(r==KErrDiskFull);	
       
   598 
       
   599 	r=fs.SetModified(buf, dtime);
       
   600 	test(r==KErrDiskFull);	
       
   601 
       
   602 	SynchronousClose(fs);
       
   603 	}
       
   604 
       
   605 	
       
   606 
       
   607 static void Test5()
       
   608 //
       
   609 //
       
   610 //
       
   611 	{
       
   612 	test.Next(_L("Test Session limits"));
       
   613 
       
   614 	if(IsTestingLFFS())
       
   615 		{
       
   616 		//-- This test is not valid for LFFS, because free space on this FS can change itself because of some 
       
   617         //-- internal FS activities
       
   618 		test.Printf(_L("This test is inconsistent on LFFS\n"));
       
   619 		return;
       
   620 		}
       
   621 
       
   622 
       
   623 	RFs fs1;
       
   624 	RFs fs2;
       
   625 	TInt r=KErrNone;
       
   626 
       
   627 	r=fs1.Connect();
       
   628 	test(r==KErrNone);
       
   629 	r=fs2.Connect();
       
   630 	test(r==KErrNone);
       
   631 
       
   632 	FormatDrive();
       
   633 
       
   634 	r=fs1.ReserveDriveSpace(gTestDrive,0x10000);		
       
   635 	test(r==KErrNone);
       
   636 
       
   637 	r=fs2.ReserveDriveSpace(gTestDrive,0x10000);		
       
   638 	test(r==KErrNone);
       
   639 
       
   640 	FillUpDisk();									
       
   641 
       
   642 	r=fs1.GetReserveAccess(gTestDrive);				
       
   643 	test(r==KErrNone);
       
   644 
       
   645 	TBuf<20> dir = KDir();
       
   646 	dir[0]=(TUint16)gCh;
       
   647 
       
   648 
       
   649 	r=fs2.MkDir(dir);
       
   650 	test(r==KErrDiskFull);
       
   651 
       
   652 	r=fs1.ReserveDriveSpace(gTestDrive,0); //can not release reserve space while you have reserve access
       
   653 	test(r==KErrInUse);
       
   654 
       
   655 	r=fs1.ReleaseReserveAccess(gTestDrive);				
       
   656 	test(r==KErrNone);
       
   657 
       
   658 	r=fs1.ReserveDriveSpace(gTestDrive,0); 
       
   659 	test(r==KErrNone);
       
   660 
       
   661 	r=fs2.MkDir(dir);
       
   662 	test(r==KErrNone);
       
   663 
       
   664 	SynchronousClose(fs1);
       
   665 	SynchronousClose(fs2);
       
   666 	}
       
   667 
       
   668 static TInt RsrvSpaceThread(TAny* aArg)
       
   669 	{
       
   670 	TInt r=KErrNone;
       
   671 	TInt64 fr1;
       
   672 	TInt64 fr2;
       
   673 	TInt64 diff;
       
   674 
       
   675 	TVolumeInfo v;
       
   676 	r=((RFs*)aArg)->Volume(v,gTestDrive);
       
   677 	if(r!=KErrNone)
       
   678 		return(r);
       
   679 
       
   680 	fr1=v.iFree;
       
   681 
       
   682 	r=((RFs*)aArg)->ReserveDriveSpace(gTestDrive,0x10000); 
       
   683 	if(r!=KErrNone)
       
   684 		return(r);
       
   685 
       
   686 	r=((RFs*)aArg)->Volume(v,gTestDrive);
       
   687 	if(r!=KErrNone)
       
   688 		return(r);
       
   689 	fr2=v.iFree;
       
   690 	
       
   691 	diff=fr1-fr2;
       
   692 	if(!(I64INT(diff)> 0xef38 && I64INT(diff)<0xf100))
       
   693 		return(KErrGeneral);
       
   694 	return r;
       
   695 	}
       
   696 
       
   697 static TInt SessCloseThread(TAny* aArg)
       
   698 	{
       
   699 	TInt r=KErrNone;
       
   700 	TInt64 fr1;
       
   701 	TInt64 fr2;
       
   702 	TInt64 diff;
       
   703 
       
   704 	TVolumeInfo v;
       
   705 	r=((RFs*)aArg)->Volume(v,gTestDrive);
       
   706 	if(r!=KErrNone)
       
   707 		return(r);
       
   708 	fr1=v.iFree;
       
   709 
       
   710 	((RFs*)aArg)->ReserveDriveSpace(gTestDrive,0x1000);
       
   711 	
       
   712 	r=((RFs*)aArg)->Volume(v,gTestDrive);
       
   713 	if(r!=KErrNone)
       
   714 		return(r);
       
   715 	fr2=v.iFree;
       
   716 
       
   717 	diff=fr2-fr1;
       
   718 	if(!(I64INT(diff)> 0xef38 && I64INT(diff)<0xf100))
       
   719 		return(KErrGeneral);
       
   720 
       
   721 	SynchronousClose(*((RFs*)aArg));
       
   722 
       
   723 	return r;
       
   724 	}
       
   725 
       
   726 static void Test6()
       
   727 //
       
   728 //	Test sharabale session
       
   729 //
       
   730 	{
       
   731 	
       
   732 	test.Next(_L("Test sharable session"));
       
   733 
       
   734 	RFs fsess;
       
   735 	TInt r=KErrNone;
       
   736 	TInt64 free1(0);
       
   737 	TInt64 free2(0);
       
   738 	TInt64 diff(0);
       
   739 	RThread t[KNumberThreads];
       
   740 	TRequestStatus tStat[KNumberThreads];
       
   741 
       
   742 	r=fsess.Connect();
       
   743 	test(r==KErrNone);
       
   744 
       
   745 	FormatDrive();
       
   746 
       
   747 	r= fsess.ShareAuto();
       
   748 	test(r==KErrNone);
       
   749 
       
   750 	GetFreeDiskSpace(free1);
       
   751 
       
   752 	fsess.ReserveDriveSpace(gTestDrive,0x1000);
       
   753 		
       
   754 	r = t[0].Create(_L("Sub_Thread1"),RsrvSpaceThread,KDefaultStackSize,KHeapSize,KHeapSize,&fsess); 
       
   755 	test(r==KErrNone);
       
   756 
       
   757 	t[0].Rendezvous(tStat[0]);
       
   758 	t[0].Resume();
       
   759 
       
   760 	User::WaitForRequest(tStat[0]);
       
   761 
       
   762 	t[0].Close();
       
   763 	test(tStat[0]==KErrNone);
       
   764 
       
   765 	r = t[1].Create(_L("Sub_Thread2"),SessCloseThread,KDefaultStackSize,KHeapSize,KHeapSize,&fsess); 
       
   766 	test(r==KErrNone);
       
   767 
       
   768 	t[1].Rendezvous(tStat[1]);
       
   769 	t[1].Resume();
       
   770 
       
   771 	User::WaitForRequest(tStat[1]);
       
   772 
       
   773 	t[1].Close();
       
   774 	test(tStat[1]==KErrNone);
       
   775 
       
   776 	GetFreeDiskSpace(free2);
       
   777 
       
   778 	diff = free1-free2;
       
   779 	test(I64INT(diff)== 0 || I64INT(diff)<0xFA0 );
       
   780 	}
       
   781 
       
   782 
       
   783 static void Test7()
       
   784 //
       
   785 // Tests notifier events for sessions with and without reserved access
       
   786 //
       
   787 	{
       
   788 	if(IsTestingLFFS())
       
   789 		{
       
   790 		// This test is not valid for LFFS...
       
   791 		test.Printf(_L("Test reserved access notification not run for LFFS\n"));
       
   792 		return;
       
   793 		}
       
   794 
       
   795 	
       
   796 	test.Next(_L("Test reserved access notification"));
       
   797 	
       
   798 	FormatDrive();
       
   799 
       
   800 	RFs theNrm;
       
   801 	RFs theRes;
       
   802 
       
   803 	TInt err = theNrm.Connect();
       
   804 	test(KErrNone == err);
       
   805 
       
   806 	err = theRes.Connect();
       
   807 	test(KErrNone == err);
       
   808 
       
   809 
       
   810 	TInt64 freeSpace(0);
       
   811 	GetFreeDiskSpace(freeSpace);
       
   812 
       
   813 	RFs theTestSession;
       
   814 	theTestSession.Connect();
       
   815 
       
   816 	_LIT(KFileFiller, "?:\\t_falseSpaceFiller");
       
   817 	TBuf<25> fileName;
       
   818 	fileName = KFileFiller;
       
   819 	fileName[0] = (TUint16)gCh;
       
   820 
       
   821 	err = theTestSession.Connect();
       
   822 	test(err == KErrNone);
       
   823 
       
   824 	// determine the cluster size
       
   825 	RFile theFile;
       
   826 	err=theFile.Replace(theTestSession, fileName, EFileShareAny | EFileWrite);
       
   827 	test(err==KErrNone);
       
   828 
       
   829 	// Neither notifier should be triggered here
       
   830 	err = theFile.SetSize(1);
       
   831 	test(KErrNone == err);
       
   832 	theFile.Close();
       
   833 
       
   834 	TInt64 newFreeSpace;
       
   835 	GetFreeDiskSpace(newFreeSpace);
       
   836 	TInt clusterSize = TInt(freeSpace - newFreeSpace);
       
   837 	theTestSession.Delete(fileName);
       
   838 	GetFreeDiskSpace(newFreeSpace);
       
   839 	test (newFreeSpace == freeSpace);
       
   840 
       
   841 	TInt resSpace = Max(0x1000, clusterSize);
       
   842 		
       
   843 	TVolumeInfo volInfo;
       
   844 	theNrm.Volume(volInfo, gTestDrive);
       
   845 	test(volInfo.iFree == freeSpace);
       
   846 
       
   847 	err = theRes.ReserveDriveSpace(gTestDrive, resSpace);
       
   848 	test(KErrNone == err);
       
   849 	err = theRes.GetReserveAccess(gTestDrive);
       
   850 	test(KErrNone == err);
       
   851 
       
   852 	theRes.Volume(volInfo, gTestDrive);
       
   853 	test(volInfo.iFree == freeSpace);
       
   854 
       
   855 	theNrm.Volume(volInfo, gTestDrive);
       
   856 	test(volInfo.iFree == freeSpace - resSpace);
       
   857 
       
   858 
       
   859 	//
       
   860 	// Register the notifiers and verify that the only the "Normal"
       
   861 	// and not the "Reserved" session is triggered.
       
   862 	//
       
   863 	TRequestStatus statNrm;
       
   864 	TRequestStatus statRes;
       
   865 
       
   866 	TInt64 threshold(freeSpace - resSpace*2);
       
   867 	theNrm.NotifyDiskSpace(threshold, gTestDrive, statNrm);
       
   868 	theRes.NotifyDiskSpace(threshold, gTestDrive, statRes);
       
   869 	test((statNrm == KRequestPending) && (statRes == KRequestPending));
       
   870 
       
   871 
       
   872 	//
       
   873 	// Main part of the test starts here.
       
   874 	// First we create a new file, then we increase its size to cause the
       
   875 	// "Normal" notifier to trigger but not the "Reserved" notifier
       
   876 	//
       
   877 	err=theFile.Replace(theTestSession, fileName, EFileShareAny | EFileWrite);
       
   878 	test(err==KErrNone);
       
   879 	test((statNrm == KRequestPending) && (statRes == KRequestPending));
       
   880 
       
   881 	// Neither notifier should be triggered here
       
   882 	err = theFile.SetSize(resSpace);
       
   883 	test(KErrNone == err);
       
   884 	test((statNrm == KRequestPending) && (statRes == KRequestPending));
       
   885 
       
   886 	// This should trigger the "Normal" notifier, but not the "Reserved" one
       
   887 	err = theFile.SetSize(2*resSpace);
       
   888 	test(KErrNone == err);
       
   889 	test((statNrm == KErrNone) && (statRes == KRequestPending));
       
   890 
       
   891 
       
   892 	//
       
   893 	// Reset the "Normal" notifier then increase the amount of reserved space
       
   894 	// on the drive. This should re-trigger the "Normal" notifier but leave
       
   895 	// the "Reserved" notifier untouched.
       
   896 	//
       
   897 	theNrm.NotifyDiskSpace(threshold - resSpace, gTestDrive, statNrm);
       
   898 	test((statNrm == KRequestPending) && (statRes == KRequestPending));
       
   899 
       
   900 	err = theTestSession.ReserveDriveSpace(gTestDrive, resSpace * 3);
       
   901 	if (err != KErrArgument)	// will have exceeded limit if resSpace = 32K
       
   902 		{
       
   903 		test(err == KErrNone);
       
   904 		test((statNrm == KErrNone) && (statRes == KRequestPending));
       
   905 		}
       
   906 
       
   907 	//
       
   908 	// All done - tidy up.
       
   909 	//
       
   910 	theFile.Close();
       
   911 	theTestSession.Delete(fileName);
       
   912 	theTestSession.Close();
       
   913 	theNrm.Close();
       
   914 	theRes.Close();
       
   915 	}
       
   916 
       
   917 LOCAL_C void TestForDEF142554()
       
   918     {
       
   919     test.Next(_L("Test for DEF142554: test RFile::Modified and RFile::Att when disk full"));
       
   920     
       
   921     Format(gTestDrive);
       
   922     
       
   923     TUint att;
       
   924     TTime time;
       
   925     
       
   926     RFs fs;
       
   927     TInt err = fs.Connect();
       
   928     test(err == KErrNone);
       
   929 
       
   930     RFile file;
       
   931     TBuf<20> fileName;
       
   932     fileName = KTestFile;
       
   933     fileName[0] = (TUint16)gCh;
       
   934     
       
   935     err = fs.ReserveDriveSpace(gTestDrive,0x10000); 
       
   936     test(err == KErrNone);
       
   937 
       
   938     err = file.Replace(fs, fileName, EFileWrite);
       
   939     test(err == KErrNone);
       
   940 
       
   941     err = file.Write(KTestData);
       
   942     test(err == KErrNone);
       
   943     
       
   944     err = file.Flush();
       
   945     test(err == KErrNone);
       
   946     
       
   947     file.Close();
       
   948     
       
   949     err = file.Open(fs, fileName, EFileRead);
       
   950     test(err == KErrNone);
       
   951     
       
   952     err = file.Att(att);
       
   953     test(err == KErrNone);
       
   954     
       
   955     err = file.Modified(time);
       
   956     test(err == KErrNone);
       
   957     
       
   958     file.Close();
       
   959     
       
   960     FillUpDisk();
       
   961     
       
   962     err = file.Open(fs, fileName, EFileRead);
       
   963     test(err == KErrNone);
       
   964     
       
   965     TUint att1;
       
   966     err = file.Att(att1);
       
   967     test(err == KErrNone);
       
   968     test(att1 == att);
       
   969     
       
   970     TTime time1;
       
   971     err = file.Modified(time1);
       
   972     test(err == KErrNone);
       
   973     test(time1 == time);
       
   974     
       
   975     file.Close();
       
   976     fs.Close();
       
   977     
       
   978     }
       
   979 
       
   980 
       
   981 //-----------------------------------------------------------------------------
       
   982 
       
   983 /**
       
   984     test creation of the the file that crosses 4G boundary on the FAT media
       
   985 
       
   986 */
       
   987 static void TestFAT4G_Boundary()
       
   988 	{
       
   989     const TInt64 K4Gig = 4*(TInt64)K1GigaByte;
       
   990 
       
   991 	test.Next(_L("Test files crossing 4G boundary on FAT"));
       
   992 
       
   993     if(!Is_Fat32(TheFs, gTestDrive))
       
   994 		{
       
   995 		test.Printf(_L("This test requires FAT32. Skipping.\n"));
       
   996 		return;
       
   997 		}
       
   998 
       
   999     TVolumeInfo volInfo;
       
  1000 	
       
  1001 	TInt nRes = TheFs.Volume(volInfo,gTestDrive);
       
  1002 	test(nRes == KErrNone);
       
  1003 	
       
  1004     if(volInfo.iSize < K4Gig+K1MegaByte)
       
  1005 		{
       
  1006 		test.Printf(_L("This test requires volume > 4G. Skipping.\n"));
       
  1007 		return;
       
  1008 		}
       
  1009 	
       
  1010     //-- 1. format the volume
       
  1011     FormatDrive();
       
  1012 
       
  1013     //-- find out media position of the data region start
       
  1014     TFatBootSector bootSector;
       
  1015     nRes = ReadBootSector(TheFs, gTestDrive, 0, bootSector);
       
  1016     test(nRes == KErrNone);
       
  1017     test(bootSector.IsValid());
       
  1018 
       
  1019     const TInt64 dataStartPos = bootSector.FirstDataSector() << KDefaultSectorLog2;
       
  1020     const TInt64 lowRegion = K4Gig - dataStartPos - K1MegaByte; 
       
  1021 
       
  1022 
       
  1023     //-- 2. create several empty files that take a bit less that 4gig
       
  1024     //-- the drive is freshly formatted and the files will expand linearry
       
  1025     _LIT(KBaseFN, "\\LargeFile");
       
  1026     
       
  1027     const TInt MaxDummyFiles = 5;
       
  1028     const TUint32 DummyFileLen = (TUint32)(lowRegion / MaxDummyFiles);
       
  1029 	TInt i;
       
  1030     for(i=0; i<MaxDummyFiles; ++i)
       
  1031 		{
       
  1032         nRes = CreateFileX(KBaseFN, i, DummyFileLen); 
       
  1033         test(nRes == KErrNone);
       
  1034 		}
       
  1035 
       
  1036     //-- 3. create a real file that crosses 4G boundary
       
  1037     nRes = CreateCheckableStuffedFile(TheFs, KBaseFN, 5*K1MegaByte);
       
  1038     test(nRes == KErrNone);
       
  1039     
       
  1040     test.Printf(_L("Verifying the file that crosses 4G boundary.\n"));
       
  1041 
       
  1042     nRes = VerifyCheckableFile(TheFs, KBaseFN);
       
  1043     test(nRes == KErrNone);
       
  1044 
       
  1045 	
       
  1046 	nRes = TheFs.Delete(KBaseFN);
       
  1047 	test(nRes == KErrNone);
       
  1048     for(i=0; i<MaxDummyFiles; ++i)
       
  1049 	    {
       
  1050         nRes = DeleteFileX(KBaseFN, i); 
       
  1051         test(nRes == KErrNone);
       
  1052 		}
       
  1053 	}
       
  1054 
       
  1055 //-----------------------------------------------------------------------------
       
  1056 
       
  1057 GLDEF_C void CallTestsL()
       
  1058 //
       
  1059 // Do tests relative to session path
       
  1060 //
       
  1061 	{
       
  1062     //-- set up console output 
       
  1063     Fat_Test_Utils::SetConsole(test.Console()); 
       
  1064 
       
  1065 
       
  1066 	if (gSessionPath[0]=='C')	//only test on non C drives
       
  1067 		{
       
  1068 		test.Printf(_L("TEST NOT RUN FOR THIS DRIVE"));
       
  1069 		return;
       
  1070 		}
       
  1071 	
       
  1072 	if (UserSvr::DebugMask(2)&0x00000002) // TESTFAST mode set? (for automated test builds)
       
  1073 		if(IsTestingLFFS())
       
  1074 			{
       
  1075 			// Don't run on LFFS (to increase speed of automated testing)
       
  1076 			test.Printf(_L("TEST NOT RUN FOR THIS DRIVE"));
       
  1077 			return;
       
  1078 			}
       
  1079 
       
  1080 	//get the number of the drive we are currently testing
       
  1081 	TInt r=0;
       
  1082 	r=RFs::CharToDrive(gSessionPath[0],gTestDrive);
       
  1083     test(r==KErrNone);
       
  1084 
       
  1085 	r=RFs::DriveToChar(gTestDrive,gCh);
       
  1086 	test(r==KErrNone);
       
  1087 
       
  1088     //-- print drive information
       
  1089     PrintDrvInfo(TheFs, gTestDrive);
       
  1090 
       
  1091 	Test1();	//General test for new APIs
       
  1092 	Test2();	//Test to ensure drive and session reserve limits are not exceeded
       
  1093 	Test3();
       
  1094 	Test4();	//test filling the drive and that each checked API fails
       
  1095 	Test5();
       
  1096 	Test6();
       
  1097 	Test7();
       
  1098 	TestForDEF142554();
       
  1099 	Test2();	//run this test to check reserves are being cleared correctly
       
  1100 
       
  1101 	TestFAT4G_Boundary();
       
  1102     
       
  1103     TurnAllocFailureOff();
       
  1104 	}
       
  1105 
       
  1106 
       
  1107 
       
  1108