os/kernelhwsrv/kerneltest/e32test/bench/t_proc1.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) 1996-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\bench\t_proc1.cpp
sl@0
    15
// One half of the process relative type test stuff
sl@0
    16
// Overview:
sl@0
    17
// Tests the RProcess class, including tests on the heap, process naming, 
sl@0
    18
// process resumption, process creation and shared chunks. 
sl@0
    19
// API Information:
sl@0
    20
// RProcess
sl@0
    21
// Details:
sl@0
    22
// - Open a nonexistent process by a process Id and checks for the failure 
sl@0
    23
// of finding this process.
sl@0
    24
// - Open a process with invalid name and verify failure results are as expected.
sl@0
    25
// - Test the closing of processes by calling Kill, Terminate, and Panic methods.
sl@0
    26
// Verify results are as expected.
sl@0
    27
// - Create a process and verify the full path name of the loaded executable on 
sl@0
    28
// which this process is based.
sl@0
    29
// - Open a process by name, rename the process in a variety of ways and verify 
sl@0
    30
// the results are as expected.
sl@0
    31
// - Open a process, assign high, low, and bad priorities, verify the results
sl@0
    32
// are as expected.
sl@0
    33
// - Open a process, kill it and verify the results are as expected.
sl@0
    34
// - Open a process by name based on the file name, verify the results are as 
sl@0
    35
// expected.
sl@0
    36
// - Retrieve the process Id and open a handle on it. Create a duplicate process.
sl@0
    37
// Verify the results are as expected.
sl@0
    38
// - Find a process using TFindProcess() and open a handle on it. Verify the
sl@0
    39
// results are as expected.
sl@0
    40
// - Check chunk sharing between threads and verify results are as expected.
sl@0
    41
// - Perform a "speed" test where a new thread is created and the thread increments
sl@0
    42
// a count until stopped. Calculate and display counts per second. Verify results
sl@0
    43
// are as expected.
sl@0
    44
// - Verify the ExitReason, ExitType and ExitCatagory when the thread dies.
sl@0
    45
// - Verify that stopping the process completes existing pending requests with 
sl@0
    46
// KErrServerTerminated.
sl@0
    47
// - Verify that the heap was not corrupted by the tests.
sl@0
    48
// Platforms/Drives/Compatibility:
sl@0
    49
// All.
sl@0
    50
// Assumptions/Requirement/Pre-requisites:
sl@0
    51
// Failures and causes:
sl@0
    52
// Base Port information:
sl@0
    53
// 
sl@0
    54
//
sl@0
    55
sl@0
    56
#include <e32test.h>
sl@0
    57
#include "../mmu/mmudetect.h"
sl@0
    58
#include "t_proc.h"
sl@0
    59
sl@0
    60
LOCAL_D RTest test(_L("T_PROC1"));
sl@0
    61
sl@0
    62
const TBufC<67> tooLong=_L("This should return KErrBadName and not crash.......0123456789ABCDEF");
sl@0
    63
const TBufC<66> notQuiteTooLong=_L("This should return KErrNone and be completely OK..0123456789ABCDEF");
sl@0
    64
const TBufC<26> bond=_L("Bond, James Bond[00000000]");
sl@0
    65
const TBufC<16> jamesBond=_L("Bond, James Bond");
sl@0
    66
sl@0
    67
RProcess proc;
sl@0
    68
RProcess proc2;
sl@0
    69
RProcess proc3;
sl@0
    70
RProcess proc4;
sl@0
    71
sl@0
    72
TName command;
sl@0
    73
TRequestStatus stat,notStat;
sl@0
    74
sl@0
    75
const TInt KKillReason=2563453;
sl@0
    76
const TInt KTerminateReason=8034255;
sl@0
    77
const TInt KPanicReason=39365235;
sl@0
    78
sl@0
    79
LOCAL_D RSemaphore client;
sl@0
    80
LOCAL_D TInt speedCount;
sl@0
    81
sl@0
    82
class RDisplay : public RSessionBase
sl@0
    83
	{
sl@0
    84
public:
sl@0
    85
	TInt Open();
sl@0
    86
	TInt Display(const TDesC& aMessage);
sl@0
    87
	TInt Read();
sl@0
    88
	TInt Write();
sl@0
    89
	TInt Stop();
sl@0
    90
	TInt Test();
sl@0
    91
	TVersion Version();
sl@0
    92
	};
sl@0
    93
sl@0
    94
TInt RDisplay::Open()
sl@0
    95
//
sl@0
    96
// Open the server.
sl@0
    97
//
sl@0
    98
	{
sl@0
    99
sl@0
   100
	return(CreateSession(_L("Display"),Version(),1));
sl@0
   101
	}
sl@0
   102
sl@0
   103
TInt RDisplay::Display(const TDesC& aMessage)
sl@0
   104
//
sl@0
   105
// Display a message.
sl@0
   106
//
sl@0
   107
	{
sl@0
   108
sl@0
   109
	TBuf<0x10> b(aMessage);
sl@0
   110
	return(SendReceive(CMyServer::EDisplay,TIpcArgs(&b)));
sl@0
   111
	}
sl@0
   112
sl@0
   113
TInt RDisplay::Read()
sl@0
   114
//
sl@0
   115
