os/kernelhwsrv/userlibandfileserver/fileserver/runtests/runtests.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-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
// f32\runtests\runtests.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32std.h>
sl@0
    19
#include <e32std_private.h>
sl@0
    20
#include <e32def_private.h>
sl@0
    21
#include <e32svr.h>
sl@0
    22
#include <e32ldr.h>
sl@0
    23
#include <hal.h>
sl@0
    24
#include "f32file.h"
sl@0
    25
#include "f32dbg.h"
sl@0
    26
#include "runtests.h"
sl@0
    27
#include <u32hal.h>
sl@0
    28
sl@0
    29
#define __PANIC(r) Panic(__LINE__,r)
sl@0
    30
sl@0
    31
//#define _RUN_FOREVER_
sl@0
    32
//#define _PANIC_ON_FAILURE_
sl@0
    33
sl@0
    34
const TInt KDefaultTimeout=1200;		// 20 minutes
sl@0
    35
const TInt KBackgroundTimeout=3000000;	// 3 seconds
sl@0
    36
const TInt KDestroyTimeout=1000000;		// 1 second
sl@0
    37
sl@0
    38
_LIT(KLitBackslash,"\\");
sl@0
    39
_LIT(KDebugMessage, "Testing finished, and the system will be panicked");
sl@0
    40
#ifdef __EPOC32__
sl@0
    41
_LIT(KLitDefaultTestPath,"Z:\\TEST");
sl@0
    42
#else
sl@0
    43
_LIT(KLitDefaultTestPath,"");
sl@0
    44
#endif
sl@0
    45
sl@0
    46
#ifdef _DEBUG
sl@0
    47
_LIT(KBuildType, "UDEB");
sl@0
    48
#else
sl@0
    49
_LIT(KBuildType, "UREL");
sl@0
    50
#endif
sl@0
    51
sl@0
    52
typedef RArray<TUint> RProcIdList;
sl@0
    53
sl@0
    54
GLDEF_D TPath TheTestPath = KLitDefaultTestPath();
sl@0
    55
GLDEF_D RFs TheFs;
sl@0
    56
GLDEF_D RLoader TheLoaderSession;
sl@0
    57
GLDEF_D TDesC8* TheTestList;
sl@0
    58
GLDEF_D RTimer TheTimer;
sl@0
    59
GLDEF_D TBuf<256> TheProcessCommand=KNullDesC();
sl@0
    60
GLDEF_D TInt TheTimeOut = KDefaultTimeout;
sl@0
    61
GLDEF_D TInt TheCurrentProcessList;
sl@0
    62
GLDEF_D RProcIdList ProcLists[2];
sl@0
    63
GLDEF_D RTimer IdleWaitTimer;
sl@0
    64
GLDEF_D RTimer DestructionTimer;
sl@0
    65
GLDEF_D RProperty CurrTest;
sl@0
    66
TBool ShowTimings = 0;
sl@0
    67
TInt TickPeriod = 15625;
sl@0
    68
TBool CleanUpProcesses = EFalse;
sl@0
    69
sl@0
    70
#ifdef _ENABLE_BTRACE_ANALYSIS_
sl@0
    71
sl@0
    72
// BTrace analysis forward declarations
sl@0
    73
const TInt KDefaultBTraceLevel = 0;
sl@0
    74
TBool BtraceAnalysis = EFalse;
sl@0
    75
TInt BTraceAnalysisLevel;
sl@0
    76
TInt BTraceAnalyseSetup();
sl@0
    77
void BTraceAnalyseEnd();
sl@0
    78
void BTraceAnalyse(TInt aAnalysisLevel);
sl@0
    79
sl@0
    80
#endif //_ENABLE_BTRACE_ANALYSIS_
sl@0
    81
sl@0
    82
_LIT(KLitPanicCategory,"RUNTESTS-");
sl@0
    83
_LIT(KLitLogPreamble,"RUNTESTS: ");
sl@0
    84
sl@0
    85
void LogMsg(TRefByValue<const TDesC> aFmt,...);
sl@0
    86
sl@0
    87
void DisableSimulatedFailure()
sl@0
    88
	{
sl@0
    89
	// Turn off simulated failure mechanisms for all base servers
sl@0
    90
	TheFs.SetAllocFailure(KAllocFailureOff);	// F32 heap failure
sl@0
    91
	TheFs.SetErrorCondition(KErrNone, 0);		// F32 other failure
sl@0
    92
	TheLoaderSession.DebugFunction(ELoaderDebug_SetHeapFail, 0, 0, 0);	// Loader heap failure
sl@0
    93
	TheLoaderSession.DebugFunction(ELoaderDebug_SetRFsFail, KErrNone, 0, 0);	// Loader RFs failure
sl@0
    94
sl@0
    95
	// make sure kernel heap debug is off
sl@0
    96
	__KHEAP_TOTAL_RESET;
sl@0
    97
	}
sl@0
    98
sl@0
    99
