os/security/cryptoservices/certificateandkeymgmt/tcertstore/tcertstoreconcurrent.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "tScriptTests.h"
sl@0
    20
#include "t_testhandler.h"
sl@0
    21
#include "t_certstoretests.h"
sl@0
    22
#include "t_certstoreactions.h"
sl@0
    23
sl@0
    24
#include <e32std.h>
sl@0
    25
#include <f32file.h>
sl@0
    26
#include <ecom/ecom.h>
sl@0
    27
sl@0
    28
sl@0
    29
///////////////
sl@0
    30
sl@0
    31
#include "t_testsetup.h"
sl@0
    32
#include "t_testactionspec.h"
sl@0
    33
#include "t_input.h"
sl@0
    34
#include "t_certstoreactionmemfail.h"
sl@0
    35
#include "tcancel.h"
sl@0
    36
#include "t_message.h"
sl@0
    37
#include "tScriptSetup.h"
sl@0
    38
#include "tHardcodedSetup.h"
sl@0
    39
#include "t_testhandler.h"
sl@0
    40
#include "t_output.h"
sl@0
    41
#include "tTestSpec.h"
sl@0
    42
#include "Ttesthandlersettings.h"
sl@0
    43
sl@0
    44
/**
sl@0
    45
*	--------------------------------------
sl@0
    46
*	How this test works:
sl@0
    47
*	--------------------------------------
sl@0
    48
*
sl@0
    49
*	RUN THIS TEST IN CONFIGURATION 1 (FILECERTSTORE.DLL SHOULD BE THE ONLY 
sl@0
    50
*	ECOM PLUGIN IN Z:\SYSTEM\LIBS\PLUGINS).  IF WAPCERTSTORE.DLL IS PRESENT
sl@0
    51
*	THIS TEST WILL HANG!!!
sl@0
    52
*
sl@0
    53
*	This test is designed to stress concurrent access to the filecertstore
sl@0
    54
*	by multiple threads, and to check that the integrity of the store is
sl@0
    55
*	retained.
sl@0
    56
*
sl@0
    57
*	The test consists of 3 scripts (certstoreconcurrent1-3.txt) which test various
sl@0
    58
*	possible accesses to the filebased store (cert add, delete, set trust, applications
sl@0
    59
*	and certificate retreival.  There are 27 possible ways to combine the 3 scripts.
sl@0
    60
*	For each of these combinations, 3 threads are started and each is assigned a
sl@0
    61
*	separate test handler and one of the scripts for that combination.  The threads
sl@0
    62
*	then run together, accessing the store concurrently. 
sl@0
    63
*	
sl@0
    64
*	BECAUSE THE THREADS ARE RUNNING CONCURRENTLY, IT IS NOT POSSIBLE TO PREDICT
sl@0
    65
*	THE RESULTS OF EACH TEST, EG IT IS NOT POSSIBLE TO DETERMINE WHETHER THE 
sl@0
    66
*	CERTIFICATE THAT ONE THREAD WISHES TO DELETE IS ACTUALLY PRESENT IN THE STORE
sl@0
    67
*	AT THAT TIME, SINCE ANOTHER THREAD MAY HAVE REMOVED IT.  BECAUSE OF THIS 
sl@0
    68
*	THE SCRIPTS ARE MARKED WITH A testconcurrent FLAG TO INDICATE THAT THE FAIL
sl@0
    69
*	RESULTS SHOULD BE DISREGARDED.  The results for each script are written to a
sl@0
    70
*	separate file in EPOC directory \tcertstoreconcurrent\ on system drive thus by the end of the
sl@0
    71
*	the test there are 81 such log files in the directory.
sl@0
    72
*
sl@0
    73
*	Following the 27 combinations of 3 threads, the test then runs a standard
sl@0
    74
*	tcertstore test using one of the scripts used in general certstore testing
sl@0
    75
*	(the script is determined by the command line for the entire test).  This
sl@0
    76
*	runs in a single thread so results can be predicted.  Thus we check that 
sl@0
    77
*	filecertstore integrity is maintained.  The log file for these tests are
sl@0
    78
*	placed in EPOC system drive and should be inspected for errors as part of the
sl@0
    79
*	testing procedure.
sl@0
    80
*
sl@0
    81
*	Thus to run these tests, the following command line should be used:
sl@0
    82
*	tcertstoreconcurrent \tcertstore\scripts\unifiedcertstore2-conf1.txt \tcertstoreconcurrent1.log
sl@0
    83
*	,the script and log file being on system drive.This runs script unifiedcertstore2-conf1 after the 
sl@0
    84
*	threaded tests and logs	the test results to tcertstoreconcurrent1.log
sl@0
    85
*/
sl@0
    86
