TestUDPSocketProvider.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "stdio.h"
  5. #include "TestCommon.h"
  6. #include "nsCOMPtr.h"
  7. #include "nsIServiceManager.h"
  8. #include "nsIComponentRegistrar.h"
  9. #include "nspr.h"
  10. #include "nsServiceManagerUtils.h"
  11. #include "nsISocketTransportService.h"
  12. #include "nsISocketTransport.h"
  13. #include "nsIOutputStream.h"
  14. #include "nsIInputStream.h"
  15. #define UDP_PORT 9050
  16. #define UDP_ASSERT(condition, message) \
  17. PR_BEGIN_MACRO \
  18. NS_ASSERTION(condition, message); \
  19. if (!(condition)) { \
  20. returnCode = -1; \
  21. break; \
  22. } \
  23. PR_END_MACRO
  24. #define UDP_ASSERT_PRSTATUS(message) \
  25. PR_BEGIN_MACRO \
  26. NS_ASSERTION(status == PR_SUCCESS, message); \
  27. if (status != PR_SUCCESS) { \
  28. PRErrorCode err = PR_GetError(); \
  29. fprintf(stderr, \
  30. "FAIL nspr: %s: (%08x) %s\n", \
  31. message, \
  32. err, \
  33. PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT)); \
  34. returnCode = -1; \
  35. break; \
  36. } \
  37. PR_END_MACRO
  38. #define UDP_ASSERT_NSRESULT(message) \
  39. PR_BEGIN_MACRO \
  40. NS_ASSERTION(NS_SUCCEEDED(rv), message); \
  41. if (NS_FAILED(rv)) { \
  42. fprintf(stderr, "FAIL UDPSocket: %s: %08x\n", \
  43. message, rv); \
  44. returnCode = -1; \
  45. break; \
  46. } \
  47. PR_END_MACRO
  48. int
  49. main(int argc, char* argv[])
  50. {
  51. if (test_common_init(&argc, &argv) != 0)
  52. return -1;
  53. int returnCode = 0;
  54. nsresult rv = NS_OK;
  55. PRFileDesc *serverFD = nullptr;
  56. do { // act both as a scope for nsCOMPtrs to be released before XPCOM
  57. // shutdown, as well as a easy way to abort the test
  58. PRStatus status = PR_SUCCESS;
  59. nsCOMPtr<nsIServiceManager> servMan;
  60. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  61. nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  62. UDP_ASSERT(registrar, "Null nsIComponentRegistrar");
  63. if (registrar)
  64. registrar->AutoRegister(nullptr);
  65. // listen for a incoming UDP connection on localhost
  66. serverFD = PR_OpenUDPSocket(PR_AF_INET);
  67. UDP_ASSERT(serverFD, "Cannot open UDP socket for listening");
  68. PRSocketOptionData socketOptions;
  69. socketOptions.option = PR_SockOpt_Nonblocking;
  70. socketOptions.value.non_blocking = false;
  71. status = PR_SetSocketOption(serverFD, &socketOptions);
  72. UDP_ASSERT_PRSTATUS("Failed to set server socket as blocking");
  73. PRNetAddr addr;
  74. status = PR_InitializeNetAddr(PR_IpAddrLoopback, UDP_PORT, &addr);
  75. UDP_ASSERT_PRSTATUS("Failed to initialize loopback address");
  76. status = PR_Bind(serverFD, &addr);
  77. UDP_ASSERT_PRSTATUS("Failed to bind server socket");
  78. // dummy IOService to get around bug 379890
  79. nsCOMPtr<nsISupports> ios =
  80. do_GetService("@mozilla.org/network/io-service;1");
  81. // and have a matching UDP connection for the client
  82. nsCOMPtr<nsISocketTransportService> sts =
  83. do_GetService("@mozilla.org/network/socket-transport-service;1", &rv);
  84. UDP_ASSERT_NSRESULT("Cannot get socket transport service");
  85. nsCOMPtr<nsISocketTransport> transport;
  86. const char *protocol = "udp";
  87. rv = sts->CreateTransport(&protocol, 1, NS_LITERAL_CSTRING("localhost"),
  88. UDP_PORT, nullptr, getter_AddRefs(transport));
  89. UDP_ASSERT_NSRESULT("Cannot create transport");
  90. uint32_t count, read;
  91. const uint32_t data = 0xFF0056A9;
  92. // write to the output stream
  93. nsCOMPtr<nsIOutputStream> outstream;
  94. rv = transport->OpenOutputStream(nsITransport::OPEN_BLOCKING,
  95. 0, 0, getter_AddRefs(outstream));
  96. UDP_ASSERT_NSRESULT("Cannot open output stream");
  97. rv = outstream->Write((const char*)&data, sizeof(uint32_t), &count);
  98. UDP_ASSERT_NSRESULT("Cannot write to output stream");
  99. UDP_ASSERT(count == sizeof(uint32_t),
  100. "Did not write enough bytes to output stream");
  101. // read from NSPR to check it's the same
  102. count = PR_RecvFrom(serverFD, &read, sizeof(uint32_t), 0, &addr, 1);
  103. UDP_ASSERT(count == sizeof(uint32_t),
  104. "Did not read enough bytes from NSPR");
  105. status = (read == data ? PR_SUCCESS : PR_FAILURE);
  106. UDP_ASSERT_PRSTATUS("Did not read expected data from NSPR");
  107. // write to NSPR
  108. count = PR_SendTo(serverFD, &data, sizeof(uint32_t), 0, &addr, 1);
  109. status = (count == sizeof(uint32_t) ? PR_SUCCESS : PR_FAILURE);
  110. UDP_ASSERT_PRSTATUS("Did not write enough bytes to NSPR");
  111. // read from stream
  112. nsCOMPtr<nsIInputStream> instream;
  113. rv = transport->OpenInputStream(nsITransport::OPEN_BLOCKING,
  114. 0, 0, getter_AddRefs(instream));
  115. UDP_ASSERT_NSRESULT("Cannot open input stream");
  116. rv = instream->Read((char*)&read, sizeof(uint32_t), &count);
  117. UDP_ASSERT_NSRESULT("Cannot read from input stream");
  118. UDP_ASSERT(count == sizeof(uint32_t),
  119. "Did not read enough bytes from input stream");
  120. UDP_ASSERT(read == data, "Did not read expected data from stream");
  121. } while (false); // release all XPCOM things
  122. if (serverFD) {
  123. PRStatus status = PR_Close(serverFD);
  124. if (status != PR_SUCCESS) {
  125. PRErrorCode err = PR_GetError();
  126. fprintf(stderr, "FAIL: Cannot close server: (%08x) %s\n",
  127. err, PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT));
  128. }
  129. }
  130. rv = NS_ShutdownXPCOM(nullptr);
  131. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  132. return returnCode;
  133. }