messagingfw/wappushfw/examples/PushAppHandlerEx/test/t_wappush.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15     
       
    16 
       
    17 #include <e32test.h>
       
    18 #include <e32svr.h>		// for RDebug::Print
       
    19 #include <e32std.h>
       
    20 #include <flogger.h>
       
    21 #include "t_wappush.h"
       
    22 #include "pushtests.h"
       
    23 #include <push/pushmessage.h>
       
    24 
       
    25 // service setup includes...
       
    26 #include <msvstd.h>
       
    27 #include <msvids.h>
       
    28 #include "t_serviceutils.h"
       
    29 #include <pushentry.h>
       
    30 
       
    31 
       
    32 _LIT(KPushMtmDatFile, "z:\\system\\mtm\\WapPushMtmU.dat");
       
    33 _LIT(KMmsMtmDatFile, "c:\\system\\mtm\\mms.dat");
       
    34 _LIT(KWapPushTestTitle,"WAP Push tests");
       
    35 
       
    36 
       
    37 // Test harness requirements
       
    38 //
       
    39 // Uses an active object state machine to run tests.
       
    40 // Creates a Connection Manager object.
       
    41 // Creates a Dummy wap stack via CDummyWapStack::NewL()
       
    42 // Feeds a push message to Dummy wap stack 
       
    43 // Starts Connection Manager - just add active object to scheduler and start?
       
    44 // Needs to know when completed - and callback?
       
    45 // Verifies that CPushMessage received is the same as sent
       
    46 // Closes/Destroys connection manager - delete?
       
    47 //
       
    48 
       
    49 
       
    50 class CTestScheduler : public CActiveScheduler
       
    51 	{
       
    52 	virtual void Error(TInt anError) const;
       
    53 	};
       
    54 
       
    55 void CTestScheduler::Error(TInt anError) const
       
    56 	{
       
    57 	_LIT(KTestPanic,"CTestScheduler RunL leave");
       
    58 	User::Panic(KTestPanic,anError);
       
    59 	}
       
    60 
       
    61 
       
    62 
       
    63 
       
    64 // cwappushtestengine
       
    65 
       
    66 void CWapPushTestEngine::DoCancel()
       
    67 	{
       
    68 	// cancel the current test
       
    69 	if (iCurrentTest)
       
    70 		iCurrentTest->Cancel();
       
    71 	}
       
    72 
       
    73 void CWapPushTestEngine::RunL()
       
    74 	{
       
    75 	switch (iState)
       
    76 		{
       
    77 	case EIdle:
       
    78 		{
       
    79 		TRAPD(err,RunNextTestL())
       
    80 		if (err!=KErrNone)
       
    81 			iConsole->Printf(KTextFailedTest);
       
    82 		}
       
    83 		break;
       
    84 	case ERunningTest:
       
    85 		break;
       
    86 	case EShuttingDown:
       
    87 		// Clear Out the Plugin Cache
       
    88 //		CPluginServerClient* psc = CPluginServerClient::NewL();
       
    89 //		psc->PluginFactory().ClearCache();
       
    90 //		delete psc;
       
    91 		CActiveScheduler::Stop();
       
    92 		break;
       
    93 		}
       
    94 	}
       
    95 
       
    96 /**
       
    97  * Static factory function for a new test engine
       
    98  */
       
    99 CWapPushTestEngine* CWapPushTestEngine::NewL()
       
   100 	{
       
   101 	CWapPushTestEngine* self = new(ELeave) CWapPushTestEngine();
       
   102 	CleanupStack::PushL(self);
       
   103 	self->ConstructL();
       
   104 	CleanupStack::Pop(self); 
       
   105 	return self;
       
   106 	}
       
   107 
       
   108 /**
       
   109  * Constructor for the test engine
       
   110  * Creates a console and sets itself active
       
   111  */
       
   112 void CWapPushTestEngine::ConstructL()
       
   113 	{
       
   114 	iConsole = Console::NewL(KWapPushTestTitle,TSize(KConsFullScreen,KConsFullScreen));
       
   115 	// set up service BEFORE active scheduler...
       
   116 	InitialiseMsgServiceL();
       
   117 	//
       
   118 	CActiveScheduler::Add(this);
       
   119 	SetActive();
       
   120 	TRequestStatus* stat = &iStatus;
       
   121 	User::RequestComplete(stat,KErrNone);
       
   122 	}
       
   123 
       
   124 /** 
       
   125  * Destructor for CWapPushTestEngine
       
   126  * Cancels any outstanding requests and deletes member variables
       
   127  */
       
   128 CWapPushTestEngine::~CWapPushTestEngine()
       
   129 	{
       
   130 	Cancel();
       
   131 	delete iConsole;
       
   132 	}
       
   133 
       
   134 /**
       
   135  * This should be called by tests to indicate that they have 
       
   136  * completed and whether they were sucessful or not 
       
   137  */
       
   138 void CWapPushTestEngine::TestCompleted(TInt aResult)
       
   139 	{
       
   140 	if (aResult==KErrNone)
       
   141 		{
       
   142 		iState=EIdle;
       
   143 		iTest=(TTest)(iTest+1);
       
   144 		}
       
   145 	else
       
   146 		{
       
   147 		iConsole->Printf(KTextFailedTest);
       
   148 		iState = EShuttingDown;
       
   149 		}
       
   150 	// set active again
       
   151 	TRequestStatus* stat = &iStatus;
       
   152 	User::RequestComplete(stat,KErrNone);
       
   153 	SetActive();
       
   154 	}
       
   155 
       
   156 /** 
       
   157  * Run the next test
       
   158  * The switch statement lists all tests to be run
       
   159  * 
       
   160  */
       
   161 void CWapPushTestEngine::RunNextTestL()
       
   162 	{
       
   163 	// delete any old tests
       
   164 	delete iCurrentTest;
       
   165 	iCurrentTest=NULL;
       
   166 	//
       
   167 
       
   168 	switch (iTest)
       
   169 		{
       
   170 	case ESCRIPT:
       
   171 		iCurrentTest = new (ELeave) CWapScriptTest();
       
   172 		break;
       
   173 	default:
       
   174 		// We've finished - set status to shut down and complete our own request
       
   175 		SetPriority(EPriorityIdle);
       
   176 		iConsole->Printf(KTextFinishedTest);
       
   177 		iState = EShuttingDown;
       
   178 		TRequestStatus* stat = &iStatus;
       
   179 		User::RequestComplete(stat,KErrNone);
       
   180 		SetActive();
       
   181 		return;
       
   182 		}
       
   183 	
       
   184 	iCurrentTest->SetEngine(this);
       
   185 	iState = ERunningTest;
       
   186 	iCurrentTest->BeginTest();
       
   187 	
       
   188 	}
       
   189 
       
   190 
       
   191 /**
       
   192  * print a descriptor to the console - prefixes aString text with aName
       
   193  * @param aName the name of the test
       
   194  * @param aString the text to be displays
       
   195  */
       
   196 void CWapPushTestEngine::Printf(const TDesC& aName, const TDesC& aString)
       
   197 	{
       
   198 	TBuf<12> format;
       
   199 	format.Zero();
       
   200 	format.AppendNum((TInt)iTest);
       
   201 	_LIT(KTextCoreFormat,".%S: %S\n");
       
   202 	format.Append(KTextCoreFormat);
       
   203 	iConsole->Printf(format,&aName,&aString);
       
   204 	}
       
   205 
       
   206 /** 
       
   207  * get a character from the console 
       
   208  */
       
   209 TKeyCode CWapPushTestEngine::Getch()
       
   210 	{
       
   211 	return iConsole->Getch();
       
   212 	}
       
   213 
       
   214 /**
       
   215  * return a reference to the console used by the test harness
       
   216  */
       
   217 CConsoleBase& CWapPushTestEngine::Console() const 
       
   218 	{
       
   219 	return *iConsole;
       
   220 	}
       
   221 
       
   222 /** connect to the push message service
       
   223 
       
   224 	if no message service set up then create one else
       
   225 	use existing service and remove any existing messages 
       
   226 */
       
   227 void CWapPushTestEngine::InitialiseMsgServiceL()
       
   228 	{
       
   229 	TMsvId pushService = KMsvNullIndexEntryId; 
       
   230 	TMsvId pushFolder = KMsvNullIndexEntryId;
       
   231 
       
   232 	CWapPushTestMsgUtils* msgUtils = CWapPushTestMsgUtils::NewL(); 
       
   233 	CleanupStack::PushL(msgUtils);
       
   234 
       
   235 	CMsvEntrySelection* idArray = new (ELeave) CMsvEntrySelection();
       
   236 	CleanupStack::PushL(idArray);
       
   237 
       
   238 	// uncomment this if you want to remove existing service entries
       
   239 	//msgUtils->ClearExistingServiceEntriesL(KUidMtmWapPush);
       
   240 
       
   241 	msgUtils->PushServiceIdL(pushService, idArray);
       
   242 	if ((pushService == KMsvNullIndexEntryId) && (idArray->Count() ==0) ) // Found no service
       
   243 		{
       
   244 		pushService = msgUtils->CreateServiceL();
       
   245 		}
       
   246 	// install mtm - leaves if found to already exist 
       
   247 	TRAPD(ignore, msgUtils->InstallMtmGroupL(KPushMtmDatFile));
       
   248 
       
   249 
       
   250 	idArray->Reset();
       
   251 	msgUtils->PushFolderIdL(pushFolder, idArray);
       
   252 
       
   253 	if ((pushFolder == KMsvNullIndexEntryId) && (idArray->Count() ==0) ) // Found no service
       
   254 		pushFolder = msgUtils->CreatePushMsgFolderL();
       
   255 		
       
   256 	
       
   257 	//Clean out all previous push entries from under the Push Service Entry
       
   258 	//myTestUtils->RemoveServiceEntryChildrenL(pushService);
       
   259 
       
   260 	// Clean the push msg folder of existing entries
       
   261 	msgUtils->RemoveEntriesFromLocalServiceFolderL(pushFolder, KUidMtmWapPush);
       
   262 	// Clean the inbox of all existing Wap Push entries....
       
   263 	msgUtils->RemoveEntriesFromLocalServiceFolderL(KMsvGlobalInBoxIndexEntryId, KUidMtmWapPush);
       
   264 
       
   265 	CleanupStack::PopAndDestroy(idArray); 
       
   266 
       
   267 	// now set up MMS Services
       
   268 	msgUtils->CreateMmsServiceL();
       
   269 	TRAPD(ignore2, msgUtils->InstallMtmGroupL(KMmsMtmDatFile));
       
   270 
       
   271 	CleanupStack::PopAndDestroy(msgUtils); 
       
   272 	}
       
   273 
       
   274 
       
   275 
       
   276 /** Add the test to the active scheduler and set it active
       
   277  *  by completing its own request
       
   278  */
       
   279 void CWapPushTest::BeginTest()
       
   280 	{
       
   281 	CActiveScheduler::Add(this);
       
   282 	SetActive();
       
   283 	TRequestStatus* stat = &iStatus;
       
   284 	User::RequestComplete(stat,KErrNone);
       
   285 	}
       
   286 
       
   287 
       
   288 /** 
       
   289  * Print a descriptor to the console 
       
   290  * @param aString descriptor to print
       
   291  */
       
   292 void CWapPushTest::Printf(const TDesC& aString)
       
   293 	{
       
   294 	iEngine->Printf(TestName(),aString);
       
   295 	}
       
   296 
       
   297 /** 
       
   298  * Check a test result and panic if false
       
   299  * @param aTestResult the result to check
       
   300  * @param aLine the line number - can be provided by MSVC macro __LINE__
       
   301  * #define TEST(A) Test(A,__LINE__)
       
   302  * TEST(err=KErrNone)
       
   303  */
       
   304 void CWapPushTest::Test(TInt aTestResult,TInt aLine)
       
   305 	{
       
   306 	if (!aTestResult)
       
   307 		{
       
   308 		User::Panic(TestName(),aLine);
       
   309 		}
       
   310 	}
       
   311 
       
   312 
       
   313 
       
   314 
       
   315 
       
   316 
       
   317 /**
       
   318 * Printf
       
   319 * 
       
   320 * @param aDescription The text to print/log
       
   321 */
       
   322 void CWapPushTest::WPLPrintf(const TDesC& aDescription)
       
   323 	{
       
   324 const TInt KWatcherLogBuffer = 256;
       
   325 	_LIT(KPushLogDir, "push");
       
   326 	_LIT(KPushLogFile, "WapPushLogging.txt");
       
   327 	// Write to log file
       
   328 	RFileLogger::Write(KPushLogDir, KPushLogFile, EFileLoggingModeAppend, aDescription);
       
   329 
       
   330 	// Write to console
       
   331 	_LIT(KPushLogFmt,"Push:%S\n");
       
   332 	TPtrC	buf = aDescription.Left(Min(KWatcherLogBuffer, aDescription.Length()));
       
   333 	iEngine->Console().Printf(KPushLogFmt,&buf);
       
   334 	}
       
   335 
       
   336 /**
       
   337  * Takes the data in a Push Message and prints it to console and the logs it to file.
       
   338  * Uses the accessor functions of the CPushMessage class and this classes Printf function 
       
   339  * to perform this.  Currently 4 headers and the message body are displayed and 
       
   340  * logged. The headers are:
       
   341  *		PushID, Content-Type, X-Application-ID, Expiry Date, 
       
   342  *
       
   343  *	@param	CPushMessage& aMessage
       
   344  *				in:  a reference to a Push Message.
       
   345  */
       
   346 void CWapPushTest::WPLPrintfL(CPushMessage& aMessage)
       
   347 	{
       
   348 	const TInt KWatcherLogBuffer=256;
       
   349 	TBuf<KWatcherLogBuffer> buf;
       
   350 	
       
   351 	// Content type
       
   352 	_LIT(KLogContentFmt,"Content Type : \t%S");
       
   353 	TPtrC contentPointer;
       
   354 	aMessage.GetContentType(contentPointer);
       
   355 	buf.Format(KLogContentFmt,&contentPointer);
       
   356 	buf.Append('\n');
       
   357 	WPLPrintf(buf);
       
   358 
       
   359 	// Date Field
       
   360 	_LIT(KLogDateTimeFmt," %-B%:0%J%:1%T%:2%S%+B   %D %N %Y  %4 %5 %3");
       
   361 	_LIT(KLogDateFmt,"Date           :\t%S");
       
   362 	TBool foundField;
       
   363 	TTime timeValue;
       
   364 	foundField = aMessage.GetHeaderField(EHttpDate, timeValue);
       
   365 	if (foundField)
       
   366 		{
       
   367 		TRAPD(err, timeValue.FormatL(buf, KLogDateTimeFmt));
       
   368 		if (err == KErrNone)
       
   369 			{
       
   370 			TBuf<KWatcherLogBuffer> dateBuf;
       
   371 			dateBuf.Format(KLogDateFmt, &buf);
       
   372 			WPLPrintf(dateBuf);
       
   373 			}
       
   374 		}
       
   375 
       
   376 	//Expires Field
       
   377 	aMessage.GetHeaderField(EHttpExpires, timeValue);
       
   378 	if (foundField)
       
   379 		{
       
   380 		TRAPD(err, timeValue.FormatL(buf, KLogDateTimeFmt));
       
   381 		if (err == KErrNone)
       
   382 			{
       
   383 			TBuf<KWatcherLogBuffer> dateBuf;
       
   384 			dateBuf.Format(KLogDateFmt, &buf);
       
   385 			WPLPrintf(dateBuf);
       
   386 			}
       
   387 		}
       
   388 
       
   389 	// X-Wap-Application-ID
       
   390 	TBool isInt;
       
   391 	TInt ID;
       
   392 	TPtrC8 generalPtr;
       
   393 	_LIT(KLogAppIdIntFmt,"App ID         :\t%X");
       
   394 	foundField = aMessage.GetAppID(generalPtr, ID, isInt);
       
   395 	if (foundField) // App ID is present
       
   396 		{
       
   397 		if (isInt)  // Field in integer format
       
   398 			{
       
   399 			buf.Format(KLogAppIdIntFmt,ID);
       
   400 			}
       
   401 		else  // should be descriptor format
       
   402 			{
       
   403 			buf.Copy(generalPtr);
       
   404 			}
       
   405 		WPLPrintf(buf);
       
   406 		}
       
   407 
       
   408 	//Message Header Binary
       
   409 	_LIT(KLogMsgHdr,"Header Binary:");
       
   410 	WPLPrintf(KLogMsgHdr);
       
   411 	aMessage.GetHeader(generalPtr);
       
   412 	
       
   413 	HBufC* tempHdr = HBufC::NewLC(generalPtr.Length());
       
   414 	tempHdr->Des().Copy(generalPtr);
       
   415 	WPLLogBinaryAsHex(*tempHdr);
       
   416 	CleanupStack::PopAndDestroy(); //tempHdr
       
   417 
       
   418 	//Message Body
       
   419 	aMessage.GetMessageBody(generalPtr);
       
   420 
       
   421 	// Dump Body As Text
       
   422 	_LIT(KLogMsgBody,"Body Text:");
       
   423 	WPLPrintf(KLogMsgBody);
       
   424 	HBufC* tempBody = HBufC::NewLC(generalPtr.Length());
       
   425 	tempBody->Des().Copy(generalPtr);
       
   426 	WPLPrintf(*tempBody);
       
   427 
       
   428 	// Dump Body As Hex
       
   429 	_LIT(KBodyBinary,"\nBody Binary:");
       
   430 	WPLPrintf(KBodyBinary);
       
   431 	WPLLogBinaryAsHex(*tempBody);
       
   432 
       
   433 	CleanupStack::PopAndDestroy(); //tempBody
       
   434 	
       
   435 	}
       
   436  
       
   437 
       
   438  /**
       
   439  * Prints out Buffer data in the format:
       
   440  *	%X %X %X %X %X %X %X\n etc 
       
   441  * For example
       
   442  *	AB CD 01 12 34 A2 
       
   443  *
       
   444  *	@param	aDescription
       
   445  *				in:  the descriptor to be dumped into the log
       
   446  */
       
   447 void CWapPushTest::WPLLogBinaryAsHex(const TDesC& aDescription)
       
   448 	{
       
   449 	const TInt KWatcherLogBuffer=256;
       
   450 	_LIT(KHexSpace,"%02X ");
       
   451 	TBuf<KWatcherLogBuffer> hexBuf;
       
   452 	TBuf<KWatcherLogBuffer> buf;
       
   453 
       
   454 	TInt i = 0, bodyLen = aDescription.Length();
       
   455 
       
   456 	for (; i < bodyLen; i++)
       
   457 		{
       
   458 		hexBuf.Format(KHexSpace,aDescription[i]);
       
   459 		buf.Append(hexBuf);
       
   460 		if ( i && ((i+1) % 8) == 0  )
       
   461 			{
       
   462 			WPLPrintf(buf);
       
   463 			buf.Zero();
       
   464 			}
       
   465 		}
       
   466 
       
   467 	if (buf.Length())
       
   468 		WPLPrintf(buf);
       
   469 	}
       
   470 
       
   471 void CWapPushTest::WPLLogError(const TDesC& aDescription,TInt aError)
       
   472 	{
       
   473 	_LIT(KErrorLogFmt,"%S, Error\t%d");
       
   474 	const TInt KWatcherLogBuffer=256;
       
   475 	TBuf<KWatcherLogBuffer> buf;
       
   476 	buf.Format(KErrorLogFmt,&aDescription,aError);
       
   477   	buf.Append('\n');
       
   478 	WPLPrintf(buf);
       
   479 	}
       
   480 
       
   481 
       
   482 
       
   483 
       
   484 // main loop
       
   485 //
       
   486 GLDEF_C TInt E32Main()
       
   487     {
       
   488 __UHEAP_MARK;
       
   489 
       
   490 	// Install exception handler
       
   491 	CTrapCleanup* theCleanup = CTrapCleanup::New(); 
       
   492 	
       
   493 	CTestScheduler* as = new CTestScheduler;
       
   494 	if (!as)
       
   495 		User::Panic(_L("Failed to create active scheduler"),KErrNoMemory);
       
   496 	CActiveScheduler::Install(as); // Install active scheduler
       
   497 
       
   498 	CWapPushTestEngine* engine=NULL;
       
   499 	TRAPD(err,engine = CWapPushTestEngine::NewL());
       
   500 	
       
   501 	if (err != KErrNone)
       
   502 		User::Panic(_L("Failed to create test engine object"),err);
       
   503 	
       
   504 
       
   505 	// All the action happens within the main event loop
       
   506 	CActiveScheduler::Start();
       
   507 	// Returned from start so shutdown
       
   508 	
       
   509 
       
   510 	
       
   511 	//RL: pause a sec to get a gander
       
   512 	engine->Getch();
       
   513 
       
   514 	delete engine;
       
   515 	delete as;
       
   516 	delete theCleanup;
       
   517 __UHEAP_MARKEND;
       
   518 	
       
   519 
       
   520 
       
   521 	User::Heap().Check();
       
   522     return(KErrNone);
       
   523 	}
       
   524 
       
   525 
       
   526