int CRoadMapNativeSocket::GetPortFromService(const char * apService)
{
  RServiceResolver resolver;
  TBuf<256> servName;
  TPortNum portNum;
  
  GSConvert::CharPtrToTDes16(apService, servName);
  TInt error = resolver.Open(*m_pSocketServer, KAfInet, KSockStream, KProtocolInetTcp);

  error = resolver.GetByName(servName, portNum);  
  resolver.Close();
  if ( error == KErrNone )
  {
    return -1;
  }
  
  return portNum();
}
enum TVerdict CSocketTest3_1::InternalDoTestStepL( void )
	{
	TVerdict verdict = EPass;
	
	Logger().WriteFormat(_L("Test Purpose: Basic Service Resolver"));
	
	// connect to esock
	Logger().WriteFormat(_L("Attempting to connect to socket server"));
    RSocketServ ss;
	TInt ret = OptimalConnect(ss);
	CleanupClosePushL(ss);
	Logger().WriteFormat(_L("Connect returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	// find dummy protocol 1
	TProtocolDesc protoInfo;
    Logger().WriteFormat(_L("Finding dummy protocol 1"));
	ret = ss.FindProtocol(_L("Dummy Protocol 1"), protoInfo);
	Logger().WriteFormat(_L("FindProtocol returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	// open a service resolver on it
	Logger().WriteFormat(_L("Open Service resolver"));
	RServiceResolver sr;
	ret = sr.Open(ss, protoInfo.iAddrFamily, protoInfo.iSockType, protoInfo.iProtocol);
	CleanupClosePushL(sr);
	Logger().WriteFormat(_L("Open returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	// try a GetByName
	Logger().WriteFormat(_L("Trying GetByName for DummyName"));
	TPortNum port;
	ret = sr.GetByName(_L("DummyName"), port);
	Logger().WriteFormat(_L("GetByName returned %S, port %d"), &EpocErrorToText(ret), port());
	TESTL(KErrNone == ret);
	TESTL(64 == port());
	
	Logger().WriteFormat(_L("Trying GetByName for Bogus"));
	ret = sr.GetByName(_L("Bogus"), port);
	Logger().WriteFormat(_L("GetByName returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNotFound == ret);
	
	// now try Cancel GetByNumber
	TRequestStatus stat;
	TServiceName service;
	Logger().WriteFormat(_L("Trying GetByNumber for 66"));
	sr.GetByNumber(66, service, stat);
	Logger().WriteFormat(_L("Cancelling GetByNumber"));
	sr.Cancel();
	User::WaitForRequest(stat);
    Logger().WriteFormat(_L("Request status is %S"), &EpocErrorToText(stat.Int()));
	TESTL(KErrCancel == stat.Int());
	
	Logger().WriteFormat(_L("Trying GetByNumber for 21"));
	ret = sr.GetByNumber(21, service);
	Logger().WriteFormat(_L("GetByNumber returned %S, service '%S'"), 
		&EpocErrorToText(ret), &service);
	TESTL(KErrNone == ret);
	TESTL(_L("DummyService") == service);
	
	Logger().WriteFormat(_L("Trying GetByNumber for 22"));
	ret = sr.GetByNumber(22, service);
	Logger().WriteFormat(_L("GetByNumber returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNotFound == ret);
	
	// register and remove service
	Logger().WriteFormat(_L("Trying RegisterService for Simpson"));
	ret = sr.RegisterService(_L("Simpson"),500);
	Logger().WriteFormat(_L("RegisterService returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	Logger().WriteFormat(_L("Trying RegisterService for Bart"));
	ret = sr.RegisterService(_L("Bart"), 10);
	Logger().WriteFormat(_L("RegisterService returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNotFound == ret);
	
	Logger().WriteFormat(_L("Trying to RemoveService Colt"));
	ret = sr.RemoveService(_L("Colt"),45);
	Logger().WriteFormat(_L("RemoveService returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	Logger().WriteFormat(_L("Trying to RemoveService Usi"));
	ret = sr.RemoveService(_L("Usi"), 9);
	Logger().WriteFormat(_L("RemoveService returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNotFound == ret);
	
	Logger().WriteFormat(_L("Close service resolver"));
	CleanupStack::Pop(&sr);
	sr.Close();
	
	CleanupStack::Pop(&ss);
	ss.Close();
	
	SetTestStepResult(verdict);
	return verdict;
	}
Пример #3
0
void CTestStepESockSSA::DoDataThreadL(TDataThreadControl& aControl)
	{
	User::LeaveIfError(aControl.iSession.Connect());

	// Wait for blocker to start blocking; we can then create an IP socket as it will have already loaded
	RProperty blockProp;
	TInt err = blockProp.Attach(TBlockerSID, CBlockerChannelHandler::EBlockingStateKey);
	if(err == KErrNone)
		{
		TRequestStatus status;
		do
			{
			blockProp.Subscribe(status);
			TInt blockState;
			err = blockProp.Get(blockState);
			if(err != KErrNone || blockState >= CBlockerChannelHandler::EStateBlocking)
				{
				blockProp.Cancel();
				}
			User::WaitForRequest(status);
			} while(status == KErrNone);
		blockProp.Close();
		}
	
	switch(aControl.iRequest)
		{
	case TDataThreadControl::ESocketOpen:
		{
		RSocket sock;
		User::LeaveIfError(sock.Open(aControl.iSession, KAfInet, KSockDatagram, KProtocolInetUdp));
		sock.Close();
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = sock.Open(aControl.iSession, KDummyOneName);		// should block
		sock.Close();
		break;
		}
	case TDataThreadControl::EHostResolverOpen:
		{
		RHostResolver hr;
		hr.Open(aControl.iSession, KAfInet, KProtocolInetUdp);
		hr.Close();
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = hr.Open(aControl.iSession, KDummyAddrFamily, KDummyOne);		// should block
		hr.Close();
		break;
		}
	case TDataThreadControl::EHostResolverOpenMulti:
		{
		RHostResolver hr;
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = hr.Open(aControl.iSession, KDummyAddrFamily, KDummyOne);		// should block
		hr.Close();
		break;
		}
	case TDataThreadControl::EServiceResolverOpen:
		{
		RServiceResolver sr;
		sr.Open(aControl.iSession, KAfInet, KSockDatagram, KProtocolInetUdp);
		sr.Close();
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = sr.Open(aControl.iSession, KDummyAddrFamily, KSockDatagram, KDummyOne);		// should block
		sr.Close();
		break;
		}
	case TDataThreadControl::ENetDBOpen:
		{
		RNetDatabase ndb;
		ndb.Open(aControl.iSession, KAfInet, KProtocolInetUdp);
		ndb.Close();
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = ndb.Open(aControl.iSession, KDummyAddrFamily, KDummyOne);		// should block
		ndb.Close();
		break;
		}
	case TDataThreadControl::ENumProtocols:
		{
		TUint numOfProtocols;
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = aControl.iSession.NumProtocols(numOfProtocols);		// should block
		break;
		}
	case TDataThreadControl::EGetProtocolInfo:
		{
		TUint absentIndex = 99;
		TProtocolDesc protocolDesc;
		RHostResolver hr;
		hr.Open(aControl.iSession, KAfInet, KProtocolInetUdp);
		hr.Close();
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = aControl.iSession.GetProtocolInfo(absentIndex, protocolDesc);		// should block
		break;
		}
	case TDataThreadControl::EFindProtocol:
		{
		_LIT(KAbsentProtocolName,"NoSuchProtocol");
		TProtocolDesc protocolDesc;
		RHostResolver hr;
		hr.Open(aControl.iSession, KAfInet, KProtocolInetUdp);
		hr.Close();
		aControl.iBlocked = ETrue;
		aControl.iBlockSemaphore.Signal(1);
		aControl.iResult = aControl.iSession.FindProtocol(KAbsentProtocolName(), protocolDesc);		// should block
		break;
		}
	default:
		ASSERT(0);
		}
	}
enum TVerdict CSocketTest7_3::InternalDoTestStepL( void )
	{
	TVerdict verdict = EPass;
	
	Logger().WriteFormat(_L("Test Purpose: Alloc Heaven for service resolver open"));
	
#if defined (_DEBUG_SOCKET_FUNCTIONS)
	
	// connect to esock
	Logger().WriteFormat(_L("Attempting to connect to socket server"));
    RSocketServ ss;
	TInt ret = OptimalConnect(ss);
	CleanupClosePushL(ss);
	Logger().WriteFormat(_L("Connect returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	// get a protocol
	Logger().WriteFormat(_L("Attempting to FindProtocol dummy protocol 1"));
	TProtocolDesc protoInfo;
	ret = ss.FindProtocol(_L("Dummy Protocol 1"), protoInfo);
	Logger().WriteFormat(_L("FindProtocol returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	// Stretch service resolver arrays in the server.
	RServiceResolver *srs = new (ELeave) RServiceResolver[KNumStretchOpens];
	CleanupArrayDeletePushL(srs);
	TInt i;
	Logger().WriteFormat(_L("Attempting to Open %d service resolvers"), KNumStretchOpens);
	for (i=0; i<KNumStretchOpens; i++)
		{
		ret = srs[i].Open(ss, protoInfo.iAddrFamily, protoInfo.iSockType, protoInfo.iProtocol);
		if (KErrNone != ret)
			{
			Logger().WriteFormat(_L("Open returned %S for service resolver %d"), &EpocErrorToText(ret), i);
			TESTL(EFalse);
			}
		}
	
	Logger().WriteFormat(_L("Closing the first %d service resolvers"), KNumStretchOpens-1);
	for (i=0; i<KNumStretchOpens-1; i++)
		{
		srs[i].Close();
		}
	
	RServiceResolver sr;
	TInt failure = 0;
	ret = -1;
	Logger().WriteFormat(_L("Starting OOM Service Resolver Open Loop"));
	//	ss.__DbgMarkHeap();		// in ESOCKMT leak checking is best done by shutting down the server
	while (ret != KErrNone)
		{
		Logger().WriteFormat(_L("Failing after %d allocs"), failure);
		ss.__DbgFailNext(failure);
		ret = sr.Open(ss, protoInfo.iAddrFamily, protoInfo.iSockType, protoInfo.iProtocol);
		//		if (ret != KErrNone)
		//			{
		//			ss.__DbgCheckHeap(0);
		//			}
		failure++;
		}
	Logger().WriteFormat(_L("Created service resolver OK"));
	
	srs[KNumStretchOpens-1].Close();
	CleanupStack::PopAndDestroy(srs);
	sr.Close();
	//	ss.__DbgMarkEnd(0);
	
	// Flush any FailNext there might be hanging around.
	ss.__DbgFailNext(-1);
	
	CleanupStack::Pop(&ss);
	ss.Close();
#else
	Logger().WriteFormat(_L("TestDisabled on release build."));
	verdict = EInconclusive;
#endif
	
	SetTestStepResult(verdict);
	return verdict;
	}