1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is UDP Socket Provider unit test.
16 * The Initial Developer of the Original Code is
17 * Mook <mook.moz+cvs.mozilla.org@gmail.com>.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
40 #include "nsIServiceManager.h"
41 #include "nsIComponentRegistrar.h"
43 #include "nsServiceManagerUtils.h"
44 #include "nsISocketTransportService.h"
45 #include "nsISocketTransport.h"
46 #include "nsIOutputStream.h"
47 #include "nsIInputStream.h"
51 #define UDP_ASSERT(condition, message) \
53 NS_ASSERTION(condition, message); \
60 #define UDP_ASSERT_PRSTATUS(message) \
62 NS_ASSERTION(status == PR_SUCCESS, message); \
63 if (status != PR_SUCCESS) { \
64 PRErrorCode err = PR_GetError(); \
66 "FAIL nspr: %s: (%08x) %s\n", \
69 PR_ErrorToString(err, PR_LANGUAGE_I_DEFAULT)); \
75 #define UDP_ASSERT_NSRESULT(message) \
77 NS_ASSERTION(NS_SUCCEEDED(rv), message); \
78 if (NS_FAILED(rv)) { \
79 fprintf(stderr, "FAIL UDPSocket: %s: %08x\n", \
87 main(int argc
, char* argv
[])
89 if (test_common_init(&argc
, &argv
) != 0)
94 PRFileDesc
*serverFD
= nsnull
;
96 do { // act both as a scope for nsCOMPtrs to be released before XPCOM
97 // shutdown, as well as a easy way to abort the test
98 PRStatus status
= PR_SUCCESS
;
100 nsCOMPtr
<nsIServiceManager
> servMan
;
101 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
102 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
103 UDP_ASSERT(registrar
, "Null nsIComponentRegistrar");
105 registrar
->AutoRegister(nsnull
);
107 // listen for a incoming UDP connection on localhost
108 serverFD
= PR_OpenUDPSocket(PR_AF_INET
);
109 UDP_ASSERT(serverFD
, "Cannot open UDP socket for listening");
111 PRSocketOptionData socketOptions
;
112 socketOptions
.option
= PR_SockOpt_Nonblocking
;
113 socketOptions
.value
.non_blocking
= PR_FALSE
;
114 status
= PR_SetSocketOption(serverFD
, &socketOptions
);
115 UDP_ASSERT_PRSTATUS("Failed to set server socket as blocking");
118 status
= PR_InitializeNetAddr(PR_IpAddrLoopback
, UDP_PORT
, &addr
);
119 UDP_ASSERT_PRSTATUS("Failed to initialize loopback address");
121 status
= PR_Bind(serverFD
, &addr
);
122 UDP_ASSERT_PRSTATUS("Failed to bind server socket");
124 // dummy IOService to get around bug 379890
125 nsCOMPtr
<nsISupports
> ios
=
126 do_GetService("@mozilla.org/network/io-service;1");
128 // and have a matching UDP connection for the client
129 nsCOMPtr
<nsISocketTransportService
> sts
=
130 do_GetService("@mozilla.org/network/socket-transport-service;1", &rv
);
131 UDP_ASSERT_NSRESULT("Cannot get socket transport service");
133 nsCOMPtr
<nsISocketTransport
> transport
;
134 const char *protocol
= "udp";
135 rv
= sts
->CreateTransport(&protocol
, 1, NS_LITERAL_CSTRING("localhost"),
136 UDP_PORT
, nsnull
, getter_AddRefs(transport
));
137 UDP_ASSERT_NSRESULT("Cannot create transport");
139 PRUint32 count
, read
;
140 const PRUint32 data
= 0xFF0056A9;
142 // write to the output stream
143 nsCOMPtr
<nsIOutputStream
> outstream
;
144 rv
= transport
->OpenOutputStream(nsITransport::OPEN_BLOCKING
,
145 0, 0, getter_AddRefs(outstream
));
146 UDP_ASSERT_NSRESULT("Cannot open output stream");
148 rv
= outstream
->Write((const char*)&data
, sizeof(PRUint32
), &count
);
149 UDP_ASSERT_NSRESULT("Cannot write to output stream");
150 UDP_ASSERT(count
== sizeof(PRUint32
),
151 "Did not write enough bytes to output stream");
153 // read from NSPR to check it's the same
154 count
= PR_RecvFrom(serverFD
, &read
, sizeof(PRUint32
), 0, &addr
, 1);
155 UDP_ASSERT(count
== sizeof(PRUint32
),
156 "Did not read enough bytes from NSPR");
157 status
= (read
== data
? PR_SUCCESS
: PR_FAILURE
);
158 UDP_ASSERT_PRSTATUS("Did not read expected data from NSPR");
161 count
= PR_SendTo(serverFD
, &data
, sizeof(PRUint32
), 0, &addr
, 1);
162 status
= (count
== sizeof(PRUint32
) ? PR_SUCCESS
: PR_FAILURE
);
163 UDP_ASSERT_PRSTATUS("Did not write enough bytes to NSPR");
166 nsCOMPtr
<nsIInputStream
> instream
;
167 rv
= transport
->OpenInputStream(nsITransport::OPEN_BLOCKING
,
168 0, 0, getter_AddRefs(instream
));
169 UDP_ASSERT_NSRESULT("Cannot open input stream");
171 rv
= instream
->Read((char*)&read
, sizeof(PRUint32
), &count
);
172 UDP_ASSERT_NSRESULT("Cannot read from input stream");
173 UDP_ASSERT(count
== sizeof(PRUint32
),
174 "Did not read enough bytes from input stream");
175 UDP_ASSERT(read
== data
, "Did not read expected data from stream");
177 } while (false); // release all XPCOM things
179 PRStatus status
= PR_Close(serverFD
);
180 if (status
!= PR_SUCCESS
) {
181 PRErrorCode err
= PR_GetError();
182 fprintf(stderr
, "FAIL: Cannot close server: (%08x) %s\n",
183 err
, PR_ErrorToString(err
, PR_LANGUAGE_I_DEFAULT
));
186 rv
= NS_ShutdownXPCOM(nsnull
);
187 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");