os/ossrv/lowlevellibsandfws/apputils/tsrc/T_BackupServerIPC.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/tsrc/T_BackupServerIPC.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,435 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +
    1.20 +#include <e32test.h>
    1.21 +#include <e32math.h>
    1.22 +#include <e32debug.h>
    1.23 +
    1.24 +#include "backup_std.h"
    1.25 +
    1.26 +LOCAL_D RTest				TheTest (_L("T_BackupServerIPC"));
    1.27 +LOCAL_D CActiveScheduler*	TheScheduler;
    1.28 +_LIT(KServerLauncherProcess, "T_BackupServerLauncher");
    1.29 +_LIT(KServerStartProcess, "baksrvs");
    1.30 +
    1.31 +_LIT(KServerName, "!BackupServer");
    1.32 +
    1.33 +struct TTestInfo
    1.34 +	{
    1.35 +	TInt iFunction;
    1.36 +	TInt iType;
    1.37 +	TInt iArgCount;
    1.38 +	};
    1.39 +
    1.40 +struct TExitDetails
    1.41 +	{
    1.42 +	TExitCategoryName iCategory;
    1.43 +	TExitType iExitType;
    1.44 +	TInt iReason;
    1.45 +	};
    1.46 +
    1.47 +const TInt KAsynchDelay = 500000;
    1.48 +
    1.49 +//===============================================================================
    1.50 +
    1.51 +TBool IsFunctionAsynchronous(TInt aFunc)
    1.52 +	{
    1.53 +	TBool asynch = EFalse;
    1.54 +	switch(aFunc)
    1.55 +		{
    1.56 +		case EBakOpCodeEventReady:
    1.57 +		case EBakOpCodeCloseAllFiles:
    1.58 +		case EBakOpCodeCloseServer:
    1.59 +		case EBakOpCodeBackupOperationEventReady:
    1.60 +			asynch = ETrue;
    1.61 +			break;
    1.62 +
    1.63 +		default:
    1.64 +			break;
    1.65 +		}
    1.66 +	return asynch;
    1.67 +	}
    1.68 +
    1.69 +class RIpcFuzzTest : public RSessionBase
    1.70 +{
    1.71 +public: // Constructors and destructor
    1.72 +
    1.73 +	/**
    1.74 +		* Constructor for performing 1st stage construction
    1.75 +		*/
    1.76 +	RIpcFuzzTest();
    1.77 +
    1.78 +	/**
    1.79 +		* Destructor.
    1.80 +		*/
    1.81 +	~RIpcFuzzTest();
    1.82 +
    1.83 +	/**
    1.84 +		* Performs test steps
    1.85 +		*/
    1.86 +
    1.87 +	void RunTestL(const TDesC& aTargetSrvName, TInt aFunc,
    1.88 +				TInt aTestType, TInt aArgCount);
    1.89 +
    1.90 +private:
    1.91 +	TInt Fuzz(TInt aMsg, TInt aArgCount);
    1.92 +	TInt FuzzL(TInt aMsg, TInt aArgCount);
    1.93 +	TInt Fuzz8L(TInt aMsg, TInt aArgCount);
    1.94 +};
    1.95 +
    1.96 +RIpcFuzzTest::RIpcFuzzTest()
    1.97 +	{
    1.98 +	// No implementation required
    1.99 +	}
   1.100 +
   1.101 +
   1.102 +RIpcFuzzTest::~RIpcFuzzTest()
   1.103 +	{
   1.104 +	Close();
   1.105 +	}
   1.106 +
   1.107 +TInt RIpcFuzzTest::Fuzz(TInt aMsg, TInt aArgCount)
   1.108 +	{
   1.109 +	TIpcArgs args;
   1.110 +
   1.111 +	for(TInt i = 0; i < aArgCount;i++)
   1.112 +		{
   1.113 +		args.Set(i,Math::Random());
   1.114 +		}
   1.115 +
   1.116 +	TInt ret;
   1.117 +
   1.118 +	if(IsFunctionAsynchronous(aMsg))
   1.119 +		{
   1.120 +		ret = Send(aMsg, args);
   1.121 +		User::After(KAsynchDelay);
   1.122 +		}
   1.123 +	else
   1.124 +		{
   1.125 +		ret = SendReceive(aMsg, args);
   1.126 +		}
   1.127 +	return ret;
   1.128 +	}
   1.129 +
   1.130 +TInt RIpcFuzzTest::Fuzz8L(TInt aMsg, TInt aArgCount)
   1.131 +	{
   1.132 +	HBufC8* buf = HBufC8::NewLC(255);
   1.133 +	TPtr8 ptr = buf->Des();
   1.134 +	ptr.Fill(Math::Random(),255);
   1.135 +
   1.136 +	TIpcArgs args;
   1.137 +
   1.138 +	for(TInt i = 0; i < aArgCount;i++)
   1.139 +		{
   1.140 +		args.Set(i,&ptr);
   1.141 +		}
   1.142 +
   1.143 +	TInt ret;
   1.144 +
   1.145 +	if(IsFunctionAsynchronous(aMsg))
   1.146 +		{
   1.147 +		ret = Send(aMsg, args);
   1.148 +		User::After(KAsynchDelay);
   1.149 +		}
   1.150 +	else
   1.151 +		{
   1.152 +		ret = SendReceive(aMsg, args);
   1.153 +		}
   1.154 +
   1.155 +	CleanupStack::PopAndDestroy(buf);
   1.156 +	return ret;
   1.157 +	}
   1.158 +
   1.159 +TInt RIpcFuzzTest::FuzzL(TInt aMsg, TInt aArgCount)
   1.160 +	{
   1.161 +	HBufC* buf = HBufC::NewLC(255);
   1.162 +	TPtr ptr = buf->Des();
   1.163 +	ptr.Fill(Math::Random(),255);
   1.164 +
   1.165 +	TIpcArgs args;
   1.166 +
   1.167 +	for(TInt i = 0; i < aArgCount;i++)
   1.168 +		{
   1.169 +		args.Set(i,&ptr);
   1.170 +		}
   1.171 +
   1.172 +	TInt ret;
   1.173 +
   1.174 +	if(IsFunctionAsynchronous(aMsg))
   1.175 +		{
   1.176 +		ret = Send(aMsg, args);
   1.177 +		User::After(KAsynchDelay);
   1.178 +		}
   1.179 +	else
   1.180 +		{
   1.181 +		ret = SendReceive(aMsg, args);
   1.182 +		}
   1.183 +
   1.184 +	CleanupStack::PopAndDestroy(buf);
   1.185 +	return ret;
   1.186 +	}
   1.187 +
   1.188 +void RIpcFuzzTest::RunTestL(const TDesC& aTargetSrvName,
   1.189 +							TInt aFunc, TInt aTestType, TInt aArgCount)
   1.190 +	{
   1.191 +		TVersion version(0,0,0);
   1.192 +
   1.193 +		User::LeaveIfError(CreateSession(aTargetSrvName, version, 200));
   1.194 +
   1.195 +		switch(aTestType)
   1.196 +			{
   1.197 +			case 0:
   1.198 +				Fuzz(aFunc,aArgCount);
   1.199 +				break;
   1.200 +
   1.201 +			case 1:
   1.202 +				Fuzz8L(aFunc,aArgCount);
   1.203 +				break;
   1.204 +
   1.205 +			case 2:
   1.206 +				FuzzL(aFunc,aArgCount);
   1.207 +				break;
   1.208 +			}
   1.209 +	}
   1.210 +
   1.211 +
   1.212 +TInt KillProcess(const TDesC& aProcessName)
   1.213 +	{
   1.214 +	TFullName name;
   1.215 +
   1.216 +	RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
   1.217 +
   1.218 +	TBuf<64> pattern(aProcessName);
   1.219 +	TInt length = pattern.Length();
   1.220 +	pattern += _L("*");
   1.221 +	TFindProcess procFinder(pattern);
   1.222 +
   1.223 +	while (procFinder.Next(name) == KErrNone)
   1.224 +		{
   1.225 +		if (name.Length() > length)
   1.226 +			{//If found name is a string containing aProcessName string.
   1.227 +			TChar c(name[length]);
   1.228 +			if (c.IsAlphaDigit() ||
   1.229 +				c == TChar('_') ||
   1.230 +				c == TChar('-'))
   1.231 +				{
   1.232 +				// If the found name is other valid application name
   1.233 +				// starting with aProcessName string.
   1.234 +				RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
   1.235 +				continue;
   1.236 +				}
   1.237 +			}
   1.238 +		RProcess proc;
   1.239 +		if (proc.Open(name) == KErrNone)
   1.240 +			{
   1.241 +			proc.Kill(0);
   1.242 +			RDebug::Print(_L("\"%S\" process killed.\n"), &name);
   1.243 +			}
   1.244 +		proc.Close();
   1.245 +		}
   1.246 +	return KErrNone;
   1.247 +	}
   1.248 +
   1.249 +
   1.250 +TInt FuzzServerL(TAny* aTestInfo)
   1.251 +	{
   1.252 +   	CTrapCleanup* cleanup=CTrapCleanup::New();
   1.253 +   	TInt err=KErrNoMemory;
   1.254 +   	if (cleanup)
   1.255 +   		{
   1.256 +
   1.257 +		TTestInfo* info = (TTestInfo*)aTestInfo;
   1.258 +		RIpcFuzzTest fuzzer;
   1.259 +
   1.260 +		TRAP(err,fuzzer.RunTestL(KServerName,info->iFunction
   1.261 +				,info->iType, info->iArgCount));
   1.262 +
   1.263 +		fuzzer.Close();
   1.264 +
   1.265 +   		delete cleanup;
   1.266 +   		}
   1.267 +   	return err;
   1.268 +	}
   1.269 +
   1.270 +void TestServerApi(TInt aFunctionNumber,
   1.271 +			TInt aTestType,TInt aArgCount, TExitDetails& aExitDetails)
   1.272 +	{
   1.273 +
   1.274 +    TTestInfo testInfo;
   1.275 +	testInfo.iFunction = aFunctionNumber;
   1.276 +	testInfo.iType = aTestType;
   1.277 +	testInfo.iArgCount = aArgCount;
   1.278 +
   1.279 +    RThread thread;
   1.280 +    _LIT(KThreadName,"FuzzerThread" );
   1.281 +	thread.Create(KThreadName,&FuzzServerL, KDefaultStackSize, NULL,&testInfo);
   1.282 +
   1.283 +	TRequestStatus threadStat;
   1.284 +	thread.Logon(threadStat);
   1.285 +
   1.286 +	TBool jit = User::JustInTime();
   1.287 +	User::SetJustInTime(EFalse);
   1.288 +
   1.289 +	thread.Resume();
   1.290 +
   1.291 +	User::WaitForRequest(threadStat);
   1.292 +
   1.293 +	User::SetJustInTime(jit);
   1.294 +
   1.295 +	aExitDetails.iCategory = thread.ExitCategory();
   1.296 +	aExitDetails.iReason = thread.ExitReason();
   1.297 +	aExitDetails.iExitType = thread.ExitType();
   1.298 +
   1.299 +	thread.Close();
   1.300 +
   1.301 +	}
   1.302 +
   1.303 +
   1.304 +TInt LaunchServer(RProcess& aServer)
   1.305 +	{
   1.306 +
   1.307 +	TheTest.Printf(_L("Launching BackupServer...\n"));
   1.308 +
   1.309 +	TInt err = aServer.Create(KServerLauncherProcess, _L(""));
   1.310 +
   1.311 +	 if(err == KErrNone)
   1.312 +		 {
   1.313 +		 //Start server and wait until it is running
   1.314 +		 TRequestStatus serverStat;
   1.315 +		 aServer.SetJustInTime(false);
   1.316 +		 aServer.Resume();
   1.317 +
   1.318 +		 aServer.Rendezvous(serverStat);
   1.319 +		 User::WaitForRequest(serverStat);
   1.320 +		 }
   1.321 +
   1.322 +	 return err;
   1.323 +
   1.324 +	}
   1.325 +
   1.326 +void PrintTestMessage(TInt iFunc, TInt iType, TInt iArgCount)
   1.327 +	{
   1.328 +	switch(iType)
   1.329 +		{
   1.330 +		case 0:
   1.331 +			TheTest.Printf(_L("\nFuzz Test on function number %d using random Int data. Number of Args = %d\n"), iFunc, iArgCount);
   1.332 +			break;
   1.333 +
   1.334 +		case 1:
   1.335 +			TheTest.Printf(_L("\nFuzz Test on function number %d using random Des8 data. Number of Args = %d\n"), iFunc, iArgCount);
   1.336 +			break;
   1.337 +
   1.338 +		case 2:
   1.339 +			TheTest.Printf(_L("\nFuzz Test on function number %d using random Des data. Number of Args = %d\n"), iFunc, iArgCount);
   1.340 +			break;
   1.341 +
   1.342 +		}
   1.343 +
   1.344 +	}
   1.345 +
   1.346 +/**
   1.347 +Invoke the tests
   1.348 +*/
   1.349 +/**
   1.350 +@SYMTestCaseID          SYSLIB-BAFL-CT-4039
   1.351 +@SYMTestCaseDesc	    Tests Bafl Backup Server APIs for IPC Robustness
   1.352 +@SYMTestPriority 	    High
   1.353 +@SYMTestActions  	    The function calls each of the Bafl Backup Server APIs through a
   1.354 +						custom session object passing random TInt, Des8 and Des16 data .
   1.355 +@SYMTestExpectedResults The server should be robust to all malformed messages and should not
   1.356 +						hang or panic.
   1.357 +@SYMDEF                	INC113760
   1.358 +*/
   1.359 +LOCAL_C void DoTestsL ()
   1.360 +    {
   1.361 +
   1.362 +    RProcess server;
   1.363 +    const TInt KMinFuncNumber = 20;
   1.364 +    const TInt KMaxFuncNumber = 35;
   1.365 +
   1.366 +    TInt err = LaunchServer(server);
   1.367 +    TheTest(err == 0);
   1.368 +
   1.369 +    TExitDetails exitDetails;
   1.370 +
   1.371 +    for(TInt i = KMinFuncNumber;i<= KMaxFuncNumber;i++)
   1.372 +    	{
   1.373 +
   1.374 +    	//Carry out each type of test
   1.375 +    	for(TInt testType = 0; testType < 3;testType++)
   1.376 +    		{
   1.377 +       		//Carry out each test with number of arguments 1 - 4
   1.378 +    		for(TInt argCount = 1;argCount <= 4;argCount++)
   1.379 +    			{
   1.380 +    			PrintTestMessage(i, testType, argCount);
   1.381 +
   1.382 +    			TestServerApi(i, testType, argCount, exitDetails);
   1.383 +		    	//Kill the server process and verify that it was still running
   1.384 +		    	//If the server was already dead it would return the reason it exited
   1.385 +	    		if(server.ExitType() != EExitPending)
   1.386 +	    			{
   1.387 +	    			server.Kill(0);
   1.388 +	    			TInt exitReason = server.ExitReason();
   1.389 +	    			server.Close();
   1.390 +	    			TheTest(exitReason == 0);
   1.391 +	    			User::LeaveIfError(LaunchServer(server));
   1.392 +	    			}
   1.393 +    			}
   1.394 +
   1.395 +	    	TheTest.Printf(_L("\nFuzz Test Successful\n"));
   1.396 +    		}
   1.397 +    	}
   1.398 +    }
   1.399 +
   1.400 +void SetupTestL()
   1.401 +	{
   1.402 +	TheScheduler = new (ELeave) CActiveScheduler;
   1.403 +   	CActiveScheduler::Install (TheScheduler);
   1.404 +
   1.405 +	}
   1.406 +
   1.407 +
   1.408 +GLDEF_C TInt E32Main ()
   1.409 +	{
   1.410 +
   1.411 +	TheTest.Printf (_L ("\n"));
   1.412 +	TheTest.Title ();
   1.413 +	TheTest.Start (_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-4039 IPC Fuzz Tests "));
   1.414 +
   1.415 +   	CTrapCleanup* cleanup=CTrapCleanup::New();
   1.416 +
   1.417 +   	TInt err=KErrNoMemory;
   1.418 +   	if (cleanup)
   1.419 +   		{
   1.420 +   		// Construct and install the active scheduler
   1.421 +   		TRAP(err, SetupTestL());
   1.422 +
   1.423 +   		//Kill baksrvs which may have already been launched by another test
   1.424 +   		KillProcess(KServerStartProcess);
   1.425 +   		KillProcess(KServerLauncherProcess);
   1.426 +
   1.427 +   		TRAP (err, DoTestsL ());
   1.428 +   		TheTest (err == KErrNone);
   1.429 +
   1.430 +   		TheTest.End ();
   1.431 +   		TheTest.Close ();
   1.432 +
   1.433 +   		delete TheScheduler;
   1.434 +   		delete cleanup;
   1.435 +
   1.436 +   		}
   1.437 +	return err;
   1.438 +	}