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_format.cpp
18 #define __E32TEST_EXTENSION__
26 #include "f32_test_utils.h"
28 using namespace F32_Test_Utils;
31 GLREF_D TFileName gSessionPath;
34 void GenerateMediaChange()
42 TInt r=format.Open(TheFs,b,EHighDensity,count);
47 RTest test(_L("T_FORMAT"));
48 RSemaphore gSleepThread;
49 TRequestStatus gThreadLogon;
51 static TInt gDrive=-1;
52 static const TInt KSectorSize=512;
53 static const TInt KHeapSize=0x200;
55 enum TTestCode{ETest3,ETest5};
58 //-------------------------------------------------------------------
59 TInt DoFormatSteps(RFormat& aFormat, TInt& aFmtCnt)
65 nRes = aFormat.Next(aFmtCnt);
68 test.Printf(_L("RFormat::Next() failed! code:%d\n"), nRes);
76 static void WaitForMedia()
78 // Wait until the media change is serviced
84 TInt r=TheFs.MkDir(_L("\\"));
91 static TInt ThreadEntryPoint(TAny* aTestCode)
98 TInt ret=fs.Connect();
100 ret=fs.SetSessionPath(gSessionPath);
102 TTestCode testCode=*(TTestCode*)&aTestCode;
109 ret=format.Open(fs,gSessionPath,EQuickFormat,count);
112 ret = DoFormatSteps(format, count);
120 ret=format.Open(fs,gSessionPath,EFullFormat,count);
122 gSleepThread.Signal();
123 User::After(100000000);
132 //-------------------------------------------------------------------
133 static void CorruptCurrentDrive()
135 // Corrupt the current drive
138 test.Printf(_L("CorruptCurrentDrive() %c:"), 'A'+CurrentDrive());
141 TInt r=raw.Open(TheFs,CurrentDrive());
143 if (!Is_Lffs(TheFs, gDrive))
145 TBuf8<KSectorSize> zeroBuf(KSectorSize);
146 Mem::FillZ((TAny*)zeroBuf.Ptr(),zeroBuf.MaxSize());
148 //-- for FAT32 we need to corrupt a backup BOOT sector as well,
149 //-- otherwise it can be used and some tests will fail..
150 const TInt KMaxSectors = 25; //-- how many sectors to corrupt
151 for(TInt i=0; i<KMaxSectors; ++i)
153 r=raw.Write(i*KSectorSize, zeroBuf);
159 TBuf8<32> zeroBuf(32);
160 for (TInt j=0;j<32;++j)
161 zeroBuf[j]=(TUint8)j; //Not actuall zero buf for lffs
162 // For LFFS, the media may not exhibit a contiguous region of sufficient length
163 // to support a continuous sequence of writes. This is the case if the
164 // Control Mode Size is non-zero
165 TInt cntlModeSize=GetLFFSControlModeSize();
168 //test.Printf(_L("CorruptCurrentDrive() - Control mode size is zero\n"),r);
169 for (TInt writePos=0;writePos<0x20200;writePos+=32)
171 r=raw.Write(writePos,zeroBuf);
172 // The device driver most likely fails when writing a random
173 // buffer due to read back checks. Since we're writing
174 // aligned 32-byte blocks, we don't need to bother that much.
175 // The device driver writes the block but fails when reading
177 // test(r==KErrNone);
180 else if(cntlModeSize>0)
182 //test.Printf(_L("CorruptCurrentDrive() - Control mode = 0x%x\n"),r);
183 // For devices which have a non-zero control mode size, the writes may
184 // require segmentation.
190 while(bytesWritten < 0x20200)
192 TInt bufLeft = 32 - bufOffset; // 32 from size of zeroBuf
193 TInt spaceLeft = cntlModeSize - cmOffset;
194 TInt writeLen=(bufLeft>spaceLeft)? spaceLeft : bufLeft;
195 writeBuf.Set(&(zeroBuf[bufOffset]),writeLen);
196 TInt writePos = cmBase + cmOffset;
197 r=raw.Write(writePos,writeBuf);
198 bytesWritten += writeLen;
199 if(bufLeft < spaceLeft)
204 else if(bufLeft == spaceLeft)
208 cmBase += (2*cntlModeSize);
211 { // bufRemaining>spaceRemaining
212 bufOffset += spaceLeft;
214 cmBase += (2*cntlModeSize);
220 // Negative value (error code) returned from GetLFFSControlModeSize()
221 test.Printf(_L("CorruptCurrentDrive() - Control mode = %d (ERROR!) \n"),cntlModeSize);
229 //-------------------------------------------------------------------
236 test.Next(_L("Test EFullFormat"));
240 TInt r=format.Open(TheFs,gSessionPath,EFullFormat,count);
243 r = DoFormatSteps(format, count);
249 r=TheFs.Volume(volInfo);
252 if (volInfo.iSize-volInfo.iFree!=0)
254 test.Printf(_L("Memory 'in use' after a full format = %ld\n"),(volInfo.iSize-volInfo.iFree));
255 test.Printf(_L("volInfo.iSize = %ld\n"),volInfo.iSize);
256 test.Printf(_L("volInfo.iFree = %ld\n"),volInfo.iFree);
259 test.Next(_L("Test EQuickFormat"));
260 r=format.Open(TheFs,gSessionPath,EQuickFormat,count);
263 r = DoFormatSteps(format, count);
268 r=TheFs.Volume(volInfo);
271 if (volInfo.iSize-volInfo.iFree!=0)
273 test.Printf(_L("Memory 'in use' after a quick format = %ld\n"),(volInfo.iSize-volInfo.iFree));
274 test.Printf(_L("volInfo.iSize = %ld\n"),volInfo.iSize);
275 test.Printf(_L("volInfo.iFree = %ld\n"),volInfo.iFree);
281 //-------------------------------------------------------------------
284 // Test access controls
288 test.Next(_L("Test disk cannot be formatted while a file is open"));
290 TInt r=f.Replace(TheFs,_L("BLARGME.BLARG"),EFileStream);
295 r=format.Open(TheFs,gSessionPath,EFullFormat,count);
299 r=format.Open(TheFs,gSessionPath,EFullFormat,count);
303 CheckFileExists(_L("BLARGME.BLARG"),KErrNone);
306 //-------------------------------------------------------------------
313 test.Next(_L("Test successful format triggers notifier"));
314 MakeFile(_L("\\BLARG_BLARG_BLARG.BLG"));
315 TRequestStatus reqStat;
316 TheFs.NotifyChange(ENotifyEntry,reqStat);
318 RThread clientThread;
319 TInt r=clientThread.Create(_L("ClientThread"),ThreadEntryPoint,0x4000,KHeapSize,KHeapSize,(TAny*)ETest3);
320 test.Printf(_L("Created helper thread #1, res=%d\n"),r);
323 clientThread.Logon(gThreadLogon);
324 clientThread.Resume();
325 clientThread.Close();
327 User::WaitForRequest(reqStat);
328 test.Printf(_L("Notifier triggered #1, res=%d\n"),reqStat.Int());
330 User::WaitForRequest(gThreadLogon);
331 test.Printf(_L("Helper thread exited #1, res=%d\n"),gThreadLogon.Int());
333 CheckFileExists(_L("BLARG_BLARG_BLARG.BLG"),KErrNotFound);
334 MakeFile(_L("\\BLARG_BLARG_BLARG.BLG"));
336 TheFs.NotifyChange(ENotifyAll,reqStat);
337 r=clientThread.Create(_L("ClientThread"),ThreadEntryPoint,0x4000,KHeapSize,KHeapSize,(TAny*)ETest3);
338 test.Printf(_L("Created helper thread #2, res=%d\n"),r);
342 clientThread.Logon(gThreadLogon);
343 clientThread.Resume();
344 clientThread.Close();
346 User::WaitForRequest(reqStat);
347 test.Printf(_L("Notifier triggered #2, res=%d\n"),reqStat.Int());
349 User::WaitForRequest(gThreadLogon);
350 test.Printf(_L("Helper thread exited #2, res=%d\n"),gThreadLogon.Int());
352 CheckFileExists(_L("BLARG_BLARG_BLARG.BLG"),KErrNotFound);
355 //-------------------------------------------------------------------
358 // Test partially completed formats
361 test.Next(_L("Test partially completed formats"));
363 if(Is_Automounter(TheFs, gDrive))
364 {//-- if we have automounter FS installed, formatting of the corrupted media is not straightforward
365 //-- The automounter might not be able to decide which of the child file systems shall be used; This issue is covered in a specific test.
366 test.Printf(_L("This step is skipped for Automounter File System\n"));
370 MakeFile(_L("\\FORMAT\\DIR1\\BLARG_BLARG_BLARG.BLG"));
372 CorruptCurrentDrive();
374 test.Printf(_L("Formatting the drive...\n"));
377 TInt r=format.Open(TheFs,gSessionPath,EFullFormat,count);
383 r=dir.Open(TheFs,_L("\\*.*"),KEntryAttNormal);
385 r=format.Next(count);
390 CheckFileExists(_L("\\FORMAT\\DIR1\\BLARG_BLARG_BLARG.BLG"),KErrPathNotFound);
393 //-------------------------------------------------------------------
396 // Test panic formatting thread
400 test.Next(_L("Test panic formatting thread"));
402 if(Is_Automounter(TheFs, gDrive))
403 {//-- if we have automounter FS installed, formatting of the corrupted media is not straightforward
404 //-- The automounter might not be able to decide which of the child file systems shall be used; This issue is covered in a specific test.
405 test.Printf(_L("This step is skipped for Automounter File System\n"));
409 CorruptCurrentDrive();
410 if (!(IsTestingLFFS() && GetDriveLFFS()==EDriveC)) // ??? Remove after ER5U
412 // UserSvr::ForceRemountMedia(ERemovableMedia0); // Generate media change
413 GenerateMediaChange();
417 gSleepThread.CreateLocal(0);
418 RThread clientThread;
419 TInt r=clientThread.Create(_L("ClientThread"),ThreadEntryPoint,0x4000,KHeapSize,KHeapSize,(TAny*)ETest5);
420 test.Printf(_L("Created helper thread #1, res=%d\n"),r);
423 test.Printf(_L("Panicing formatting thread #1\n"));
424 clientThread.Resume();
426 test.Printf(_L("Panicing formatting thread #2\n"));
427 User::SetJustInTime(EFalse);
428 clientThread.Panic(_L("Panic formatting thread"),KErrGeneral);
429 User::SetJustInTime(ETrue);
430 User::After(200000); // to let panic take effect
431 test.Printf(_L("Panicing formatting thread #3\n"));
434 r=dir.Open(TheFs,_L("\\*.*"),KEntryAttNormal);
435 // if(IsTestingLFFS() && (r==KErrNone))
441 test(r==KErrCorrupt);
444 test.Printf(_L("Formatting the drive...\n"));
448 r=format.Open(TheFs,gSessionPath,EQuickFormat,count);
451 r = DoFormatSteps(format, count);
456 MakeFile(_L("BLARGOID.BLARG"));
457 CheckFileExists(_L("BLARGOID.BLARG"),KErrNone);
458 clientThread.Close();
459 gSleepThread.Close();
462 //-------------------------------------------------------------------
465 // Test ramdrive is shrunk after formatting
469 test.Next(_L("Test ramdrive shrinks after formatting"));
471 TInt r=TheFs.Volume(volInfo);
473 if ((volInfo.iDrive.iMediaAtt&KMediaAttVariableSize)==0)
476 TInt64 used=volInfo.iSize-volInfo.iFree;
478 r=f.Replace(TheFs,_L("BIGFILE.SIZE"),EFileRead|EFileWrite);
480 f.SetSize(0x100000); // 1MB
483 r=TheFs.Volume(volInfo);
485 TInt64 used2=volInfo.iSize-volInfo.iFree;
488 r=TheFs.Delete(_L("BIGFILE.SIZE"));
490 r=TheFs.Volume(volInfo);
492 used2=volInfo.iSize-volInfo.iFree;
495 r=f.Replace(TheFs,_L("BIGFILE.SIZE"),EFileRead|EFileWrite);
497 f.SetSize(0x100000); // 1MB
500 r=TheFs.Volume(volInfo);
502 used2=volInfo.iSize-volInfo.iFree;
507 r=format.Open(TheFs,gSessionPath,EQuickFormat,count);
510 r = DoFormatSteps(format, count);
515 r=TheFs.Volume(volInfo);
517 used2=volInfo.iSize-volInfo.iFree;
523 // Generate media change before formatting.
527 test.Next(_L("Generate Media change before formatting"));
529 if(Is_Automounter(TheFs, gDrive))
530 {//-- if we have automounter FS installed, formatting of the corrupted media is not straightforward
531 //-- The automounter might not be able to decide which of the child file systems shall be used; This issue is covered in a specific test.
532 test.Printf(_L("This step is skipped for Automounter File System\n"));
537 TInt r=TheFs.Volume(volInfo);
540 if (volInfo.iDrive.iMediaAtt&KMediaAttVariableSize)
541 return; // Don't bother on internal disk
543 if (Is_Lffs(TheFs, gDrive))
544 return; // Don't bother on LFFS
546 CorruptCurrentDrive();
547 // UserSvr::ForceRemountMedia(ERemovableMedia0); // Generate media change
548 GenerateMediaChange();
552 r=format.Open(TheFs,gSessionPath,EQuickFormat,count);
555 r = DoFormatSteps(format, count);
561 //-------------------------------------------------------------------
564 // Test incomplete format
568 test.Next(_L("Test incomplete format"));
570 if(Is_Automounter(TheFs, gDrive))
571 {//-- if we have automounter FS installed, formatting of the corrupted media is not straightforward
572 //-- The automounter might not be able to decide which of the child file systems shall be used; This issue is covered in a specific test.
573 test.Printf(_L("This step is skipped for Automounter File System\n"));
577 CorruptCurrentDrive();
579 if (!(IsTestingLFFS() && GetDriveLFFS()==EDriveC)) // ??? Remove after ER5U
581 // UserSvr::ForceRemountMedia(ERemovableMedia0); // Generate media change
582 GenerateMediaChange();
587 TInt r=TheFs.Volume(volInfo);
588 // test(r==KErrCorrupt);
591 r=format.Open(TheFs,gSessionPath,EQuickFormat,count);
592 r=TheFs.Volume(volInfo);
594 r=format.Next(count);
596 TDriveList driveList;
597 r=TheFs.DriveList(driveList);
600 if(gDrive == EDriveC)
602 r=TheFs.Volume(volInfo, gDrive);
607 r=TheFs.Volume(volInfo,EDriveC);
610 r=TheFs.Volume(volInfo,gDrive);
613 r=TheFs.Volume(volInfo,gDrive);
619 Format(CurrentDrive());
623 //-------------------------------------------------------------------
625 Test an API that allows force media formatting with the files or other objects opened on the volume
627 void TestFormat_ForceDismount()
629 test.Next(_L("Test format with forced media dismounting"));
631 if(Is_Lffs(TheFs, gDrive))
632 {//-- forced FS dismounting with files/directories opened damages LFFS structure for unknown reason.
633 //-- this is a problem of LFFS, anyway, it is not supported.
634 test.Next(_L("This test can't be performed on LFFS, Skipping."));
640 TUint fmtMode = EQuickFormat;
643 drivePath.Format(_L("%C:\\"), gDrive+'A');
650 const TInt KBufLen = 128*K1KiloByte;
651 nRes = buf8.CreateMax(KBufLen);
654 _LIT(KFname, "\\file1");
658 //---------------------------------------------------------------------------------
659 //-- 1.1 open a file, try to format in normal mode; this shall fail with KErrInUse
660 test.Printf(_L("Test normal format with normal opened objects\n"));
661 nRes = file1.Replace(TheFs, KFname, EFileWrite);
664 fmtMode = EQuickFormat;
666 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
667 test(nRes == KErrInUse);
671 nRes = file1.Write(buf8);
676 //-- 1.2 open a directory, try to format in normal mode; this shall fail with KErrInUse
677 nRes = dir.Open(TheFs,_L("\\*.*"),KEntryAttNormal);
680 fmtMode = EQuickFormat;
681 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
682 test(nRes == KErrInUse);
688 //---------------------------------------------------------------------------------
689 //-- 2.1 forced quick formatting
690 test.Printf(_L("Test forced quick formatting\n"));
691 nRes = file1.Replace(TheFs, KFname, EFileWrite); //-- open a file
694 nRes = dir.Open(TheFs,_L("\\*.*"),KEntryAttNormal); //-- open a directory
697 //-- this will mark the current Mount as "Dismounted" and will instantiate another CMountCB for formatting
698 fmtMode = EQuickFormat | EForceFormat;
700 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
703 nRes = DoFormatSteps(format, fmtCnt);
709 nRes=TheFs.CheckDisk(gSessionPath);
710 test(nRes==KErrNone||nRes==KErrNotSupported);
713 nRes = file1.Write(buf8);
714 test(nRes == KErrDisMounted);
715 file1.Close(); //-- this will make the previously "Dismounted" mount die.
719 //---------------------------------------------------------------------------------
720 //-- 2.2 forced full formatting
721 test.Printf(_L("Test forced full formatting\n"));
722 nRes = file1.Replace(TheFs, KFname, EFileWrite);
725 //-- this will mark the current Mount as "Dismounted" and will instantiate another CMountCB for formatting
726 fmtMode = EFullFormat | EForceFormat;
728 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
731 nRes = DoFormatSteps(format, fmtCnt);
736 nRes=TheFs.CheckDisk(gSessionPath);
737 test(nRes==KErrNone||nRes==KErrNotSupported);
740 nRes = file1.Write(buf8);
741 test(nRes == KErrDisMounted);
742 file1.Close(); //-- this will make the previously "Dismounted" mount die.
744 //---------------------------------------------------------------------------------
745 //-- 3. check that forced formatting will complete NotifyDismount
746 test.Printf(_L("Test forced formatting completes NotifyDismount\n"));
747 TRequestStatus stat1;
749 nRes = file1.Replace(TheFs, KFname, EFileWrite);
752 TheFs.NotifyDismount(gDrive, stat1, EFsDismountRegisterClient);
753 test(stat1.Int() == KRequestPending);
755 fmtMode = EQuickFormat;
756 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
757 test(nRes == KErrInUse);
760 test(stat1.Int() == KRequestPending);
762 fmtMode = EQuickFormat | EForceFormat;
763 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
767 User::WaitForRequest(stat1);
768 test(stat1.Int() == KErrNone);
771 nRes = file1.Write(buf8);
772 test(nRes == KErrDisMounted);
775 //---------------------------------------------------------------------------------
776 //-- 4.1 check that forced formatting will succeed with dirty file cache
777 test.Printf(_L("Test forced formatting will succeed with dirty file cache\n"));
779 nRes = file1.Replace(TheFs, KFname, EFileWrite | EFileWriteBuffered); //-- enable write caching
782 buf8.SetLength(KBufLen);
783 nRes = file1.Write(buf8); //-- this will hopefully get via file write cache
786 fmtMode = EQuickFormat | EForceFormat;
787 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
791 nRes = file1.Write(buf8);
792 test(nRes == KErrDisMounted);
796 //---------------------------------------------------------------------------------
798 test.Printf(_L("Test forced formatting with disk access objects opened\n"));
800 //-- 5.1 check that forced formatting will fail when there are "disk access" objects opened RFormat
803 nRes = format1.Open(TheFs, drivePath, fmtMode, fmtCnt);
804 test(nRes == KErrNone);
806 fmtMode = EQuickFormat | EForceFormat;
807 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
808 test(nRes == KErrInUse);
813 //-- 5.1 check that forced formatting will fail when there are "disk access" objects opened RRawDisk
815 nRes = rawDisk.Open(TheFs, gDrive);
816 test(nRes == KErrNone);
818 fmtMode = EQuickFormat | EForceFormat;
819 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
820 test(nRes == KErrInUse);
826 //---------------------------------------------------------------------------------
827 //-- 6. Try forced formatting with clamped files, this shall fail with KErrInuse
828 test.Printf(_L("Test forced formatting and clamps on the volume\n"));
830 nRes = file1.Replace(TheFs, KFname, EFileWrite);
833 buf8.SetLength(KBufLen);
834 nRes = file1.Write(buf8); //-- this will hopefully get via file write cache
841 nRes=handle.Clamp(file1);
844 test.Printf(_L("file clamps on this drive are not supported\n"));
848 fmtMode = EQuickFormat | EForceFormat;
849 nRes = format.Open(TheFs, drivePath, fmtMode, fmtCnt);
850 test(nRes == KErrInUse);
864 // Call tests that may leave
869 r = TheFs.CharToDrive(gDriveToTest, gDrive);
872 //-- set up console output
873 F32_Test_Utils::SetConsole(test.Console());
875 TInt nRes=TheFs.CharToDrive(gDriveToTest, gDrive);
876 test(nRes==KErrNone);
878 PrintDrvInfo(TheFs, gDrive);
880 if(Is_Win32(TheFs, gDrive))
881 return; //-- emulator drive c:
884 SetSessionPath(_L("\\"));
894 TestFormat_ForceDismount();
896 r=TheFs.CheckDisk(gSessionPath);
897 test(r==KErrNone||r==KErrNotSupported);