os/security/authorisation/userpromptservice/server/test/upstest/upstest.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 skeleton UPS server 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 <ups/upserr.h>
    31 #include "f32file.h"
    32 
    33 using namespace UserPromptService;
    34 
    35 /** Top-level test object renders stages and confirms conditions. */
    36 static RTestWrapper test(_L("UPSTEST"));
    37 
    38 /**
    39 	This session handle is defined at the file level so each individual test
    40 	does not have to connect to the server.
    41  */
    42 static RUpsSession sTestSession;
    43 /**
    44 	This subsession handle is defined at the file level so each individual test
    45 	does not have to connect to the server and create a subssesion.
    46  */
    47 static RUpsSubsession sTestSubsession;
    48 
    49 // -------- open / close session --------
    50 
    51 static void TestOpenCloseSession()
    52 /**
    53 	Open and close a connection to the UPS server.
    54  */
    55 	{
    56 	test.Start(_L("TestOpenCloseSession"));
    57 	
    58 	RUpsSession s;
    59 	TInt r = s.Connect();
    60 	test(r == KErrNone);
    61 
    62 	r = s.ShareAuto();
    63 	test(r == KErrNone);
    64 	
    65 	s.Close();
    66 	
    67 	test.End();
    68 	}
    69 
    70 // -------- open / close subsession --------
    71 
    72 static void TestOpenCloseSubsession()
    73 /**
    74 	Open and close a subsession on the UPS server.
    75  */
    76 	{
    77 	test.Start(_L("TestOpenCloseSubsession"));
    78 	
    79 	RUpsSubsession ss;
    80 	RThread thd;
    81 	TInt r = ss.Initialise(sTestSession, thd);
    82 	test(r == KErrNone);
    83 	ss.Close();
    84 	
    85 	test.End();
    86 	}
    87 
    88 // -------- Authorise succeed, cancel --------
    89 _LIT8(KOpaqueData, "<ce>fred</ce>");
    90 static void TestAuthoriseL()
    91 /**
    92 	Launch an asynchronous Authorise request on a UPS subsession.
    93 	Let it complete normally, and also cancel it.
    94  */
    95 	{
    96 	test.Start(_L("TestAuthorise"));
    97 	
    98 	TServiceId serviceId = {43};
    99 	TRequestStatus rs;
   100 
   101 	
   102 	test.Next(_L("complete normally - no opaque data\n"));
   103 	TUpsDecision dec = EUpsDecYes;
   104 	sTestSubsession.Authorise(EFalse, serviceId, KNullDesC, dec, rs);
   105 	test(dec == EUpsDecYes);	// not changed yet
   106 	User::WaitForRequest(rs);
   107 	test(rs == KErrNone);
   108 	test(dec == EUpsDecNo);
   109 	
   110 	test.Next(_L("cancel - empty opaque data\n"));
   111 	dec = EUpsDecYes;	// ensure changed
   112 	sTestSubsession.Authorise(EFalse, serviceId, KNullDesC, KNullDesC8, dec, rs);
   113 	test(dec == EUpsDecYes);	// not changed yet
   114 	test.Printf(_L("About to cancel - current status is %d"), rs.Int());
   115 	sTestSubsession.CancelPrompt();
   116 	User::WaitForRequest(rs);
   117 	test(rs == KErrCancel);
   118 	test(dec == EUpsDecYes);	// not changed
   119 
   120 	test.Next(_L("Opaque data\n"));
   121 	dec = EUpsDecYes;	// ensure changed
   122 	sTestSubsession.Authorise(EFalse, serviceId, _L("opaque data test"), KOpaqueData, dec, rs);
   123 	test(dec == EUpsDecYes);	// not changed yet
   124 	User::WaitForRequest(rs);
   125 	test(rs == KErrNone);
   126 	test(dec == EUpsDecNo);
   127 
   128 	
   129 	test.Next(_L("cancel when no outstanding - harmless\n"));
   130 	sTestSubsession.CancelPrompt();
   131 
   132 	test.Next(_L("cancel/close when sub session never created - harmless\n"));
   133 	RUpsSubsession uninitialisedCancel;
   134 	uninitialisedCancel.CancelPrompt();
   135 	uninitialisedCancel.Close();
   136 	
   137 	test.End();
   138 	}
   139 
   140 static void TestFlurryL()
   141 /**
   142 	Launch multiple requests
   143  */
   144 	{
   145 	test.Start(_L("TestFlurry"));
   146 
   147 	RThread thd;
   148 	RUpsSubsession testSubsession1;
   149 	testSubsession1.Initialise(sTestSession, thd);
   150 	RUpsSubsession testSubsession2;
   151 	testSubsession2.Initialise(sTestSession, thd);
   152 	RUpsSubsession testSubsession3;
   153 	testSubsession3.Initialise(sTestSession, thd);
   154 	RUpsSubsession testSubsession4;
   155 	testSubsession4.Initialise(sTestSession, thd);
   156 	RUpsSubsession testSubsession5;
   157 	testSubsession5.Initialise(sTestSession, thd);
   158 	RUpsSubsession testSubsession6;
   159 	testSubsession6.Initialise(sTestSession, thd);
   160 	
   161 	TServiceId serviceId = {43};
   162 	TRequestStatus rs1;
   163 	TRequestStatus rs2;
   164 	TRequestStatus rs3;
   165 	TRequestStatus rs4;
   166 	TRequestStatus rs5;
   167 	TRequestStatus rs6;
   168 	
   169 	// complete normally - no opaque data
   170 	TUpsDecision dec1 = EUpsDecYes;
   171 	TUpsDecision dec2 = EUpsDecYes;
   172 	TUpsDecision dec3 = EUpsDecYes;
   173 	TUpsDecision dec4 = EUpsDecYes;
   174 	TUpsDecision dec5 = EUpsDecYes;
   175 	TUpsDecision dec6 = EUpsDecYes;
   176 	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
   177 	testSubsession2.Authorise(EFalse, serviceId, _L("reqX"), dec2, rs2);
   178 	testSubsession3.Authorise(EFalse, serviceId, _L("req2"), dec3, rs3);
   179 	testSubsession4.Authorise(EFalse, serviceId, _L("reqX"), dec4, rs4);
   180 	testSubsession5.Authorise(EFalse, serviceId, _L("req3"), dec5, rs5);
   181 	testSubsession6.Authorise(EFalse, serviceId, _L("req4"), dec6, rs6);
   182 #if 0
   183 	// Will change immediately in non-interactive testing
   184 	test(dec1 == EUpsDecYes);	// not changed yet
   185 	test(dec2 == EUpsDecYes);	// not changed yet
   186 	test(dec3 == EUpsDecYes);	// not changed yet
   187 	test(dec4 == EUpsDecYes);	// not changed yet
   188 	test(dec5 == EUpsDecYes);	// not changed yet
   189 	test(dec6 == EUpsDecYes);	// not changed yet
   190 #endif
   191 	User::After(1000);
   192 
   193 	User::WaitForRequest(rs1);
   194 
   195 	User::WaitForRequest(rs2);
   196 	User::WaitForRequest(rs4);
   197 
   198 	User::WaitForRequest(rs3);
   199 	User::WaitForRequest(rs5);
   200 	User::WaitForRequest(rs6);
   201 	test(rs1 == KErrNone);
   202 	test(rs2 == KErrNone);
   203 	test(rs3 == KErrNone);
   204 	test(rs4 == KErrNone);
   205 	test(rs5 == KErrNone);
   206 	test(rs6 == KErrNone);
   207 	test(dec1 == EUpsDecNo);
   208 	test(dec2 == EUpsDecNo);
   209 	test(dec3 == EUpsDecNo);
   210 	test(dec4 == EUpsDecNo);
   211 	test(dec5 == EUpsDecNo);
   212 	test(dec6 == EUpsDecNo);
   213 	
   214 	test.End();
   215 	}
   216 
   217 // -------- RUpsSubsession --------
   218 _LIT(KSayYes,"SayYes");
   219 static void TestRUpsSubsession()
   220 /**
   221 	Attempt query with server checks passed and without
   222  */
   223 	{
   224 	//
   225 	// Tests for RUpsSubsession
   226 	//
   227 	RThread thd;
   228 
   229 	test.Start(_L("Testing RUpsSubsession"));
   230 	RUpsSubsession clientSubsession;
   231 	TInt r = clientSubsession.Initialise(sTestSession, thd);
   232 	test(r == KErrNone);
   233 
   234 	test.Next(_L("Query with server checks passed"));
   235 	TServiceId serviceId = {42};
   236 	TUpsDecision dec = EUpsDecNo;
   237 	TRequestStatus rs;
   238 	// Query saying our checks were ok, expect to get decision set to EUpsDecYes
   239 	clientSubsession.Authorise(ETrue, serviceId, _L("Destination"), _L8("Opaque data"), dec, rs);
   240 	User::WaitForRequest(rs);
   241 	test(rs == KErrNone);
   242 	test(dec == EUpsDecYes);
   243 
   244 
   245 
   246 	test.Next(_L("Try closing client subsession before it is really created"));
   247 	clientSubsession.Close();
   248 
   249 	test.Next(_L("Re-\"create\" client subsession"));
   250 	r = clientSubsession.Initialise(sTestSession, thd);
   251 	test(r == KErrNone);
   252 
   253 	test.Next(_L("Query with server checks failed, ie query UPS, expect fail"));
   254 	// Query saying our checks failed, should talk to UPS and change decision to EUpsDecNo
   255 	dec = EUpsDecYes;
   256 	clientSubsession.Authorise(EFalse, serviceId, KNullDesC, dec, rs);
   257 	User::WaitForRequest(rs);
   258 	test(rs == KErrNone);
   259 	test(dec == EUpsDecNo);
   260 
   261 
   262 	test.Next(_L("Query with server checks failed, ie query UPS, special destination, expect yes"));
   263 	// Query saying our checks failed, should talk to UPS and change decision to EUpsDecNo
   264 	dec = EUpsDecYes;
   265 	clientSubsession.Authorise(EFalse, serviceId, KSayYes(), dec, rs);
   266 	User::WaitForRequest(rs);
   267 	test(rs == KErrNone);
   268 	test(dec == EUpsDecYes);
   269 
   270 
   271 	clientSubsession.Close();
   272 	test.End();
   273 	}
   274 
   275 TInt ThreadFunction(TAny *)
   276 	{
   277 	return KErrNone;
   278 	}
   279 
   280 static void TestRUpsSubsessionDeathL()
   281 	{
   282 	RThread thd;
   283 	TRequestStatus thdStatus;
   284 	User::LeaveIfError(thd.Create(_L("MyThread"), ThreadFunction, 4096, 4096, 4096, 0, EOwnerThread));
   285 //	thd.SetHandle(666);
   286 	thd.Rendezvous(thdStatus);
   287 	thd.Kill(KErrAbort);
   288 	User::WaitForRequest(thdStatus);
   289 
   290 	test.Start(_L("Testing RUpsSubsession"));
   291 	RUpsSubsession clientSubsession;
   292 	TInt r = clientSubsession.Initialise(sTestSession, thd);
   293 	test(r == KErrNone);
   294 
   295 	test.Next(_L("Query with dead thread id"));
   296 	TServiceId serviceId = {43};
   297 	TUpsDecision dec = EUpsDecYes;
   298 	TRequestStatus rs;
   299 	thd.Close();
   300 	clientSubsession.Authorise(EFalse, serviceId, _L("req1"), dec, rs);
   301 	User::WaitForRequest(rs);
   302 	test(rs == KErrNone);
   303 	test(dec == EUpsDecNo);
   304 
   305 	
   306 	clientSubsession.Close();
   307 	test.End();
   308 	}
   309 
   310 static void TestRUpsManagementL()
   311 /**
   312 	Attempt to delete database
   313  */
   314 	{
   315 	test.Start(_L("Testing RUpsManagement"));
   316 	RThread thd;
   317 	TRequestStatus rs;
   318 
   319 	// Create filter
   320 	TServiceId serviceId = {43};
   321 	CDecisionFilter *filter = CDecisionFilter::NewLC();
   322 	filter->SetClientSid(thd.SecureId(), EEqual);
   323 	filter->SetServerSid(thd.SecureId(), EEqual);
   324 	filter->SetServiceId(serviceId, EEqual);
   325 
   326 	RUpsSubsession clientSubsession;
   327 	TInt r = clientSubsession.Initialise(sTestSession, thd);
   328 	test(r == KErrNone);
   329 
   330 	test.Next(_L("Open management session"));
   331 	RUpsManagement mngmnt;
   332 	r = mngmnt.Connect();
   333 	test(r == KErrNone);
   334 	User::LeaveIfError(r);
   335 
   336 	test.Next(_L("View create - then delete DB"));
   337 	mngmnt.CreateView(*filter, rs);
   338 
   339 	test.Next(_L("Delete database"));
   340 	TRAP(r, mngmnt.DeleteDatabaseL());
   341 	test(r == KErrNone);
   342 
   343 	test.Next(_L("Now see what view create completed with...."));
   344 	User::WaitForRequest(rs);
   345 	test(rs.Int() == KErrAbort);
   346 
   347 	test.Next(_L("Add entry to new database"));
   348 	TUpsDecision dec = EUpsDecYes;
   349 	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 1"), _L8("Opaque data"), dec, rs);
   350 	User::WaitForRequest(rs);
   351 	test(rs == KErrNone);
   352 	test(dec == EUpsDecNo);
   353 
   354 	dec = EUpsDecYes;
   355 	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 2"), _L8("Opaque data"), dec, rs);
   356 	User::WaitForRequest(rs);
   357 	test(rs == KErrNone);
   358 	test(dec == EUpsDecNo);
   359 
   360 
   361 	test.Next(_L("View create - immediate cancel"));
   362 	mngmnt.CreateView(*filter, rs);
   363 	mngmnt.CancelAndCloseView();
   364 	User::WaitForRequest(rs);
   365 	test(rs.Int() == KErrCancel);
   366 
   367 	mngmnt.UpdateDecision(TUint32(-23), ETrue, rs);
   368 	mngmnt.CancelUpdateDecision();
   369 	User::WaitForRequest(rs);
   370 	test(rs.Int() == KErrCancel);
   371 
   372 	mngmnt.UpdateDecision(TUint32(-23), ETrue, rs);
   373 	User::WaitForRequest(rs);
   374 	test(rs.Int() == KErrNotFound);
   375 
   376 	test.Next(_L("View create - when busy"));
   377 	TRequestStatus rs2;
   378 	mngmnt.CreateView(*filter, rs);
   379 	mngmnt.CreateView(*filter, rs2);
   380 	User::WaitForRequest(rs2);
   381 	test(rs2.Int() == KErrServerBusy);
   382 	User::WaitForRequest(rs);
   383 	test(rs.Int() == KErrNone);
   384 	mngmnt.CancelAndCloseView();
   385 
   386 	test.Next(_L("View create - iterate through it"));
   387 	mngmnt.CreateView(*filter, rs);
   388 
   389 	User::WaitForRequest(rs);
   390 	test(rs.Int() == KErrNone);
   391 	
   392 	CleanupStack::PopAndDestroy(filter);
   393 
   394 	CDecisionRecord *record = 0;
   395 	r = KErrNone;
   396 	TInt recordCount = 0;
   397 	while(r == KErrNone)
   398 		{
   399 		TRAP(r, record = mngmnt.NextMatchL());
   400 		if(record == 0)
   401 			{
   402 			break;
   403 			}
   404 		test(r == KErrNone);
   405 		if(r == KErrNone)
   406 			{
   407 			++recordCount;
   408 			CleanupStack::PushL(record);
   409 			CDecisionFilter *exactFilter = CDecisionFilter::NewLC(record->iClientSid,
   410 																  record->iEvaluatorId,
   411 																  record->iServiceId,
   412 																  record->iServerSid,
   413 																  record->iFingerprint,
   414 																  record->iClientEntity,
   415 																  record->iMajorPolicyVersion);
   416 				
   417 			
   418 			mngmnt.UpdateDecision(record->iRecordId, ETrue, rs);
   419 			User::WaitForRequest(rs);
   420 			test(rs.Int() == KErrNone);
   421 			TRAP(r, mngmnt.RemoveDecisionsL(*exactFilter));
   422 			test(r == KErrNone);
   423 
   424 			CleanupStack::PopAndDestroy(exactFilter);
   425 			CleanupStack::PopAndDestroy(record);
   426 			}
   427 		
   428 		};
   429 	test(recordCount == 2);
   430 	
   431 	TRAP(r, record = mngmnt.NextMatchL());
   432 	test((r == KErrNone) && (record == 0));
   433 
   434 	mngmnt.CancelAndCloseView();
   435 
   436 	test.Next(_L("Close management session and clientSubsession"));
   437 	mngmnt.Close();
   438 	clientSubsession.Close();
   439 
   440 	test.End();
   441 	}
   442 
   443 void TestSwiObserverSecurityL()
   444 {
   445 	test.Start(_L("Testing swi observer functions do not work from here..."));
   446 
   447 	TInt r;
   448 
   449 	RUpsManagement session;
   450 	User::LeaveIfError(session.Connect());
   451 	CleanupClosePushL(session);
   452 
   453 	TUid ourSid;
   454 	ourSid.iUid = 0x10283559;
   455 	TRAP(r, session.DeleteDecisionsForExeL(ourSid));
   456 	test(r == KErrPermissionDenied);
   457 
   458 	TRAP(r, session.NotifyPluginsMayHaveChangedL());
   459 	test(r == KErrPermissionDenied);
   460 
   461 	TRequestStatus rs;
   462 	session.NotifyPolicyFilesChanged(rs);
   463 	User::WaitForRequest(rs);
   464 
   465 	test(rs.Int() == KErrPermissionDenied);
   466 
   467 	session.CancelNotifyPolicyFilesChanged();
   468 
   469 	CleanupStack::PopAndDestroy(&session);
   470 	
   471 	test.End();
   472 }
   473 
   474 // -------- entrypoint --------
   475 
   476 
   477 void MainL()
   478 	{
   479 	test.Title(_L("c:\\upstest.log"));
   480 	test.Start(_L(" @SYMTestCaseID:SEC-UPS-0001 Testing RUpsSubsession "));
   481 
   482 	RFs fs;
   483 	User::LeaveIfError(fs.Connect());
   484 	CleanupClosePushL(fs);
   485 	
   486 	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
   487 	notifierConfig[0] = fs.GetSystemDriveChar();
   488 
   489 	TBuf<35> database(_L("!:\\Private\\10283558\\database\\ups.db"));
   490 	database[0] = fs.GetSystemDriveChar();
   491 
   492 	TInt lineLength = User::CommandLineLength();
   493 	switch(lineLength)
   494 		{
   495 	default:
   496 		case 2:
   497 			(void) fs.Delete(database);
   498 			// Fall through to also delete notifier config file
   499 		case 1:
   500 			(void) fs.Delete(notifierConfig);
   501 			break;
   502 		case 0:
   503 			{
   504 			// No args so run in silent mode
   505 			(void) fs.Delete(database);
   506 			(void) fs.Delete(notifierConfig);
   507 			RFile file;
   508 			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
   509 			User::LeaveIfError(file.Write(_L8("Never")));
   510 			file.Close();
   511 			break;
   512 			}
   513 		}
   514 
   515 	//RThread ourThread;
   516 	//ourThread.SetPriority(EPriorityMore);
   517 	//ourThread.Close();
   518 //	User::SetProcessCritical(User::ESystemCritical);
   519 //	User::SetCritical(User::ESystemCritical);
   520 	TestOpenCloseSession();
   521 
   522 
   523 	TInt r = sTestSession.Connect();
   524 	test(r == KErrNone);
   525 	User::LeaveIfError(r);
   526 
   527 	TestRUpsSubsessionDeathL();
   528 
   529 	TestOpenCloseSubsession();
   530 
   531 	TestSwiObserverSecurityL();
   532 	
   533 	RThread thd;
   534 	r = sTestSubsession.Initialise(sTestSession, thd);
   535 	test(r == KErrNone);
   536 	User::LeaveIfError(r);
   537 	
   538 	TestFlurryL();
   539 
   540 	TestAuthoriseL();
   541 	
   542 	sTestSubsession.Close();
   543 
   544 	TestRUpsSubsession();
   545 
   546 	TestRUpsManagementL();
   547 
   548 	sTestSession.ShutdownServer();
   549 
   550 	// Close top level session (low level session was closed by
   551 	// ShutdownServer, but we still need to do the RUpsSession
   552 	// cleanup).
   553 	sTestSession.Close();
   554 
   555 	(void) fs.Delete(notifierConfig);
   556 	CleanupStack::PopAndDestroy(&fs);
   557 	
   558 	test.End();
   559 	test.Close();
   560 }
   561 
   562 void PanicIfError(TInt r)
   563 	{
   564 	if(r != KErrNone)
   565 		{
   566 		User::Panic(_L("upstest failed: "), r);
   567 		}
   568 	}
   569 
   570 
   571 TInt E32Main()
   572 /**
   573 	Executable entrypoint establishes connection with UPS server
   574 	and then invokes tests for each functional area.
   575 	
   576 	@return					Symbian OS error code where KErrNone indicates
   577 							success and any other value indicates failure.
   578  */
   579 	{
   580 	// disable lazy DLL unloading so kernel heap balances at end
   581 	RLoader l;
   582 	PanicIfError(l.Connect());
   583 	PanicIfError(l.CancelLazyDllUnload());
   584 	l.Close();
   585 	
   586 	__UHEAP_MARK;
   587 	//__KHEAP_MARK;
   588 	
   589 	// allocating a cleanup stack also installs it
   590 	CTrapCleanup* tc = CTrapCleanup::New();
   591 	if (tc == 0)
   592 		return KErrNoMemory;
   593 
   594 	TRAPD(err, MainL());
   595 	if(err != KErrNone)
   596 		{
   597 		User::Panic(_L("upstest failed: "), err);
   598 		}
   599 	delete tc;
   600 	
   601 	//__KHEAP_MARKEND;
   602 	__UHEAP_MARKEND;
   603 	
   604 	
   605 	return KErrNone;
   606 	}
   607