TestBlockingSocket.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "nsISocketTransportService.h"
  7. #include "nsISocketTransport.h"
  8. #include "nsIServiceManager.h"
  9. #include "nsIComponentManager.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsStringAPI.h"
  12. #include "nsIFile.h"
  13. #include "nsNetUtil.h"
  14. #include "nsIInputStream.h"
  15. #include "nsServiceManagerUtils.h"
  16. #include "nsIOutputStream.h"
  17. #include "NetwerkTestLogging.h"
  18. #include "prenv.h"
  19. #include "prthread.h"
  20. #include <stdlib.h>
  21. ////////////////////////////////////////////////////////////////////////////////
  22. //
  23. // set NSPR_LOG_MODULES=Test:5
  24. //
  25. static PRLogModuleInfo *gTestLog = nullptr;
  26. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  27. ////////////////////////////////////////////////////////////////////////////////
  28. static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
  29. ////////////////////////////////////////////////////////////////////////////////
  30. static nsresult
  31. RunBlockingTest(const nsACString &host, int32_t port, nsIFile *file)
  32. {
  33. nsresult rv;
  34. LOG(("RunBlockingTest\n"));
  35. nsCOMPtr<nsISocketTransportService> sts =
  36. do_GetService(kSocketTransportServiceCID, &rv);
  37. if (NS_FAILED(rv)) return rv;
  38. nsCOMPtr<nsIInputStream> input;
  39. rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file);
  40. if (NS_FAILED(rv)) return rv;
  41. nsCOMPtr<nsISocketTransport> trans;
  42. rv = sts->CreateTransport(nullptr, 0, host, port, nullptr, getter_AddRefs(trans));
  43. if (NS_FAILED(rv)) return rv;
  44. nsCOMPtr<nsIOutputStream> output;
  45. rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output));
  46. if (NS_FAILED(rv)) return rv;
  47. char buf[120];
  48. uint32_t nr, nw;
  49. for (;;) {
  50. rv = input->Read(buf, sizeof(buf), &nr);
  51. if (NS_FAILED(rv) || (nr == 0)) return rv;
  52. /*
  53. const char *p = buf;
  54. while (nr) {
  55. rv = output->Write(p, nr, &nw);
  56. if (NS_FAILED(rv)) return rv;
  57. nr -= nw;
  58. p += nw;
  59. }
  60. */
  61. rv = output->Write(buf, nr, &nw);
  62. if (NS_FAILED(rv)) return rv;
  63. NS_ASSERTION(nr == nw, "not all written");
  64. }
  65. LOG((" done copying data.\n"));
  66. return NS_OK;
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. int
  70. main(int argc, char* argv[])
  71. {
  72. if (test_common_init(&argc, &argv) != 0)
  73. return -1;
  74. nsresult rv;
  75. if (argc < 4) {
  76. printf("usage: %s <host> <port> <file-to-read>\n", argv[0]);
  77. return -1;
  78. }
  79. char* hostName = argv[1];
  80. int32_t port = atoi(argv[2]);
  81. char* fileName = argv[3];
  82. {
  83. nsCOMPtr<nsIServiceManager> servMan;
  84. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  85. gTestLog = PR_NewLogModule("Test");
  86. nsCOMPtr<nsIFile> file;
  87. rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
  88. if (NS_FAILED(rv)) return -1;
  89. rv = RunBlockingTest(nsDependentCString(hostName), port, file);
  90. if (NS_FAILED(rv))
  91. LOG(("RunBlockingTest failed [rv=%x]\n", rv));
  92. // give background threads a chance to finish whatever work they may
  93. // be doing.
  94. LOG(("sleeping for 5 seconds...\n"));
  95. PR_Sleep(PR_SecondsToInterval(5));
  96. } // this scopes the nsCOMPtrs
  97. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  98. rv = NS_ShutdownXPCOM(nullptr);
  99. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  100. return 0;
  101. }