os/security/authorisation/userpromptservice/server/test/upstest/upstestobsif.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 swi observer UPS 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("UPSTESTOBSIF"));
sl@0
    36
sl@0
    37
static RUpsManagement sMngmntSession;
sl@0
    38
sl@0
    39
void PopulateDatabaseL()
sl@0
    40
{
sl@0
    41
	test.Start(_L("Populate database"));
sl@0
    42
	RUpsSession session;
sl@0
    43
	User::LeaveIfError(session.Connect());
sl@0
    44
	CleanupClosePushL(session);
sl@0
    45
sl@0
    46
	RThread thd;
sl@0
    47
	RUpsSubsession clientSubsession;
sl@0
    48
	TInt r = clientSubsession.Initialise(session, thd);
sl@0
    49
	test(r == KErrNone);
sl@0
    50
	CleanupClosePushL(clientSubsession);
sl@0
    51
sl@0
    52
	RBuf destination;
sl@0
    53
	destination.CreateL(100);
sl@0
    54
	CleanupClosePushL(destination);
sl@0
    55
sl@0
    56
	for(TInt i=0 ; i<100; ++i)
sl@0
    57
		{
sl@0
    58
		TServiceId serviceId = {42};
sl@0
    59
		if( i & 1) serviceId.iUid = 43;
sl@0
    60
		destination.Zero();
sl@0
    61
		destination.AppendFormat(_L("destination %x"), i);
sl@0
    62
		
sl@0
    63
		TUpsDecision dec = EUpsDecNo;
sl@0
    64
		TRequestStatus rs;
sl@0
    65
		clientSubsession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), dec, rs);
sl@0
    66
		User::WaitForRequest(rs);
sl@0
    67
		test(rs == KErrNone);
sl@0
    68
		if(serviceId.iUid == 42)
sl@0
    69
			{
sl@0
    70
			test(dec == EUpsDecYes);
sl@0
    71
			}
sl@0
    72
		else
sl@0
    73
			{
sl@0
    74
			test(dec == EUpsDecNo);
sl@0
    75
			}
sl@0
    76
sl@0
    77
		}
sl@0
    78
sl@0
    79
	CleanupStack::PopAndDestroy(&destination);
sl@0
    80
	CleanupStack::PopAndDestroy(&clientSubsession);
sl@0
    81
	CleanupStack::PopAndDestroy(&session);
sl@0
    82
sl@0
    83
	test.End();
sl@0
    84
}
sl@0
    85
	
sl@0
    86
#if 1
sl@0
    87
class CTestSwiIf;
sl@0
    88
NONSHARABLE_CLASS(CRequest) : public CActive
sl@0
    89
	{
sl@0
    90
public:
sl@0
    91
	static CRequest *NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected);
sl@0
    92
	~CRequest();
sl@0
    93
private:
sl@0
    94
	CRequest(CTestSwiIf &aParent, TUpsDecision aExpected);
sl@0
    95
	void ConstructL(RUpsSession &aSession, TInt aId);
sl@0
    96
sl@0
    97
	virtual void RunL();
sl@0
    98
	virtual void DoCancel();
sl@0
    99
	virtual TInt RunError(TInt aError);
sl@0
   100
sl@0
   101
	CTestSwiIf &iParent;
sl@0
   102
sl@0
   103
	RUpsSubsession iSubSession;
sl@0
   104
sl@0
   105
	TUpsDecision iDec;
sl@0
   106
	TUpsDecision iExpected;
sl@0
   107
	};
sl@0
   108
sl@0
   109
sl@0
   110
NONSHARABLE_CLASS(CTestSwiIf) : public CActive
sl@0
   111
	{
sl@0
   112
public:
sl@0
   113
	static CTestSwiIf *NewL();
sl@0
   114
sl@0
   115
	~CTestSwiIf();
sl@0
   116
sl@0
   117
	void IncUsers();
sl@0
   118
	void DecUsers();
sl@0
   119
private:
sl@0
   120
	CTestSwiIf();
sl@0
   121
	void ConstructL();
sl@0
   122
sl@0
   123
	enum EState
sl@0
   124
		{
sl@0
   125
		EPrePolicyChange,
sl@0
   126
		EPostPolicyChange,
sl@0
   127
		ERevertPolicyChange,
sl@0
   128
		ETestingComplete
sl@0
   129
		};
sl@0
   130
	
sl@0
   131
	virtual void RunL();
sl@0
   132
	virtual void DoCancel();
sl@0
   133
	virtual TInt RunError(TInt aError);
sl@0
   134
sl@0
   135
	TInt iUsers;
sl@0
   136
sl@0
   137
	RFs iFs;
sl@0
   138
	EState iState;
sl@0
   139
	RUpsSession iUpsSession;
sl@0
   140
	RUpsManagement iManagementSession;
sl@0
   141
	};
