// -----------------------------------------------------------------------------
// SRTPAuthentication_HMAC_SHA1::AuthenticateL
// -----------------------------------------------------------------------------
//
HBufC8* CSRTPAuthentication_HMAC_SHA1::AuthenticateL(TUint aBitLength,
                                                    const TDesC8& aKey, 
                                                    const TDesC8& aAuthPortion,
                                                    const TDesC8& aRoc)
    {        
    SRTP_DEBUG_DETAIL( "CSRTPAuthentication_HMAC_SHA1::AuthenticateL Entry" );
    
    TBuf8<20> buf;    
	

    if (aBitLength != KSRTPAuthTagLength80 && aBitLength != KSRTPAuthTagLength32)
        {
        SRTP_DEBUG_TUINT_VALUE( "aBitLength is not valid", aBitLength );
	    
        User::Leave(KErrArgument);
        }
	
    // reserve size for 160 bit tag which seems to be the default        
	HBufC8* result = HBufC8::NewL(20);  
	TPtr8 ptrOutputBuff = result->Des();	
	
    CleanupStack::PushL(result);

    if ( !iHmac || !iKey || (*iKey != aKey) )
        {
        CreateHmacL(aKey);
        }
    
    CleanupStack::Pop(result);
    
    SRTP_DEBUG_TINT_VALUE( "HMAC INPUT and INPUT Length is ", aAuthPortion.Length() );
    SRTP_DEBUG_TINT_VALUE( "HMAC INPUT and INPUT Size is ", aAuthPortion.Size());     
    SRTP_DEBUG_PACKET ( DES_AS_8_BIT(aAuthPortion) );    
    
    if(aRoc.Length())
    	{
  		iHmac->Update(DES_AS_8_BIT(aAuthPortion));
    	buf.Copy(iHmac->Final(DES_AS_8_BIT(aRoc))); 	
    	}
    else
    	{
    	buf.Copy(iHmac->Final(DES_AS_8_BIT(aAuthPortion)));
    	}
    *result = buf;    
    ptrOutputBuff.SetLength(aBitLength/8);
    
   	SRTP_DEBUG_DETAIL( "HMAC caculated Authentication tag");
    SRTP_DEBUG_PACKET( *result );    
    SRTP_DEBUG_DETAIL( "CSRTPAuthentication_HMAC_SHA1::AuthenticateL Exit" );
                            
    return result;            
    }
void CSmilTranslatorTestUtils::ComposeFileCompleteL()
// call back function called from Composer::RunL()
	{
	TTime timeNow;
	timeNow.UniversalTime();
	TTimeIntervalMicroSeconds timeForCompose = timeNow.MicroSecondsFrom(iStartComposeTime);
	iComposeTime += timeForCompose.Int64();

	if (iComposerState == ESizing)
		{
		// Check the size of the file that has been written against the size calulated by the
		// call to CMDXMLComposer::CalculateFileSize

		RFile outputXMLFile;
		outputXMLFile.Open(iSession, iOutputFileName, EFileRead);

		TInt actualSize;
		User::LeaveIfError(outputXMLFile.Size(actualSize));
		
		if (iSize != actualSize)
			{
			// The calculated file size doesn't match the real file size, this test has failed
			TBuf<255> outputMsg;

			outputMsg.Append(KOutputNewLine);
			outputMsg.Append(_L("Test Failed - The calculated file size doesn't match the actual size."));
			outputMsg.Append(KOutputNewLine);			
			
			test.Printf(outputMsg);						// print to console
			iErrorFile.Write(DES_AS_8_BIT(outputMsg));	// print to error file			
			}

		outputXMLFile.Close();

		// If we are sizing then stop the active scheduler. Once the scheduler is stopped
		// and this function exits, program control resumes where the scheduler was started
		// in RunTestL.
//		CActiveScheduler::Stop();
		}

	else if (iComposerState == EComposing)
		{
		// The XML file has been composed. Now we need to run the sizing function to check
		// that we can calculate the size correctly.

		// Set the state to sizing and run the sizing operation...
		iComposerState = ESizing;

		// Calculate the file size and wait for the callback to this function again.
		iComposer->CalculateFileSize(iSize, iXMLDoc, testConfig->FileType());
		}
	}
