netTest.cpp 5.7 KB

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