os/kernelhwsrv/kerneltest/e32test/system/t_env.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\system\t_env.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test RProcess parameters
sl@0
    17
// API Information:
sl@0
    18
// RProcess
sl@0
    19
// Details:
sl@0
    20
// - Create a thread that causes a variety of panics. Verify that the exit
sl@0
    21
// reason is as expected.
sl@0
    22
// - Create a process that causes a variety of panics. Verify that the results
sl@0
    23
// are as expected.
sl@0
    24
// - Test passing 16 bit and 8 bit descriptors to another process. Verify the 
sl@0
    25
// text and other results are as expected.
sl@0
    26
// - Verify that an invalid read of a descriptor by a process causes a panic.
sl@0
    27
// - Test passing zero length data to a separate process works as expected.
sl@0
    28
// - Test that passing each of a mutex, semaphore, file handle and chunk to a 
sl@0
    29
// separate process works as expected.
sl@0
    30
// Platforms/Drives/Compatibility:
sl@0
    31
// All.
sl@0
    32
// Assumptions/Requirement/Pre-requisites:
sl@0
    33
// Failures and causes:
sl@0
    34
// Base Port information:
sl@0
    35
// 
sl@0
    36
//
sl@0
    37
sl@0
    38
#include <e32std.h>
sl@0
    39
#include <e32std_private.h>
sl@0
    40
#include <e32test.h>
sl@0
    41
#include <e32panic.h>
sl@0
    42
#include <e32msgqueue.h>
sl@0
    43
#include <d32comm.h>
sl@0
    44
#include <f32file.h>
sl@0
    45
sl@0
    46
#if defined (__WINS__)
sl@0
    47
#define PDD_NAME _L("ECDRV.PDD")
sl@0
    48
#define LDD_NAME _L("ECOMM.LDD")
sl@0
    49
#else
sl@0
    50
#define PDD_NAME _L("EUART")
sl@0
    51
#define LDD_NAME _L("ECOMM")
sl@0
    52
#endif
sl@0
    53
sl@0
    54
sl@0
    55
LOCAL_D RTest test(_L("T_ENV"));
sl@0
    56
sl@0
    57
sl@0
    58
_LIT(KProcName, "t_env_child.exe");
sl@0
    59
_LIT(KThreadName, "t_env_panic_thread");
sl@0
    60
_LIT(KQueueName, "testqueue");
sl@0
    61
_LIT(KMutexName, "testmutex");
sl@0
    62
_LIT(KSemName, "testsemaphore");
sl@0
    63
_LIT(KChunkName, "testchunk");
sl@0
    64
_LIT(KFileName, "c:\\testfile");
sl@0
    65
sl@0
    66
const TInt KHeapSize=0x2000;
sl@0
    67
sl@0
    68
sl@0
    69
sl@0
    70
class TData 
sl@0
    71
	{
sl@0
    72
public:
sl@0
    73
	TData(TInt aTest, RProcess& aProcess);
sl@0
    74
	TInt iTest;
sl@0
    75
	RProcess& iProcess;
sl@0
    76
	};
sl@0
    77
sl@0
    78
TData::TData(TInt aTest, RProcess&  aProcess) :	iTest(aTest), iProcess(aProcess)
sl@0
    79
	{
sl@0
    80
	//empty
sl@0
    81
	};
sl@0
    82
sl@0
    83
sl@0
    84
class Handles
sl@0
    85
	{
sl@0
    86
	public:
sl@0
    87
	void SetupParameters(RProcess& aNewProcess);
sl@0
    88
	Handles();
sl@0
    89
	~Handles();
sl@0
    90
	void Command(TInt aCommand);
sl@0
    91
sl@0
    92
	public:
sl@0
    93
	RMsgQueue<TInt> iCommandQueue;
sl@0
    94
	RMsgQueue<TInt> iIntQueue;
sl@0
    95
	RMutex iMutex;
sl@0
    96
	RSemaphore iSem;
sl@0
    97
	RChunk iChunk;
sl@0
    98
	RFile iFile;
sl@0
    99
	RFs iSession;
sl@0
   100
	};
