os/security/authorisation/userpromptservice/server/test/upstest/upstestoom.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/server/test/upstest/upstestoom.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,426 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Test program exercises the skeleton UPS server API.
    1.19 +* See individual test functions for more information.
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +
    1.28 +#include <e32ldr.h>
    1.29 +#include <e32ldr_private.h>
    1.30 +#include "rtestwrapper.h"
    1.31 +
    1.32 +#include <ups/upsclient.h>
    1.33 +#include "f32file.h"
    1.34 +
    1.35 +using namespace UserPromptService;
    1.36 +
    1.37 +/** Top-level test object renders stages and confirms conditions. */
    1.38 +static RTestWrapper test(_L("UPSTESTOOM"));
    1.39 +
    1.40 +
    1.41 +static void TestFlurryL()
    1.42 +/**
    1.43 +	Launch multiple requests
    1.44 + */
    1.45 +	{
    1.46 +	RUpsSession session;
    1.47 +	User::LeaveIfError(session.Connect());
    1.48 +	CleanupClosePushL(session);
    1.49 +
    1.50 +	RThread thd;
    1.51 +
    1.52 +#if 0
    1.53 +	RUpsSubsession testSubsession1;
    1.54 +	testSubsession1.Initialise(session, thd);
    1.55 +	
    1.56 +	TServiceId serviceId = {42};
    1.57 +	TRequestStatus rs1;
    1.58 +	
    1.59 +	// complete normally - no opaque data
    1.60 +	TUpsDecision dec1 = EUpsDecYes;
    1.61 +	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
    1.62 +
    1.63 +	User::After(1000);
    1.64 +
    1.65 +	User::WaitForRequest(rs1);
    1.66 +	User::LeaveIfError(rs1.Int());
    1.67 +	test(dec1 == EUpsDecNo);
    1.68 +#else
    1.69 +	RUpsSubsession testSubsession1;
    1.70 +	testSubsession1.Initialise(session, thd);
    1.71 +	RUpsSubsession testSubsession2;
    1.72 +	testSubsession2.Initialise(session, thd);
    1.73 +	RUpsSubsession testSubsession3;
    1.74 +	testSubsession3.Initialise(session, thd);
    1.75 +	RUpsSubsession testSubsession4;
    1.76 +	testSubsession4.Initialise(session, thd);
    1.77 +	RUpsSubsession testSubsession5;
    1.78 +	testSubsession5.Initialise(session, thd);
    1.79 +	RUpsSubsession testSubsession6;
    1.80 +	testSubsession6.Initialise(session, thd);
    1.81 +	
    1.82 +	TServiceId serviceId = {42};
    1.83 +	TRequestStatus rs1;
    1.84 +	TRequestStatus rs2;
    1.85 +	TRequestStatus rs3;
    1.86 +	TRequestStatus rs4;
    1.87 +	TRequestStatus rs5;
    1.88 +	TRequestStatus rs6;
    1.89 +	
    1.90 +	// complete normally - no opaque data
    1.91 +	TUpsDecision dec1 = EUpsDecYes;
    1.92 +	TUpsDecision dec2 = EUpsDecYes;
    1.93 +	TUpsDecision dec3 = EUpsDecYes;
    1.94 +	TUpsDecision dec4 = EUpsDecYes;
    1.95 +	TUpsDecision dec5 = EUpsDecYes;
    1.96 +	TUpsDecision dec6 = EUpsDecYes;
    1.97 +	testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1);
    1.98 +	testSubsession2.Authorise(EFalse, serviceId, _L("reqX"), dec2, rs2);
    1.99 +	testSubsession3.Authorise(EFalse, serviceId, _L("req2"), dec3, rs3);
   1.100 +	testSubsession4.Authorise(EFalse, serviceId, _L("reqX"), dec4, rs4);
   1.101 +	testSubsession5.Authorise(EFalse, serviceId, _L("req3"), dec5, rs5);
   1.102 +	testSubsession6.Authorise(EFalse, serviceId, _L("req4"), dec6, rs6);
   1.103 +
   1.104 +	User::After(1000);
   1.105 +
   1.106 +	User::WaitForRequest(rs1);
   1.107 +
   1.108 +	User::WaitForRequest(rs2);
   1.109 +	User::WaitForRequest(rs4);
   1.110 +
   1.111 +	User::WaitForRequest(rs3);
   1.112 +	User::WaitForRequest(rs5);
   1.113 +	User::WaitForRequest(rs6);
   1.114 +	User::LeaveIfError(rs1.Int());
   1.115 +	User::LeaveIfError(rs2.Int());
   1.116 +	User::LeaveIfError(rs3.Int());
   1.117 +	User::LeaveIfError(rs4.Int());
   1.118 +	User::LeaveIfError(rs5.Int());
   1.119 +	User::LeaveIfError(rs6.Int());
   1.120 +	test(dec1 == EUpsDecNo);
   1.121 +	test(dec2 == EUpsDecNo);
   1.122 +	test(dec3 == EUpsDecNo);
   1.123 +	test(dec4 == EUpsDecNo);
   1.124 +	test(dec5 == EUpsDecNo);
   1.125 +	test(dec6 == EUpsDecNo);
   1.126 +#endif
   1.127 +	
   1.128 +	CleanupStack::PopAndDestroy(&session);
   1.129 +	}
   1.130 +
   1.131 +static void TestRUpsManagementL()
   1.132 +/**
   1.133 +	Attempt to delete database
   1.134 + */
   1.135 +	{
   1.136 +//	test.Start(_L("Testing RUpsManagement"));
   1.137 +	RThread thd;
   1.138 +	TRequestStatus rs;
   1.139 +	TInt r;
   1.140 +
   1.141 +	// Create filter
   1.142 +	TServiceId serviceId = {43};
   1.143 +	CDecisionFilter *filter = CDecisionFilter::NewLC();
   1.144 +	filter->SetClientSid(thd.SecureId(), EEqual);
   1.145 +	filter->SetServerSid(thd.SecureId(), EEqual);
   1.146 +	filter->SetServiceId(serviceId, EEqual);
   1.147 +
   1.148 +	RUpsSession session;
   1.149 +	User::LeaveIfError(session.Connect());
   1.150 +	CleanupClosePushL(session);
   1.151 +
   1.152 +	RUpsSubsession clientSubsession;
   1.153 +	User::LeaveIfError(clientSubsession.Initialise(session, thd));
   1.154 +	CleanupClosePushL(clientSubsession);
   1.155 +
   1.156 +	test.Printf(_L("Open management session\n"));
   1.157 +	RUpsManagement mngmnt;
   1.158 +	User::LeaveIfError(mngmnt.Connect());
   1.159 +	CleanupClosePushL(mngmnt);
   1.160 +	
   1.161 +	test.Printf(_L("View create - then delete DB\n"));
   1.162 +	mngmnt.CreateView(*filter, rs);
   1.163 +
   1.164 +	test.Printf(_L("Delete database\n"));
   1.165 +	TRAP(r, mngmnt.DeleteDatabaseL());
   1.166 +	test((r == KErrNone) || (r == KErrNoMemory));
   1.167 +
   1.168 +	test.Printf(_L("Now see what view create completed with....\n"));
   1.169 +	User::WaitForRequest(rs);
   1.170 +	// The createview may actually complete if the DeleteDatabase fails due to OOM
   1.171 +//	test((rs.Int() == KErrAbort) || (rs.Int() == KErrNoMemory) || (rs.Int() == KErrNone));
   1.172 +	test((rs.Int() == KErrAbort) || (rs.Int() == KErrNoMemory));
   1.173 +
   1.174 +	test.Printf(_L("Add entry to new database\n"));
   1.175 +	TUpsDecision dec = EUpsDecYes;
   1.176 +	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 1"), _L8("Opaque data"), dec, rs);
   1.177 +	User::WaitForRequest(rs);
   1.178 +	test(rs == KErrNone);
   1.179 +	test(dec == EUpsDecNo);
   1.180 +
   1.181 +	dec = EUpsDecYes;
   1.182 +	clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 2"), _L8("Opaque data"), dec, rs);
   1.183 +	User::WaitForRequest(rs);
   1.184 +	test(rs == KErrNone);
   1.185 +	test(dec == EUpsDecNo);
   1.186 +
   1.187 +
   1.188 +	test.Printf(_L("View create - immediate cancel\n"));
   1.189 +	mngmnt.CreateView(*filter, rs);
   1.190 +	mngmnt.CancelAndCloseView();
   1.191 +	User::WaitForRequest(rs);
   1.192 +	test((rs.Int() == KErrCancel) || (rs.Int() == KErrNoMemory));
   1.193 +
   1.194 +	test.Printf(_L("View create - iterate through it\n"));
   1.195 +	mngmnt.CreateView(*filter, rs);
   1.196 +
   1.197 +	User::WaitForRequest(rs);
   1.198 +	test((rs.Int() == KErrNone) || (rs.Int() == KErrNoMemory));
   1.199 +	
   1.200 +
   1.201 +	CDecisionRecord *record = 0;
   1.202 +	r = KErrNone;
   1.203 +	while(r == KErrNone)
   1.204 +		{
   1.205 +		TRAP(r, record = mngmnt.NextMatchL());
   1.206 +		if(record == 0)
   1.207 +			{
   1.208 +			break;
   1.209 +			}
   1.210 +		test(r == KErrNone);
   1.211 +    	if(r == KErrNone)
   1.212 +    		{
   1.213 +    		CleanupStack::PushL(record);
   1.214 +			CDecisionFilter *exactFilter = CDecisionFilter::NewLC(record->iClientSid,
   1.215 +																  record->iEvaluatorId,
   1.216 +																  record->iServiceId,
   1.217 +																  record->iServerSid,
   1.218 +																  record->iFingerprint,
   1.219 +																  record->iClientEntity,
   1.220 +																  record->iMajorPolicyVersion);
   1.221 +				
   1.222 +			mngmnt.UpdateDecision(record->iRecordId, ETrue, rs);
   1.223 +			User::WaitForRequest(rs);
   1.224 +			test(rs.Int() == KErrNone);
   1.225 +			TRAP(r, mngmnt.RemoveDecisionsL(*exactFilter));
   1.226 +			test(r == KErrNone);
   1.227 +
   1.228 +			CleanupStack::PopAndDestroy(exactFilter);
   1.229 +			CleanupStack::PopAndDestroy(record);
   1.230 +    		}
   1.231 +		
   1.232 +		};
   1.233 +
   1.234 +	
   1.235 +	TRAP(r, record = mngmnt.NextMatchL());
   1.236 +	test((r == KErrNone) && (record == 0));
   1.237 +
   1.238 +	mngmnt.CancelAndCloseView();
   1.239 +
   1.240 +	test.Printf(_L("Close management session and clientSubsession\n"));
   1.241 +	CleanupStack::PopAndDestroy(&mngmnt);
   1.242 +	CleanupStack::PopAndDestroy(&clientSubsession);
   1.243 +	CleanupStack::PopAndDestroy(&session);
   1.244 +	CleanupStack::PopAndDestroy(filter);
   1.245 +
   1.246 +//	test.End();
   1.247 +	}
   1.248 +
   1.249 +void MainL()
   1.250 +	{
   1.251 +	test.Title(_L("c:\\upstestoom.log"));
   1.252 +	test.Start(_L(" @SYMTestCaseID:SEC-UPS-OOM-0001 Testing UPS OOM "));
   1.253 +
   1.254 +	// We need to increase the priority of the thread running the test code to make sure
   1.255 +	// that asynchronous ups management calls, for example CreateView() , won't finish
   1.256 +	// before following synchronous cancellation or termination calls, for example:
   1.257 +	// DeleteDatabaseL() or CancelAndCloseView().
   1.258 +	RThread thread;
   1.259 +	TThreadPriority currentPri = thread.Priority();
   1.260 +	currentPri = (TThreadPriority)((TInt)currentPri+10);
   1.261 +	thread.SetPriority(currentPri);
   1.262 +
   1.263 +	RFs fs;
   1.264 +	User::LeaveIfError(fs.Connect());
   1.265 +	CleanupClosePushL(fs);
   1.266 +	
   1.267 +	TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
   1.268 +	notifierConfig[0] = fs.GetSystemDriveChar();
   1.269 +
   1.270 +	TBuf<35> database(_L("!:\\Private\\10283558\\database\\ups.db"));
   1.271 +	database[0] = fs.GetSystemDriveChar();
   1.272 +
   1.273 +	TInt lineLength = User::CommandLineLength();
   1.274 +	switch(lineLength)
   1.275 +		{
   1.276 +		default:
   1.277 +			// fall through - extra command line arguments are ignored
   1.278 +		case 2:
   1.279 +			(void) fs.Delete(database);
   1.280 +			// Fall through to also delete notifier config file
   1.281 +		case 1:
   1.282 +			(void) fs.Delete(notifierConfig);
   1.283 +			break;
   1.284 +		case 0:
   1.285 +			{
   1.286 +			// No args so run in silent mode
   1.287 +			(void) fs.Delete(database);
   1.288 +			(void) fs.Delete(notifierConfig);
   1.289 +			RFile file;
   1.290 +			User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
   1.291 +			User::LeaveIfError(file.Write(_L8("Never")));
   1.292 +			file.Close();
   1.293 +			break;
   1.294 +			}
   1.295 +		}
   1.296 +
   1.297 +	// Connect to server
   1.298 +	RUpsSession session;
   1.299 +	User::LeaveIfError(session.Connect());
   1.300 +	CleanupClosePushL(session);
   1.301 +
   1.302 +	TInt err = KErrNone;
   1.303 +	TInt err2 = KErrNone;
   1.304 +	TInt run = 0;
   1.305 +	TInt passingRuns = 0;
   1.306 +	static const TInt maxRun = 1000;
   1.307 +	static const TInt passThreshold = 5;
   1.308 +	for(run=1; run<=maxRun; ++run)
   1.309 +		{
   1.310 +		test.Printf(_L("\n\nOOM -- Run %d\n"), run);
   1.311 +		err = session.SetServerHeapFail(run);
   1.312 +		if(err == KErrNoMemory)
   1.313 +			{
   1.314 +			// Reinitialisation failed
   1.315 +			test.Printf(_L("\tReinitialisation failed\n"));
   1.316 +			session.ResetServerHeapFail();
   1.317 +			continue;
   1.318 +			}
   1.319 +		if(err != KErrNone)
   1.320 +			{
   1.321 +			// Failed to set heap fail, maybe the previous loop crashed the server??
   1.322 +			test.Printf(_L("Failed to set heap fail with error code %d\n"), err);
   1.323 +			test(EFalse);
   1.324 +			break;
   1.325 +			}
   1.326 +
   1.327 +		// Run the test
   1.328 +		//test.Start(_L("TestFlurry"));
   1.329 +		test.Printf(_L("TestFlurry\n"));
   1.330 +		TRAP(err, TestFlurryL());
   1.331 +		test.Printf(_L("TestRUpsManagementL\n"));
   1.332 +		TRAP(err2, TestRUpsManagementL());
   1.333 +		if(err == KErrNone)
   1.334 +			{
   1.335 +			err = err2;
   1.336 +			}
   1.337 +		test.Printf(_L("done\n"));
   1.338 +		//test.End();
   1.339 +		// Clear the heap fail
   1.340 +		test.Printf(_L("Reseting heap failure\n"));
   1.341 +		err2 = session.ResetServerHeapFail();
   1.342 +		if((err == KErrServerTerminated) || (err2 == KErrServerTerminated))
   1.343 +			{
   1.344 +			test.Printf(_L("\tUPS server died\n"));
   1.345 +			test(EFalse);
   1.346 +			break;
   1.347 +			}
   1.348 +		if((err == KErrNone) && (err2 != KErrNone))
   1.349 +			{
   1.350 +			err = err2;
   1.351 +			}
   1.352 +		
   1.353 +		// Did it work?
   1.354 +		if(err == KErrNone)
   1.355 +			{
   1.356 +			++passingRuns;
   1.357 +			}
   1.358 +		else
   1.359 +			{
   1.360 +			passingRuns = 0;
   1.361 +			}
   1.362 +		
   1.363 +		if(passingRuns > passThreshold) break;
   1.364 +		} // End of OOM loop
   1.365 +
   1.366 +	if(run > maxRun)
   1.367 +		{
   1.368 +		User::Leave(err);
   1.369 +		}
   1.370 +
   1.371 +	// Synchronous call to shutdown the server
   1.372 +	session.ShutdownServer();
   1.373 +	// Close top level session (low level session was closed by
   1.374 +	// ShutdownServer, but we still need to do the RUpsSession
   1.375 +	// cleanup).
   1.376 +	CleanupStack::PopAndDestroy(&session);
   1.377 +
   1.378 +	(void) fs.Delete(notifierConfig);
   1.379 +	CleanupStack::PopAndDestroy(&fs);
   1.380 +	
   1.381 +	test.End();
   1.382 +	test.Close();
   1.383 +}
   1.384 +
   1.385 +void PanicIfError(TInt r)
   1.386 +	{
   1.387 +	if(r != KErrNone)
   1.388 +		{
   1.389 +		User::Panic(_L("upstestoom failed: "), r);
   1.390 +		}
   1.391 +	}
   1.392 +
   1.393 +
   1.394 +// -------- entrypoint --------
   1.395 +TInt E32Main()
   1.396 +/**
   1.397 +	Executable entrypoint establishes connection with UPS server
   1.398 +	and then invokes tests for each functional area.
   1.399 +	
   1.400 +	@return					Symbian OS error code where KErrNone indicates
   1.401 +							success and any other value indicates failure.
   1.402 + */
   1.403 +	{
   1.404 +	// disable lazy DLL unloading so kernel heap balances at end
   1.405 +	RLoader l;
   1.406 +	PanicIfError(l.Connect());
   1.407 +	PanicIfError(l.CancelLazyDllUnload());
   1.408 +	l.Close();
   1.409 +	
   1.410 +	__UHEAP_MARK;
   1.411 +	//__KHEAP_MARK;
   1.412 +	
   1.413 +	// allocating a cleanup stack also installs it
   1.414 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.415 +	if (tc == 0)
   1.416 +		return KErrNoMemory;
   1.417 +
   1.418 +	TRAPD(err, MainL());
   1.419 +	PanicIfError(err);
   1.420 +	delete tc;
   1.421 +	
   1.422 +	//__KHEAP_MARKEND;
   1.423 +	__UHEAP_MARKEND;
   1.424 +	
   1.425 +	
   1.426 +	return KErrNone;
   1.427 +	}
   1.428 +
   1.429 +// End of file