sl@0
   142
sl@0
   143
CTestSwiIf *CTestSwiIf::NewL()
sl@0
   144
	{
sl@0
   145
	CTestSwiIf *self = new(ELeave) CTestSwiIf;
sl@0
   146
	CleanupStack::PushL(self);
sl@0
   147
	self->ConstructL();
sl@0
   148
	CleanupStack::Pop(self);
sl@0
   149
	return self;
sl@0
   150
	}
sl@0
   151
sl@0
   152
CTestSwiIf::CTestSwiIf()
sl@0
   153
	:	CActive(CActive::EPriorityStandard), iState(EPrePolicyChange)
sl@0
   154
	{
sl@0
   155
	CActiveScheduler::Add(this);
sl@0
   156
	}
sl@0
   157
sl@0
   158
_LIT(KCheckIfFailed, "z:\\private\\10283558\\policies\\ups_102836c3_0000002b_checkiffailed.rsc");
sl@0
   159
_LIT(KResourceFileDirC, "c:\\private\\10283558\\policies\\");
sl@0
   160
_LIT(KResourceFileOnC, "c:\\private\\10283558\\policies\\ups_102836c3_0000002b.rsc");
sl@0
   161
sl@0
   162
void CTestSwiIf::ConstructL()
sl@0
   163
	{
sl@0
   164
	User::LeaveIfError(iFs.Connect());
sl@0
   165
sl@0
   166
	// Make sure the C: policy is deleted.
sl@0
   167
	(void)iFs.Delete(KResourceFileOnC);
sl@0
   168
sl@0
   169
	// Make sure server is not holding cached values for the C: policy
sl@0
   170
	sMngmntSession.ShutdownServer();
sl@0
   171
	sMngmntSession.Close();
sl@0
   172
	User::LeaveIfError(sMngmntSession.Connect());
sl@0
   173
sl@0
   174
	User::LeaveIfError(iUpsSession.Connect());
sl@0
   175
	User::LeaveIfError(iManagementSession.Connect());
sl@0
   176
sl@0
   177
sl@0
   178
	TRequestStatus *rs = &iStatus;
sl@0
   179
	*rs = KRequestPending;
sl@0
   180
	User::RequestComplete(rs, KErrNone);
sl@0
   181
	SetActive();
sl@0
   182
	}
sl@0
   183
sl@0
   184
sl@0
   185
CTestSwiIf::~CTestSwiIf()
sl@0
   186
	{
sl@0
   187
	Cancel();
sl@0
   188
	iManagementSession.Close();
sl@0
   189
	iUpsSession.Close();
sl@0
   190
	iFs.Close();
sl@0
   191
	}
sl@0
   192
sl@0
   193
void CTestSwiIf::IncUsers()
sl@0
   194
	{
sl@0
   195
	++iUsers;
sl@0
   196
	}
sl@0
   197
sl@0
   198
void CTestSwiIf::DecUsers()
sl@0
   199
	{
sl@0
   200
	--iUsers;
sl@0
   201
	if((iUsers <= 0) && (iState == ETestingComplete))
sl@0
   202
		{
sl@0
   203
		CActiveScheduler::Stop();
sl@0
   204
		}
sl@0
   205
	}
sl@0
   206
sl@0
   207
sl@0
   208
void CTestSwiIf::RunL()
sl@0
   209
	{
sl@0
   210
	test(iStatus.Int() == KErrNone);
sl@0
   211
	switch(iState)
sl@0
   212
		{
sl@0
   213
		case EPrePolicyChange:
sl@0
   214
			{
sl@0
   215
			PopulateDatabaseL();
sl@0
   216
sl@0
   217
			(void)CRequest::NewL(iUpsSession, *this, 42, EUpsDecYes);
sl@0
   218
			(void)CRequest::NewL(iUpsSession, *this, 18, EUpsDecYes);
sl@0
   219
			(void)CRequest::NewL(iUpsSession, *this, 75, EUpsDecNo);
sl@0
   220
			(void)CRequest::NewL(iUpsSession, *this, 20, EUpsDecYes);
sl@0
   221
			(void)CRequest::NewL(iUpsSession, *this, 15, EUpsDecNo);
sl@0
   222
sl@0
   223
			(void)iFs.MkDirAll(KResourceFileDirC);
sl@0
   224
sl@0
   225
			CFileMan *fileman = CFileMan::NewL(iFs);
sl@0
   226
			CleanupStack::PushL(fileman);
sl@0
   227
			TInt r = fileman->Copy(KCheckIfFailed, KResourceFileOnC);
sl@0
   228
			User::LeaveIfError(r);
sl@0
   229
			CleanupStack::PopAndDestroy(fileman);
sl@0
   230
sl@0
   231
			TRequestStatus rs;
sl@0
   232
			iManagementSession.NotifyPolicyFilesChanged(rs);
sl@0
   233
			iManagementSession.CancelNotifyPolicyFilesChanged();
sl@0
   234
			User::WaitForRequest(rs);
sl@0
   235
			if(rs.Int() != KErrCancel) User::Leave(rs.Int());
sl@0
   236
sl@0
   237
			iState = EPostPolicyChange;
sl@0
   238
			iManagementSession.NotifyPolicyFilesChanged(iStatus);
sl@0
   239
			SetActive();
sl@0
   240
			break;
sl@0
   241
			}
sl@0
   242
		case EPostPolicyChange:
sl@0
   243
			// Notify complete, do some more queries
sl@0
   244
			(void)CRequest::NewL(iUpsSession, *this, 42, EUpsDecYes);
sl@0
   245
			(void)CRequest::NewL(iUpsSession, *this, 18, EUpsDecYes);
sl@0
   246
			(void)CRequest::NewL(iUpsSession, *this, 75, EUpsDecYes);
sl@0
   247
			(void)CRequest::NewL(iUpsSession, *this, 20, EUpsDecYes);
sl@0
   248
			(void)CRequest::NewL(iUpsSession, *this, 15, EUpsDecYes);
sl@0
   249
sl@0
   250
			// Revert change
sl@0
   251
			User::LeaveIfError(iFs.Delete(KResourceFileOnC));
sl@0
   252
sl@0
   253
			iState = ERevertPolicyChange;
sl@0
   254
			iManagementSession.NotifyPolicyFilesChanged(iStatus);
sl@0
   255
			SetActive();
sl@0
   256
			break;
sl@0
   257
sl@0
   258
		case ERevertPolicyChange:
sl@0
   259
			iState = ETestingComplete;
sl@0
   260
			if(iUsers <= 0)
sl@0
   261
				{
sl@0
   262
				CActiveScheduler::Stop();
sl@0
   263
				}
sl@0
   264
			break;
sl@0
   265
			
sl@0
   266
		case ETestingComplete:
sl@0
   267
			break;
sl@0
   268
		}
sl@0
   269
	}
sl@0
   270
sl@0
   271
void CTestSwiIf::DoCancel()
sl@0
   272
	{
sl@0
   273
	switch(iState)
sl@0
   274
		{
sl@0
   275
		case EPrePolicyChange:
sl@0
   276
			{
sl@0
   277
			TRequestStatus *rs = &iStatus;
sl@0
   278
			if(*rs == KRequestPending)
sl@0
   279
				{
sl@0
   280
				User::RequestComplete(rs, KErrCancel);
sl@0
   281
				}
sl@0
   282
			break;
sl@0
   283
			}
sl@0
   284
sl@0
   285
		case EPostPolicyChange:
sl@0
   286
			iManagementSession.CancelNotifyPolicyFilesChanged();
sl@0
   287
			break;
sl@0
   288
sl@0
   289
		case ERevertPolicyChange:
sl@0
   290
			iManagementSession.CancelNotifyPolicyFilesChanged();
sl@0
   291
			break;
sl@0
   292
sl@0
   293
		case ETestingComplete:
sl@0
   294
			break;
sl@0
   295
		}
sl@0
   296
sl@0
   297
	}
sl@0
   298
sl@0
   299
TInt CTestSwiIf::RunError(TInt aError)
sl@0
   300
	{
sl@0
   301
	User::Panic(_L("CTestSwiIf::RunError"), aError);
sl@0
   302
	/*lint -unreachable */
sl@0
   303
	return KErrNone;
sl@0
   304
	}
sl@0
   305
sl@0
   306
CRequest *CRequest::NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected)
sl@0
   307
	{
sl@0
   308
	CRequest *self = new(ELeave) CRequest(aParent, aExpected);
sl@0
   309
	CleanupStack::PushL(self);
sl@0
   310
	self->ConstructL(aSession, aId);
sl@0
   311
	CleanupStack::Pop(self);
sl@0
   312
	return self;
sl@0
   313
	}
sl@0
   314
sl@0
   315
