author | Mike Kinghan <mikek@symbian.org> |
Thu, 25 Nov 2010 14:35:45 +0000 | |
branch | GCC_SURGE |
changeset 305 | 1ba12ef4ef89 |
parent 109 | b3a1d9898418 |
child 257 | 3e88ff8f41d5 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2005-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". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// f32test\server\t_fatcorrupt.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
//! @SYMTestCaseID FSBASE-CR-JHAS-68YPX7 |
|
19 |
//! @SYMTestType CT |
|
20 |
//! @SYMREQ CR JHAS-68YPX7 |
|
21 |
//! @SYMTestCaseDesc Test functionality of handling corrupt data on disk |
|
22 |
//! @SYMTestStatus Implemented |
|
23 |
//! @SYMTestActions Use fat test extension to test read/write/format |
|
24 |
//! @SYMTestExpectedResults Read bad sector fail, others pass |
|
25 |
//! @SYMTestPriority Medium |
|
26 |
//! @SYMAuthor Ying Shi |
|
27 |
//! @SYMCreationDate 20/05/2005 |
|
28 |
//! @See EFat and EFat32 components |
|
29 |
//! @file f32test\server\t_fatcorrupt.cpp |
|
30 |
||
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
31 |
#define __E32TEST_EXTENSION__ |
0 | 32 |
#include <f32file.h> |
33 |
#include <e32test.h> |
|
34 |
#include <e32math.h> |
|
35 |
||
36 |
#include "t_server.h" |
|
37 |
#include "t_fatext.h" |
|
38 |
||
39 |
using namespace Fat_Test_Utils; |
|
40 |
||
41 |
GLDEF_D RTest test(_L("T_FATCORRUPT")); |
|
42 |
||
43 |
static TInt gDrive; |
|
44 |
static TBuf16<2> gDriveName(2); |
|
45 |
static TFileName gFileSystem; |
|
46 |
static TFullName gExtName; |
|
47 |
static TBool gExtExists=EFalse; |
|
48 |
static TFatBootSector BootSector; |
|
49 |
||
50 |
static TInt gBadList[3]; |
|
51 |
||
52 |
_LIT(KExtName,"FATTEST"); |
|
53 |
||
54 |
||
55 |
void RestoreEnv(); |
|
56 |
||
57 |
/** |
|
58 |
Do format steps and print progress |
|
59 |
||
60 |
@param aFormat specifies the format object |
|
61 |
@param aStepsCnt format steps counter from RFormat::Open |
|
62 |
||
63 |
@return format completion error code |
|
64 |
*/ |
|
65 |
static TInt DoFormatSteps(RFormat& aFormat, TInt aStepsCnt) |
|
66 |
{ |
|
67 |
TInt nRes = KErrNone; |
|
68 |
TInt prevCnt = aStepsCnt; |
|
69 |
||
70 |
while (aStepsCnt && nRes == KErrNone) |
|
71 |
{ |
|
72 |
if(prevCnt != aStepsCnt) |
|
73 |
{ |
|
74 |
test.Printf(_L(".")); |
|
75 |
prevCnt = aStepsCnt; |
|
76 |
} |
|
77 |
||
78 |
nRes = aFormat.Next(aStepsCnt); |
|
79 |
} |
|
80 |
||
81 |
test.Printf(_L("\n\r")); |
|
82 |
||
83 |
return nRes; |
|
84 |
} |
|
85 |
||
86 |
||
87 |
#define TESTL(con) TestL(con,r,__LINE__) |
|
88 |
||
89 |
void TestL(TBool aCondition, TInt aError, TInt aLine = 0) |
|
90 |
{ |
|
91 |
if (!aCondition) |
|
92 |
{ |
|
93 |
RDebug::Printf("Test fail at line: %d with error code %d",aLine, aError); |
|
94 |
RestoreEnv(); |
|
95 |
} |
|
96 |
test(aCondition); |
|
97 |
} |
|
98 |
||
99 |
//------------------------------------------------------------- |
|
100 |
||
101 |
void RestoreEnv() |
|
102 |
{ |
|
103 |
test.Printf(_L("RestoreEnv()\n")); |
|
104 |
||
105 |
TInt r = TheFs.DismountExtension(KExtName, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
test_KErrNone(r); |
0 | 107 |
r = TheFs.RemoveExtension(KExtName); |
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
test_KErrNone(r); |
0 | 109 |
|
110 |
if(gExtExists) // remount existing secondary extension |
|
111 |
{ |
|
112 |
test(TheFs.MountExtension(gExtName, gDrive)==KErrNone); |
|
113 |
} |
|
114 |
||
115 |
||
116 |
//-- We need a full format, because quick format preserves bad sectors marked in FAT |
|
117 |
r = FormatFatDrive(TheFs, gDrive, EFalse); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
118 |
test_KErrNone(r); |
0 | 119 |
|
120 |
} |
|
121 |
||
122 |
//------------------------------------------------------------- |
|
123 |
||
124 |
// Remount drive with FAT test extension |
|
125 |
TBool PrepareMount() |
|
126 |
{ |
|
127 |
test.Printf(_L("PrepareMountL(), drive:%d \n"),gDrive); |
|
128 |
||
129 |
TInt r = TheFs.AddExtension(KExtName); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
test_Value(r, r == KErrNone || r==KErrAlreadyExists); |
0 | 131 |
r = TheFs.ExtensionName(gExtName,gDrive,0); |
132 |
if (r == KErrNone) // an extension already exists -> dismount it |
|
133 |
{ |
|
134 |
test.Printf(_L("Drive %d has extension, attempt dismounting it\n"),gDrive); |
|
135 |
r=TheFs.DismountExtension(gExtName,gDrive); |
|
136 |
if(r==KErrAccessDenied) // primary extension |
|
137 |
{ |
|
138 |
test.Printf(_L("Drive %d has primary extension, skip\n"),gDrive); |
|
139 |
test(TheFs.RemoveExtension(KExtName) == KErrNone); |
|
140 |
return EFalse; |
|
141 |
} |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
test_KErrNone(r); |
0 | 143 |
gExtExists=ETrue; |
144 |
} |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
test_Value(r, r == KErrNone || r==KErrNotFound); |
0 | 146 |
r = TheFs.MountExtension(KExtName, gDrive); |
147 |
if (r != KErrNone) |
|
148 |
test(TheFs.RemoveExtension(KExtName) == KErrNone); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
149 |
test_KErrNone(r); |
0 | 150 |
return ETrue; |
151 |
} |
|
152 |
||
153 |
//------------------------------------------------------------- |
|
154 |
||
155 |
void MarkClustersL() |
|
156 |
{ |
|
157 |
TInt size = sizeof(gBadList) / sizeof(TInt); |
|
158 |
for (TInt i=0; i<size; i++) |
|
159 |
{ |
|
160 |
TInt r = TheFs.ControlIo(gDrive,CFatTestProxyDrive::EMark,(TAny*)gBadList[i],NULL); |
|
161 |
TESTL(r == KErrNone); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
//------------------------------------------------------------- |
|
166 |
||
167 |
/** |
|
168 |
Create 3 empty files on the volume |
|
169 |
*/ |
|
170 |
void CreateFiles() |
|
171 |
{ |
|
172 |
TInt r; |
|
173 |
RFile file; |
|
174 |
TBuf<10> fileName01; |
|
175 |
fileName01.Append(_L("?:\\file01")); |
|
176 |
fileName01[0] = (TUint16)gDriveToTest; |
|
177 |
r = file.Create(TheFs, fileName01, EFileWrite); |
|
178 |
TESTL(r==KErrNone||r==KErrAlreadyExists); |
|
179 |
file.Close(); |
|
180 |
||
181 |
TBuf<10> fileName02; |
|
182 |
fileName02.Append(_L("?:\\file02")); |
|
183 |
fileName02[0] = (TUint16)gDriveToTest; |
|
184 |
r = file.Create(TheFs, fileName02, EFileWrite); |
|
185 |
TESTL(r==KErrNone||r==KErrAlreadyExists); |
|
186 |
file.Close(); |
|
187 |
||
188 |
TBuf<10> fileName03; |
|
189 |
fileName03.Append(_L("?:\\file03")); |
|
190 |
fileName03[0] = (TUint16)gDriveToTest; |
|
191 |
r = file.Create(TheFs, fileName03, EFileWrite); |
|
192 |
TESTL(r==KErrNone||r==KErrAlreadyExists); |
|
193 |
file.Close(); |
|
194 |
} |
|
195 |
||
196 |
//------------------------------------------------------------- |
|
197 |
||
198 |
/** |
|
199 |
Read/Write to the file with damaged clusters |
|
200 |
*/ |
|
201 |
void DoTestReadWriteL() |
|
202 |
{ |
|
203 |
CreateFiles(); |
|
204 |
||
205 |
// Get disk mount information |
|
206 |
TPckgBuf<TInt> dataPositionBuf; |
|
207 |
TPckgBuf<TInt> sectorsPerClusterBuf; |
|
208 |
||
209 |
TInt r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EGetDataPosition,dataPositionBuf); |
|
210 |
TESTL(r==KErrNone); |
|
211 |
||
212 |
//-- media position of the 1st data sector on the volume. |
|
213 |
//-- for FAT32 this is the 2nd cluster of the data ares, because 1st belongs to the root dir. |
|
214 |
const TInt dataPosition = dataPositionBuf(); |
|
215 |
||
216 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::ESectorsPerCluster,sectorsPerClusterBuf); |
|
217 |
TESTL(r==KErrNone); |
|
218 |
||
219 |
const TInt sectorsPerCluster = sectorsPerClusterBuf(); //-- FAT sectors per cluster |
|
220 |
||
221 |
||
222 |
||
223 |
// Calculate file data sectors |
|
224 |
// For FAT32, first data sector falls under root directory, and current FAT32 |
|
225 |
// file system implementation does not take care of handling bad sectors in |
|
226 |
// root directory so changing start sector to be the start sector of the file, |
|
227 |
// instead of first data sector. |
|
228 |
TInt f01_StartSector; |
|
229 |
if(Is_Fat32(TheFs,gDrive)) |
|
230 |
{ |
|
231 |
f01_StartSector = (dataPosition >> KDefaultSectorLog2); |
|
232 |
test.Printf(_L("FAT32, dataPosition: %d, sec:%d, spc:%d\n"), dataPosition, f01_StartSector, sectorsPerCluster); |
|
233 |
} |
|
234 |
else |
|
235 |
{ |
|
236 |
f01_StartSector = (dataPosition >> KDefaultSectorLog2); |
|
237 |
test.Printf(_L("dataPosition: %d, sec:%d, spc:%d\n"), dataPosition, f01_StartSector, sectorsPerCluster); |
|
238 |
} |
|
239 |
||
240 |
//TInt f01end = f01beg + sectorsPerCluster - 1; |
|
241 |
//TInt f02beg = f01end + 1; |
|
242 |
//TInt f02end = f02beg + sectorsPerCluster - 1; |
|
243 |
//TInt f03beg = f02end + 1; |
|
244 |
//TInt f03end = f03beg + sectorsPerCluster - 1; |
|
245 |
||
246 |
test.Next(_L("Test file write")); |
|
247 |
||
248 |
//-- mark the sector that file01 starts with as bad |
|
249 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EMark,(TAny*)(f01_StartSector),NULL); |
|
250 |
TESTL(r==KErrNone); |
|
251 |
||
252 |
TBuf<10> fileName01; |
|
253 |
fileName01.Append(_L("?:\\file01")); |
|
254 |
fileName01[0] = (TUint16)gDriveToTest; |
|
255 |
||
256 |
RFile file; |
|
257 |
||
258 |
//-- write to the file with bad sector |
|
259 |
r = file.Open(TheFs, fileName01, EFileWrite); |
|
260 |
TESTL(r==KErrNone); |
|
261 |
||
262 |
TInt size = 2 * sectorsPerCluster * KDefaultSectorSize; //-- 2 clusters |
|
263 |
RBuf8 writeBuf01; |
|
264 |
||
265 |
r = writeBuf01.CreateMax(size); |
|
266 |
TESTL(r == KErrNone); |
|
267 |
||
268 |
r = file.Write(writeBuf01, size); |
|
269 |
TESTL(r==KErrNone); |
|
270 |
||
271 |
file.Close(); |
|
272 |
||
273 |
test.Next(_L("Test file read")); |
|
274 |
||
275 |
//-- unmark bas sector |
|
276 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EUnmark,(TAny*)(f01_StartSector),NULL); |
|
277 |
TESTL(r==KErrNone); |
|
278 |
||
279 |
//-- mark the 2nd cluster in the file as bad |
|
280 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EMark,(TAny*)(f01_StartSector+sectorsPerCluster),NULL); |
|
281 |
TESTL(r==KErrNone); |
|
282 |
||
283 |
r = file.Open(TheFs, fileName01, EFileRead); |
|
284 |
TESTL(r==KErrNone); |
|
285 |
||
286 |
//-- read the file from "corrupted" media |
|
287 |
r = file.Read(writeBuf01, size); |
|
288 |
TESTL(r==KErrCorrupt); //-- the file must be corrupted |
|
289 |
||
290 |
file.Close(); |
|
291 |
||
292 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EUnmarkAll, NULL, NULL); |
|
293 |
TESTL(r==KErrNone); |
|
294 |
||
295 |
writeBuf01.Close(); |
|
296 |
||
297 |
} |
|
298 |
||
299 |
||
300 |
//------------------------------------------------------------------------------------------------------- |
|
301 |
||
302 |
/** |
|
303 |
Suggest FAT volume metrics by volume size in sectors. Pay attention to the KMaxSectorsPerCluster constant. |
|
304 |
||
305 |
@param aVolSizeInSectors FAT volume size in sectors |
|
306 |
@param apSectPerCluster on return here will be suggested number of sectors per cluster |
|
307 |
@param apRootDirEntries on return here will be suggested max. number of root dir. entries. |
|
308 |
*/ |
|
309 |
void SuggestVolMetrics(TUint aVolSizeInSectors, TUint16 *apSectPerCluster, TUint *apRootDirEntries=NULL) |
|
310 |
{ |
|
311 |
const TUint16 KMaxSectorsPerCluster = 32; //-- maximal number sectors per cluster to use |
|
312 |
TUint16 sectorsPerCluster; |
|
313 |
TUint rootDirEntries; |
|
314 |
||
315 |
//-- suggest Sector per Cluster value, most suitable for the given volume size. (taken from CFatFormatCB::InitFormatDataForFixedSizeDiskUser(...)) |
|
316 |
if (aVolSizeInSectors < 4096) // < 2MB |
|
317 |
{ |
|
318 |
sectorsPerCluster = 1; |
|
319 |
rootDirEntries = 128; |
|
320 |
} |
|
321 |
else if (aVolSizeInSectors < 8192) // < 4MB |
|
322 |
{ |
|
323 |
sectorsPerCluster = Min(KMaxSectorsPerCluster, 2); |
|
324 |
rootDirEntries = 256; |
|
325 |
} |
|
326 |
else if (aVolSizeInSectors < 32768) // < 16MB |
|
327 |
{ |
|
328 |
sectorsPerCluster = Min(KMaxSectorsPerCluster, 4); |
|
329 |
rootDirEntries = 512; |
|
330 |
} |
|
331 |
else if (aVolSizeInSectors < 1048576) // < 512MB |
|
332 |
{ |
|
333 |
sectorsPerCluster = Min(KMaxSectorsPerCluster, 8); |
|
334 |
rootDirEntries = 512; |
|
335 |
} |
|
336 |
else // FAT32 |
|
337 |
{ |
|
338 |
sectorsPerCluster = KMaxSectorsPerCluster; |
|
339 |
rootDirEntries = 512; |
|
340 |
} |
|
341 |
||
342 |
//--------------------------------------- |
|
343 |
if(apSectPerCluster) |
|
344 |
*apSectPerCluster = sectorsPerCluster; |
|
345 |
||
346 |
if(apRootDirEntries) |
|
347 |
*apRootDirEntries = rootDirEntries; |
|
348 |
||
349 |
} |
|
350 |
||
351 |
//------------------------------------------------------------- |
|
352 |
||
353 |
/** |
|
354 |
Test user-specific FAT format. FAT type 12,16 or 32 will depend on disk size. |
|
355 |
@param aQuick if not 0 a quick format will be performed. |
|
356 |
*/ |
|
357 |
void DoTestUserFormatL(TBool aQuick, TBool aMark) |
|
358 |
{ |
|
359 |
const TUint32 KKiloByte = 1024; |
|
360 |
||
361 |
TInt r; |
|
362 |
||
363 |
//-- obtain current FAT volume metrics |
|
364 |
TVolumeInfo volInfo; |
|
365 |
r = TheFs.Volume(volInfo, gDrive); |
|
366 |
TESTL(r == KErrNone); |
|
367 |
||
368 |
const TInt64 volSize = volInfo.iSize; //-- volume size in bytes |
|
369 |
const TUint volSizeInSectors = TUint(volSize >> KDefaultSectorLog2); //-- sectors per volume |
|
370 |
||
371 |
TUint16 sectorsPerCluster=0; |
|
372 |
TUint rootDirEntries = 0; |
|
373 |
TUint fatType = 0; |
|
374 |
||
375 |
//-- roughly suggest FAT type for this volume, if FAT type is incorrect, RFormat will fail. |
|
376 |
//-- suggest Sector per Cluster value, most suitable for this volume size (taken from CFatFormatCB::InitFormatDataForFixedSizeDiskUser(...)) |
|
377 |
SuggestVolMetrics(volSizeInSectors, §orsPerCluster, &rootDirEntries); |
|
378 |
||
379 |
const TUint totVolClusterCnt = volSizeInSectors / sectorsPerCluster; //-- total clusters per this volume |
|
380 |
||
381 |
//-- doesn't count here FAT size, RootDir size and reserved sectors. For rough estimations only, otherwise |
|
382 |
//-- it will be far too complicated. |
|
383 |
const TUint dataClusterCnt = totVolClusterCnt; |
|
384 |
||
385 |
//-- magic. see FAT specs for details. |
|
386 |
if(dataClusterCnt < 4085) |
|
387 |
fatType = TLDFormatInfo::EFB12; |
|
388 |
else if(dataClusterCnt < 65525) |
|
389 |
fatType = TLDFormatInfo::EFB16; |
|
390 |
else |
|
391 |
fatType = TLDFormatInfo::EFB32; |
|
392 |
||
393 |
test.Printf(_L("Formatting drive %C:, size=%uKB, SecPerClust=%u, FAT%d\n"), ('A'+gDrive), (TUint32)(volSize/KKiloByte), sectorsPerCluster, fatType); |
|
394 |
||
395 |
//-- formatting the volume |
|
396 |
TInt formatCnt; |
|
397 |
TLDFormatInfo formatInfo; |
|
398 |
||
399 |
formatInfo.iSectorsPerCluster = sectorsPerCluster; |
|
400 |
formatInfo.iFATBits = (TLDFormatInfo::TFATBits)fatType; |
|
401 |
||
402 |
TSpecialFormatInfoBuf formatInfoBuf(formatInfo); |
|
403 |
RFormat formatUser; |
|
404 |
||
405 |
if (aQuick) |
|
406 |
{ |
|
407 |
r = formatUser.Open(TheFs, gDriveName, ESpecialFormat|EQuickFormat, formatCnt, formatInfoBuf); |
|
408 |
TESTL(r == KErrNone); |
|
409 |
} |
|
410 |
else |
|
411 |
{ |
|
412 |
r = formatUser.Open(TheFs, gDriveName, ESpecialFormat, formatCnt, formatInfoBuf); |
|
413 |
TESTL(r == KErrNone); |
|
414 |
} |
|
415 |
||
416 |
if(aMark) |
|
417 |
{ |
|
418 |
MarkClustersL(); |
|
419 |
} |
|
420 |
||
421 |
r = DoFormatSteps(formatUser, formatCnt); |
|
422 |
TESTL(r == KErrNone || r == KErrNotSupported); |
|
423 |
||
424 |
formatUser.Close(); |
|
425 |
||
426 |
if (r == KErrNotSupported) |
|
427 |
{ |
|
428 |
test.Printf(_L("Media does not support ESpecialFormat")); |
|
429 |
return; |
|
430 |
} |
|
431 |
||
432 |
//-- format has finished, Check format validity |
|
433 |
TPckgBuf<TInt> sbuf; |
|
434 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::ESectorsPerCluster, sbuf); |
|
435 |
TESTL(r==KErrNone); |
|
436 |
const TInt resSectorsPerCluster = sbuf(); |
|
437 |
||
438 |
TPckgBuf<TInt64> sizebuf; |
|
439 |
r = TheFs.ControlIo(gDrive, CTestProxyDrive::EDiskSize, sizebuf); |
|
440 |
TESTL(r==KErrNone); |
|
441 |
const TInt64 resVolSize = sizebuf(); |
|
442 |
||
443 |
//-- Check fat type |
|
444 |
TPckgBuf<TInt> tbuf; |
|
445 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EFatType, tbuf); |
|
446 |
TESTL(r==KErrNone); |
|
447 |
const TUint resFatType = tbuf(); |
|
448 |
||
449 |
test.Printf(_L("Format result:, size=%uKB, SecPerClust=%u, FAT%d\n"), (TUint32)(resVolSize/KKiloByte), resSectorsPerCluster, resFatType); |
|
450 |
||
451 |
TESTL(resFatType == fatType); |
|
452 |
||
453 |
//-- user-defined format CFatFormatCB::InitFormatDataForFixedSizeDiskUser(TInt aDiskSizeInSectors) |
|
454 |
//-- can decide decides how many SectorsPerCluster will be on the volume. As the result, requested and resulted values can be different. |
|
455 |
TESTL(resSectorsPerCluster == sectorsPerCluster); |
|
456 |
||
457 |
//-- check whole drive |
|
458 |
test.Next(_L("Check Disk")); |
|
459 |
r = TheFs.CheckDisk(gDriveName); |
|
460 |
TESTL(r==KErrNone); |
|
461 |
} |
|
462 |
||
463 |
//------------------------------------------------------------- |
|
464 |
||
465 |
/** Randomly generate several sector numbers in FAT data area, which will be marked as bad. */ |
|
466 |
static void CreateBadList() |
|
467 |
{ |
|
468 |
static TInt64 rndSeed = Math::Random(); |
|
469 |
const TInt size = sizeof(gBadList) / sizeof(TInt); |
|
470 |
||
471 |
//const TUint32 dataStartSector = BootSector.FirstFreeSector(); //-- 1st sector number of the data area (after the root directory for fat12/16) |
|
472 |
||
473 |
//-- 1st sector number of the data area. |
|
474 |
//-- for fat12/16 it will be 1st sector after the root directory |
|
475 |
//-- for fat32 it will be the 1st sector of the second cluster after the root directory. At present we don't handle the 1st bad cluster of the root dir. |
|
476 |
TUint32 dataStartSector; |
|
477 |
||
478 |
if(BootSector.FatType() == EFat32) |
|
479 |
{ |
|
480 |
dataStartSector = BootSector.RootDirStartSector() + 1*BootSector.SectorsPerCluster(); |
|
481 |
} |
|
482 |
else |
|
483 |
{ |
|
484 |
dataStartSector = BootSector.FirstDataSector(); |
|
485 |
} |
|
486 |
||
487 |
||
488 |
test.Printf(_L("data start sector:%d, total sectors:%d\n"), dataStartSector, BootSector.VolumeTotalSectorNumber()); |
|
489 |
||
490 |
//-- randomly generate bad sectors numbers in the FAT volume data area |
|
491 |
for(TInt i = 0; i<size; ++i) |
|
492 |
{ |
|
493 |
const TUint32 rand = ((TUint32)Math::Rand(rndSeed)) % (BootSector.VolumeTotalSectorNumber() - dataStartSector); |
|
494 |
const TUint32 sectNum = dataStartSector + rand; |
|
495 |
||
496 |
gBadList[i] = sectNum; |
|
497 |
test.Printf(_L("Value of gBadList[%d] is %d\n"), i, gBadList[i]); |
|
498 |
} |
|
499 |
||
500 |
} |
|
501 |
||
502 |
||
503 |
//------------------------------------------------------------- |
|
504 |
||
505 |
/** |
|
506 |
Read and validate the boot sector |
|
507 |
*/ |
|
508 |
static void ReadBootSector() |
|
509 |
{ |
|
510 |
test.Printf(_L("Reading Boot sector...\n")); |
|
511 |
||
512 |
TInt nRes = ReadBootSector(TheFs, gDrive, KBootSectorNum << KDefaultSectorLog2, BootSector); |
|
513 |
test(nRes == KErrNone); |
|
514 |
||
515 |
if(!BootSector.IsValid()) |
|
516 |
{ |
|
517 |
test.Printf(_L("The boot sector is invalid! dump:\n")); |
|
518 |
BootSector.PrintDebugInfo(); |
|
519 |
test(0); |
|
520 |
} |
|
521 |
||
522 |
} |
|
523 |
||
524 |
// |
|
525 |
// Write version number to disk using raw disk write |
|
526 |
// For FAT 32 the version number is written to the backup boot sector as well. |
|
527 |
// |
|
528 |
void WriteVersionNumber() |
|
529 |
{ |
|
530 |
//-- boot sector version number is already corrupt in ChangeVersionNumberAndMountL() |
|
531 |
test.Printf(_L("writing boot sector version number.\n")); |
|
532 |
||
533 |
TInt nRes; |
|
534 |
||
535 |
//-- 1. corrupt a backup boot sector if it is on the volume. This will prevent |
|
536 |
//-- using a valid backup boot sector instead of corrupted main one. |
|
537 |
//-- we need to do it before corrupting the main one, because RawWrite causes volume remount. |
|
538 |
TFatBootSector bs1; |
|
539 |
const TUint32 KBkBootSecPos = KBkBootSectorNum << KDefaultSectorLog2; |
|
540 |
||
541 |
nRes = ReadBootSector(TheFs, gDrive, KBkBootSecPos, bs1); |
|
542 |
if(nRes == KErrNone && bs1.IsValid()) |
|
543 |
{//-- write corrupted backup boot sector |
|
544 |
nRes = WriteBootSector(TheFs, gDrive, KBkBootSecPos, BootSector); |
|
545 |
test(nRes == KErrNone); |
|
546 |
} |
|
547 |
||
548 |
//-- write main boot sector from the pos 0 |
|
549 |
nRes = WriteBootSector(TheFs, gDrive, KBootSectorNum << KDefaultSectorLog2, BootSector); |
|
550 |
test(nRes == KErrNone); |
|
551 |
} |
|
552 |
||
553 |
// |
|
554 |
// Root Cluster Number update test |
|
555 |
// |
|
556 |
void DoTestRootClusterUpdate() |
|
557 |
{ |
|
558 |
test.Next(_L("Performing fat32 root cluster number update check\n")); |
|
559 |
||
560 |
//Mark cluster 2 as bad by raw access. |
|
561 |
RRawDisk rawDisk; |
|
562 |
||
563 |
TInt r=rawDisk.Open(TheFs,gSessionPath[0]-'A'); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
564 |
test_KErrNone(r); |
0 | 565 |
|
566 |
//Mark Cluster 2 & 3 as bad |
|
567 |
const TInt fatStartPos = BootSector.FirstFatSector() * BootSector.BytesPerSector(); |
|
568 |
TInt64 pos = 8 + fatStartPos; |
|
569 |
||
570 |
TBuf8<4> data(4); |
|
571 |
data[0] = 0xF7; |
|
572 |
data[1] = 0xFF; |
|
573 |
data[2] = 0xFF; |
|
574 |
data[3] = 0x0F; |
|
575 |
||
576 |
r=rawDisk.Write(pos, data); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
577 |
test_KErrNone(r); |
0 | 578 |
|
579 |
pos += 4; |
|
580 |
r = rawDisk.Write(pos, data); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
581 |
test_KErrNone(r); |
0 | 582 |
|
583 |
rawDisk.Close(); |
|
584 |
||
585 |
//-- quick format the drive |
|
586 |
r = FormatFatDrive(TheFs, gDrive, ETrue); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
587 |
test_KErrNone(r); |
0 | 588 |
|
589 |
const TUint oldClusterNum = BootSector.RootClusterNum(); |
|
590 |
ReadBootSector(); |
|
591 |
||
592 |
test.Printf(_L("Old Rool clNum:%d, new:%d\n"), oldClusterNum, BootSector.RootClusterNum()); |
|
593 |
||
594 |
test(BootSector.RootClusterNum() != oldClusterNum); |
|
595 |
||
596 |
} |
|
597 |
||
598 |
// |
|
599 |
//Make version number invalid and try to mount |
|
600 |
// |
|
601 |
void ChangeVersionNumberAndMountL() |
|
602 |
{ |
|
603 |
test.Printf(_L("Change fat32 version number and remount\n")); |
|
604 |
||
605 |
TFullName name; |
|
606 |
||
607 |
BootSector.SetVersionNumber(0x707); //-- corrupt Version number in the boot sector |
|
608 |
||
609 |
WriteVersionNumber(); |
|
610 |
||
611 |
TInt r = TheFs.FileSystemName(name, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
612 |
test_KErrNone(r); |
0 | 613 |
|
614 |
r = DismountFileSystem(TheFs, name, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
615 |
test_KErrNone(r); |
0 | 616 |
|
617 |
r = MountFileSystem(TheFs, name, gDrive); |
|
618 |
||
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
619 |
test_Value(r, r == KErrCorrupt); |
0 | 620 |
|
621 |
} |
|
622 |
||
623 |
||
624 |
// |
|
625 |
// Test version number |
|
626 |
// |
|
627 |
void DoTestVersionNumber() |
|
628 |
{ |
|
629 |
test.Next(_L("Performing fat32 version number check\n")); |
|
630 |
TFullName name; |
|
631 |
TInt count; |
|
632 |
||
633 |
ReadBootSector(); |
|
634 |
ChangeVersionNumberAndMountL(); |
|
635 |
||
636 |
// Test quick format |
|
637 |
RFormat formatQuick; |
|
638 |
TInt r = formatQuick.Open(TheFs, gDriveName, EQuickFormat, count); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
639 |
test_KErrNone(r); |
0 | 640 |
|
641 |
r = DoFormatSteps(formatQuick, count); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
642 |
test_KErrNone(r); |
0 | 643 |
|
644 |
formatQuick.Close(); |
|
645 |
||
646 |
ReadBootSector(); |
|
647 |
r = TheFs.FileSystemName(name, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
648 |
test_KErrNone(r); |
0 | 649 |
|
650 |
r = DismountFileSystem(TheFs, name, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
651 |
test_KErrNone(r); |
0 | 652 |
|
653 |
r = MountFileSystem(TheFs, name, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
654 |
test_KErrNone(r); |
0 | 655 |
|
656 |
} |
|
657 |
||
658 |
// |
|
659 |
// Test format disk with bad clusters |
|
660 |
// |
|
661 |
void DoTestFormatL() |
|
662 |
{ |
|
663 |
test.Next(_L("Test full format\n")); |
|
664 |
||
665 |
// Test full format |
|
666 |
TInt count; |
|
667 |
TInt r = KErrNone; |
|
668 |
||
669 |
RFormat formatFull; |
|
670 |
r = formatFull.Open(TheFs, gDriveName, EFullFormat, count); |
|
671 |
TESTL(r == KErrNone); |
|
672 |
||
673 |
// Insert bad clusters, based on the boot sector info obtained by the initial format |
|
674 |
MarkClustersL(); |
|
675 |
||
676 |
r = DoFormatSteps(formatFull, count); |
|
677 |
TESTL(r == KErrNone); |
|
678 |
||
679 |
formatFull.Close(); |
|
680 |
||
681 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EUnmarkAll, NULL, NULL); |
|
682 |
TESTL(r==KErrNone); |
|
683 |
||
684 |
test.Next(_L("Test quick format")); |
|
685 |
||
686 |
// Test quick format |
|
687 |
RFormat formatQuick; |
|
688 |
r = formatQuick.Open(TheFs, gDriveName, EQuickFormat, count); |
|
689 |
TESTL(r == KErrNone); |
|
690 |
||
691 |
r = DoFormatSteps(formatQuick, count); |
|
692 |
TESTL(r == KErrNone); |
|
693 |
||
694 |
formatQuick.Close(); |
|
695 |
||
696 |
test.Next(_L("Test full format")); |
|
697 |
||
698 |
// Test full format |
|
699 |
RFormat formatFull2; |
|
700 |
r = formatFull2.Open(TheFs, gDriveName, EFullFormat, count); |
|
701 |
TESTL(r == KErrNone); |
|
702 |
MarkClustersL(); |
|
703 |
||
704 |
r = DoFormatSteps(formatFull2, count); |
|
705 |
TESTL(r == KErrNone); |
|
706 |
||
707 |
formatFull2.Close(); |
|
708 |
||
709 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EUnmarkAll, NULL, NULL); |
|
710 |
TESTL(r==KErrNone); |
|
711 |
||
712 |
test.Next(_L("Test user specific format")); |
|
713 |
||
714 |
// Test user specific format |
|
715 |
DoTestUserFormatL(EFalse, EFalse); |
|
716 |
ReadBootSector(); |
|
717 |
CreateBadList(); |
|
718 |
DoTestUserFormatL(EFalse, ETrue); |
|
719 |
||
720 |
r = TheFs.ControlIo(gDrive, CFatTestProxyDrive::EUnmarkAll, NULL, NULL); |
|
721 |
TESTL(r==KErrNone); |
|
722 |
||
723 |
test.Next(_L("Test user specific quick format")); |
|
724 |
||
725 |
// Test user specific quick format |
|
726 |
DoTestUserFormatL(ETrue, EFalse); |
|
727 |
} |
|
728 |
||
729 |
void DoTestL() |
|
730 |
{ |
|
731 |
// Make sure the card is formatted, then extract boot sector info |
|
732 |
||
733 |
TInt count; |
|
734 |
RFormat formatFull; |
|
735 |
||
736 |
//Mini SD cards works properly only with ESpecialFormat. Fix for Defect DEF091659 |
|
737 |
TInt r = formatFull.Open(TheFs, gDriveName, ESpecialFormat, count); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
738 |
test_KErrNone(r); |
0 | 739 |
|
740 |
r = DoFormatSteps(formatFull, count); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
741 |
test_KErrNone(r); |
0 | 742 |
|
743 |
formatFull.Close(); |
|
744 |
||
745 |
ReadBootSector(); |
|
746 |
//...and create the bad sector list |
|
747 |
CreateBadList(); |
|
748 |
||
749 |
DoTestReadWriteL(); |
|
750 |
||
751 |
DoTestFormatL(); |
|
752 |
} |
|
753 |
||
754 |
||
755 |
//------------------------------------------------------------- |
|
756 |
||
757 |
void CallTestsL() |
|
758 |
{ |
|
759 |
#if !(defined(_USE_CONTROLIO) || defined(_DEBUG) || defined(_DEBUG_RELEASE)) |
|
760 |
test.Printf(_L("Tests only run in debug mode")); |
|
761 |
return; |
|
762 |
#else |
|
763 |
// Only test FAT filesystem |
|
764 |
TInt r; |
|
765 |
r = TheFs.CharToDrive(gDriveToTest, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
766 |
test_KErrNone(r); |
0 | 767 |
gDriveName[0] = (TText)gDriveToTest; |
768 |
gDriveName[1] = ':'; |
|
769 |
||
770 |
//-- set up console output |
|
771 |
Fat_Test_Utils::SetConsole(test.Console()); |
|
772 |
||
773 |
//-- print drive information |
|
774 |
PrintDrvInfo(TheFs, gDrive); |
|
775 |
||
776 |
if (!Is_Fat(TheFs, gDrive)) |
|
777 |
{ |
|
778 |
test.Printf(_L("Drive %d is not FAT file system\n"),gDrive); |
|
779 |
return; |
|
780 |
} |
|
781 |
||
782 |
// No need for variable size disk |
|
783 |
TDriveInfo info; |
|
784 |
r = TheFs.Drive(info, gDrive); |
|
109
b3a1d9898418
Revision: 201019
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
785 |
test_KErrNone(r); |
0 | 786 |
if (info.iMediaAtt & KMediaAttVariableSize) |
787 |
{ |
|
788 |
test.Printf(_L("Drive %d is variable-size disk, skip\n"),gDrive); |
|
789 |
return; |
|
790 |
} |
|
791 |
||
792 |
//-- perform FAT32 specific tests |
|
793 |
||
794 |
if(Is_Fat32(TheFs, gDrive) && !Is_Automounter(TheFs, gDrive)) |
|
795 |
{//-- these tests tend to corrupt the volume; Automounter can't cope with it easily |
|
796 |
DoTestVersionNumber(); |
|
797 |
DoTestRootClusterUpdate(); |
|
798 |
} |
|
799 |
||
800 |
//-- install test extension etc. |
|
801 |
if(!PrepareMount()) |
|
802 |
return; |
|
803 |
||
804 |
//-- perform the rest of the tests |
|
805 |
DoTestL(); |
|
806 |
||
807 |
//-- dismount the extension and format the drive |
|
808 |
//-- there is an issue here: if one of the tests fail, the extension won't be dismounted, which |
|
809 |
//-- will cause total crash. |
|
810 |
RestoreEnv(); |
|
811 |
#endif |
|
812 |
} |
|
813 |
||
814 |
||
815 |
||
816 |