// Get session to test CSession2::ReadL.
sl@0
   116
//
sl@0
   117
	{
sl@0
   118
sl@0
   119
	TBuf<0x10> b(_L("Testing read"));
sl@0
   120
	return(SendReceive(CMyServer::ERead,TIpcArgs(&b)));
sl@0
   121
	}
sl@0
   122
sl@0
   123
TInt RDisplay::Write()
sl@0
   124
//
sl@0
   125
// Get session to test CSession2::WriteL.
sl@0
   126
//
sl@0
   127
	{
sl@0
   128
sl@0
   129
	TBuf<0x10> b;
sl@0
   130
    TBufC<0x10> c; // Bad descriptor - read only
sl@0
   131
	TInt r=SendReceive(CMyServer::EWrite,TIpcArgs(&b,&c));
sl@0
   132
	if (r==KErrNone && b!=_L("It worked!"))
sl@0
   133
		r=KErrGeneral;
sl@0
   134
	return r;
sl@0
   135
	}
sl@0
   136
sl@0
   137
TInt RDisplay::Test()
sl@0
   138
//
sl@0
   139
// Send a message and wait for completion.
sl@0
   140
//
sl@0
   141
	{
sl@0
   142
sl@0
   143
	TInt i[4];
sl@0
   144
	return(SendReceive(CMyServer::ETest,TIpcArgs(&i[0])));
sl@0
   145
	}
sl@0
   146
sl@0
   147
TInt RDisplay::Stop()
sl@0
   148
//
sl@0
   149
// Stop the server.
sl@0
   150
//
sl@0
   151
	{
sl@0
   152
sl@0
   153
	TInt i[4];
sl@0
   154
	return(SendReceive(CMyServer::EStop,TIpcArgs(&i[0])));
sl@0
   155
	}
sl@0
   156
sl@0
   157
TVersion RDisplay::Version()
sl@0
   158
//
sl@0
   159
// Return the current version.
sl@0
   160
