os/security/authorisation/userpromptservice/server/test/upstest/upstestoom.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
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Test program exercises the skeleton UPS server API.
sl@0
    16
* See individual test functions for more information.
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include <e32ldr.h>
sl@0
    26
#include <e32ldr_private.h>
sl@0
    27
#include "rtestwrapper.h"
sl@0
    28
sl@0
    29
#include <ups/upsclient.h>
sl@0
    30
#include "f32file.h"
sl@0
    31
sl@0
    32
using namespace UserPromptService;
sl@0
    33
sl@0
    34
/** Top-level test object renders stages and confirms conditions. */
sl@0
    35
static RTestWrapper test(_L("UPSTESTOOM"));
sl@0
    36
sl@0
    37
sl@0
    38
static void TestFlurryL()
sl@0
    39
/**
sl@0
    40
	Launch multiple requests
sl@0
    41
 */
sl@0
    42
	{
sl@0
    43
	RUpsSession session;
sl@0
    44
	User::LeaveIfError(session.Connect());
sl@0
    45
	CleanupClosePushL(session);
sl@0
    46
sl@0
    47
	RThread thd;
sl@0
    48
sl@0
    49
#if 0
sl@0
    50
	RUpsSubsession testSubsession1;
sl@0
    51
	testSubsession1.Initialise(session, thd);
sl@0
    52
	
sl@0
    53
	TServiceId serviceId = {42};
sl@0
    54
	TRequestStatus rs1;
sl@0
    55
	
sl@0
    56
	// complete normally - no opaque data
sl@0
    57
	TUpsDecision dec1 = EUpsDecYes;
sl@0
    58
	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
sl@0
    59
sl@0
    60
	User::After(1000);
sl@0
    61
sl@0
    62
	User::WaitForRequest(rs1);
sl@0
    63
	User::LeaveIfError(rs1.Int());
sl@0
    64
	test(dec1 == EUpsDecNo);
sl@0
    65
#else
sl@0
    66
	RUpsSubsession testSubsession1;
sl@0
    67
	testSubsession1.Initialise(session, thd);
sl@0
    68
	RUpsSubsession testSubsession2;
sl@0
    69
	testSubsession2.Initialise(session, thd);
sl@0
    70
	RUpsSubsession testSubsession3;
sl@0
    71
	testSubsession3.Initialise(session, thd);
sl@0
    72
	RUpsSubsession testSubsession4;
sl@0
    73
	testSubsession4.Initialise(session, thd);
sl@0
    74
	RUpsSubsession testSubsession5;
sl@0
    75
	testSubsession5.Initialise(session, thd);
sl@0
    76
	RUpsSubsession testSubsession6;
sl@0
    77
	testSubsession6.Initialise(session, thd);
sl@0
    78
	
sl@0
    79
	TServiceId serviceId = {42};
sl@0
    80
	TRequestStatus rs1;
sl@0
    81
	TRequestStatus rs2;
sl@0
    82
	TRequestStatus rs3;
sl@0
    83
	TRequestStatus rs4;
sl@0
    84
	TRequestStatus rs5;
sl@0
    85
	TRequestStatus rs6;
sl@0
    86
	
sl@0
    87
	// complete normally - no opaque data
sl@0
    88
	TUpsDecision dec1 = EUpsDecYes;
sl@0
    89
	TUpsDecision dec2 = EUpsDecYes;
sl@0
    90
	TUpsDecision dec3 = EUpsDecYes;
sl@0
    91
	TUpsDecision dec4 = EUpsDecYes;
sl@0
    92
	TUpsDecision dec5 = EUpsDecYes;
sl@0
    93
	TUpsDecision dec6 = EUpsDecYes;
sl@0
    94
	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
sl@0
    95
	testSubsession2.Authorise(EFalse, serviceId, _L("reqX"), dec2, rs2);
sl@0
    96
	testSubsession3.Authorise(EFalse, serviceId, _L("req2"), dec3, rs3);
sl@0
    97
	testSubsession4.Authorise(EFalse, serviceId, _L("reqX"), dec4, rs4);
sl@0
    98
	testSubsession5.Authorise(EFalse, serviceId, _L("req3"), dec5, rs5);
sl@0
    99
	testSubsession6.Authorise(EFalse, serviceId, _L("req4"), dec6, rs6);
sl@0
   100
sl@0
   101
	User::After(1000);
sl@0
   102
sl@0
   103
	User::WaitForRequest(rs1);
sl@0
   104
sl@0
   105
	User::WaitForRequest(rs2);
sl@0
   106
	User::WaitForRequest(rs4);
sl@0
   107
sl@0
   108
	User::WaitForRequest(rs3);
sl@0
   109
	User::WaitForRequest(rs5);
sl@0
   110
	User::WaitForRequest(rs6);
sl@0
   111
	User::LeaveIfError(rs1.Int());
sl@0
   112
	User::LeaveIfError(rs2.Int());
sl@0
   113
	User::LeaveIfError(rs3.Int());
sl@0
   114
	User::LeaveIfError(rs4.Int());
sl@0
   115
	User::LeaveIfError(rs5.Int());
sl@0
   116
	User::LeaveIfError(rs6.Int());
sl@0
   117
	test(dec1 == EUpsDecNo);
sl@0
   118
	test(dec2 == EUpsDecNo);
sl@0
   119
	test(dec3 == EUpsDecNo);
sl@0
   120
	test(dec4 == EUpsDecNo);
sl@0
   121
	test(dec5 == EUpsDecNo);
sl@0
   122
	test(dec6 == EUpsDecNo);
sl@0
   123
#endif
sl@0
   124
	
sl@0
   125
	CleanupStack::PopAndDestroy(&session);
sl@0
   126
	}
