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