os/kernelhwsrv/kerneltest/f32test/server/t_lock.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/server/t_lock.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,328 @@
     1.4 +// Copyright (c) 1995-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\t_lock.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <f32file.h>
    1.22 +#include <e32test.h>
    1.23 +#include "t_server.h"
    1.24 +
    1.25 +
    1.26 +class RFileTest : public RFile
    1.27 +	{
    1.28 +public:
    1.29 +	RFileTest(const TDesC& aName);
    1.30 +	RFileTest& Replace(const TDesC& aName);
    1.31 +	RFileTest& Open(const TDesC& aName);
    1.32 +	RFileTest& Lock(TInt aPos,TInt aLen=1);
    1.33 +	RFileTest& LockE(TInt aPos,TInt aLen=1);
    1.34 +	RFileTest& UnLock(TInt aPos,TInt aLen=1);
    1.35 +	RFileTest& UnLockE(TInt aPos,TInt aLen=1);
    1.36 +	RFileTest& LockEA(TInt aPos,TInt aLen=1);
    1.37 +	RFileTest& UnLockEA(TInt aPos,TInt aLen=1);
    1.38 +	RFileTest& Write(TInt aPos,TInt aLen=1);
    1.39 +	RFileTest& WriteE(TInt aPos,TInt aLen=1);
    1.40 +	RFileTest& Read(TInt aPos,TInt aLen=1);
    1.41 +	RFileTest& ReadE(TInt aPos,TInt aLen=1);
    1.42 +	RFileTest& Size(TInt aSize);
    1.43 +	RFileTest& SizeE(TInt aSize);
    1.44 +private:
    1.45 +	TName iName;
    1.46 +	};
    1.47 +
    1.48 +GLDEF_D RTest test(_L("T_LOCK"));
    1.49 +LOCAL_D	RFileTest Test1(_L("File 1"));
    1.50 +LOCAL_D	RFileTest Test2(_L("File 2"));
    1.51 +LOCAL_D TBuf8<0x100> Pattern;
    1.52 +LOCAL_D TBuf8<0x100> Buffer;
    1.53 +
    1.54 +LOCAL_C void DoFormat()
    1.55 +//
    1.56 +// Format the ramdisk
    1.57 +//
    1.58 +	{
    1.59 +
    1.60 +	TInt count;
    1.61 +	RFormat format;
    1.62 +#if defined(__WINS__)
    1.63 +	TInt r=format.Open(TheFs,_L("Y:\\"),EHighDensity,count);
    1.64 +#else
    1.65 +	TInt r=format.Open(TheFs,_L("C:\\"),EHighDensity,count);
    1.66 +#endif
    1.67 +	test(r==KErrNone);
    1.68 +	while(count)
    1.69 +		{
    1.70 +		r=format.Next(count);
    1.71 +		test(r==KErrNone);
    1.72 +		}
    1.73 +	format.Close();
    1.74 +	}
    1.75 +
    1.76 +LOCAL_C void MakeTestDirectory()
    1.77 +//
    1.78 +// Format the ramdisk if it is corrupt
    1.79 +//
    1.80 +	{
    1.81 +
    1.82 +	TPtrC testDir=_L("\\F32-TST\\");
    1.83 +	TInt r=TheFs.MkDir(testDir);
    1.84 +	if (r==KErrNone || r==KErrAlreadyExists)
    1.85 +		return;
    1.86 +	test.Next(_L("Formatting disk"));
    1.87 +	DoFormat();
    1.88 +	r=TheFs.MkDir(testDir);
    1.89 +	test(r==KErrNone);
    1.90 +	}
    1.91 +
    1.92 +RFileTest::RFileTest(const TDesC& aName)
    1.93 +//
    1.94 +// Constructor
    1.95 +//
    1.96 +	: iName(aName)
    1.97 +	{}
    1.98 +
    1.99 +RFileTest& RFileTest::Replace(const TDesC& aName)
   1.100 +//
   1.101 +// Replace a file.
   1.102 +//
   1.103 +	{
   1.104 +
   1.105 +	test.Printf(_L("%S replace %S\n"),&iName,&aName);
   1.106 +	TInt r=RFile::Replace(TheFs,aName,EFileStream|EFileWrite|EFileShareAny);
   1.107 +	test(r==KErrNone);
   1.108 +	return(*this);
   1.109 +	}
   1.110 +
   1.111 +RFileTest& RFileTest::Open(const TDesC& aName)
   1.112 +//
   1.113 +// Open a file.
   1.114 +//
   1.115 +	{
   1.116 +
   1.117 +	test.Printf(_L("%S open %S\n"),&iName,&aName);
   1.118 +	TInt r=RFile::Open(TheFs,aName,EFileStream|EFileWrite|EFileShareAny);
   1.119 +	test(r==KErrNone);
   1.120 +	return(*this);
   1.121 +	}
   1.122 +
   1.123 +RFileTest& RFileTest::Lock(TInt aPos,TInt aLen)
   1.124 +//
   1.125 +// Set a lock on the file. Expected not to fail.
   1.126 +//
   1.127 +	{
   1.128 +
   1.129 +	test.Printf(_L("%S lock   %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.130 +	TInt r=RFile::Lock(aPos,aLen);
   1.131 +	test(r==KErrNone);
   1.132 +	return(*this);
   1.133 +	}
   1.134 +
   1.135 +RFileTest& RFileTest::LockE(TInt aPos,TInt aLen)
   1.136 +//
   1.137 +// Set a lock on the file. Expected to fail.
   1.138 +//
   1.139 +	{
   1.140 +
   1.141 +	test.Printf(_L("%S lockE  %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.142 +	TInt r=RFile::Lock(aPos,aLen);
   1.143 +	test(r==KErrLocked);
   1.144 +	return(*this);
   1.145 +	}
   1.146 +
   1.147 +RFileTest& RFileTest::UnLock(TInt aPos,TInt aLen)
   1.148 +//
   1.149 +// Unlock the file. Expected not to fail.
   1.150 +//
   1.151 +	{
   1.152 +
   1.153 +	test.Printf(_L("%S ulock  %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.154 +	TInt r=RFile::UnLock(aPos,aLen);
   1.155 +	test(r==KErrNone);
   1.156 +	return(*this);
   1.157 +	}
   1.158 +
   1.159 +RFileTest& RFileTest::UnLockE(TInt aPos,TInt aLen)
   1.160 +//
   1.161 +// Unlock the file. Expected to fail.
   1.162 +//
   1.163 +	{
   1.164 +
   1.165 +	test.Printf(_L("%S ulockE %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.166 +	TInt r=RFile::UnLock(aPos,aLen);
   1.167 +	test(r==KErrNotFound);
   1.168 +	return(*this);
   1.169 +	}
   1.170 + 
   1.171 +
   1.172 +RFileTest& RFileTest::LockEA(TInt aPos,TInt aLen)
   1.173 +//
   1.174 +// Set a lock on the file. Expected to fail with KErrArgument.
   1.175 +//
   1.176 +	{
   1.177 +
   1.178 +	test.Printf(_L("%S lock   %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.179 +	TInt r=RFile::Lock(aPos,aLen);
   1.180 +	test(r==KErrArgument);
   1.181 +	return(*this);
   1.182 +	}
   1.183 +
   1.184 +RFileTest& RFileTest::UnLockEA(TInt aPos,TInt aLen)
   1.185 +//
   1.186 +// Unlock the file. Expected to fail with KErrArgument.
   1.187 +//
   1.188 +	{
   1.189 +
   1.190 +	test.Printf(_L("%S ulock  %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.191 +	TInt r=RFile::UnLock(aPos,aLen);
   1.192 +	test(r==KErrArgument);
   1.193 +	return(*this);
   1.194 +	}
   1.195 +
   1.196 +RFileTest& RFileTest::Write(TInt aPos,TInt aLen)
   1.197 +//
   1.198 +// Write to the file. Expected not to fail.
   1.199 +//
   1.200 +	{
   1.201 +
   1.202 +	test.Printf(_L("%S write  %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.203 +	TInt r=RFile::Write(aPos,Pattern,aLen);
   1.204 +	test(r==KErrNone);
   1.205 +	return(*this);
   1.206 +	}
   1.207 +
   1.208 +RFileTest& RFileTest::WriteE(TInt aPos,TInt aLen)
   1.209 +//
   1.210 +// Write to the file. Expected to fail.
   1.211 +//
   1.212 +	{
   1.213 +
   1.214 +	test.Printf(_L("%S writeE %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.215 +	TInt r=RFile::Write(aPos,Pattern,aLen);
   1.216 +	test(r==KErrLocked);
   1.217 +	return(*this);
   1.218 +	}
   1.219 +
   1.220 +RFileTest& RFileTest::Read(TInt aPos,TInt aLen)
   1.221 +//
   1.222 +// Read from the file. Expected not to fail.
   1.223 +//
   1.224 +	{
   1.225 +
   1.226 +	test.Printf(_L("%S read   %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.227 +	TInt r=RFile::Read(aPos,Buffer,aLen);
   1.228 +	test(r==KErrNone);
   1.229 +	return(*this);
   1.230 +	}
   1.231 +
   1.232 +RFileTest& RFileTest::ReadE(TInt aPos,TInt aLen)
   1.233 +//
   1.234 +// Read from the file. Expected to fail.
   1.235 +//
   1.236 +	{
   1.237 +
   1.238 +	test.Printf(_L("%S readE  %08x-%08x\n"),&iName,aPos,aPos+aLen-1);
   1.239 +	TInt r=RFile::Read(aPos,Buffer,aLen);
   1.240 +	test(r==KErrLocked);
   1.241 +	return(*this);
   1.242 +	}
   1.243 +
   1.244 +RFileTest& RFileTest::Size(TInt aSize)
   1.245 +//
   1.246 +// Set the size of the file. Expected not to fail.
   1.247 +//
   1.248 +	{
   1.249 +
   1.250 +	test.Printf(_L("%S size   %08x\n"),&iName,aSize);
   1.251 +	TInt r=RFile::SetSize(aSize);
   1.252 +	test(r==KErrNone);
   1.253 +	return(*this);
   1.254 +	}
   1.255 +
   1.256 +RFileTest& RFileTest::SizeE(TInt aSize)
   1.257 +//
   1.258 +// Set the size of the file. Expected to fail.
   1.259 +//
   1.260 +	{
   1.261 +
   1.262 +	test.Printf(_L("%S sizeE  %08x\n"),&iName,aSize);
   1.263 +	TInt r=RFile::SetSize(aSize);
   1.264 +	test(r==KErrLocked);
   1.265 +	return(*this);
   1.266 +	}
   1.267 +
   1.268 +LOCAL_C void setup()
   1.269 +//
   1.270 +// Create the test files.
   1.271 +//
   1.272 +	{
   1.273 +
   1.274 +	test.Start(_L("Settting test environment"));
   1.275 +//
   1.276 +	Test1.Replace(_L("\\F32-TST\\LOCK.TST"));
   1.277 +	Test2.Open(_L("\\F32-TST\\LOCK.TST"));
   1.278 +//
   1.279 +	test.Next(_L("Creating test pattern"));
   1.280 +	Pattern.SetLength(Pattern.MaxLength());
   1.281 +	for (TInt i=0;i<Pattern.MaxLength();i++)
   1.282 +		Pattern[i]=(TText8)i;
   1.283 +//
   1.284 +	test.End();
   1.285 +	}
   1.286 +
   1.287 +LOCAL_C void testLock1()
   1.288 +//
   1.289 +// Test file sharing.
   1.290 +//
   1.291 +	{
   1.292 +
   1.293 +	test.Start(_L("Test locking 1"));
   1.294 +//
   1.295 +	test.Next(_L("Single file tests"));
   1.296 +	Test1.UnLockE(0).Lock(0).LockE(0).UnLock(0);
   1.297 +	Test1.Lock(0,0x100).Lock(0x100,0x100).Lock(0x200,0x100);
   1.298 +	Test1.UnLock(0x100,0x100).UnLock(0x200,0x100).UnLock(0,0x100);
   1.299 +	Test1.Lock(0,0x100).Lock(0x200,0x100).Lock(0x100,0x100);
   1.300 +	Test1.UnLock(0x200,0x100).UnLock(0x100,0x100).UnLock(0,0x100);
   1.301 +	Test1.Lock(0,0x100).Lock(0x200,0x100);
   1.302 +	Test1.LockE(0x100,0x101).LockE(0x180,0x100).LockE(0x80,0x100);
   1.303 +	Test1.Lock(0x400,0x100).LockE(0x180,0x400).LockE(0,0x400);
   1.304 +	Test1.UnLock(0x0,0x100).UnLock(0x200,0x100).UnLock(0x400,0x100);
   1.305 +	Test1.LockEA(0x40000000,0x40000002).LockEA(0x7FFFFFFC,0x10).LockEA(0x7FFFFFFF,0x100);
   1.306 + 	Test1.UnLockEA(0x40000000,0x40000001).UnLockEA(0x7FFFFFFF,0x100).UnLockEA(0x7FFFFFFE,0x05);
   1.307 +//
   1.308 +	test.Next(_L("Multi file tests"));
   1.309 +	Test1.Lock(0);
   1.310 +	Test2.LockE(0);
   1.311 +	Test1.UnLock(0);
   1.312 +	Test2.Lock(0);
   1.313 +	Test1.LockE(0);
   1.314 +	Test2.UnLock(0);
   1.315 +//
   1.316 +	test.End();
   1.317 +	}
   1.318 +
   1.319 +GLDEF_C void CallTestsL()
   1.320 +//
   1.321 +// Do all tests
   1.322 +//
   1.323 +	{
   1.324 +
   1.325 +	MakeTestDirectory();
   1.326 +	setup();
   1.327 +	testLock1();
   1.328 +	
   1.329 +	Test1.Close();
   1.330 +	Test2.Close();
   1.331 +	}