sl@0
   127
sl@0
   128
static void TestRUpsManagementL()
sl@0
   129
/**
sl@0
   130
	Attempt to delete database
sl@0
   131
 */
sl@0
   132
	{
sl@0
   133
//	test.Start(_L("Testing RUpsManagement"));
sl@0
   134
	RThread thd;
sl@0
   135
	TRequestStatus rs;
sl@0
   136
	TInt r;
sl@0
   137
sl@0
   138
	// Create filter
sl@0
   139
	TServiceId serviceId = {43};
sl@0
   140
	CDecisionFilter *filter = CDecisionFilter::NewLC();
sl@0
   141
	filter->SetClientSid(thd.SecureId(), EEqual);
sl@0
   142
	filter->SetServerSid(thd.SecureId(), EEqual);
sl@0
   143
	filter->SetServiceId(serviceId, EEqual);
sl@0
   144
sl@0
   145
	RUpsSession session;
sl@0
   146
	User::LeaveIfError(session.Connect());
sl@0
   147
	CleanupClosePushL(session);
sl@0
   148
sl@0
   149
	RUpsSubsession clientSubsession;
sl@0
   150
	User::LeaveIfError(clientSubsession.Initialise(session, thd));
sl@0
   151
	CleanupClosePushL(clientSubsession);
sl@0
   152
sl@0
   153
	test.Printf(_L("Open management session\n"));
sl@0
   154
	RUpsManagement mngmnt;
sl@0
   155
	User::LeaveIfError(mngmnt.Connect());
sl@0
   156
	CleanupClosePushL(mngmnt);
sl@0
   157
	
sl@0
   158
	test.Printf(_L("View create - then delete DB\n"));
sl@0
   159
	mngmnt.CreateView(*filter, rs);
sl@0
   160
sl@0
   161
	test.Printf(_L("Delete database\n"));
sl@0
   162
	TRAP(r, mngmnt.DeleteDatabaseL());
sl@0
   163
	test((r == KErrNone) || (r == KErrNoMemory));
sl@0
   164
sl@0
   165
	test.Printf(_L("Now see what view create completed with....\n"));
sl@0
   166
	User::WaitForRequest(rs);
sl@0
   167
	// The createview may actually complete if the DeleteDatabase fails due to OOM
sl@0
   168
//	test((rs.Int() == KErrAbort) || (rs.Int() == KErrNoMemory) || (rs.Int() == KErrNone));
sl@0
   169
	test((rs.Int() == KErrAbort) || (rs.Int() == KErrNoMemory));
sl@0
   170
sl@0
   171
	test.Printf(_L("Add entry to new database\n"));
sl@0
   172
	TUpsDecision dec = EUpsDecYes;
sl@0
   173
	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 1"), _L8("Opaque data"), dec, rs);
sl@0
   174
	User::WaitForRequest(rs);
sl@0
   175
	test(rs == KErrNone);
sl@0
   176
	test(dec == EUpsDecNo);
sl@0
   177
sl@0
   178
	dec = EUpsDecYes;
sl@0
   179
	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 2"), _L8("Opaque data"), dec, rs);
sl@0
   180
	User::WaitForRequest(rs);
sl@0
   181
	test(rs == KErrNone);
sl@0
   182
	test(dec == EUpsDecNo);
sl@0
   183
sl@0
   184
sl@0
   185
	test.Printf(_L("View create - immediate cancel\n"));
sl@0
   186
	mngmnt.CreateView(*filter, rs);
sl@0
   187
	mngmnt.CancelAndCloseView();
sl@0
   188
	User::WaitForRequest(rs);
sl@0
   189
	test((rs.Int() == KErrCancel) || (rs.Int() == KErrNoMemory));
sl@0
   190
sl@0
   191
	test.Printf(_L("View create - iterate through it\n"));
sl@0
   192
	mngmnt.CreateView(*filter, rs);
sl@0
   193
sl@0
   194
	User::WaitForRequest(rs);
sl@0
   195
	test((rs.Int() == KErrNone) || (rs.Int() == KErrNoMemory));
sl@0
   196
	
sl@0
   197
sl@0
   198
	CDecisionRecord *record = 0;
sl@0
   199
	r = KErrNone;
sl@0
   200
	while(r == KErrNone)
sl@0
   201
		{
sl@0
   202
		TRAP(r, record = mngmnt.NextMatchL());
sl@0
   203
		if(record == 0)
sl@0
   204
			{
sl@0
   205
			break;
sl@0
   206
			}
sl@0
   207
		test(r == KErrNone);
sl@0
   208
    	if(r == KErrNone)
sl@0
   209
    		{
sl@0
   210
    		CleanupStack::PushL(record);
sl@0
   211
			CDecisionFilter *exactFilter = CDecisionFilter::NewLC(record->iClientSid,
sl@0
   212
																  record->iEvaluatorId,
sl@0
   213
																  record->iServiceId,
sl@0
   214
																  record->iServerSid,
sl@0
   215
																  record->iFingerprint,
sl@0
   216
																  record->iClientEntity,
sl@0
   217
																  record->iMajorPolicyVersion);
sl@0
   218
				
sl@0
   219
			mngmnt.UpdateDecision(record->iRecordId, ETrue, rs);
sl@0
   220
			User::WaitForRequest(rs);
sl@0
   221
			test(rs.Int() == KErrNone);
sl@0
   222
			TRAP(r, mngmnt.RemoveDecisionsL(*exactFilter));
sl@0
   223
			test(r == KErrNone);
sl@0
   224
sl@0
   225
			CleanupStack::PopAndDestroy(exactFilter);
sl@0
   226
			CleanupStack::PopAndDestroy(record);
sl@0
   227
    		}
sl@0
   228
		
sl@0
   229
		};
sl@0
   230
sl@0
   231
	
sl@0
   232
	TRAP(r, record = mngmnt.NextMatchL());
sl@0
   233
	test((r == KErrNone) && (record == 0));
sl@0
   234
sl@0
   235
	mngmnt.CancelAndCloseView();
sl@0
   236
sl@0
   237
	test.Printf(_L("Close management session and clientSubsession\n"));
sl@0
   238
	CleanupStack::PopAndDestroy(&mngmnt);
sl@0
   239
	CleanupStack::PopAndDestroy(&clientSubsession);
sl@0
   240
	CleanupStack::PopAndDestroy(&session);
sl@0
   241
	CleanupStack::PopAndDestroy(filter);
sl@0
   242
sl@0
   243
//	test.End();
sl@0
   244
	}
