os/kernelhwsrv/kerneltest/e32test/system/d_dobject.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/system/d_dobject.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,965 @@
     1.4 +// Copyright (c) 2004-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 +// e32test\system\d_dobject.cpp
    1.18 +// LDD for testing RObjectIx class
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include <kernel/kern_priv.h>
    1.23 +#include "d_dobject.h"
    1.24 +#include "../misc/prbs.h"
    1.25 +
    1.26 +
    1.27 +//
    1.28 +// Handle Mutex referenced from ObjectIx.cpp (normally from sglobals.cpp).
    1.29 +//
    1.30 +DMutex* RObjectIx::HandleMutex;
    1.31 +
    1.32 +//
    1.33 +// Constants for test sizes and stepings...
    1.34 +//
    1.35 +const TInt KDObjectTestMaxTestSize  = 2000;	// Bigest size to test
    1.36 +const TInt KDObjectTestStepStart    = 19;	// Test all values below this
    1.37 +const TInt KDObjectTestStep         = 31;	// Step value used when above KDObjectTestStepStart.
    1.38 +const TInt KDObjectTestLoopCount    = 3;	// Loop count used in some tests to repeat random testing.
    1.39 +
    1.40 +
    1.41 +//
    1.42 +// LDD factory
    1.43 +//
    1.44 +
    1.45 +class DObjectTestFactory : public DLogicalDevice
    1.46 +	{
    1.47 +public:
    1.48 +	DObjectTestFactory();
    1.49 +	~DObjectTestFactory();
    1.50 +	virtual TInt Install();
    1.51 +	virtual void GetCaps(TDes8& aDes) const;
    1.52 +	virtual TInt Create(DLogicalChannelBase*& aChannel);
    1.53 +public:
    1.54 +	};
    1.55 +
    1.56 +//
    1.57 +// Logical Channel
    1.58 +//
    1.59 +const TInt KObjectIndexMask=0x7fff;
    1.60 +
    1.61 +class DObjectTest : public DLogicalChannelBase
    1.62 +	{
    1.63 +public:
    1.64 +	virtual ~DObjectTest();
    1.65 +protected:
    1.66 +	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
    1.67 +	virtual TInt Request(TInt aFunction, TAny* a1, TAny* a2);
    1.68 +public:
    1.69 +	DObjectTestFactory* iFactory;
    1.70 +
    1.71 +private:
    1.72 +	TInt RObjectIxTest1(TInt aSize);
    1.73 +	TInt RObjectIxTest2(TInt aSize);
    1.74 +	TInt RObjectIxTest3(TInt aSize, TBool aPerformanceTest);
    1.75 +	TInt RObjectIxTest4(TInt aSize);
    1.76 +	TInt RObjectIxTestExerciseIx(RObjectIx& aObjectIx, TInt aSize);
    1.77 +	TInt RObjectIxInvalidHandleLookupTest(TInt aSize);
    1.78 +	TInt DObjectNameTest();
    1.79 +
    1.80 +	inline TInt Index(TInt aHandle)	{return(aHandle&KObjectIndexMask);}
    1.81 +
    1.82 +private:
    1.83 +	struct DOBjAndHandle
    1.84 +		{
    1.85 +		DObject* iObject;
    1.86 +		TInt	 iHandle;
    1.87 +		};
    1.88 +
    1.89 +	TUint iSeed[2];
    1.90 +	DOBjAndHandle* iObjAndHandle;	
    1.91 +	};
    1.92 +
    1.93 +//
    1.94 +// LDD factory
    1.95 +//
    1.96 +
    1.97 +DObjectTestFactory::DObjectTestFactory()
    1.98 +	{
    1.99 +	Kern::MutexCreate(RObjectIx::HandleMutex, _L("HandleMutex"), KMutexOrdHandle);
   1.100 +	}
   1.101 +
   1.102 +DObjectTestFactory::~DObjectTestFactory()
   1.103 +	{
   1.104 +	delete RObjectIx::HandleMutex;
   1.105 +	RObjectIx::HandleMutex = NULL;
   1.106 +	}
   1.107 +
   1.108 +TInt DObjectTestFactory::Create(DLogicalChannelBase*& aChannel)
   1.109 +	{
   1.110 +	aChannel=new DObjectTest;
   1.111 +	return KErrNone;
   1.112 +	}
   1.113 +
   1.114 +TInt DObjectTestFactory::Install()
   1.115 +	{
   1.116 +	return SetName(&KDObjectTestLddName);
   1.117 +	}
   1.118 +
   1.119 +void DObjectTestFactory::GetCaps(TDes8& /* aDes */) const
   1.120 +	{
   1.121 +	//aDes.FillZ(aDes.MaxLength());
   1.122 +	}
   1.123 +
   1.124 +DECLARE_STANDARD_LDD()
   1.125 +	{
   1.126 +	return new DObjectTestFactory;
   1.127 +	}
   1.128 +
   1.129 +//
   1.130 +// Logical Channel
   1.131 +//
   1.132 +
   1.133 +TInt DObjectTest::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
   1.134 +	{
   1.135 +	if (NULL == (iObjAndHandle = (DOBjAndHandle*)Kern::Alloc(KDObjectTestMaxTestSize * sizeof(DOBjAndHandle))))
   1.136 +		return KErrNoMemory;
   1.137 +	return KErrNone;
   1.138 +	}
   1.139 +
   1.140 +DObjectTest::~DObjectTest()
   1.141 +	{
   1.142 +	delete[] iObjAndHandle;
   1.143 +	}
   1.144 +
   1.145 +
   1.146 +//Adds a number of DObjects to RObjectIx, then removes them in the same order.
   1.147 +TInt DObjectTest::RObjectIxTest1(TInt aSize)
   1.148 +{
   1.149 +	TInt i,r;
   1.150 +	DObject* object;
   1.151 +	RObjectIx  myIx;
   1.152 +
   1.153 +	myIx.Check(0);
   1.154 +
   1.155 +	for (i=0; i<aSize; i++) //Add
   1.156 +		{
   1.157 +		if( (iObjAndHandle[i].iObject = new DObject) == NULL) 
   1.158 +			{
   1.159 +			myIx.Close(NULL);
   1.160 +			return KErrNoMemory;
   1.161 +			}
   1.162 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.163 +		r = myIx.Add(iObjAndHandle[i].iObject, 0);
   1.164 +		if (r<0)
   1.165 +			{
   1.166 +			myIx.Check(0);
   1.167 +			myIx.Close(NULL);
   1.168 +			return r;
   1.169 +			}
   1.170 +		iObjAndHandle[i].iHandle = r;
   1.171 +		}
   1.172 +
   1.173 +	myIx.Check(0);
   1.174 +	
   1.175 +	for (i=0; i<aSize; i++)		//Remove
   1.176 +		{
   1.177 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.178 +
   1.179 +		if (KErrNone != (r=myIx.Remove(iObjAndHandle[i].iHandle, object, attr))) 
   1.180 +			{
   1.181 +			myIx.Check(0);
   1.182 +			myIx.Close(NULL);
   1.183 +			return r;
   1.184 +			}
   1.185 +		iObjAndHandle[i].iObject->Close(NULL);
   1.186 +		}
   1.187 +
   1.188 +	myIx.Check(0);
   1.189 +	myIx.Close(NULL);
   1.190 +
   1.191 +	return KErrNone;
   1.192 +}
   1.193 +
   1.194 +//Adds a number of DObjects to RObjectIx, then removes them in the reverse order.
   1.195 +TInt DObjectTest::RObjectIxTest2(TInt aSize)
   1.196 +{
   1.197 +	TInt i,r;
   1.198 +	DObject* object;
   1.199 +	RObjectIx myIx;
   1.200 +	
   1.201 +	myIx.Check(0);
   1.202 +
   1.203 +	for (i=0; i<aSize; i++) //Add
   1.204 +		{
   1.205 +		if( (iObjAndHandle[i].iObject = new DObject) == NULL) 
   1.206 +			{
   1.207 +			myIx.Close(NULL);
   1.208 +			return KErrNoMemory;
   1.209 +			}
   1.210 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.211 +		r = myIx.Add(iObjAndHandle[i].iObject, 0);
   1.212 +		if (r<0)
   1.213 +			{
   1.214 +			myIx.Check(0);
   1.215 +			myIx.Close(NULL);
   1.216 +			return r;
   1.217 +			}
   1.218 +		iObjAndHandle[i].iHandle = r;
   1.219 +		}
   1.220 +
   1.221 +	myIx.Check(0);
   1.222 +
   1.223 +	for (i=aSize-1; i>=0; i--)		//Remove in reverse
   1.224 +		{
   1.225 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.226 +
   1.227 +		if (KErrNone != (r=myIx.Remove(iObjAndHandle[i].iHandle, object, attr))) 
   1.228 +			{
   1.229 +			myIx.Check(0);
   1.230 +			myIx.Close(NULL);
   1.231 +			return r;
   1.232 +			}
   1.233 +		iObjAndHandle[i].iObject->Close(NULL);
   1.234 +		}
   1.235 +
   1.236 +	myIx.Check(0);
   1.237 +	myIx.Close(NULL);
   1.238 +
   1.239 +	return KErrNone;
   1.240 +}
   1.241 +
   1.242 +//Adds and removes random number of DObjects to/from RObjectIx.
   1.243 +TInt DObjectTest::RObjectIxTest3(TInt aSize, TBool aPerformanceTest)
   1.244 +{
   1.245 +	TInt index, x, r;
   1.246 +	DObject* object;
   1.247 +	RObjectIx myIx;
   1.248 +
   1.249 +	//---Create & init the objects we need
   1.250 +	for (x=0; x<aSize; x++) iObjAndHandle[x].iObject = NULL; //initialize the array
   1.251 +
   1.252 +	if (!aPerformanceTest)
   1.253 +		myIx.Check(0);
   1.254 +
   1.255 +	for (x = 0;  x < KDObjectTestLoopCount;  x++)
   1.256 +		{
   1.257 +		
   1.258 +		//---Add the random number of objects (in random order)---
   1.259 +		TInt toAdd=Random(iSeed)%(aSize-myIx.ActiveCount()+1);
   1.260 +		while (toAdd--)
   1.261 +			{
   1.262 +			index=Random(iSeed)%aSize;
   1.263 +			while (iObjAndHandle[index].iObject) index=(index+1)%aSize; //Find the next NULL pointer 
   1.264 +			if( (iObjAndHandle[index].iObject = new DObject) == NULL) 
   1.265 +				{
   1.266 +				myIx.Close(NULL);
   1.267 +				return KErrNoMemory;
   1.268 +				}
   1.269 +			r = myIx.Add(iObjAndHandle[index].iObject, 0);
   1.270 +			if (r<0)
   1.271 +				{
   1.272 +				if (!aPerformanceTest)
   1.273 +					myIx.Check(0);
   1.274 +				myIx.Close(NULL);
   1.275 +				return r;
   1.276 +				}
   1.277 +			iObjAndHandle[index].iHandle = r;
   1.278 +			}
   1.279 +
   1.280 +		if (!aPerformanceTest)
   1.281 +			myIx.Check(0);
   1.282 +
   1.283 +		//---Remove the random number of objects (in random order)---
   1.284 +		TInt toRemove=Random(iSeed)%(myIx.ActiveCount()+1);
   1.285 +		while (toRemove--)
   1.286 +			{
   1.287 +			index=Random(iSeed)%aSize;
   1.288 +			while (!iObjAndHandle[index].iObject) index=(index+1)%aSize; //Find the next non-NULL pointer 
   1.289 +
   1.290 +			TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.291 +
   1.292 +			if (KErrNone != (r=myIx.Remove(iObjAndHandle[index].iHandle, object, attr)))
   1.293 +				{
   1.294 +				if (!aPerformanceTest)
   1.295 +					myIx.Check(0);
   1.296 +				myIx.Close(NULL);
   1.297 +				return r;
   1.298 +				}
   1.299 +			iObjAndHandle[index].iObject->Close(NULL);
   1.300 +			iObjAndHandle[index].iObject=NULL;
   1.301 +			}
   1.302 +
   1.303 +		if(aPerformanceTest) continue;
   1.304 +
   1.305 +		myIx.Check(0);
   1.306 +
   1.307 +		//---Test data consistency---
   1.308 +		TInt objNum=0;
   1.309 +		for (index=0;index<aSize;index++) 
   1.310 +			{
   1.311 +			if (iObjAndHandle[index].iObject)
   1.312 +				{
   1.313 +				objNum++;
   1.314 +				
   1.315 +				//Test At(TInt aHandle) method
   1.316 +				NKern::LockSystem();
   1.317 +				if (iObjAndHandle[index].iObject != myIx.At(iObjAndHandle[index].iHandle))
   1.318 +					{
   1.319 +					NKern::UnlockSystem();
   1.320 +					myIx.Check(0);
   1.321 +					myIx.Close(NULL);
   1.322 +					return KErrGeneral;
   1.323 +					}
   1.324 +				NKern::UnlockSystem();
   1.325 +
   1.326 +				
   1.327 +				//Test Count(CObject* aObject) method
   1.328 +				RObjectIx::Wait();
   1.329 +				if (1!=myIx.Count(iObjAndHandle[index].iObject))
   1.330 +					{
   1.331 +					RObjectIx::Signal();
   1.332 +					myIx.Check(0);
   1.333 +					myIx.Close(NULL);
   1.334 +					return KErrGeneral;
   1.335 +					}
   1.336 +				
   1.337 +				//Test At(CObject* aObject) method
   1.338 +				if (iObjAndHandle[index].iHandle != myIx.At(iObjAndHandle[index].iObject))
   1.339 +					{
   1.340 +					RObjectIx::Signal();
   1.341 +					myIx.Check(0);
   1.342 +					myIx.Close(NULL);
   1.343 +					return KErrGeneral;
   1.344 +					}
   1.345 +				RObjectIx::Signal();
   1.346 +
   1.347 +				//Test operator[](TInt index) method
   1.348 +				NKern::LockSystem();
   1.349 +				if (iObjAndHandle[index].iObject != myIx[Index(iObjAndHandle[index].iHandle)])
   1.350 +					{
   1.351 +					NKern::UnlockSystem();
   1.352 +					myIx.Check(0);
   1.353 +					myIx.Close(NULL);
   1.354 +					return KErrGeneral;
   1.355 +					}
   1.356 +				NKern::UnlockSystem();
   1.357 +
   1.358 +				}
   1.359 +			}
   1.360 +	
   1.361 +		if (objNum != myIx.ActiveCount())
   1.362 +			{
   1.363 +			myIx.Check(0);
   1.364 +			myIx.Close(NULL);
   1.365 +			return KErrGeneral;
   1.366 +			}
   1.367 +		}
   1.368 +
   1.369 +	myIx.Check(0);
   1.370 +	myIx.Close(NULL);
   1.371 +
   1.372 +	return KErrNone;
   1.373 +}
   1.374 +
   1.375 +
   1.376 +//Adds a number of DObjects to RObjectIx using reserved slots, then removes
   1.377 +// them in the same order. Repeat using the reverse order.
   1.378 +TInt DObjectTest::RObjectIxTest4(TInt aSize)
   1.379 +	{
   1.380 +	TInt i,r;
   1.381 +	DObject* object;
   1.382 +	RObjectIx myIx;
   1.383 +
   1.384 +	myIx.Check(0);
   1.385 +	myIx.Reserve(aSize);
   1.386 +	myIx.Check(0);
   1.387 +	myIx.Reserve(-aSize);
   1.388 +	myIx.Check(0);
   1.389 +	myIx.Reserve(aSize);
   1.390 +	myIx.Check(0);
   1.391 +	
   1.392 +	for (i=0; i<aSize; i++) //Add
   1.393 +		{
   1.394 +		if( (iObjAndHandle[i].iObject = new DObject) == NULL) 
   1.395 +			{
   1.396 +			myIx.Check(0);
   1.397 +			myIx.Close(NULL);
   1.398 +			return KErrNoMemory;
   1.399 +			}
   1.400 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.401 +		r = myIx.Add(iObjAndHandle[i].iObject, (TUint32)RObjectIx::EReserved);
   1.402 +		if (r<0)
   1.403 +			{
   1.404 +			myIx.Check(0);
   1.405 +			myIx.Close(NULL);
   1.406 +			return r;
   1.407 +			}
   1.408 +		iObjAndHandle[i].iHandle = r;
   1.409 +		}
   1.410 +
   1.411 +	myIx.Check(0);
   1.412 +
   1.413 +	TInt toRemove=Random(iSeed)%(myIx.ActiveCount()+1);
   1.414 +	TInt toAdd=toRemove; // will put them all back again...
   1.415 +	while (toRemove--)
   1.416 +		{
   1.417 +		i=Random(iSeed)%aSize;
   1.418 +		while (!iObjAndHandle[i].iObject) i=(i+1)%aSize; //Find the next non-NULL pointer 
   1.419 +
   1.420 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.421 +
   1.422 +		if (KErrNone != (r=myIx.Remove(iObjAndHandle[i].iHandle, object, attr))) 
   1.423 +			{
   1.424 +			myIx.Check(0);
   1.425 +			myIx.Close(NULL);
   1.426 +			return r;
   1.427 +			}
   1.428 +		if ((attr & RObjectIx::EReserved) == 0) 
   1.429 +			{
   1.430 +			myIx.Check(0);
   1.431 +			myIx.Close(NULL);
   1.432 +			return KErrBadHandle;
   1.433 +			}
   1.434 +		iObjAndHandle[i].iObject->Close(NULL);
   1.435 +		iObjAndHandle[i].iObject = NULL;
   1.436 +		}
   1.437 +
   1.438 +	myIx.Check(0);
   1.439 +
   1.440 +	while (toAdd--)
   1.441 +		{
   1.442 +		i=Random(iSeed)%aSize;
   1.443 +		while (iObjAndHandle[i].iObject) i=(i+1)%aSize; //Find the next NULL pointer 
   1.444 +
   1.445 +		if( (iObjAndHandle[i].iObject = new DObject) == NULL) 
   1.446 +			{
   1.447 +			myIx.Check(0);
   1.448 +			myIx.Close(NULL);
   1.449 +			return KErrNoMemory;
   1.450 +			}
   1.451 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.452 +		r = myIx.Add(iObjAndHandle[i].iObject, (TUint32)RObjectIx::EReserved);
   1.453 +		if (r<0)
   1.454 +			{
   1.455 +			myIx.Check(0);
   1.456 +			myIx.Close(NULL);
   1.457 +			return r;
   1.458 +			}
   1.459 +		iObjAndHandle[i].iHandle = r;
   1.460 +		}
   1.461 +
   1.462 +	myIx.Check(0);
   1.463 +
   1.464 +	for (i=aSize-1; i>=0; i--)		//Remove in reverse
   1.465 +		{
   1.466 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.467 +
   1.468 +		if (KErrNone != (r=myIx.Remove(iObjAndHandle[i].iHandle, object, attr))) 
   1.469 +			{
   1.470 +			myIx.Check(0);
   1.471 +			myIx.Close(NULL);
   1.472 +			return r;
   1.473 +			}
   1.474 +		if ((attr & RObjectIx::EReserved) == 0) 
   1.475 +			{
   1.476 +			myIx.Check(0);
   1.477 +			myIx.Close(NULL);
   1.478 +			return KErrBadHandle;
   1.479 +			}
   1.480 +		iObjAndHandle[i].iObject->Close(NULL);
   1.481 +		iObjAndHandle[i].iObject = NULL;
   1.482 +		}
   1.483 +
   1.484 +	myIx.Check(0);
   1.485 +	myIx.Close(NULL);
   1.486 +
   1.487 +	return KErrNone;
   1.488 +	} // DObjectTest::RObjectIxTest4
   1.489 +
   1.490 +
   1.491 +// Adds a number of DObjects to RObjectIx using both normal and reserved slots, plus
   1.492 +// unused reserved slots then removes
   1.493 +// them in the same order. Repeat using the reverse order. Used in concurrent
   1.494 +// testing with multiple threads.
   1.495 +TInt DObjectTest::RObjectIxTestExerciseIx(RObjectIx& aObjectIx, TInt aSize)
   1.496 +	{
   1.497 +	TInt i,r;
   1.498 +	DObject* object;
   1.499 +
   1.500 +	aObjectIx.Check(0);
   1.501 +
   1.502 +	for (i=0; i<aSize; i++) //Add and reserve (causing a Grow())...
   1.503 +		{
   1.504 +		// we reserve handles because it encourages grow and shrinking of the pool
   1.505 +		aObjectIx.Reserve(1);
   1.506 +
   1.507 +		if( (iObjAndHandle[i].iObject = new DObject) == NULL)
   1.508 +			{
   1.509 +			return KErrNoMemory;
   1.510 +			}
   1.511 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.512 +		r = aObjectIx.Add(iObjAndHandle[i].iObject, i&0x1 ? (TUint32)RObjectIx::EReserved : 0);
   1.513 +		if (r<0)
   1.514 +			{
   1.515 +			return r;
   1.516 +			}
   1.517 +		iObjAndHandle[i].iHandle = r;
   1.518 +		}
   1.519 +
   1.520 +	//---Test data consistency---
   1.521 +	for (i=0;i<aSize;i++)
   1.522 +		{
   1.523 +		if (iObjAndHandle[i].iObject)
   1.524 +			{
   1.525 +			//Test At(TInt aHandle) method
   1.526 +			NKern::LockSystem();
   1.527 +			if (iObjAndHandle[i].iObject != aObjectIx.At(iObjAndHandle[i].iHandle))
   1.528 +				{
   1.529 +				NKern::UnlockSystem();
   1.530 +				return KErrGeneral;
   1.531 +				}
   1.532 +			NKern::UnlockSystem();
   1.533 +			}
   1.534 +		}
   1.535 +
   1.536 +	aObjectIx.Check(0);
   1.537 +
   1.538 +	for (i=0; i<aSize; i++)		//Remove
   1.539 +		{
   1.540 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.541 +		if (KErrNone != (r=aObjectIx.Remove(iObjAndHandle[i].iHandle, object, attr)))
   1.542 +			{
   1.543 +			return r;
   1.544 +			}
   1.545 +
   1.546 +		if ((i&0x1) && ((attr & RObjectIx::EReserved) == 0))
   1.547 +			{
   1.548 +			return KErrBadHandle;
   1.549 +			}
   1.550 +
   1.551 +		iObjAndHandle[i].iObject->Close(NULL);
   1.552 +		iObjAndHandle[i].iObject = NULL;
   1.553 +		}
   1.554 +
   1.555 +	aObjectIx.Check(0);
   1.556 +
   1.557 +	for (i=0; i<aSize; i++) //Add
   1.558 +		{
   1.559 +		if( (iObjAndHandle[i].iObject = new DObject) == NULL)
   1.560 +			{
   1.561 +			return KErrNoMemory;
   1.562 +			}
   1.563 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.564 +		r = aObjectIx.Add(iObjAndHandle[i].iObject, i&0x1 ?  (TUint32)RObjectIx::EReserved : 0);
   1.565 +		if (r<0)
   1.566 +			{
   1.567 +			return r;
   1.568 +			}
   1.569 +		iObjAndHandle[i].iHandle = r;
   1.570 +		}
   1.571 +
   1.572 +	//---Test data consistency---
   1.573 +	for (i=0;i<aSize;i++)
   1.574 +		{
   1.575 +		if (iObjAndHandle[i].iObject)
   1.576 +			{
   1.577 +			NKern::LockSystem();
   1.578 +			//Test At(TInt aHandle) method
   1.579 +			if (iObjAndHandle[i].iObject != aObjectIx.At(iObjAndHandle[i].iHandle))
   1.580 +				{
   1.581 +				NKern::UnlockSystem();
   1.582 +				return KErrGeneral;
   1.583 +				}
   1.584 +			NKern::UnlockSystem();
   1.585 +			}
   1.586 +		}
   1.587 +
   1.588 +	aObjectIx.Check(0);
   1.589 +
   1.590 +	for (i=aSize-1; i>=0; i--)		//Remove in reverse
   1.591 +		{
   1.592 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.593 +		if (KErrNone != (r=aObjectIx.Remove(iObjAndHandle[i].iHandle, object, attr)))
   1.594 +			{
   1.595 +			return r;
   1.596 +			}
   1.597 +		if ((i&0x1) && ((attr & RObjectIx::EReserved) == 0))
   1.598 +			{
   1.599 +			return KErrBadHandle;
   1.600 +			}
   1.601 +
   1.602 +		iObjAndHandle[i].iObject->Close(NULL);
   1.603 +
   1.604 +		aObjectIx.Reserve(-1); // Remove a reserved object causing a TidyAndCompact()...
   1.605 +		}
   1.606 +
   1.607 +	aObjectIx.Check(0);
   1.608 +
   1.609 +	return KErrNone;
   1.610 +	} // DObjectTest::RObjectIxTestExerciseIx
   1.611 +
   1.612 +
   1.613 +/**
   1.614 + *  Adds a number of DObjects to RObjectIx, tries random invalid handle to access them,
   1.615 + *  then attempts removes them in the same order.
   1.616 + * 
   1.617 + *  @param aSize  Size of handle array to use.
   1.618 + * 
   1.619 + *  @return KErrNone or standard error code.
   1.620 + */
   1.621 +TInt DObjectTest::RObjectIxInvalidHandleLookupTest(TInt aSize)
   1.622 +	{
   1.623 +	TInt  i, r;
   1.624 +	DObject*  object;
   1.625 +	RObjectIx myIx;
   1.626 +
   1.627 +	myIx.Check(0);
   1.628 +
   1.629 +	//
   1.630 +	// Add in some DObjects...
   1.631 +	//
   1.632 +	for (i = 0;  i < aSize;  i++)
   1.633 +		{
   1.634 +		if ((iObjAndHandle[i].iObject = new DObject) == NULL) 
   1.635 +			{
   1.636 +			myIx.Check(0);
   1.637 +			myIx.Close(NULL);
   1.638 +			return KErrNoMemory;
   1.639 +			}
   1.640 +
   1.641 +		iObjAndHandle[i].iObject->iContainerID = 0;
   1.642 +
   1.643 +		r = myIx.Add(iObjAndHandle[i].iObject, 0);
   1.644 +		if (r < 0)
   1.645 +			{
   1.646 +			myIx.Check(0);
   1.647 +			myIx.Close(NULL);
   1.648 +			return r;
   1.649 +			}
   1.650 +		iObjAndHandle[i].iHandle = r;
   1.651 +		}
   1.652 +
   1.653 +	myIx.Check(0);
   1.654 +
   1.655 +	//
   1.656 +	// Randomly attempt to access handles...
   1.657 +	//
   1.658 +	TInt  handlesToTest = aSize * KDObjectTestLoopCount;
   1.659 +	TInt  count;
   1.660 +	
   1.661 +	for (count = 0;  count < handlesToTest;  count++)
   1.662 +		{
   1.663 +		//
   1.664 +		// A handle looks like this:
   1.665 +		//	Bits 0-14	index
   1.666 +		//	Bit 15		no-close flag (ignored)
   1.667 +		//	Bits 16-29	instance value
   1.668 +		//	Bit 30		thread local flag (ignored)
   1.669 +		//	Bit 31		special handle flag (should be 0)
   1.670 +		//
   1.671 +		NKern::LockSystem();
   1.672 +		TInt  randomHandle = Kern::Random() & 0x3fff7fff;
   1.673 +		TInt  uniqueID = 0;		// any object type!
   1.674 +				
   1.675 +		object = myIx.At(randomHandle, uniqueID);
   1.676 +		NKern::UnlockSystem();
   1.677 +
   1.678 +		if (object != NULL)
   1.679 +			{
   1.680 +			//
   1.681 +			// We've picked a valid handle, this is unlikely but check if
   1.682 +			// it is really valid...
   1.683 +			//
   1.684 +			TBool  found = EFalse;
   1.685 +			
   1.686 +			for (i = 0;  i < aSize;  i++)
   1.687 +				{
   1.688 +				if (iObjAndHandle[i].iHandle == randomHandle  &&
   1.689 +					iObjAndHandle[i].iObject == object)
   1.690 +					{
   1.691 +					found = ETrue;
   1.692 +					break;
   1.693 +					}
   1.694 +				}
   1.695 +			
   1.696 +			if (found == EFalse) 
   1.697 +				{
   1.698 +				myIx.Check(0);
   1.699 +				myIx.Close(NULL);
   1.700 +				return KErrBadHandle;
   1.701 +				}
   1.702 +			}
   1.703 +		}
   1.704 +	
   1.705 +	myIx.Check(0);
   1.706 +
   1.707 +	//
   1.708 +	// Remove the DObjects...
   1.709 +	//
   1.710 +	for (i = 0;  i < aSize;  i++)
   1.711 +		{
   1.712 +		TUint32  attr = 0; // Receives the attributes of the removed handle...
   1.713 +
   1.714 +		if (KErrNone != (r=myIx.Remove(iObjAndHandle[i].iHandle, object, attr))) 
   1.715 +			{
   1.716 +			myIx.Check(0);
   1.717 +			myIx.Close(NULL);
   1.718 +			return r;
   1.719 +			}
   1.720 +
   1.721 +		iObjAndHandle[i].iObject->Close(NULL);
   1.722 +		}
   1.723 +
   1.724 +	myIx.Check(0);
   1.725 +	myIx.Close(NULL);
   1.726 +
   1.727 +	return KErrNone;
   1.728 +	} // DObjectTest::RObjectIxInvalidHandleLookupTest
   1.729 +
   1.730 +
   1.731 +TInt DObjectTest::DObjectNameTest()
   1.732 +	{
   1.733 +#define TEST_GOOD_NAME(name) if (Kern::ValidateName(name) != KErrNone) return KErrBadName;
   1.734 +#define TEST_BAD_NAME(name) if (Kern::ValidateName(name) != KErrBadName) return KErrBadName;
   1.735 +#define TEST_GOOD_FULLNAME(name) if (Kern::ValidateFullName(name) != KErrNone) return KErrBadName;
   1.736 +#define TEST_BAD_FULLNAME(name) if (Kern::ValidateFullName(name) != KErrBadName) return KErrBadName;
   1.737 +	
   1.738 +	
   1.739 +	_LIT(KGoodName1,"DObject1 ABCDEFGHIJKLMNOPRSTUVWXYZ0123456789");
   1.740 +	_LIT(KGoodName2,"DObject2 abdefghijklmnoprstuvwxyz!\"#$%&'()+,-./;<=>@[\\]^_`{|}~");
   1.741 +	_LIT(KGoodFullName1,"DObject :3");
   1.742 +	_LIT(KBadName1,"DObject 5 *");
   1.743 +	_LIT(KBadName2,"DObject 6 ?");
   1.744 +	TUint8 badName3[] = {'D','O','b','j','e','c','t',0x00};
   1.745 +	TUint8 badName4[] = {'D','O','b','j','e','c','t',0x1f};
   1.746 +	TUint8 badName5[] = {'D','O','b','j','e','c','t',0x7f};
   1.747 +	TUint8 badName6[] = {'D','O','b','j','e','c','t',0xff};
   1.748 +	TPtr8 badNamePtr(badName3, sizeof(badName3), sizeof(badName3));
   1.749 +	
   1.750 +	// Test Kern::ValidateName for good and bad names
   1.751 +	TEST_GOOD_NAME(KGoodName1);
   1.752 +	TEST_GOOD_NAME(KGoodName2);
   1.753 +	TEST_BAD_NAME(KGoodFullName1);
   1.754 +	TEST_BAD_NAME(KBadName1);
   1.755 +	TEST_BAD_NAME(KBadName2);
   1.756 +	TEST_BAD_NAME(badNamePtr); // already set to badName3 as no TPtr8 default constructor
   1.757 +	badNamePtr.Set(badName4, sizeof(badName4), sizeof(badName4));
   1.758 +	TEST_BAD_NAME(badNamePtr);
   1.759 +	badNamePtr.Set(badName5, sizeof(badName5), sizeof(badName5));
   1.760 +	TEST_BAD_NAME(badNamePtr);
   1.761 +	badNamePtr.Set(badName6, sizeof(badName6), sizeof(badName6));
   1.762 +	TEST_BAD_NAME(badNamePtr);
   1.763 +	
   1.764 +	// Test Kern::ValidateFullName for good and bad full names
   1.765 +	TEST_GOOD_FULLNAME(KGoodName1);
   1.766 +	TEST_GOOD_FULLNAME(KGoodName2);
   1.767 +	TEST_GOOD_FULLNAME(KGoodFullName1);
   1.768 +	TEST_BAD_FULLNAME(KBadName1);
   1.769 +	TEST_BAD_FULLNAME(KBadName2);
   1.770 +	badNamePtr.Set(badName3, sizeof(badName3), sizeof(badName3));
   1.771 +	TEST_BAD_FULLNAME(badNamePtr);
   1.772 +	badNamePtr.Set(badName4, sizeof(badName4), sizeof(badName4));
   1.773 +	TEST_BAD_FULLNAME(badNamePtr);
   1.774 +	badNamePtr.Set(badName5, sizeof(badName5), sizeof(badName5));
   1.775 +	TEST_BAD_FULLNAME(badNamePtr);
   1.776 +	badNamePtr.Set(badName6, sizeof(badName6), sizeof(badName6));
   1.777 +	TEST_BAD_FULLNAME(badNamePtr);
   1.778 +
   1.779 +	return KErrNone;
   1.780 +	}
   1.781 +
   1.782 +TInt DObjectTest::Request(TInt aFunction, TAny* a1, TAny* a2)
   1.783 +	{
   1.784 +	TInt r=KErrNone;
   1.785 +	TInt i;
   1.786 +	TInt duration;
   1.787 +	SParam param;
   1.788 +	TAny* args[2];	
   1.789 +
   1.790 +	TRequestStatus* s=(TRequestStatus*)a1;
   1.791 +	kumemget32(args,a2,sizeof(args));
   1.792 +
   1.793 +	switch (~aFunction)
   1.794 +		{
   1.795 +	case RTestDObject::ERObjectIxTest1:
   1.796 +		duration = NKern::TickCount();
   1.797 +
   1.798 +		NKern::ThreadEnterCS();
   1.799 +		for (i = 1;  i < KDObjectTestMaxTestSize;  i = i<KDObjectTestStepStart?i+1:i+KDObjectTestStep) 
   1.800 +			if (KErrNone != (r=RObjectIxTest1(i)))
   1.801 +				{
   1.802 +				NKern::ThreadLeaveCS();
   1.803 +				Kern::RequestComplete(s, r);
   1.804 +				return KErrNone;
   1.805 +				}
   1.806 +		NKern::ThreadLeaveCS();
   1.807 +
   1.808 +		duration = NKern::TickCount() - duration;
   1.809 +		kumemput32(args[0], &duration, sizeof(TInt));
   1.810 +
   1.811 +		Kern::RequestComplete(s,KErrNone);
   1.812 +		return KErrNone;
   1.813 +
   1.814 +	case RTestDObject::ERObjectIxTest2:
   1.815 +		duration = NKern::TickCount();
   1.816 +
   1.817 +		NKern::ThreadEnterCS();
   1.818 +		for (i = 1;  i < KDObjectTestMaxTestSize;  i = i<KDObjectTestStepStart?i+1:i+KDObjectTestStep) 
   1.819 +			if (KErrNone != (r=RObjectIxTest2(i)))
   1.820 +				{
   1.821 +				NKern::ThreadLeaveCS();
   1.822 +				Kern::RequestComplete(s, r);
   1.823 +				return KErrNone;
   1.824 +				}
   1.825 +
   1.826 +		NKern::ThreadLeaveCS();
   1.827 +
   1.828 +		duration = NKern::TickCount() - duration;
   1.829 +		kumemput32(args[0], &duration, sizeof(TInt));
   1.830 +
   1.831 +		Kern::RequestComplete(s,KErrNone);
   1.832 +		return KErrNone;
   1.833 +
   1.834 +	case RTestDObject::ERObjectIxTest3:
   1.835 +		kumemget32(&param, args[0], sizeof(param));
   1.836 +
   1.837 +		duration = NKern::TickCount();
   1.838 +		iSeed[0] = param.iSeed[0];
   1.839 +		iSeed[1] = param.iSeed[1];
   1.840 +		NKern::ThreadEnterCS();
   1.841 +		for (i = 1;  i < KDObjectTestMaxTestSize;  i = i<KDObjectTestStepStart?i+1:i+KDObjectTestStep) 
   1.842 +			if (KErrNone != (r=RObjectIxTest3(i, param.iPerformanceTest)))
   1.843 +				{
   1.844 +				NKern::ThreadLeaveCS();
   1.845 +				Kern::RequestComplete(s, r);
   1.846 +				return KErrNone;
   1.847 +				}
   1.848 +		NKern::ThreadLeaveCS();
   1.849 +		
   1.850 +		duration = NKern::TickCount() - duration;
   1.851 +		kumemput32(&((SParam*)args[0])->duration, &duration, sizeof(TInt));
   1.852 +
   1.853 +		Kern::RequestComplete(s,KErrNone);
   1.854 +		return KErrNone;
   1.855 +
   1.856 +	case RTestDObject::ERObjectIxTest4:
   1.857 +		duration = NKern::TickCount();
   1.858 +
   1.859 +		NKern::ThreadEnterCS();
   1.860 +		for (i = 1;  i < KDObjectTestMaxTestSize;  i = i<KDObjectTestStepStart?i+1:i+KDObjectTestStep) 
   1.861 +			if (KErrNone != (r=RObjectIxTest4(i)))
   1.862 +				{
   1.863 +				NKern::ThreadLeaveCS();
   1.864 +				Kern::RequestComplete(s, r);
   1.865 +				return KErrNone;
   1.866 +				}
   1.867 +		NKern::ThreadLeaveCS();
   1.868 +
   1.869 +		duration = NKern::TickCount() - duration;
   1.870 +		kumemput32(args[0], &duration, sizeof(TInt));
   1.871 +
   1.872 +		Kern::RequestComplete(s,KErrNone);
   1.873 +		return KErrNone;
   1.874 +
   1.875 +	case RTestDObject::ERObjectIxThreadTestCreateIx:
   1.876 +		//RTestDObject::RObjectIxThreadTestCreateIx(void*& aRObjectIxPtr);
   1.877 +		{
   1.878 +		RObjectIx*  threadTestObjectIx = NULL;
   1.879 +		
   1.880 +		NKern::ThreadEnterCS();
   1.881 +		threadTestObjectIx = new RObjectIx();
   1.882 +		if (threadTestObjectIx == NULL)
   1.883 +			{
   1.884 +			NKern::ThreadLeaveCS();
   1.885 +			r = KErrNoMemory;
   1.886 +			}
   1.887 +		else
   1.888 +			{
   1.889 +			NKern::ThreadLeaveCS();
   1.890 +			
   1.891 +			kumemput32(args[0], &threadTestObjectIx, sizeof(RObjectIx*));
   1.892 +			r = KErrNone;
   1.893 +			}
   1.894 +		Kern::RequestComplete(s, r);
   1.895 +		r = KErrNone;
   1.896 +		}
   1.897 +		break;
   1.898 +		
   1.899 +	case RTestDObject::ERObjectIxThreadTestExerciseIx:
   1.900 +		//RTestDObject::RObjectIxThreadTestExerciseIx(void* aRObjectIxPtr);
   1.901 +		{
   1.902 +		RObjectIx*  threadTestObjectIx = (RObjectIx*) args[0];
   1.903 +
   1.904 +		NKern::ThreadEnterCS();
   1.905 +		for (i = 1;  i < KDObjectTestMaxTestSize;  i = i<KDObjectTestStepStart?i+1:i+KDObjectTestStep) 
   1.906 +			{
   1.907 +			if (KErrNone != (r=RObjectIxTestExerciseIx(*threadTestObjectIx, i)))
   1.908 +				{
   1.909 +				NKern::ThreadLeaveCS();
   1.910 +				Kern::RequestComplete(s, r);
   1.911 +				return KErrNone;
   1.912 +				}
   1.913 +			}
   1.914 +		NKern::ThreadLeaveCS();
   1.915 +
   1.916 +		Kern::RequestComplete(s, KErrNone);
   1.917 +		r = KErrNone;
   1.918 +		}
   1.919 +		break;
   1.920 +		
   1.921 +	case RTestDObject::ERObjectIxThreadTestFreeIx:
   1.922 +		//RTestDObject::RObjectIxThreadTestFreeIx(void* aRObjectIxPtr)
   1.923 +		{
   1.924 +		RObjectIx*  threadTestObjectIx = (RObjectIx*) args[0];
   1.925 +		
   1.926 +		NKern::ThreadEnterCS();
   1.927 +		threadTestObjectIx->Check(0);
   1.928 +		threadTestObjectIx->Close(NULL);
   1.929 +		delete threadTestObjectIx;
   1.930 +		threadTestObjectIx = NULL;
   1.931 +		NKern::ThreadLeaveCS();
   1.932 +		
   1.933 +		Kern::RequestComplete(s, KErrNone);
   1.934 +		r = KErrNone;
   1.935 +		}
   1.936 +		break;
   1.937 +		
   1.938 +	case RTestDObject::ERObjectIxInvalidHandleLookupTest:
   1.939 +		//RTestDObject::InvalidHandleLookupTest()
   1.940 +		{
   1.941 +		NKern::ThreadEnterCS();
   1.942 +		for (i = 1;  i < KDObjectTestMaxTestSize;  i = i<KDObjectTestStepStart?i+1:i+KDObjectTestStep) 
   1.943 +			{
   1.944 +			if (KErrNone != (r=RObjectIxInvalidHandleLookupTest(i)))
   1.945 +				{
   1.946 +				NKern::ThreadLeaveCS();
   1.947 +				Kern::RequestComplete(s, r);
   1.948 +				return KErrNone;
   1.949 +				}
   1.950 +			}
   1.951 +		NKern::ThreadLeaveCS();
   1.952 +
   1.953 +		Kern::RequestComplete(s, r);
   1.954 +		}
   1.955 +		break;
   1.956 +		
   1.957 +	case RTestDObject::EDObjectNameTest:
   1.958 +		
   1.959 +		r = DObjectNameTest();
   1.960 +		Kern::RequestComplete(s, r);
   1.961 +		break;
   1.962 +		
   1.963 +	default:
   1.964 +		r = KErrNotSupported;
   1.965 +		break;
   1.966 +		}
   1.967 +	return r;
   1.968 +	}