os/security/cryptomgmtlibs/securityutils/test/trecog/trecogstep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 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
/**
sl@0
    20
 @file
sl@0
    21
 @internalComponent - Internal Symbian test code 
sl@0
    22
*/
sl@0
    23
sl@0
    24
sl@0
    25
#include "trecogstep.h"
sl@0
    26
sl@0
    27
// Literals & Constants
sl@0
    28
_LIT(KCompleted, "Completed.");
sl@0
    29
_LIT(KMethodName, "byName");
sl@0
    30
_LIT(KMethodHandle, "byHandle");
sl@0
    31
_LIT(KMethodBuffer, "byBuffer");
sl@0
    32
_LIT8(KEmptyBuffer8,"");
sl@0
    33
sl@0
    34
sl@0
    35
class RDeletefile
sl@0
    36
	{
sl@0
    37
public:
sl@0
    38
	RDeletefile(RFs &aFs, TDesC &aFileName)
sl@0
    39
		: iFs(aFs),
sl@0
    40
		  iFileName(aFileName)
sl@0
    41
		{
sl@0
    42
		}
sl@0
    43
	void Close()
sl@0
    44
		{
sl@0
    45
		(void) iFs.Delete(iFileName);
sl@0
    46
		}
sl@0
    47
	
sl@0
    48
private:
sl@0
    49
	RFs &iFs;
sl@0
    50
	TDesC &iFileName;
sl@0
    51
	};
sl@0
    52
sl@0
    53
