os/kernelhwsrv/kerneltest/f32test/server/b_open.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/server/b_open.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,361 @@
     1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32test\server\b_open.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#define __E32TEST_EXTENSION__
    1.22 +
    1.23 +#include <f32file.h>
    1.24 +#include <e32test.h>
    1.25 +#include <e32hal.h>
    1.26 +#include <f32dbg.h>
    1.27 +#include "t_server.h"
    1.28 +#include "t_chlffs.h"
    1.29 +
    1.30 +#ifdef __WINS__
    1.31 +#define WIN32_LEAN_AND_MEAN
    1.32 +#pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
    1.33 +#include <windows.h>
    1.34 +#pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
    1.35 +#endif
    1.36 +
    1.37 +GLDEF_D RTest test(_L("B_OPEN"));
    1.38 +
    1.39 +LOCAL_D const TInt KMaxFiles=200;
    1.40 +LOCAL_D TBuf<32> nameBuf[KMaxFiles];
    1.41 +LOCAL_D TBuf<0x100> nameBuf1;
    1.42 +LOCAL_D RFile chan[KMaxFiles];
    1.43 +LOCAL_D RFile chan1;
    1.44 +LOCAL_D TFileName fBuf;
    1.45 +LOCAL_D TVolumeInfo vInfo;
    1.46 +LOCAL_D TInt LeaveMemFree;
    1.47 +
    1.48 +LOCAL_C void testOpenFiles()
    1.49 +//
    1.50 +// Open files till memory is full
    1.51 +// Write, seek, read, seteof, close, check and delete all files.
    1.52 +//
    1.53 +    {
    1.54 +	
    1.55 +	TChar currentDrive=gSessionPath[0];
    1.56 +	TInt driveNum;
    1.57 +	TInt r=TheFs.CharToDrive(currentDrive,driveNum);
    1.58 +	test_KErrNone(r);
    1.59 +
    1.60 +    TInt i=0;
    1.61 +	TInt totalRam;
    1.62 +    FOREVER
    1.63 +        {
    1.64 +
    1.65 +#if defined(__WINS__)
    1.66 +		DWORD sectorsPerCluster;
    1.67 +		DWORD bytesPerSector;
    1.68 +		DWORD freeClusters;
    1.69 +		DWORD sizeClusters;
    1.70 +		BOOL b=GetDiskFreeSpaceA("C:\\",&sectorsPerCluster,&bytesPerSector,&freeClusters,&sizeClusters);
    1.71 +		test(b==TRUE);
    1.72 +		totalRam=sizeClusters*sectorsPerCluster*bytesPerSector;
    1.73 +#else
    1.74 +		TMemoryInfoV1Buf memInfoBuf;
    1.75 +		UserHal::MemoryInfo(memInfoBuf);
    1.76 +		totalRam=memInfoBuf().iTotalRamInBytes;
    1.77 +#endif
    1.78 +        test.Printf(_L("Open %u\n"),i);
    1.79 +        nameBuf[i].Format(_L("B_OPEN test file %d"),i);
    1.80 +		r=chan[i].Replace(TheFs,nameBuf[i],EFileStream|EFileWrite);
    1.81 +		if (r==KErrNone)
    1.82 +			{
    1.83 +			r=chan[i].Write(_L8("SomeText"));
    1.84 +			if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
    1.85 +				r = chan[i].Flush();
    1.86 +			if (r==KErrDiskFull)
    1.87 +				{
    1.88 +				chan[i].Close();
    1.89 +				test(TheFs.Delete(nameBuf[i])==KErrNone);
    1.90 +				}
    1.91 +			}
    1.92 +		if (r==KErrDiskFull)
    1.93 +			break;
    1.94 +		if (r==KErrNoMemory)
    1.95 +			break;
    1.96 +		if (r!=KErrNone)
    1.97 +			{
    1.98 +			test.Printf(_L("ERROR: RFile::Replace returned %d\n"),r);
    1.99 +			test(EFalse);
   1.100 +			}
   1.101 +
   1.102 +        r=TheFs.Volume(vInfo);
   1.103 +		test_KErrNone(r);
   1.104 +		if (driveNum==EDriveC)
   1.105 +	        test.Printf(_L("VInfo size=0x%x free=0x%x TotalRam = 0x%x\n"),vInfo.iSize,vInfo.iFree,totalRam);
   1.106 +        test(vInfo.iFree<=vInfo.iSize);
   1.107 +
   1.108 +//      r=TheFs.Volume(vInfo,(driveNum==EDriveC) ? EDriveD : EDriveC);
   1.109 +//		if (r==KErrNone);
   1.110 +//		test(vInfo.iFree<=vInfo.iSize);
   1.111 +	    i++;
   1.112 +        if (i==KMaxFiles)
   1.113 +            {
   1.114 +            if (IsTestingLFFS())
   1.115 +                break;
   1.116 +            else
   1.117 +                test.Panic(_L("Too many files opened"));
   1.118 +            }
   1.119 +        }
   1.120 +
   1.121 +	test.Printf(_L("Created %d extra files\n"),i);
   1.122 +	const TInt n_files=i;
   1.123 +	r=TheFs.Volume(vInfo);
   1.124 +	test_KErrNone(r);
   1.125 +	if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
   1.126 +		{
   1.127 +		test(vInfo.iSize<=totalRam);
   1.128 +		test(vInfo.iFree<=totalRam);
   1.129 +		}
   1.130 +    test(vInfo.iFree<=vInfo.iSize);
   1.131 +
   1.132 +	test.Next(_L("SetSize to each file"));
   1.133 +    for (i=0;i<n_files;i++)
   1.134 +        {
   1.135 +		r=chan[i].SetSize(0);
   1.136 +		test_KErrNone(r);
   1.137 +        }
   1.138 +
   1.139 +	test.Next(_L("Write to each file"));
   1.140 +    TBuf8<3> numBuf;
   1.141 +    for (i=0;i<n_files;i++)
   1.142 +        {
   1.143 +        test.Printf(_L("Write %u\n"),i);
   1.144 +		numBuf.Format(_L8("%d"),i);
   1.145 +		r=chan[i].Write(0,numBuf);
   1.146 +		test_KErrNone(r);
   1.147 +        }
   1.148 +
   1.149 +	test.Next(_L("Seek on each file"));
   1.150 +    for (i=0;i<n_files;i++)
   1.151 +        {
   1.152 +        test.Printf(_L("Seeking %u\n"),i);
   1.153 +        TInt pos=0;
   1.154 +        r=chan[i].Seek(ESeekStart,pos);
   1.155 +		test_KErrNone(r);
   1.156 +        }
   1.157 +
   1.158 +	test.Next(_L("Read from each file"));
   1.159 +    TBuf8<3> checkBuf;
   1.160 +	for (i=0;i<n_files;i++)
   1.161 +        {
   1.162 +        test.Printf(_L("Read %u\n"),i);
   1.163 +        r=chan[i].Read(checkBuf,3);
   1.164 +		test_KErrNone(r);
   1.165 +		numBuf.Format(_L8("%d"),i);
   1.166 +		test(numBuf==checkBuf);
   1.167 +        }
   1.168 +
   1.169 +	test.Next(_L("Set size of each file"));
   1.170 +    for (i=0;i<n_files;i++)
   1.171 +        {
   1.172 +        test.Printf(_L("Set size %u\n"),i);
   1.173 +        r=chan[i].SetSize(i);
   1.174 +		test_KErrNone(r);
   1.175 +        }
   1.176 +
   1.177 +    r=TheFs.Volume(vInfo);
   1.178 +	test_KErrNone(r);
   1.179 +	if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
   1.180 +		{
   1.181 +	    test(vInfo.iSize<=totalRam);
   1.182 +		test(vInfo.iFree<=totalRam);
   1.183 +		}
   1.184 +    test(vInfo.iFree<=vInfo.iSize);
   1.185 +
   1.186 +	test.Next(_L("Close each file"));
   1.187 +    for (i=0;i<n_files;i++)
   1.188 +        {
   1.189 +        test.Printf(_L("Close %u\n"),i);
   1.190 +        chan[i].Close();
   1.191 +        }
   1.192 +
   1.193 +	test.Next(_L("Open each file"));
   1.194 +
   1.195 +	TInt n_files_open;
   1.196 +    for (n_files_open=0; n_files_open < n_files; n_files_open++)
   1.197 +        {
   1.198 +        r = chan[n_files_open].Open(TheFs,nameBuf[n_files_open],EFileRead|EFileStream);
   1.199 +		test.Printf(_L("Open(%d) ret %d\n"), n_files_open, r);
   1.200 +		if (r != KErrNone)
   1.201 +			{
   1.202 +			if (driveNum==EDriveC && (vInfo.iDrive.iMediaAtt & KMediaAttVariableSize))
   1.203 +				break;
   1.204 +			else
   1.205 +				test(0);
   1.206 +			}
   1.207 +        }
   1.208 +
   1.209 +	test.Next(_L("Check size of each file"));
   1.210 +    for (i=0;i<n_files_open;i++)
   1.211 +        {
   1.212 +        test.Printf(_L("Check %u\n"),i);
   1.213 +        TInt size;
   1.214 +		TInt r=chan[i].Size(size);
   1.215 +		test.Printf(_L("size of file %u is %d ret %d\n"),i,size, r);
   1.216 +		test_KErrNone(r);
   1.217 +		test_Equal(i,size);
   1.218 +        }
   1.219 +
   1.220 +	test.Next(_L("Close each file"));
   1.221 +    for (i=0;i<n_files_open;i++)
   1.222 +        {
   1.223 +        test.Printf(_L("Close %u\n"),i);
   1.224 +        chan[i].Close();
   1.225 +        }
   1.226 +	
   1.227 +	test.Next(_L("Delete files"));
   1.228 +    for (i=0;i<n_files;i++)
   1.229 +        {
   1.230 +        test.Printf(_L("Delete %u\n"),i);
   1.231 +        r=TheFs.Delete(nameBuf[i]);
   1.232 +		test_KErrNone(r);
   1.233 +        }
   1.234 +    }
   1.235 +
   1.236 +
   1.237 +
   1.238 +LOCAL_C void InitTest()
   1.239 +//
   1.240 +// Create a large file
   1.241 +//
   1.242 +	{
   1.243 +	TInt fileNumber = 0;
   1.244 +	TBool fileSizeTruncated;
   1.245 +
   1.246 +	do
   1.247 +		{
   1.248 +		fileSizeTruncated = EFalse;
   1.249 +		nameBuf1.Format(_L("\\Hooge Test File for B_OPEN %d"), fileNumber++);
   1.250 +
   1.251 +		TInt r=chan1.Replace(TheFs,nameBuf1,EFileStream|EFileWrite);
   1.252 +		test_KErrNone(r);
   1.253 +		r=TheFs.Volume(vInfo);
   1.254 +		test_KErrNone(r);
   1.255 +		TInt64 size;
   1.256 +		LeaveMemFree = 0x400; // ???
   1.257 +		if (vInfo.iFree>LeaveMemFree)
   1.258 +			size=vInfo.iFree-LeaveMemFree;
   1.259 +		else
   1.260 +			size=0;
   1.261 +
   1.262 +		const TInt KMaxFileSize = 0x40000000;
   1.263 +
   1.264 +		// test as 64 bit numbers in case size is very large (eg. enough that TInt is
   1.265 +		// not large enough to hold it)
   1.266 +		TInt64 KMaxFileSize64 = MAKE_TINT64(0, KMaxFileSize);
   1.267 +
   1.268 +		test.Printf(_L("Free space available = %08x:%08x\n"), I64HIGH(size), I64LOW(size));
   1.269 +		if (size > KMaxFileSize64)
   1.270 +			{
   1.271 +			size = KMaxFileSize;
   1.272 +			test.Printf(_L("Truncated to %d to avoid current FAT FSY file size limit !!!\n"), size);
   1.273 +			fileSizeTruncated = ETrue;
   1.274 +			}
   1.275 +
   1.276 +		TFileName sessionPath;
   1.277 +		TheFs.SessionPath(sessionPath);
   1.278 +		TBuf<32> message=_L("?: has %ld bytes free\n");
   1.279 +		message[0]=sessionPath[0];
   1.280 +		test.Printf(message,vInfo.iFree);
   1.281 +		if (((vInfo.iDrive.iMediaAtt)&KMediaAttVariableSize)==0)
   1.282 +			{
   1.283 +			// Not a variable sized drive, so should be safe to to just create big file
   1.284 +			test.Printf(_L("Creating %S, 0x%08lx\n"),&nameBuf1,size);
   1.285 +			r=chan1.SetSize((TUint)size);
   1.286 +			}
   1.287 +		else
   1.288 +			{
   1.289 +			// Variable sized drive (proabably RAM drive) needs a bit of special treatment
   1.290 +			// Use a binary search to allocate largest sized file possible...
   1.291 +			test.Printf(_L("Creating %S, 0x%08x\n"),&nameBuf1,size);
   1.292 +			TInt lo = 0;
   1.293 +			TInt hi = (TInt)size;
   1.294 +			const TInt KSizeGranularity = 0x200; // must be power-of-2
   1.295 +			while(hi-lo>KSizeGranularity && r==KErrNone)
   1.296 +				{
   1.297 +				TInt trySize = (lo+hi)/2;
   1.298 +				trySize &= ~(KSizeGranularity-1);
   1.299 +				r = chan1.SetSize((TUint)trySize);
   1.300 +				if(r==KErrNone)
   1.301 +					{
   1.302 +					size = trySize;
   1.303 +					lo = trySize;
   1.304 +					}
   1.305 +				else if(r==KErrDiskFull)
   1.306 +					{
   1.307 +					hi = trySize;
   1.308 +					r = KErrNone;
   1.309 +					}
   1.310 +				}
   1.311 +			if(r==KErrNone)
   1.312 +				{
   1.313 +				// reduce size to leave some free for tests...
   1.314 +				LeaveMemFree = 4096*4; // best leave several RAM pages worth of space so rest of test has some memory
   1.315 +				size -= LeaveMemFree;
   1.316 +				r = chan1.SetSize((TUint)size);
   1.317 +				}
   1.318 +			}
   1.319 +
   1.320 +		if (r!=KErrNone)
   1.321 +			{
   1.322 +			test.Printf(_L("ERROR: Creating large file failed %d\n"),r);
   1.323 +			test(EFalse);
   1.324 +			}
   1.325 +		test.Printf(_L("Created %S, 0x%08x\n"),&nameBuf1,size);
   1.326 +		chan1.Close();
   1.327 +		}
   1.328 +	while (fileSizeTruncated);
   1.329 +
   1.330 +	}
   1.331 +
   1.332 +LOCAL_C void Cleanup()
   1.333 +//
   1.334 +// Cleanup test files
   1.335 +//
   1.336 +	{
   1.337 +
   1.338 +	TInt r=TheFs.Delete(nameBuf1);
   1.339 +	test_KErrNone(r);
   1.340 +	r=TheFs.RmDir(gSessionPath);
   1.341 +	test_KErrNone(r);
   1.342 +	}
   1.343 +
   1.344 +
   1.345 +GLDEF_C void CallTestsL()
   1.346 +//
   1.347 +// Call tests that may leave
   1.348 +//
   1.349 +	{
   1.350 +
   1.351 +#ifdef __WINS__
   1.352 +// These tests try to create a huge file to fill up the drive.
   1.353 +// This fails on WINS with drives with > 1/2G free because
   1.354 +// RFile::SetSize() (among other things) only takes a TInt.
   1.355 +//
   1.356 +	if (gSessionPath.Left(1).CompareF(_L("C")) == 0)
   1.357 +		return;
   1.358 +#endif
   1.359 +	CreateTestDirectory(_L("\\B_OPEN\\"));
   1.360 +	InitTest();
   1.361 +	testOpenFiles();
   1.362 +	Cleanup();
   1.363 +	}
   1.364 +