GLDEF_C void Panic(TInt aLine, TInt aReason)
sl@0
   100
	{
sl@0
   101
	TBuf<16> cat=KLitPanicCategory();
sl@0
   102
	cat.AppendNum(aLine);
sl@0
   103
	User::Panic(cat,aReason);
sl@0
   104
	}
sl@0
   105
sl@0
   106
TInt CloseAndWait(RHandleBase aH, TRequestStatus *aN = NULL)
sl@0
   107
	{
sl@0
   108
	TRequestStatus tempS;
sl@0
   109
	if(!aN)
sl@0
   110
		{
sl@0
   111
		// Create a destruction notifier if none was supplied.
sl@0
   112
		aH.NotifyDestruction(tempS);
sl@0
   113
		aN = &tempS;
sl@0
   114
		}
sl@0
   115
	if (*aN!=KRequestPending)
sl@0
   116
		{
sl@0
   117
		User::WaitForRequest(*aN);
sl@0
   118
		aH.Close();
sl@0
   119
		return KErrNoMemory;
sl@0
   120
		}
sl@0
   121
	TRequestStatus t;
sl@0
   122
	DestructionTimer.After(t, KDestroyTimeout);
sl@0
   123
	aH.Close();
sl@0
   124
	User::WaitForRequest(*aN, t);
sl@0
   125
	if (*aN != KRequestPending)
sl@0
   126
		{
sl@0
   127
		DestructionTimer.Cancel();
sl@0
   128
		User::WaitForRequest(t);
sl@0
   129
		return KErrNone;
sl@0
   130
		}
sl@0
   131
	User::CancelMiscNotifier(*aN);
sl@0
   132
	User::WaitForRequest(*aN);
sl@0
   133
	return KErrTimedOut;
sl@0
   134
	}
sl@0
   135
sl@0
   136
void CloseWaitAndWarn(RHandleBase aH, TRequestStatus *aN = NULL)
sl@0
   137
	{
sl@0
   138
	TFullName fn(aH.FullName());
sl@0
   139
	TInt r = CloseAndWait(aH, aN);
sl@0
   140
	if (r == KErrNoMemory)
sl@0
   141
		LogMsg(_L("WARNING OOM checking destruction of %S"), &fn);
sl@0
   142
	else if (r == KErrTimedOut)
sl@0
   143
		LogMsg(_L("ERROR Destruction of %S timed out"), &fn);
sl@0
   144
	}
sl@0
   145
sl@0
   146
TInt InitIdleWait()
sl@0
   147
	{
sl@0
   148
	TInt r = IdleWaitTimer.CreateLocal();
sl@0
   149
	if (r!=KErrNone)
sl@0
   150
		return r;
sl@0
   151
	return KErrNone;
sl@0
   152
	}
sl@0
   153
sl@0
   154
void WaitForIdle()
sl@0
   155
	{
sl@0
   156
	TRequestStatus idle_req;
sl@0
   157
	TRequestStatus timer_req;
sl@0
   158
	IdleWaitTimer.After(timer_req, KBackgroundTimeout);
sl@0
   159
	User::NotifyOnIdle(idle_req);
sl@0
   160
	User::WaitForRequest(idle_req, timer_req);
sl@0
   161
	if (idle_req != KRequestPending)
sl@0
   162
		{
sl@0
   163
		IdleWaitTimer.Cancel();
sl@0
   164
		User::WaitForRequest(timer_req);
sl@0
   165
		}
sl@0
   166
	else
sl@0
   167
		{
sl@0
   168
		User::CancelMiscNotifier(idle_req);
sl@0
   169
		User::WaitForRequest(idle_req);
sl@0
   170
		LogMsg(_L("WARNING Excessive Background Activity Detected"));
sl@0
   171
		}
sl@0
   172
	}
sl@0
   173
sl@0
   174
TBool IntentionallyPersistent(RProcess aProcess)
sl@0
   175
	{
sl@0
   176
	TInt v;
sl@0
   177
	TInt r = RProperty::Get(aProcess.SecureId(), KRuntestsIntentionalPersistenceKey, v);
sl@0
   178
	if (r==KErrNone && TUint(v)==KRuntestsIntentionalPersistenceValue)
sl@0
   179
		return ETrue;
sl@0
   180
	return EFalse;
sl@0
   181
	}
sl@0
   182
sl@0
   183
TInt GetProcessListThread(TAny* a)
sl@0
   184
	{
sl@0
   185
	RProcIdList& pl = *(RProcIdList*)a;
sl@0
   186
	TFindProcess fp(_L("*"));
sl@0
   187
	TFullName fn;
sl@0
   188
	TInt r = KErrNone;
sl@0
   189
	while (r==KErrNone && fp.Next(fn)==KErrNone)
sl@0
   190
		{
sl@0
   191
		RProcess p;
sl@0
   192
		r = p.Open(fp, EOwnerThread);
sl@0
   193
		if (r==KErrNone)
sl@0
   194
			{
sl@0
   195
			TUint id = (TUint)p.Id();
sl@0
   196
			r = pl.Append(id);
sl@0
   197
			p.Close();
sl@0
   198
			}
sl@0
   199
		}
sl@0
   200
	return r;
sl@0
   201
	}