sl@0
    87
//	3 scripts available, switch between them
sl@0
    88
const static TText* scripts[] = {	_S("dummy for zero element"),
sl@0
    89
									_S("\\tcertstoreconcurrent\\scripts\\certstoreconcurrent1.txt"), 
sl@0
    90
									_S("\\tcertstoreconcurrent\\scripts\\certstoreconcurrent2.txt"),
sl@0
    91
									_S("\\tcertstoreconcurrent\\scripts\\certstoreconcurrent3.txt")};
sl@0
    92
sl@0
    93
sl@0
    94
const TInt KMaxIterations = 27;
sl@0
    95
sl@0
    96
const TInt scriptCombinations[] = {	1,1,1,	1,1,2,	1,1,3,
sl@0
    97
									1,2,1,	1,2,2,	1,2,3,
sl@0
    98
									1,3,1,	1,3,2,	1,3,3,
sl@0
    99
									2,1,1,	2,1,2,	2,1,3,
sl@0
   100
									2,2,1,	2,2,2,	2,2,3,
sl@0
   101
									2,3,1,	2,3,2,	2,3,3,
sl@0
   102
									3,1,1,	3,1,2,	3,1,3,
sl@0
   103
									3,2,1,	3,2,2,	3,2,3,
sl@0
   104
									3,3,1,	3,3,2,	3,3,3};
sl@0
   105
sl@0
   106
class TThreadData
sl@0
   107
	{
sl@0
   108
public:
sl@0
   109
	void InitialiseL(TInt aIteration, TInt aScriptNum);
sl@0
   110
public:	
sl@0
   111
	TPtrC iScriptFile;
sl@0
   112
	TFileName iLogFile;
sl@0
   113
	};
sl@0
   114
sl@0
   115
void TThreadData::InitialiseL(TInt aIteration, TInt aThreadNum)
sl@0
   116
	{
sl@0
   117
	ASSERT(aIteration >= 0 && aIteration < KMaxIterations);
sl@0
   118
	ASSERT(aThreadNum >= 1 && aThreadNum <= 3);
sl@0
   119
sl@0
   120
	TInt script = scriptCombinations[aIteration * 3 + aThreadNum - 1];		
sl@0
   121
	
sl@0
   122
	// Set script file
sl@0
   123
	TDriveUnit sysDrive (RFs::GetSystemDrive());
sl@0
   124
	TDriveName sysdriveName (sysDrive.Name());
sl@0
   125
	TBuf <60> scriptFile (sysdriveName);
sl@0
   126
	scriptFile.Append(scripts[script]);
sl@0
   127
	iScriptFile.Set(scriptFile);
sl@0
   128
sl@0
   129
	// Set log file
sl@0
   130
	iLogFile.Zero();
sl@0
   131
	TBuf<80> scriptName (sysdriveName);
sl@0
   132
	scriptName.Append(_L("\\tcertstoreconcurrent\\iteration%02d_thread%d_script%d.txt"));
sl@0
   133
	TBuf<80> buf ;
sl@0
   134
	buf.Format(scriptName,aIteration, aThreadNum, script); 
sl@0
   135
	iLogFile.Append(buf);
sl@0
   136
	}
sl@0
   137
sl@0
   138
sl@0
   139
