messagingfw/msgtestproduct/testutils/src/MessagingTestUtilityServer2.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2006-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 // MessagingTestUtilityServer2 implementation
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 #include <e32std.h>
       
    23 #include "MessagingTestUtilityServer2.h"
       
    24 #include <messagingtestutility2.h>
       
    25 
       
    26 //DoDeleteMessageStore headers:
       
    27 #include "TestUtilityServerObserver.h"
       
    28 
       
    29 //DoCopyFile headers:
       
    30 #include <bautils.h>
       
    31 
       
    32 #include "cmsgtestpropertywatcher.h"
       
    33 #include "tmobilitytestpropertymapaccess.h"
       
    34 #include "mobilitytestpropertyvalue.h"
       
    35 
       
    36 inline CShutdown::CShutdown()
       
    37 	:CTimer(-1)
       
    38 	{CActiveScheduler::Add(this);}
       
    39 inline void CShutdown::ConstructL()
       
    40 	{CTimer::ConstructL();}
       
    41 inline void CShutdown::Start()
       
    42 	{After(KMyShutdownDelay);}
       
    43 
       
    44 inline CMessagingTestUtilityServer2::CMessagingTestUtilityServer2()
       
    45 	:CServer2(0,ESharableSessions)
       
    46 	{}
       
    47 
       
    48 inline CMessagingTestUtilitySession2::CMessagingTestUtilitySession2()
       
    49 : iEventList()
       
    50 	{}
       
    51 
       
    52 inline CMessagingTestUtilityServer2& CMessagingTestUtilitySession2::Server()
       
    53 	{return *static_cast<CMessagingTestUtilityServer2*>(const_cast<CServer2*>(CSession2::Server()));}
       
    54 
       
    55 
       
    56 /**
       
    57 PanicClient()
       
    58 RMessage::Panic() also completes the message. This is:
       
    59 (a) important for efficient cleanup within the kernel
       
    60 (b) a problem if the message is completed a second time
       
    61 
       
    62 @param aMessage
       
    63 @param aPanic
       
    64 */
       
    65 void PanicClient(const RMessage2& aMessage,TTestPanic aPanic)
       
    66 	{
       
    67 	_LIT(KPanic,"MessagingTestUtilityServer2");
       
    68  	aMessage.Panic(KPanic,aPanic);
       
    69 	}
       
    70 
       
    71 
       
    72 /**
       
    73 CreateL()
       
    74 2nd phase construct for sessions - called by the CServer framework
       
    75 */
       
    76 void CMessagingTestUtilitySession2::CreateL()
       
    77 	{
       
    78 	Server().AddSession();
       
    79 
       
    80 	User::LeaveIfError(iFs.Connect());
       
    81 	}
       
    82 
       
    83 
       
    84 /**
       
    85 ~CMessagingTestUtilitySession2()
       
    86 Destructor
       
    87 */
       
    88 CMessagingTestUtilitySession2::~CMessagingTestUtilitySession2()
       
    89 	{
       
    90 	Server().DropSession();
       
    91 
       
    92 	iFs.Close();
       
    93 	delete iDir;
       
    94 
       
    95 	delete iWatcherNetTestStub;
       
    96 
       
    97 	iEventList.ResetAndDestroy();
       
    98 	iEventList.Close();
       
    99 	}
       
   100 
       
   101 
       
   102 /**
       
   103 ServiceL()
       
   104 Entry point for when a new message is received
       
   105 
       
   106 @param aMessage
       
   107 */
       
   108 void CMessagingTestUtilitySession2::ServiceL(const RMessage2& aMessage)
       
   109 	{
       
   110 	TInt result = KErrNone;
       
   111 
       
   112 	switch (aMessage.Function())
       
   113 		{
       
   114 	case EDeleteMessageStore:
       
   115 		result = DoDeleteMessageStoreL(aMessage);
       
   116 		break;
       
   117 	case EKillProcess:
       
   118 		result = DoKillProcessL(aMessage);
       
   119 		break;
       
   120 	case ECopyFile:
       
   121 		result = DoCopyFileL(aMessage);
       
   122 		break;
       
   123 	case EDeleteFile:
       
   124 		result = DoDeleteFileL(aMessage);
       
   125 		break;
       
   126 	case EMkDir:
       
   127 		result = DoMkDirL(aMessage);
       
   128 		break;
       
   129 	case ERmDir:
       
   130 		result = DoRmDirL(aMessage);
       
   131 		break;
       
   132 	case EGetDir:
       
   133 		result = DoGetDirL(aMessage);
       
   134 		break;
       
   135 	case EGetDirCount:
       
   136 		result = DoGetDirCountL(aMessage);
       
   137 		break;
       
   138 	case EGetDirEntry:
       
   139 		result = DoGetDirEntryL(aMessage);
       
   140 		break;
       
   141 	case EFileExists:
       
   142 		result = DoFileExistsL(aMessage);
       
   143 		break;
       
   144 	case EEventHandlerInit:
       
   145 		result = DoEventHandlerInitL();
       
   146 		break;
       
   147 	case EEventHandlerAdd:
       
   148 		result = DoEventHandlerAddL(aMessage);
       
   149 		break;
       
   150 	case EEventHandlerCheck:
       
   151 		result = DoEventHandlerCheck();
       
   152 		break;
       
   153 	case EMount:
       
   154 		{
       
   155 		TInt drive = aMessage.Int0();
       
   156 		TFileName fsName;
       
   157 		result = iFs.FileSystemName(fsName, drive);
       
   158 		result = iFs.MountFileSystem(fsName, drive);
       
   159 		break;
       
   160 		}
       
   161 	case EUnMount:
       
   162 		{
       
   163 		TInt drive = aMessage.Int0();
       
   164 		TFileName fsName;
       
   165 		result = iFs.FileSystemName(fsName, drive);
       
   166 		result = iFs.DismountFileSystem(fsName, drive);
       
   167 		break;
       
   168 		}
       
   169 	default:
       
   170 		PanicClient(aMessage,ETestPanicIllegalFunction);
       
   171 		break;
       
   172 		}
       
   173 	aMessage.Complete(result);
       
   174 	}
       
   175 
       
   176 
       
   177 /**
       
   178 ServiceError()
       
   179 Handle an error from CMySession::ServiceL()
       
   180 A bad descriptor error implies a badly programmed client, so panic it;
       
   181 otherwise use the default handling (report the error to the client)
       
   182 */
       
   183 void CMessagingTestUtilitySession2::ServiceError(const RMessage2& aMessage,TInt aError)
       
   184 	{
       
   185 	if (aError==KErrBadDescriptor)
       
   186 		PanicClient(aMessage,ETestPanicBadDescriptor);
       
   187 	CSession2::ServiceError(aMessage,aError);
       
   188 	}
       
   189 
       
   190 
       
   191 /**
       
   192 RunL()
       
   193 Initiate server exit when the timer expires
       
   194 */
       
   195 void CShutdown::RunL()
       
   196 	{
       
   197 	CActiveScheduler::Stop();
       
   198 	}
       
   199 
       
   200 
       
   201 /**
       
   202 NewLC()
       
   203 static interface for symbian standard 2 phase constructor
       
   204 */
       
   205 CServer2* CMessagingTestUtilityServer2::NewLC()
       
   206 	{
       
   207 	CMessagingTestUtilityServer2* self=new(ELeave) CMessagingTestUtilityServer2;
       
   208 	CleanupStack::PushL(self);
       
   209 	self->ConstructL();
       
   210 	return self;
       
   211 	}
       
   212 
       
   213 
       
   214 /**
       
   215 ConstructL()
       
   216 2nd phase construction - ensure the timer and server objects are running
       
   217 */
       
   218 void CMessagingTestUtilityServer2::ConstructL()
       
   219 	{
       
   220 	StartL(KTestServerName);
       
   221 	iShutdown.ConstructL();
       
   222 	// ensure that the server still exits even if the 1st client fails to connect
       
   223 	iShutdown.Start();
       
   224 	}
       
   225 
       
   226 
       
   227 /**
       
   228 NewSessionL()
       
   229 Create a new client session. This should really check the version number.
       
   230 
       
   231 @return
       
   232 */
       
   233 CSession2* CMessagingTestUtilityServer2::NewSessionL(const TVersion&,const RMessage2&) const
       
   234 	{
       
   235 	return new(ELeave) CMessagingTestUtilitySession2();
       
   236 	}
       
   237 
       
   238 
       
   239 /**
       
   240 AddSession()
       
   241 A new session is being created
       
   242 Cancel the shutdown timer if it was running
       
   243 */
       
   244 void CMessagingTestUtilityServer2::AddSession()
       
   245 	{
       
   246 	++iSessionCount;
       
   247 	iShutdown.Cancel();
       
   248 	}
       
   249 
       
   250 
       
   251 /**
       
   252 DropSession()
       
   253 A session is being destroyed
       
   254 Start the shutdown timer if it is the last session.
       
   255 */
       
   256 void CMessagingTestUtilityServer2::DropSession()
       
   257 	{
       
   258 	if (--iSessionCount==0)
       
   259 		{
       
   260 		iShutdown.Start();
       
   261 		}
       
   262 	}
       
   263 
       
   264 /**
       
   265 DoDeleteMessageStoreL()
       
   266 Functions to service calls to the Messaging Test Utility Server:
       
   267 
       
   268 @param aMessage
       
   269 @return
       
   270 */
       
   271 TInt CMessagingTestUtilitySession2::DoDeleteMessageStoreL(const RMessage2& aMessage)
       
   272 	{
       
   273 	TPckgBuf<TMsvId> msvIdBuf;
       
   274 	aMessage.ReadL(0,msvIdBuf);
       
   275 	TMsvId msvId;
       
   276 	msvId = msvIdBuf();
       
   277 
       
   278 	CTestUtilityServerObserver* ob = CTestUtilityServerObserver::NewLC();
       
   279 
       
   280 	CMsvSession* session = CMsvSession::OpenSyncL(*ob);
       
   281 	CleanupStack::PushL(session);
       
   282 
       
   283 	CMsvEntry* cEntry = CMsvEntry::NewL(*session, msvId, TMsvSelectionOrdering());
       
   284 	CleanupStack::PushL(cEntry);
       
   285 
       
   286 	TInt ret = KErrNone;
       
   287 /*
       
   288 	if (cEntry->HasStoreL())
       
   289 		{
       
   290 		CMsvStore* store = cEntry->EditStoreL();
       
   291 		store->DeleteL();
       
   292 //		test(cEntry->HasStoreL()==EFalse);
       
   293 		}
       
   294 	else
       
   295 		{
       
   296 		ret = KErrInUse;
       
   297 		}
       
   298 */
       
   299 /*#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_TEST_USING_SQLDB)
       
   300 	CMsvStore* store = cEntry->EditDBStoreL(ETrue);
       
   301 #else
       
   302 	CMsvStore* store = cEntry->EditStoreL();	
       
   303 #endif*/
       
   304 
       
   305 	CMsvStore* store = cEntry->EditStoreL();
       
   306 	store->DeleteL();
       
   307 
       
   308 	delete store;
       
   309 
       
   310 	CleanupStack::PopAndDestroy(3); //cEntry, session, ob.
       
   311 
       
   312 	return ret;
       
   313 	}
       
   314 
       
   315 
       
   316 /**
       
   317 DoKillProcessL()
       
   318 Kills a process
       
   319 
       
   320 @param aMessage
       
   321 @return
       
   322 */
       
   323 TInt CMessagingTestUtilitySession2::DoKillProcessL(const RMessage2& aMessage)
       
   324 	{
       
   325 	TPckgBuf<TProcessId> procIdBuf;
       
   326 	aMessage.ReadL(0,procIdBuf);
       
   327 	TProcessId procId;
       
   328 	procId = procIdBuf();
       
   329 
       
   330 	RProcess process;
       
   331 
       
   332 	TInt ret;
       
   333 
       
   334 	ret = process.Open(procId);
       
   335 
       
   336 	if (ret != KErrNone)
       
   337 		return ret;
       
   338 
       
   339 	if (User::ProcessCritical() != User::ENotCritical)
       
   340 		ret = KErrPermissionDenied;
       
   341 	else
       
   342 		process.Kill(KErrNone);
       
   343 
       
   344 	process.Close();
       
   345 
       
   346 	return ret;
       
   347 	}
       
   348 
       
   349 
       
   350 /**
       
   351 DoCopyFileL()
       
   352 Copies the file from source to the destination
       
   353 
       
   354 @param aMessage
       
   355 @return
       
   356 */
       
   357 TInt CMessagingTestUtilitySession2::DoCopyFileL(const RMessage2& aMessage)
       
   358 	{
       
   359 	TBuf<255> srcPath, destPath;
       
   360 	aMessage.ReadL(0, srcPath);
       
   361 	aMessage.ReadL(1, destPath);
       
   362 
       
   363 	TInt ret = BaflUtils::CopyFile(iFs, srcPath, destPath);
       
   364 
       
   365 	return ret;
       
   366 	}
       
   367 
       
   368 
       
   369 /**
       
   370 DoDeleteFileL()
       
   371 Delete the file
       
   372 
       
   373 @param aMessage
       
   374 @return
       
   375 */
       
   376 TInt CMessagingTestUtilitySession2::DoDeleteFileL(const RMessage2& aMessage)
       
   377 	{
       
   378 	TBuf<255> filePath;
       
   379 	aMessage.ReadL(0, filePath);
       
   380 
       
   381 	TInt ret = iFs.Delete(filePath);
       
   382 
       
   383 	return ret;
       
   384 	}
       
   385 
       
   386 
       
   387 /**
       
   388 DoMkDirL()
       
   389 Creates a directory
       
   390 
       
   391 @param aMessage
       
   392 @return
       
   393 */
       
   394 TInt CMessagingTestUtilitySession2::DoMkDirL(const RMessage2& aMessage)
       
   395 	{
       
   396 	TBuf<255> dirPath;
       
   397 	aMessage.ReadL(0, dirPath);
       
   398 
       
   399 	TInt ret = iFs.MkDirAll(dirPath);
       
   400 
       
   401 	return ret;
       
   402 	}
       
   403 
       
   404 
       
   405 /**
       
   406 DoRmDirL()
       
   407 Removes a directory
       
   408 
       
   409 @param aMessage
       
   410 @return
       
   411 */
       
   412 TInt CMessagingTestUtilitySession2::DoRmDirL(const RMessage2& aMessage)
       
   413 	{
       
   414 	TBuf<255> dirPath;
       
   415 	aMessage.ReadL(0, dirPath);
       
   416 
       
   417 	TInt ret = iFs.RmDir(dirPath);
       
   418 
       
   419 	return ret;
       
   420 	}
       
   421 
       
   422 
       
   423 /**
       
   424 DoGetDirL()
       
   425 Gets a directory
       
   426 
       
   427 @param aMessage
       
   428 @return
       
   429 */
       
   430 TInt CMessagingTestUtilitySession2::DoGetDirL(const RMessage2& aMessage)
       
   431 	{
       
   432 	TBuf<255> dirPath;
       
   433 	aMessage.ReadL(0, dirPath);
       
   434 
       
   435 	TPckgBuf<TUint> entryAttMaskBuf;
       
   436 	aMessage.ReadL(1,entryAttMaskBuf);
       
   437 	TUint entryAttMask = entryAttMaskBuf();
       
   438 
       
   439 	TPckgBuf<TUint> entrySortKeyBuf;
       
   440 	aMessage.ReadL(2,entrySortKeyBuf);
       
   441 	TUint entrySortKey = entrySortKeyBuf();
       
   442 
       
   443 	delete iDir; //clean up first.
       
   444 
       
   445 	// Hold the CDir* in the Session object.
       
   446 	// Used by calls to DoGetDirCount() and DoGetDirEntry().
       
   447 	TInt ret = iFs.GetDir(dirPath, entryAttMask, entrySortKey, iDir);
       
   448 
       
   449 	return ret;
       
   450 	}
       
   451 
       
   452 
       
   453 /**
       
   454 DoGetDirCountL()
       
   455 Gets a directory counts
       
   456 
       
   457 @param aMessage
       
   458 @return
       
   459 */
       
   460 TInt CMessagingTestUtilitySession2::DoGetDirCountL(const RMessage2& aMessage)
       
   461 	{
       
   462 	TUint count =0;
       
   463 
       
   464 	if(iDir != 0)
       
   465 	{
       
   466 		count = iDir->Count();
       
   467 
       
   468 		TPckgBuf<TUint> countBuf(count);
       
   469 		aMessage.WriteL(0, countBuf);
       
   470 		return KErrNone;
       
   471 	}
       
   472 
       
   473 	return KErrNotFound;
       
   474 	}
       
   475 
       
   476 
       
   477 /**
       
   478 DoGetDirEntryL()
       
   479 Gets a directory entry
       
   480 
       
   481 @param aMessage
       
   482 @return
       
   483 */
       
   484 TInt CMessagingTestUtilitySession2::DoGetDirEntryL(const RMessage2& aMessage)
       
   485 	{
       
   486 	TPckgBuf<TInt> indexBuf;
       
   487 	aMessage.ReadL(0,indexBuf);
       
   488 	TInt index = indexBuf();
       
   489 
       
   490 	TEntry entry;
       
   491 
       
   492 	if(iDir != 0)
       
   493 	{
       
   494 		entry = iDir->operator[](index);
       
   495 
       
   496 		TPckgBuf<TEntry> entryBuf(entry);
       
   497 		aMessage.WriteL(1, entryBuf);
       
   498 		return KErrNone;
       
   499 	}
       
   500 
       
   501 	return KErrNotFound;
       
   502 	}
       
   503 
       
   504 
       
   505 /**
       
   506 DoFileExistsL()
       
   507 checks if the file exist
       
   508 
       
   509 @param aMessage
       
   510 @return
       
   511 */
       
   512 TBool CMessagingTestUtilitySession2::DoFileExistsL(const RMessage2& aMessage)
       
   513 	{
       
   514 	TBuf<255> filePath;
       
   515 	aMessage.ReadL(0, filePath);
       
   516 
       
   517 	TBool ret = BaflUtils::FileExists(iFs, filePath);
       
   518 
       
   519 	return ret;
       
   520 	}
       
   521 
       
   522 
       
   523 //EventHandler
       
   524 
       
   525 #ifdef __MOBILITY_TEST_FRAMEWORK
       
   526 void CMessagingTestUtilitySession2::HandleEventL(RProperty& aProperty, TUint /*aKey*/)
       
   527 #else //__MOBILITY_TEST_FRAMEWORK
       
   528 void CMessagingTestUtilitySession2::HandleEventL(RProperty& /*aProperty*/, TUint /*aKey*/)
       
   529 #endif //__MOBILITY_TEST_FRAMEWORK
       
   530 	{
       
   531 #ifdef __MOBILITY_TEST_FRAMEWORK
       
   532 
       
   533 //Check the event is expected...
       
   534 //	-take the first event from the list.
       
   535 //	-if it matches (flag) then set the next event (call).
       
   536 //	-then remove it from the list.
       
   537 
       
   538 	TInt propertyVal = 0;
       
   539 	User::LeaveIfError(aProperty.Get(propertyVal));
       
   540 
       
   541 	const TMsgTestPropertyMapEntry* propertyMap = TMobilityTestPropertyMapAccess::Get(propertyVal);
       
   542 
       
   543 	if(propertyMap == NULL)
       
   544 		{
       
   545 		User::Leave(KErrNotFound);
       
   546 		}
       
   547 
       
   548 	if(iEventList.Count() > 0)
       
   549 		{
       
   550 		CMsgTestPropertyMapPair* ptr = iEventList[0];
       
   551 
       
   552 		if(propertyMap->value == ptr->flag->value)
       
   553 			{
       
   554 			aProperty.Set(KPropertyCatMsgTestUtilityServer2, EMyPropertyCounter, ptr->call->value);
       
   555 
       
   556 			iEventList.Remove(0);
       
   557 			delete ptr;
       
   558 			}
       
   559 		}
       
   560 
       
   561 #endif //__MOBILITY_TEST_FRAMEWORK
       
   562 	}
       
   563 
       
   564 /**
       
   565 DoEventHandlerInitL()
       
   566 Initialise event handler.
       
   567 
       
   568 @param aMessage
       
   569 @return
       
   570 */
       
   571 TInt CMessagingTestUtilitySession2::DoEventHandlerInitL()
       
   572 	{
       
   573 #ifdef __MOBILITY_TEST_FRAMEWORK
       
   574 
       
   575 	RProcess me;
       
   576 	me.SetPriority(EPriorityHigh);
       
   577 	User::SetPriorityControl(EFalse);
       
   578 
       
   579 	TInt r=RProperty::Define(EMyPropertyCounter,RProperty::EInt,KAllowAllPolicy,KAllowAllPolicy);
       
   580 	if (r!=KErrAlreadyExists)
       
   581 	    {
       
   582 	    User::LeaveIfError(r);
       
   583 	    }
       
   584 
       
   585 	delete iWatcherNetTestStub;
       
   586 	iWatcherNetTestStub = NULL;
       
   587 	iWatcherNetTestStub = CMsgTestPropertyWatcher::NewL(KPropertyCatMsgTestUtilityServer2, EMyPropertyCounter, *this);
       
   588 
       
   589 	iEventList.ResetAndDestroy();
       
   590 
       
   591 #endif //__MOBILITY_TEST_FRAMEWORK
       
   592 
       
   593 	return KErrNone;
       
   594 	}
       
   595 
       
   596 /**
       
   597 DoEventHandlerAdd()
       
   598 Add an event handler.
       
   599 
       
   600 @param aMessage
       
   601 @return
       
   602 */
       
   603 #ifdef __MOBILITY_TEST_FRAMEWORK
       
   604 TInt CMessagingTestUtilitySession2::DoEventHandlerAddL(const RMessage2& aMessage)
       
   605 #else //__MOBILITY_TEST_FRAMEWORK
       
   606 TInt CMessagingTestUtilitySession2::DoEventHandlerAddL(const RMessage2& /*aMessage*/)
       
   607 #endif //__MOBILITY_TEST_FRAMEWORK
       
   608 	{
       
   609 #ifdef __MOBILITY_TEST_FRAMEWORK
       
   610 
       
   611 	TBuf<255> buf;
       
   612 
       
   613 	aMessage.ReadL(0, buf);
       
   614 	const TMsgTestPropertyMapEntry* propertyMapFlag = TMobilityTestPropertyMapAccess::Get(&buf);
       
   615 
       
   616 	aMessage.ReadL(1, buf);
       
   617 	const TMsgTestPropertyMapEntry* propertyMapCall = TMobilityTestPropertyMapAccess::Get(&buf);
       
   618 
       
   619 	CMsgTestPropertyMapPair* propertyMapPair = new(ELeave) CMsgTestPropertyMapPair(const_cast<TMsgTestPropertyMapEntry*>(propertyMapFlag), const_cast<TMsgTestPropertyMapEntry*>(propertyMapCall));
       
   620 
       
   621 	if((propertyMapPair->call == NULL) || (propertyMapPair->flag == NULL))
       
   622 		{
       
   623 		return KErrArgument;
       
   624 		}
       
   625 
       
   626 	iEventList.Append(propertyMapPair);
       
   627 
       
   628 #endif //__MOBILITY_TEST_FRAMEWORK
       
   629 
       
   630 	return KErrNone;
       
   631 	}
       
   632 
       
   633 /**
       
   634 DoEventHandlerCheck()
       
   635 Check event handler.
       
   636 
       
   637 @param aMessage
       
   638 @return Number of events left in list
       
   639 */
       
   640 TInt CMessagingTestUtilitySession2::DoEventHandlerCheck()
       
   641 	{
       
   642 	TInt numEvents = 0;
       
   643 
       
   644 #ifdef __MOBILITY_TEST_FRAMEWORK
       
   645 
       
   646 	numEvents = iEventList.Count();
       
   647 	iEventList.ResetAndDestroy();
       
   648 
       
   649 #endif //__MOBILITY_TEST_FRAMEWORK
       
   650 
       
   651 	return numEvents;
       
   652 	}