Update contrib.
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__
27 GLDEF_D TFileName gSessionPath;
28 GLDEF_D TInt gAllocFailOff=KAllocFailureOff;
29 GLDEF_D TInt gAllocFailOn=KAllocFailureOff;
30 GLDEF_D TInt64 gSeed=51703;
32 GLDEF_D TChar gDriveToTest;
33 GLDEF_D TVolumeInfo gVolInfo; // volume info for current drive
34 GLDEF_D TFileCacheFlags gDriveCacheFlags;
36 _LIT(KPrivate, "\\Private\\");
39 ////////////////////////////////////////////////////////////
40 // Template functions encapsulating ControlIo magic
42 GLDEF_D template <class C>
43 GLDEF_C TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
45 TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
47 TInt r = fs.ControlIo(drv, fkn, ptrC);
53 GLDEF_C void CreateShortName(TDes& aFileName,TInt64& aSeed)
55 // Create a random, dos legal 8.3 char name
59 TInt length=Math::Rand(aSeed)%11;
62 else if (length==3) // don't create three letter names like 'AUX' or 'PRN'
64 else if (length>8) // end in '.' if no extension
67 aFileName.SetLength(length);
68 for(TInt i=0;i<length;i++)
75 TInt letter=Math::Rand(aSeed)%26;
76 aFileName[i]=(TText)('A'+letter);
81 GLDEF_C void CreateLongName(TDes& aFileName,TInt64& aSeed,TInt aLength)
83 // Create a random, dos legal 8.3 char name
92 length=Math::Rand(aSeed)%128;
93 length+=Math::Rand(aSeed)%128;
94 length+=Math::Rand(aSeed)%128;
95 length+=Math::Rand(aSeed)%128;
103 if (length==3) // don't create three letter names like 'AUX' or 'PRN'
106 aFileName.SetLength(length);
109 for(i=0;i<length;i++)
113 TBool illegalChar=ETrue;
117 #if defined(__WINS__)
118 if (gSessionPath[0]=='C')
119 letter=(TChar)('A'+Math::Rand(aSeed)%26);
121 letter=(TChar)Math::Rand(aSeed)%256;
123 letter=(TChar)Math::Rand(aSeed)%256;
125 TBool space=letter.IsSpace();
126 if (space && spaceChar==-1)
128 else if (!space && spaceChar!=-1)
148 aFileName[i]=(TText)letter;
159 GLDEF_C void CheckDisk()
161 // Do a checkdisk and report failure
164 test.Next(_L("Check Disk"));
165 TInt r=TheFs.CheckDisk(gSessionPath);
166 if (r!=KErrNone && r!=KErrNotSupported && r!=KErrPermissionDenied)
167 ReportCheckDiskFailure(r);
170 GLDEF_C void ReportCheckDiskFailure(TInt aRet)
172 // Report the failure of checkdisk
176 test.Printf(_L("CHECKDISK FAILED: "));
179 case 1: test.Printf(_L("File cluster chain contains a bad value (<2 or >maxCluster)\n")); break;
180 case 2: test.Printf(_L("Two files are linked to the same cluster\n")); break;
181 case 3: test.Printf(_L("Unallocated cluster contains a value != 0\n")); break;
182 case 4: test.Printf(_L("Size of file != number of clusters in chain\n")); break;
183 default: test.Printf(_L("Undefined Error value %d\n"),aRet);
189 GLDEF_C void MakeFile(const TDesC& aFileName,const TUidType& aUidType,const TDesC8& aFileContents)
191 // Make a file and write uid and data
196 TInt r=file.Replace(TheFs,aFileName,0);
197 if (r==KErrPathNotFound)
199 r=TheFs.MkDirAll(aFileName);
201 r=file.Replace(TheFs,aFileName,0);
204 TCheckedUid checkedUid(aUidType);
205 TPtrC8 uidData((TUint8*)&checkedUid,sizeof(TCheckedUid));
206 r=file.Write(uidData);
208 r=file.Write(aFileContents);
213 GLDEF_C void MakeFile(const TDesC& aFileName,const TDesC8& aFileContents)
215 // Make a file and write something in it
220 TInt r=file.Replace(TheFs,aFileName,0);
221 if (r==KErrPathNotFound)
223 r=TheFs.MkDirAll(aFileName);
225 r=file.Replace(TheFs,aFileName,0);
228 r=file.Write(aFileContents);
233 GLDEF_C void MakeFile(const TDesC& aFileName,TInt anAttributes)
235 // Make a file and write something in it
240 TInt r=file.Replace(TheFs,aFileName,0);
241 if (r==KErrPathNotFound)
243 r=TheFs.MkDirAll(aFileName);
245 r=file.Replace(TheFs,aFileName,0);
249 r=TheFs.SetAtt(aFileName,anAttributes,0);
253 GLDEF_C void MakeFile(const TDesC& aFileName)
259 MakeFile(aFileName,_L8(""));
262 GLDEF_C void MakeDir(const TDesC& aDirName)
268 TInt r=TheFs.MkDirAll(aDirName);
269 if (r!=KErrNone && r!=KErrAlreadyExists)
271 test.Printf(_L("%c: MakeDir Error %d\n"),aDirName[0],r);
277 GLDEF_C void DeleteTestDirectory()
279 // Delete the leaf session path directory
283 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden"), 0, KEntryAttHidden);
284 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden\\HiddenFile"), 0, KEntryAttHidden);
285 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden\\System"), 0, KEntryAttSystem);
286 test.Next(_L("Delete test directory"));
287 CFileMan* fMan=CFileMan::NewL(TheFs);
289 TInt r=TheFs.SessionPath(gSessionPath);
291 r=TheFs.CheckDisk(gSessionPath);
292 if (r!=KErrNone && r!=KErrNotSupported)
293 ReportCheckDiskFailure(r);
294 r=fMan->RmDir(gSessionPath);
299 GLDEF_C void CreateTestDirectory(const TDesC& aSessionPath)
301 // Create directory for test
304 TParsePtrC path(aSessionPath);
305 test(path.DrivePresent()==EFalse);
307 TInt r=TheFs.SetSessionPath(aSessionPath);
309 r=TheFs.SessionPath(gSessionPath);
311 r=TheFs.MkDirAll(gSessionPath);
312 test_Value(r, r == KErrNone || r == KErrAlreadyExists);
315 GLDEF_C TInt CurrentDrive()
317 // Return the current drive number
322 TInt r=TheFs.CharToDrive(gSessionPath[0],driveNum);
327 GLDEF_C void Format(TInt aDrive)
329 // Format current drive
333 test.Next(_L("Format"));
334 TBuf<4> driveBuf=_L("?:\\");
335 driveBuf[0]=(TText)(aDrive+'A');
338 TInt r=format.Open(TheFs,driveBuf,EQuickFormat,count);
342 TInt r=format.Next(count);
348 LOCAL_C void PushLotsL()
350 // Expand the cleanup stack
355 CleanupStack::PushL((CBase*)NULL);
356 CleanupStack::Pop(1000);
360 LOCAL_C void DoTests(TInt aDrive)
362 // Do testing on aDrive
366 gSessionPath=_L("?:\\F32-TST\\");
368 TInt r=TheFs.DriveToChar(aDrive,driveLetter);
370 gSessionPath[0]=(TText)driveLetter;
371 r=TheFs.SetSessionPath(gSessionPath);
374 User::After(1000000);
376 // Format(CurrentDrive());
378 test.Printf(_L("Creating session path"));
379 r=TheFs.MkDirAll(gSessionPath);
382 test.Printf(_L("Attempting to create directory \'%S\' failed, KErrCorrupt\n"), &gSessionPath);
383 test.Printf(_L("This could be caused by a previous failing test, or a test media defect\n"));
384 test.Printf(_L("Formatting drive, retrying MkDirall\nShould subsequent tests fail with KErrCorrupt (%d) as well, replace test medium !\n"),
387 r=TheFs.MkDirAll(gSessionPath);
390 else if (r == KErrNotReady)
393 r=TheFs.Drive(d, aDrive);
395 if (d.iType == EMediaNotPresent)
396 test.Printf(_L("%c: Medium not present - cannot perform test.\n"), (TUint)driveLetter);
398 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);
400 test_Value(r, r == KErrNone || r == KErrAlreadyExists);
401 TheFs.ResourceCountMarkStart();
402 test.Printf(_L("Calling main test sequence ...\n"));
403 TRAP(r,CallTestsL());
405 test.Printf(_L("test sequence completed without error\n"));
406 TheFs.ResourceCountMarkEnd();
412 void ParseCommandArguments()
418 User::CommandLine(cmd);
420 TPtrC token=lex.NextToken();
421 TFileName thisfile=RProcess().FileName();
422 if (token.MatchF(thisfile)==0)
424 token.Set(lex.NextToken());
426 test.Printf(_L("CLP=%S\n"),&token);
428 if(token.Length()!=0)
430 gDriveToTest=token[0];
431 gDriveToTest.UpperCase();
438 TBool gPrimaryExtensionExists = EFalse;
440 GLDEF_C TInt DismountFileSystem(RFs& aFs, const TDesC& aFileSystemName,TInt aDrive)
442 //Make note of the first extension if it exists, so that we remount
443 //it when the file system is remounted.
444 TInt r = aFs.ExtensionName(gExtName, aDrive, 0);
448 gPrimaryExtensionExists = ETrue;
450 return aFs.DismountFileSystem(aFileSystemName, aDrive);
453 GLDEF_C TInt MountFileSystem(RFs& aFs, const TDesC& aFileSystemName,TInt aDrive, TBool aIsSync)
456 if (gPrimaryExtensionExists)
458 r = aFs.MountFileSystem(aFileSystemName, gExtName, aDrive, aIsSync);
462 r = aFs. MountFileSystem(aFileSystemName, aDrive, aIsSync);
467 GLDEF_C TInt E32Main()
469 // Test with drive nearly full
473 CTrapCleanup* cleanup;
474 cleanup=CTrapCleanup::New();
475 TRAPD(r,PushLotsL());
479 test.Start(_L("Starting tests..."));
482 ParseCommandArguments(); //need this for drive letter to test
487 TheFs.SetAllocFailure(gAllocFailOn);
491 TheFs.SessionPath(sessionp);
494 privatedir = KPrivate;
496 TUid thisUID = RProcess().Identity();
497 privatedir.AppendFormat(_L("%08x"),thisUID.iUid);
498 privatedir.Append(_L("\\"));
500 test(privatedir == sessionp.Mid(2,sessionp.Length()-2));
502 test.Printf(_L("sp=%S\n"),&sessionp);
503 sessionp[0]=(TText)gDriveToTest;
504 test.Printf(_L("sp1=%S\n"),&sessionp);
507 r=TheFs.CharToDrive(gDriveToTest,theDrive);
510 // Get the TFileCacheFlags for this drive
511 r = TheFs.Volume(gVolInfo, theDrive);
512 if (r == KErrNotReady)
515 TInt err = TheFs.Drive(info,theDrive);
517 if (info.iType == EMediaNotPresent)
518 test.Printf(_L("%c: Medium not present - cannot perform test.\n"), (TUint)gDriveToTest);
520 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);
522 else if (r == KErrCorrupt)
524 test.Printf(_L("%c: Media corruption; previous test may have aborted; else, check hardware\n"), (TUint)gDriveToTest);
527 gDriveCacheFlags = gVolInfo.iFileCacheFlags;
528 test.Printf(_L("DriveCacheFlags = %08X\n"), gDriveCacheFlags);
530 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
531 TPckgBuf<TIOCacheValues> pkgOrgValues;
532 TIOCacheValues& orgValues=pkgOrgValues();
533 r = controlIo(TheFs,theDrive, KControlIoCacheCount, orgValues);
536 test.Printf(_L("\n"));
537 test.Printf(_L("Requests on close queue at start=%d\n"),orgValues.iCloseCount);
538 test.Printf(_L("Requests on free queue at start=%d\n"),orgValues.iFreeCount);
539 test.Printf(_L("Requests dynamically allocated at start=%d\n"),orgValues.iAllocated);
540 test.Printf(_L("Requests in total at start=%d\n"),orgValues.iTotalCount);
544 // flush closed files queue
545 r = TheFs.ControlIo(theDrive, KControlIoFlushClosedFiles);
548 // get number of items on File Cache
549 TFileCacheStats startFileCacheStats;
550 r = controlIo(TheFs,theDrive, KControlIoFileCacheStats, startFileCacheStats);
551 test_Value(r, r == KErrNone || r == KErrNotSupported);
552 test.Printf(_L("File cache: Cachelines (free %d, used %d), Segments(allocated %d locked %d). Closed files(%d)\n"),
553 startFileCacheStats.iFreeCount,
554 startFileCacheStats.iUsedCount,
555 startFileCacheStats.iAllocatedSegmentCount,
556 startFileCacheStats.iLockedSegmentCount,
557 startFileCacheStats.iFilesOnClosedQueue);
564 TTimeIntervalSeconds timeTakenC;
565 r=endTimeC.SecondsFrom(timerC,timeTakenC);
567 test.Printf(_L("Time taken for test = %d seconds\n"),timeTakenC.Int());
568 TheFs.SetAllocFailure(gAllocFailOff);
570 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
571 TPckgBuf<TIOCacheValues> pkgValues;
572 TIOCacheValues& values=pkgValues();
573 r = controlIo(TheFs,theDrive, KControlIoCacheCount, values);
576 test.Printf(_L("Requests on close queue at end=%d\n"),values.iCloseCount);
577 test.Printf(_L("Requests on free queue at end=%d\n"),values.iFreeCount);
578 test.Printf(_L("Requests dynamically allocated at end=%d\n"),values.iAllocated);
579 test.Printf(_L("Requests in total at end=%d\n"),values.iTotalCount);
581 test(orgValues.iCloseCount==values.iCloseCount);
582 test(orgValues.iAllocated == values.iAllocated);
583 // The free count can increase if the file server runs out of requests in the RequestAllocator
584 // free pool but this should never decrease - this implies a request leak
585 test(orgValues.iFreeCount <= values.iFreeCount);
587 // The total number of allocated requests should be equal to :
588 // requests on the close queue + requests on free queue
589 // + 1 (because we used one request to issue KControlIoCacheCount)
590 // If this doesn't equate then this implies a request leak
591 test(values.iTotalCount == values.iCloseCount + values.iFreeCount + 1);
594 TFileCacheStats endFileCacheStats;
595 r = controlIo(TheFs,theDrive, KControlIoFileCacheStats, endFileCacheStats);
596 test_Value(r, r == KErrNone || r == KErrNotSupported);
598 test.Printf(_L("File cache: Cachelines (free %d, used %d), Segments(allocated %d locked %d). Closed files(%d)\n"),
599 endFileCacheStats.iFreeCount,
600 endFileCacheStats.iUsedCount,
601 endFileCacheStats.iAllocatedSegmentCount,
602 endFileCacheStats.iLockedSegmentCount,
603 endFileCacheStats.iFilesOnClosedQueue);
605 // flush closed files queue
606 test.Printf(_L("Flushing close queue..."));
607 r = TheFs.ControlIo(theDrive, KControlIoFlushClosedFiles);
610 r = controlIo(TheFs,theDrive, KControlIoFileCacheStats, endFileCacheStats);
611 test_Value(r, r == KErrNone || r == KErrNotSupported);
612 test.Printf(_L("File cache: Cachelines (free %d, used %d), Segments(allocated %d locked %d). Closed files(%d)\n"),
613 endFileCacheStats.iFreeCount,
614 endFileCacheStats.iUsedCount,
615 endFileCacheStats.iAllocatedSegmentCount,
616 endFileCacheStats.iLockedSegmentCount,
617 endFileCacheStats.iFilesOnClosedQueue);
622 test(startFileCacheStats.iFreeCount == endFileCacheStats.iFreeCount);
623 test(startFileCacheStats.iUsedCount == endFileCacheStats.iUsedCount);
624 test(startFileCacheStats.iAllocatedSegmentCount == endFileCacheStats.iAllocatedSegmentCount);
625 test(startFileCacheStats.iLockedSegmentCount == endFileCacheStats.iLockedSegmentCount);
626 test(startFileCacheStats.iFileCount == endFileCacheStats.iFileCount);