LOCAL_C TInt ThreadEntryPoint(TAny* aArg)
sl@0
   140
{
sl@0
   141
	__UHEAP_MARK;
sl@0
   142
sl@0
   143
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   144
sl@0
   145
	TThreadData* data = static_cast<TThreadData*>(aArg);
sl@0
   146
	ASSERT(data);
sl@0
   147
sl@0
   148
	TRAPD(r, DoTests(data->iScriptFile, data->iLogFile, ETrue));
sl@0
   149
	//TRAPD(r, PerformTests(TestTypes(), data->iScriptFile, data->iLogFile));
sl@0
   150
sl@0
   151
	ASSERT( (r==KErrNone) || (r==KErrInUse) );
sl@0
   152
sl@0
   153
	delete cleanup;
sl@0
   154
	
sl@0
   155
	__UHEAP_MARKEND;
sl@0
   156
sl@0
   157
	return (r);
sl@0
   158
}
sl@0
   159
sl@0
   160
/** Start a thread. */
sl@0
   161
LOCAL_D void StartThreadL(RThread& aThread, TThreadData& aData, TInt aIteration, TInt aThreadNum, TRequestStatus& aStatus)
sl@0
   162
	{
sl@0
   163
	aData.InitialiseL(aIteration, aThreadNum);
sl@0
   164
sl@0
   165
	TBuf<32> threadName;
sl@0
   166
	threadName.Format(_L("iteration%02d_thread%d"), aIteration, aThreadNum);
sl@0
   167
	
sl@0
   168
	RHeap* heap = User::ChunkHeap(NULL, KMinHeapSize, 0x100000);
sl@0
   169
	User::LeaveIfNull(heap);
sl@0
   170
	User::LeaveIfError(aThread.Create(threadName, ThreadEntryPoint, KDefaultStackSize, heap, (TAny*)&aData));	
sl@0
   171
	aStatus = KRequestPending;
sl@0
   172
	aThread.Logon(aStatus);
sl@0
   173
	aThread.Resume();
sl@0
   174
	}
sl@0
   175
sl@0
   176
//	Kicks off each thread for multiple concurrent certstore access
sl@0
   177