sl@0
   245
sl@0
   246
void MainL()
sl@0
   247
	{
sl@0
   248
	test.Title(_L("c:\\upstestoom.log"));
sl@0
   249
	test.Start(_L(" @SYMTestCaseID:SEC-UPS-OOM-0001 Testing UPS OOM "));
sl@0
   250
sl@0
   251
	// We need to increase the priority of the thread running the test code to make sure
sl@0
   252
	// that asynchronous ups management calls, for example CreateView() , won't finish
sl@0
   253
	// before following synchronous cancellation or termination calls, for example:
sl@0
   254
	// DeleteDatabaseL() or CancelAndCloseView().
sl@0
   255
	RThread thread;
sl@0
   256
	TThreadPriority currentPri = thread.Priority();
sl@0
   257
	currentPri = (TThreadPriority)((TInt)currentPri+10);
sl@0
   258
	thread.SetPriority(currentPri);
sl@0
   259
sl@0
   260
	RFs fs;
sl@0
   261
	User::LeaveIfError(fs.Connect());
sl@0
   262
	CleanupClosePushL(fs);
sl@0
   263
	
sl@0
   264
	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
sl@0
   265
	notifierConfig[0] = fs.GetSystemDriveChar();
sl@0
   266
sl@0
   267
	TBuf<35> database(_L("!:\\Private\\10283558\\database\\ups.db"));
sl@0
   268
	database[0] = fs.GetSystemDriveChar();
sl@0
   269
sl@0
   270
	TInt lineLength = User::CommandLineLength();
sl@0
   271
	switch(lineLength)
sl@0
   272
		{
sl@0
   273
		default:
sl@0
   274
			// fall through - extra command line arguments are ignored
sl@0
   275
		case 2:
sl@0
   276
			(void) fs.Delete(database);
sl@0
   277
			// Fall through to also delete notifier config file
sl@0
   278
		case 1:
sl@0
   279
			(void) fs.Delete(notifierConfig);
sl@0
   280
			break;
sl@0
   281
		case 0:
sl@0
   282
			{
sl@0
   283
			// No args so run in silent mode
sl@0
   284
			(void) fs.Delete(database);
sl@0
   285
			(void) fs.Delete(notifierConfig);
sl@0
   286
			RFile file;
sl@0
   287
			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
sl@0
   288
			User::LeaveIfError(file.Write(_L8("Never")));
sl@0
   289
			file.Close();
sl@0
   290
			break;
sl@0
   291
			}
sl@0
   292
		}
sl@0
   293
sl@0
   294
	// Connect to server
sl@0
   295
	RUpsSession session;
sl@0
   296
	User::LeaveIfError(session.Connect());
sl@0
   297
	CleanupClosePushL(session);
sl@0
   298
sl@0
   299
	TInt err = KErrNone;
sl@0
   300
	TInt err2 = KErrNone;
sl@0
   301
	TInt run = 0;
sl@0
   302
	TInt passingRuns = 0;
sl@0
   303
	static const TInt maxRun = 1000;
sl@0
   304
	static const TInt passThreshold = 5;
sl@0
   305
	for(run=1; run<=maxRun; ++run)
sl@0
   306
		{
sl@0
   307
		test.Printf(_L("\n\nOOM -- Run %d\n"), run);
sl@0
   308
		err = session.SetServerHeapFail(run);
sl@0
   309
		if(err == KErrNoMemory)
sl@0
   310
			{
sl@0
   311
			// Reinitialisation failed
sl@0
   312
			test.Printf(_L("\tReinitialisation failed\n"));
sl@0
   313
			session.ResetServerHeapFail();
sl@0
   314
			continue;
sl@0
   315
			}
sl@0
   316
		if(err != KErrNone)
sl@0
   317
			{
sl@0
   318
			// Failed to set heap fail, maybe the previous loop crashed the server??
sl@0
   319
			test.Printf(_L("Failed to set heap fail with error code %d\n"), err);
sl@0
   320
			test(EFalse);
sl@0
   321
			break;
sl@0
   322
			}
sl@0
   323
sl@0
   324
		// Run the test
sl@0
   325
		//test.Start(_L("TestFlurry"));
sl@0
   326
		test.Printf(_L("TestFlurry\n"));
sl@0
   327
		TRAP(err, TestFlurryL());
sl@0
   328
		test.Printf(_L("TestRUpsManagementL\n"));
sl@0
   329
		TRAP(err2, TestRUpsManagementL());
sl@0
   330
		if(err == KErrNone)
sl@0
   331
			{
sl@0
   332
			err = err2;
sl@0
   333
			}
sl@0
   334
		test.Printf(_L("done\n"));
sl@0
   335
		//test.End();
sl@0
   336
		// Clear the heap fail
sl@0
   337
		test.Printf(_L("Reseting heap failure\n"));
sl@0
   338
		err2 = session.ResetServerHeapFail();
sl@0
   339
		if((err == KErrServerTerminated) || (err2 == KErrServerTerminated))
sl@0
   340
			{
sl@0
   341
			test.Printf(_L("\tUPS server died\n"));
sl@0
   342
			test(EFalse);
sl@0
   343
			break;
sl@0
   344
			}
sl@0
   345
		if((err == KErrNone) && (err2 != KErrNone))
sl@0
   346
			{
sl@0
   347
			err = err2;
sl@0
   348
			}
sl@0
   349
		
sl@0
   350
		// Did it work?
sl@0
   351
		if(err == KErrNone)
sl@0
   352
			{
sl@0
   353
			++passingRuns;
sl@0
   354
			}
sl@0
   355
		else
sl@0
   356
			{
sl@0
   357
			passingRuns = 0;
sl@0
   358
			}
sl@0
   359
		
sl@0
   360
		if(passingRuns > passThreshold) break;
sl@0
   361
		} // End of OOM loop
sl@0
   362
sl@0
   363
	if(run > maxRun)
sl@0
   364
		{
sl@0
   365
		User::Leave(err);
sl@0
   366
		}
sl@0
   367
sl@0
   368
	// Synchronous call to shutdown the server
sl@0
   369
	session.ShutdownServer();
sl@0
   370
	// Close top level session (low level session was closed by
sl@0
   371
	// ShutdownServer, but we still need to do the RUpsSession
sl@0
   372
	// cleanup).
sl@0
   373
	CleanupStack::PopAndDestroy(&session);
sl@0
   374
sl@0
   375
	(void) fs.Delete(notifierConfig);
sl@0
   376
	CleanupStack::PopAndDestroy(&fs);
sl@0
   377
	
sl@0
   378
	test.End();
sl@0
   379
	test.Close();
sl@0
   380
}
sl@0
   381
