os/security/authorisation/userpromptservice/server/test/upstest/upstestoom.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 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 "f32file.h"
    31 
    32 using namespace UserPromptService;
    33 
    34 /** Top-level test object renders stages and confirms conditions. */
    35 static RTestWrapper test(_L("UPSTESTOOM"));
    36 
    37 
    38 static void TestFlurryL()
    39 /**
    40 	Launch multiple requests
    41  */
    42 	{
    43 	RUpsSession session;
    44 	User::LeaveIfError(session.Connect());
    45 	CleanupClosePushL(session);
    46 
    47 	RThread thd;
    48 
    49 #if 0
    50 	RUpsSubsession testSubsession1;
    51 	testSubsession1.Initialise(session, thd);
    52 	
    53 	TServiceId serviceId = {42};
    54 	TRequestStatus rs1;
    55 	
    56 	// complete normally - no opaque data
    57 	TUpsDecision dec1 = EUpsDecYes;
    58 	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
    59 
    60 	User::After(1000);
    61 
    62 	User::WaitForRequest(rs1);
    63 	User::LeaveIfError(rs1.Int());
    64 	test(dec1 == EUpsDecNo);
    65 #else
    66 	RUpsSubsession testSubsession1;
    67 	testSubsession1.Initialise(session, thd);
    68 	RUpsSubsession testSubsession2;
    69 	testSubsession2.Initialise(session, thd);
    70 	RUpsSubsession testSubsession3;
    71 	testSubsession3.Initialise(session, thd);
    72 	RUpsSubsession testSubsession4;
    73 	testSubsession4.Initialise(session, thd);
    74 	RUpsSubsession testSubsession5;
    75 	testSubsession5.Initialise(session, thd);
    76 	RUpsSubsession testSubsession6;
    77 	testSubsession6.Initialise(session, thd);
    78 	
    79 	TServiceId serviceId = {42};
    80 	TRequestStatus rs1;
    81 	TRequestStatus rs2;
    82 	TRequestStatus rs3;
    83 	TRequestStatus rs4;
    84 	TRequestStatus rs5;
    85 	TRequestStatus rs6;
    86 	
    87 	// complete normally - no opaque data
    88 	TUpsDecision dec1 = EUpsDecYes;
    89 	TUpsDecision dec2 = EUpsDecYes;
    90 	TUpsDecision dec3 = EUpsDecYes;
    91 	TUpsDecision dec4 = EUpsDecYes;
    92 	TUpsDecision dec5 = EUpsDecYes;
    93 	TUpsDecision dec6 = EUpsDecYes;
    94 	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
    95 	testSubsession2.Authorise(EFalse, serviceId, _L("reqX"), dec2, rs2);
    96 	testSubsession3.Authorise(EFalse, serviceId, _L("req2"), dec3, rs3);
    97 	testSubsession4.Authorise(EFalse, serviceId, _L("reqX"), dec4, rs4);
    98 	testSubsession5.Authorise(EFalse, serviceId, _L("req3"), dec5, rs5);
    99 	testSubsession6.Authorise(EFalse, serviceId, _L("req4"), dec6, rs6);
   100 
   101 	User::After(1000);
   102 
   103 	User::WaitForRequest(rs1);
   104 
   105 	User::WaitForRequest(rs2);
   106 	User::WaitForRequest(rs4);
   107 
   108 	User::WaitForRequest(rs3);
   109 	User::WaitForRequest(rs5);
   110 	User::WaitForRequest(rs6);
   111 	User::LeaveIfError(rs1.Int());
   112 	User::LeaveIfError(rs2.Int());
   113 	User::LeaveIfError(rs3.Int());
   114 	User::LeaveIfError(rs4.Int());
   115 	User::LeaveIfError(rs5.Int());
   116 	User::LeaveIfError(rs6.Int());
   117 	test(dec1 == EUpsDecNo);
   118 	test(dec2 == EUpsDecNo);
   119 	test(dec3 == EUpsDecNo);
   120 	test(dec4 == EUpsDecNo);
   121 	test(dec5 == EUpsDecNo);
   122 	test(dec6 == EUpsDecNo);
   123 #endif
   124 	
   125 	CleanupStack::PopAndDestroy(&session);
   126 	}
   127 
   128 static void TestRUpsManagementL()
   129 /**
   130 	Attempt to delete database
   131  */
   132 	{
   133 //	test.Start(_L("Testing RUpsManagement"));
   134 	RThread thd;
   135 	TRequestStatus rs;
   136 	TInt r;
   137 
   138 	// Create filter
   139 	TServiceId serviceId = {43};
   140 	CDecisionFilter *filter = CDecisionFilter::NewLC();
   141 	filter->SetClientSid(thd.SecureId(), EEqual);
   142 	filter->SetServerSid(thd.SecureId(), EEqual);
   143 	filter->SetServiceId(serviceId, EEqual);
   144 
   145 	RUpsSession session;
   146 	User::LeaveIfError(session.Connect());
   147 	CleanupClosePushL(session);
   148 
   149 	RUpsSubsession clientSubsession;
   150 	User::LeaveIfError(clientSubsession.Initialise(session, thd));
   151 	CleanupClosePushL(clientSubsession);
   152 
   153 	test.Printf(_L("Open management session\n"));
   154 	RUpsManagement mngmnt;
   155 	User::LeaveIfError(mngmnt.Connect());
   156 	CleanupClosePushL(mngmnt);
   157 	
   158 	test.Printf(_L("View create - then delete DB\n"));
   159 	mngmnt.CreateView(*filter, rs);
   160 
   161 	test.Printf(_L("Delete database\n"));
   162 	TRAP(r, mngmnt.DeleteDatabaseL());
   163 	test((r == KErrNone) || (r == KErrNoMemory));
   164 
   165 	test.Printf(_L("Now see what view create completed with....\n"));
   166 	User::WaitForRequest(rs);
   167 	// The createview may actually complete if the DeleteDatabase fails due to OOM
   168 //	test((rs.Int() == KErrAbort) || (rs.Int() == KErrNoMemory) || (rs.Int() == KErrNone));
   169 	test((rs.Int() == KErrAbort) || (rs.Int() == KErrNoMemory));
   170 
   171 	test.Printf(_L("Add entry to new database\n"));
   172 	TUpsDecision dec = EUpsDecYes;
   173 	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 1"), _L8("Opaque data"), dec, rs);
   174 	User::WaitForRequest(rs);
   175 	test(rs == KErrNone);
   176 	test(dec == EUpsDecNo);
   177 
   178 	dec = EUpsDecYes;
   179 	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 2"), _L8("Opaque data"), dec, rs);
   180 	User::WaitForRequest(rs);
   181 	test(rs == KErrNone);
   182 	test(dec == EUpsDecNo);
   183 
   184 
   185 	test.Printf(_L("View create - immediate cancel\n"));
   186 	mngmnt.CreateView(*filter, rs);
   187 	mngmnt.CancelAndCloseView();
   188 	User::WaitForRequest(rs);
   189 	test((rs.Int() == KErrCancel) || (rs.Int() == KErrNoMemory));
   190 
   191 	test.Printf(_L("View create - iterate through it\n"));
   192 	mngmnt.CreateView(*filter, rs);
   193 
   194 	User::WaitForRequest(rs);
   195 	test((rs.Int() == KErrNone) || (rs.Int() == KErrNoMemory));
   196 	
   197 
   198 	CDecisionRecord *record = 0;
   199 	r = KErrNone;
   200 	while(r == KErrNone)
   201 		{
   202 		TRAP(r, record = mngmnt.NextMatchL());
   203 		if(record == 0)
   204 			{
   205 			break;
   206 			}
   207 		test(r == KErrNone);
   208     	if(r == KErrNone)
   209     		{
   210     		CleanupStack::PushL(record);
   211 			CDecisionFilter *exactFilter = CDecisionFilter::NewLC(record->iClientSid,
   212 																  record->iEvaluatorId,
   213 																  record->iServiceId,
   214 																  record->iServerSid,
   215 																  record->iFingerprint,
   216 																  record->iClientEntity,
   217 																  record->iMajorPolicyVersion);
   218 				
   219 			mngmnt.UpdateDecision(record->iRecordId, ETrue, rs);
   220 			User::WaitForRequest(rs);
   221 			test(rs.Int() == KErrNone);
   222 			TRAP(r, mngmnt.RemoveDecisionsL(*exactFilter));
   223 			test(r == KErrNone);
   224 
   225 			CleanupStack::PopAndDestroy(exactFilter);
   226 			CleanupStack::PopAndDestroy(record);
   227     		}
   228 		
   229 		};
   230 
   231 	
   232 	TRAP(r, record = mngmnt.NextMatchL());
   233 	test((r == KErrNone) && (record == 0));
   234 
   235 	mngmnt.CancelAndCloseView();
   236 
   237 	test.Printf(_L("Close management session and clientSubsession\n"));
   238 	CleanupStack::PopAndDestroy(&mngmnt);
   239 	CleanupStack::PopAndDestroy(&clientSubsession);
   240 	CleanupStack::PopAndDestroy(&session);
   241 	CleanupStack::PopAndDestroy(filter);
   242 
   243 //	test.End();
   244 	}
   245 
   246 void MainL()
   247 	{
   248 	test.Title(_L("c:\\upstestoom.log"));
   249 	test.Start(_L(" @SYMTestCaseID:SEC-UPS-OOM-0001 Testing UPS OOM "));
   250 
   251 	// We need to increase the priority of the thread running the test code to make sure
   252 	// that asynchronous ups management calls, for example CreateView() , won't finish
   253 	// before following synchronous cancellation or termination calls, for example:
   254 	// DeleteDatabaseL() or CancelAndCloseView().
   255 	RThread thread;
   256 	TThreadPriority currentPri = thread.Priority();
   257 	currentPri = (TThreadPriority)((TInt)currentPri+10);
   258 	thread.SetPriority(currentPri);
   259 
   260 	RFs fs;
   261 	User::LeaveIfError(fs.Connect());
   262 	CleanupClosePushL(fs);
   263 	
   264 	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
   265 	notifierConfig[0] = fs.GetSystemDriveChar();
   266 
   267 	TBuf<35> database(_L("!:\\Private\\10283558\\database\\ups.db"));
   268 	database[0] = fs.GetSystemDriveChar();
   269 
   270 	TInt lineLength = User::CommandLineLength();
   271 	switch(lineLength)
   272 		{
   273 		default:
   274 			// fall through - extra command line arguments are ignored
   275 		case 2:
   276 			(void) fs.Delete(database);
   277 			// Fall through to also delete notifier config file
   278 		case 1:
   279 			(void) fs.Delete(notifierConfig);
   280 			break;
   281 		case 0:
   282 			{
   283 			// No args so run in silent mode
   284 			(void) fs.Delete(database);
   285 			(void) fs.Delete(notifierConfig);
   286 			RFile file;
   287 			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
   288 			User::LeaveIfError(file.Write(_L8("Never")));
   289 			file.Close();
   290 			break;
   291 			}
   292 		}
   293 
   294 	// Connect to server
   295 	RUpsSession session;
   296 	User::LeaveIfError(session.Connect());
   297 	CleanupClosePushL(session);
   298 
   299 	TInt err = KErrNone;
   300 	TInt err2 = KErrNone;
   301 	TInt run = 0;
   302 	TInt passingRuns = 0;
   303 	static const TInt maxRun = 1000;
   304 	static const TInt passThreshold = 5;
   305 	for(run=1; run<=maxRun; ++run)
   306 		{
   307 		test.Printf(_L("\n\nOOM -- Run %d\n"), run);
   308 		err = session.SetServerHeapFail(run);
   309 		if(err == KErrNoMemory)
   310 			{
   311 			// Reinitialisation failed
   312 			test.Printf(_L("\tReinitialisation failed\n"));
   313 			session.ResetServerHeapFail();
   314 			continue;
   315 			}
   316 		if(err != KErrNone)
   317 			{
   318 			// Failed to set heap fail, maybe the previous loop crashed the server??
   319 			test.Printf(_L("Failed to set heap fail with error code %d\n"), err);
   320 			test(EFalse);
   321 			break;
   322 			}
   323 
   324 		// Run the test
   325 		//test.Start(_L("TestFlurry"));
   326 		test.Printf(_L("TestFlurry\n"));
   327 		TRAP(err, TestFlurryL());
   328 		test.Printf(_L("TestRUpsManagementL\n"));
   329 		TRAP(err2, TestRUpsManagementL());
   330 		if(err == KErrNone)
   331 			{
   332 			err = err2;
   333 			}
   334 		test.Printf(_L("done\n"));
   335 		//test.End();
   336 		// Clear the heap fail
   337 		test.Printf(_L("Reseting heap failure\n"));
   338 		err2 = session.ResetServerHeapFail();
   339 		if((err == KErrServerTerminated) || (err2 == KErrServerTerminated))
   340 			{
   341 			test.Printf(_L("\tUPS server died\n"));
   342 			test(EFalse);
   343 			break;
   344 			}
   345 		if((err == KErrNone) && (err2 != KErrNone))
   346 			{
   347 			err = err2;
   348 			}
   349 		
   350 		// Did it work?
   351 		if(err == KErrNone)
   352 			{
   353 			++passingRuns;
   354 			}
   355 		else
   356 			{
   357 			passingRuns = 0;
   358 			}
   359 		
   360 		if(passingRuns > passThreshold) break;
   361 		} // End of OOM loop
   362 
   363 	if(run > maxRun)
   364 		{
   365 		User::Leave(err);
   366 		}
   367 
   368 	// Synchronous call to shutdown the server
   369 	session.ShutdownServer();
   370 	// Close top level session (low level session was closed by
   371 	// ShutdownServer, but we still need to do the RUpsSession
   372 	// cleanup).
   373 	CleanupStack::PopAndDestroy(&session);
   374 
   375 	(void) fs.Delete(notifierConfig);
   376 	CleanupStack::PopAndDestroy(&fs);
   377 	
   378 	test.End();
   379 	test.Close();
   380 }
   381 
   382 void PanicIfError(TInt r)
   383 	{
   384 	if(r != KErrNone)
   385 		{
   386 		User::Panic(_L("upstestoom failed: "), r);
   387 		}
   388 	}
   389 
   390 
   391 // -------- entrypoint --------
   392 TInt E32Main()
   393 /**
   394 	Executable entrypoint establishes connection with UPS server
   395 	and then invokes tests for each functional area.
   396 	
   397 	@return					Symbian OS error code where KErrNone indicates
   398 							success and any other value indicates failure.
   399  */
   400 	{
   401 	// disable lazy DLL unloading so kernel heap balances at end
   402 	RLoader l;
   403 	PanicIfError(l.Connect());
   404 	PanicIfError(l.CancelLazyDllUnload());
   405 	l.Close();
   406 	
   407 	__UHEAP_MARK;
   408 	//__KHEAP_MARK;
   409 	
   410 	// allocating a cleanup stack also installs it
   411 	CTrapCleanup* tc = CTrapCleanup::New();
   412 	if (tc == 0)
   413 		return KErrNoMemory;
   414 
   415 	TRAPD(err, MainL());
   416 	PanicIfError(err);
   417 	delete tc;
   418 	
   419 	//__KHEAP_MARKEND;
   420 	__UHEAP_MARKEND;
   421 	
   422 	
   423 	return KErrNone;
   424 	}
   425 
   426 // End of file