Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / netwerk / test / TestSocketTransport.cpp
blob562db706b6381a75e902906fb4c8464c9517b7d3
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
12 * License.
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Darin Fisher <darin@netscape.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
39 #include "nsIComponentRegistrar.h"
40 #include "nsPISocketTransportService.h"
41 #include "nsISocketTransport.h"
42 #include "nsIAsyncInputStream.h"
43 #include "nsIAsyncOutputStream.h"
44 #include "nsIProgressEventSink.h"
45 #include "nsIInterfaceRequestor.h"
46 #include "nsIInterfaceRequestorUtils.h"
47 #include "nsIProxyObjectManager.h"
48 #include "nsIRequest.h"
49 #include "nsIServiceManager.h"
50 #include "nsIComponentManager.h"
51 #include "nsCOMPtr.h"
52 #include "nsMemory.h"
53 #include "nsStringAPI.h"
54 #include "nsIDNSService.h"
55 #include "nsIFileStreams.h"
56 #include "nsIStreamListener.h"
57 #include "nsILocalFile.h"
58 #include "nsNetUtil.h"
59 #include "nsAutoLock.h"
60 #include "prlog.h"
62 ////////////////////////////////////////////////////////////////////////////////
64 #if defined(PR_LOGGING)
66 // set NSPR_LOG_MODULES=Test:5
68 static PRLogModuleInfo *gTestLog = nsnull;
69 #endif
70 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
72 ////////////////////////////////////////////////////////////////////////////////
74 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
76 ////////////////////////////////////////////////////////////////////////////////
78 class MyHandler : public nsIOutputStreamCallback
79 , public nsIInputStreamCallback
81 public:
82 NS_DECL_ISUPPORTS
84 MyHandler(const char *path,
85 nsIAsyncInputStream *in,
86 nsIAsyncOutputStream *out)
87 : mInput(in)
88 , mOutput(out)
89 , mWriteOffset(0)
91 mBuf.Assign(NS_LITERAL_CSTRING("GET "));
92 mBuf.Append(path);
93 mBuf.Append(NS_LITERAL_CSTRING(" HTTP/1.0\r\n\r\n"));
95 virtual ~MyHandler() {}
97 // called on any thread
98 NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *out)
100 LOG(("OnOutputStreamReady\n"));
102 nsresult rv;
103 PRUint32 n, count = mBuf.Length() - mWriteOffset;
105 rv = out->Write(mBuf.get() + mWriteOffset, count, &n);
107 LOG((" write returned [rv=%x count=%u]\n", rv, n));
109 if (NS_FAILED(rv) || (n == 0)) {
110 if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
111 LOG((" done writing; starting to read\n"));
112 mInput->AsyncWait(this, 0, 0, nsnull);
113 return NS_OK;
117 mWriteOffset += n;
119 return out->AsyncWait(this, 0, 0, nsnull);
122 // called on any thread
123 NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *in)
125 LOG(("OnInputStreamReady\n"));
127 nsresult rv;
128 PRUint32 n;
129 char buf[500];
131 rv = in->Read(buf, sizeof(buf), &n);
133 LOG((" read returned [rv=%x count=%u]\n", rv, n));
135 if (NS_FAILED(rv) || (n == 0)) {
136 if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
137 QuitPumpingEvents();
138 return NS_OK;
142 return in->AsyncWait(this, 0, 0, nsnull);
145 private:
146 nsCOMPtr<nsIAsyncInputStream> mInput;
147 nsCOMPtr<nsIAsyncOutputStream> mOutput;
148 nsCString mBuf;
149 PRUint32 mWriteOffset;
152 NS_IMPL_THREADSAFE_ISUPPORTS2(MyHandler,
153 nsIOutputStreamCallback,
154 nsIInputStreamCallback)
156 ////////////////////////////////////////////////////////////////////////////////
159 * create transport, open streams, and close
161 static nsresult
162 RunCloseTest(nsISocketTransportService *sts,
163 const char *host, int port,
164 PRUint32 inFlags, PRUint32 outFlags)
166 nsresult rv;
168 LOG(("RunCloseTest\n"));
170 nsCOMPtr<nsISocketTransport> transport;
171 rv = sts->CreateTransport(nsnull, 0,
172 nsDependentCString(host), port, nsnull,
173 getter_AddRefs(transport));
174 if (NS_FAILED(rv)) return rv;
176 nsCOMPtr<nsIInputStream> in;
177 rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
178 nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
179 if (NS_FAILED(rv)) return rv;
181 nsCOMPtr<nsIOutputStream> out;
182 rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
183 nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
184 if (NS_FAILED(rv)) return rv;
186 LOG(("waiting 1 second before closing transport and streams...\n"));
187 PR_Sleep(PR_SecondsToInterval(1));
189 // let nsCOMPtr destructors close everything...
190 return NS_OK;
195 * asynchronously read socket stream
197 static nsresult
198 RunTest(nsISocketTransportService *sts,
199 const char *host, int port, const char *path,
200 PRUint32 inFlags, PRUint32 outFlags)
202 nsresult rv;
204 LOG(("RunTest\n"));
206 nsCOMPtr<nsISocketTransport> transport;
207 rv = sts->CreateTransport(nsnull, 0,
208 nsDependentCString(host), port, nsnull,
209 getter_AddRefs(transport));
210 if (NS_FAILED(rv)) return rv;
212 nsCOMPtr<nsIInputStream> in;
213 rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
214 nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
215 if (NS_FAILED(rv)) return rv;
217 nsCOMPtr<nsIOutputStream> out;
218 rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
219 nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
220 if (NS_FAILED(rv)) return rv;
222 MyHandler *handler = new MyHandler(path, asyncIn, asyncOut);
223 if (handler == nsnull)
224 return NS_ERROR_OUT_OF_MEMORY;
225 NS_ADDREF(handler);
227 rv = asyncOut->AsyncWait(handler, 0, 0, nsnull);
229 if (NS_SUCCEEDED(rv))
230 PumpEvents();
232 NS_RELEASE(handler);
234 return NS_OK;
237 ////////////////////////////////////////////////////////////////////////////////
240 main(int argc, char* argv[])
242 if (test_common_init(&argc, &argv) != 0)
243 return -1;
245 nsresult rv;
247 if (argc < 4) {
248 printf("usage: TestSocketTransport <host> <port> <path>\n");
249 return -1;
253 nsCOMPtr<nsIServiceManager> servMan;
254 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
255 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
256 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
257 if (registrar)
258 registrar->AutoRegister(nsnull);
260 #if defined(PR_LOGGING)
261 gTestLog = PR_NewLogModule("Test");
262 #endif
264 // Make sure the DNS service is initialized on the main thread
265 nsCOMPtr<nsIDNSService> dns =
266 do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
267 if (NS_FAILED(rv)) return rv;
269 nsCOMPtr<nsPISocketTransportService> sts =
270 do_GetService(kSocketTransportServiceCID, &rv);
271 if (NS_FAILED(rv)) return rv;
273 LOG(("phase 1 tests...\n"));
275 LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
276 rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
277 nsITransport::OPEN_UNBUFFERED,
278 nsITransport::OPEN_UNBUFFERED);
279 NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
281 LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
282 rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
283 0 /* nsITransport::OPEN_BUFFERED */,
284 nsITransport::OPEN_UNBUFFERED);
285 NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
287 LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
288 rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
289 nsITransport::OPEN_UNBUFFERED,
290 0 /*nsITransport::OPEN_BUFFERED */);
291 NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
293 LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
294 rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
295 0 /*nsITransport::OPEN_BUFFERED */,
296 0 /*nsITransport::OPEN_BUFFERED */);
297 NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
299 LOG(("calling Shutdown on socket transport service:\n"));
300 sts->Shutdown();
302 LOG(("calling Init on socket transport service:\n"));
303 sts->Init();
305 LOG(("phase 2 tests...\n"));
307 LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
308 rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
309 nsITransport::OPEN_UNBUFFERED,
310 nsITransport::OPEN_UNBUFFERED);
311 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
313 LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
314 rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
315 0 /* nsITransport::OPEN_BUFFERED */,
316 nsITransport::OPEN_UNBUFFERED);
317 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
319 LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
320 rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
321 nsITransport::OPEN_UNBUFFERED,
322 0 /*nsITransport::OPEN_BUFFERED */);
323 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
325 LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
326 rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
327 0 /*nsITransport::OPEN_BUFFERED */,
328 0 /*nsITransport::OPEN_BUFFERED */);
329 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
331 LOG(("waiting 1 second before calling Shutdown...\n"));
332 PR_Sleep(PR_SecondsToInterval(1));
334 LOG(("calling Shutdown on socket transport service:\n"));
335 sts->Shutdown();
337 // give background threads a chance to finish whatever work they may
338 // be doing.
339 LOG(("waiting 1 second before exiting...\n"));
340 PR_Sleep(PR_SecondsToInterval(1));
341 } // this scopes the nsCOMPtrs
342 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
343 rv = NS_ShutdownXPCOM(nsnull);
344 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
345 return 0;