First public contribution.
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\server\b_open.cpp
18 #define __E32TEST_EXTENSION__
28 #define WIN32_LEAN_AND_MEAN
29 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
31 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
34 GLDEF_D RTest test(_L("B_OPEN"));
36 LOCAL_D const TInt KMaxFiles=200;
37 LOCAL_D TBuf<32> nameBuf[KMaxFiles];
38 LOCAL_D TBuf<0x100> nameBuf1;
39 LOCAL_D RFile chan[KMaxFiles];
41 LOCAL_D TFileName fBuf;
42 LOCAL_D TVolumeInfo vInfo;
43 LOCAL_D TInt LeaveMemFree;
45 LOCAL_C void testOpenFiles()
47 // Open files till memory is full
48 // Write, seek, read, seteof, close, check and delete all files.
52 TChar currentDrive=gSessionPath[0];
54 TInt r=TheFs.CharToDrive(currentDrive,driveNum);
63 DWORD sectorsPerCluster;
67 BOOL b=GetDiskFreeSpaceA("C:\\",§orsPerCluster,&bytesPerSector,&freeClusters,&sizeClusters);
69 totalRam=sizeClusters*sectorsPerCluster*bytesPerSector;
71 TMemoryInfoV1Buf memInfoBuf;
72 UserHal::MemoryInfo(memInfoBuf);
73 totalRam=memInfoBuf().iTotalRamInBytes;
75 test.Printf(_L("Open %u\n"),i);
76 nameBuf[i].Format(_L("B_OPEN test file %d"),i);
77 r=chan[i].Replace(TheFs,nameBuf[i],EFileStream|EFileWrite);
80 r=chan[i].Write(_L8("SomeText"));
81 if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
86 test(TheFs.Delete(nameBuf[i])==KErrNone);
95 test.Printf(_L("ERROR: RFile::Replace returned %d\n"),r);
99 r=TheFs.Volume(vInfo);
101 if (driveNum==EDriveC)
102 test.Printf(_L("VInfo size=0x%x free=0x%x TotalRam = 0x%x\n"),vInfo.iSize,vInfo.iFree,totalRam);
103 test(vInfo.iFree<=vInfo.iSize);
105 // r=TheFs.Volume(vInfo,(driveNum==EDriveC) ? EDriveD : EDriveC);
107 // test(vInfo.iFree<=vInfo.iSize);
114 test.Panic(_L("Too many files opened"));
118 test.Printf(_L("Created %d extra files\n"),i);
119 const TInt n_files=i;
120 r=TheFs.Volume(vInfo);
122 if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
124 test(vInfo.iSize<=totalRam);
125 test(vInfo.iFree<=totalRam);
127 test(vInfo.iFree<=vInfo.iSize);
129 test.Next(_L("SetSize to each file"));
130 for (i=0;i<n_files;i++)
132 r=chan[i].SetSize(0);
136 test.Next(_L("Write to each file"));
138 for (i=0;i<n_files;i++)
140 test.Printf(_L("Write %u\n"),i);
141 numBuf.Format(_L8("%d"),i);
142 r=chan[i].Write(0,numBuf);
146 test.Next(_L("Seek on each file"));
147 for (i=0;i<n_files;i++)
149 test.Printf(_L("Seeking %u\n"),i);
151 r=chan[i].Seek(ESeekStart,pos);
155 test.Next(_L("Read from each file"));
157 for (i=0;i<n_files;i++)
159 test.Printf(_L("Read %u\n"),i);
160 r=chan[i].Read(checkBuf,3);
162 numBuf.Format(_L8("%d"),i);
163 test(numBuf==checkBuf);
166 test.Next(_L("Set size of each file"));
167 for (i=0;i<n_files;i++)
169 test.Printf(_L("Set size %u\n"),i);
170 r=chan[i].SetSize(i);
174 r=TheFs.Volume(vInfo);
176 if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
178 test(vInfo.iSize<=totalRam);
179 test(vInfo.iFree<=totalRam);
181 test(vInfo.iFree<=vInfo.iSize);
183 test.Next(_L("Close each file"));
184 for (i=0;i<n_files;i++)
186 test.Printf(_L("Close %u\n"),i);
190 test.Next(_L("Open each file"));
193 for (n_files_open=0; n_files_open < n_files; n_files_open++)
195 r = chan[n_files_open].Open(TheFs,nameBuf[n_files_open],EFileRead|EFileStream);
196 test.Printf(_L("Open(%d) ret %d\n"), n_files_open, r);
199 if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
206 test.Next(_L("Check size of each file"));
207 for (i=0;i<n_files_open;i++)
209 test.Printf(_L("Check %u\n"),i);
211 TInt r=chan[i].Size(size);
212 test.Printf(_L("size of file %u is %d ret %d\n"),i,size, r);
217 test.Next(_L("Close each file"));
218 for (i=0;i<n_files_open;i++)
220 test.Printf(_L("Close %u\n"),i);
224 test.Next(_L("Delete files"));
225 for (i=0;i<n_files;i++)
227 test.Printf(_L("Delete %u\n"),i);
228 r=TheFs.Delete(nameBuf[i]);
235 LOCAL_C void InitTest()
237 // Create a large file
241 TBool fileSizeTruncated;
245 fileSizeTruncated = EFalse;
246 nameBuf1.Format(_L("\\Hooge Test File for B_OPEN %d"), fileNumber++);
248 TInt r=chan1.Replace(TheFs,nameBuf1,EFileStream|EFileWrite);
250 r=TheFs.Volume(vInfo);
253 LeaveMemFree = 0x400; // ???
254 if (vInfo.iFree>LeaveMemFree)
255 size=vInfo.iFree-LeaveMemFree;
259 const TInt KMaxFileSize = 0x40000000;
261 // test as 64 bit numbers in case size is very large (eg. enough that TInt is
262 // not large enough to hold it)
263 TInt64 KMaxFileSize64 = MAKE_TINT64(0, KMaxFileSize);
265 test.Printf(_L("Free space available = %08x:%08x\n"), I64HIGH(size), I64LOW(size));
266 if (size > KMaxFileSize64)
269 test.Printf(_L("Truncated to %d to avoid current FAT FSY file size limit !!!\n"), size);
270 fileSizeTruncated = ETrue;
273 TFileName sessionPath;
274 TheFs.SessionPath(sessionPath);
275 TBuf<32> message=_L("?: has %ld bytes free\n");
276 message[0]=sessionPath[0];
277 test.Printf(message,vInfo.iFree);
278 if (((vInfo.iDrive.iMediaAtt)&KMediaAttVariableSize)==0)
280 // Not a variable sized drive, so should be safe to to just create big file
281 test.Printf(_L("Creating %S, 0x%08lx\n"),&nameBuf1,size);
282 r=chan1.SetSize((TUint)size);
286 // Variable sized drive (proabably RAM drive) needs a bit of special treatment
287 // Use a binary search to allocate largest sized file possible...
288 test.Printf(_L("Creating %S, 0x%08x\n"),&nameBuf1,size);
290 TInt hi = (TInt)size;
291 const TInt KSizeGranularity = 0x200; // must be power-of-2
292 while(hi-lo>KSizeGranularity && r==KErrNone)
294 TInt trySize = (lo+hi)/2;
295 trySize &= ~(KSizeGranularity-1);
296 r = chan1.SetSize((TUint)trySize);
302 else if(r==KErrDiskFull)
310 // reduce size to leave some free for tests...
311 LeaveMemFree = 4096*4; // best leave several RAM pages worth of space so rest of test has some memory
312 size -= LeaveMemFree;
313 r = chan1.SetSize((TUint)size);
319 test.Printf(_L("ERROR: Creating large file failed %d\n"),r);
322 test.Printf(_L("Created %S, 0x%08x\n"),&nameBuf1,size);
325 while (fileSizeTruncated);
329 LOCAL_C void Cleanup()
331 // Cleanup test files
335 TInt r=TheFs.Delete(nameBuf1);
337 r=TheFs.RmDir(gSessionPath);
342 GLDEF_C void CallTestsL()
344 // Call tests that may leave
349 // These tests try to create a huge file to fill up the drive.
350 // This fails on WINS with drives with > 1/2G free because
351 // RFile::SetSize() (among other things) only takes a TInt.
353 if (gSessionPath.Left(1).CompareF(_L("C")) == 0)
356 CreateTestDirectory(_L("\\B_OPEN\\"));