void CRecogStep::TestRecognizeDataL()
sl@0
    54
	{
sl@0
    55
	TDataRecognitionResult recogResult;
sl@0
    56
	
sl@0
    57
	TPtrC tmp;
sl@0
    58
	GetStringFromConfig(ConfigSection(), _L("fileName"), tmp);
sl@0
    59
	iFileName = tmp;
sl@0
    60
	
sl@0
    61
sl@0
    62
	TPtrC expectedDataType16;
sl@0
    63
	GetStringFromConfig(ConfigSection(), _L("expectedDataType"), expectedDataType16);
sl@0
    64
	HBufC8 *expectedDataBuf8 = ConvertDes16toHBufC8LC(expectedDataType16);
sl@0
    65
sl@0
    66
	TInt expectedConfidence;
sl@0
    67
	GetIntFromConfig(ConfigSection(), _L("expectedConfidence"), expectedConfidence);
sl@0
    68
	
sl@0
    69
	TInt maxDataBufSize;
sl@0
    70
	if (GetIntFromConfig(ConfigSection(), _L("maxDataBufSize"), maxDataBufSize))
sl@0
    71
		{
sl@0
    72
		iLs.SetMaxDataBufSize(maxDataBufSize);
sl@0
    73
		}
sl@0
    74
		
sl@0
    75
	TPtrC method;
sl@0
    76
	GetStringFromConfig(ConfigSection(), _L("method"), method);
sl@0
    77
		
sl@0
    78
	TBool checkSpecific = EFalse;
sl@0
    79
	TBool matchedSpecificMimeType = EFalse;
sl@0
    80
	TPtrC specificMimeType;
sl@0
    81
	if (method == KMethodHandle)
sl@0
    82
		{		
sl@0
    83
		checkSpecific = GetStringFromConfig(ConfigSection(), _L("checkSpecificMimeType"), specificMimeType);
sl@0
    84
		}
sl@0
    85
sl@0
    86
	TInt usePrivateFile = 0;
sl@0
    87
	GetIntFromConfig(ConfigSection(), _L("usePrivateFile"), usePrivateFile);
sl@0
    88
	
sl@0
    89
	RDeletefile deletefile(iTheFs, iFileName);
sl@0
    90
	if(!usePrivateFile)
sl@0
    91
		{
sl@0
    92
		// Tweak file modification time to defeat the apparch recognizer result cache....
sl@0
    93
		TTime time;
sl@0
    94
		User::LeaveIfError(iTheFs.Modified(iFileName, time));
sl@0
    95
		time += TTimeIntervalSeconds(1);
sl@0
    96
		User::LeaveIfError(iTheFs.SetModified(iFileName, time));
sl@0
    97
		}
sl@0
    98
	else
sl@0
    99
		{
sl@0
   100
		// Copy file to private dir, this will make it inaccesible to the recognizer code (except via handle).
sl@0
   101
		ConvertFileToPrivateL();	
sl@0
   102
		CleanupClosePushL(deletefile);
sl@0
   103
		}
sl@0
   104
	
sl@0
   105
	
sl@0
   106
	if (method == KMethodName)
sl@0
   107
		{
sl@0
   108
		INFO_PRINTF2(_L("Test Recognizing %S by File Name"), &iFileName);
sl@0
   109
	    User::LeaveIfError(iLs.RecognizeData(iFileName, KEmptyBuffer8, recogResult));
sl@0
   110
		}
sl@0
   111
	else if((method == KMethodHandle) || (method == KMethodBuffer))
sl@0
   112
		{
sl@0
   113
		RFile fileToRead;	
sl@0
   114
		User::LeaveIfError(fileToRead.Open(iTheFs, iFileName, EFileShareReadersOrWriters | EFileRead | EFileStream));
sl@0
   115
		CleanupClosePushL(fileToRead);
sl@0
   116
		if(method == KMethodHandle)
sl@0
   117
			{
sl@0
   118
			if (checkSpecific)
sl@0
   119
				{
sl@0
   120
				HBufC8* specificMimeType8 = ConvertDes16toHBufC8LC(specificMimeType);
sl@0
   121
				TDataType mime(*specificMimeType8);																					
sl@0
   122
				INFO_PRINTF2(_L("Test matching specific mime-type %S by Handle"), &iFileName);		
sl@0
   123
				User::LeaveIfError(iLs.RecognizeSpecificData(fileToRead, mime, matchedSpecificMimeType));
sl@0
   124
				CleanupStack::PopAndDestroy(specificMimeType8);
sl@0
   125
				}
sl@0
   126
			else 
sl@0
   127
				{
sl@0
   128
				INFO_PRINTF2(_L("Test Recognizing %S by Handle"), &iFileName);		
sl@0
   129
			    User::LeaveIfError(iLs.RecognizeData(fileToRead, recogResult));				
sl@0
   130
				}
sl@0
   131
			}
sl@0
   132
		else
sl@0
   133
			{
sl@0
   134
			INFO_PRINTF2(_L("Test Recognizing %S by Buffer"), &iFileName);		
sl@0
   135
			TInt size;
sl@0
   136
			User::LeaveIfError(fileToRead.Size(size));
sl@0
   137
sl@0
   138
			TInt maxBufferSize;
sl@0
   139
			if(GetIntFromConfig(ConfigSection(), _L("maxBufferSize"), maxBufferSize))
sl@0
   140
				{
sl@0
   141
				if(size > maxBufferSize)
sl@0
   142
					{
sl@0
   143
					size = maxBufferSize;
sl@0
   144
					}
sl@0
   145
				}
sl@0
   146
sl@0
   147
			HBufC8* memForFile = HBufC8::NewLC(size);
sl@0
   148
			TPtr8 fileContent(memForFile->Des());		
sl@0
   149
			User::LeaveIfError(fileToRead.Read(fileContent, size));
sl@0
   150
			User::LeaveIfError(iLs.RecognizeData(iFileName, fileContent, recogResult));
sl@0
   151
			CleanupStack::PopAndDestroy(); //memForFile,
sl@0
   152
			}
sl@0
   153
		CleanupStack::PopAndDestroy(&fileToRead);
sl@0
   154
		}
sl@0
   155
	else
sl@0
   156
		{
sl@0
   157
		ERR_PRINTF1(_L("method not set correctly"));		
sl@0
   158
		User::Leave(KErrArgument);
sl@0
   159
		}
sl@0
   160
	
sl@0
   161
	
sl@0
   162
	if (checkSpecific)
sl@0
   163
		{
sl@0
   164
		if (matchedSpecificMimeType)
sl@0
   165
			{
sl@0
   166
			INFO_PRINTF2(_L("Specific type '%S' matched\n"), &specificMimeType);
sl@0
   167
			SetTestStepResult(EPass);			
sl@0
   168
			}
sl@0
   169
		else 
sl@0
   170
			{
sl@0
   171
			INFO_PRINTF2(_L("Specific type '%S' not matched\n"), &specificMimeType);
sl@0
   172
			SetTestStepResult(EFail);			
sl@0
   173
			}
sl@0
   174
		}
sl@0
   175
    else 
sl@0
   176
    	{
sl@0
   177
    	TPtrC result16 = recogResult.iDataType.Des();
sl@0
   178
    	if (// Expected failure
sl@0
   179
		((expectedConfidence == CApaDataRecognizerType::ENotRecognized) && 
sl@0
   180
		 (recogResult.iDataType != expectedDataBuf8->Des())) ||
sl@0
   181
		// Expected success
sl@0
   182
		((recogResult.iConfidence == expectedConfidence) && 
sl@0
   183
		 (recogResult.iDataType == expectedDataBuf8->Des())))
sl@0
   184
		    {
sl@0
   185
			INFO_PRINTF3(_L("PASSED - type '%S' confidence=%d\n"), 
sl@0
   186
						 &result16,
sl@0
   187
						 recogResult.iConfidence);		
sl@0
   188
		    SetTestStepResult(EPass);	
sl@0
   189
		    }
sl@0
   190
		else
sl@0
   191
			{
sl@0
   192
			ERR_PRINTF5(_L("FAILED - expected '%S', got type '%S' - expected confidence=%d got confidence %d\n"), 
sl@0
   193
						&expectedDataType16,
sl@0
   194
						&result16,
sl@0
   195
						expectedConfidence,
sl@0
   196
						recogResult.iConfidence);		
sl@0
   197
			SetTestStepResult(EFail);		
sl@0
   198
			}
sl@0
   199
    	}
sl@0
   200
	
sl@0
   201
	if(usePrivateFile)
sl@0
   202
		{
sl@0
   203
		CleanupStack::PopAndDestroy(&deletefile);
sl@0
   204
		}
sl@0
   205
	
sl@0
   206
	CleanupStack::PopAndDestroy(expectedDataBuf8);
sl@0
   207
	INFO_PRINTF1(KCompleted);
sl@0
   208
   	}