CRequest::CRequest(CTestSwiIf &aParent, TUpsDecision aExpected)
sl@0
   316
	:	CActive(CActive::EPriorityStandard-1),
sl@0
   317
		iParent(aParent),
sl@0
   318
		iExpected(aExpected)
sl@0
   319
	{
sl@0
   320
	CActiveScheduler::Add(this);
sl@0
   321
	iParent.IncUsers();
sl@0
   322
	}
sl@0
   323
sl@0
   324
void CRequest::ConstructL(RUpsSession &aSession, TInt aId)
sl@0
   325
	{
sl@0
   326
	RThread thd;
sl@0
   327
	User::LeaveIfError(iSubSession.Initialise(aSession, thd));
sl@0
   328
	TServiceId serviceId = {42};
sl@0
   329
	if( aId & 1) serviceId.iUid = 43;
sl@0
   330
	RBuf destination;
sl@0
   331
	destination.CreateL(100);
sl@0
   332
	CleanupClosePushL(destination);
sl@0
   333
	destination.AppendFormat(_L("destination %x"), aId);
sl@0
   334
		
sl@0
   335
	iDec = EUpsDecNo;
sl@0
   336
	iSubSession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), iDec, iStatus);
sl@0
   337
	SetActive();
sl@0
   338
sl@0
   339
	CleanupStack::PopAndDestroy(&destination);
sl@0
   340
	}
sl@0
   341
sl@0
   342
CRequest::~CRequest()
sl@0
   343
	{
sl@0
   344
	iSubSession.Close();
sl@0
   345
	iParent.DecUsers();
sl@0
   346
	}
sl@0
   347
sl@0
   348
void CRequest::RunL()
sl@0
   349
	{
sl@0
   350
	test(iStatus.Int() == KErrNone);
sl@0
   351
	test(iDec == iExpected);
sl@0
   352
	delete this;
sl@0
   353
	}
sl@0
   354
sl@0
   355
void CRequest::DoCancel()
sl@0
   356
	{
sl@0
   357
	}
sl@0
   358
sl@0
   359
TInt CRequest::RunError(TInt aError)
sl@0
   360
	{
sl@0
   361
	User::Panic(_L("CRequest::RunError"), aError);
sl@0
   362
	/*lint -unreachable */
sl@0
   363
	return KErrNone;
sl@0
   364
	}
sl@0
   365
sl@0
   366
sl@0
   367
#endif
sl@0
   368
void TestSwiObserverL()
sl@0
   369
{
sl@0
   370
	test.Start(_L("Testing swi observer functions work from here..."));
sl@0
   371
sl@0
   372
	RUpsManagement session;
sl@0
   373
	User::LeaveIfError(session.Connect());
sl@0
   374
	CleanupClosePushL(session);
sl@0
   375
sl@0
   376
	session.NotifyPluginsMayHaveChangedL();
sl@0
   377
sl@0
   378
	TRequestStatus rs;
sl@0
   379
	session.NotifyPolicyFilesChanged(rs);
sl@0
   380
	User::WaitForRequest(rs);
sl@0
   381
sl@0
   382
	test(rs.Int() == KErrNone);
sl@0
   383
sl@0
   384
	session.CancelNotifyPolicyFilesChanged();
sl@0
   385
sl@0
   386
	TSecureId ourSid(0x10283559);
sl@0
   387
	session.DeleteDecisionsForExeL(ourSid);
sl@0
   388
sl@0
   389
sl@0
   390
	CleanupStack::PopAndDestroy(&session);
sl@0
   391
	
sl@0
   392
	test.End();
sl@0
   393
}
sl@0
   394
sl@0
   395
// -------- entrypoint --------
sl@0
   396
sl@0
   397
sl@0
   398
