netTest.cpp 5.3 KB

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