sl@0
   202
sl@0
   203
TInt GetProcessList(RProcIdList& aList)
sl@0
   204
	{
sl@0
   205
	aList.Reset();
sl@0
   206
	RThread t;
sl@0
   207
	TRequestStatus s;
sl@0
   208
	TInt r = t.Create(KNullDesC, &GetProcessListThread, 0x1000, NULL, &aList);
sl@0
   209
	if (r==KErrNone)
sl@0
   210
		{
sl@0
   211
		t.Logon(s);
sl@0
   212
		t.SetPriority(EPriorityAbsoluteHigh);
sl@0
   213
		if (s==KRequestPending)
sl@0
   214
			t.Resume();
sl@0
   215
		User::WaitForRequest(s);
sl@0
   216
		r=s.Int();
sl@0
   217
		if (t.ExitType()==EExitPending)
sl@0
   218
			{
sl@0
   219
			t.Kill(0);
sl@0
   220
			WaitForIdle();
sl@0
   221
			}
sl@0
   222
		else if (t.ExitType()!=EExitKill)
sl@0
   223
			{
sl@0
   224
			r = -99;
sl@0
   225
			}
sl@0
   226
		CloseWaitAndWarn(t);
sl@0
   227
		}
sl@0
   228
	aList.Sort();
sl@0
   229
	return r;
sl@0
   230
	}
sl@0
   231
sl@0
   232
TBool ParseNumber(TLex& aLex, TUint& aNumber, TBool isTime)
sl@0
   233
	{
sl@0
   234
	TPtrC numberDes = aLex.NextToken();
sl@0
   235
	TInt len = numberDes.Length();
sl@0
   236
	if (len == 0)
sl@0
   237
		{
sl@0
   238
		return EFalse;
sl@0
   239
		}
sl@0
   240
sl@0
   241
	aNumber = 0;
sl@0
   242
	TInt magnitude = 1;
sl@0
   243
	TChar c = numberDes[len-1];
sl@0
   244
	if (isTime)
sl@0
   245
		{
sl@0
   246
		switch (c)
sl@0
   247
			{
sl@0
   248
			case 'h':
sl@0
   249
			case 'H':
sl@0
   250
				len -= 1;
sl@0
   251
				magnitude = 3600;
sl@0
   252
				break;
sl@0
   253
sl@0
   254
			case 'm':
sl@0
   255
			case 'M':
sl@0
   256
				len -= 1;
sl@0
   257
				/*FALLTHRU*/
sl@0
   258
			default:
sl@0
   259
				magnitude = 60;
sl@0
   260
				break;
sl@0
   261
sl@0
   262
			case 's':
sl@0
   263
			case 'S':
sl@0
   264
				len -= 1;
sl@0
   265
				magnitude = 1;
sl@0
   266
				break;
sl@0
   267
			}
sl@0
   268
		}
sl@0
   269
sl@0
   270
	for (TInt i = len-1; i >= 0; --i)
sl@0
   271
		{
sl@0
   272
		c = numberDes[i];
sl@0
   273
		if (c < '0' || c > '9')
sl@0
   274
			__PANIC(KErrArgument);
sl@0
   275
		aNumber += ((TInt)c-'0')*magnitude;
sl@0
   276
		magnitude *= 10;
sl@0
   277
		}
sl@0
   278
sl@0
   279
	return ETrue;
sl@0
   280
	}
sl@0
   281
sl@0
   282
void GetTimeOut(TLex& aLex)
sl@0
   283
//
sl@0
   284
//
sl@0
   285
//
sl@0
   286
	{
sl@0
   287
	TheTimeOut = KDefaultTimeout;
sl@0
   288
	TUint timeOut = 0;
sl@0
   289
	if (ParseNumber(aLex, timeOut, ETrue))
sl@0
   290
		{
sl@0
   291
		TheTimeOut = timeOut;
sl@0
   292
		}
sl@0
   293
	}
sl@0
   294
sl@0
   295
#ifdef _ENABLE_BTRACE_ANALYSIS_
sl@0
   296
sl@0
   297
void GetAnalysisLevel(TLex& aLex)
sl@0
   298
	{
sl@0
   299
	BTraceAnalysisLevel = KDefaultBTraceLevel;
sl@0
   300
	TUint level;
sl@0
   301
	if (ParseNumber(aLex, level, EFalse))
sl@0
   302
		{
sl@0
   303
		BTraceAnalysisLevel = level;
sl@0
   304
		}
sl@0
   305
	}
sl@0
   306
#endif //_ENABLE_BTRACE_ANALYSIS_
sl@0
   307
sl@0
   308