void MainL()
sl@0
   399
	{
sl@0
   400
	CActiveScheduler *scheduler = new(ELeave) CActiveScheduler;
sl@0
   401
	CActiveScheduler::Install(scheduler);
sl@0
   402
	CleanupStack::PushL(scheduler);
sl@0
   403
	
sl@0
   404
	test.Title(_L("c:\\upstestobsif.log"));
sl@0
   405
	test.Start(_L(" @SYMTestCaseID:SEC-UPS-OBSIF-0001 Testing RUpsSession SWI observer IF "));
sl@0
   406
sl@0
   407
	RFs fs;
sl@0
   408
	User::LeaveIfError(fs.Connect());
sl@0
   409
	CleanupClosePushL(fs);
sl@0
   410
sl@0
   411
	User::LeaveIfError(sMngmntSession.Connect());
sl@0
   412
	sMngmntSession.ShutdownServer();
sl@0
   413
	sMngmntSession.Close();
sl@0
   414
sl@0
   415
	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
sl@0
   416
	notifierConfig[0] = fs.GetSystemDriveChar();
sl@0
   417
sl@0
   418
	TInt lineLength = User::CommandLineLength();
sl@0
   419
	switch(lineLength)
sl@0
   420
		{
sl@0
   421
		default:
sl@0
   422
			// fall through - extra command line arguments are ignored
sl@0
   423
		case 2:
sl@0
   424
			// 2 char arg - Delete DB and run interactive
sl@0
   425
			(void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
sl@0
   426
			// Fall through to also delete notifier config file
sl@0
   427
		case 1:
sl@0
   428
			// 1 char arg - Run interactive, without deleting DB 
sl@0
   429
			(void) fs.Delete(notifierConfig);
sl@0
   430
			break;
sl@0
   431
		case 0:
sl@0
   432
			{
sl@0
   433
			// No args - delete DB and run in silent mode
sl@0
   434
			(void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
sl@0
   435
sl@0
   436
			(void) fs.Delete(notifierConfig);
sl@0
   437
			RFile file;
sl@0
   438
			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
sl@0
   439
			User::LeaveIfError(file.Write(_L8("Always")));
sl@0
   440
			file.Close();
sl@0
   441
			break;
sl@0
   442
			}
sl@0
   443
		}
sl@0
   444
sl@0
   445
	User::LeaveIfError(sMngmntSession.Connect());
sl@0
   446
	CleanupClosePushL(sMngmntSession);
sl@0
   447
sl@0
   448
sl@0
   449
	CTestSwiIf *t = CTestSwiIf::NewL();
sl@0
   450
	CActiveScheduler::Start();
sl@0
   451
	delete t;
sl@0
   452
sl@0
   453
	PopulateDatabaseL();
sl@0
   454
	TestSwiObserverL();
sl@0
   455
	
sl@0
   456
	sMngmntSession.ShutdownServer();
sl@0
   457
sl@0
   458
	// Close top level session (low level session was closed by
sl@0
   459
	// ShutdownServer, but we still need to do the RUpsManagement
sl@0
   460
	// cleanup).
sl@0
   461
	CleanupStack::PopAndDestroy(&sMngmntSession);
sl@0
   462
sl@0
   463
	(void) fs.Delete(notifierConfig);
sl@0
   464
	CleanupStack::PopAndDestroy(&fs);
sl@0
   465
	
sl@0
   466
	test.End();
sl@0
   467
	test.Close();
sl@0
   468
sl@0
   469
	CleanupStack::PopAndDestroy(scheduler);
sl@0
   470
}
sl@0
   471
sl@0
   472
void PanicIfError(TInt r)
sl@0
   473
	{
sl@0
   474
	if(r != KErrNone)
sl@0
   475
		{
sl@0
   476
		User::Panic(_L("upstest failed: "), r);
sl@0
   477
		}
sl@0
   478
	}
sl@0
   479
sl@0
   480
sl@0
   481
TInt E32Main()
sl@0
   482
/**
sl@0
   483
	Executable entrypoint establishes connection with UPS server
sl@0
   484
	and then invokes tests for each functional area.
sl@0
   485
	
sl@0
   486
	@return					Symbian OS error code where KErrNone indicates
sl@0
   487
							success and any other value indicates failure.
sl@0
   488
 */
sl@0
   489
	{
sl@0
   490
	// disable lazy DLL unloading so kernel heap balances at end
sl@0
   491
	RLoader l;
sl@0
   492
	PanicIfError(l.Connect());
sl@0
   493
	PanicIfError(l.CancelLazyDllUnload());
sl@0
   494
	l.Close();
sl@0
   495
	
sl@0
   496
	__UHEAP_MARK;
sl@0
   497
	//__KHEAP_MARK;
sl@0
   498
	
sl@0
   499
	// allocating a cleanup stack also installs it
sl@0
   500
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   501
	if (tc == 0)
sl@0
   502
		return KErrNoMemory;
sl@0
   503
sl@0
   504
sl@0
   505
	TRAPD(err, MainL());
sl@0
   506
	if(err != KErrNone)
sl@0
   507
		{
sl@0
   508
		User::Panic(_L("upstest failed: "), err);
sl@0
   509
		}
sl@0
   510
	delete tc;
sl@0
   511
	
sl@0
   512
	//__KHEAP_MARKEND;
sl@0
   513
	__UHEAP_MARKEND;
sl@0
   514
	
sl@0
   515
	
sl@0
   516
	return KErrNone;
sl@0
   517
	}
sl@0
   518