netTest.cpp

Engine/source/platform/test/netTest.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2014 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifdef TORQUE_TESTS_ENABLED
 25#include "testing/unitTesting.h"
 26#include "platform/platformNet.h"
 27#include "core/util/journal/process.h"
 28
 29struct TcpHandle
 30{
 31   NetSocket mSocket;
 32   S32 mDataReceived;
 33
 34   void notify(NetSocket sock, U32 state) 
 35   {
 36      // Only consider our own socket.
 37      if(mSocket != sock)
 38         return;
 39
 40      // Ok - what's the state? We do some dumb responses to given states
 41      // in order to fulfill the request.
 42      if(state == Net::Connected)
 43      {
 44         U8 reqBuffer[] = {
 45            "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
 46         };
 47
 48         Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
 49
 50         ASSERT_EQ(Net::NoError, e)
 51            << "Got an error sending our HTTP request!";
 52      }
 53      else
 54      {
 55         Process::requestShutdown();
 56         mSocket = NetSocket::INVALID;
 57         ASSERT_EQ(Net::Disconnected, state)
 58            << "Ended with a network error!";
 59      }
 60   }
 61
 62   void receive(NetSocket sock, RawData incomingData)
 63   {
 64      // Only consider our own socket.
 65      if(mSocket != sock)
 66         return;
 67
 68      mDataReceived += incomingData.size;
 69   }
 70};
 71
 72TEST(Net, TCPRequest)
 73{
 74   TcpHandle handler;
 75
 76   handler.mSocket = NetSocket::INVALID;
 77   handler.mDataReceived = 0;
 78
 79   // Hook into the signals.
 80   Net::smConnectionNotify ->notify(&handler, &TcpHandle::notify);
 81   Net::smConnectionReceive->notify(&handler, &TcpHandle::receive);
 82
 83   // Open a TCP connection to garagegames.com
 84   handler.mSocket = Net::openConnectTo("72.246.107.193:80");
 85   const U32 limit = Platform::getRealMilliseconds() + (5*1000);
 86   while(Process::processEvents() && (Platform::getRealMilliseconds() < limit) ) {}
 87
 88   // Unhook from the signals.
 89   Net::smConnectionNotify ->remove(&handler, &TcpHandle::notify);
 90   Net::smConnectionReceive->remove(&handler, &TcpHandle::receive);
 91
 92   EXPECT_GT(handler.mDataReceived, 0)
 93      << "Didn't get any data back!";
 94}
 95
 96struct JournalHandle
 97{
 98   NetSocket mSocket;
 99   S32 mDataReceived;
100
101   void notify(NetSocket sock, U32 state)
102   {
103      // Only consider our own socket.
104      if(mSocket != sock)
105         return;
106
107      // Ok - what's the state? We do some dumb responses to given states
108      // in order to fulfill the request.
109      if(state == Net::Connected)
110      {
111         U8 reqBuffer[] = {
112            "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
113         };
114
115         Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
116
117         ASSERT_EQ(Net::NoError, e)
118            << "Got an error sending our HTTP request!";
119      }
120      else
121      {
122         Process::requestShutdown();
123         mSocket = NetSocket::INVALID;
124         ASSERT_EQ(Net::Disconnected, state)
125            << "Ended with a network error!";
126      }
127   }
128
129   void receive(NetSocket sock, RawData incomingData)
130   {
131      // Only consider our own socket.
132      if(mSocket != sock)
133         return;
134      mDataReceived += incomingData.size;
135   }
136
137   void makeRequest()
138   {
139      mSocket = NetSocket::INVALID;
140      mDataReceived = 0;
141
142      // Hook into the signals.
143      Net::smConnectionNotify ->notify(this, &JournalHandle::notify);
144      Net::smConnectionReceive->notify(this, &JournalHandle::receive);
145
146      // Open a TCP connection to garagegames.com
147      mSocket = Net::openConnectTo("72.246.107.193:80");
148
149      // Let the callbacks enable things to process.
150      while(Process::processEvents()) {}
151
152      // Unhook from the signals.
153      Net::smConnectionNotify ->remove(this, &JournalHandle::notify);
154      Net::smConnectionReceive->remove(this, &JournalHandle::receive);
155
156      EXPECT_GT(mDataReceived, 0)
157         << "Didn't get any data back!";
158   }
159};
160
161TEST(Net, JournalTCPRequest)
162{
163   JournalHandle handler;
164
165   Journal::Record("journalTCP.jrn");
166   ASSERT_TRUE(Journal::IsRecording());
167   handler.makeRequest();
168   S32 bytesRead = handler.mDataReceived;
169   Journal::Stop();
170
171   Journal::Play("journalTCP.jrn");
172   handler.makeRequest();
173   Journal::Stop();
174
175   EXPECT_EQ(bytesRead, handler.mDataReceived)
176      << "Didn't get same data back from journal playback.";
177}
178
179#endif
180