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