sl@0
   209
sl@0
   210
HBufC8* CRecogStep::ConvertDes16toHBufC8LC(TDesC& source)
sl@0
   211
	{
sl@0
   212
	HBufC8 *buf = HBufC8::NewL(source.Length());
sl@0
   213
	CleanupStack::PushL(buf);
sl@0
   214
	TPtr8 ptr=buf->Des();
sl@0
   215
sl@0
   216
	CCnvCharacterSetConverter *converter = CCnvCharacterSetConverter::NewLC();
sl@0
   217
	converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierAscii, iTheFs);
sl@0
   218
	converter->ConvertFromUnicode(ptr, source);
sl@0
   219
	CleanupStack::PopAndDestroy(converter);
sl@0
   220
	return buf;
sl@0
   221
	}
sl@0
   222
sl@0
   223
sl@0
   224
sl@0
   225
void CRecogStep::RunTestCasesL()
sl@0
   226
	{
sl@0
   227
	__UHEAP_MARK;
sl@0
   228
	TRAPD(r,TestRecognizeDataL());
sl@0
   229
	TEST(r==KErrNone);
sl@0
   230
	__UHEAP_MARKEND;
sl@0
   231
	}
sl@0
   232
sl@0
   233
sl@0
   234
/**
sl@0
   235
   Destructor
sl@0
   236
 */
sl@0
   237
CRecogStep::~CRecogStep()
sl@0
   238
	{
sl@0
   239
	iTheFs.Close();
sl@0
   240
	iLs.Close();
sl@0
   241
	}
sl@0
   242
sl@0
   243
/**
sl@0
   244
   Constructor
sl@0
   245
 */
sl@0
   246