LOCAL_D TBool DoThreadedTestsL(CConsoleBase* console, HBufC* logFileName, TBool wait)
sl@0
   178
{
sl@0
   179
 	RFs myfs;
sl@0
   180
 	CleanupClosePushL(myfs);
sl@0
   181
 
sl@0
   182
 	RFile logfile;
sl@0
   183
 	CleanupClosePushL(logfile);
sl@0
   184
 
sl@0
   185
 	Output* out;
sl@0
   186
 	
sl@0
   187
 	User::LeaveIfError(myfs.Connect());
sl@0
   188
 	User::LeaveIfError(logfile.Replace(myfs, *logFileName, EFileWrite));
sl@0
   189
 
sl@0
   190
 	out = new (ELeave) FileOutput(logfile);
sl@0
   191
 	CleanupStack::PushL(out);
sl@0
   192
sl@0
   193
	TInt failureCount = 0;
sl@0
   194
	
sl@0
   195
	for (TInt i = 0 ; i < KMaxIterations; ++i)
sl@0
   196
		{
sl@0
   197
		console->Printf(_L("Iteration %d \n"), i);
sl@0
   198
		out->writeNewLine();
sl@0
   199
		out->writeString(_L("Iteration "));
sl@0
   200
		out->writeNum(i);
sl@0
   201
		out->writeNewLine();
sl@0
   202
sl@0
   203
		RThread thread1;
sl@0
   204
		TThreadData data1;
sl@0
   205
		TRequestStatus status1;
sl@0
   206
		StartThreadL(thread1, data1, i, 1, status1);
sl@0
   207
sl@0
   208
		RThread thread2;
sl@0
   209
		TThreadData data2;
sl@0
   210
		TRequestStatus status2;
sl@0
   211
		StartThreadL(thread2, data2, i, 2, status2);
sl@0
   212
sl@0
   213
		RThread thread3;
sl@0
   214
		TThreadData data3;
sl@0
   215
		TRequestStatus status3;
sl@0
   216
		StartThreadL(thread3, data3, i, 3, status3);
sl@0
   217
		
sl@0
   218
		User::WaitForRequest(status1);
sl@0
   219
		User::WaitForRequest(status2);
sl@0
   220
		User::WaitForRequest(status3);
sl@0
   221
sl@0
   222
		TExitType exit1 = thread1.ExitType();
sl@0
   223
		TExitType exit2 = thread2.ExitType();
sl@0
   224
		TExitType exit3 = thread3.ExitType();
sl@0
   225
		
sl@0
   226
		if (exit1 != EExitKill) 
sl@0
   227
			{
sl@0
   228
			console->Printf(_L("ERROR: Thread 1 exited with exit type: %d \n"), exit1);
sl@0
   229
 			out->writeString(_L("ERROR: Thread 1 exited with exit type: "));
sl@0
   230
			out->writeNum(exit1);
sl@0
   231
	 		out->writeNewLine();
sl@0
   232
			failureCount++;
sl@0
   233
			}
sl@0
   234
		if (exit2 != EExitKill) 		
sl@0
   235
			{
sl@0
   236
			console->Printf(_L("ERROR: Thread 2 exited with exit type: %d \n"), exit2);
sl@0
   237
 			out->writeString(_L("ERROR: Thread 2 exited with exit type: "));
sl@0
   238
			out->writeNum(exit2);
sl@0
   239
	 		out->writeNewLine();			
sl@0
   240
			failureCount++;
sl@0
   241
			}
sl@0
   242
		if (exit3 != EExitKill) 
sl@0
   243
			{
sl@0
   244
			console->Printf(_L("ERROR: Thread 3 exited with exit type: %d \n"), exit3);
sl@0
   245
 			out->writeString(_L("ERROR: Thread 2 exited with exit type: "));
sl@0
   246
			out->writeNum(exit3);
sl@0
   247
	 		out->writeNewLine();			
sl@0
   248
			failureCount++;
sl@0
   249
			}
sl@0
   250
				
sl@0
   251
		thread1.Heap()->Close();
sl@0
   252
		thread2.Heap()->Close();
sl@0
   253
		thread3.Heap()->Close();
sl@0
   254
		
sl@0
   255
		thread1.Close();
sl@0
   256
		thread2.Close();
sl@0
   257
		thread3.Close(); 
sl@0
   258
sl@0
   259
		User::LeaveIfError(status1.Int());
sl@0
   260
		User::LeaveIfError(status2.Int());
sl@0
   261
		User::LeaveIfError(status3.Int());		
sl@0
   262
	}
sl@0
   263
	if (failureCount > 0) 
sl@0
   264
 		{
sl@0
   265
 		out->writeNewLine();
sl@0
   266
		console->Printf(_L("\n %d tests failed out of %d \n"), failureCount, (KMaxIterations*3));
sl@0
   267
 		out->writeNewLine();
sl@0
   268
 		out->writeNum(failureCount);
sl@0
   269
 		out->writeString(_L(" tests failed out of "));
sl@0
   270
 		out->writeNum(KMaxIterations*3);
sl@0
   271
 		out->writeNewLine();		
sl@0
   272
 		}
sl@0
   273
 	if (wait) 
sl@0
   274
 		{
sl@0
   275
		console->Printf(_L("\n Press any key to continue \n"));		
sl@0
   276
		console->Getch();
sl@0
   277
 		}
sl@0
   278
 	CleanupStack::PopAndDestroy(out);
sl@0
   279
 	CleanupStack::PopAndDestroy(&logfile);
sl@0
   280
 	CleanupStack::PopAndDestroy(&myfs); 	
sl@0
   281
 	if (failureCount>0) 
sl@0
   282
 		{
sl@0
   283
 		return EFalse;
sl@0
   284
 		}
sl@0
   285
 	return ETrue;
sl@0
   286
}
sl@0
   287
sl@0
   288
sl@0
   289
/**
sl@0
   290
 * Extracts the nPos command line argument.
sl@0
   291
 */
sl@0
   292
