installationservices/swi/test/tintegrityservices/tintegrityservicesstep.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-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 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file 
       
    21  @test
       
    22  @internalTechnology
       
    23 */
       
    24  
       
    25 #include "tintegrityservicesstep.h"
       
    26 #include "dummyintegrityservices.h"
       
    27 #include <test/testexecutelog.h>
       
    28 #include <s32file.h>
       
    29 #include "testutilclientswi.h"
       
    30 
       
    31 
       
    32 /////////////////////////////////////////////////////////////////////
       
    33 // Tags used by test steps
       
    34 /////////////////////////////////////////////////////////////////////
       
    35 
       
    36 _LIT(KRemoveFile, "removefile");
       
    37 _LIT(KTempFile, "temporaryfile");
       
    38 _LIT(KAddFile, "addfile");
       
    39 _LIT(KFailType, "failtype");
       
    40 _LIT(KFailPosition, "failposition");
       
    41 _LIT(KFailFileName, "failfilename");
       
    42 _LIT(KPresent, "present");
       
    43 _LIT(KAbsent, "absent");
       
    44 _LIT(KCleanupFile, "cleanupfile");
       
    45 _LIT(KCleanupDirectory, "cleanupdirectory");
       
    46 _LIT(KArbitraryOpFile, "arbitraryopfile");
       
    47 _LIT(KOperation, "operation");
       
    48 
       
    49 using namespace Swi;
       
    50 using namespace Swi::Test;
       
    51 
       
    52 /////////////////////////////////////////////////////////////////////
       
    53 // CIntegrityServicesStep
       
    54 /////////////////////////////////////////////////////////////////////
       
    55 TVerdict CIntegrityServicesStepBase::doTestStepPreambleL()
       
    56 	{
       
    57 	__UHEAP_MARK;
       
    58 
       
    59 	// Install an active scheduler
       
    60 	CActiveScheduler* s = new (ELeave) CActiveScheduler;
       
    61 	s->Install(s);
       
    62 	
       
    63 	User::LeaveIfError(iIntegritySession.Connect());
       
    64 
       
    65 	ReadFailureSettings();
       
    66 	return TestStepResult();
       
    67 	}
       
    68 
       
    69 TVerdict CIntegrityServicesStepBase::doTestStepPostambleL()
       
    70 	{
       
    71 	// Remove the installed active scheduler
       
    72 	CActiveScheduler* s = CActiveScheduler::Current();
       
    73 	s->Install(NULL);
       
    74 	delete s;
       
    75 
       
    76 	iIntegritySession.Close();
       
    77 
       
    78 	__UHEAP_MARKEND;
       
    79 
       
    80 	return TestStepResult();
       
    81 	}
       
    82 
       
    83 void CIntegrityServicesStepBase::GetStringArrayFromConfigL(const TDesC& aSectName, const TDesC& aKeyName, RArray<TPtrC>& aArray)
       
    84 	{
       
    85 	_LIT(KKeyFormat, "-%02d");
       
    86 	HBufC* buf = HBufC::NewLC(aKeyName.Length() + KKeyFormat().Length());
       
    87 	TPtr ptr(buf->Des());
       
    88 	INFO_PRINTF2(_L("Parsing attribute: %S"), &aKeyName);
       
    89 
       
    90 	TInt i = 0;
       
    91 	TBool cont = ETrue;
       
    92 	do
       
    93 		{
       
    94 		++i;
       
    95 		ptr = aKeyName;
       
    96 		ptr.AppendFormat(KKeyFormat(), i);
       
    97 		TPtrC val;
       
    98 
       
    99 		cont = GetStringFromConfig(aSectName, ptr, val);
       
   100 		if (cont)
       
   101 			{
       
   102 			User::LeaveIfError(aArray.Append(val));
       
   103 			}
       
   104 		} while (cont);
       
   105 
       
   106 	INFO_PRINTF2(_L("Element count: %d"), i-1);
       
   107 	CleanupStack::PopAndDestroy(buf);
       
   108 	}
       
   109 
       
   110 TBool CIntegrityServicesStepBase::CheckFilesL()
       
   111 	{
       
   112 	TInt result = ETrue;
       
   113 	RTestUtilSessionSwi testutil;
       
   114 	User::LeaveIfError(testutil.Connect());
       
   115 	CleanupClosePushL(testutil);
       
   116 	RArray<TPtrC> fileArray;
       
   117 	CleanupClosePushL(fileArray);
       
   118 	
       
   119 	GetStringArrayFromConfigL(ConfigSection(), KPresent, fileArray);
       
   120 	for (TInt file = 0; file < fileArray.Count();file++)
       
   121 		{
       
   122 		if(testutil.FileExistsL(fileArray[file]))
       
   123 			{
       
   124 			INFO_PRINTF2(_L("%S was found"), &fileArray[file]);
       
   125 			}
       
   126 		else
       
   127 			{
       
   128 			ERR_PRINTF2(_L("%S was not found"), &fileArray[file]);
       
   129 			result = EFalse;
       
   130 			}
       
   131 		}
       
   132 	fileArray.Reset();
       
   133 	
       
   134 	GetStringArrayFromConfigL(ConfigSection(), KAbsent, fileArray);
       
   135 	for (TInt file = 0; file < fileArray.Count();file++)
       
   136 		{
       
   137 		if(testutil.FileExistsL(fileArray[file]))
       
   138 			{
       
   139 			ERR_PRINTF2(_L("%S was found"), &fileArray[file]);
       
   140 			result = EFalse;
       
   141 			}
       
   142 		else
       
   143 			{
       
   144 			INFO_PRINTF2(_L("%S was not found"), &fileArray[file]);
       
   145 			}
       
   146 		}
       
   147 	fileArray.Reset();
       
   148 	CleanupStack::PopAndDestroy(&fileArray);
       
   149 	
       
   150 	CleanupStack::PopAndDestroy(&testutil);
       
   151 	return result;
       
   152 	}
       
   153 
       
   154 void CIntegrityServicesStepBase::ReadFailureSettings()
       
   155 	{		
       
   156 	TPtrC failType;
       
   157 	if(!GetStringFromConfig(ConfigSection(), KFailType, failType))
       
   158 		{
       
   159 		failType.Set(KNullDesC);
       
   160 		}
       
   161 		
       
   162 	TPtrC failPosition;
       
   163 	if(!GetStringFromConfig(ConfigSection(), KFailPosition, failPosition))
       
   164 		{
       
   165 		failPosition.Set(KNullDesC);
       
   166 		}
       
   167 		
       
   168 	TPtrC failFileName;
       
   169 	if(!GetStringFromConfig(ConfigSection(), KFailFileName, failFileName))
       
   170 		{
       
   171 		failFileName.Set(KNullDesC);
       
   172 		}
       
   173 	
       
   174 	iIntegritySession.SetSimulatedFailure(failType, failPosition, failFileName);
       
   175 	}
       
   176 	
       
   177 void CIntegrityServicesStepBase::doInstallL()
       
   178 	{
       
   179 	RArray<TPtrC> fileArray;
       
   180 	CleanupClosePushL(fileArray);
       
   181 	
       
   182 	GetStringArrayFromConfigL(ConfigSection(), KRemoveFile, fileArray);
       
   183 	for (TInt file = 0; file < fileArray.Count();file++)
       
   184 		{
       
   185 		INFO_PRINTF2(_L("RemovingFile: %S"), &fileArray[file]);
       
   186 		
       
   187 		iIntegritySession.RemoveL(fileArray[file]);
       
   188 		}
       
   189 	fileArray.Reset();
       
   190 	
       
   191 	
       
   192 	GetStringArrayFromConfigL(ConfigSection(), KAddFile, fileArray);
       
   193 	for (TInt file = 0; file < fileArray.Count();file++)
       
   194 		{
       
   195 		INFO_PRINTF2(_L("AddingFile: %S"), &fileArray[file]);
       
   196 		iIntegritySession.AddL(fileArray[file]);
       
   197 		iIntegritySession.CreateNewTestFileL(fileArray[file]);
       
   198 		}
       
   199 	fileArray.Reset();
       
   200 
       
   201 	GetStringArrayFromConfigL(ConfigSection(), KTempFile, fileArray);
       
   202 	for (TInt file = 0; file < fileArray.Count();file++)
       
   203 		{
       
   204 		INFO_PRINTF2(_L("TemporaryFile: %S"), &fileArray[file]);
       
   205 		iIntegritySession.TemporaryL(fileArray[file]);
       
   206 		iIntegritySession.CreateTempTestFileL(fileArray[file]);
       
   207 		}
       
   208 	fileArray.Reset();
       
   209 
       
   210 	RArray<TPtrC> operationArray;
       
   211 	CleanupClosePushL(operationArray);
       
   212 	
       
   213 	GetStringArrayFromConfigL(ConfigSection(), KArbitraryOpFile, fileArray);
       
   214 	GetStringArrayFromConfigL(ConfigSection(), KOperation, operationArray);
       
   215 	TInt fileCount = fileArray.Count();
       
   216 	
       
   217 	if (fileCount != operationArray.Count())
       
   218 		{
       
   219 		ERR_PRINTF3(_L("Number of files (%d) does not match number of operations (%d)"), fileCount, operationArray.Count());
       
   220 		SetTestStepResult(EFail);
       
   221 		}
       
   222 	else
       
   223 		{
       
   224 		for (TInt file = 0; file < fileCount;file++)
       
   225 			{
       
   226 			if (operationArray[file].CompareF(KAddFile) == 0)
       
   227 				{
       
   228 				INFO_PRINTF2(_L("AddingFile: %S"), &fileArray[file]);
       
   229 				iIntegritySession.AddL(fileArray[file]);
       
   230 				iIntegritySession.CreateNewTestFileL(fileArray[file]);
       
   231 				}
       
   232 			else if (operationArray[file].CompareF(KRemoveFile) == 0)
       
   233 				{
       
   234 				INFO_PRINTF2(_L("RemovingFile: %S"), &fileArray[file]);
       
   235 				iIntegritySession.RemoveL(fileArray[file]);
       
   236 				}
       
   237 			else if (operationArray[file].CompareF(KTempFile) == 0)
       
   238 				{
       
   239 				INFO_PRINTF2(_L("TemporaryFile: %S"), &fileArray[file]);
       
   240 				iIntegritySession.TemporaryL(fileArray[file]);
       
   241 				iIntegritySession.CreateTempTestFileL(fileArray[file]);
       
   242 				}
       
   243 			else
       
   244 				{
       
   245 				ERR_PRINTF3(_L("Operation %S not understood, skipping file %S"), &operationArray[file], &fileArray[file]);
       
   246 				}
       
   247 			}
       
   248 		}
       
   249 	CleanupStack::PopAndDestroy(2, &fileArray);
       
   250 	
       
   251 	iIntegritySession.CommitL();
       
   252 	}
       
   253 
       
   254 void CIntegrityServicesStepBase::doRecoverL()
       
   255 	{
       
   256 	INFO_PRINTF1(_L("CIntegrityServicesStepBase::doRecoverL"));
       
   257 	iIntegritySession.RollBackL(EFalse);
       
   258 	}
       
   259 
       
   260 void CIntegrityServicesStepBase::doCleanupL()
       
   261 	{
       
   262 	RArray<TPtrC> fileArray;
       
   263 	CleanupClosePushL(fileArray);
       
   264 	
       
   265 	RTestUtilSessionSwi testutil;
       
   266 	User::LeaveIfError(testutil.Connect());
       
   267 	CleanupClosePushL(testutil);
       
   268 	
       
   269 	GetStringArrayFromConfigL(ConfigSection(), KCleanupFile, fileArray);
       
   270 	for (TInt file = 0; file < fileArray.Count();file++)
       
   271 		{
       
   272 		TInt err = testutil.Delete(fileArray[file]);
       
   273 		if(err != KErrNone && err != KErrPathNotFound && err != KErrNotFound)
       
   274 			{
       
   275 			User::Leave(err);
       
   276 			}
       
   277 		}
       
   278 	fileArray.Reset();
       
   279 
       
   280 	GetStringArrayFromConfigL(ConfigSection(), KCleanupDirectory, fileArray);
       
   281 	for (TInt file = 0; file < fileArray.Count();file++)
       
   282 		{
       
   283 		TInt err = testutil.RmDir(fileArray[file]);
       
   284 		if(err != KErrNone && err != KErrPathNotFound && err != KErrNotFound)
       
   285 			{
       
   286 			User::Leave(err);
       
   287 			}
       
   288 		}
       
   289 	fileArray.Reset();
       
   290 
       
   291 	CleanupStack::PopAndDestroy(&testutil);
       
   292 	CleanupStack::PopAndDestroy(&fileArray);
       
   293 	}
       
   294 
       
   295 /////////////////////////////////////////////////////////////////////
       
   296 // CInstallStep
       
   297 /////////////////////////////////////////////////////////////////////
       
   298 CInstallStep::CInstallStep()
       
   299 	{
       
   300 	SetTestStepName(KInstall);
       
   301 	}
       
   302 
       
   303 TVerdict CInstallStep::doTestStepL()
       
   304 	{
       
   305 	TRAPD(err, doInstallL());
       
   306 	
       
   307 	if(err == KErrNone || err == KIntegrityServicesSimulatedBatteryFailure)
       
   308 		{
       
   309 		if(CheckFilesL())
       
   310 			{
       
   311 			SetTestStepResult(EPass);
       
   312 			}
       
   313 		else
       
   314 			{
       
   315 			SetTestStepResult(EFail);
       
   316 			}
       
   317 		}
       
   318 	else
       
   319 		{
       
   320 		User::Leave(err);
       
   321 		}
       
   322 	return TestStepResult();
       
   323 	}
       
   324 
       
   325 /////////////////////////////////////////////////////////////////////
       
   326 // CRecoverStep
       
   327 /////////////////////////////////////////////////////////////////////
       
   328 CRecoverStep::CRecoverStep()
       
   329 	{
       
   330 	SetTestStepName(KRecover);
       
   331 	}
       
   332 
       
   333 TVerdict CRecoverStep::doTestStepL()
       
   334 	{
       
   335 	// begin recovery
       
   336 	TRAPD(err, doRecoverL());
       
   337 	
       
   338 	if(err == KErrNone || err == KIntegrityServicesSimulatedBatteryFailure)
       
   339 		{
       
   340 		if(CheckFilesL())
       
   341 			{
       
   342 			SetTestStepResult(EPass);
       
   343 			}
       
   344 		else
       
   345 			{
       
   346 			SetTestStepResult(EFail);
       
   347 			}
       
   348 		}
       
   349 	else
       
   350 		{
       
   351 		User::Leave(err);
       
   352 		}
       
   353 
       
   354 	return TestStepResult();
       
   355 	}
       
   356 
       
   357 /////////////////////////////////////////////////////////////////////
       
   358 // CCleanupStep
       
   359 /////////////////////////////////////////////////////////////////////
       
   360 CCleanupStep::CCleanupStep()
       
   361 	{
       
   362 	SetTestStepName(KCleanup);
       
   363 	}
       
   364 
       
   365 TVerdict CCleanupStep::doTestStepL()
       
   366 	{
       
   367 	doCleanupL();
       
   368 	
       
   369 	SetTestStepResult(EPass);
       
   370 	return TestStepResult();
       
   371 	}
       
   372 
       
   373 /////////////////////////////////////////////////////////////////////
       
   374 // COOMStep
       
   375 /////////////////////////////////////////////////////////////////////
       
   376 COOMStep::COOMStep()
       
   377 	{
       
   378 	SetTestStepName(KOOM);
       
   379 	}
       
   380 
       
   381 TVerdict COOMStep::doTestStepL()
       
   382 	{
       
   383 	const TInt maxFailCount = 200;
       
   384 
       
   385 	__UHEAP_MARK;
       
   386 
       
   387 	TInt installFailCount = 0;
       
   388 	TInt installResult = KErrNoMemory;
       
   389 
       
   390 	while(installResult != KErrNone && installFailCount < maxFailCount)
       
   391 		{
       
   392 		INFO_PRINTF2(_L("OOM InstallFailCount: %d"), installFailCount);
       
   393 				
       
   394 		__UHEAP_MARK;
       
   395 		
       
   396 		__UHEAP_FAILNEXT(installFailCount);
       
   397 
       
   398 		TRAP(installResult, doInstallL());
       
   399 		
       
   400 		__UHEAP_MARKEND;
       
   401 
       
   402 		__UHEAP_RESET;
       
   403 		
       
   404 		if(installResult == KErrNone)
       
   405 			{
       
   406 			if(CheckFilesL())
       
   407 				{
       
   408 				SetTestStepResult(EPass);
       
   409 				}
       
   410 			else
       
   411 				{
       
   412 				SetTestStepResult(EFail);
       
   413 				}
       
   414 
       
   415 			doCleanupL();
       
   416 			}
       
   417 		else if(installResult == KErrNoMemory)
       
   418 			{
       
   419 			// recover any partial installation, again testing oom
       
   420 			TInt recoverFailCount = 0;
       
   421 			TInt recoverResult = KErrNoMemory;
       
   422 			
       
   423 			while(recoverResult != KErrNone && recoverFailCount < maxFailCount)
       
   424 				{
       
   425 				INFO_PRINTF2(_L("OOM RecoverFailCount: %d"), recoverFailCount);
       
   426 		
       
   427 				__UHEAP_MARK;
       
   428 		
       
   429 				__UHEAP_FAILNEXT(recoverFailCount);
       
   430 
       
   431 				TRAP(recoverResult, doRecoverL());
       
   432 			
       
   433 				__UHEAP_MARKEND;
       
   434 
       
   435 				__UHEAP_RESET;
       
   436 		
       
   437 				// Fail further into the test
       
   438 				recoverFailCount++;
       
   439 				}
       
   440 			}
       
   441 		else
       
   442 			{
       
   443 			User::Leave(installResult);
       
   444 			}
       
   445 		
       
   446 		
       
   447 		// Fail further into the test
       
   448 		installFailCount++;
       
   449 		}
       
   450 	__UHEAP_MARKEND;
       
   451 				
       
   452 	SetTestStepResult(EPass);
       
   453 	return TestStepResult();
       
   454 	}
       
   455 	
       
   456 /////////////////////////////////////////////////////////////////////
       
   457 // CRecoverStep
       
   458 /////////////////////////////////////////////////////////////////////
       
   459 CCheckStep::CCheckStep()
       
   460 	{
       
   461 	SetTestStepName(KCheck);
       
   462 	}
       
   463 
       
   464 TVerdict CCheckStep::doTestStepL()
       
   465 	{
       
   466 	// Just check for files
       
   467 	if(CheckFilesL())
       
   468 		{
       
   469 		SetTestStepResult(EPass);
       
   470 		}
       
   471 	else
       
   472 		{
       
   473 		SetTestStepResult(EFail);
       
   474 		}
       
   475 		
       
   476 	return TestStepResult();
       
   477 	}