TestSocketTransport.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "TestCommon.h"
  5. #include "nsIComponentRegistrar.h"
  6. #include "nsPISocketTransportService.h"
  7. #include "nsISocketTransport.h"
  8. #include "nsIAsyncInputStream.h"
  9. #include "nsIAsyncOutputStream.h"
  10. #include "nsIProgressEventSink.h"
  11. #include "nsIInterfaceRequestor.h"
  12. #include "nsIInterfaceRequestorUtils.h"
  13. #include "nsIRequest.h"
  14. #include "nsIServiceManager.h"
  15. #include "nsIComponentManager.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsMemory.h"
  18. #include "nsStringAPI.h"
  19. #include "nsIDNSService.h"
  20. #include "nsIFileStreams.h"
  21. #include "nsIStreamListener.h"
  22. #include "nsIFile.h"
  23. #include "nsAutoLock.h"
  24. #include "mozilla/Logging.h"
  25. ////////////////////////////////////////////////////////////////////////////////
  26. //
  27. // set NSPR_LOG_MODULES=Test:5
  28. //
  29. static PRLogModuleInfo *gTestLog = nullptr;
  30. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  31. ////////////////////////////////////////////////////////////////////////////////
  32. static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
  33. ////////////////////////////////////////////////////////////////////////////////
  34. class MyHandler : public nsIOutputStreamCallback
  35. , public nsIInputStreamCallback
  36. {
  37. public:
  38. NS_DECL_THREADSAFE_ISUPPORTS
  39. MyHandler(const char *path,
  40. nsIAsyncInputStream *in,
  41. nsIAsyncOutputStream *out)
  42. : mInput(in)
  43. , mOutput(out)
  44. , mWriteOffset(0)
  45. {
  46. mBuf.AssignLiteral("GET ");
  47. mBuf.Append(path);
  48. mBuf.AppendLiteral(" HTTP/1.0\r\n\r\n");
  49. }
  50. virtual ~MyHandler() {}
  51. // called on any thread
  52. NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *out)
  53. {
  54. LOG(("OnOutputStreamReady\n"));
  55. nsresult rv;
  56. uint32_t n, count = mBuf.Length() - mWriteOffset;
  57. rv = out->Write(mBuf.get() + mWriteOffset, count, &n);
  58. LOG((" write returned [rv=%x count=%u]\n", rv, n));
  59. if (NS_FAILED(rv) || (n == 0)) {
  60. if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
  61. LOG((" done writing; starting to read\n"));
  62. mInput->AsyncWait(this, 0, 0, nullptr);
  63. return NS_OK;
  64. }
  65. }
  66. mWriteOffset += n;
  67. return out->AsyncWait(this, 0, 0, nullptr);
  68. }
  69. // called on any thread
  70. NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *in)
  71. {
  72. LOG(("OnInputStreamReady\n"));
  73. nsresult rv;
  74. uint32_t n;
  75. char buf[500];
  76. rv = in->Read(buf, sizeof(buf), &n);
  77. LOG((" read returned [rv=%x count=%u]\n", rv, n));
  78. if (NS_FAILED(rv) || (n == 0)) {
  79. if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
  80. QuitPumpingEvents();
  81. return NS_OK;
  82. }
  83. }
  84. return in->AsyncWait(this, 0, 0, nullptr);
  85. }
  86. private:
  87. nsCOMPtr<nsIAsyncInputStream> mInput;
  88. nsCOMPtr<nsIAsyncOutputStream> mOutput;
  89. nsCString mBuf;
  90. uint32_t mWriteOffset;
  91. };
  92. NS_IMPL_ISUPPORTS(MyHandler,
  93. nsIOutputStreamCallback,
  94. nsIInputStreamCallback)
  95. ////////////////////////////////////////////////////////////////////////////////
  96. /**
  97. * create transport, open streams, and close
  98. */
  99. static nsresult
  100. RunCloseTest(nsISocketTransportService *sts,
  101. const char *host, int port,
  102. uint32_t inFlags, uint32_t outFlags)
  103. {
  104. nsresult rv;
  105. LOG(("RunCloseTest\n"));
  106. nsCOMPtr<nsISocketTransport> transport;
  107. rv = sts->CreateTransport(nullptr, 0,
  108. nsDependentCString(host), port, nullptr,
  109. getter_AddRefs(transport));
  110. if (NS_FAILED(rv)) return rv;
  111. nsCOMPtr<nsIInputStream> in;
  112. rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
  113. nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
  114. if (NS_FAILED(rv)) return rv;
  115. nsCOMPtr<nsIOutputStream> out;
  116. rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
  117. nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
  118. if (NS_FAILED(rv)) return rv;
  119. LOG(("waiting 1 second before closing transport and streams...\n"));
  120. PR_Sleep(PR_SecondsToInterval(1));
  121. // let nsCOMPtr destructors close everything...
  122. return NS_OK;
  123. }
  124. /**
  125. * asynchronously read socket stream
  126. */
  127. static nsresult
  128. RunTest(nsISocketTransportService *sts,
  129. const char *host, int port, const char *path,
  130. uint32_t inFlags, uint32_t outFlags)
  131. {
  132. nsresult rv;
  133. LOG(("RunTest\n"));
  134. nsCOMPtr<nsISocketTransport> transport;
  135. rv = sts->CreateTransport(nullptr, 0,
  136. nsDependentCString(host), port, nullptr,
  137. getter_AddRefs(transport));
  138. if (NS_FAILED(rv)) return rv;
  139. nsCOMPtr<nsIInputStream> in;
  140. rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
  141. nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
  142. if (NS_FAILED(rv)) return rv;
  143. nsCOMPtr<nsIOutputStream> out;
  144. rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
  145. nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
  146. if (NS_FAILED(rv)) return rv;
  147. MyHandler *handler = new MyHandler(path, asyncIn, asyncOut);
  148. if (handler == nullptr)
  149. return NS_ERROR_OUT_OF_MEMORY;
  150. NS_ADDREF(handler);
  151. rv = asyncOut->AsyncWait(handler, 0, 0, nullptr);
  152. if (NS_SUCCEEDED(rv))
  153. PumpEvents();
  154. NS_RELEASE(handler);
  155. return NS_OK;
  156. }
  157. ////////////////////////////////////////////////////////////////////////////////
  158. int
  159. main(int argc, char* argv[])
  160. {
  161. if (test_common_init(&argc, &argv) != 0)
  162. return -1;
  163. nsresult rv;
  164. if (argc < 4) {
  165. printf("usage: TestSocketTransport <host> <port> <path>\n");
  166. return -1;
  167. }
  168. {
  169. nsCOMPtr<nsIServiceManager> servMan;
  170. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  171. nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  172. NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
  173. if (registrar)
  174. registrar->AutoRegister(nullptr);
  175. gTestLog = PR_NewLogModule("Test");
  176. // Make sure the DNS service is initialized on the main thread
  177. nsCOMPtr<nsIDNSService> dns =
  178. do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
  179. if (NS_FAILED(rv)) return rv;
  180. nsCOMPtr<nsPISocketTransportService> sts =
  181. do_GetService(kSocketTransportServiceCID, &rv);
  182. if (NS_FAILED(rv)) return rv;
  183. LOG(("phase 1 tests...\n"));
  184. LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
  185. rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
  186. nsITransport::OPEN_UNBUFFERED,
  187. nsITransport::OPEN_UNBUFFERED);
  188. NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
  189. LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
  190. rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
  191. 0 /* nsITransport::OPEN_BUFFERED */,
  192. nsITransport::OPEN_UNBUFFERED);
  193. NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
  194. LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
  195. rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
  196. nsITransport::OPEN_UNBUFFERED,
  197. 0 /*nsITransport::OPEN_BUFFERED */);
  198. NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
  199. LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
  200. rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
  201. 0 /*nsITransport::OPEN_BUFFERED */,
  202. 0 /*nsITransport::OPEN_BUFFERED */);
  203. NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
  204. LOG(("calling Shutdown on socket transport service:\n"));
  205. sts->Shutdown();
  206. LOG(("calling Init on socket transport service:\n"));
  207. sts->Init();
  208. LOG(("phase 2 tests...\n"));
  209. LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
  210. rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
  211. nsITransport::OPEN_UNBUFFERED,
  212. nsITransport::OPEN_UNBUFFERED);
  213. NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
  214. LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
  215. rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
  216. 0 /* nsITransport::OPEN_BUFFERED */,
  217. nsITransport::OPEN_UNBUFFERED);
  218. NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
  219. LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
  220. rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
  221. nsITransport::OPEN_UNBUFFERED,
  222. 0 /*nsITransport::OPEN_BUFFERED */);
  223. NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
  224. LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
  225. rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
  226. 0 /*nsITransport::OPEN_BUFFERED */,
  227. 0 /*nsITransport::OPEN_BUFFERED */);
  228. NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
  229. LOG(("waiting 1 second before calling Shutdown...\n"));
  230. PR_Sleep(PR_SecondsToInterval(1));
  231. LOG(("calling Shutdown on socket transport service:\n"));
  232. sts->Shutdown();
  233. // give background threads a chance to finish whatever work they may
  234. // be doing.
  235. LOG(("waiting 1 second before exiting...\n"));
  236. PR_Sleep(PR_SecondsToInterval(1));
  237. } // this scopes the nsCOMPtrs
  238. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  239. rv = NS_ShutdownXPCOM(nullptr);
  240. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  241. return 0;
  242. }