void LogMsg(TRefByValue<const TDesC> aFmt,...)
sl@0
   309
	{
sl@0
   310
	VA_LIST list;
sl@0
   311
	VA_START(list,aFmt);
sl@0
   312
	TBuf<0x100> buf=KLitLogPreamble();
sl@0
   313
	buf.AppendFormatList(aFmt,list);
sl@0
   314
	RDebug::Print(_L("%S"),&buf);
sl@0
   315
	}
sl@0
   316
sl@0
   317
_LIT(KLitError, "Error ");
sl@0
   318
TBool LogProcess(TUint aId, TBool aInit)
sl@0
   319
	{
sl@0
   320
	TFullName pn;
sl@0
   321
	TFileName fn;
sl@0
   322
	RProcess p;
sl@0
   323
	TBool killed = EFalse;
sl@0
   324
	TInt r = p.Open(TProcessId(aId));
sl@0
   325
	if (r==KErrNone)
sl@0
   326
		{
sl@0
   327
		if (IntentionallyPersistent(p))
sl@0
   328
			{
sl@0
   329
			p.Close();
sl@0
   330
			return killed;
sl@0
   331
			}
sl@0
   332
		pn = p.FullName();
sl@0
   333
		fn = p.FileName();
sl@0
   334
		if (!aInit && CleanUpProcesses && p.ExitType()==EExitPending)
sl@0
   335
			{// p is a left over process so terminate it.
sl@0
   336
			killed = ETrue;
sl@0
   337
			TRequestStatus status;
sl@0
   338
			p.Logon(status);
sl@0
   339
			p.Kill(KErrNone);	// Kill with KErrNone to suppress extra debug output from kernel.
sl@0
   340
			User::WaitForRequest(status);
sl@0
   341
			CloseAndWait(p);
sl@0
   342
			}
sl@0
   343
		else
sl@0
   344
			{
sl@0
   345
			p.Close();
sl@0
   346
			}
sl@0
   347
		}
sl@0
   348
	else
sl@0
   349
		{
sl@0
   350
		pn = KLitError;
sl@0
   351
		pn.AppendNum(r);
sl@0
   352
		}
sl@0
   353
	if (aInit)
sl@0
   354
		LogMsg(_L("Running process id=%d: %S (%S)"),aId,&pn,&fn);
sl@0
   355
	else
sl@0
   356
		{
sl@0
   357
		if(killed)
sl@0
   358
			LogMsg(_L("ERROR Leftover process was killed id=%d: %S (%S)"),aId,&pn,&fn);
sl@0
   359
		else
sl@0
   360
			LogMsg(_L("ERROR Leftover process id=%d: %S (%S)"),aId,&pn,&fn);
sl@0
   361
		}
sl@0
   362
	return killed;
sl@0
   363
	}
sl@0
   364
sl@0
   365
void ListProcesses()
sl@0
   366
	{
sl@0
   367
	RProcIdList& cur_list = ProcLists[TheCurrentProcessList];
sl@0
   368
	TInt cc = cur_list.Count();
sl@0
   369
	TInt ci;
sl@0
   370
	for (ci=0; ci<cc; ++ci)
sl@0
   371
		{
sl@0
   372
		LogProcess(cur_list[ci], ETrue);
sl@0
   373
		}
sl@0
   374
	}
sl@0
   375
sl@0
   376
void CheckProcesses()
sl@0
   377
	{
sl@0
   378
	RProcIdList& cur_list = ProcLists[TheCurrentProcessList];
sl@0
   379
	RProcIdList& new_list = ProcLists[1-TheCurrentProcessList];
sl@0
   380
	TInt r = GetProcessList(new_list);
sl@0
   381
	if (r!=KErrNone)
sl@0
   382
		{
sl@0
   383
		LogMsg(_L("WARNING Problem getting process list, error %d"),r);
sl@0
   384
		return;
sl@0
   385
		}
sl@0
   386
sl@0
   387
	TInt cc = cur_list.Count();
sl@0
   388
	TInt nc = new_list.Count();
sl@0
   389
	TInt ci = 0;
sl@0
   390
	TInt ni = 0;
sl@0
   391
	while (ci<cc || ni<nc)
sl@0
   392
		{
sl@0
   393
		TUint id1=0;
sl@0
   394
		TUint id2=0;
sl@0
   395
		if (ci<cc)
sl@0
   396
			id1 = cur_list[ci];
sl@0
   397
		if (ni<nc)
sl@0
   398
			id2 = new_list[ni];
sl@0
   399
		if (ci==cc)
sl@0
   400
			{
sl@0
   401
			// extra process has appeared so kill it and output an error message.
sl@0
   402
			if (LogProcess(id2, EFalse))
sl@0
   403
				{// Remove from list as we don't want it to be considered as vanished when the next test completes.
sl@0
   404
				new_list.Remove(ni);
sl@0
   405
				nc--;
sl@0
   406
				}
sl@0
   407
			else
sl@0
   408
				{// Extra process was left running so just move onto the next one.
sl@0
   409
				ni++;
sl@0
   410
				}
sl@0
   411
			}
sl@0
   412
		else if (ni<nc && id1==id2)
sl@0
   413
			{
sl@0
   414
			// process remains
sl@0
   415
			++ci, ++ni;
sl@0
   416
			}
sl@0
   417
		else
sl@0
   418
			{
sl@0
   419
			// process has disappeared
sl@0
   420
			LogMsg(_L("WARNING Vanished process, id=%d"),id1);
sl@0
   421
			++ci;
sl@0
   422
			}
sl@0
   423
		}
sl@0
   424
sl@0
   425
	// current list = new list
sl@0
   426
	TheCurrentProcessList = 1 - TheCurrentProcessList;
sl@0
   427
sl@0
   428
	// throw away the old list
sl@0
   429
	cur_list.Reset();
sl@0
   430
	}