sl@0
   101
sl@0
   102
void Handles::Command(TInt aC)
sl@0
   103
	{
sl@0
   104
	iCommandQueue.SendBlocking(aC);
sl@0
   105
	}
sl@0
   106
sl@0
   107
void Handles::SetupParameters(RProcess& aNewProcess)
sl@0
   108
	{
sl@0
   109
	aNewProcess.SetParameter(1, iCommandQueue);
sl@0
   110
	aNewProcess.SetParameter(2, iIntQueue);
sl@0
   111
	aNewProcess.SetParameter(3, iMutex);
sl@0
   112
	aNewProcess.SetParameter(4, iSem);
sl@0
   113
	aNewProcess.SetParameter(5, iChunk);
sl@0
   114
	aNewProcess.SetParameter(7, iSession);
sl@0
   115
	aNewProcess.SetParameter(8, iFile);
sl@0
   116
	}
sl@0
   117
sl@0
   118
sl@0
   119
sl@0
   120
_LIT8(KTestData,"test data");
sl@0
   121
sl@0
   122
Handles::Handles()
sl@0
   123
	{
sl@0
   124
	TInt ret = iCommandQueue.CreateGlobal(KNullDesC, 10, EOwnerProcess);
sl@0
   125
	test(ret == KErrNone);
sl@0
   126
sl@0
   127
	ret = iIntQueue.CreateGlobal(KQueueName,1);
sl@0
   128
	test(ret == KErrNone);
sl@0
   129
	
sl@0
   130
	ret = iMutex.CreateGlobal(KMutexName);
sl@0
   131
	test(ret == KErrNone);
sl@0
   132
sl@0
   133
	ret = iSem.CreateGlobal(KSemName,0);
sl@0
   134
	test(ret == KErrNone);
sl@0
   135
sl@0
   136
	ret = iChunk.CreateGlobal(KChunkName, 1024, 2048);
sl@0
   137
	test(ret == KErrNone);
sl@0
   138
sl@0
   139
	ret = iSession.Connect();
sl@0
   140
	test(ret == KErrNone);
sl@0
   141
sl@0
   142
	ret = iSession.ShareProtected();
sl@0
   143
	test(ret == KErrNone);
sl@0
   144
sl@0
   145
	ret = iFile.Open(iSession, KFileName, EFileStreamText|EFileWrite|EFileShareAny); 
sl@0
   146
	if (ret == KErrNotFound) // file does not exist - create it 
sl@0
   147
		ret = iFile.Create(iSession, KFileName, EFileStreamText|EFileWrite|EFileShareAny);
sl@0
   148
	test(ret == KErrNone);
sl@0
   149
	ret = iFile.Write(0, KTestData);
sl@0
   150
	test(ret == KErrNone);
sl@0
   151
	}
sl@0
   152
sl@0
   153
Handles::~Handles()
sl@0
   154
	{
sl@0
   155
	iCommandQueue.Close();
sl@0
   156
	iIntQueue.Close();
sl@0
   157
	iMutex.Close();
sl@0
   158
	iSem.Close();
sl@0
   159
	iChunk.Close();
sl@0
   160
	iSession.Close();
sl@0
   161
	}
sl@0
   162
sl@0
   163
sl@0
   164
