sysstatemgmt/systemstarter/test/tstartsafe/src/tstartsafe_procstartmon.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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  @file
       
    18  @test
       
    19  @internalComponent - Internal Symbian test code
       
    20 */
       
    21 
       
    22 #include <startupproperties.h>
       
    23 
       
    24 #include "startsafe.h"
       
    25 #include "tstartsafe_procstartmon.h"
       
    26 #include "testapps.h"
       
    27 #include "testprocgoodsession.h"
       
    28 
       
    29 
       
    30 const TInt KStartSafeTestKillCode = 666;
       
    31 const TInt KStartSafeTestFinalKillCode = 667;
       
    32 const TInt KThrottleTime = 15000000; //15s
       
    33 const TInt KPoliteInterval = 1000000; //1s
       
    34 
       
    35 
       
    36 /**
       
    37 Old Test CaseID 		APPFWK-STARTSAFE-0011
       
    38 New Test CaseID 		DEVSRVS-SYSSTART-STARTSAFE-0011
       
    39  */
       
    40 
       
    41 TVerdict CAppFwkStartSafeTestStepProcStartMon::doTestStepL( void )
       
    42 	{
       
    43 	
       
    44 	CStartSafe* startSafe = CStartSafe::NewL();	
       
    45 	CleanupStack::PushL( startSafe );
       
    46 	
       
    47 	CStartupProperties* prop = CStartupProperties::NewL();
       
    48 	CleanupStack::PushL( prop );
       
    49 
       
    50 	RProcess proc;	
       
    51 	CleanupClosePushL( proc );
       
    52 	
       
    53 	// KLaunchServerCommandLineOption makes the process listen for remote control connections
       
    54 	prop->SetFileParamsL( KTestProcGood, KLaunchServerCommandLineOption ); 
       
    55 	prop->SetStartupType( EStartProcess );
       
    56 	prop->SetStartMethod( EWaitForStart ); // EFireAndForget is disallowed
       
    57 	prop->SetMonitored( ETrue ); // the process death will be monitored
       
    58 	prop->SetNoOfRetries( 1 );   // Must be non-zero in order to invoke a restart
       
    59 
       
    60 
       
    61 		
       
    62 	INFO_PRINTF1( _L("Starting the Test-process") );
       
    63 		
       
    64 	TInt tried = 0;	
       
    65 		
       
    66 	TRAPD( err, startSafe->StartAndMonitorL( *prop, proc, tried) );
       
    67 	User::After( KPoliteInterval );
       
    68 
       
    69 	// Produce the process-name search-term
       
    70 	TPtrC namePtr( KTestProcGood() );
       
    71 	namePtr.Set( namePtr.Ptr(), namePtr.Find(_L(".")) );
       
    72 
       
    73 
       
    74 	// See if StartSafe thinks it's been successful _and that the process can be found. (if, line 73)
       
    75 	// Then check to see that it has been restarted after being Kill()ed (if, line 85)
       
    76 	if( (KErrNone == err) && ProcessExists(namePtr) )
       
    77 		{
       
    78 		
       
    79 		INFO_PRINTF2( _L("Process \"%S\" started"), &namePtr );
       
    80 		
       
    81 			
       
    82 		proc.Kill( KStartSafeTestKillCode );
       
    83 		proc.Close();
       
    84 		// Process should be restarted here
       
    85 		User::After( KThrottleTime );
       
    86 		
       
    87 		
       
    88 		if( ProcessExists( namePtr, proc ) )
       
    89 			{
       
    90 			INFO_PRINTF2( _L("Process \"%S\" REstarted successfully"), &namePtr );
       
    91 			
       
    92 			// RTestProcGoodSession is used to remotely control the process, allowing this process to cancel the monitoring of the target process, 
       
    93 			// otherwise it keeps being restarted
       
    94 			RTestProcGoodSession testProc;
       
    95 			testProc.Connect();
       
    96 			testProc.CancelMonitor();
       
    97 			testProc.Close();
       
    98 			
       
    99 			proc.Kill( KStartSafeTestFinalKillCode );
       
   100 		
       
   101 			SetTestStepResult( EPass );	
       
   102 			}
       
   103 		else
       
   104 			{
       
   105 			ERR_PRINTF1( _L("Process was _not restarted") );
       
   106 			
       
   107 			SetTestStepResult( EFail );	
       
   108 			}
       
   109 			
       
   110 		}
       
   111 	else 
       
   112 		{
       
   113 		ERR_PRINTF2( _L("StartAndMonitorL left with"), err );	
       
   114 		
       
   115 		SetTestStepResult( EFail );
       
   116 		}
       
   117 		
       
   118 		
       
   119 	CleanupStack::PopAndDestroy( 3, startSafe ); // proc, prop, startSafe
       
   120 	
       
   121 	return TestStepResult();	
       
   122 	}
       
   123 
       
   124 TBool CAppFwkStartSafeTestStepProcStartMon::ProcessExists(const TDesC& aProcessName)
       
   125 	{
       
   126 	RProcess process;
       
   127 	TBool result = EFalse;
       
   128 	result = ProcessExists(aProcessName, process);
       
   129 	process.Close(); //Closing a closed process handle, which would be the case if the process we wanted can't be found, is not an error, so no need to check if open
       
   130 	return result;
       
   131 	}
       
   132 	
       
   133 TBool CAppFwkStartSafeTestStepProcStartMon::ProcessExists(const TDesC& aProcessName, RProcess& aProcess)
       
   134 	{
       
   135 	TBool found = EFalse;
       
   136 	TFullName searchTerm(aProcessName);
       
   137 	searchTerm += _L("*");
       
   138 	TFindProcess find(searchTerm);
       
   139 	TFullName name;
       
   140 	while(find.Next(name)==KErrNone)
       
   141 		{
       
   142 		const TInt err = aProcess.Open(find);
       
   143 		if (err == KErrNone)
       
   144 			{
       
   145 			if (aProcess.ExitType() == EExitPending)
       
   146 				{
       
   147 				found = ETrue;
       
   148 				break; //We have found the process we want, and we want to pass back an active process handle. Breaks to line 150 (return).
       
   149 				}
       
   150 			aProcess.Close(); //The process isn't the one we want, so close the handle.
       
   151 			}
       
   152 		}
       
   153 	return found;
       
   154 	}
       
   155 
       
   156 
       
   157 CAppFwkStartSafeTestStepProcStartMon::CAppFwkStartSafeTestStepProcStartMon()
       
   158 	{
       
   159 	SetTestStepName(KCTestCaseProcStartMon);
       
   160 	}