sl@0
   382
void PanicIfError(TInt r)
sl@0
   383
	{
sl@0
   384
	if(r != KErrNone)
sl@0
   385
		{
sl@0
   386
		User::Panic(_L("upstestoom failed: "), r);
sl@0
   387
		}
sl@0
   388
	}
sl@0
   389
sl@0
   390
sl@0
   391
// -------- entrypoint --------
sl@0
   392
TInt E32Main()
sl@0
   393
/**
sl@0
   394
	Executable entrypoint establishes connection with UPS server
sl@0
   395
	and then invokes tests for each functional area.
sl@0
   396
	
sl@0
   397
	@return					Symbian OS error code where KErrNone indicates
sl@0
   398
							success and any other value indicates failure.
sl@0
   399
 */
sl@0
   400
	{
sl@0
   401
	// disable lazy DLL unloading so kernel heap balances at end
sl@0
   402
	RLoader l;
sl@0
   403
	PanicIfError(l.Connect());
sl@0
   404
	PanicIfError(l.CancelLazyDllUnload());
sl@0
   405
	l.Close();
sl@0
   406
	
sl@0
   407
	__UHEAP_MARK;
sl@0
   408
	//__KHEAP_MARK;
sl@0
   409
	
sl@0
   410
	// allocating a cleanup stack also installs it
sl@0
   411
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   412
	if (tc == 0)
sl@0
   413
		return KErrNoMemory;
sl@0
   414
sl@0
   415
	TRAPD(err, MainL());
sl@0
   416
	PanicIfError(err);
sl@0
   417
	delete tc;
sl@0
   418
	
sl@0
   419
	//__KHEAP_MARKEND;
sl@0
   420
	__UHEAP_MARKEND;
sl@0
   421
	
sl@0
   422
	
sl@0
   423
	return KErrNone;
sl@0
   424
	}
sl@0
   425
sl@0
   426
// End of file