LOCAL_C TInt testSetParameterPanics(TAny* aData)
sl@0
   165
	{
sl@0
   166
	const TData* data = (const TData*)aData;
sl@0
   167
	switch (data->iTest)
sl@0
   168
		{
sl@0
   169
		case 0:	//try and pass a non local handle
sl@0
   170
			{
sl@0
   171
			RMsgQueue<TInt> localMsgQueue;
sl@0
   172
			localMsgQueue.CreateLocal(1);
sl@0
   173
			data->iProcess.SetParameter(1, localMsgQueue);	//should panic with plat security panic
sl@0
   174
			break;
sl@0
   175
			}
sl@0
   176
		
sl@0
   177
		case 1:	//out of range slot
sl@0
   178
			{
sl@0
   179
			RMsgQueue<TInt> globalMsgQueue;
sl@0
   180
			globalMsgQueue.CreateGlobal(KNullDesC, 1);
sl@0
   181
			data->iProcess.SetParameter(-1, globalMsgQueue);	//should panic with range error
sl@0
   182
			break;
sl@0
   183
			}
sl@0
   184
sl@0
   185
		case 2:
sl@0
   186
			{
sl@0
   187
			RMsgQueue<TInt> globalMsgQueue;
sl@0
   188
			globalMsgQueue.CreateGlobal(KNullDesC, 1);
sl@0
   189
			data->iProcess.SetParameter(1234, globalMsgQueue);	//should panic with range error
sl@0
   190
			break;
sl@0
   191
			}
sl@0
   192
		
sl@0
   193
		case 3:	//in use 
sl@0
   194
			{
sl@0
   195
			RMsgQueue<TInt> globalMsgQueue;
sl@0
   196
			globalMsgQueue.CreateGlobal(KNullDesC, 1);
sl@0
   197
			data->iProcess.SetParameter(1, globalMsgQueue);
sl@0
   198
			data->iProcess.SetParameter(1, globalMsgQueue);	//panic, in use
sl@0
   199
			break;
sl@0
   200
			}
sl@0
   201
sl@0
   202
		case 4:
sl@0
   203
			{
sl@0
   204
			TPtrC8 bad((const TUint8*)0xfeed,4);
sl@0
   205
			data->iProcess.SetParameter(1, bad);
sl@0
   206
			break;
sl@0
   207
			}
sl@0
   208
sl@0
   209
		case 5:	//slot 0 is for the command line
sl@0
   210
			{
sl@0
   211
			RMsgQueue<TInt> globalMsgQueue;
sl@0
   212
			globalMsgQueue.CreateGlobal(KNullDesC, 1);
sl@0
   213
			data->iProcess.SetParameter(0, globalMsgQueue);	//panic, in use
sl@0
   214
			break;
sl@0
   215
			}
sl@0
   216
sl@0
   217
		}
sl@0
   218
	return KErrNone;
sl@0
   219
	}
sl@0
   220
sl@0
   221
sl@0
   222
sl@0
   223
sl@0
   224