LOCAL_D HBufC* GetArgument(TInt nPos)
sl@0
   293
	{
sl@0
   294
	HBufC *argv = HBufC::NewLC(User::CommandLineLength());
sl@0
   295
	TPtr cmd(argv->Des());
sl@0
   296
	User::CommandLine(cmd);
sl@0
   297
sl@0
   298
	TLex arguments(cmd);
sl@0
   299
sl@0
   300
	// finds nth parameter 
sl@0
   301
	while(nPos && !arguments.Eos())
sl@0
   302
		{
sl@0
   303
		TPtrC token = arguments.NextToken();
sl@0
   304
		if(token.Length() > 0)		
sl@0
   305
			nPos--;
sl@0
   306
		}
sl@0
   307
sl@0
   308
	HBufC* result = NULL;
sl@0
   309
	if(!arguments.Eos())
sl@0
   310
		{
sl@0
   311
		TPtrC testfile(arguments.NextToken());
sl@0
   312
sl@0
   313
		if(testfile.Length() > 0)
sl@0
   314
			result = testfile.AllocL();
sl@0
   315
		};
sl@0
   316
sl@0
   317
	// no parameter found, but must return something so..
sl@0
   318
	if(!result)
sl@0
   319
		result = HBufC::NewL(0);
sl@0
   320
sl@0
   321
	CleanupStack::PopAndDestroy(argv);
sl@0
   322
sl@0
   323
	return result;
sl@0
   324
	}
sl@0
   325
sl@0
   326
/**
sl@0
   327
 * This function sets up a console, a log file and checks
sl@0
   328
 * whether we need to wait for a key pressed after test 
sl@0
   329
 * completion.
sl@0
   330
 * First DoThreadedTestsL is called, if everything is ok
sl@0
   331
 * it return ETrue and we move on to the standard tests.
sl@0
   332
 * If something went amiss (return EFalse) we skip 
sl@0
   333
 * the standard test and return. 
sl@0
   334
 */
sl@0
   335
LOCAL_D void SetupAndRunTests() 
sl@0
   336
{
sl@0
   337
sl@0
   338
	CConsoleBase* console = Console::NewL(_L("Test code"), TSize(KConsFullScreen, KConsFullScreen));
sl@0
   339
	
sl@0
   340
	HBufC* logFile = GetArgument(1);
sl@0
   341
	
sl@0
   342
	if (logFile->Length()==0) 
sl@0
   343
		{
sl@0
   344
		_LIT(defaultLog, "\\tcertstore.log");
sl@0
   345
		TDriveUnit sysDrive (RFs::GetSystemDrive());
sl@0
   346
		TDriveName sysdriveName (sysDrive.Name());
sl@0
   347
		TBuf <18> fileName (sysdriveName);
sl@0
   348
		fileName.Append(defaultLog);
sl@0
   349
		logFile->ReAlloc(18);
sl@0
   350
		TPtr16 plog = logFile->Des();
sl@0
   351
		plog.Append(fileName);
sl@0
   352
		}
sl@0
   353
		
sl@0
   354
	HBufC* wait = GetArgument(2);
sl@0
   355
sl@0
   356
	TBool waitAfterCompletion = EFalse;
sl@0
   357
	if (wait->Find(_L("-w")) != KErrNotFound) 
sl@0
   358
		{
sl@0
   359
		waitAfterCompletion = ETrue;
sl@0
   360
		}
sl@0
   361
	
sl@0
   362
	TBool res = EFalse;	
sl@0
   363
	TRAPD(err, res = DoThreadedTestsL(console, logFile, waitAfterCompletion));
sl@0
   364
	
sl@0
   365
	if (res) 
sl@0
   366
		{
sl@0
   367
		// Now run a normal tcertstore test to check store integrity
sl@0
   368
		TRAP(err, DoTests());
sl@0
   369
		}
sl@0
   370
		
sl@0
   371
	delete console;
sl@0
   372
	delete wait;
sl@0
   373
	delete logFile;
sl@0
   374
}
sl@0
   375
sl@0
   376
GLDEF_C TInt E32Main()
sl@0
   377
{
sl@0
   378
	__UHEAP_MARK;
sl@0
   379
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   380
	
sl@0
   381
	TRAPD(err, SetupAndRunTests());
sl@0
   382
	
sl@0
   383
	REComSession::FinalClose();
sl@0
   384
sl@0
   385
	delete cleanup;
sl@0
   386
sl@0
   387
	__UHEAP_MARKEND;
sl@0
   388
	return 0;
sl@0
   389
}