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 // Based on t_main.cpp as used for other tests.
18 //! @file f32test\concur\t_cfsmain.cpp
27 #include "cfafsdlyif.h"
30 GLDEF_D TFileName gSessionPath;
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 TUint32 gDebugFlags = 0;
38 GLDEF_D TPtrC gArgV[128];
39 GLDEF_D TInt gArgC = 0;
41 LOCAL_D TTestType TheTestType=ENotifierNone;
42 LOCAL_D RSemaphore ControlIoSem;
44 TMediaPassword thePassword=_L8("abc");
46 enum TTestCode {ESimLockRemMed, EClearSimLockRemMed};
48 const TInt KControlIoRemMedLock=5; //setflag
49 const TInt KControlClearMedLock=6; //clears flag
50 const TInt KControIoRemMedRepeat=9;
52 _LIT(KNotifierHang,"Hang");
53 _LIT(KNotifierRepeat,"Repeat");
54 _LIT(KNotifierWithRepeat,"WithRepeat");
55 _LIT(KPrivate, "\\Private\\");
57 GLDEF_C TBool IsTestTypeNotifyHang()
62 return(TheTestType==ENotifierHang);
65 GLDEF_C TBool IsTestTypeNotifyRepeat()
70 return(TheTestType==ENotifierRepeat);
73 GLDEF_C TBool IsTestTypeNotifyWithRepeat()
78 return(TheTestType==ENotifierWithRepeat);
82 GLDEF_C TBool IsTestTypeStandard()
87 return(TheTestType==ENotifierNone);
90 GLDEF_C TTestType TestType()
98 GLDEF_C void CreateShortName(TDes& aFileName,TInt64& aSeed)
100 // Create a random, dos legal 8.3 char name
104 TInt length=Math::Rand(aSeed)%11;
107 else if (length==3) // don't create three letter names like 'AUX' or 'PRN'
109 else if (length>8) // end in '.' if no extension
112 aFileName.SetLength(length);
113 for(TInt i=0;i<length;i++)
120 TInt letter=Math::Rand(aSeed)%26;
121 aFileName[i]=(TText)('A'+letter);
125 _LIT(KFatName,"Fat");
126 GLDEF_C TBool IsFileSystemFAT(RFs &aFsSession,TInt aDrive)
128 // return true if fat on aDrive
132 TInt r=aFsSession.FileSystemName(f,aDrive);
133 test(r==KErrNone || r==KErrNotFound);
134 return (f.CompareF(KFatName)==0);
137 GLDEF_C void CreateLongName(TDes& aFileName,TInt64& aSeed,TInt aLength)
139 // Create a random, dos legal 8.3 char name
148 length=Math::Rand(aSeed)%128;
149 length+=Math::Rand(aSeed)%128;
150 length+=Math::Rand(aSeed)%128;
151 length+=Math::Rand(aSeed)%128;
159 if (length==3) // don't create three letter names like 'AUX' or 'PRN'
162 aFileName.SetLength(length);
165 for(i=0;i<length;i++)
169 TBool illegalChar=ETrue;
173 #if defined(__WINS__)
174 if (gSessionPath[0]=='C')
175 letter=(TChar)('A'+Math::Rand(aSeed)%26);
177 letter=(TChar)Math::Rand(aSeed)%256;
179 letter=(TChar)Math::Rand(aSeed)%256;
181 TBool space=letter.IsSpace();
182 if (space && spaceChar==-1)
184 else if (!space && spaceChar!=-1)
204 aFileName[i]=(TText)letter;
214 GLDEF_C void CheckEntry(const TDesC& aName,TUint anAttributes,const TTime& aModified)
216 // Checks the values associated with an entry
221 TInt r=TheFs.Entry(aName,entry);
223 test(entry.iAtt==anAttributes);
224 if (aModified!=TTime(0))
225 test(entry.iModified==aModified);
228 GLDEF_C void CheckDisk()
230 // Do a checkdisk and report failure
233 test.Next(_L("Check Disk"));
234 TInt r=TheFs.CheckDisk(gSessionPath);
235 if (r!=KErrNone && r!=KErrNotSupported && r!=KErrPermissionDenied)
236 ReportCheckDiskFailure(r);
239 GLDEF_C void ReportCheckDiskFailure(TInt aRet)
241 // Report the failure of checkdisk
245 test.Printf(_L("CHECKDISK FAILED: "));
248 case 1: test.Printf(_L("File cluster chain contains a bad value (<2 or >maxCluster)\n")); break;
249 case 2: test.Printf(_L("Two files are linked to the same cluster\n")); break;
250 case 3: test.Printf(_L("Unallocated cluster contains a value != 0\n")); break;
251 case 4: test.Printf(_L("Size of file != number of clusters in chain\n")); break;
252 default: test.Printf(_L("Undefined Error value %d\n"),aRet);
257 GLDEF_C void TurnAllocFailureOff()
259 // Switch off all allocFailure
263 test.Printf(_L("Disable Alloc Failure\n"));
264 TheFs.SetAllocFailure(gAllocFailOff);
265 gAllocFailOn=KAllocFailureOff; // Disable gAllocFailOn
268 GLDEF_C void TurnAllocFailureOn()
270 // Switch off all allocFailure
274 test.Printf(_L("Enable Alloc Failure\n"));
275 gAllocFailOn=KAllocFailureOn; // Enable gAllocFailOn
276 TheFs.SetAllocFailure(gAllocFailOn);
279 GLDEF_C void MakeFile(const TDesC& aFileName,const TUidType& aUidType,const TDesC8& aFileContents)
281 // Make a file and write uid and data
286 TInt r=file.Replace(TheFs,aFileName,0);
287 test(r==KErrNone || r==KErrPathNotFound);
288 if (r==KErrPathNotFound)
290 r=TheFs.MkDirAll(aFileName);
292 r=file.Replace(TheFs,aFileName,0);
295 TCheckedUid checkedUid(aUidType);
296 TPtrC8 uidData((TUint8*)&checkedUid,sizeof(TCheckedUid));
297 r=file.Write(uidData);
299 r=file.Write(aFileContents);
304 GLDEF_C void MakeFile(const TDesC& aFileName,const TDesC8& aFileContents)
306 // Make a file and write something in it
311 TInt r=file.Replace(TheFs,aFileName,0);
312 if(r!=KErrNone && r!=KErrPathNotFound)
314 test.Printf(_L("ERROR: r=%d"),r);
317 test(r==KErrNone || r==KErrPathNotFound);
318 if (r==KErrPathNotFound)
320 r=TheFs.MkDirAll(aFileName);
322 r=file.Replace(TheFs,aFileName,0);
325 r=file.Write(aFileContents);
330 GLDEF_C void MakeFile(const TDesC& aFileName,TInt anAttributes)
332 // Make a file and write something in it
337 TInt r=file.Replace(TheFs,aFileName,0);
338 test(r==KErrNone || r==KErrPathNotFound);
339 if (r==KErrPathNotFound)
341 r=TheFs.MkDirAll(aFileName);
343 r=file.Replace(TheFs,aFileName,0);
347 r=TheFs.SetAtt(aFileName,anAttributes,0);
351 GLDEF_C void SetSessionPath(const TDesC& aPathName)
353 // Set the session path and update gSessionPath
357 TInt r=TheFs.SetSessionPath(aPathName);
359 r=TheFs.SessionPath(gSessionPath);
363 GLDEF_C void MakeFile(const TDesC& aFileName)
369 MakeFile(aFileName,_L8(""));
372 GLDEF_C void MakeDir(const TDesC& aDirName)
378 TInt r=TheFs.MkDirAll(aDirName);
379 test(r==KErrNone || r==KErrAlreadyExists);
382 GLDEF_C TInt CheckFileExists(const TDesC& aName,TInt aResult,TBool aCompRes/*=ETrue*/)
384 // Check aName exists
389 TInt r=TheFs.Entry(aName,entry);
391 if (aResult!=KErrNone)
393 TParsePtrC nameParse(aName);
394 TParsePtrC entryParse(entry.iName);
395 TBool nameMatch=(entryParse.Name()==nameParse.Name());
396 TBool extMatch=(entryParse.Ext()==nameParse.Ext()) || (entryParse.Ext().Length()<=1 && nameParse.Ext().Length()<=1);
397 test((nameMatch && extMatch)==aCompRes);
401 GLDEF_C void CheckFileContents(const TDesC& aName,const TDesC8& aContents)
403 // Check contents of file
408 TInt r=f.Open(TheFs,aName,EFileRead);
410 HBufC8* testBuf=HBufC8::NewL(aContents.Length());
412 TPtr8 bufPtr(testBuf->Des());
415 test(bufPtr==aContents);
418 test(bufPtr.Length()==0);
423 GLDEF_C void DeleteTestDirectory()
425 // Delete the leaf session path directory
429 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden"), 0, KEntryAttHidden);
430 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden\\HiddenFile"), 0, KEntryAttHidden);
431 TheFs.SetAtt(_L("\\F32-TST\\SCANTEST\\Left\\Dir3\\Dir4\\Hidden\\System"), 0, KEntryAttSystem);
432 test.Next(_L("Delete test directory"));
433 CFileMan* fMan=CFileMan::NewL(TheFs);
435 TInt r=TheFs.SessionPath(gSessionPath);
437 r=TheFs.CheckDisk(gSessionPath);
438 if (r!=KErrNone && r!=KErrNotSupported)
439 ReportCheckDiskFailure(r);
440 r=fMan->RmDir(gSessionPath);
445 GLDEF_C void CreateTestDirectory(const TDesC& aSessionPath)
447 // Create directory for test
450 TParsePtrC path(aSessionPath);
451 test(path.DrivePresent()==EFalse);
453 TInt r=TheFs.SetSessionPath(aSessionPath);
455 r=TheFs.SessionPath(gSessionPath);
457 r=TheFs.MkDirAll(gSessionPath);
458 test(r==KErrNone || r==KErrAlreadyExists);
461 GLDEF_C TInt CurrentDrive()
463 // Return the current drive number
468 TInt r=TheFs.CharToDrive(gSessionPath[0],driveNum);
473 GLDEF_C void Format(TInt aDrive)
475 // Format current drive
479 test.Next(_L("Format"));
480 TBuf<4> driveBuf=_L("?:\\");
481 driveBuf[0]=(TText)(aDrive+'A');
484 TInt r=format.Open(TheFs,driveBuf,EHighDensity,count);
488 TInt r=format.Next(count);
494 LOCAL_C void PushLotsL()
496 // Expand the cleanup stack
501 CleanupStack::PushL((CBase*)NULL);
502 CleanupStack::Pop(1000);
506 LOCAL_C void DoTests(TInt aDrive)
508 // Do testing on aDrive
512 gSessionPath=_L("?:\\F32-TST\\");
514 TInt r=TheFs.DriveToChar(aDrive,driveLetter);
516 gSessionPath[0]=(TText)driveLetter;
517 r=TheFs.SetSessionPath(gSessionPath);
520 // !!! Disable platform security tests until we get the new APIs
521 // if(User::Capability() & KCapabilityRoot)
522 // CheckMountLFFS(TheFs,driveLetter);
524 r=TheFs.MkDirAll(gSessionPath);
525 TheFs.ResourceCountMarkStart();
527 TRAP(r,CallTestsL());
530 TheFs.ResourceCountMarkEnd();
533 test.Printf(_L("Error: Leave %d\n"),r);
540 void ParseCommandArguments()
542 // Parse command line. Put the parameters into global array gArgv for
543 // use by the tests, strip out flags starting with / or - and interpret
544 // them to set debug flags.
547 LOCAL_D TBuf<0x100> cmd;
548 User::CommandLine(cmd);
550 TPtrC token=lex.NextToken();
551 TFileName thisfile=RProcess().FileName();
552 if (token.MatchF(thisfile)==0)
554 token.Set(lex.NextToken());
556 // set up parameter list (offset zero is the filename)
558 gArgV[gArgC++].Set(thisfile);
559 while (token.Length() != 0)
562 // strip out (and interpret) flags starting with - or /
563 if (ch == '-' || ch == '/')
565 for (TInt i = 1; i < token.Length(); i++)
567 switch (User::UpperCase(token[i]))
570 gDebugFlags |= KDLYFAST;
573 gDebugFlags |= KFSYS;
576 gDebugFlags |= KISO9660;
579 gDebugFlags |= KFLDR;
581 #ifdef __CONCURRENT_FILE_ACCESS__
583 gDebugFlags |= KTHRD;
587 gDebugFlags |= KNTFS;
590 gDebugFlags |= KFSERV;
593 gDebugFlags |= KLFFS;
596 gDebugFlags |= KDLYTRC;
602 gArgV[gArgC++].Set(token);
603 token.Set(lex.NextToken());
606 // set up drive to test
610 gDriveToTest = gArgV[1][0];
611 gDriveToTest.UpperCase();
616 GLDEF_C TInt E32Main()
618 CTrapCleanup* cleanup;
619 cleanup=CTrapCleanup::New();
620 TRAPD(r,PushLotsL());
624 test.Start(_L("Starting tests..."));
629 ParseCommandArguments(); //need this for drive letter to test
631 TheFs.SetAllocFailure(gAllocFailOn);
635 TheFs.SessionPath(sessionp);
638 privatedir = KPrivate;
640 TUid thisUID = RProcess().Identity();
641 privatedir.AppendFormat(_L("%08x"),thisUID.iUid);
642 privatedir.Append(_L("\\"));
644 test(privatedir == sessionp.Mid(2,sessionp.Length()-2));
646 test.Printf(_L("sp=%S"),&sessionp);
647 sessionp[0]=(TText)gDriveToTest;
648 test.Printf(_L("sp1=%S"),&sessionp);
651 r=TheFs.CharToDrive(gDriveToTest,theDrive);
654 // set up debug register
655 test.Printf(_L("debug register = 0x%X"), gDebugFlags);
656 TheFs.SetDebugRegister(gDebugFlags);
658 // actually do the tests!
661 // reset the debug register
662 TheFs.SetDebugRegister(0);
666 TTimeIntervalSeconds timeTakenC;
667 r=endTimeC.SecondsFrom(timerC,timeTakenC);
669 test.Printf(_L("Time taken for test = %d seconds\n"),timeTakenC);
670 TheFs.SetAllocFailure(gAllocFailOff);