GLDEF_C TInt E32Main()
sl@0
   225
    {
sl@0
   226
sl@0
   227
	test.Title();
sl@0
   228
	test.Start(_L("Process Parameters"));
sl@0
   229
sl@0
   230
	test.Next(_L("Test Panics on a set"));
sl@0
   231
sl@0
   232
	TBool jit = User::JustInTime();
sl@0
   233
	User::SetJustInTime(EFalse);
sl@0
   234
	TInt i;
sl@0
   235
	RProcess p;
sl@0
   236
	TInt ret = p.Create(KProcName, KProcName);
sl@0
   237
	test(ret == KErrNone);
sl@0
   238
	for (i = 0; i < 6; i++)
sl@0
   239
		{
sl@0
   240
		test.Printf(_L("panic test number %d\n"), i);
sl@0
   241
		TData data(i, p);
sl@0
   242
		RThread thread;
sl@0
   243
		ret = thread.Create(KThreadName, testSetParameterPanics, KDefaultStackSize, KHeapSize, KHeapSize, &data);
sl@0
   244
		test(KErrNone == ret);
sl@0
   245
		TRequestStatus stat;
sl@0
   246
		thread.Logon(stat);
sl@0
   247
		thread.Resume();
sl@0
   248
		User::WaitForRequest(stat);
sl@0
   249
		test.Printf(_L("exit type is %d, stat is %d"), thread.ExitType(), stat.Int());
sl@0
   250
		test (thread.ExitType() == EExitPanic);
sl@0
   251
		switch (i)
sl@0
   252
			{
sl@0
   253
			case 0:
sl@0
   254
			test (thread.ExitReason() == EPlatformSecurityTrap);
sl@0
   255
			break;
sl@0
   256
sl@0
   257
			case 1:
sl@0
   258
			case 2:
sl@0
   259
			test (thread.ExitReason() == EParameterSlotRange);
sl@0
   260
			break;
sl@0
   261
sl@0
   262
			case 3:
sl@0
   263
			case 5:
sl@0
   264
			test (thread.ExitReason() == EParameterSlotInUse);
sl@0
   265
			break;
sl@0
   266
sl@0
   267
			case 4:
sl@0
   268
			test (thread.ExitReason() == ECausedException);
sl@0
   269
			break;
sl@0
   270
			}
sl@0
   271
		CLOSE_AND_WAIT(thread);
sl@0
   272
		}
sl@0
   273
	p.Kill(0);	
sl@0
   274
	CLOSE_AND_WAIT(p);
sl@0
   275
	
sl@0
   276
	test.Next(_L("launch panicing process"));
sl@0
   277
sl@0
   278
	
sl@0
   279
	Handles h;	
sl@0
   280
	
sl@0
   281
	TRequestStatus stat;
sl@0
   282
	for (i = 0; i < 1; i++)
sl@0
   283
		{
sl@0
   284
		h.Command(i);
sl@0
   285
		ret = p.Create(KProcName, KNullDesC);
sl@0
   286
		test(ret == KErrNone);
sl@0
   287
		p.Logon(stat);
sl@0
   288
		h.SetupParameters(p);
sl@0
   289
		p.Resume();
sl@0
   290
		User::WaitForRequest(stat);
sl@0
   291
		test(p.ExitType()==EExitPanic);
sl@0
   292
		test(p.ExitReason()==EParameterSlotRange);
sl@0
   293
		CLOSE_AND_WAIT(p);
sl@0
   294
		}
sl@0
   295
	User::SetJustInTime(jit);
sl@0
   296
sl@0
   297
	test.Next(_L("test 16 bit descriptor"));
sl@0
   298
	h.Command(8);
sl@0
   299
	ret = p.Create(KProcName, KNullDesC);
sl@0
   300
	test(ret == KErrNone);
sl@0
   301
	p.Logon(stat);
sl@0
   302
	h.SetupParameters(p);
sl@0
   303
	p.SetParameter(15, _L("16 bit text"));
sl@0
   304
	p.Resume();
sl@0
   305
	User::WaitForRequest(stat);
sl@0
   306
	test(p.ExitType()==EExitKill);
sl@0
   307
	test(p.ExitReason()==KErrNone);
sl@0
   308
	CLOSE_AND_WAIT(p);
sl@0
   309
sl@0
   310
	test.Next(_L("test 8 bit descriptor"));
sl@0
   311
	h.Command(9);
sl@0
   312
	ret = p.Create(KProcName, KNullDesC);
sl@0
   313
	test(ret == KErrNone);
sl@0
   314
	p.Logon(stat);
sl@0
   315
	h.SetupParameters(p);
sl@0
   316
	p.SetParameter(15, _L8("8 bit text"));
sl@0
   317
	p.Resume();
sl@0
   318
	User::WaitForRequest(stat);
sl@0
   319
	test(p.ExitType()==EExitKill);
sl@0
   320
	test(p.ExitReason()==KErrNone);
sl@0
   321
	CLOSE_AND_WAIT(p);
sl@0
   322
sl@0
   323
sl@0
   324
	test.Next(_L("test bad read of descriptor"));
sl@0
   325
	h.Command(10);
sl@0
   326
	ret = p.Create(KProcName, KNullDesC);
sl@0
   327
	test(ret == KErrNone);
sl@0
   328
	p.Logon(stat);
sl@0
   329
	h.SetupParameters(p);
sl@0
   330
	p.SetParameter(15, _L8("aa"));
sl@0
   331
	p.Resume();
sl@0
   332
	User::WaitForRequest(stat);
sl@0
   333
	test(p.ExitType()==EExitPanic);
sl@0
   334
	test(p.ExitReason()==ECausedException);
sl@0
   335
	CLOSE_AND_WAIT(p);
sl@0
   336
sl@0
   337
	test.Next(_L("test zero length data"));
sl@0
   338
	h.Command(11);
sl@0
   339
	ret = p.Create(KProcName, KNullDesC);
sl@0
   340
	test(ret == KErrNone);
sl@0
   341
	p.Logon(stat);
sl@0
   342
	h.SetupParameters(p);
sl@0
   343
	p.SetParameter(15, KNullDesC);
sl@0
   344
	p.Resume();
sl@0
   345
	User::WaitForRequest(stat);
sl@0
   346
	test(p.ExitType()==EExitKill);
sl@0
   347
	test(p.ExitReason()==KErrNone);
sl@0
   348
	CLOSE_AND_WAIT(p);
sl@0
   349
sl@0
   350
	test.Next(_L("test reserved command line"));
sl@0
   351
	h.Command(12);
sl@0
   352
	ret = p.Create(KProcName, KNullDesC);
sl@0
   353
	test(ret == KErrNone);
sl@0
   354
	p.Logon(stat);
sl@0
   355
	h.SetupParameters(p);
sl@0
   356
	p.Resume();
sl@0
   357
	User::WaitForRequest(stat);
sl@0
   358
	test(p.ExitType()==EExitKill);
sl@0
   359
	test(p.ExitReason()==KErrNone);
sl@0
   360
	CLOSE_AND_WAIT(p);
sl@0
   361
sl@0
   362
sl@0
   363
	test.Next(_L("test mutex"));
sl@0
   364
	h.Command(4);
sl@0
   365
	ret = p.Create(KProcName, KNullDesC);
sl@0
   366
	test(ret == KErrNone);
sl@0
   367
	p.Logon(stat);
sl@0
   368
	h.SetupParameters(p);
sl@0
   369
	p.Resume();
sl@0
   370
	User::WaitForRequest(stat);
sl@0
   371
	test(p.ExitType()==EExitKill);
sl@0
   372
	test(p.ExitReason()==KErrNone);
sl@0
   373
	CLOSE_AND_WAIT(p);
sl@0
   374
sl@0
   375
	
sl@0
   376
	test.Next(_L("test semaphore"));
sl@0
   377
	h.Command(5);
sl@0
   378
	ret = p.Create(KProcName, KNullDesC);
sl@0
   379
	test(ret == KErrNone);
sl@0
   380
	p.Logon(stat);
sl@0
   381
	h.SetupParameters(p);
sl@0
   382
	p.Resume();
sl@0
   383
	User::WaitForRequest(stat);
sl@0
   384
	test(p.ExitType()==EExitKill);
sl@0
   385
	test(p.ExitReason()==KErrNone);
sl@0
   386
	CLOSE_AND_WAIT(p);
sl@0
   387
sl@0
   388
	test.Next(_L("test file handle"));
sl@0
   389
	h.Command(6);
sl@0
   390
	ret = p.Create(KProcName, KNullDesC);
sl@0
   391
	test(ret == KErrNone);
sl@0
   392
	p.Logon(stat);
sl@0
   393
	h.SetupParameters(p);
sl@0
   394
	p.Resume();
sl@0
   395
	User::WaitForRequest(stat);
sl@0
   396
	test(p.ExitType()==EExitKill);
sl@0
   397
	test(p.ExitReason()==KErrNone);
sl@0
   398
	CLOSE_AND_WAIT(p);
sl@0
   399
sl@0
   400
	test.Next(_L("test chunk"));
sl@0
   401
	h.Command(7);
sl@0
   402
	ret = p.Create(KProcName, KNullDesC);
sl@0
   403
	test(ret == KErrNone);
sl@0
   404
	p.Logon(stat);
sl@0
   405
	h.SetupParameters(p);
sl@0
   406
	p.Resume();
sl@0
   407
	User::WaitForRequest(stat);
sl@0
   408
	test(p.ExitType()==EExitKill);
sl@0
   409
	test(p.ExitReason()==KErrNone);
sl@0
   410
	CLOSE_AND_WAIT(p);
sl@0
   411
sl@0
   412
	test.End();
sl@0
   413
	return 0;
sl@0
   414
    }