lowlevellibsandfws/apputils/tsrc/T_BackupServerIPC.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include <e32test.h>
       
    18 #include <e32math.h>
       
    19 #include <e32debug.h>
       
    20 
       
    21 #include "backup_std.h"
       
    22 
       
    23 LOCAL_D RTest				TheTest (_L("T_BackupServerIPC"));
       
    24 LOCAL_D CActiveScheduler*	TheScheduler;
       
    25 _LIT(KServerLauncherProcess, "T_BackupServerLauncher");
       
    26 _LIT(KServerStartProcess, "baksrvs");
       
    27 
       
    28 _LIT(KServerName, "!BackupServer");
       
    29 
       
    30 struct TTestInfo
       
    31 	{
       
    32 	TInt iFunction;
       
    33 	TInt iType;
       
    34 	TInt iArgCount;
       
    35 	};
       
    36 
       
    37 struct TExitDetails
       
    38 	{
       
    39 	TExitCategoryName iCategory;
       
    40 	TExitType iExitType;
       
    41 	TInt iReason;
       
    42 	};
       
    43 
       
    44 const TInt KAsynchDelay = 500000;
       
    45 
       
    46 //===============================================================================
       
    47 
       
    48 TBool IsFunctionAsynchronous(TInt aFunc)
       
    49 	{
       
    50 	TBool asynch = EFalse;
       
    51 	switch(aFunc)
       
    52 		{
       
    53 		case EBakOpCodeEventReady:
       
    54 		case EBakOpCodeCloseAllFiles:
       
    55 		case EBakOpCodeCloseServer:
       
    56 		case EBakOpCodeBackupOperationEventReady:
       
    57 			asynch = ETrue;
       
    58 			break;
       
    59 
       
    60 		default:
       
    61 			break;
       
    62 		}
       
    63 	return asynch;
       
    64 	}
       
    65 
       
    66 class RIpcFuzzTest : public RSessionBase
       
    67 {
       
    68 public: // Constructors and destructor
       
    69 
       
    70 	/**
       
    71 		* Constructor for performing 1st stage construction
       
    72 		*/
       
    73 	RIpcFuzzTest();
       
    74 
       
    75 	/**
       
    76 		* Destructor.
       
    77 		*/
       
    78 	~RIpcFuzzTest();
       
    79 
       
    80 	/**
       
    81 		* Performs test steps
       
    82 		*/
       
    83 
       
    84 	void RunTestL(const TDesC& aTargetSrvName, TInt aFunc,
       
    85 				TInt aTestType, TInt aArgCount);
       
    86 
       
    87 private:
       
    88 	TInt Fuzz(TInt aMsg, TInt aArgCount);
       
    89 	TInt FuzzL(TInt aMsg, TInt aArgCount);
       
    90 	TInt Fuzz8L(TInt aMsg, TInt aArgCount);
       
    91 };
       
    92 
       
    93 RIpcFuzzTest::RIpcFuzzTest()
       
    94 	{
       
    95 	// No implementation required
       
    96 	}
       
    97 
       
    98 
       
    99 RIpcFuzzTest::~RIpcFuzzTest()
       
   100 	{
       
   101 	Close();
       
   102 	}
       
   103 
       
   104 TInt RIpcFuzzTest::Fuzz(TInt aMsg, TInt aArgCount)
       
   105 	{
       
   106 	TIpcArgs args;
       
   107 
       
   108 	for(TInt i = 0; i < aArgCount;i++)
       
   109 		{
       
   110 		args.Set(i,Math::Random());
       
   111 		}
       
   112 
       
   113 	TInt ret;
       
   114 
       
   115 	if(IsFunctionAsynchronous(aMsg))
       
   116 		{
       
   117 		ret = Send(aMsg, args);
       
   118 		User::After(KAsynchDelay);
       
   119 		}
       
   120 	else
       
   121 		{
       
   122 		ret = SendReceive(aMsg, args);
       
   123 		}
       
   124 	return ret;
       
   125 	}
       
   126 
       
   127 TInt RIpcFuzzTest::Fuzz8L(TInt aMsg, TInt aArgCount)
       
   128 	{
       
   129 	HBufC8* buf = HBufC8::NewLC(255);
       
   130 	TPtr8 ptr = buf->Des();
       
   131 	ptr.Fill(Math::Random(),255);
       
   132 
       
   133 	TIpcArgs args;
       
   134 
       
   135 	for(TInt i = 0; i < aArgCount;i++)
       
   136 		{
       
   137 		args.Set(i,&ptr);
       
   138 		}
       
   139 
       
   140 	TInt ret;
       
   141 
       
   142 	if(IsFunctionAsynchronous(aMsg))
       
   143 		{
       
   144 		ret = Send(aMsg, args);
       
   145 		User::After(KAsynchDelay);
       
   146 		}
       
   147 	else
       
   148 		{
       
   149 		ret = SendReceive(aMsg, args);
       
   150 		}
       
   151 
       
   152 	CleanupStack::PopAndDestroy(buf);
       
   153 	return ret;
       
   154 	}
       
   155 
       
   156 TInt RIpcFuzzTest::FuzzL(TInt aMsg, TInt aArgCount)
       
   157 	{
       
   158 	HBufC* buf = HBufC::NewLC(255);
       
   159 	TPtr ptr = buf->Des();
       
   160 	ptr.Fill(Math::Random(),255);
       
   161 
       
   162 	TIpcArgs args;
       
   163 
       
   164 	for(TInt i = 0; i < aArgCount;i++)
       
   165 		{
       
   166 		args.Set(i,&ptr);
       
   167 		}
       
   168 
       
   169 	TInt ret;
       
   170 
       
   171 	if(IsFunctionAsynchronous(aMsg))
       
   172 		{
       
   173 		ret = Send(aMsg, args);
       
   174 		User::After(KAsynchDelay);
       
   175 		}
       
   176 	else
       
   177 		{
       
   178 		ret = SendReceive(aMsg, args);
       
   179 		}
       
   180 
       
   181 	CleanupStack::PopAndDestroy(buf);
       
   182 	return ret;
       
   183 	}
       
   184 
       
   185 void RIpcFuzzTest::RunTestL(const TDesC& aTargetSrvName,
       
   186 							TInt aFunc, TInt aTestType, TInt aArgCount)
       
   187 	{
       
   188 		TVersion version(0,0,0);
       
   189 
       
   190 		User::LeaveIfError(CreateSession(aTargetSrvName, version, 200));
       
   191 
       
   192 		switch(aTestType)
       
   193 			{
       
   194 			case 0:
       
   195 				Fuzz(aFunc,aArgCount);
       
   196 				break;
       
   197 
       
   198 			case 1:
       
   199 				Fuzz8L(aFunc,aArgCount);
       
   200 				break;
       
   201 
       
   202 			case 2:
       
   203 				FuzzL(aFunc,aArgCount);
       
   204 				break;
       
   205 			}
       
   206 	}
       
   207 
       
   208 
       
   209 TInt KillProcess(const TDesC& aProcessName)
       
   210 	{
       
   211 	TFullName name;
       
   212 
       
   213 	RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
       
   214 
       
   215 	TBuf<64> pattern(aProcessName);
       
   216 	TInt length = pattern.Length();
       
   217 	pattern += _L("*");
       
   218 	TFindProcess procFinder(pattern);
       
   219 
       
   220 	while (procFinder.Next(name) == KErrNone)
       
   221 		{
       
   222 		if (name.Length() > length)
       
   223 			{//If found name is a string containing aProcessName string.
       
   224 			TChar c(name[length]);
       
   225 			if (c.IsAlphaDigit() ||
       
   226 				c == TChar('_') ||
       
   227 				c == TChar('-'))
       
   228 				{
       
   229 				// If the found name is other valid application name
       
   230 				// starting with aProcessName string.
       
   231 				RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
       
   232 				continue;
       
   233 				}
       
   234 			}
       
   235 		RProcess proc;
       
   236 		if (proc.Open(name) == KErrNone)
       
   237 			{
       
   238 			proc.Kill(0);
       
   239 			RDebug::Print(_L("\"%S\" process killed.\n"), &name);
       
   240 			}
       
   241 		proc.Close();
       
   242 		}
       
   243 	return KErrNone;
       
   244 	}
       
   245 
       
   246 
       
   247 TInt FuzzServerL(TAny* aTestInfo)
       
   248 	{
       
   249    	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   250    	TInt err=KErrNoMemory;
       
   251    	if (cleanup)
       
   252    		{
       
   253 
       
   254 		TTestInfo* info = (TTestInfo*)aTestInfo;
       
   255 		RIpcFuzzTest fuzzer;
       
   256 
       
   257 		TRAP(err,fuzzer.RunTestL(KServerName,info->iFunction
       
   258 				,info->iType, info->iArgCount));
       
   259 
       
   260 		fuzzer.Close();
       
   261 
       
   262    		delete cleanup;
       
   263    		}
       
   264    	return err;
       
   265 	}
       
   266 
       
   267 void TestServerApi(TInt aFunctionNumber,
       
   268 			TInt aTestType,TInt aArgCount, TExitDetails& aExitDetails)
       
   269 	{
       
   270 
       
   271     TTestInfo testInfo;
       
   272 	testInfo.iFunction = aFunctionNumber;
       
   273 	testInfo.iType = aTestType;
       
   274 	testInfo.iArgCount = aArgCount;
       
   275 
       
   276     RThread thread;
       
   277     _LIT(KThreadName,"FuzzerThread" );
       
   278 	thread.Create(KThreadName,&FuzzServerL, KDefaultStackSize, NULL,&testInfo);
       
   279 
       
   280 	TRequestStatus threadStat;
       
   281 	thread.Logon(threadStat);
       
   282 
       
   283 	TBool jit = User::JustInTime();
       
   284 	User::SetJustInTime(EFalse);
       
   285 
       
   286 	thread.Resume();
       
   287 
       
   288 	User::WaitForRequest(threadStat);
       
   289 
       
   290 	User::SetJustInTime(jit);
       
   291 
       
   292 	aExitDetails.iCategory = thread.ExitCategory();
       
   293 	aExitDetails.iReason = thread.ExitReason();
       
   294 	aExitDetails.iExitType = thread.ExitType();
       
   295 
       
   296 	thread.Close();
       
   297 
       
   298 	}
       
   299 
       
   300 
       
   301 TInt LaunchServer(RProcess& aServer)
       
   302 	{
       
   303 
       
   304 	TheTest.Printf(_L("Launching BackupServer...\n"));
       
   305 
       
   306 	TInt err = aServer.Create(KServerLauncherProcess, _L(""));
       
   307 
       
   308 	 if(err == KErrNone)
       
   309 		 {
       
   310 		 //Start server and wait until it is running
       
   311 		 TRequestStatus serverStat;
       
   312 		 aServer.SetJustInTime(false);
       
   313 		 aServer.Resume();
       
   314 
       
   315 		 aServer.Rendezvous(serverStat);
       
   316 		 User::WaitForRequest(serverStat);
       
   317 		 }
       
   318 
       
   319 	 return err;
       
   320 
       
   321 	}
       
   322 
       
   323 void PrintTestMessage(TInt iFunc, TInt iType, TInt iArgCount)
       
   324 	{
       
   325 	switch(iType)
       
   326 		{
       
   327 		case 0:
       
   328 			TheTest.Printf(_L("\nFuzz Test on function number %d using random Int data. Number of Args = %d\n"), iFunc, iArgCount);
       
   329 			break;
       
   330 
       
   331 		case 1:
       
   332 			TheTest.Printf(_L("\nFuzz Test on function number %d using random Des8 data. Number of Args = %d\n"), iFunc, iArgCount);
       
   333 			break;
       
   334 
       
   335 		case 2:
       
   336 			TheTest.Printf(_L("\nFuzz Test on function number %d using random Des data. Number of Args = %d\n"), iFunc, iArgCount);
       
   337 			break;
       
   338 
       
   339 		}
       
   340 
       
   341 	}
       
   342 
       
   343 /**
       
   344 Invoke the tests
       
   345 */
       
   346 /**
       
   347 @SYMTestCaseID          SYSLIB-BAFL-CT-4039
       
   348 @SYMTestCaseDesc	    Tests Bafl Backup Server APIs for IPC Robustness
       
   349 @SYMTestPriority 	    High
       
   350 @SYMTestActions  	    The function calls each of the Bafl Backup Server APIs through a
       
   351 						custom session object passing random TInt, Des8 and Des16 data .
       
   352 @SYMTestExpectedResults The server should be robust to all malformed messages and should not
       
   353 						hang or panic.
       
   354 @SYMDEF                	INC113760
       
   355 */
       
   356 LOCAL_C void DoTestsL ()
       
   357     {
       
   358 
       
   359     RProcess server;
       
   360     const TInt KMinFuncNumber = 20;
       
   361     const TInt KMaxFuncNumber = 35;
       
   362 
       
   363     TInt err = LaunchServer(server);
       
   364     TheTest(err == 0);
       
   365 
       
   366     TExitDetails exitDetails;
       
   367 
       
   368     for(TInt i = KMinFuncNumber;i<= KMaxFuncNumber;i++)
       
   369     	{
       
   370 
       
   371     	//Carry out each type of test
       
   372     	for(TInt testType = 0; testType < 3;testType++)
       
   373     		{
       
   374        		//Carry out each test with number of arguments 1 - 4
       
   375     		for(TInt argCount = 1;argCount <= 4;argCount++)
       
   376     			{
       
   377     			PrintTestMessage(i, testType, argCount);
       
   378 
       
   379     			TestServerApi(i, testType, argCount, exitDetails);
       
   380 		    	//Kill the server process and verify that it was still running
       
   381 		    	//If the server was already dead it would return the reason it exited
       
   382 	    		if(server.ExitType() != EExitPending)
       
   383 	    			{
       
   384 	    			server.Kill(0);
       
   385 	    			TInt exitReason = server.ExitReason();
       
   386 	    			server.Close();
       
   387 	    			TheTest(exitReason == 0);
       
   388 	    			User::LeaveIfError(LaunchServer(server));
       
   389 	    			}
       
   390     			}
       
   391 
       
   392 	    	TheTest.Printf(_L("\nFuzz Test Successful\n"));
       
   393     		}
       
   394     	}
       
   395     }
       
   396 
       
   397 void SetupTestL()
       
   398 	{
       
   399 	TheScheduler = new (ELeave) CActiveScheduler;
       
   400    	CActiveScheduler::Install (TheScheduler);
       
   401 
       
   402 	}
       
   403 
       
   404 
       
   405 GLDEF_C TInt E32Main ()
       
   406 	{
       
   407 
       
   408 	TheTest.Printf (_L ("\n"));
       
   409 	TheTest.Title ();
       
   410 	TheTest.Start (_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-4039 IPC Fuzz Tests "));
       
   411 
       
   412    	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   413 
       
   414    	TInt err=KErrNoMemory;
       
   415    	if (cleanup)
       
   416    		{
       
   417    		// Construct and install the active scheduler
       
   418    		TRAP(err, SetupTestL());
       
   419 
       
   420    		//Kill baksrvs which may have already been launched by another test
       
   421    		KillProcess(KServerStartProcess);
       
   422    		KillProcess(KServerLauncherProcess);
       
   423 
       
   424    		TRAP (err, DoTestsL ());
       
   425    		TheTest (err == KErrNone);
       
   426 
       
   427    		TheTest.End ();
       
   428    		TheTest.Close ();
       
   429 
       
   430    		delete TheScheduler;
       
   431    		delete cleanup;
       
   432 
       
   433    		}
       
   434 	return err;
       
   435 	}