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