sl@0
   431
sl@0
   432
_LIT8 (KLitRemark,"REM");
sl@0
   433
_LIT8 (KLitAtRemark,"@REM");
sl@0
   434
sl@0
   435
void ProcessLine(const TDesC8& aLine)
sl@0
   436
	{
sl@0
   437
	TLex8 lex(aLine);
sl@0
   438
	TPtrC8 testname=lex.NextToken();
sl@0
   439
	if (testname.Length()>=2 && testname[0]=='/' && testname[1]=='/')
sl@0
   440
		return;
sl@0
   441
	// ignore this line if it begins with rem or @rem 
sl@0
   442
	if (testname.CompareF(KLitRemark) == 0 || testname.CompareF(KLitAtRemark) == 0)
sl@0
   443
		return;
sl@0
   444
	TFileName testnameU;
sl@0
   445
	testnameU.Copy(testname);
sl@0
   446
	TFileName fullpathname;
sl@0
   447
	if (testnameU.Locate(TChar('\\'))==KErrNotFound)
sl@0
   448
		fullpathname=TheTestPath;
sl@0
   449
	fullpathname+=testnameU;
sl@0
   450
	if (testname.Locate(TChar('.'))==KErrNotFound)
sl@0
   451
		fullpathname+=_L(".EXE");
sl@0
   452
	TInt r;
sl@0
   453
sl@0
   454
	RFile file;
sl@0
   455
	r=file.Open(TheFs,fullpathname,EFileRead);
sl@0
   456
	if (r!=KErrNone)
sl@0
   457
		{
sl@0
   458
		// Remove path to let loader locate exe
sl@0
   459
		fullpathname = fullpathname.Mid(fullpathname.LocateReverse('\\')+1);
sl@0
   460
		}
sl@0
   461
	else
sl@0
   462
		file.Close();
sl@0
   463
sl@0
   464
	RProcess p;
sl@0
   465
	if(TheProcessCommand==KNullDesC)
sl@0
   466
		{
sl@0
   467
		TheProcessCommand.Copy(lex.Remainder());
sl@0
   468
		r=p.Create(fullpathname, TheProcessCommand);
sl@0
   469
		TheProcessCommand=KNullDesC();
sl@0
   470
		}
sl@0
   471
	else
sl@0
   472
		r=p.Create(fullpathname, TheProcessCommand);
sl@0
   473
	if (r!=KErrNone)
sl@0
   474
		{
sl@0
   475
		LogMsg(_L("Test %S ERROR Could not load file, error %d"),&fullpathname,r);
sl@0
   476
		return;
sl@0
   477
		}
sl@0
   478
	else
sl@0
   479
		{
sl@0
   480
		LogMsg(_L("Started test %S"),&fullpathname);
sl@0
   481
		}
sl@0
   482
	TRequestStatus ds;
sl@0
   483
	p.NotifyDestruction(ds);	// allocate the destruction notifier early so that it doesn't get flagged as a leak by kernel heap checking in e.g., efile (DEF133800)
sl@0
   484
	CurrTest.Set(p.FileName());
sl@0
   485
	User::After(100000);		// allow latency measurements to be output
sl@0
   486
	p.SetJustInTime(EFalse);	// we don't want the automatic test run to be halted by the debugger
sl@0
   487
	TRequestStatus ps;
sl@0
   488
	p.Rendezvous(ps);
sl@0
   489
	TInt time_remain = TheTimeOut;
sl@0
   490
	TRequestStatus ts;
sl@0
   491
	TUint start = User::TickCount();
sl@0
   492
	p.Resume();
sl@0
   493
	TBool persist = EFalse;
sl@0
   494
	TBool timer_running = EFalse;
sl@0
   495
	FOREVER
sl@0
   496
		{
sl@0
   497
		TInt nsec = Min(time_remain, 1800);
sl@0
   498
		if (!timer_running)
sl@0
   499
			TheTimer.After(ts, nsec*1000000);
sl@0
   500
		timer_running = ETrue;
sl@0
   501
		User::WaitForRequest(ps,ts);
sl@0
   502
		if (ps!=KRequestPending)
sl@0
   503
			{
sl@0
   504
			if (p.ExitType()==EExitPending)
sl@0
   505
				{
sl@0
   506
				// rendezvous completed but process not terminated
sl@0
   507
				if (!IntentionallyPersistent(p))
sl@0
   508
					{
sl@0
   509
					// not persistent - wait for process to terminate
sl@0
   510
					p.Logon(ps);
sl@0
   511
					continue;
sl@0
   512
					}
sl@0
   513
				persist = ETrue;
sl@0
   514
				}
sl@0
   515
			break;
sl@0
   516
			}
sl@0
   517
		timer_running = EFalse;
sl@0
   518
		time_remain -= nsec;
sl@0
   519
		if (time_remain==0)
sl@0
   520
			{
sl@0
   521
			LogMsg(_L("Going to kill test %S: it's taken %u seconds, which is too long"),&fullpathname,TheTimeOut);
sl@0
   522
			p.Kill(0);
sl@0
   523
			User::WaitForRequest(ps);
sl@0
   524
			p.Logon(ps);
sl@0
   525
			User::WaitForRequest(ps);
sl@0
   526
sl@0
   527
			CloseWaitAndWarn(p, &ds);
sl@0
   528
			RDebug::Print(_L("\n"));
sl@0
   529
			LogMsg(_L("Test %S TIMEOUT"),&fullpathname);
sl@0
   530
			return;
sl@0
   531
			}
sl@0
   532
		else
sl@0
   533
			{
sl@0
   534
			LogMsg(_L("Taken %u seconds so far"),TheTimeOut-time_remain);
sl@0
   535
			}
sl@0
   536
		}
sl@0
   537
	TUint end = User::TickCount();
sl@0
   538
	if(timer_running)
sl@0
   539
		{
sl@0
   540
		TheTimer.Cancel();
sl@0
   541
		User::WaitForRequest(ts);
sl@0
   542
		}
sl@0
   543
sl@0
   544
#ifdef _ENABLE_BTRACE_ANALYSIS_
sl@0
   545
	//
sl@0
   546
	//
sl@0
   547
	//
sl@0
   548
	if (BtraceAnalysis)
sl@0
   549
		{// Analyse BTrace buffer
sl@0
   550
		BTraceAnalyse(BTraceAnalysisLevel);
sl@0
   551
		}
sl@0
   552
#endif //_ENABLE_BTRACE_ANALYSIS_
sl@0
   553
sl@0
   554
	TBuf<32> exitCat=p.ExitCategory();
sl@0
   555
	TExitType exitType=p.ExitType();
sl@0
   556
	TInt exitReason=p.ExitReason();
sl@0
   557
	if (persist || (exitType==EExitKill && exitReason==KErrNone))
sl@0
   558
		{
sl@0
   559
		TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
sl@0
   560
		if(ShowTimings)
sl@0
   561
			{
sl@0
   562
			LogMsg(_L("Test %S OK - Seconds Taken: %u"),&fullpathname, time);
sl@0
   563
			}
sl@0
   564
		else
sl@0
   565
			{
sl@0
   566
			LogMsg(_L("Test %S OK"),&fullpathname);
sl@0
   567
			}
sl@0
   568
		if (persist)
sl@0
   569
			{
sl@0
   570
			// We do not need this destruction notifier so cancel it.
sl@0
   571
			User::CancelMiscNotifier(ds);
sl@0
   572
			User::WaitForRequest(ds);
sl@0
   573
			
sl@0
   574
			p.Close();
sl@0
   575
			}
sl@0
   576
		else
sl@0
   577
			{
sl@0
   578
			CloseWaitAndWarn(p, &ds);
sl@0
   579
			}
sl@0
   580
		return;
sl@0
   581
		}
sl@0
   582
	LogMsg(_L("Test %S FAIL - Exit code %d,%d,%S"),&fullpathname,exitType,exitReason,&exitCat);
sl@0
   583
	CloseWaitAndWarn(p, &ds);
sl@0
   584
#if defined(_PANIC_ON_FAILURE_)
sl@0
   585
	__PANIC(KErrGeneral);
sl@0
   586
#endif
sl@0
   587
	}
sl@0
   588
sl@0
   589
void ProcessTestList()
sl@0
   590
	{
sl@0
   591
	TUint start = User::TickCount();
sl@0
   592
sl@0
   593
	TLex8 llex(*TheTestList);
sl@0
   594
	while(!llex.Eos())
sl@0
   595
		{
sl@0
   596
		llex.SkipSpace();
sl@0
   597
		llex.Mark();
sl@0
   598
		while(!llex.Eos() && llex.Peek()!='\n' && llex.Peek()!='\r')
sl@0
   599
			llex.Inc();
sl@0
   600
		TPtrC8 line=llex.MarkedToken();
sl@0
   601
		if (line.Length()!=0)
sl@0
   602
			ProcessLine(line);
sl@0
   603
sl@0
   604
		// allow cleanup to complete before starting the next test
sl@0
   605
		WaitForIdle();
sl@0
   606
sl@0
   607
		// make sure simulated failure is off
sl@0
   608
		DisableSimulatedFailure();
sl@0
   609
sl@0
   610
		// check for leftover processes
sl@0
   611
		CheckProcesses();
sl@0
   612
		
sl@0
   613
		// Reset the demand paging cache to its default size.
sl@0
   614
		UserSvr::HalFunction(EHalGroupVM,EVMHalSetCacheSize, 0, 0);
sl@0
   615
		}
sl@0
   616
sl@0
   617
	TUint end = User::TickCount();
sl@0
   618
	TUint time = TUint((TUint64)(end-start)*(TUint64)TickPeriod/(TUint64)1000000);
sl@0
   619
	LogMsg(_L("Elapsed Seconds: %u"), time);
sl@0
   620
	}
