First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\server\t_main.cpp
18 #define __E32TEST_EXTENSION__
29 GLDEF_D TFileName gSessionPath;
30 GLDEF_D TFileName gExeFileName(RProcess().FileName());
31 GLDEF_D TInt gAllocFailOff=KAllocFailureOff;
32 GLDEF_D TInt gAllocFailOn=KAllocFailureOff;
33 GLDEF_D TInt64 gSeed=51703;
35 GLDEF_D TChar gDriveToTest;
36 GLDEF_D TVolumeInfo gVolInfo; // volume info for current drive
37 GLDEF_D TFileCacheFlags gDriveCacheFlags;
39 _LIT(KPrivate, "\\Private\\");
42 ////////////////////////////////////////////////////////////
43 // Template functions encapsulating ControlIo magic
45 GLDEF_D template <class C>
46 GLDEF_C TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
48 TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
50 TInt r = fs.ControlIo(drv, fkn, ptrC);
55 GLDEF_C void CreateShortName(TDes& aFileName,TInt64& aSeed)
57 // Create a random, dos legal 8.3 char name
61 TInt length=Math::Rand(aSeed)%11;
64 else if (length==3) // don't create three letter names like 'AUX' or 'PRN'
66 else if (length>8) // end in '.' if no extension
69 aFileName.SetLength(length);
70 for(TInt i=0;i<length;i++)
77 TInt letter=Math::Rand(aSeed)%26;
78 aFileName[i]=(TText)('A'+letter);
82 _LIT(KFatName, "Fat");
83 _LIT(KFat32Name, "Fat32");
86 TBool isFAT(RFs &aFsSession, TInt aDrive)
89 TInt r = aFsSession.FileSystemName(f, aDrive);
90 test_Value(r, r == KErrNone || r == KErrNotFound);
91 return (f.CompareF(KFatName) == 0 || f.CompareF(KFat32Name) == 0);
95 TUint16 getRootEntCnt(RFs &aFsSession, TInt aDrive)
99 TPtr8 reader((TUint8*)&rootEntCnt, sizeof(rootEntCnt));
101 test_KErrNone(rdisk.Open(aFsSession, aDrive));
102 test_KErrNone(rdisk.Read(17, reader)); // "BPB_RootEntCnt" field at offset 17
108 GLDEF_C TBool IsFileSystemFAT(RFs &aFsSession,TInt aDrive)
110 // return true if FAT on aDrive
113 return (isFAT(aFsSession, aDrive) && getRootEntCnt(aFsSession, aDrive) != 0);
116 GLDEF_C TBool IsFileSystemFAT32(RFs &aFsSession,TInt aDrive)
118 // return true if FAT32 on aDrive
121 return (isFAT(aFsSession, aDrive) && getRootEntCnt(aFsSession, aDrive) == 0);
124 GLDEF_C void CreateLongName(TDes& aFileName,TInt64& aSeed,TInt aLength)
126 // Create a random, dos legal 8.3 char name
135 length=Math::Rand(aSeed)%128;
136 length+=Math::Rand(aSeed)%128;
137 length+=Math::Rand(aSeed)%128;
138 length+=Math::Rand(aSeed)%128;
146 if (length==3) // don't create three letter names like 'AUX' or 'PRN'
149 aFileName.SetLength(length);
152 for(i=0;i<length;i++)
156 TBool illegalChar=ETrue;
160 #if defined(__WINS__)
161 if (gSessionPath[0]=='C')
162 letter=(TChar)('A'+Math::Rand(aSeed)%26);
164 letter=(TChar)Math::Rand(aSeed)%256;
166 letter=(TChar)Math::Rand(aSeed)%256;
168 TBool space=letter.IsSpace();
169 if (space && spaceChar==-1)
171 else if (!space && spaceChar!=-1)
191 aFileName[i]=(TText)letter;
201 GLDEF_C void CheckEntry(const TDesC& aName,TUint anAttributes,const TTime& aModified)
203 // Checks the values associated with an entry
208 TInt r=TheFs.Entry(aName,entry);
210 test(entry.iAtt==anAttributes);
211 if (aModified!=TTime(0))
212 test(entry.iModified==aModified);
215 GLDEF_C void CheckDisk()
217 // Do a checkdisk and report failure
220 test.Next(_L("Check Disk"));
221 TInt r=TheFs.CheckDisk(gSessionPath);
222 if (r!=KErrNone && r!=KErrNotSupported && r!=KErrPermissionDenied)
223 ReportCheckDiskFailure(r);
226 GLDEF_C void ReportCheckDiskFailure(TInt aRet)
228 // Report the failure of checkdisk
232 test.Printf(_L("CHECKDISK FAILED: "));
235 case 1: test.Printf(_L("File cluster chain contains a bad value (<2 or >maxCluster)\n")); break;
236 case 2: test.Printf(_L("Two files are linked to the same cluster\n")); break;
237 case 3: test.Printf(_L("Unallocated cluster contains a value != 0\n")); break;
238 case 4: test.Printf(_L("Size of file != number of clusters in chain\n")); break;
239 default: test.Printf(_L("Undefined Error value %d\n"),aRet);
244 GLDEF_C void TurnAllocFailureOff()
246 // Switch off all allocFailure
250 test.Printf(_L("Disable Alloc Failure\n"));
251 TheFs.SetAllocFailure(gAllocFailOff);
252 gAllocFailOn=KAllocFailureOff; // Disable gAllocFailOn
255 GLDEF_C void TurnAllocFailureOn()
257 // Switch off all allocFailure
261 test.Printf(_L("Enable Alloc Failure\n"));
262 gAllocFailOn=KAllocFailureOn; // Enable gAllocFailOn
263 TheFs.SetAllocFailure(gAllocFailOn);
266 GLDEF_C void MakeFile(const TDesC& aFileName,const TUidType& aUidType,const TDesC8& aFileContents)
268 // Make a file and write uid and data
273 TInt r=file.Replace(TheFs,aFileName,0);
274 if (r==KErrPathNotFound)
276 r=TheFs.MkDirAll(aFileName);
278 r=file.Replace(TheFs,aFileName,0);
281 TCheckedUid checkedUid(aUidType);
282 TPtrC8 uidData((TUint8*)&checkedUid,sizeof(TCheckedUid));
283 r=file.Write(uidData);
285 r=file.Write(aFileContents);
290 GLDEF_C void MakeFile(const TDesC& aFileName,const TDesC8& aFileContents)
292 // Make a file and write something in it
297 TInt r=file.Replace(TheFs,aFileName,0);
298 if (r==KErrPathNotFound)
300 r=TheFs.MkDirAll(aFileName);
302 r=file.Replace(TheFs,aFileName,0);
305 r=file.Write(aFileContents);
310 GLDEF_C void MakeFile(const TDesC& aFileName,TInt anAttributes)
312 // Make a file and write something in it
317 TInt r=file.Replace(TheFs,aFileName,0);
318 if (r==KErrPathNotFound)
320 r=TheFs.MkDirAll(aFileName);
322 r=file.Replace(TheFs,aFileName,0);
326 r=TheFs.SetAtt(aFileName,anAttributes,0);
330 GLDEF_C void SetSessionPath(const TDesC& aPathName)
332 // Set the session path and update gSessionPath
336 TInt r=TheFs.SetSessionPath(aPathName);
338 r=TheFs.SessionPath(gSessionPath);
342 GLDEF_C void MakeFile(const TDesC& aFileName)
348 MakeFile(aFileName,_L8(""));
351 GLDEF_C void MakeDir(const TDesC& aDirName)
357 TInt r=TheFs.MkDirAll(aDirName);
358 if (r!=KErrNone && r!=KErrAlreadyExists)
360 test.Printf(_L("%c: MakeDir Error %d\n"),aDirName[0],r);
365 GLDEF_C TInt CheckFileExists(const TDesC& aName,TInt aResult,TBool aCompRes/*=ETrue*/)
367 // Check aName exists
372 TInt r=TheFs.Entry(aName,entry);
373 test_Value(r, r == KErrNone || r == aResult);
374 if (aResult!=KErrNone)
376 TParsePtrC nameParse(aName);
377 TParsePtrC entryParse(entry.iName);
378 TBool nameMatch=(entryParse.Name()==nameParse.Name());
379 TBool extMatch=(entryParse.Ext()==nameParse.Ext()) || (entryParse.Ext().Length()<=1 && nameParse.Ext().Length()<=1);
380 test((nameMatch && extMatch)==aCompRes);
384 GLDEF_C void CheckFileContents(const TDesC& aName,const TDesC8& aContents)
386 // Check contents of file
391 TInt r=f.Open(TheFs,aName,EFileRead);
393 HBufC8* testBuf=HBufC8::NewL(aContents.Length());
395 TPtr8 bufPtr(testBuf->Des());
398 test(bufPtr==aContents);
401 test(bufPtr.Length()==0);
406 GLDEF_C void DeleteTestDirectory()
408 // Delete the leaf session path directory
412 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden"), 0, KEntryAttHidden);
413 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden\\HiddenFile"), 0, KEntryAttHidden);
414 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden\\System"), 0, KEntryAttSystem);
415 test.Next(_L("Delete test directory"));
416 CFileMan* fMan=CFileMan::NewL(TheFs);
418 TInt r=TheFs.SessionPath(gSessionPath);
420 r=TheFs.CheckDisk(gSessionPath);
421 if (r!=KErrNone && r!=KErrNotSupported)
422 ReportCheckDiskFailure(r);
423 r=fMan->RmDir(gSessionPath);
428 GLDEF_C void CreateTestDirectory(const TDesC& aSessionPath)
430 // Create directory for test
433 TParsePtrC path(aSessionPath);
434 test(path.DrivePresent()==EFalse);
436 TInt r=TheFs.SetSessionPath(aSessionPath);
438 r=TheFs.SessionPath(gSessionPath);
440 r=TheFs.MkDirAll(gSessionPath);
441 test_Value(r, r == KErrNone || r == KErrAlreadyExists);
444 GLDEF_C TInt CurrentDrive()
446 // Return the current drive number
451 TInt r=TheFs.CharToDrive(gSessionPath[0],driveNum);
456 GLDEF_C void Format(TInt aDrive)
458 // Format current drive
462 test.Next(_L("Format"));
463 TBuf<4> driveBuf=_L("?:\\");
464 driveBuf[0]=(TText)(aDrive+'A');
467 TInt r=format.Open(TheFs,driveBuf,EQuickFormat,count);
471 TInt r=format.Next(count);
477 LOCAL_C void PushLotsL()
479 // Expand the cleanup stack
484 CleanupStack::PushL((CBase*)NULL);
485 CleanupStack::Pop(1000);
489 LOCAL_C void DoTests(TInt aDrive)
491 // Do testing on aDrive
495 gSessionPath=_L("?:\\F32-TST\\");
497 TInt r=TheFs.DriveToChar(aDrive,driveLetter);
499 gSessionPath[0]=(TText)driveLetter;
500 r=TheFs.SetSessionPath(gSessionPath);
503 // !!! Disable platform security tests until we get the new APIs
504 // if(User::Capability() & KCapabilityRoot)
505 CheckMountLFFS(TheFs,driveLetter);
507 User::After(1000000);
509 // Format(CurrentDrive());
511 test.Printf(_L("Creating session path"));
512 r=TheFs.MkDirAll(gSessionPath);
515 test.Printf(_L("Attempting to create directory \'%S\' failed, KErrCorrupt\n"), &gSessionPath);
516 test.Printf(_L("This could be caused by a previous failing test, or a test media defect\n"));
517 test.Printf(_L("Formatting drive, retrying MkDirall\nShould subsequent tests fail with KErrCorrupt (%d) as well, replace test medium !\n"),
520 r=TheFs.MkDirAll(gSessionPath);
523 else if (r == KErrNotReady)
526 r=TheFs.Drive(d, aDrive);
528 if (d.iType == EMediaNotPresent)
529 test.Printf(_L("%c: Medium not present - cannot perform test.\n"), (TUint)driveLetter);
531 test.Printf(_L("medium found (type %d) but drive %c: not ready\nPrevious test may have hung; else, check hardware.\n"), (TInt)d.iType, (TUint)driveLetter);
533 test_Value(r, r == KErrNone || r == KErrAlreadyExists);
534 TheFs.ResourceCountMarkStart();
535 test.Printf(_L("Calling main test sequence ...\n"));
536 TRAP(r,CallTestsL());
538 test.Printf(_L("test sequence completed without error\n"));
539 TheFs.ResourceCountMarkEnd();
546 void ParseCommandArguments()
552 User::CommandLine(cmd);
554 TPtrC token=lex.NextToken();
555 TFileName thisfile=RProcess().FileName();
556 if (token.MatchF(thisfile)==0)
558 token.Set(lex.NextToken());
560 test.Printf(_L("CLP=%S\n"),&token);
562 if(token.Length()!=0)
564 gDriveToTest=token[0];
565 gDriveToTest.UpperCase();
572 TBool gPrimaryExtensionExists = EFalse;
574 GLDEF_C TInt DismountFileSystem(RFs& aFs, const TDesC& aFileSystemName,TInt aDrive)
576 //Make note of the first extension if it exists, so that we remount
577 //it when the file system is remounted.
578 TInt r = aFs.ExtensionName(gExtName, aDrive, 0);
582 gPrimaryExtensionExists = ETrue;
584 return aFs.DismountFileSystem(aFileSystemName, aDrive);
587 GLDEF_C TInt MountFileSystem(RFs& aFs, const TDesC& aFileSystemName,TInt aDrive, TBool aIsSync)
590 if (gPrimaryExtensionExists)
592 r = aFs.MountFileSystem(aFileSystemName, gExtName, aDrive, aIsSync);
596 r = aFs. MountFileSystem(aFileSystemName, aDrive, aIsSync);
601 GLDEF_C TInt E32Main()
603 // Test with drive nearly full
607 CTrapCleanup* cleanup;
608 cleanup=CTrapCleanup::New();
609 TRAPD(r,PushLotsL());
613 test.Start(_L("Starting tests..."));
616 ParseCommandArguments(); //need this for drive letter to test
621 TheFs.SetAllocFailure(gAllocFailOn);
625 TheFs.SessionPath(sessionp);
628 privatedir = KPrivate;
630 TUid thisUID = RProcess().Identity();
631 privatedir.AppendFormat(_L("%08x"),thisUID.iUid);
632 privatedir.Append(_L("\\"));
634 test(privatedir == sessionp.Mid(2,sessionp.Length()-2));
636 test.Printf(_L("sp=%S\n"),&sessionp);
637 sessionp[0]=(TText)gDriveToTest;
638 test.Printf(_L("sp1=%S\n"),&sessionp);
641 r=TheFs.CharToDrive(gDriveToTest,theDrive);
644 // Get the TFileCacheFlags for this drive
645 r = TheFs.Volume(gVolInfo, theDrive);
646 if (r == KErrNotReady)
649 TInt err = TheFs.Drive(info,theDrive);
651 if (info.iType == EMediaNotPresent)
652 test.Printf(_L("%c: Medium not present - cannot perform test.\n"), (TUint)gDriveToTest);
654 test.Printf(_L("%c: medium found (type %d) but drive not ready\nPrevious test may have hung; else, check hardware.\n"), (TUint)gDriveToTest, (TInt)info.iType);
656 else if (r == KErrCorrupt)
658 test.Printf(_L("%c: Media corruption; previous test may have aborted; else, check hardware\n"), (TUint)gDriveToTest);
661 gDriveCacheFlags = gVolInfo.iFileCacheFlags;
662 test.Printf(_L("DriveCacheFlags = %08X\n"), gDriveCacheFlags);
664 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
665 TPckgBuf<TIOCacheValues> pkgOrgValues;
666 TIOCacheValues& orgValues=pkgOrgValues();
667 r = controlIo(TheFs,theDrive, KControlIoCacheCount, orgValues);
670 test.Printf(_L("\n"));
671 test.Printf(_L("Requests on close queue at start=%d\n"),orgValues.iCloseCount);
672 test.Printf(_L("Requests on free queue at start=%d\n"),orgValues.iFreeCount);
673 test.Printf(_L("Requests dynamically allocated at start=%d\n"),orgValues.iAllocated);
674 test.Printf(_L("Requests in total at start=%d\n"),orgValues.iTotalCount);
678 // flush closed files queue
679 r = TheFs.ControlIo(theDrive, KControlIoFlushClosedFiles);
682 // get number of items on File Cache
683 TFileCacheStats startFileCacheStats;
684 r = controlIo(TheFs,theDrive, KControlIoFileCacheStats, startFileCacheStats);
685 test_Value(r, r == KErrNone || r == KErrNotSupported);
686 test.Printf(_L("File cache: Cachelines (free %d, used %d), Segments(allocated %d locked %d). Closed files(%d)\n"),
687 startFileCacheStats.iFreeCount,
688 startFileCacheStats.iUsedCount,
689 startFileCacheStats.iAllocatedSegmentCount,
690 startFileCacheStats.iLockedSegmentCount,
691 startFileCacheStats.iFilesOnClosedQueue);
698 TTimeIntervalSeconds timeTakenC;
699 r=endTimeC.SecondsFrom(timerC,timeTakenC);
701 test.Printf(_L("Time taken for test = %d seconds\n"),timeTakenC.Int());
702 TheFs.SetAllocFailure(gAllocFailOff);
704 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
705 TPckgBuf<TIOCacheValues> pkgValues;
706 TIOCacheValues& values=pkgValues();
707 r = controlIo(TheFs,theDrive, KControlIoCacheCount, values);
710 test.Printf(_L("Requests on close queue at end=%d\n"),values.iCloseCount);
711 test.Printf(_L("Requests on free queue at end=%d\n"),values.iFreeCount);
712 test.Printf(_L("Requests dynamically allocated at end=%d\n"),values.iAllocated);
713 test.Printf(_L("Requests in total at end=%d\n"),values.iTotalCount);
715 test(orgValues.iCloseCount==values.iCloseCount);
716 test(orgValues.iAllocated == values.iAllocated);
717 // The free count can increase if the file server runs out of requests in the RequestAllocator
718 // free pool but this should never decrease - this implies a request leak
719 test(orgValues.iFreeCount <= values.iFreeCount);
721 // The total number of allocated requests should be equal to :
722 // requests on the close queue + requests on free queue
723 // + 1 (because we used one request to issue KControlIoCacheCount)
724 // If this doesn't equate then this implies a request leak
725 test(values.iTotalCount == values.iCloseCount + values.iFreeCount + 1);
728 TFileCacheStats endFileCacheStats;
729 r = controlIo(TheFs,theDrive, KControlIoFileCacheStats, endFileCacheStats);
730 test_Value(r, r == KErrNone || r == KErrNotSupported);
732 test.Printf(_L("File cache: Cachelines (free %d, used %d), Segments(allocated %d locked %d). Closed files(%d)\n"),
733 endFileCacheStats.iFreeCount,
734 endFileCacheStats.iUsedCount,
735 endFileCacheStats.iAllocatedSegmentCount,
736 endFileCacheStats.iLockedSegmentCount,
737 endFileCacheStats.iFilesOnClosedQueue);
739 // flush closed files queue
740 test.Printf(_L("Flushing close queue..."));
741 r = TheFs.ControlIo(theDrive, KControlIoFlushClosedFiles);
744 r = controlIo(TheFs,theDrive, KControlIoFileCacheStats, endFileCacheStats);
745 test_Value(r, r == KErrNone || r == KErrNotSupported);
746 test.Printf(_L("File cache: Cachelines (free %d, used %d), Segments(allocated %d locked %d). Closed files(%d)\n"),
747 endFileCacheStats.iFreeCount,
748 endFileCacheStats.iUsedCount,
749 endFileCacheStats.iAllocatedSegmentCount,
750 endFileCacheStats.iLockedSegmentCount,
751 endFileCacheStats.iFilesOnClosedQueue);
756 test(startFileCacheStats.iFreeCount == endFileCacheStats.iFreeCount);
757 test(startFileCacheStats.iUsedCount == endFileCacheStats.iUsedCount);
758 test(startFileCacheStats.iAllocatedSegmentCount == endFileCacheStats.iAllocatedSegmentCount);
759 test(startFileCacheStats.iLockedSegmentCount == endFileCacheStats.iLockedSegmentCount);
760 test(startFileCacheStats.iFileCount == endFileCacheStats.iFileCount);