authorisation/userpromptservice/server/test/upstest/upstestobsifoom.cpp
changeset 8 35751d3474b7
child 15 da2ae96f639b
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 <scs/rtestwrapper.h>
       
    27 
       
    28 #include <ups/upsclient.h>
       
    29 #include "f32file.h"
       
    30 
       
    31 using namespace UserPromptService;
       
    32 
       
    33 /** Top-level test object renders stages and confirms conditions. */
       
    34 static RTestWrapper test(_L("UPSTESTOBSIFOOM"));
       
    35 
       
    36 static RUpsManagement sMngmntSession;
       
    37 
       
    38 void PopulateDatabaseL()
       
    39 	{
       
    40 	//test.Start(_L("Populate database"));
       
    41 	RUpsSession session;
       
    42 	User::LeaveIfError(session.Connect());
       
    43 	CleanupClosePushL(session);
       
    44 
       
    45 	RThread thd;
       
    46 	RUpsSubsession clientSubsession;
       
    47 	TInt r = clientSubsession.Initialise(session, thd);
       
    48 	test(r == KErrNone);
       
    49 	CleanupClosePushL(clientSubsession);
       
    50 
       
    51 	RBuf destination;
       
    52 	destination.CreateL(100);
       
    53 	CleanupClosePushL(destination);
       
    54 
       
    55 	for(TInt i=0 ; i<10; ++i)
       
    56 		{
       
    57 		TServiceId serviceId = {42};
       
    58 		if( i & 1) serviceId.iUid = 43;
       
    59 		destination.Zero();
       
    60 		destination.AppendFormat(_L("destination %x"), i);
       
    61 		
       
    62 		TUpsDecision dec = EUpsDecNo;
       
    63 		TRequestStatus rs;
       
    64 		clientSubsession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), dec, rs);
       
    65 		User::WaitForRequest(rs);
       
    66 		test((rs == KErrNone) || (rs == KErrNoMemory));
       
    67 		if(rs == KErrNone)
       
    68 			{
       
    69 			if(serviceId.iUid == 42)
       
    70 				{
       
    71 				test(dec == EUpsDecYes);
       
    72 				}
       
    73 			else
       
    74 				{
       
    75 				test(dec == EUpsDecNo);
       
    76 				}
       
    77 			}
       
    78 		}
       
    79 
       
    80 	CleanupStack::PopAndDestroy(&destination);
       
    81 	CleanupStack::PopAndDestroy(&clientSubsession);
       
    82 	CleanupStack::PopAndDestroy(&session);
       
    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 
       
   120 	TInt Result();
       
   121 private:
       
   122 	CTestSwiIf();
       
   123 	void ConstructL();
       
   124 
       
   125 	enum EState
       
   126 		{
       
   127 		EPrePolicyChange,
       
   128 		EPostPolicyChange,
       
   129 		ERevertPolicyChange,
       
   130 		ETestingComplete
       
   131 		};
       
   132 	
       
   133 	virtual void RunL();
       
   134 	virtual void DoCancel();
       
   135 	virtual TInt RunError(TInt aError);
       
   136 
       
   137 	TInt iUsers;
       
   138 
       
   139 	RFs iFs;
       
   140 	EState iState;
       
   141 	RUpsSession iUpsSession;
       
   142 	RUpsManagement iManagementSession;
       
   143 
       
   144 	TInt iResult;
       
   145 	};
       
   146 
       
   147 CTestSwiIf *CTestSwiIf::NewL()
       
   148 	{
       
   149 	CTestSwiIf *self = new(ELeave) CTestSwiIf;
       
   150 	CleanupStack::PushL(self);
       
   151 	self->ConstructL();
       
   152 	CleanupStack::Pop(self);
       
   153 	return self;
       
   154 	}
       
   155 
       
   156 CTestSwiIf::CTestSwiIf()
       
   157 	:	CActive(CActive::EPriorityStandard), iState(EPrePolicyChange)
       
   158 	{
       
   159 	CActiveScheduler::Add(this);
       
   160 	}
       
   161 
       
   162 _LIT(KCheckIfFailed, "z:\\private\\10283558\\policies\\ups_102836c3_0000002b_checkiffailed.rsc");
       
   163 _LIT(KResourceFileDirC, "c:\\private\\10283558\\policies\\");
       
   164 _LIT(KResourceFileOnC, "c:\\private\\10283558\\policies\\ups_102836c3_0000002b.rsc");
       
   165 
       
   166 void CTestSwiIf::ConstructL()
       
   167 	{
       
   168 	User::LeaveIfError(iFs.Connect());
       
   169 
       
   170 
       
   171 	User::LeaveIfError(iUpsSession.Connect());
       
   172 	User::LeaveIfError(iManagementSession.Connect());
       
   173 
       
   174 
       
   175 	TRequestStatus *rs = &iStatus;
       
   176 	*rs = KRequestPending;
       
   177 	User::RequestComplete(rs, KErrNone);
       
   178 	SetActive();
       
   179 	}
       
   180 
       
   181 
       
   182 CTestSwiIf::~CTestSwiIf()
       
   183 	{
       
   184 	Cancel();
       
   185 	iManagementSession.Close();
       
   186 	iUpsSession.Close();
       
   187 	iFs.Close();
       
   188 	}
       
   189 
       
   190 TInt CTestSwiIf::Result()
       
   191 	{
       
   192 	return iResult;
       
   193 	}
       
   194 
       
   195 void CTestSwiIf::IncUsers()
       
   196 	{
       
   197 	++iUsers;
       
   198 	}
       
   199 
       
   200 void CTestSwiIf::DecUsers()
       
   201 	{
       
   202 	--iUsers;
       
   203 	if((iUsers <= 0) && 
       
   204 		((iState == ETestingComplete) || (iResult != KErrNone)))
       
   205 		{
       
   206 		CActiveScheduler::Stop();
       
   207 		}
       
   208 	}
       
   209 void CTestSwiIf::RunL()
       
   210 	{
       
   211 	User::LeaveIfError(iStatus.Int());
       
   212 	switch(iState)
       
   213 		{
       
   214 		case EPrePolicyChange:
       
   215 			{
       
   216 			PopulateDatabaseL();
       
   217 
       
   218 			(void)CRequest::NewL(iUpsSession, *this, 8, EUpsDecYes);
       
   219 			(void)CRequest::NewL(iUpsSession, *this, 4, EUpsDecYes);
       
   220 			(void)CRequest::NewL(iUpsSession, *this, 5, EUpsDecNo);
       
   221 			(void)CRequest::NewL(iUpsSession, *this, 2, EUpsDecYes);
       
   222 			(void)CRequest::NewL(iUpsSession, *this, 7, EUpsDecNo);
       
   223 
       
   224 			(void)iFs.MkDirAll(KResourceFileDirC);
       
   225 
       
   226 			CFileMan *fileman = CFileMan::NewL(iFs);
       
   227 			CleanupStack::PushL(fileman);
       
   228 			TInt r = fileman->Copy(KCheckIfFailed, KResourceFileOnC);
       
   229 			User::LeaveIfError(r);
       
   230 			CleanupStack::PopAndDestroy(fileman);
       
   231 
       
   232 			TRequestStatus rs;
       
   233 			iManagementSession.NotifyPolicyFilesChanged(rs);
       
   234 			iManagementSession.CancelNotifyPolicyFilesChanged();
       
   235 			User::WaitForRequest(rs);
       
   236 			if(rs.Int() != KErrCancel) User::Leave(rs.Int());
       
   237 
       
   238 			iState = EPostPolicyChange;
       
   239 			iManagementSession.NotifyPolicyFilesChanged(iStatus);
       
   240 			SetActive();
       
   241 			break;
       
   242 			}
       
   243 		case EPostPolicyChange:
       
   244 			// Notify complete, do some more queries
       
   245 			(void)CRequest::NewL(iUpsSession, *this, 2, EUpsDecYes);
       
   246 			(void)CRequest::NewL(iUpsSession, *this, 8, EUpsDecYes);
       
   247 			(void)CRequest::NewL(iUpsSession, *this, 5, EUpsDecYes);
       
   248 			(void)CRequest::NewL(iUpsSession, *this, 4, EUpsDecYes);
       
   249 			(void)CRequest::NewL(iUpsSession, *this, 3, EUpsDecYes);
       
   250 
       
   251 			// Revert change
       
   252 			User::LeaveIfError(iFs.Delete(KResourceFileOnC));
       
   253 
       
   254 			iState = ERevertPolicyChange;
       
   255 			iManagementSession.NotifyPolicyFilesChanged(iStatus);
       
   256 			SetActive();
       
   257 			break;
       
   258 
       
   259 		case ERevertPolicyChange:
       
   260 			iState = ETestingComplete;
       
   261 			if(iUsers <= 0)
       
   262 				{
       
   263 				CActiveScheduler::Stop();
       
   264 				}
       
   265 			break;
       
   266 			
       
   267 		case ETestingComplete:
       
   268 			break;
       
   269 		}
       
   270 	}
       
   271 
       
   272 void CTestSwiIf::DoCancel()
       
   273 	{
       
   274 	switch(iState)
       
   275 		{
       
   276 		case EPrePolicyChange:
       
   277 			{
       
   278 			TRequestStatus *rs = &iStatus;
       
   279 			if(*rs == KRequestPending)
       
   280 				{
       
   281 				User::RequestComplete(rs, KErrCancel);
       
   282 				}
       
   283 			break;
       
   284 			}
       
   285 
       
   286 		case EPostPolicyChange:
       
   287 			iManagementSession.CancelNotifyPolicyFilesChanged();
       
   288 			break;
       
   289 
       
   290 		case ERevertPolicyChange:
       
   291 			iManagementSession.CancelNotifyPolicyFilesChanged();
       
   292 			break;
       
   293 
       
   294 		case ETestingComplete:
       
   295 			break;
       
   296 		default:
       
   297 			ASSERT(0); // Unknown state				
       
   298 		}
       
   299 
       
   300 	}
       
   301 
       
   302 TInt CTestSwiIf::RunError(TInt aError)
       
   303 	{
       
   304 	iResult = aError;
       
   305 	if(iUsers <= 0)
       
   306 		{
       
   307 		CActiveScheduler::Stop();
       
   308 		}
       
   309 	return KErrNone;
       
   310 	}
       
   311 
       
   312 CRequest *CRequest::NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected)
       
   313 	{
       
   314 	CRequest *self = new(ELeave) CRequest(aParent, aExpected);
       
   315 	CleanupStack::PushL(self);
       
   316 	self->ConstructL(aSession, aId);
       
   317 	CleanupStack::Pop(self);
       
   318 	return self;
       
   319 	}
       
   320 
       
   321 CRequest::CRequest(CTestSwiIf &aParent, TUpsDecision aExpected)
       
   322 	:	CActive(CActive::EPriorityStandard-1),
       
   323 		iParent(aParent),
       
   324 		iExpected(aExpected)
       
   325 	{
       
   326 	CActiveScheduler::Add(this);
       
   327 	iParent.IncUsers();
       
   328 	}
       
   329 
       
   330 void CRequest::ConstructL(RUpsSession &aSession, TInt aId)
       
   331 	{
       
   332 	RThread thd;
       
   333 	User::LeaveIfError(iSubSession.Initialise(aSession, thd));
       
   334 	TServiceId serviceId = {42};
       
   335 	if( aId & 1) serviceId.iUid = 43;
       
   336 	RBuf destination;
       
   337 	destination.CreateL(100);
       
   338 	CleanupClosePushL(destination);
       
   339 	destination.AppendFormat(_L("destination %x"), aId);
       
   340 		
       
   341 	iDec = EUpsDecNo;
       
   342 	iSubSession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), iDec, iStatus);
       
   343 	SetActive();
       
   344 
       
   345 	CleanupStack::PopAndDestroy(&destination);
       
   346 	}
       
   347 
       
   348 CRequest::~CRequest()
       
   349 	{
       
   350 	iSubSession.Close();
       
   351 	iParent.DecUsers();
       
   352 	}
       
   353 
       
   354 void CRequest::RunL()
       
   355 	{
       
   356 	test((iStatus.Int() == KErrNone) || (iStatus.Int() == KErrNoMemory));
       
   357 	if(iStatus.Int() == KErrNone)
       
   358 		{
       
   359 		// Some OOM situations appear to cause us to return NO.
       
   360 		test((iDec == iExpected) || (iDec == EUpsDecNo));
       
   361 		}
       
   362 	delete this;
       
   363 	}
       
   364 
       
   365 void CRequest::DoCancel()
       
   366 	{
       
   367 	}
       
   368 
       
   369 TInt CRequest::RunError(TInt aError)
       
   370 	{
       
   371 	User::Panic(_L("CRequest::RunError"), aError);
       
   372 	/*lint -unreachable*/
       
   373 	return KErrNone;
       
   374 	}
       
   375 
       
   376 
       
   377 #endif
       
   378 void TestSwiObserverL()
       
   379 	{
       
   380 	RUpsManagement session;
       
   381 	User::LeaveIfError(session.Connect());
       
   382 	CleanupClosePushL(session);
       
   383 
       
   384 	session.NotifyPluginsMayHaveChangedL();
       
   385 
       
   386 	TRequestStatus rs;
       
   387 	session.NotifyPolicyFilesChanged(rs);
       
   388 	User::WaitForRequest(rs);
       
   389 
       
   390 	test((rs.Int() == KErrNone) || (rs.Int() == KErrNoMemory));
       
   391 	if(rs.Int() == KErrNone)
       
   392 		{
       
   393 		session.CancelNotifyPolicyFilesChanged();
       
   394 		}
       
   395 
       
   396 	TSecureId ourSid(0x10283559);
       
   397 	session.DeleteDecisionsForExeL(ourSid);
       
   398 
       
   399 
       
   400 	CleanupStack::PopAndDestroy(&session);
       
   401 	}
       
   402 
       
   403 
       
   404 // -------- entrypoint --------
       
   405 void MainL()
       
   406 	{
       
   407  	// We need to increase the priority of the thread running the test code to make sure
       
   408  	// that asynchronous ups management calls, for example CreateView() , won't finish
       
   409  	// before following synchronous cancellation or termination calls, for example:
       
   410  	// DeleteDatabaseL() or CancelAndCloseView().
       
   411  	RThread thread;
       
   412  	TThreadPriority currentPri = thread.Priority();
       
   413  	currentPri = (TThreadPriority)((TInt)currentPri+10);
       
   414  	thread.SetPriority(currentPri);
       
   415 
       
   416 	CActiveScheduler *scheduler = new(ELeave) CActiveScheduler;
       
   417 	CActiveScheduler::Install(scheduler);
       
   418 	CleanupStack::PushL(scheduler);
       
   419 	
       
   420 	test.Title(_L("c:\\upstestobsifoom.log"));
       
   421 	test.Start(_L(" @SYMTestCaseID:SEC-UPS-OBSIF_OOM-0001 Testing RUpsSession SWI observer IF "));
       
   422 
       
   423 	RFs fs;
       
   424 	User::LeaveIfError(fs.Connect());
       
   425 	CleanupClosePushL(fs);
       
   426 
       
   427 	User::LeaveIfError(sMngmntSession.Connect());
       
   428 	sMngmntSession.ShutdownServer();
       
   429 	sMngmntSession.Close();
       
   430 
       
   431 	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
       
   432 	notifierConfig[0] = fs.GetSystemDriveChar();
       
   433 
       
   434 	TInt lineLength = User::CommandLineLength();
       
   435 	switch(lineLength)
       
   436 		{
       
   437 		default:
       
   438 		// fall through - extra command line arguments are ignored
       
   439 		case 2:
       
   440 			// 2 char arg - Delete DB and run interactive
       
   441 			(void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
       
   442 			// Fall through to also delete notifier config file
       
   443 		case 1:
       
   444 			// 1 char arg - Run interactive, without deleting DB 
       
   445 			(void) fs.Delete(notifierConfig);
       
   446 			break;
       
   447 		case 0:
       
   448 			{
       
   449 			// No args - delete DB and run in silent mode
       
   450 			(void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
       
   451 
       
   452 			(void) fs.Delete(notifierConfig);
       
   453 			RFile file;
       
   454 			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
       
   455 			User::LeaveIfError(file.Write(_L8("Always")));
       
   456 			file.Close();
       
   457 			break;
       
   458 			}
       
   459 		}
       
   460 
       
   461 	User::LeaveIfError(sMngmntSession.Connect());
       
   462 	CleanupClosePushL(sMngmntSession);
       
   463 
       
   464 
       
   465 	TInt err = KErrNone;
       
   466 	TInt errTmp = KErrNone;
       
   467 	TInt run = 0;
       
   468 	TInt passingRuns = 0;
       
   469 	static const TInt maxRun = 1000;
       
   470 	static const TInt passThreshold = 5;
       
   471 
       
   472 	for(run=1; run<=maxRun; ++run)
       
   473 		{
       
   474 		test.Printf(_L("\n\nOBSIF OOM -- Run %d\n"), run);
       
   475 
       
   476 		// Make sure the C: policy is deleted.
       
   477 		(void)fs.Delete(KResourceFileOnC);
       
   478 
       
   479 		// Make sure server is not holding cached values for the C: policy
       
   480 		// We could stop/restart the server, but that is very slow...
       
   481 		TRequestStatus rs;
       
   482 		sMngmntSession.NotifyPolicyFilesChanged(rs);
       
   483 		User::WaitForRequest(rs);
       
   484 
       
   485 		err = sMngmntSession.SetServerHeapFail(run);
       
   486 		if(err == KErrNoMemory)
       
   487 			{
       
   488 			// Reinitialisation failed
       
   489 			test.Printf(_L("\tReinitialisation failed\n"));
       
   490 			sMngmntSession.ResetServerHeapFail();
       
   491 			continue;
       
   492 			}
       
   493 		if(err != KErrNone)
       
   494 			{
       
   495 			// Failed to set heap fail, maybe the previous loop crashed the server??
       
   496 			test.Printf(_L("Failed to set heap fail with error code %d"), err);
       
   497 			test(EFalse);
       
   498 			break;
       
   499 			}
       
   500 
       
   501 		// Run the test
       
   502 		#if 1
       
   503 		CTestSwiIf *t = 0;
       
   504 		TRAP(err, t = CTestSwiIf::NewL());
       
   505 		if(err == KErrNone)
       
   506 			{
       
   507 			CActiveScheduler::Start();
       
   508 			err = t->Result();
       
   509 			delete t;
       
   510 			}
       
   511 		#else
       
   512 		err = KErrNone;
       
   513 		#endif
       
   514 
       
   515 #if 1
       
   516 		if(err == KErrNone)
       
   517 			{
       
   518 			TRAP(err, TestSwiObserverL());
       
   519 			}
       
   520 #endif
       
   521 
       
   522 		// Clear the heap fail
       
   523 		test.Printf(_L("Reseting heap failure\n"));
       
   524 		errTmp = sMngmntSession.ResetServerHeapFail();		
       
   525 		if((err == KErrServerTerminated) || (errTmp == KErrServerTerminated))
       
   526 			{
       
   527 			test.Printf(_L("\tUPS server died"));
       
   528 			test(EFalse);
       
   529 			break;
       
   530 			}
       
   531 		if((err == KErrNone) && (errTmp != KErrNone))
       
   532 			{
       
   533 			err = errTmp;
       
   534 			}
       
   535 		
       
   536 		// Did it work?
       
   537 		if(err == KErrNone)
       
   538 			{
       
   539 			++passingRuns;
       
   540 			}
       
   541 		else
       
   542 			{
       
   543 			passingRuns = 0;
       
   544 			}
       
   545 		
       
   546 		if(passingRuns > passThreshold) break;
       
   547 		} // End of OOM loop
       
   548 
       
   549 	if(run > maxRun)
       
   550 		{
       
   551 		User::Leave(err);
       
   552 		}
       
   553 
       
   554 	sMngmntSession.ShutdownServer();
       
   555 
       
   556 	// Close top level session (low level session was closed by
       
   557 	// ShutdownServer, but we still need to do the RUpsManagement
       
   558 	// cleanup).
       
   559 	CleanupStack::PopAndDestroy(&sMngmntSession);
       
   560 
       
   561 	(void) fs.Delete(notifierConfig);
       
   562 	CleanupStack::PopAndDestroy(&fs);
       
   563 	
       
   564 	test.End();
       
   565 	test.Close();
       
   566 
       
   567 	CleanupStack::PopAndDestroy(scheduler);
       
   568 }
       
   569 
       
   570 void PanicIfError(TInt r)
       
   571 	{
       
   572 	if(r != KErrNone)
       
   573 		{
       
   574 		User::Panic(_L("upstest failed: "), r);
       
   575 		}
       
   576 	}
       
   577 
       
   578 
       
   579 TInt E32Main()
       
   580 /**
       
   581 	Executable entrypoint establishes connection with UPS server
       
   582 	and then invokes tests for each functional area.
       
   583 	
       
   584 	@return					Symbian OS error code where KErrNone indicates
       
   585 							success and any other value indicates failure.
       
   586  */
       
   587 	{
       
   588 	// disable lazy DLL unloading so kernel heap balances at end
       
   589 	RLoader l;
       
   590 	PanicIfError(l.Connect());
       
   591 	PanicIfError(l.CancelLazyDllUnload());
       
   592 	l.Close();
       
   593 	
       
   594 	__UHEAP_MARK;
       
   595 	//__KHEAP_MARK;
       
   596 	
       
   597 	// allocating a cleanup stack also installs it
       
   598 	CTrapCleanup* tc = CTrapCleanup::New();
       
   599 	if (tc == 0)
       
   600 		return KErrNoMemory;
       
   601 
       
   602 
       
   603 	TRAPD(err, MainL());
       
   604 	if(err != KErrNone)
       
   605 		{
       
   606 		User::Panic(_L("upstest failed: "), err);
       
   607 		}
       
   608 	delete tc;
       
   609 	
       
   610 	//__KHEAP_MARKEND;
       
   611 	__UHEAP_MARKEND;
       
   612 	
       
   613 	
       
   614 	return KErrNone;
       
   615 	}
       
   616