//
sl@0
   161
	{
sl@0
   162
sl@0
   163
	TVersion v(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
   164
	return(v);
sl@0
   165
	}
sl@0
   166
sl@0
   167
LOCAL_C TInt RunPanicThread(RThread& aThread)
sl@0
   168
	{
sl@0
   169
	TRequestStatus s;
sl@0
   170
	aThread.Logon(s);
sl@0
   171
	TBool jit = User::JustInTime();
sl@0
   172
	User::SetJustInTime(EFalse);
sl@0
   173
	aThread.Resume();
sl@0
   174
	User::WaitForRequest(s);
sl@0
   175
	User::SetJustInTime(jit);
sl@0
   176
	return s.Int();
sl@0
   177
	}
sl@0
   178
sl@0
   179
TInt KillProtectedEntry(TAny*)
sl@0
   180
	{
sl@0
   181
	proc.Kill(KErrGeneral);
sl@0
   182
	return KErrGeneral;
sl@0
   183
	}
sl@0
   184
sl@0
   185
TInt createProc2(RProcess& aProcess)
sl@0
   186
	{
sl@0
   187
	TFileName filename(RProcess().FileName());
sl@0
   188
	TInt pos=filename.LocateReverse(TChar('\\'));
sl@0
   189
	filename.SetLength(pos+1);
sl@0
   190
	filename+=_L("T_PROC2.EXE");
sl@0
   191
	TInt r=aProcess.Create(filename, command);
sl@0
   192
	if (r==KErrNone)
sl@0
   193
		{
sl@0
   194
		TFullName fn(aProcess.FullName());
sl@0
   195
		test.Printf(_L("Created %S\n"),&fn);
sl@0
   196
		}
sl@0
   197
	return r;
sl@0
   198
	}
sl@0
   199
sl@0
   200
void simpleTests1()
sl@0
   201
	{
sl@0
   202
	test.Next(_L("Open by name"));
sl@0
   203
	RProcess me;
sl@0
   204
	TInt r=proc2.Open(me.Name());
sl@0
   205
	test(r==KErrNone);
sl@0
   206
	test.Next(_L("Rename"));
sl@0
   207
	TName initName(me.Name());
sl@0
   208
	r=User::RenameProcess(jamesBond);
sl@0
   209
	test(r==KErrNone);
sl@0
   210
	test(me.Name().Left(26)==bond);
sl@0
   211
	test(proc2.Name().Left(26)==bond);
sl@0
   212
	r=User::RenameProcess(tooLong);
sl@0
   213
	test(r==KErrBadName);
sl@0
   214
	test(me.Name().Left(26)==bond);
sl@0
   215
	test(proc2.Name().Left(26)==bond);
sl@0
   216
	TName* work=new TName(notQuiteTooLong);
sl@0
   217
	r=User::RenameProcess(*work);
sl@0
   218
	test(r==KErrNone);
sl@0
   219
	work->Append(_L("[00000000]"));
sl@0
   220
	test(me.Name().Length()==KMaxKernelName);
sl@0
   221
	test(me.Name().Left(KMaxKernelName-4)==*work);
sl@0
   222
	test(proc2.Name().Length()==KMaxKernelName);
sl@0
   223
	test(proc2.Name().Left(KMaxKernelName-4)==*work);
sl@0
   224
	delete work;
sl@0
   225
	r=User::RenameProcess(_L("T_PROC1"));
sl@0
   226
	test(r==KErrNone);
sl@0
   227
	TFullName fn(_L("T_PROC1["));
sl@0
   228
	TUidType uidType(me.Type());
sl@0
   229
	TUint32 uid3 = uidType[2].iUid;
sl@0
   230
	fn.AppendNumFixedWidth(uid3,EHex,8);
sl@0
   231
	fn.Append(']');
sl@0
   232
	test(proc2.Name().Left(17)==fn);
sl@0
   233
	test(me.Name().Left(17)==fn);
sl@0
   234
	TInt l = initName.Locate('[');
sl@0
   235
	r=User::RenameProcess(initName.Left(l));
sl@0
   236
	test(r==KErrNone);
sl@0
   237
	test(proc2.Name()==initName);
sl@0
   238
	proc2.Close();
sl@0
   239
	}
sl@0
   240
sl@0
   241
TInt BadPriority(TAny* proc2)
sl@0
   242
	{
sl@0
   243
	((RProcess*)proc2)->SetPriority(EPriorityWindowServer);
sl@0
   244
	return KErrNone; 
sl@0
   245
	}
sl@0
   246
sl@0
   247
void simpleTests2()
sl@0
   248
	{
sl@0
   249
	TInt r=proc2.Open(proc.Name());
sl@0
   250
	test.Next(_L("Mess with Priority"));
sl@0
   251
	proc.SetPriority(EPriorityHigh);
sl@0
   252
	test.Printf(_L("%d %d\n"),proc.Priority(),proc2.Priority());
sl@0
   253
	test(proc.Priority()==EPriorityHigh);
sl@0
   254
	test(proc2.Priority()==EPriorityHigh);
sl@0
   255
	proc2.SetPriority(EPriorityLow);
sl@0
   256
	test(proc.Priority()==EPriorityLow);
sl@0
   257
	test(proc2.Priority()==EPriorityLow);
sl@0
   258
sl@0
   259
	RThread thread;
sl@0
   260
	r=thread.Create(_L("Bad Priority"),BadPriority,KDefaultStackSize,NULL,&proc2);
sl@0
   261
	test(r==KErrNone);
sl@0
   262
	r=RunPanicThread(thread);
sl@0
   263
	test(r==EBadPriority);
sl@0
   264
	test(thread.ExitType()==EExitPanic);
sl@0
   265
	test(thread.ExitReason()==EBadPriority);
sl@0
   266
	test(thread.ExitCategory()==_L("KERN-EXEC"));
sl@0
   267
	CLOSE_AND_WAIT(thread);
sl@0
   268
	test(proc.Priority()==EPriorityLow);
sl@0
   269
	test(proc2.Priority()==EPriorityLow);
sl@0
   270
	proc2.Close();
sl@0
   271
	}
sl@0
   272
sl@0
   273
void procTests1()
sl@0
   274
	{
sl@0
   275
	test.Next(_L("Test functions"));
sl@0
   276
    TFileName fileName(proc.FileName());
sl@0
   277
    test.Printf(fileName);
sl@0
   278
#ifndef __WINS__
sl@0
   279
	if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
sl@0
   280
		test(fileName.Mid(1).CompareF(_L(":\\Sys\\Bin\\T_PROC2.EXE"))==0);
sl@0
   281
	else
sl@0
   282
		test(fileName.Mid(1).CompareF(_L(":\\System\\Bin\\T_PROC2.EXE"))==0);
sl@0
   283
#else
sl@0
   284
	if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
sl@0
   285
		test(fileName.CompareF(_L("Z:\\Sys\\Bin\\T_PROC2.EXE"))==0);
sl@0
   286
	else
sl@0
   287
		test(fileName.CompareF(_L("Z:\\System\\Bin\\T_PROC2.EXE"))==0);
sl@0
   288
#endif
sl@0
   289
	test(proc.Name().Left(21).CompareF(_L("T_PROC2.EXE[00000000]"))==0);
sl@0
   290
	test(proc.Priority()==EPriorityForeground);
sl@0
   291
	test(proc.ExitType()==EExitPending);
sl@0
   292
	test(proc.ExitReason()==0);
sl@0
   293
	test(proc.ExitCategory()==KNullDesC);
sl@0
   294
	}
sl@0
   295
sl@0
   296
void procTests2()
sl@0
   297
	{
sl@0
   298
	test.Next(_L("Kill and recreate"));
sl@0
   299
	RProcess proc2;
sl@0
   300
	TInt r=proc2.Open(proc.Id());
sl@0
   301
	test(r==KErrNone);
sl@0
   302
	test(proc2.Handle()!=0);
sl@0
   303
	proc.Logon(stat);
sl@0
   304
	proc.Logon(notStat);
sl@0
   305
	r=proc.LogonCancel(notStat);
sl@0
   306
	test(r==KErrNone);
sl@0
   307
	test(notStat==KErrNone);
sl@0
   308
	proc.Kill(KKillReason);
sl@0
   309
	User::WaitForRequest(stat);
sl@0
   310
	test(stat==KKillReason);
sl@0
   311
	test(proc.ExitType()==EExitKill);
sl@0
   312
	test(proc.ExitReason()==KKillReason);
sl@0
   313
	test(proc.ExitCategory()==_L("Kill"));
sl@0
   314
	proc.Close();
sl@0
   315
	test(proc.Handle()==0);
sl@0
   316
	test(proc2.ExitType()==EExitKill);
sl@0
   317
	test(proc2.ExitReason()==KKillReason);
sl@0
   318
	test(proc2.ExitCategory()==_L("Kill"));
sl@0
   319
	CLOSE_AND_WAIT(proc2);
sl@0
   320
	test(proc2.Handle()==0);
sl@0
   321
	}
sl@0
   322
sl@0
   323
void procTests3()
sl@0
   324
	{
sl@0
   325
	TFileName filename(RProcess().FileName());
sl@0
   326
	TInt pos=filename.LocateReverse(TChar('\\'));
sl@0
   327
	filename.SetLength(pos+1);
sl@0
   328
	filename+=_L("T_PROC2.EXE");
sl@0
   329
	TInt r=proc.Create(filename, command);
sl@0
   330
	test(r==KErrNone);
sl@0
   331
	TFullName fn(proc.FullName());
sl@0
   332
	test.Printf(_L("Created %S\n"),&fn);
sl@0
   333
	test(proc.FileName().CompareF(filename)==0);
sl@0
   334
	test(proc.Name().Left(21).CompareF(_L("T_PROC2.EXE[00000000]"))==0);
sl@0
   335
	test(proc.Priority()==EPriorityForeground);
sl@0
   336
	test(proc.ExitType()==EExitPending);
sl@0
   337
	test(proc.ExitReason()==0);
sl@0
   338
	test(proc.ExitCategory()==KNullDesC);
sl@0
   339
	}
sl@0
   340
sl@0
   341
void procTests4()
sl@0
   342
	{
sl@0
   343
	test.Next(_L("Get and open by Id"));
sl@0
   344
	TProcessId id=proc.Id();
sl@0
   345
	TProcessId id2=proc.Id();
sl@0
   346
	test(id==id2);
sl@0
   347
	TInt r=proc2.Open(proc.Name());
sl@0
   348
	test(r==KErrNone);
sl@0
   349
	id2=proc2.Id();
sl@0
   350
	test(id==id2);
sl@0
   351
	r=proc3.Open(id);
sl@0
   352
	test(r==KErrNone);
sl@0
   353
	id2=proc3.Id();
sl@0
   354
	test(id==id2);
sl@0
   355
	proc3.Close();
sl@0
   356
	if (HaveVirtMem())
sl@0
   357
		{
sl@0
   358
		test.Next(_L("Create duplicate"));
sl@0
   359
		r=createProc2(proc3);
sl@0
   360
		test(r==KErrNone);
sl@0
   361
		id2=proc3.Id();
sl@0
   362
		test(id!=id2);
sl@0
   363
		test(*(TUint*)&id<*(TUint*)&id2);
sl@0
   364
		}
sl@0
   365
	}
sl@0
   366
sl@0
   367
void procTests5()
sl@0
   368
	{
sl@0
   369
	test.Next(_L("Try to find processes"));
sl@0
   370
	TFindProcess* findProc=new TFindProcess(_L("T_PROC2*"));
sl@0
   371
	test(findProc!=NULL);
sl@0
   372
	TFullName* result=new TFullName;
sl@0
   373
	test(result!=NULL);
sl@0
   374
	TInt r=findProc->Next(*result);
sl@0
   375
	test(r==KErrNone);
sl@0
   376
	TFullName temp = proc.FullName();
sl@0
   377
	test(result->CompareF(temp)==0);
sl@0
   378
	r=findProc->Next(*result);
sl@0
   379
	test(r==KErrNone);
sl@0
   380
	test(result->CompareF(proc3.FullName())==0);
sl@0
   381
	r=findProc->Next(*result);
sl@0
   382
	test(r==KErrNotFound);
sl@0
   383
	findProc->Find(_L("T?PROC2*]*"));
sl@0
   384
	r=findProc->Next(*result);
sl@0
   385
	test(r==KErrNone);
sl@0
   386
	test(result->CompareF(temp)==0);
sl@0
   387
	r=findProc->Next(*result);
sl@0
   388
	test(r==KErrNone);
sl@0
   389
	test(result->CompareF(proc3.FullName())==0);
sl@0
   390
	delete result;
sl@0
   391
	test.Next(_L("Open by find handle"));
sl@0
   392
	r=proc4.Open(*findProc);
sl@0
   393
	test(r==KErrNone);
sl@0
   394
	TProcessId id=proc3.Id();
sl@0
   395
	TProcessId id2=proc4.Id();
sl@0
   396
	test(id==id2);
sl@0
   397
	delete findProc;
sl@0
   398
	}
sl@0
   399
sl@0
   400
void procTests6()
sl@0
   401
	{
sl@0
   402
	test.Next(_L("Kill duplicate"));
sl@0
   403
	proc3.Logon(stat);
sl@0
   404
	test(stat==KRequestPending);
sl@0
   405
	proc4.Kill(12345);
sl@0
   406
	User::WaitForRequest(stat);
sl@0
   407
	test(stat==12345);
sl@0
   408
	}
sl@0
   409
sl@0
   410
void createProcess()
sl@0
   411
//
sl@0
   412
// Create T_PROC2 and, on the way, do lots of basic tests
sl@0
   413
//
sl@0
   414
	{
sl@0
   415
	test.Start(_L("Create"));
sl@0
   416
	TInt r=globSem1.CreateGlobal(_L("GlobSem1"), 0);
sl@0
   417
	test(r==KErrNone);
sl@0
   418
	r=globSem2.CreateGlobal(_L("GlobSem2"), 0);
sl@0
   419
	test(r==KErrNone);
sl@0
   420
sl@0
   421
	r=createProc2(proc);
sl@0
   422
	test(r==KErrNone);
sl@0
   423
sl@0
   424
	procTests1();
sl@0
   425
sl@0
   426
	simpleTests1();
sl@0
   427
	simpleTests2();
sl@0
   428
sl@0
   429
	procTests2();
sl@0
   430
	procTests3();
sl@0
   431
	procTests4();
sl@0
   432
	if (HaveVirtMem())
sl@0
   433
		{
sl@0
   434
		procTests5();
sl@0
   435
		procTests6();
sl@0
   436
		}
sl@0
   437
sl@0
   438
	proc2.Close();
sl@0
   439
	proc3.Close();
sl@0
   440
	if (proc4.Handle())
sl@0
   441
		CLOSE_AND_WAIT(proc4);
sl@0
   442
sl@0
   443
	test.Next(_L("Resume"));
sl@0
   444
	proc.Logon(stat); // logon to process
sl@0
   445
	proc.Logon(notStat);
sl@0
   446
	r=proc.LogonCancel(notStat);
sl@0
   447
	test(r==KErrNone);
sl@0
   448
	test(notStat==KErrNone);
sl@0
   449
	test(proc.ExitType()==EExitPending);
sl@0
   450
	proc.Resume();
sl@0
   451
	globSem1.Wait(); // wait for T_PROC2 to get started
sl@0
   452
	test.End();
sl@0
   453
	}
sl@0
   454
sl@0
   455
void murderProcess()
sl@0
   456
	{
sl@0
   457
	test.Start(_L("Kill"));
sl@0
   458
	RProcess process;
sl@0
   459
	TInt r=createProc2(process);
sl@0
   460
	test(r==KErrNone);
sl@0
   461
	TProcessId id=process.Id();
sl@0
   462
	TProcessId id2=process.Id();
sl@0
   463
	test(id==id2);
sl@0
   464
 	TRequestStatus stat;
sl@0
   465
	process.Logon(stat);
sl@0
   466
	test(process.ExitType()==EExitPending);
sl@0
   467
	process.Kill(KKillReason);
sl@0
   468
	User::WaitForRequest(stat);
sl@0
   469
	test(stat==KKillReason);
sl@0
   470
	test(process.ExitType()==EExitKill);
sl@0
   471
	test(process.ExitReason()==KKillReason);
sl@0
   472
	test(process.ExitCategory()==_L("Kill"));
sl@0
   473
	CLOSE_AND_WAIT(process);
sl@0
   474
sl@0
   475
	test.Next(_L("Terminate"));
sl@0
   476
	r=createProc2(process);
sl@0
   477
	test(r==KErrNone);
sl@0
   478
	id2=process.Id();
sl@0
   479
	test(*(TUint*)&id+2==*(TUint*)&id2);	// use 2 ID's each time, one for process, one for thread
sl@0
   480
	process.Logon(stat);
sl@0
   481
	test(process.ExitType()==EExitPending);
sl@0
   482
	process.Terminate(KTerminateReason);
sl@0
   483
	User::WaitForRequest(stat);
sl@0
   484
	test(stat==KTerminateReason);
sl@0
   485
	test(process.ExitType()==EExitTerminate);
sl@0
   486
	test(process.ExitReason()==KTerminateReason);
sl@0
   487
	test(process.ExitCategory()==_L("Terminate"));
sl@0
   488
	CLOSE_AND_WAIT(process);
sl@0
   489
sl@0
   490
	test.Next(_L("Panic"));
sl@0
   491
	r=createProc2(process);
sl@0
   492
	test(r==KErrNone);
sl@0
   493
	id2=process.Id();
sl@0
   494
	test(*(TUint*)&id+4==*(TUint*)&id2);
sl@0
   495
 	test(process.ExitType()==EExitPending);
sl@0
   496
	process.Logon(stat);
sl@0
   497
	process.SetJustInTime(EFalse);	// prevent the process panic from starting the debugger
sl@0
   498
	process.Panic(_L("BOO!"),KPanicReason);
sl@0
   499
	User::WaitForRequest(stat);
sl@0
   500
	test(stat==KPanicReason);
sl@0
   501
	test(process.ExitType()==EExitPanic);
sl@0
   502
	test(process.ExitReason()==KPanicReason);
sl@0
   503
	test(process.ExitCategory()==_L("BOO!"));
sl@0
   504
	CLOSE_AND_WAIT(process);
sl@0
   505
	test.End();
sl@0
   506
	}
sl@0
   507
sl@0
   508
void sharedChunks()
sl@0
   509
	{
sl@0
   510
	test.Start(_L("Test chunk sharing between threads"));
sl@0
   511
sl@0
   512
	test.Next(_L("Create chunk Marmalade"));
sl@0
   513
	TInt r=0;
sl@0
   514
	RChunk chunk;
sl@0
   515
	TInt size=0x1000;
sl@0
   516
	TInt maxSize=0x5000;
sl@0
   517
	r=chunk.CreateGlobal(_L("Marmalade"),size,maxSize);
sl@0
   518
	test(r==KErrNone);
sl@0
   519
	test.Next(_L("Write 0-9 to it"));
sl@0
   520
	TUint8* base=chunk.Base();
sl@0
   521
	for (TInt8 j=0;j<10;j++)
sl@0
   522
		*base++=j; // write 0 - 9 to the chunk
sl@0
   523
  	globSem2.Signal(); // T_PROC2 can check the chunk now
sl@0
   524
	globSem1.Wait();
sl@0
   525
	chunk.Close(); // now it's ok to kill the chunk
sl@0
   526
sl@0
   527
	test.End();
sl@0
   528
	}
sl@0
   529
sl@0
   530
TInt sharedChunks2(TAny* /*aDummy*/)
sl@0
   531
	{
sl@0
   532
    RTest test(_L("Shared Chunks 2"));
sl@0
   533
sl@0
   534
	test.Title();	
sl@0
   535
	test.Start(_L("Test chunk sharing between threads"));
sl@0
   536
sl@0
   537
	test.Next(_L("Create chunk Marmalade"));
sl@0
   538
	TInt r=0;
sl@0
   539
	RChunk chunk;
sl@0
   540
	TInt size=0x1000;
sl@0
   541
	TInt maxSize=0x5000;
sl@0
   542
	r=chunk.CreateGlobal(_L("Marmalade"),size,maxSize);
sl@0
   543
	test(r==KErrNone);
sl@0
   544
	test.Next(_L("Write 0-9 to it"));
sl@0
   545
	TUint8* base=chunk.Base();
sl@0
   546
	for (TInt8 j=0;j<10;j++)
sl@0
   547
		*base++=j; // write 0 - 9 to the chunk
sl@0
   548
  	globSem2.Signal(); // T_PROC2 can check the chunk now
sl@0
   549
	globSem1.Wait();
sl@0
   550
	chunk.Close(); // now it's ok to kill the chunk
sl@0
   551
sl@0
   552
	test.End();
sl@0
   553
    return(KErrNone);
sl@0
   554
	}
sl@0
   555
sl@0
   556
TInt speedyThreadEntryPoint(TAny*)
sl@0
   557
//
sl@0
   558
// The entry point for the speed test thread.
sl@0
   559
//
sl@0
   560
	{
sl@0
   561
	RDisplay t;
sl@0
   562
	TInt r=t.Open();
sl@0
   563
	test(r==KErrNone);
sl@0
   564
	speedCount=0;
sl@0
   565
	client.Signal();
sl@0
   566
	while ((r=t.Test())==KErrNone)
sl@0
   567
		speedCount++;
sl@0
   568
	t.Close();
sl@0
   569
	return r;
sl@0
   570
	}
sl@0
   571
sl@0
   572
TInt BadName(TAny*)
sl@0
   573
	{
sl@0
   574
	proc.Open(_L("*"));
sl@0
   575
	return KErrNone;
sl@0
   576
	}
sl@0
   577
sl@0
   578
TInt sharedHeap(TAny*)
sl@0
   579
	{
sl@0
   580
	RTest test2(_L("sharedHeap"));
sl@0
   581
	test2.Title();
sl@0
   582
	test2.Start(_L("Shared heap tests"));
sl@0
   583
sl@0
   584
	RAllocator* allocator = &User::Allocator();
sl@0
   585
	test2.Printf(_L("sharedHeap's heap is at %08x\n"), allocator);
sl@0
   586
	
sl@0
   587
	TInt size;
sl@0
   588
	allocator->AllocSize(size);
sl@0
   589
	test2.Printf(_L("sharedHeap's heap allocsize is %08x\n"),size);
sl@0
   590
sl@0
   591
// Press a key only if running the test in manual mode. We will be
sl@0
   592
// able to ascertain this when RTest has been enhanced.
sl@0
   593
//	test.Next(_L("Press a key to continue"));
sl@0
   594
//	test2.Getch();
sl@0
   595
sl@0
   596
	test2.End();
sl@0
   597
	return(KErrNone);
sl@0
   598
	}
sl@0
   599
sl@0
   600
_LIT(KTestProcessNewName,"T_PROC1_NEW.EXE");
sl@0
   601
sl@0
   602
TInt DupRenameProcTest(TInt aCall)
sl@0
   603
	{
sl@0
   604
	test.Printf(_L("DupRenameProcTest: call %d\n"),aCall);
sl@0
   605
sl@0
   606
	TInt r;
sl@0
   607
sl@0
   608
	switch(aCall)
sl@0
   609
		{
sl@0
   610
	case 1:
sl@0
   611
		{
sl@0
   612
		r = User::RenameProcess(KTestProcessNewName);
sl@0
   613
		test(r==KErrNone);
sl@0
   614
		TFullName fn(RProcess().FullName());
sl@0
   615
		test.Printf(_L("Renamed to %S\n"),&fn);
sl@0
   616
		TInt li = fn.Locate('[');
sl@0
   617
		TInt ri = fn.Locate(']');
sl@0
   618
		test(fn.Left(li)==KTestProcessNewName);
sl@0
   619
		test(fn.Mid(ri+1)==_L("0001"));
sl@0
   620
		}
sl@0
   621
sl@0
   622
	case 0:
sl@0
   623
		{
sl@0
   624
		TFileName filename(RProcess().FileName());
sl@0
   625
		TInt pos=filename.LocateReverse(TChar('\\'));
sl@0
   626
		filename.SetLength(pos+1);
sl@0
   627
		filename+=_L("T_PROC1.EXE");
sl@0
   628
		RProcess pr;
sl@0
   629
		TBuf16<10> call;
sl@0
   630
		call.Num(aCall+1);
sl@0
   631
		r = pr.Create(filename, call);
sl@0
   632
		TFullName fn(pr.FullName());
sl@0
   633
		test.Printf(_L("Created %S\n"),&fn);
sl@0
   634
		TRequestStatus st;
sl@0
   635
		pr.Logon(st);
sl@0
   636
		pr.Resume();
sl@0
   637
		User::WaitForRequest(st);
sl@0
   638
		CLOSE_AND_WAIT(pr);
sl@0
   639
		}
sl@0
   640
		return KErrNone;
sl@0
   641
sl@0
   642
	case 2:
sl@0
   643
		{
sl@0
   644
		r = User::RenameProcess(KTestProcessNewName);
sl@0
   645
		test(r==KErrNone);
sl@0
   646
		TFullName fn(RProcess().FullName());
sl@0
   647
		test.Printf(_L("Renamed to %S\n"),&fn);
sl@0
   648
		TInt li = fn.Locate('[');
sl@0
   649
		TInt ri = fn.Locate(']');
sl@0
   650
		test(fn.Left(li)==KTestProcessNewName);
sl@0
   651
		test(fn.Mid(ri+1)==_L("0002"));
sl@0
   652
		}
sl@0
   653
		return KErrNone;
sl@0
   654
sl@0
   655
	default:
sl@0
   656
		return KErrArgument;
sl@0
   657
		}
sl@0
   658
	}
sl@0
   659
sl@0
   660
_LIT(KTestProcessName,"TestName");
sl@0
   661
sl@0
   662
void TestProcessRename()
sl@0
   663
	{
sl@0
   664
	// Rename the current process with test name
sl@0
   665
    TInt r = User::RenameProcess(KTestProcessName);
sl@0
   666
    test(r==KErrNone);
sl@0
   667
    TName name1 = RProcess().Name();
sl@0
   668
sl@0
   669
    // Check new name is correct
sl@0
   670
    TName name2 = name1;
sl@0
   671
    name2.SetLength(KTestProcessName().Length());
sl@0
   672
    test(name2.CompareF(KTestProcessName)==0);
sl@0
   673
    
sl@0
   674
    // Rename the process with same test name
sl@0
   675
    r = User::RenameProcess(KTestProcessName);
sl@0
   676
    test(r==KErrNone);
sl@0
   677
    name2 = RProcess().Name();
sl@0
   678
    test(name1.Compare(name2)==0);  // name should be unchanged
sl@0
   679
	}
sl@0
   680
sl@0
   681
TInt E32Main()
sl@0
   682
	{
sl@0
   683
	__KHEAP_MARK;
sl@0
   684
sl@0
   685
	// Turn off lazy dll unloading
sl@0
   686
	RLoader l;
sl@0
   687
	test(l.Connect()==KErrNone);
sl@0
   688
	test(l.CancelLazyDllUnload()==KErrNone);
sl@0
   689
	l.Close();
sl@0
   690
sl@0
   691
	TBuf16<512> cmd;
sl@0
   692
	User::CommandLine(cmd);
sl@0
   693
	if(cmd.Length() && TChar(cmd[0]).IsDigit())
sl@0
   694
		{
sl@0
   695
		TInt r = DupRenameProcTest(TUint(TChar(cmd[0])) - '0');
sl@0
   696
		test(r==KErrNone);
sl@0
   697
		return 0;
sl@0
   698
		}
sl@0
   699
sl@0
   700
	test.Title();
sl@0
   701
sl@0
   702
	test.Start(_L("Testing process stuff 1"));
sl@0
   703
	TInt r;
sl@0
   704
	TRequestStatus s;
sl@0
   705
	test.Next(_L("Creating semaphore"));
sl@0
   706
	r=client.CreateLocal(0);
sl@0
   707
	test(r==KErrNone);
sl@0
   708
sl@0
   709
	test.Next(_L("Try to open nonexistant process by ID"));
sl@0
   710
	r=proc.Open(*(TProcessId*)&KMaxTUint);
sl@0
   711
	test(r==KErrNotFound);
sl@0
   712
sl@0
   713
	test.Next(_L("Try to open process with invalid name"));
sl@0
   714
	RThread thread;
sl@0
   715
	r=thread.Create(_L("Bad Name"),BadName,KDefaultStackSize,NULL,NULL);
sl@0
   716
	test(r==KErrNone);
sl@0
   717
	TRequestStatus threadStat;
sl@0
   718
	thread.Logon(threadStat);
sl@0
   719
	TBool justInTime=User::JustInTime();
sl@0
   720
	User::SetJustInTime(EFalse);
sl@0
   721
	thread.Resume();
sl@0
   722
	User::WaitForRequest(threadStat);
sl@0
   723
	User::SetJustInTime(justInTime);
sl@0
   724
	test(threadStat==EBadName);
sl@0
   725
	test(thread.ExitType()==EExitPanic);
sl@0
   726
	test(thread.ExitReason()==EBadName);
sl@0
   727
	test(thread.ExitCategory()==_L("KERN-EXEC"));
sl@0
   728
	CLOSE_AND_WAIT(thread);
sl@0
   729
sl@0
   730
	test.Next(_L("Murder processes in different ways"));
sl@0
   731
	murderProcess();
sl@0
   732
sl@0
   733
	test.Next(_L("Create second process"));
sl@0
   734
	createProcess();
sl@0
   735
sl@0
   736
	test.Next(_L("Shared Chunks from main thread"));
sl@0
   737
	sharedChunks();
sl@0
   738
sl@0
   739
	test.Next(_L("Shared chunks from secondary thread"));
sl@0
   740
	RThread t;
sl@0
   741
	r=t.Create(_L("Shared chunks 2"),sharedChunks2,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   742
    test(r==KErrNone);
sl@0
   743
	t.Logon(s);
sl@0
   744
	t.Resume();
sl@0
   745
	User::WaitForRequest(s);
sl@0
   746
	test(s==KErrNone);
sl@0
   747
	CLOSE_AND_WAIT(t);
sl@0
   748
sl@0
   749
	test.Next(_L("Starting speedy client"));
sl@0
   750
	RThread speedy;
sl@0
   751
	r=speedy.Create(_L("Speedy"),speedyThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   752
	test(r==KErrNone);
sl@0
   753
sl@0
   754
	RThread().SetPriority(EPriorityMuchMore);
sl@0
   755
    speedy.SetPriority(EPriorityNormal);
sl@0
   756
	TRequestStatus speedyStatus;
sl@0
   757
	speedy.Logon(speedyStatus);
sl@0
   758
	speedy.Resume();
sl@0
   759
sl@0
   760
	test.Next(_L("Wait for speedy to start"));
sl@0
   761
	client.Wait();
sl@0
   762
sl@0
   763
	globSem1.Wait(); // wait for proc2 to be nice & quiet
sl@0
   764
	test.Printf(_L("Starting speed test...\n"));
sl@0
   765
    User::After(300000);
sl@0
   766
    TInt b=speedCount;
sl@0
   767
    User::After(3000000);
sl@0
   768
    TInt n=speedCount;
sl@0
   769
    test.Printf(_L("Count = %d in 1 second\n"),(n-b)/3);
sl@0
   770
sl@0
   771
	test.Next(_L("Tell second process speed tests are done"));
sl@0
   772
	globSem2.Signal();
sl@0
   773
sl@0
   774
	test.Next(_L("Process Logon"));
sl@0
   775
	User::WaitForRequest(stat);
sl@0
   776
	const TDesC& cat=proc.ExitCategory();
sl@0
   777
	test.Printf(_L("Exit category = %S\n"),&cat);
sl@0
   778
	test.Printf(_L("Exit reason = %x\n"),proc.ExitReason());
sl@0
   779
	test.Printf(_L("Exit type = %x\n"),proc.ExitType());
sl@0
   780
sl@0
   781
	test(stat==KErrNone);
sl@0
   782
	test(proc.ExitCategory()==_L("Kill"));
sl@0
   783
	test(proc.ExitReason()==KErrNone);
sl@0
   784
	test(proc.ExitType()==EExitKill);
sl@0
   785
	test(notStat==KErrNone);
sl@0
   786
	test.Next(_L("Test LogonCancel to dead process is ok"));
sl@0
   787
	r=proc.LogonCancel(stat);
sl@0
   788
	test(r==KErrGeneral);
sl@0
   789
	globSem1.Close();
sl@0
   790
	globSem2.Close();
sl@0
   791
	client.Close();
sl@0
   792
sl@0
   793
	User::WaitForRequest(speedyStatus);
sl@0
   794
	test(speedyStatus==KErrServerTerminated);
sl@0
   795
	test(speedy.ExitReason()==KErrServerTerminated);
sl@0
   796
	test(speedy.ExitType()==EExitKill);
sl@0
   797
	CLOSE_AND_WAIT(speedy);
sl@0
   798
	CLOSE_AND_WAIT(proc);
sl@0
   799
sl@0
   800
	User::After(5000000);	// wait for MMC session to disappear
sl@0
   801
sl@0
   802
	test.Next(_L("Test rename of the processes with duplicate names"));
sl@0
   803
	r = DupRenameProcTest(0);
sl@0
   804
	test(r==KErrNone);
sl@0
   805
sl@0
   806
    TestProcessRename();
sl@0
   807
sl@0
   808
	test.Next(_L("Check for kernel alloc heaven"));
sl@0
   809
	__KHEAP_MARKEND; 
sl@0
   810
sl@0
   811
	test.End();
sl@0
   812
sl@0
   813
	return(KErrNone);
sl@0
   814
	}
sl@0
   815
sl@0
   816