netwerk/test/TestUDPSocketProvider.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial