os/kernelhwsrv/kerneltest/e32test/system/t_env.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/system/t_env.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,414 @@
     1.4 +// Copyright (c) 1994-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\t_env.cpp
    1.18 +// Overview:
    1.19 +// Test RProcess parameters
    1.20 +// API Information:
    1.21 +// RProcess
    1.22 +// Details:
    1.23 +// - Create a thread that causes a variety of panics. Verify that the exit
    1.24 +// reason is as expected.
    1.25 +// - Create a process that causes a variety of panics. Verify that the results
    1.26 +// are as expected.
    1.27 +// - Test passing 16 bit and 8 bit descriptors to another process. Verify the 
    1.28 +// text and other results are as expected.
    1.29 +// - Verify that an invalid read of a descriptor by a process causes a panic.
    1.30 +// - Test passing zero length data to a separate process works as expected.
    1.31 +// - Test that passing each of a mutex, semaphore, file handle and chunk to a 
    1.32 +// separate process works as expected.
    1.33 +// Platforms/Drives/Compatibility:
    1.34 +// All.
    1.35 +// Assumptions/Requirement/Pre-requisites:
    1.36 +// Failures and causes:
    1.37 +// Base Port information:
    1.38 +// 
    1.39 +//
    1.40 +
    1.41 +#include <e32std.h>
    1.42 +#include <e32std_private.h>
    1.43 +#include <e32test.h>
    1.44 +#include <e32panic.h>
    1.45 +#include <e32msgqueue.h>
    1.46 +#include <d32comm.h>
    1.47 +#include <f32file.h>
    1.48 +
    1.49 +#if defined (__WINS__)
    1.50 +#define PDD_NAME _L("ECDRV.PDD")
    1.51 +#define LDD_NAME _L("ECOMM.LDD")
    1.52 +#else
    1.53 +#define PDD_NAME _L("EUART")
    1.54 +#define LDD_NAME _L("ECOMM")
    1.55 +#endif
    1.56 +
    1.57 +
    1.58 +LOCAL_D RTest test(_L("T_ENV"));
    1.59 +
    1.60 +
    1.61 +_LIT(KProcName, "t_env_child.exe");
    1.62 +_LIT(KThreadName, "t_env_panic_thread");
    1.63 +_LIT(KQueueName, "testqueue");
    1.64 +_LIT(KMutexName, "testmutex");
    1.65 +_LIT(KSemName, "testsemaphore");
    1.66 +_LIT(KChunkName, "testchunk");
    1.67 +_LIT(KFileName, "c:\\testfile");
    1.68 +
    1.69 +const TInt KHeapSize=0x2000;
    1.70 +
    1.71 +
    1.72 +
    1.73 +class TData 
    1.74 +	{
    1.75 +public:
    1.76 +	TData(TInt aTest, RProcess& aProcess);
    1.77 +	TInt iTest;
    1.78 +	RProcess& iProcess;
    1.79 +	};
    1.80 +
    1.81 +TData::TData(TInt aTest, RProcess&  aProcess) :	iTest(aTest), iProcess(aProcess)
    1.82 +	{
    1.83 +	//empty
    1.84 +	};
    1.85 +
    1.86 +
    1.87 +class Handles
    1.88 +	{
    1.89 +	public:
    1.90 +	void SetupParameters(RProcess& aNewProcess);
    1.91 +	Handles();
    1.92 +	~Handles();
    1.93 +	void Command(TInt aCommand);
    1.94 +
    1.95 +	public:
    1.96 +	RMsgQueue<TInt> iCommandQueue;
    1.97 +	RMsgQueue<TInt> iIntQueue;
    1.98 +	RMutex iMutex;
    1.99 +	RSemaphore iSem;
   1.100 +	RChunk iChunk;
   1.101 +	RFile iFile;
   1.102 +	RFs iSession;
   1.103 +	};
   1.104 +
   1.105 +void Handles::Command(TInt aC)
   1.106 +	{
   1.107 +	iCommandQueue.SendBlocking(aC);
   1.108 +	}
   1.109 +
   1.110 +void Handles::SetupParameters(RProcess& aNewProcess)
   1.111 +	{
   1.112 +	aNewProcess.SetParameter(1, iCommandQueue);
   1.113 +	aNewProcess.SetParameter(2, iIntQueue);
   1.114 +	aNewProcess.SetParameter(3, iMutex);
   1.115 +	aNewProcess.SetParameter(4, iSem);
   1.116 +	aNewProcess.SetParameter(5, iChunk);
   1.117 +	aNewProcess.SetParameter(7, iSession);
   1.118 +	aNewProcess.SetParameter(8, iFile);
   1.119 +	}
   1.120 +
   1.121 +
   1.122 +
   1.123 +_LIT8(KTestData,"test data");
   1.124 +
   1.125 +Handles::Handles()
   1.126 +	{
   1.127 +	TInt ret = iCommandQueue.CreateGlobal(KNullDesC, 10, EOwnerProcess);
   1.128 +	test(ret == KErrNone);
   1.129 +
   1.130 +	ret = iIntQueue.CreateGlobal(KQueueName,1);
   1.131 +	test(ret == KErrNone);
   1.132 +	
   1.133 +	ret = iMutex.CreateGlobal(KMutexName);
   1.134 +	test(ret == KErrNone);
   1.135 +
   1.136 +	ret = iSem.CreateGlobal(KSemName,0);
   1.137 +	test(ret == KErrNone);
   1.138 +
   1.139 +	ret = iChunk.CreateGlobal(KChunkName, 1024, 2048);
   1.140 +	test(ret == KErrNone);
   1.141 +
   1.142 +	ret = iSession.Connect();
   1.143 +	test(ret == KErrNone);
   1.144 +
   1.145 +	ret = iSession.ShareProtected();
   1.146 +	test(ret == KErrNone);
   1.147 +
   1.148 +	ret = iFile.Open(iSession, KFileName, EFileStreamText|EFileWrite|EFileShareAny); 
   1.149 +	if (ret == KErrNotFound) // file does not exist - create it 
   1.150 +		ret = iFile.Create(iSession, KFileName, EFileStreamText|EFileWrite|EFileShareAny);
   1.151 +	test(ret == KErrNone);
   1.152 +	ret = iFile.Write(0, KTestData);
   1.153 +	test(ret == KErrNone);
   1.154 +	}
   1.155 +
   1.156 +Handles::~Handles()
   1.157 +	{
   1.158 +	iCommandQueue.Close();
   1.159 +	iIntQueue.Close();
   1.160 +	iMutex.Close();
   1.161 +	iSem.Close();
   1.162 +	iChunk.Close();
   1.163 +	iSession.Close();
   1.164 +	}
   1.165 +
   1.166 +
   1.167 +LOCAL_C TInt testSetParameterPanics(TAny* aData)
   1.168 +	{
   1.169 +	const TData* data = (const TData*)aData;
   1.170 +	switch (data->iTest)
   1.171 +		{
   1.172 +		case 0:	//try and pass a non local handle
   1.173 +			{
   1.174 +			RMsgQueue<TInt> localMsgQueue;
   1.175 +			localMsgQueue.CreateLocal(1);
   1.176 +			data->iProcess.SetParameter(1, localMsgQueue);	//should panic with plat security panic
   1.177 +			break;
   1.178 +			}
   1.179 +		
   1.180 +		case 1:	//out of range slot
   1.181 +			{
   1.182 +			RMsgQueue<TInt> globalMsgQueue;
   1.183 +			globalMsgQueue.CreateGlobal(KNullDesC, 1);
   1.184 +			data->iProcess.SetParameter(-1, globalMsgQueue);	//should panic with range error
   1.185 +			break;
   1.186 +			}
   1.187 +
   1.188 +		case 2:
   1.189 +			{
   1.190 +			RMsgQueue<TInt> globalMsgQueue;
   1.191 +			globalMsgQueue.CreateGlobal(KNullDesC, 1);
   1.192 +			data->iProcess.SetParameter(1234, globalMsgQueue);	//should panic with range error
   1.193 +			break;
   1.194 +			}
   1.195 +		
   1.196 +		case 3:	//in use 
   1.197 +			{
   1.198 +			RMsgQueue<TInt> globalMsgQueue;
   1.199 +			globalMsgQueue.CreateGlobal(KNullDesC, 1);
   1.200 +			data->iProcess.SetParameter(1, globalMsgQueue);
   1.201 +			data->iProcess.SetParameter(1, globalMsgQueue);	//panic, in use
   1.202 +			break;
   1.203 +			}
   1.204 +
   1.205 +		case 4:
   1.206 +			{
   1.207 +			TPtrC8 bad((const TUint8*)0xfeed,4);
   1.208 +			data->iProcess.SetParameter(1, bad);
   1.209 +			break;
   1.210 +			}
   1.211 +
   1.212 +		case 5:	//slot 0 is for the command line
   1.213 +			{
   1.214 +			RMsgQueue<TInt> globalMsgQueue;
   1.215 +			globalMsgQueue.CreateGlobal(KNullDesC, 1);
   1.216 +			data->iProcess.SetParameter(0, globalMsgQueue);	//panic, in use
   1.217 +			break;
   1.218 +			}
   1.219 +
   1.220 +		}
   1.221 +	return KErrNone;
   1.222 +	}
   1.223 +
   1.224 +
   1.225 +
   1.226 +
   1.227 +GLDEF_C TInt E32Main()
   1.228 +    {
   1.229 +
   1.230 +	test.Title();
   1.231 +	test.Start(_L("Process Parameters"));
   1.232 +
   1.233 +	test.Next(_L("Test Panics on a set"));
   1.234 +
   1.235 +	TBool jit = User::JustInTime();
   1.236 +	User::SetJustInTime(EFalse);
   1.237 +	TInt i;
   1.238 +	RProcess p;
   1.239 +	TInt ret = p.Create(KProcName, KProcName);
   1.240 +	test(ret == KErrNone);
   1.241 +	for (i = 0; i < 6; i++)
   1.242 +		{
   1.243 +		test.Printf(_L("panic test number %d\n"), i);
   1.244 +		TData data(i, p);
   1.245 +		RThread thread;
   1.246 +		ret = thread.Create(KThreadName, testSetParameterPanics, KDefaultStackSize, KHeapSize, KHeapSize, &data);
   1.247 +		test(KErrNone == ret);
   1.248 +		TRequestStatus stat;
   1.249 +		thread.Logon(stat);
   1.250 +		thread.Resume();
   1.251 +		User::WaitForRequest(stat);
   1.252 +		test.Printf(_L("exit type is %d, stat is %d"), thread.ExitType(), stat.Int());
   1.253 +		test (thread.ExitType() == EExitPanic);
   1.254 +		switch (i)
   1.255 +			{
   1.256 +			case 0:
   1.257 +			test (thread.ExitReason() == EPlatformSecurityTrap);
   1.258 +			break;
   1.259 +
   1.260 +			case 1:
   1.261 +			case 2:
   1.262 +			test (thread.ExitReason() == EParameterSlotRange);
   1.263 +			break;
   1.264 +
   1.265 +			case 3:
   1.266 +			case 5:
   1.267 +			test (thread.ExitReason() == EParameterSlotInUse);
   1.268 +			break;
   1.269 +
   1.270 +			case 4:
   1.271 +			test (thread.ExitReason() == ECausedException);
   1.272 +			break;
   1.273 +			}
   1.274 +		CLOSE_AND_WAIT(thread);
   1.275 +		}
   1.276 +	p.Kill(0);	
   1.277 +	CLOSE_AND_WAIT(p);
   1.278 +	
   1.279 +	test.Next(_L("launch panicing process"));
   1.280 +
   1.281 +	
   1.282 +	Handles h;	
   1.283 +	
   1.284 +	TRequestStatus stat;
   1.285 +	for (i = 0; i < 1; i++)
   1.286 +		{
   1.287 +		h.Command(i);
   1.288 +		ret = p.Create(KProcName, KNullDesC);
   1.289 +		test(ret == KErrNone);
   1.290 +		p.Logon(stat);
   1.291 +		h.SetupParameters(p);
   1.292 +		p.Resume();
   1.293 +		User::WaitForRequest(stat);
   1.294 +		test(p.ExitType()==EExitPanic);
   1.295 +		test(p.ExitReason()==EParameterSlotRange);
   1.296 +		CLOSE_AND_WAIT(p);
   1.297 +		}
   1.298 +	User::SetJustInTime(jit);
   1.299 +
   1.300 +	test.Next(_L("test 16 bit descriptor"));
   1.301 +	h.Command(8);
   1.302 +	ret = p.Create(KProcName, KNullDesC);
   1.303 +	test(ret == KErrNone);
   1.304 +	p.Logon(stat);
   1.305 +	h.SetupParameters(p);
   1.306 +	p.SetParameter(15, _L("16 bit text"));
   1.307 +	p.Resume();
   1.308 +	User::WaitForRequest(stat);
   1.309 +	test(p.ExitType()==EExitKill);
   1.310 +	test(p.ExitReason()==KErrNone);
   1.311 +	CLOSE_AND_WAIT(p);
   1.312 +
   1.313 +	test.Next(_L("test 8 bit descriptor"));
   1.314 +	h.Command(9);
   1.315 +	ret = p.Create(KProcName, KNullDesC);
   1.316 +	test(ret == KErrNone);
   1.317 +	p.Logon(stat);
   1.318 +	h.SetupParameters(p);
   1.319 +	p.SetParameter(15, _L8("8 bit text"));
   1.320 +	p.Resume();
   1.321 +	User::WaitForRequest(stat);
   1.322 +	test(p.ExitType()==EExitKill);
   1.323 +	test(p.ExitReason()==KErrNone);
   1.324 +	CLOSE_AND_WAIT(p);
   1.325 +
   1.326 +
   1.327 +	test.Next(_L("test bad read of descriptor"));
   1.328 +	h.Command(10);
   1.329 +	ret = p.Create(KProcName, KNullDesC);
   1.330 +	test(ret == KErrNone);
   1.331 +	p.Logon(stat);
   1.332 +	h.SetupParameters(p);
   1.333 +	p.SetParameter(15, _L8("aa"));
   1.334 +	p.Resume();
   1.335 +	User::WaitForRequest(stat);
   1.336 +	test(p.ExitType()==EExitPanic);
   1.337 +	test(p.ExitReason()==ECausedException);
   1.338 +	CLOSE_AND_WAIT(p);
   1.339 +
   1.340 +	test.Next(_L("test zero length data"));
   1.341 +	h.Command(11);
   1.342 +	ret = p.Create(KProcName, KNullDesC);
   1.343 +	test(ret == KErrNone);
   1.344 +	p.Logon(stat);
   1.345 +	h.SetupParameters(p);
   1.346 +	p.SetParameter(15, KNullDesC);
   1.347 +	p.Resume();
   1.348 +	User::WaitForRequest(stat);
   1.349 +	test(p.ExitType()==EExitKill);
   1.350 +	test(p.ExitReason()==KErrNone);
   1.351 +	CLOSE_AND_WAIT(p);
   1.352 +
   1.353 +	test.Next(_L("test reserved command line"));
   1.354 +	h.Command(12);
   1.355 +	ret = p.Create(KProcName, KNullDesC);
   1.356 +	test(ret == KErrNone);
   1.357 +	p.Logon(stat);
   1.358 +	h.SetupParameters(p);
   1.359 +	p.Resume();
   1.360 +	User::WaitForRequest(stat);
   1.361 +	test(p.ExitType()==EExitKill);
   1.362 +	test(p.ExitReason()==KErrNone);
   1.363 +	CLOSE_AND_WAIT(p);
   1.364 +
   1.365 +
   1.366 +	test.Next(_L("test mutex"));
   1.367 +	h.Command(4);
   1.368 +	ret = p.Create(KProcName, KNullDesC);
   1.369 +	test(ret == KErrNone);
   1.370 +	p.Logon(stat);
   1.371 +	h.SetupParameters(p);
   1.372 +	p.Resume();
   1.373 +	User::WaitForRequest(stat);
   1.374 +	test(p.ExitType()==EExitKill);
   1.375 +	test(p.ExitReason()==KErrNone);
   1.376 +	CLOSE_AND_WAIT(p);
   1.377 +
   1.378 +	
   1.379 +	test.Next(_L("test semaphore"));
   1.380 +	h.Command(5);
   1.381 +	ret = p.Create(KProcName, KNullDesC);
   1.382 +	test(ret == KErrNone);
   1.383 +	p.Logon(stat);
   1.384 +	h.SetupParameters(p);
   1.385 +	p.Resume();
   1.386 +	User::WaitForRequest(stat);
   1.387 +	test(p.ExitType()==EExitKill);
   1.388 +	test(p.ExitReason()==KErrNone);
   1.389 +	CLOSE_AND_WAIT(p);
   1.390 +
   1.391 +	test.Next(_L("test file handle"));
   1.392 +	h.Command(6);
   1.393 +	ret = p.Create(KProcName, KNullDesC);
   1.394 +	test(ret == KErrNone);
   1.395 +	p.Logon(stat);
   1.396 +	h.SetupParameters(p);
   1.397 +	p.Resume();
   1.398 +	User::WaitForRequest(stat);
   1.399 +	test(p.ExitType()==EExitKill);
   1.400 +	test(p.ExitReason()==KErrNone);
   1.401 +	CLOSE_AND_WAIT(p);
   1.402 +
   1.403 +	test.Next(_L("test chunk"));
   1.404 +	h.Command(7);
   1.405 +	ret = p.Create(KProcName, KNullDesC);
   1.406 +	test(ret == KErrNone);
   1.407 +	p.Logon(stat);
   1.408 +	h.SetupParameters(p);
   1.409 +	p.Resume();
   1.410 +	User::WaitForRequest(stat);
   1.411 +	test(p.ExitType()==EExitKill);
   1.412 +	test(p.ExitReason()==KErrNone);
   1.413 +	CLOSE_AND_WAIT(p);
   1.414 +
   1.415 +	test.End();
   1.416 +	return 0;
   1.417 +    }