netTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. TEST(Net, TCPRequest)
  27. {
  28. struct handle
  29. {
  30. NetSocket mSocket;
  31. S32 mDataReceived;
  32. void notify(NetSocket sock, U32 state)
  33. {
  34. // Only consider our own socket.
  35. if(mSocket != sock)
  36. return;
  37. // Ok - what's the state? We do some dumb responses to given states
  38. // in order to fulfill the request.
  39. if(state == Net::Connected)
  40. {
  41. U8 reqBuffer[] = {
  42. "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
  43. };
  44. Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
  45. ASSERT_EQ(Net::NoError, e)
  46. << "Got an error sending our HTTP request!";
  47. }
  48. else
  49. {
  50. Process::requestShutdown();
  51. mSocket = NULL;
  52. ASSERT_EQ(Net::Disconnected, state)
  53. << "Ended with a network error!";
  54. }
  55. }
  56. void receive(NetSocket sock, RawData incomingData)
  57. {
  58. // Only consider our own socket.
  59. if(mSocket != sock)
  60. return;
  61. mDataReceived += incomingData.size;
  62. }
  63. } handler;
  64. handler.mSocket = InvalidSocket;
  65. handler.mDataReceived = 0;
  66. // Hook into the signals.
  67. Net::smConnectionNotify. notify(&handler, &handle::notify);
  68. Net::smConnectionReceive.notify(&handler, &handle::receive);
  69. // Open a TCP connection to garagegames.com
  70. handler.mSocket = Net::openConnectTo("72.246.107.193:80");
  71. const U32 limit = Platform::getRealMilliseconds() + (5*1000);
  72. while(Process::processEvents() && (Platform::getRealMilliseconds() < limit) ) {}
  73. // Unhook from the signals.
  74. Net::smConnectionNotify. remove(&handler, &handle::notify);
  75. Net::smConnectionReceive.remove(&handler, &handle::receive);
  76. EXPECT_GT(handler.mDataReceived, 0)
  77. << "Didn't get any data back!";
  78. }
  79. TEST(Net, JournalTCPRequest)
  80. {
  81. struct handle
  82. {
  83. NetSocket mSocket;
  84. S32 mDataReceived;
  85. void notify(NetSocket sock, U32 state)
  86. {
  87. // Only consider our own socket.
  88. if(mSocket != sock)
  89. return;
  90. // Ok - what's the state? We do some dumb responses to given states
  91. // in order to fulfill the request.
  92. if(state == Net::Connected)
  93. {
  94. U8 reqBuffer[] = {
  95. "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
  96. };
  97. Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
  98. ASSERT_EQ(Net::NoError, e)
  99. << "Got an error sending our HTTP request!";
  100. }
  101. else
  102. {
  103. Process::requestShutdown();
  104. mSocket = NULL;
  105. ASSERT_EQ(Net::Disconnected, state)
  106. << "Ended with a network error!";
  107. }
  108. }
  109. void receive(NetSocket sock, RawData incomingData)
  110. {
  111. // Only consider our own socket.
  112. if(mSocket != sock)
  113. return;
  114. mDataReceived += incomingData.size;
  115. }
  116. void makeRequest()
  117. {
  118. mSocket = InvalidSocket;
  119. mDataReceived = 0;
  120. // Hook into the signals.
  121. Net::smConnectionNotify. notify(this, &handle::notify);
  122. Net::smConnectionReceive.notify(this, &handle::receive);
  123. // Open a TCP connection to garagegames.com
  124. mSocket = Net::openConnectTo("72.246.107.193:80");
  125. // Let the callbacks enable things to process.
  126. while(Process::processEvents()) {}
  127. // Unhook from the signals.
  128. Net::smConnectionNotify. remove(this, &handle::notify);
  129. Net::smConnectionReceive.remove(this, &handle::receive);
  130. EXPECT_GT(mDataReceived, 0)
  131. << "Didn't get any data back!";
  132. }
  133. } handler;
  134. Journal::Record("journalTCP.jrn");
  135. ASSERT_TRUE(Journal::IsRecording());
  136. handler.makeRequest();
  137. S32 bytesRead = handler.mDataReceived;
  138. Journal::Stop();
  139. Journal::Play("journalTCP.jrn");
  140. handler.makeRequest();
  141. Journal::Stop();
  142. EXPECT_EQ(bytesRead, handler.mDataReceived)
  143. << "Didn't get same data back from journal playback.";
  144. }
  145. #endif