os/security/authorisation/userpromptservice/server/test/upstest/upstestobsif.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/server/test/upstest/upstestobsif.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,518 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Test program exercises the swi observer UPS API.
    1.19 +* See individual test functions for more information.
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +
    1.28 +#include <e32ldr.h>
    1.29 +#include <e32ldr_private.h>
    1.30 +#include "rtestwrapper.h"
    1.31 +
    1.32 +#include <ups/upsclient.h>
    1.33 +#include "f32file.h"
    1.34 +
    1.35 +using namespace UserPromptService;
    1.36 +
    1.37 +/** Top-level test object renders stages and confirms conditions. */
    1.38 +static RTestWrapper test(_L("UPSTESTOBSIF"));
    1.39 +
    1.40 +static RUpsManagement sMngmntSession;
    1.41 +
    1.42 +void PopulateDatabaseL()
    1.43 +{
    1.44 +	test.Start(_L("Populate database"));
    1.45 +	RUpsSession session;
    1.46 +	User::LeaveIfError(session.Connect());
    1.47 +	CleanupClosePushL(session);
    1.48 +
    1.49 +	RThread thd;
    1.50 +	RUpsSubsession clientSubsession;
    1.51 +	TInt r = clientSubsession.Initialise(session, thd);
    1.52 +	test(r == KErrNone);
    1.53 +	CleanupClosePushL(clientSubsession);
    1.54 +
    1.55 +	RBuf destination;
    1.56 +	destination.CreateL(100);
    1.57 +	CleanupClosePushL(destination);
    1.58 +
    1.59 +	for(TInt i=0 ; i<100; ++i)
    1.60 +		{
    1.61 +		TServiceId serviceId = {42};
    1.62 +		if( i & 1) serviceId.iUid = 43;
    1.63 +		destination.Zero();
    1.64 +		destination.AppendFormat(_L("destination %x"), i);
    1.65 +		
    1.66 +		TUpsDecision dec = EUpsDecNo;
    1.67 +		TRequestStatus rs;
    1.68 +		clientSubsession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), dec, rs);
    1.69 +		User::WaitForRequest(rs);
    1.70 +		test(rs == KErrNone);
    1.71 +		if(serviceId.iUid == 42)
    1.72 +			{
    1.73 +			test(dec == EUpsDecYes);
    1.74 +			}
    1.75 +		else
    1.76 +			{
    1.77 +			test(dec == EUpsDecNo);
    1.78 +			}
    1.79 +
    1.80 +		}
    1.81 +
    1.82 +	CleanupStack::PopAndDestroy(&destination);
    1.83 +	CleanupStack::PopAndDestroy(&clientSubsession);
    1.84 +	CleanupStack::PopAndDestroy(&session);
    1.85 +
    1.86 +	test.End();
    1.87 +}
    1.88 +	
    1.89 +#if 1
    1.90 +class CTestSwiIf;
    1.91 +NONSHARABLE_CLASS(CRequest) : public CActive
    1.92 +	{
    1.93 +public:
    1.94 +	static CRequest *NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected);
    1.95 +	~CRequest();
    1.96 +private:
    1.97 +	CRequest(CTestSwiIf &aParent, TUpsDecision aExpected);
    1.98 +	void ConstructL(RUpsSession &aSession, TInt aId);
    1.99 +
   1.100 +	virtual void RunL();
   1.101 +	virtual void DoCancel();
   1.102 +	virtual TInt RunError(TInt aError);
   1.103 +
   1.104 +	CTestSwiIf &iParent;
   1.105 +
   1.106 +	RUpsSubsession iSubSession;
   1.107 +
   1.108 +	TUpsDecision iDec;
   1.109 +	TUpsDecision iExpected;
   1.110 +	};
   1.111 +
   1.112 +
   1.113 +NONSHARABLE_CLASS(CTestSwiIf) : public CActive
   1.114 +	{
   1.115 +public:
   1.116 +	static CTestSwiIf *NewL();
   1.117 +
   1.118 +	~CTestSwiIf();
   1.119 +
   1.120 +	void IncUsers();
   1.121 +	void DecUsers();
   1.122 +private:
   1.123 +	CTestSwiIf();
   1.124 +	void ConstructL();
   1.125 +
   1.126 +	enum EState
   1.127 +		{
   1.128 +		EPrePolicyChange,
   1.129 +		EPostPolicyChange,
   1.130 +		ERevertPolicyChange,
   1.131 +		ETestingComplete
   1.132 +		};
   1.133 +	
   1.134 +	virtual void RunL();
   1.135 +	virtual void DoCancel();
   1.136 +	virtual TInt RunError(TInt aError);
   1.137 +
   1.138 +	TInt iUsers;
   1.139 +
   1.140 +	RFs iFs;
   1.141 +	EState iState;
   1.142 +	RUpsSession iUpsSession;
   1.143 +	RUpsManagement iManagementSession;
   1.144 +	};
   1.145 +
   1.146 +CTestSwiIf *CTestSwiIf::NewL()
   1.147 +	{
   1.148 +	CTestSwiIf *self = new(ELeave) CTestSwiIf;
   1.149 +	CleanupStack::PushL(self);
   1.150 +	self->ConstructL();
   1.151 +	CleanupStack::Pop(self);
   1.152 +	return self;
   1.153 +	}
   1.154 +
   1.155 +CTestSwiIf::CTestSwiIf()
   1.156 +	:	CActive(CActive::EPriorityStandard), iState(EPrePolicyChange)
   1.157 +	{
   1.158 +	CActiveScheduler::Add(this);
   1.159 +	}
   1.160 +
   1.161 +_LIT(KCheckIfFailed, "z:\\private\\10283558\\policies\\ups_102836c3_0000002b_checkiffailed.rsc");
   1.162 +_LIT(KResourceFileDirC, "c:\\private\\10283558\\policies\\");
   1.163 +_LIT(KResourceFileOnC, "c:\\private\\10283558\\policies\\ups_102836c3_0000002b.rsc");
   1.164 +
   1.165 +void CTestSwiIf::ConstructL()
   1.166 +	{
   1.167 +	User::LeaveIfError(iFs.Connect());
   1.168 +
   1.169 +	// Make sure the C: policy is deleted.
   1.170 +	(void)iFs.Delete(KResourceFileOnC);
   1.171 +
   1.172 +	// Make sure server is not holding cached values for the C: policy
   1.173 +	sMngmntSession.ShutdownServer();
   1.174 +	sMngmntSession.Close();
   1.175 +	User::LeaveIfError(sMngmntSession.Connect());
   1.176 +
   1.177 +	User::LeaveIfError(iUpsSession.Connect());
   1.178 +	User::LeaveIfError(iManagementSession.Connect());
   1.179 +
   1.180 +
   1.181 +	TRequestStatus *rs = &iStatus;
   1.182 +	*rs = KRequestPending;
   1.183 +	User::RequestComplete(rs, KErrNone);
   1.184 +	SetActive();
   1.185 +	}
   1.186 +
   1.187 +
   1.188 +CTestSwiIf::~CTestSwiIf()
   1.189 +	{
   1.190 +	Cancel();
   1.191 +	iManagementSession.Close();
   1.192 +	iUpsSession.Close();
   1.193 +	iFs.Close();
   1.194 +	}
   1.195 +
   1.196 +void CTestSwiIf::IncUsers()
   1.197 +	{
   1.198 +	++iUsers;
   1.199 +	}
   1.200 +
   1.201 +void CTestSwiIf::DecUsers()
   1.202 +	{
   1.203 +	--iUsers;
   1.204 +	if((iUsers <= 0) && (iState == ETestingComplete))
   1.205 +		{
   1.206 +		CActiveScheduler::Stop();
   1.207 +		}
   1.208 +	}
   1.209 +
   1.210 +
   1.211 +void CTestSwiIf::RunL()
   1.212 +	{
   1.213 +	test(iStatus.Int() == KErrNone);
   1.214 +	switch(iState)
   1.215 +		{
   1.216 +		case EPrePolicyChange:
   1.217 +			{
   1.218 +			PopulateDatabaseL();
   1.219 +
   1.220 +			(void)CRequest::NewL(iUpsSession, *this, 42, EUpsDecYes);
   1.221 +			(void)CRequest::NewL(iUpsSession, *this, 18, EUpsDecYes);
   1.222 +			(void)CRequest::NewL(iUpsSession, *this, 75, EUpsDecNo);
   1.223 +			(void)CRequest::NewL(iUpsSession, *this, 20, EUpsDecYes);
   1.224 +			(void)CRequest::NewL(iUpsSession, *this, 15, EUpsDecNo);
   1.225 +
   1.226 +			(void)iFs.MkDirAll(KResourceFileDirC);
   1.227 +
   1.228 +			CFileMan *fileman = CFileMan::NewL(iFs);
   1.229 +			CleanupStack::PushL(fileman);
   1.230 +			TInt r = fileman->Copy(KCheckIfFailed, KResourceFileOnC);
   1.231 +			User::LeaveIfError(r);
   1.232 +			CleanupStack::PopAndDestroy(fileman);
   1.233 +
   1.234 +			TRequestStatus rs;
   1.235 +			iManagementSession.NotifyPolicyFilesChanged(rs);
   1.236 +			iManagementSession.CancelNotifyPolicyFilesChanged();
   1.237 +			User::WaitForRequest(rs);
   1.238 +			if(rs.Int() != KErrCancel) User::Leave(rs.Int());
   1.239 +
   1.240 +			iState = EPostPolicyChange;
   1.241 +			iManagementSession.NotifyPolicyFilesChanged(iStatus);
   1.242 +			SetActive();
   1.243 +			break;
   1.244 +			}
   1.245 +		case EPostPolicyChange:
   1.246 +			// Notify complete, do some more queries
   1.247 +			(void)CRequest::NewL(iUpsSession, *this, 42, EUpsDecYes);
   1.248 +			(void)CRequest::NewL(iUpsSession, *this, 18, EUpsDecYes);
   1.249 +			(void)CRequest::NewL(iUpsSession, *this, 75, EUpsDecYes);
   1.250 +			(void)CRequest::NewL(iUpsSession, *this, 20, EUpsDecYes);
   1.251 +			(void)CRequest::NewL(iUpsSession, *this, 15, EUpsDecYes);
   1.252 +
   1.253 +			// Revert change
   1.254 +			User::LeaveIfError(iFs.Delete(KResourceFileOnC));
   1.255 +
   1.256 +			iState = ERevertPolicyChange;
   1.257 +			iManagementSession.NotifyPolicyFilesChanged(iStatus);
   1.258 +			SetActive();
   1.259 +			break;
   1.260 +
   1.261 +		case ERevertPolicyChange:
   1.262 +			iState = ETestingComplete;
   1.263 +			if(iUsers <= 0)
   1.264 +				{
   1.265 +				CActiveScheduler::Stop();
   1.266 +				}
   1.267 +			break;
   1.268 +			
   1.269 +		case ETestingComplete:
   1.270 +			break;
   1.271 +		}
   1.272 +	}
   1.273 +
   1.274 +void CTestSwiIf::DoCancel()
   1.275 +	{
   1.276 +	switch(iState)
   1.277 +		{
   1.278 +		case EPrePolicyChange:
   1.279 +			{
   1.280 +			TRequestStatus *rs = &iStatus;
   1.281 +			if(*rs == KRequestPending)
   1.282 +				{
   1.283 +				User::RequestComplete(rs, KErrCancel);
   1.284 +				}
   1.285 +			break;
   1.286 +			}
   1.287 +
   1.288 +		case EPostPolicyChange:
   1.289 +			iManagementSession.CancelNotifyPolicyFilesChanged();
   1.290 +			break;
   1.291 +
   1.292 +		case ERevertPolicyChange:
   1.293 +			iManagementSession.CancelNotifyPolicyFilesChanged();
   1.294 +			break;
   1.295 +
   1.296 +		case ETestingComplete:
   1.297 +			break;
   1.298 +		}
   1.299 +
   1.300 +	}
   1.301 +
   1.302 +TInt CTestSwiIf::RunError(TInt aError)
   1.303 +	{
   1.304 +	User::Panic(_L("CTestSwiIf::RunError"), aError);
   1.305 +	/*lint -unreachable */
   1.306 +	return KErrNone;
   1.307 +	}
   1.308 +
   1.309 +CRequest *CRequest::NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected)
   1.310 +	{
   1.311 +	CRequest *self = new(ELeave) CRequest(aParent, aExpected);
   1.312 +	CleanupStack::PushL(self);
   1.313 +	self->ConstructL(aSession, aId);
   1.314 +	CleanupStack::Pop(self);
   1.315 +	return self;
   1.316 +	}
   1.317 +
   1.318 +CRequest::CRequest(CTestSwiIf &aParent, TUpsDecision aExpected)
   1.319 +	:	CActive(CActive::EPriorityStandard-1),
   1.320 +		iParent(aParent),
   1.321 +		iExpected(aExpected)
   1.322 +	{
   1.323 +	CActiveScheduler::Add(this);
   1.324 +	iParent.IncUsers();
   1.325 +	}
   1.326 +
   1.327 +void CRequest::ConstructL(RUpsSession &aSession, TInt aId)
   1.328 +	{
   1.329 +	RThread thd;
   1.330 +	User::LeaveIfError(iSubSession.Initialise(aSession, thd));
   1.331 +	TServiceId serviceId = {42};
   1.332 +	if( aId & 1) serviceId.iUid = 43;
   1.333 +	RBuf destination;
   1.334 +	destination.CreateL(100);
   1.335 +	CleanupClosePushL(destination);
   1.336 +	destination.AppendFormat(_L("destination %x"), aId);
   1.337 +		
   1.338 +	iDec = EUpsDecNo;
   1.339 +	iSubSession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), iDec, iStatus);
   1.340 +	SetActive();
   1.341 +
   1.342 +	CleanupStack::PopAndDestroy(&destination);
   1.343 +	}
   1.344 +
   1.345 +CRequest::~CRequest()
   1.346 +	{
   1.347 +	iSubSession.Close();
   1.348 +	iParent.DecUsers();
   1.349 +	}
   1.350 +
   1.351 +void CRequest::RunL()
   1.352 +	{
   1.353 +	test(iStatus.Int() == KErrNone);
   1.354 +	test(iDec == iExpected);
   1.355 +	delete this;
   1.356 +	}
   1.357 +
   1.358 +void CRequest::DoCancel()
   1.359 +	{
   1.360 +	}
   1.361 +
   1.362 +TInt CRequest::RunError(TInt aError)
   1.363 +	{
   1.364 +	User::Panic(_L("CRequest::RunError"), aError);
   1.365 +	/*lint -unreachable */
   1.366 +	return KErrNone;
   1.367 +	}
   1.368 +
   1.369 +
   1.370 +#endif
   1.371 +void TestSwiObserverL()
   1.372 +{
   1.373 +	test.Start(_L("Testing swi observer functions work from here..."));
   1.374 +
   1.375 +	RUpsManagement session;
   1.376 +	User::LeaveIfError(session.Connect());
   1.377 +	CleanupClosePushL(session);
   1.378 +
   1.379 +	session.NotifyPluginsMayHaveChangedL();
   1.380 +
   1.381 +	TRequestStatus rs;
   1.382 +	session.NotifyPolicyFilesChanged(rs);
   1.383 +	User::WaitForRequest(rs);
   1.384 +
   1.385 +	test(rs.Int() == KErrNone);
   1.386 +
   1.387 +	session.CancelNotifyPolicyFilesChanged();
   1.388 +
   1.389 +	TSecureId ourSid(0x10283559);
   1.390 +	session.DeleteDecisionsForExeL(ourSid);
   1.391 +
   1.392 +
   1.393 +	CleanupStack::PopAndDestroy(&session);
   1.394 +	
   1.395 +	test.End();
   1.396 +}
   1.397 +
   1.398 +// -------- entrypoint --------
   1.399 +
   1.400 +
   1.401 +void MainL()
   1.402 +	{
   1.403 +	CActiveScheduler *scheduler = new(ELeave) CActiveScheduler;
   1.404 +	CActiveScheduler::Install(scheduler);
   1.405 +	CleanupStack::PushL(scheduler);
   1.406 +	
   1.407 +	test.Title(_L("c:\\upstestobsif.log"));
   1.408 +	test.Start(_L(" @SYMTestCaseID:SEC-UPS-OBSIF-0001 Testing RUpsSession SWI observer IF "));
   1.409 +
   1.410 +	RFs fs;
   1.411 +	User::LeaveIfError(fs.Connect());
   1.412 +	CleanupClosePushL(fs);
   1.413 +
   1.414 +	User::LeaveIfError(sMngmntSession.Connect());
   1.415 +	sMngmntSession.ShutdownServer();
   1.416 +	sMngmntSession.Close();
   1.417 +
   1.418 +	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
   1.419 +	notifierConfig[0] = fs.GetSystemDriveChar();
   1.420 +
   1.421 +	TInt lineLength = User::CommandLineLength();
   1.422 +	switch(lineLength)
   1.423 +		{
   1.424 +		default:
   1.425 +			// fall through - extra command line arguments are ignored
   1.426 +		case 2:
   1.427 +			// 2 char arg - Delete DB and run interactive
   1.428 +			(void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
   1.429 +			// Fall through to also delete notifier config file
   1.430 +		case 1:
   1.431 +			// 1 char arg - Run interactive, without deleting DB 
   1.432 +			(void) fs.Delete(notifierConfig);
   1.433 +			break;
   1.434 +		case 0:
   1.435 +			{
   1.436 +			// No args - delete DB and run in silent mode
   1.437 +			(void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
   1.438 +
   1.439 +			(void) fs.Delete(notifierConfig);
   1.440 +			RFile file;
   1.441 +			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
   1.442 +			User::LeaveIfError(file.Write(_L8("Always")));
   1.443 +			file.Close();
   1.444 +			break;
   1.445 +			}
   1.446 +		}
   1.447 +
   1.448 +	User::LeaveIfError(sMngmntSession.Connect());
   1.449 +	CleanupClosePushL(sMngmntSession);
   1.450 +
   1.451 +
   1.452 +	CTestSwiIf *t = CTestSwiIf::NewL();
   1.453 +	CActiveScheduler::Start();
   1.454 +	delete t;
   1.455 +
   1.456 +	PopulateDatabaseL();
   1.457 +	TestSwiObserverL();
   1.458 +	
   1.459 +	sMngmntSession.ShutdownServer();
   1.460 +
   1.461 +	// Close top level session (low level session was closed by
   1.462 +	// ShutdownServer, but we still need to do the RUpsManagement
   1.463 +	// cleanup).
   1.464 +	CleanupStack::PopAndDestroy(&sMngmntSession);
   1.465 +
   1.466 +	(void) fs.Delete(notifierConfig);
   1.467 +	CleanupStack::PopAndDestroy(&fs);
   1.468 +	
   1.469 +	test.End();
   1.470 +	test.Close();
   1.471 +
   1.472 +	CleanupStack::PopAndDestroy(scheduler);
   1.473 +}
   1.474 +
   1.475 +void PanicIfError(TInt r)
   1.476 +	{
   1.477 +	if(r != KErrNone)
   1.478 +		{
   1.479 +		User::Panic(_L("upstest failed: "), r);
   1.480 +		}
   1.481 +	}
   1.482 +
   1.483 +
   1.484 +TInt E32Main()
   1.485 +/**
   1.486 +	Executable entrypoint establishes connection with UPS server
   1.487 +	and then invokes tests for each functional area.
   1.488 +	
   1.489 +	@return					Symbian OS error code where KErrNone indicates
   1.490 +							success and any other value indicates failure.
   1.491 + */
   1.492 +	{
   1.493 +	// disable lazy DLL unloading so kernel heap balances at end
   1.494 +	RLoader l;
   1.495 +	PanicIfError(l.Connect());
   1.496 +	PanicIfError(l.CancelLazyDllUnload());
   1.497 +	l.Close();
   1.498 +	
   1.499 +	__UHEAP_MARK;
   1.500 +	//__KHEAP_MARK;
   1.501 +	
   1.502 +	// allocating a cleanup stack also installs it
   1.503 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.504 +	if (tc == 0)
   1.505 +		return KErrNoMemory;
   1.506 +
   1.507 +
   1.508 +	TRAPD(err, MainL());
   1.509 +	if(err != KErrNone)
   1.510 +		{
   1.511 +		User::Panic(_L("upstest failed: "), err);
   1.512 +		}
   1.513 +	delete tc;
   1.514 +	
   1.515 +	//__KHEAP_MARKEND;
   1.516 +	__UHEAP_MARKEND;
   1.517 +	
   1.518 +	
   1.519 +	return KErrNone;
   1.520 +	}
   1.521 +