Torque3D/Engine/source/platform/test/netTest.cpp

179 lines
5.3 KiB
C++
Raw Normal View History

2014-08-08 03:17:14 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2014 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifdef TORQUE_TESTS_ENABLED
#include "testing/unitTesting.h"
#include "platform/platformNet.h"
#include "core/util/journal/process.h"
2015-02-10 23:17:19 +00:00
struct TcpHandle
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
NetSocket mSocket;
S32 mDataReceived;
void notify(NetSocket sock, U32 state)
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
// Only consider our own socket.
if(mSocket != sock)
return;
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
// Ok - what's the state? We do some dumb responses to given states
// in order to fulfill the request.
if(state == Net::Connected)
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
U8 reqBuffer[] = {
"GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
};
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
ASSERT_EQ(Net::NoError, e)
<< "Got an error sending our HTTP request!";
2014-08-08 03:17:14 +00:00
}
2015-02-10 23:17:19 +00:00
else
{
Process::requestShutdown();
2016-09-10 22:01:10 +00:00
mSocket = NetSocket::INVALID;
2015-02-10 23:17:19 +00:00
ASSERT_EQ(Net::Disconnected, state)
<< "Ended with a network error!";
}
}
void receive(NetSocket sock, RawData incomingData)
{
// Only consider our own socket.
if(mSocket != sock)
return;
mDataReceived += incomingData.size;
}
};
TEST(Net, TCPRequest)
{
TcpHandle handler;
2014-08-08 03:17:14 +00:00
2016-09-10 22:01:10 +00:00
handler.mSocket = NetSocket::INVALID;
2014-08-08 03:17:14 +00:00
handler.mDataReceived = 0;
// Hook into the signals.
Net::smConnectionNotify ->notify(&handler, &TcpHandle::notify);
Net::smConnectionReceive->notify(&handler, &TcpHandle::receive);
2014-08-08 03:17:14 +00:00
// Open a TCP connection to garagegames.com
handler.mSocket = Net::openConnectTo("72.246.107.193:80");
const U32 limit = Platform::getRealMilliseconds() + (5*1000);
while(Process::processEvents() && (Platform::getRealMilliseconds() < limit) ) {}
// Unhook from the signals.
Net::smConnectionNotify ->remove(&handler, &TcpHandle::notify);
Net::smConnectionReceive->remove(&handler, &TcpHandle::receive);
2014-08-08 03:17:14 +00:00
EXPECT_GT(handler.mDataReceived, 0)
<< "Didn't get any data back!";
}
2015-02-10 23:17:19 +00:00
struct JournalHandle
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
NetSocket mSocket;
S32 mDataReceived;
void notify(NetSocket sock, U32 state)
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
// Only consider our own socket.
if(mSocket != sock)
return;
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
// Ok - what's the state? We do some dumb responses to given states
// in order to fulfill the request.
if(state == Net::Connected)
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
U8 reqBuffer[] = {
"GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
};
Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
ASSERT_EQ(Net::NoError, e)
<< "Got an error sending our HTTP request!";
}
else
2014-08-08 03:17:14 +00:00
{
2015-02-10 23:17:19 +00:00
Process::requestShutdown();
2016-09-10 22:01:10 +00:00
mSocket = NetSocket::INVALID;
2015-02-10 23:17:19 +00:00
ASSERT_EQ(Net::Disconnected, state)
<< "Ended with a network error!";
2014-08-08 03:17:14 +00:00
}
2015-02-10 23:17:19 +00:00
}
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
void receive(NetSocket sock, RawData incomingData)
{
// Only consider our own socket.
if(mSocket != sock)
return;
mDataReceived += incomingData.size;
}
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
void makeRequest()
{
2016-09-10 22:01:10 +00:00
mSocket = NetSocket::INVALID;
2015-02-10 23:17:19 +00:00
mDataReceived = 0;
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
// Hook into the signals.
Net::smConnectionNotify ->notify(this, &JournalHandle::notify);
Net::smConnectionReceive->notify(this, &JournalHandle::receive);
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
// Open a TCP connection to garagegames.com
mSocket = Net::openConnectTo("72.246.107.193:80");
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
// Let the callbacks enable things to process.
while(Process::processEvents()) {}
2014-08-08 03:17:14 +00:00
2015-02-10 23:17:19 +00:00
// Unhook from the signals.
Net::smConnectionNotify ->remove(this, &JournalHandle::notify);
Net::smConnectionReceive->remove(this, &JournalHandle::receive);
2015-02-10 23:17:19 +00:00
EXPECT_GT(mDataReceived, 0)
<< "Didn't get any data back!";
}
};
TEST(Net, JournalTCPRequest)
{
JournalHandle handler;
2014-08-08 03:17:14 +00:00
Journal::Record("journalTCP.jrn");
ASSERT_TRUE(Journal::IsRecording());
handler.makeRequest();
S32 bytesRead = handler.mDataReceived;
Journal::Stop();
Journal::Play("journalTCP.jrn");
handler.makeRequest();
Journal::Stop();
EXPECT_EQ(bytesRead, handler.mDataReceived)
<< "Didn't get same data back from journal playback.";
}
2016-09-10 22:01:10 +00:00
#endif