sl@0
   621
sl@0
   622
void Help()
sl@0
   623
	{
sl@0
   624
	RDebug::Print(_L("Runtests test list [-x where tests are] [-d drive letter to test] [-t timeout] [-p] [-st] [-c]"));
sl@0
   625
	RDebug::Print(_L("where -p sets runtests to be system permanent, -st enables timing information,"));
sl@0
   626
	RDebug::Print(_L("-c enables left over processes to be cleaned up"));
sl@0
   627
	}
sl@0
   628
sl@0
   629
GLDEF_C TInt E32Main()
sl@0
   630
	{
sl@0
   631
	HAL::Get(HAL::ESystemTickPeriod, TickPeriod);
sl@0
   632
	RThread().SetPriority(EPriorityAbsoluteHigh);
sl@0
   633
	TBuf<0x100> cmd;
sl@0
   634
	User::CommandLine(cmd);
sl@0
   635
	TFileName thisfile=RProcess().FileName();
sl@0
   636
	TLex lex(cmd);
sl@0
   637
	TPtrC token=lex.NextToken();
sl@0
   638
	if (token.MatchF(thisfile)==0)
sl@0
   639
		{
sl@0
   640
		token.Set(lex.NextToken());
sl@0
   641
		}
sl@0
   642
	if (token.Length()==0)
sl@0
   643
		{//	__PANIC(0);
sl@0
   644
		Help();
sl@0
   645
		return 0;
sl@0
   646
		}
sl@0
   647
	TFileName listfilename=token;
sl@0
   648
	while (!lex.Eos())
sl@0
   649
		{
sl@0
   650
		token.Set(lex.NextToken());
sl@0
   651
		if (token.Length()==0)
sl@0
   652
			break;	// ignore trailing whitespace
sl@0
   653
		else if (token==_L("-x"))
sl@0
   654
			{
sl@0
   655
			token.Set(lex.NextToken());
sl@0
   656
			TheTestPath = token;
sl@0
   657
			}
sl@0
   658
		else if (token==_L("-d"))
sl@0
   659
			{
sl@0
   660
			token.Set(lex.NextToken());
sl@0
   661
			TheProcessCommand = token;
sl@0
   662
			}
sl@0
   663
		else if (token==_L("-t"))
sl@0
   664
			{
sl@0
   665
			GetTimeOut(lex);
sl@0
   666
			}
sl@0
   667
		else if (token==_L("-p"))
sl@0
   668
			{
sl@0
   669
			User::SetCritical(User::ESystemPermanent);
sl@0
   670
			}
sl@0
   671
		else if (token==_L("-st"))
sl@0
   672
			ShowTimings = 1;
sl@0
   673
		
sl@0
   674
#ifdef _ENABLE_BTRACE_ANALYSIS_		
sl@0
   675
		else if (token == _L("-a"))
sl@0
   676
			{
sl@0
   677
			BtraceAnalysis = ETrue;
sl@0
   678
			GetAnalysisLevel(lex);
sl@0
   679
			TInt r = BTraceAnalyseSetup();
sl@0
   680
			if (r != KErrNone)
sl@0
   681
				{
sl@0
   682
				RDebug::Print(_L("ERROR - Couldn't open BTrace driver (Code %d)"), r);
sl@0
   683
				return 0;
sl@0
   684
				}
sl@0
   685
			}
sl@0
   686
#endif //_ENABLE_BTRACE_ANALYSIS_
sl@0
   687
sl@0
   688
		else if (token == _L("-c"))
sl@0
   689
			CleanUpProcesses = ETrue;
sl@0
   690
		else
sl@0
   691
			{
sl@0
   692
			RDebug::Print(_L("Unknown option %S"), &token);
sl@0
   693
			Help();
sl@0
   694
			return 0;
sl@0
   695
			}
sl@0
   696
		}
sl@0
   697
		
sl@0
   698
	RDebug::Print(_L("TPTT= %S \n"), &TheProcessCommand);											
sl@0
   699
	RDebug::Print(_L("TTL= %S \n"), &listfilename);
sl@0
   700
	RDebug::Print(_L("TTP= %S \n"), &TheTestPath);
sl@0
   701
	RDebug::Print(_L("TO= %d seconds\n"), TheTimeOut);
sl@0
   702
sl@0
   703
	TInt l=TheTestPath.Length();
sl@0
   704
	if (l > 0 && TheTestPath[l-1]!='\\')
sl@0
   705
		TheTestPath+=KLitBackslash;
sl@0
   706
	if (listfilename.Locate(TChar('\\'))==KErrNotFound)
sl@0
   707
		listfilename.Insert(0,TheTestPath);
sl@0
   708
	TInt r=TheFs.Connect();
sl@0
   709
	if (r!=KErrNone)
sl@0
   710
		__PANIC(r);
sl@0
   711
	r = TheLoaderSession.Connect();
sl@0
   712
	if (r!=KErrNone)
sl@0
   713
		__PANIC(r);
sl@0
   714
	DisableSimulatedFailure();
sl@0
   715
	r=TheFs.SetSessionPath(_L("Z:\\test\\"));
sl@0
   716
	if (r!=KErrNone)
sl@0
   717
		__PANIC(r);
sl@0
   718
	r=TheTimer.CreateLocal();
sl@0
   719
	if (r!=KErrNone)
sl@0
   720
		__PANIC(r);
sl@0
   721
	RFile listfile;
sl@0
   722
	r=listfile.Open(TheFs,listfilename,EFileRead|EFileShareAny);
sl@0
   723
	if (r!=KErrNone)
sl@0
   724
		__PANIC(r);
sl@0
   725
	TInt listfilesize;
sl@0
   726
	r=listfile.Size(listfilesize);
sl@0
   727
	if (r!=KErrNone)
sl@0
   728
		__PANIC(r);
sl@0
   729
	HBufC8* pL=HBufC8::New(listfilesize);
sl@0
   730
	if (!pL)
sl@0
   731
		__PANIC(KErrNoMemory);
sl@0
   732
	TPtr8 ptr=pL->Des();
sl@0
   733
	TheTestList=pL;
sl@0
   734
	r=listfile.Read(ptr);
sl@0
   735
	if (r!=KErrNone)
sl@0
   736
		__PANIC(r);
sl@0
   737
	listfile.Close();
sl@0
   738
	LogMsg(_L("Running test script %S"),&listfilename);
sl@0
   739
	LogMsg(_L("Build %S"),&KBuildType);
sl@0
   740
	LogMsg(_L("Path to test %S"),&TheProcessCommand);
sl@0
   741
sl@0
   742
	r = RProperty::Define(	KRuntestsCurrentTestKey,
sl@0
   743
							RProperty::EText,
sl@0
   744
							TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
sl@0
   745
							TSecurityPolicy(RProcess().SecureId()),
sl@0
   746
							512
sl@0
   747
						);
sl@0
   748
	if (r!=KErrNone && r!=KErrAlreadyExists)
sl@0
   749
		__PANIC(r);
sl@0
   750
	r = CurrTest.Attach(RProcess().SecureId(), KRuntestsCurrentTestKey);
sl@0
   751
	if (r!=KErrNone)
sl@0
   752
		__PANIC(r);
sl@0
   753
	r = CurrTest.Set(KNullDesC);
sl@0
   754
	if (r!=KErrNone)
sl@0
   755
		__PANIC(r);
sl@0
   756
sl@0
   757
	r = DestructionTimer.CreateLocal();
sl@0
   758
	if (r!=KErrNone)
sl@0
   759
		__PANIC(r);
sl@0
   760
	TheCurrentProcessList = 0;
sl@0
   761
	r = GetProcessList(ProcLists[0]);
sl@0
   762
	if (r!=KErrNone)
sl@0
   763
		__PANIC(r);
sl@0
   764
	ListProcesses();
sl@0
   765
	r = InitIdleWait();
sl@0
   766
	if (r!=KErrNone)
sl@0
   767
		__PANIC(r);
sl@0
   768
#if defined(_RUN_FOREVER_)
sl@0
   769
	FOREVER
sl@0
   770
#endif
sl@0
   771
	ProcessTestList();
sl@0
   772
	r = CurrTest.Set(KNullDesC);
sl@0
   773
	if (r!=KErrNone)
sl@0
   774
		__PANIC(r);
sl@0
   775
	CurrTest.Close();
sl@0
   776
	User::After(1000000);	// allow latency measurements to be output before exiting
sl@0
   777
	LogMsg(_L("Completed test script %S"),&listfilename);
sl@0
   778
	TheLoaderSession.Close();
sl@0
   779
	TheFs.Close();
sl@0
   780
	TheTimer.Close();
sl@0
   781
	IdleWaitTimer.Close();
sl@0
   782
	DestructionTimer.Close();
sl@0
   783
sl@0
   784
#ifdef _ENABLE_BTRACE_ANALYSIS_
sl@0
   785
	BTraceAnalyseEnd();
sl@0
   786
#endif //_ENABLE_BTRACE_ANALYSIS_
sl@0
   787
	if(User::Critical()==User::ESystemPermanent)
sl@0
   788
		RDebug::Print(KDebugMessage);
sl@0
   789
	return KErrNone;
sl@0
   790
	}