Update contrib.
1 // Copyright (c) 1996-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\fat32\t_compat32.cpp
18 #define __E32TEST_EXTENSION__
26 #include "fat_utils.h"
27 using namespace Fat_Test_Utils;
29 RTest test(_L("T_COMPAT32"));
31 static RRawDisk TheDisk;
32 static TFatBootSector gBootSector;
35 static void QuickFormat()
37 FormatFatDrive(TheFs, CurrentDrive(), ETrue);
40 static void ReadBootSector(TFatBootSector& aBootSector)
43 TInt nRes = ReadBootSector(TheFs, CurrentDrive(), KBootSectorNum<<KDefaultSectorLog2, aBootSector);
44 test(nRes == KErrNone);
46 if(!aBootSector.IsValid())
48 test.Printf(_L("Wrong bootsector! Dump:\n"));
49 aBootSector.PrintDebugInfo();
55 static void GetBootInfo()
58 ReadBootSector(gBootSector);
63 EUpper, // Test directory entries with 8.3 uppercase (no VFAT entries expected)
64 ELower, // Test directory entries with 8.3 lowercase ( VFAT entries expected)
65 EMixed // Test directory entries with 8.3 mixed ( VFAT entries expected)
70 Fiddles with root directory entries.
71 Creates a file, if it has 1 VFAT and 1 DOS dir. entries, places an illegal lower case
72 symbol to the DOS entry, fixing VFAT name checksums
74 static void DoFiddleWithFileNames(TNameCase aCase)
76 TFileName fileName = _L("\\WORD");
77 TBool expectVfatEntry = EFalse;
85 fileName = _L("\\word");
86 expectVfatEntry = ETrue;
90 fileName = _L("\\WoRd");
91 expectVfatEntry = ETrue;
100 TInt r=file.Create(TheFs,fileName,EFileRead);
103 // Assume this file is the first entry in the root directory
106 r=TheDisk.Open(TheFs,CurrentDrive());
109 //-- read 1st dir. entry it can be FAT or VFat , depending on the filename
110 const TInt posEntry1=gBootSector.RootDirStartSector() << KDefaultSectorLog2; //-- dir entry1 position
112 TFatDirEntry fatEntry1;
113 TPtr8 ptrEntry1((TUint8*)&fatEntry1,sizeof(TFatDirEntry));
115 test(TheDisk.Read(posEntry1, ptrEntry1)==KErrNone);
117 if(!fatEntry1.IsVFatEntry())
118 {//-- we expected FAT entry, everything is OK
119 test(!expectVfatEntry);
122 {//-- we have 2 FAT entries, 1st is VFat, 2nd is DOS.
123 //-- put lower case letters into DOS entry( not compliant with FAT specs), correct VFat entries checksums,
124 //-- in this case the system shall correctly deal with the file, using long names.
126 test(expectVfatEntry);
127 test(fatEntry1.iData[0] == 0x41); //-- must have only 2 entries
129 //-- read DOS entry now
130 TFatDirEntry fatEntry2;
131 TPtr8 ptrEntry2((TUint8*)&fatEntry2,sizeof(TFatDirEntry));
132 const TInt posEntry2 = posEntry1 + sizeof(TFatDirEntry); //-- dir entry2 position
134 test(TheDisk.Read(posEntry2, ptrEntry2)==KErrNone);
136 //-- ensure that the name and checksum are correct
137 test(fatEntry1.iData[13] == CalculateShortNameCheckSum(fatEntry2.Name()));
138 test(fatEntry2.Name()==_L8("WORD "));
140 //-- put lower case symbol to the DOS entry and fix the checksum
141 _LIT8(KBadDosName, "Word ");
142 fatEntry2.SetName(KBadDosName);
143 fatEntry1.iData[13] = CalculateShortNameCheckSum(fatEntry2.Name());
145 //-- write data to the disk
146 test(TheDisk.Write(posEntry1, ptrEntry1)==KErrNone);
147 test(TheDisk.Write(posEntry2, ptrEntry2)==KErrNone);
156 // Replace a 8.3 filename with upper and lower case letters which is, actually out of FAT specs.
157 // I.e. VFAT entries are valid, but DOS entry has a lower case symbol, which is wrong.
159 LOCAL_C void Test1(TNameCase aCase)
161 test.Next(_L("Replace a file with a wrong DOS entry"));
164 //-- N.B. This shall be the before any dir. entries creation in the root directory
165 //-- because it directly accesses the directory's 1st file
166 DoFiddleWithFileNames(aCase);
171 r=file.Replace(TheFs,_L("\\FILE.TMP"),EFileRead);
173 r=file.Write(_L8("Hello World"));
176 r=TheFs.Replace(_L("\\File.tmp"),_L("\\Word"));
180 r=TheFs.GetDir(_L("\\*.*"),KEntryAttMaskSupported,ESortNone,entryCount);
182 TInt count=entryCount->Count();
190 // Renaming a 8.3 filename with upper and lower case letters which is, actually out of FAT specs.
191 // I.e. VFAT entries are valid, but DOS entry has a lower case symbol, which is wrong.
193 LOCAL_C void Test2(TNameCase aCase)
195 test.Next(_L("Rename a file with a wrong DOS entry"));
200 //-- N.B. This shall be the before any dir. entries creation in the root dir
201 //-- because it directly accesses the directory's 1st file
202 DoFiddleWithFileNames(aCase);
204 r=file.Create(TheFs,_L("\\TEST"),EFileRead);
208 r=TheFs.Rename(_L("\\TEST"),_L("\\Word"));
209 test(r==KErrAlreadyExists);
210 r=TheFs.Delete(_L("\\TEST"));
214 r=TheFs.GetDir(_L("\\*.*"),KEntryAttMaskSupported,ESortNone,entryCount);
216 TInt count=entryCount->Count();
222 //---------------------------------------------
223 //! @SYMTestCaseID PBASE-T_COMPAT32-0686
225 //! @SYMREQ DEF115314
226 //! @SYMTestCaseDesc Test character '`' (0x60) is recognized as a legal char for short file names.
227 //! @SYMTestActions Creates a file named "\x60\x60\x60.TXT", checks only DOS entry is created for
228 //! it and its short name equals "```.TXT" instead of "___.TXT"
229 //! @SYMTestExpectedResults The operation completes with error code KErrNone;
230 //! @SYMTestPriority High
231 //! @SYMTestStatus Implemented
232 //---------------------------------------------
235 test.Next(_L("Test DEF115314: TTG:<`(0x60) code cannot be used as valid Short File Name>"));
241 fn.Format(_L("%c:\\\x60\x60\x60.TXT"), (TUint8)gDriveToTest);
243 r = TheFs.Delete(fn);
244 test(r==KErrNone || r==KErrNotFound);
246 r = file.Create(TheFs, fn, EFileRead);
250 r=TheDisk.Open(TheFs,CurrentDrive());
253 //-- read 1st dir. it should be DOS Entry
254 const TInt posEntry1=gBootSector.RootDirStartSector() << KDefaultSectorLog2; //-- dir entry1 position
255 TFatDirEntry fatEntry1;
256 TPtr8 ptrEntry1((TUint8*)&fatEntry1,sizeof(TFatDirEntry));
257 test(TheDisk.Read(posEntry1, ptrEntry1)==KErrNone);
259 test(!fatEntry1.IsVFatEntry());
263 r = TheFs.GetShortName(fn, sn);
265 test(sn.Compare(_L("```.TXT"))==0);
267 r = TheFs.Delete(fn);
271 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
272 _LIT(KTestLocale, "t_tlocl_cp932.dll");
273 _LIT(KTestUnicodeFileName, "\\\x65B0\x6587\x4EF6");
274 #endif //_DEBUG || _DEBUG_RELEASE
276 //---------------------------------------------
277 //! @SYMTestCaseID PBASE-T_COMPAT32-0685
279 //! @SYMREQ DEF113633
280 //! @SYMTestCaseDesc Test FAT volume creates VFat entries for short unicode named files
281 //! @SYMTestActions Enables FatUtilityFunctions. Loads cp932 codepage dll. Create a file
282 //! named as "\x65B0\x6587\x4EF6", checks both a VFat entry and a DOS
283 //! entry have been created for the file.
284 //! @SYMTestExpectedResults The operation completes with error code KErrNone;
285 //! @SYMTestPriority High
286 //! @SYMTestStatus Implemented
287 //---------------------------------------------
290 test.Next(_L("Test DEF113633 - FAT should create VFat entries for unicode character contained file names"));
291 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
297 r = TheFs.CharToDrive(gDriveToTest,drvNum);
300 // turn on FatUtilityFunctions
301 r = TheFs.ControlIo(drvNum, KControlIoEnableFatUtilityFunctions);
304 // load cp932 codepage dll
305 r = UserSvr::ChangeLocale(KTestLocale);
308 // create file "\x65B0\x6587\x4EF6", check DOS entry & VFat entry
309 r = file.Create(TheFs, KTestUnicodeFileName, EFileRead);
313 r=TheDisk.Open(TheFs,CurrentDrive());
316 //-- read 1st dir. it should be VFat
317 // const TInt posEntry1=gRootDirStart; //-- dir entry1 position
318 const TInt posEntry1=gBootSector.RootDirStartSector() << KDefaultSectorLog2; //-- dir entry1 position
319 TFatDirEntry fatEntry1;
320 TPtr8 ptrEntry1((TUint8*)&fatEntry1,sizeof(TFatDirEntry));
322 test(TheDisk.Read(posEntry1, ptrEntry1)==KErrNone);
324 test(fatEntry1.IsVFatEntry());
326 test(fatEntry1.iData[0] == 0x41); //-- must have only 2 entries
328 //-- read DOS entry now
329 TFatDirEntry fatEntry2;
330 TPtr8 ptrEntry2((TUint8*)&fatEntry2,sizeof(TFatDirEntry));
331 const TInt posEntry2 = posEntry1 + sizeof(TFatDirEntry); //-- dir entry2 position
333 test(TheDisk.Read(posEntry2, ptrEntry2)==KErrNone);
335 //-- ensure that the name and checksum are correct
336 test(!fatEntry2.IsVFatEntry());
337 test(fatEntry1.iData[13] == CalculateShortNameCheckSum(fatEntry2.Name()));
341 r = TheFs.Delete(KTestUnicodeFileName);
344 // turn off FatUtilityFunctions
345 r = TheFs.ControlIo(drvNum, KControlIoDisableFatUtilityFunctions);
349 test.Printf(_L("Test only runs on DEBUG builds, see test logs of debug builds for details."));
350 #endif // _DEBUG) || _DEBUG_RELEASE
356 //---------------------------------------------
357 // If the parent directory of a directory is the root directory,
358 // the '..' entry should point to start cluster 0 in any case
359 //---------------------------------------------
360 void TestPDEF116912()
362 test.Next(_L("Test PDEF116912 - Check that '..' parent cluster address is 0 after renaming\n"));
365 test(KErrNone == TheFs.CharToDrive(gDriveToTest, drvNum));
367 if(!Is_Fat32(TheFs, drvNum))
369 _LIT(KMessage, "Test only applicable to FAT32 file systems. Skipping.\n");
370 test.Printf(KMessage);
376 _LIT(KDirA, "\\dirA\\");
377 _LIT(KDirB, "\\dirB\\");
379 test(KErrNone == TheFs.MkDir(KDirA));
380 test(KErrNone == TheFs.Rename(KDirA, KDirB));
382 test(gBootSector.IsValid());
383 TInt mediaPosition = gBootSector.RootDirStartSector() * gBootSector.BytesPerSector();
385 TFatDirEntry dirEntry;
386 TPtr8 ptrEntry((TUint8*) &dirEntry,sizeof(TFatDirEntry));
388 _LIT8(KDirBMatchPattern, "DIRB *");
389 _LIT8(KDotDotMatchPattern, ".. *");
391 const TInt KMaxEntriesToSearch = (gBootSector.SectorsPerCluster() * gBootSector.BytesPerSector()) / KSizeOfFatDirEntry;
392 test(KErrNone == TheDisk.Open(TheFs, drvNum));
394 for(TInt c = 0; c < KMaxEntriesToSearch; c++)
396 test(KErrNone == TheDisk.Read(mediaPosition, ptrEntry));
398 if(KErrNotFound == ptrEntry.Match(KDirBMatchPattern))
401 mediaPosition += sizeof(TFatDirEntry);
405 // found, locate '..' entry
406 test(dirEntry.StartCluster() >= KFatFirstSearchCluser);
407 mediaPosition = gBootSector.FirstDataSector();
408 mediaPosition += (dirEntry.StartCluster() - KFatFirstSearchCluser) * gBootSector.SectorsPerCluster();
409 mediaPosition *= gBootSector.BytesPerSector();
410 mediaPosition += KSizeOfFatDirEntry; // '..' is always the 2nd entry
412 test(KErrNone == TheDisk.Read(mediaPosition, ptrEntry));
414 test(KErrNotFound != ptrEntry.Match(KDotDotMatchPattern));
415 test(dirEntry.StartCluster() == 0);
422 // dirB entry not found - test failed
427 //---------------------------------------------
429 Test replacing files by theis short names
431 void TestReplaceByShortName()
433 test.Next(_L("Test replacing files using short names\n"));
436 _LIT(KLongName1, "abcdefghi.txt");
437 _LIT(KShortName1, "ABCDEF~1.TXT");
438 const TInt KFile1Sz = 100;
440 _LIT(KLongName2, "abcdefghij.txt");
441 _LIT(KShortName2, "ABCDEF~2.TXT");
442 const TInt KFile2Sz = 200;
449 TheFs.SetSessionPath(_L("\\"));
451 nRes = CreateCheckableStuffedFile(TheFs, KLongName1, KFile1Sz);
454 nRes = TheFs.GetShortName(KLongName1, fn);
455 test(nRes == KErrNone && fn == KShortName1); //-- just check short name generation
457 nRes =CreateCheckableStuffedFile(TheFs, KLongName2, KFile2Sz);
460 nRes = TheFs.GetShortName(KLongName2, fn);
461 test(nRes == KErrNone && fn == KShortName2); //-- just check short name generation
463 //-- try to replace the file with itself using its short name alias
464 //-- nothing shall happen and the file must remain the same
465 nRes = TheFs.Replace(KLongName1, KShortName1);
466 test(nRes == KErrNone);
468 nRes = TheFs.Entry(KLongName1, entry);
469 test(nRes == KErrNone);
470 test(entry.iSize == KFile1Sz);
472 nRes = TheFs.Entry(KShortName1, entry);
473 test(nRes == KErrNone);
474 test(entry.iSize == KFile1Sz);
477 nRes = TheFs.Replace(KShortName1, KLongName1);
478 test(nRes == KErrNone);
480 nRes = TheFs.Entry(KLongName1, entry);
481 test(nRes == KErrNone);
482 test(entry.iSize == KFile1Sz);
484 nRes = TheFs.Entry(KShortName1, entry);
485 test(nRes == KErrNone);
486 test(entry.iSize == KFile1Sz);
488 nRes = VerifyCheckableFile(TheFs, KLongName1);
489 test(nRes == KErrNone);
491 nRes = VerifyCheckableFile(TheFs, KShortName1);
492 test(nRes == KErrNone);
495 //-- replace "abcdefghi.txt" by "ABCDEF~2.TXT" which is the alias for "abcdefghij.txt"
496 //-- expected: contents and all attributes of the "abcdefghij.txt" is replaced with "abcdefghi.txt"
497 //-- "abcdefghi.txt" entries gets deleted.
499 nRes = TheFs.Replace(KLongName1, KShortName2);
500 test(nRes == KErrNone);
502 //User::After(5*K1Sec);
504 nRes = VerifyCheckableFile(TheFs, KLongName2);
505 test(nRes == KErrNone);
507 nRes = VerifyCheckableFile(TheFs, KShortName2);
508 test(nRes == KErrNone);
512 nRes = TheFs.Entry(KLongName2, entry);
513 test(nRes == KErrNone);
514 test(entry.iSize == KFile1Sz);
516 nRes = TheFs.Entry(KShortName2, entry);
517 test(nRes == KErrNone);
518 test(entry.iSize == KFile1Sz && entry.iName == KLongName2);
524 GLDEF_C void CallTestsL()
526 // Call tests that may leave
531 TInt r=TheFs.CharToDrive(gDriveToTest,drvNum);
534 if (!Is_Fat(TheFs,drvNum))
536 test.Printf(_L("CallTestsL: Skipped: Requires FAT filesystem to run.\n"));
541 //-- set up console output
542 SetConsole(test.Console());
544 //-- print drive information
545 PrintDrvInfo(TheFs, drvNum);
549 Test1(EUpper); // Test directory entries with 8.3 uppercase (no VFAT entries expected)
550 Test1(ELower); // Test directory entries with 8.3 lowercase ( VFAT entries expected)
551 Test1(EMixed); // Test directory entries with 8.3 mixed ( VFAT entries expected)
553 Test2(EUpper); // Test directory entries with 8.3 uppercase (no VFAT entries expected)
554 Test2(ELower); // Test directory entries with 8.3 lowercase ( VFAT entries expected)
555 Test2(EMixed); // Test directory entries with 8.3 mixed ( VFAT entries expected)
561 TestReplaceByShortName();