TestStreamTransport.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "nsIStreamTransportService.h"
  7. #include "nsIAsyncInputStream.h"
  8. #include "nsIProgressEventSink.h"
  9. #include "nsIInterfaceRequestor.h"
  10. #include "nsIInterfaceRequestorUtils.h"
  11. #include "nsIRequest.h"
  12. #include "nsIServiceManager.h"
  13. #include "nsIComponentManager.h"
  14. #include "nsCOMPtr.h"
  15. #include "nsMemory.h"
  16. #include "nsStringAPI.h"
  17. #include "nsIFileStreams.h"
  18. #include "nsIStreamListener.h"
  19. #include "nsIFile.h"
  20. #include "nsNetUtil.h"
  21. #include "nsAutoLock.h"
  22. #include "mozilla/Logging.h"
  23. #include "prenv.h"
  24. ////////////////////////////////////////////////////////////////////////////////
  25. //
  26. // set NSPR_LOG_MODULES=Test:5
  27. //
  28. static PRLogModuleInfo *gTestLog = nullptr;
  29. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  30. ////////////////////////////////////////////////////////////////////////////////
  31. static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
  32. ////////////////////////////////////////////////////////////////////////////////
  33. #define CHUNK_SIZE 500
  34. class MyCopier : public nsIInputStreamCallback
  35. , public nsIOutputStreamCallback
  36. {
  37. public:
  38. NS_DECL_THREADSAFE_ISUPPORTS
  39. MyCopier()
  40. : mLock(nullptr)
  41. , mInputCondition(NS_OK)
  42. {
  43. }
  44. virtual ~MyCopier()
  45. {
  46. if (mLock)
  47. nsAutoLock::DestroyLock(mLock);
  48. if (mInput)
  49. mInput->Close();
  50. if (mOutput)
  51. mOutput->Close();
  52. }
  53. // called on any thread
  54. NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *inStr)
  55. {
  56. LOG(("OnInputStreamReady\n"));
  57. nsAutoLock lock(mLock);
  58. NS_ASSERTION(inStr == mInput, "unexpected stream");
  59. Process_Locked();
  60. return NS_OK;
  61. }
  62. // called on any thread
  63. NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *outStr)
  64. {
  65. LOG(("OnOutputStreamReady\n"));
  66. nsAutoLock lock(mLock);
  67. NS_ASSERTION(outStr == mOutput, "unexpected stream");
  68. Process_Locked();
  69. return NS_OK;
  70. }
  71. void Close_Locked()
  72. {
  73. LOG(("Close_Locked\n"));
  74. mOutput->Close();
  75. mOutput = 0;
  76. mInput->Close();
  77. mInput = 0;
  78. // post done copying event
  79. QuitPumpingEvents();
  80. }
  81. void Process_Locked()
  82. {
  83. while (1) {
  84. mInputCondition = NS_OK; // reset
  85. uint32_t n;
  86. nsresult rv = mOutput->WriteSegments(FillOutputBuffer, this, CHUNK_SIZE, &n);
  87. if (NS_FAILED(rv) || (n == 0)) {
  88. if (rv == NS_BASE_STREAM_WOULD_BLOCK)
  89. mOutput->AsyncWait(this, 0, 0, nullptr);
  90. else if (mInputCondition == NS_BASE_STREAM_WOULD_BLOCK)
  91. mInput->AsyncWait(this, 0, 0, nullptr);
  92. else
  93. Close_Locked();
  94. break;
  95. }
  96. }
  97. }
  98. nsresult AsyncCopy(nsITransport *srcTrans, nsITransport *destTrans)
  99. {
  100. mLock = nsAutoLock::NewLock("MyCopier::mLock");
  101. if (!mLock)
  102. return NS_ERROR_OUT_OF_MEMORY;
  103. nsresult rv;
  104. nsCOMPtr<nsIInputStream> inStr;
  105. rv = srcTrans->OpenInputStream(0, 0, 0, getter_AddRefs(inStr));
  106. if (NS_FAILED(rv)) return rv;
  107. nsCOMPtr<nsIOutputStream> outStr;
  108. rv = destTrans->OpenOutputStream(0, 0, 0, getter_AddRefs(outStr));
  109. if (NS_FAILED(rv)) return rv;
  110. mInput = do_QueryInterface(inStr);
  111. mOutput = do_QueryInterface(outStr);
  112. return mInput->AsyncWait(this, 0, 0, nullptr);
  113. }
  114. static nsresult FillOutputBuffer(nsIOutputStream *outStr,
  115. void *closure,
  116. char *buffer,
  117. uint32_t offset,
  118. uint32_t count,
  119. uint32_t *countRead)
  120. {
  121. MyCopier *self = (MyCopier *) closure;
  122. nsresult rv = self->mInput->Read(buffer, count, countRead);
  123. if (NS_FAILED(rv))
  124. self->mInputCondition = rv;
  125. else if (*countRead == 0)
  126. self->mInputCondition = NS_BASE_STREAM_CLOSED;
  127. return self->mInputCondition;
  128. }
  129. protected:
  130. PRLock *mLock;
  131. nsCOMPtr<nsIAsyncInputStream> mInput;
  132. nsCOMPtr<nsIAsyncOutputStream> mOutput;
  133. nsresult mInputCondition;
  134. };
  135. NS_IMPL_ISUPPORTS(MyCopier,
  136. nsIInputStreamCallback,
  137. nsIOutputStreamCallback)
  138. ////////////////////////////////////////////////////////////////////////////////
  139. /**
  140. * asynchronously copy file.
  141. */
  142. static nsresult
  143. RunTest(nsIFile *srcFile, nsIFile *destFile)
  144. {
  145. nsresult rv;
  146. LOG(("RunTest\n"));
  147. nsCOMPtr<nsIStreamTransportService> sts =
  148. do_GetService(kStreamTransportServiceCID, &rv);
  149. if (NS_FAILED(rv)) return rv;
  150. nsCOMPtr<nsIInputStream> srcStr;
  151. rv = NS_NewLocalFileInputStream(getter_AddRefs(srcStr), srcFile);
  152. if (NS_FAILED(rv)) return rv;
  153. nsCOMPtr<nsIOutputStream> destStr;
  154. rv = NS_NewLocalFileOutputStream(getter_AddRefs(destStr), destFile);
  155. if (NS_FAILED(rv)) return rv;
  156. nsCOMPtr<nsITransport> srcTransport;
  157. rv = sts->CreateInputTransport(srcStr, int64_t(-1), int64_t(-1), true,
  158. getter_AddRefs(srcTransport));
  159. if (NS_FAILED(rv)) return rv;
  160. nsCOMPtr<nsITransport> destTransport;
  161. rv = sts->CreateOutputTransport(destStr, int64_t(-1), int64_t(-1), true,
  162. getter_AddRefs(destTransport));
  163. if (NS_FAILED(rv)) return rv;
  164. MyCopier *copier = new MyCopier();
  165. if (copier == nullptr)
  166. return NS_ERROR_OUT_OF_MEMORY;
  167. NS_ADDREF(copier);
  168. rv = copier->AsyncCopy(srcTransport, destTransport);
  169. if (NS_FAILED(rv)) return rv;
  170. PumpEvents();
  171. NS_RELEASE(copier);
  172. return NS_OK;
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////
  175. static nsresult
  176. RunBlockingTest(nsIFile *srcFile, nsIFile *destFile)
  177. {
  178. nsresult rv;
  179. LOG(("RunBlockingTest\n"));
  180. nsCOMPtr<nsIStreamTransportService> sts =
  181. do_GetService(kStreamTransportServiceCID, &rv);
  182. if (NS_FAILED(rv)) return rv;
  183. nsCOMPtr<nsIInputStream> srcIn;
  184. rv = NS_NewLocalFileInputStream(getter_AddRefs(srcIn), srcFile);
  185. if (NS_FAILED(rv)) return rv;
  186. nsCOMPtr<nsIOutputStream> fileOut;
  187. rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOut), destFile);
  188. if (NS_FAILED(rv)) return rv;
  189. nsCOMPtr<nsITransport> destTransport;
  190. rv = sts->CreateOutputTransport(fileOut, int64_t(-1), int64_t(-1),
  191. true, getter_AddRefs(destTransport));
  192. if (NS_FAILED(rv)) return rv;
  193. nsCOMPtr<nsIOutputStream> destOut;
  194. rv = destTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(destOut));
  195. if (NS_FAILED(rv)) return rv;
  196. char buf[120];
  197. uint32_t n;
  198. for (;;) {
  199. rv = srcIn->Read(buf, sizeof(buf), &n);
  200. if (NS_FAILED(rv) || (n == 0)) return rv;
  201. rv = destOut->Write(buf, n, &n);
  202. if (NS_FAILED(rv)) return rv;
  203. }
  204. return NS_OK;
  205. }
  206. ////////////////////////////////////////////////////////////////////////////////
  207. int
  208. main(int argc, char* argv[])
  209. {
  210. if (test_common_init(&argc, &argv) != 0)
  211. return -1;
  212. nsresult rv;
  213. if (argc < 2) {
  214. printf("usage: %s <file-to-read>\n", argv[0]);
  215. return -1;
  216. }
  217. char* fileName = argv[1];
  218. {
  219. nsCOMPtr<nsIServiceManager> servMan;
  220. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  221. nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  222. NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
  223. if (registrar)
  224. registrar->AutoRegister(nullptr);
  225. gTestLog = PR_NewLogModule("Test");
  226. nsCOMPtr<nsIFile> srcFile;
  227. rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(srcFile));
  228. if (NS_FAILED(rv)) return rv;
  229. nsCOMPtr<nsIFile> destFile;
  230. rv = srcFile->Clone(getter_AddRefs(destFile));
  231. if (NS_FAILED(rv)) return rv;
  232. nsAutoCString leafName;
  233. rv = destFile->GetNativeLeafName(leafName);
  234. if (NS_FAILED(rv)) return rv;
  235. nsAutoCString newName(leafName);
  236. newName.AppendLiteral(".1");
  237. rv = destFile->SetNativeLeafName(newName);
  238. if (NS_FAILED(rv)) return rv;
  239. rv = RunTest(srcFile, destFile);
  240. NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
  241. newName = leafName;
  242. newName.AppendLiteral(".2");
  243. rv = destFile->SetNativeLeafName(newName);
  244. if (NS_FAILED(rv)) return rv;
  245. rv = RunBlockingTest(srcFile, destFile);
  246. NS_ASSERTION(NS_SUCCEEDED(rv), "RunBlockingTest failed");
  247. // give background threads a chance to finish whatever work they may
  248. // be doing.
  249. PR_Sleep(PR_SecondsToInterval(1));
  250. } // this scopes the nsCOMPtrs
  251. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  252. rv = NS_ShutdownXPCOM(nullptr);
  253. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  254. return NS_OK;
  255. }