testNet.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "platform/platformNet.h"
  23. #include "unit/test.h"
  24. #include "core/util/journal/process.h"
  25. using namespace UnitTesting;
  26. CreateUnitTest( TestTCPRequest, "Platform/Net/TCPRequest")
  27. {
  28. NetSocket mSocket;
  29. S32 mDataRecved;
  30. void handleNotify(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. test(e == Net::NoError, "Got an error sending our HTTP request!");
  44. }
  45. else if(state == Net::Disconnected)
  46. {
  47. Process::requestShutdown();
  48. mSocket = NULL;
  49. }
  50. }
  51. void handleReceive(NetSocket sock, RawData incomingData)
  52. {
  53. // Only consider our own socket.
  54. if(mSocket != sock)
  55. return;
  56. char buff[4096];
  57. dMemcpy(buff, incomingData.data, incomingData.size);
  58. buff[incomingData.size] = 0;
  59. UnitPrint("Got a message...\n");
  60. UnitPrint(buff);
  61. UnitPrint("------\n");
  62. mDataRecved += incomingData.size;
  63. }
  64. void run()
  65. {
  66. mSocket = InvalidSocket;
  67. mDataRecved = 0;
  68. // Initialize networking - done by initLibraries currently
  69. //test(Net::init(), "Failed to initialize networking!");
  70. // Hook into the signals.
  71. Net::smConnectionNotify. notify(this, &TestTCPRequest::handleNotify);
  72. Net::smConnectionReceive.notify(this, &TestTCPRequest::handleReceive);
  73. // Open a TCP connection to garagegames.com
  74. mSocket = Net::openConnectTo("ip:72.246.107.193:80");
  75. while(Process::processEvents())
  76. ;
  77. // Unhook from the signals.
  78. Net::smConnectionNotify. remove(this, &TestTCPRequest::handleNotify);
  79. Net::smConnectionReceive.remove(this, &TestTCPRequest::handleReceive);
  80. test(mDataRecved > 0, "Didn't get any data back!");
  81. }
  82. };
  83. CreateUnitTest( TestTCPRequestJournal, "Platform/Net/JournalTCPRequest")
  84. {
  85. NetSocket mSocket;
  86. S32 mDataRecved;
  87. void handleNotify(NetSocket sock, U32 state)
  88. {
  89. // Only consider our own socket.
  90. if(mSocket != sock)
  91. return;
  92. // Ok - what's the state? We do some dumb responses to given states
  93. // in order to fulfill the request.
  94. if(state == Net::Connected)
  95. {
  96. U8 reqBuffer[] = {
  97. "GET / HTTP/1.0\nUser-Agent: Torque/1.0\n\n"
  98. };
  99. Net::Error e = Net::sendtoSocket(mSocket, reqBuffer, sizeof(reqBuffer));
  100. test(e == Net::NoError, "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 handleReceive(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. UnitPrint("Got a message...\n");
  117. UnitPrint(buff);
  118. UnitPrint("------\n");
  119. mDataRecved += incomingData.size;
  120. }
  121. void makeRequest()
  122. {
  123. mSocket = InvalidSocket;
  124. mDataRecved = 0;
  125. // Initialize networking - done by initLibraries currently
  126. //test(Net::init(), "Failed to initialize networking!");
  127. // Hook into the signals.
  128. Net::smConnectionNotify. notify(this, &TestTCPRequestJournal::handleNotify);
  129. Net::smConnectionReceive.notify(this, &TestTCPRequestJournal::handleReceive);
  130. // Open a TCP connection to garagegames.com
  131. mSocket = Net::openConnectTo("ip:72.246.107.193:80");
  132. // Let the callbacks enable things to process.
  133. while(Process::processEvents())
  134. ;
  135. // Unhook from the signals.
  136. Net::smConnectionNotify. remove(this, &TestTCPRequestJournal::handleNotify);
  137. Net::smConnectionReceive.remove(this, &TestTCPRequestJournal::handleReceive);
  138. test(mDataRecved > 0, "Didn't get any data back!");
  139. }
  140. void run()
  141. {
  142. Journal::Record("journalTCP.jrn");
  143. makeRequest();
  144. S32 bytesRead = mDataRecved;
  145. Journal::Stop();
  146. Journal::Play("journalTCP.jrn");
  147. makeRequest();
  148. Journal::Stop();
  149. test(bytesRead == mDataRecved, "Didn't get same data back from journal playback.");
  150. }
  151. };