enum TVerdict CTestStepNullAgtLoopbackTest::doTestStepL(void)
{
    __UHEAP_MARK;

    TInt r;                // the result of various operations
    TRequestStatus status; // status of asynchronous ops

    RSocketServ server;    // connection paraphanelia
    RConnection connection;
    RSocket socket;

    TInetAddr dest;
    dest.SetAddress(KDummyNifLocalAddressBase + 4);
    dest.SetPort(KPortNo);

    TBuf8<KBufferLength> buffer;

    // connect to the socket server
    r = server.Connect();
    TESTEL(r == KErrNone, r);
    CleanupClosePushL(server);

    // this is why we needed a socket server...
    r = connection.Open(server, KAfInet);
    TESTEL(r == KErrNone, r);
    CleanupClosePushL(connection);

    // start the connection up (outgoing)
    connection.Start(status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());

    // open a udp socket
    r = socket.Open(server, KAfInet, KSockDatagram, KProtocolInetUdp);
    TESTEL(r == KErrNone, r);
    CleanupClosePushL(socket);
    TESTL(socket.SetOpt(KSoReuseAddr, KSolInetIp, 1)==KErrNone);
    // set the source port number - otherwise will panic cos it's zero
    r = socket.SetLocalPort(KPortNo);
    TESTEL(r == KErrNone, r);

    // build some data to send on the socket
    // this is an ICMP ping request apparently
    buffer.SetMax();
    buffer.FillZ();
    buffer[0] = (TUint8) 0x8;		// ICMP type = 8
    buffer[1] = (TUint8) 0x0;		// ICMP code = 0
    buffer[2] = (TUint8) 0xF7;		// ICMP checksum high byte
    buffer[3] = (TUint8) 0xFF;		// ICMP checksum low byte
    // NB the rest of the buffer is zero
    // hence the checksum (0xFFFF - 0x800) since 0x8
    // is the only non-zero element of the buffer

    // send the data out over the socket
    socket.SendTo(buffer, dest, 0, status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());

    buffer.Zero();
    // I expect to get the data looped back from the dummy NIF
    socket.RecvFrom(buffer, dest, 0, status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());

    // check that what we sent is what we got back
    if (status.Int() == KErrNone)
    {
        // if the receive times out and we access buffer we get a panic
        TEST(buffer[0] == 0x08);
        TEST(buffer[1] == 0x00);
        TEST(buffer[2] == 0xF7);
        TEST(buffer[3] == 0xFF);
    }

    // close the socket
    socket.Shutdown(RSocket::ENormal, status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());
    CleanupStack::Pop();

    // force the destruction of the connection
    r = connection.Stop();
    TESTEL(r == KErrNone, r);
    CleanupStack::Pop();

    // close the socket server
    server.Close();
    CleanupStack::Pop();

    __UHEAP_MARKEND;

    return iTestStepResult;
}
Beispiel #2
0
enum TVerdict CTestStepLoopback::doTestStepL()
	{
	SetTestStepResult(EFail);
	
//	const TUid KPktTxKey = {0x104045dd};
//	const TUid KPktRxKey = {0x104045de};
//	const TUid KMeUid = {0x101F529F};
//	TSecurityPolicy readPolicy(ECapability_None);
//	TSecurityPolicy writePolicy(ECapability_None);
//	TInt result = RProperty::Define(KMeUid, KPktTxKey .iUid, RProperty::EInt, readPolicy, writePolicy);
//	result = RProperty::Define(KMeUid, KPktRxKey .iUid, RProperty::EInt, readPolicy, writePolicy);
	
	
	RSocketServ ss;
	CleanupClosePushL(ss);
	Logger().WriteFormat(_L("Start: creating + starting connection"));
	User::LeaveIfError(ss.Connect());
	RConnection conn;
	CleanupClosePushL(conn);
	User::LeaveIfError(conn.Open(ss));
	User::LeaveIfError(conn.Start());

	TInt srvPort;
	if(!GetIntFromConfig(_L("TcpLoop"), _L("Port"), srvPort))
		{
		srvPort = 5002;
		}
	Logger().WriteFormat(_L("Start: creating server socket listening on %d"), srvPort);
	RSocket srv;
	CleanupClosePushL(srv);
	User::LeaveIfError(srv.Open(ss, KAfInet, KSockStream, KProtocolInetTcp, conn));
	TInetAddr srvAddr(KAfInet);
	srvAddr.SetPort(srvPort);
	User::LeaveIfError(srv.Bind(srvAddr));
	User::LeaveIfError(srv.Listen(5));
	RSocket acc;
	CleanupClosePushL(acc);
	User::LeaveIfError(acc.Open(ss));
	TRequestStatus accStat;
	srv.Accept(acc, accStat);
	
	Logger().WriteFormat(_L("Start: connecting client socket"));
	RSocket cli;
	CleanupClosePushL(cli);
	User::LeaveIfError(cli.Open(ss, KAfInet, KSockStream, KProtocolInetTcp, conn));
	srvAddr.SetAddress(0xC0A80707);
	TRequestStatus cliStat;
	cli.Connect(srvAddr, cliStat);
	User::WaitForRequest(cliStat, accStat);
	User::WaitForRequest(cliStat, accStat);
	User::LeaveIfError(cliStat.Int());
	User::LeaveIfError(accStat.Int());

	//_LIT8(KTest, "jackdaws love my big sphinx of quartz");
	
	TInt xferSize = 0;
	if(!GetIntFromConfig(_L("TcpLoop"), _L("xferSize"), xferSize))
		{
		xferSize = 1 * 1000 * 1000;
		}
	TInt fc = User::FastCounter();
	TInt txSize = 0;
	TInt txCnt = 0;
	TRequestStatus txStat(KErrNone);
	TBuf8<4096> txBuf;
	txBuf.SetMax();
	TInt rxSize = 0;
	TInt rxCnt = -1;
	TRequestStatus rxStat(KErrNone);
	TBuf8<4096> rxBuf;
	
	Logger().WriteFormat(_L("Transferring %d bytes"), xferSize);
	while(rxSize < xferSize)
		{
		fc = User::FastCounter();
		if(txStat.Int() != KRequestPending)
			{
			RDebug::Printf("tx status:%d, ", txStat.Int());
			cli.Send(txBuf, 0, txStat);
			++txCnt;
			txSize += txBuf.Length();
			RDebug::Printf("tx #%d, %d, +%d\n", txCnt, fc, txBuf.Length());
			}
		if(rxStat.Int() != KRequestPending)
			{
			RDebug::Printf("rx status:%d, ", rxStat.Int());
			++rxCnt;
			rxSize += rxBuf.Length();
			RDebug::Printf("rx #%d, %d, +%d\n", rxCnt, fc, rxBuf.Length());
			TSockXfrLength dummy;
			acc.RecvOneOrMore(rxBuf, 0, rxStat, dummy);
			}
		User::WaitForRequest(rxStat, txStat);
		}
	
	Logger().WriteFormat(_L("Transferred; %d writes, %d reads"), txCnt, rxCnt);
	
//	RProperty::Get(KUidSystemCategory, KPktTxKey .iUid, txCnt);
//	RProperty::Define(KUidSystemCategory, KPktRxKey .iUid, rxCnt);
//	Logger().WriteFormat(_L("Packet counts; %d sends, %d processes"), txCnt, rxCnt);
	
	
	CleanupStack::PopAndDestroy(5, &ss);
	
	SetTestStepResult(EPass);
	return TestStepResult();
	};