void CSmilTranslatorTestUtils::RunTestL()
	{
	TRequestStatus *s = &iStatus;

	// used to generate a leave if an out of memory error was encountered, specifically
	// during the memory test loop in E32Main().  This is necessary because leaves in
	// the .dll Active Object RunL() functions do not return to this application, so
	// have to be Trapped in the Active objects and translated into an error code.
	TBool memoryError = EFalse;

	switch(iState)
		{
	case KInit:
		{
		// Utility class for file manipulation
		iFileFinder = new TFindFile(iSession);

		TPtrC errorFileName(KErrorFileName);

		// create err dir if doesn't exist - this api ignores the file name (ignores everything after final '/')
		fileSystem.MkDirAll(KErrorFileName);

		// overwrite any existing file of this name
		iErr = iErrorFile.Replace(iSession, errorFileName, EFileWrite | EFileStreamText);

		if(iErr == KErrNone)
			{
			TBuf<1> bom;
			bom.Append(CEditableText::EByteOrderMark);
			iErrorFile.Write(DES_AS_8_BIT(bom));
			// will search multiple directories, but halt after completing current directory
			// if at least one match is made.  Remembers which directories have been searched
			// in order to continue search using .FindWild() function later
			iErr = iFileFinder->FindWildByPath(KWildName, &KInputPathList, iFileList);
			iIndex = 0;
			}

		if(iErr == KErrNone)
			{
			iState = KParseFile;
			}
		else
			{
			iState = KEnd;
			}
		User::RequestComplete(s, KErrNone);
		SetActive();
		}
		break;

	case KParseFile:
		{
		++iFilesProcessed;
		iErr = KErrNone;

		TParse fullEntry;
		fullEntry.Set((*iFileList)[iIndex++].iName,& iFileFinder->File(),NULL);
		iInputFileName = fullEntry.FullName(); // extract individual path + name from list
		SetOutputFileName(); // output name is based on input one

		iOutputMsg = KStartFile;
		iOutputMsg.Append(iInputFileName);		// display full path
		test.Start(iOutputMsg);				// print to console

		// test console automatically places output on a new line, for output
		// to error file we need to add white space ready for next line
		iOutputMsg.Append(KOutputNewLine);
		iErrorFile.Write(DES_AS_8_BIT(iOutputMsg));	// print to error file

		// schedule Parser active object for call to it's RunL function
		if (testConfig->DataMode() == CTestConfig::EBufferData)
			{
			// We're testing the buffering API...
			// Create a data supplier object and pass it in to the parser
			delete iDataSupplier;
			iDataSupplier = NULL;
			iDataSupplier = CTestDataSupplier::NewL(iSession, iInputFileName);
			iParser->ParseSource(iDataSupplier);
			}
		else
			{
			if( iUseFileHandle )
				{
				RFile file;
				User::LeaveIfError(file.Open(iSession, iInputFileName, EFileRead | EFileShareReadersOnly));
			// 	No function declaration of ParseFile() that take RFile Object parameter
			//	iParser->ParseFile(file);
				iParser->ParseFile(iSession, iInputFileName);
				}
			else
				{
				// We're testing the file mode so parse the file.
				iParser->ParseFile(iSession, iInputFileName);
				}
			}

		iState = KCheckResults;
		iStatus = KRequestPending;
		SetActive();
		}
		break;

	case KCheckResults:
		{
		// when execution begins again one parse followed by a compose would have
		// completed for the current file, handle any error messages generated here
		iErr = iParser->Error();
		TInt severity = iParser->ErrorSeverity();
		if(iErr != KErrNone)
			{
			iOutputMsg = KParseError;
			AppendErrorStr(iErr, iOutputMsg);
			AppendSeverityStr(severity, iOutputMsg);
			iOutputMsg.Append(KOutputNewLine);
			
			// IF there are no more errors for this file bung in an
			// extra line to make output more prominent
			if(iComposer->Error() == KErrNone)
				{
				iOutputMsg.Append(KOutputNewLine);
				}
			test.Printf(iOutputMsg);						// print to console
			iErrorFile.Write(DES_AS_8_BIT(iOutputMsg));	// print to error file
			
			if(iErr == KErrNoMemory)
				{
				memoryError = ETrue;
				}
			}

		iErr = iComposer->Error();
		severity = iComposer->ErrorSeverity();
		if(iErr != KErrNone)
			{
			iOutputMsg = KComposeError;
			AppendErrorStr(iErr, iOutputMsg);
			AppendSeverityStr(severity, iOutputMsg);
			iOutputMsg.Append(KOutputNewLine);
			iOutputMsg.Append(KOutputNewLine);								
			test.Printf(iOutputMsg);
			iErrorFile.Write(DES_AS_8_BIT(iOutputMsg));
			
			if(iErr == KErrNoMemory)
				{
				memoryError = ETrue;
				}
			}

		test.End();

		// if the OOM condition occured during Parsing or Composing
		if(memoryError)
			{
			User::Leave(KErrNoMemory);
			}

		iState = KParseFile;

		if(iIndex >= iFileList->Count())
			{
			// fileList must be deleted after each loop prior to being passed
			// back to fileFinder (unnecessary after KErrNotFound)
			delete iFileList;
			iFileList = 0; // Just in case it doesn't get set in the FindWild

			// continue wildcard search for next directory in list
			iErr = iFileFinder->FindWild(iFileList);
			iIndex = 0;
			if(iErr != KErrNone)
				iState = KEnd;
			}

		SetActive();
		User::RequestComplete(s, KErrNone);
		}
		break;

	default:
	case KEnd:
		{
		TTime endTime;
		TTimeIntervalSeconds interval;
		endTime.UniversalTime();
		endTime.SecondsFrom(iStartTime, interval);

		TBuf<100> time;
		_LIT(KComposeTime, "Total time for composing: %d microseconds\n");
		time.Format(KComposeTime, iComposeTime);
		iErrorFile.Write(DES_AS_8_BIT(time));

		_LIT(KTimeTaken, "Total time for tests: %d seconds");
		time.Format(KTimeTaken, interval.Int());
		iErrorFile.Write(DES_AS_8_BIT(time));


		delete iFileFinder;
		delete iDataSupplier;
		CActiveScheduler::Stop();
		}
		break;
		}
	}