CRecogStep::CRecogStep()
sl@0
   247
	{
sl@0
   248
sl@0
   249
	TInt ret=iTheFs.Connect();
sl@0
   250
	TEST(ret==KErrNone);
sl@0
   251
	iTheFs.ShareProtected();
sl@0
   252
sl@0
   253
	ret = iLs.Connect();
sl@0
   254
	TEST(ret==KErrNone);
sl@0
   255
sl@0
   256
	
sl@0
   257
	// Call base class method to set up the human readable name for logging
sl@0
   258
	SetTestStepName(KRecogStep);
sl@0
   259
	}
sl@0
   260
sl@0
   261
/**
sl@0
   262
   @return - TVerdict code
sl@0
   263
   Override of base class virtual
sl@0
   264
 */
sl@0
   265
TVerdict CRecogStep::doTestStepPreambleL()
sl@0
   266
	{
sl@0
   267
	SetTestStepResult(EPass);
sl@0
   268
	return TestStepResult();
sl@0
   269
	}
sl@0
   270
sl@0
   271
/**
sl@0
   272
   @return - TVerdict code
sl@0
   273
   Override of base class virtual
sl@0
   274
 */
sl@0
   275
TVerdict CRecogStep::doTestStepPostambleL()
sl@0
   276
	{
sl@0
   277
	return TestStepResult();
sl@0
   278
	}
sl@0
   279
sl@0
   280
sl@0
   281
TVerdict CRecogStep::doTestStepL()
sl@0
   282
	{
sl@0
   283
	TRAPD(ret,RunTestCasesL())
sl@0
   284
	TEST(ret==KErrNone);
sl@0
   285
	return TestStepResult();
sl@0
   286
	}
sl@0
   287
sl@0
   288
void CRecogStep::ConvertFileToPrivateL()
sl@0
   289
	{
sl@0
   290
	// Read source file
sl@0
   291
	RFile fromFile;	
sl@0
   292
	User::LeaveIfError(fromFile.Open(iTheFs, iFileName, EFileShareReadersOrWriters | EFileRead | EFileStream));
sl@0
   293
	CleanupClosePushL(fromFile);
sl@0
   294
	TInt size;
sl@0
   295
	User::LeaveIfError(fromFile.Size(size));
sl@0
   296
	HBufC8* memForFileContents = HBufC8::NewLC(size);
sl@0
   297
	TPtr8 fileContents(memForFileContents->Des());		
sl@0
   298
	User::LeaveIfError(fromFile.Read(fileContents, size));
sl@0
   299
	
sl@0
   300
	// Create destination path
sl@0
   301
	TDriveUnit sysDrive = RFs::GetSystemDrive();
sl@0
   302
	(void)iTheFs.CreatePrivatePath(static_cast<TInt> (sysDrive));
sl@0
   303
	TFileName newFileName;
sl@0
   304
	User::LeaveIfError(iTheFs.PrivatePath(newFileName));
sl@0
   305
	newFileName.Insert(0, sysDrive.Name());
sl@0
   306
	TParsePtr parseFrom(iFileName);
sl@0
   307
	newFileName.Append(parseFrom.NameAndExt());
sl@0
   308
sl@0
   309
	// Make sure destination does not already exist.
sl@0
   310
	(void) iTheFs.Delete(newFileName);
sl@0
   311
sl@0
   312
	// Copy file to private location
sl@0
   313
	RFile toFile;
sl@0
   314
	User::LeaveIfError(toFile.Create(iTheFs, newFileName, EFileShareExclusive | EFileWrite | EFileStream));
sl@0
   315
	CleanupClosePushL(toFile);
sl@0
   316
sl@0
   317
	User::LeaveIfError(toFile.Write(fileContents));
sl@0
   318
sl@0
   319
	// Update iFileName
sl@0
   320
	iFileName = newFileName;
sl@0
   321
	
sl@0
   322
	// Cleanup
sl@0
   323
	CleanupStack::PopAndDestroy(&toFile);
sl@0
   324
	CleanupStack::PopAndDestroy(memForFileContents);
sl@0
   325
	CleanupStack::PopAndDestroy(&fromFile);
sl@0
   326
	}
sl@0
   327
sl@0
   328
// End of file