Back to index

lightning-sunbird  0.9+nobinonly
TestSocketTransport.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "TestCommon.h"
00039 #include "nsIComponentRegistrar.h"
00040 #include "nsPISocketTransportService.h"
00041 #include "nsISocketTransport.h"
00042 #include "nsIAsyncInputStream.h"
00043 #include "nsIAsyncOutputStream.h"
00044 #include "nsIProgressEventSink.h"
00045 #include "nsIInterfaceRequestor.h"
00046 #include "nsIInterfaceRequestorUtils.h"
00047 #include "nsIProxyObjectManager.h"
00048 #include "nsIRequest.h"
00049 #include "nsIServiceManager.h"
00050 #include "nsIComponentManager.h"
00051 #include "nsCOMPtr.h"
00052 #include "nsMemory.h"
00053 #include "nsString.h"
00054 #include "nsIFileStreams.h"
00055 #include "nsIStreamListener.h"
00056 #include "nsIEventQueueService.h"
00057 #include "nsIEventQueue.h"
00058 #include "nsILocalFile.h"
00059 #include "nsNetUtil.h"
00060 #include "nsAutoLock.h"
00061 #include "prlog.h"
00062 
00064 
00065 #if defined(PR_LOGGING)
00066 //
00067 // set NSPR_LOG_MODULES=Test:5
00068 //
00069 static PRLogModuleInfo *gTestLog = nsnull;
00070 #endif
00071 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00072 
00074 
00075 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
00076 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00077 
00078 PRBool gDone = PR_FALSE;
00079 nsIEventQueue* gEventQ = nsnull;
00080 
00082 
00083 static void *PR_CALLBACK
00084 DoneEvent_Handler(PLEvent *ev)
00085 {
00086     gDone = PR_TRUE;
00087     return nsnull;
00088 }
00089 
00090 static void PR_CALLBACK
00091 DoneEvent_Cleanup(PLEvent *ev)
00092 {
00093     delete ev;
00094 }
00095 
00096 static void
00097 PostDoneEvent()
00098 {
00099     LOG(("PostDoneEvent\n"));
00100 
00101     PLEvent *ev = new PLEvent();
00102 
00103     PL_InitEvent(ev, nsnull, 
00104             DoneEvent_Handler,
00105             DoneEvent_Cleanup);
00106 
00107     gEventQ->PostEvent(ev);
00108 }
00109 
00111 
00112 class MyHandler : public nsIOutputStreamCallback
00113                 , public nsIInputStreamCallback
00114 {
00115 public:
00116     NS_DECL_ISUPPORTS
00117 
00118     MyHandler(const char *path,
00119               nsIAsyncInputStream *in,
00120               nsIAsyncOutputStream *out)
00121         : mInput(in)
00122         , mOutput(out)
00123         , mWriteOffset(0)
00124         {
00125             mBuf = NS_LITERAL_CSTRING("GET ")
00126                  + nsDependentCString(path)
00127                  + NS_LITERAL_CSTRING(" HTTP/1.0\r\n\r\n");
00128         }
00129     virtual ~MyHandler() {}
00130 
00131     // called on any thread
00132     NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *out)
00133     {
00134         LOG(("OnOutputStreamReady\n"));
00135 
00136         nsresult rv;
00137         PRUint32 n, count = mBuf.Length() - mWriteOffset;
00138 
00139         rv = out->Write(mBuf.get() + mWriteOffset, count, &n);
00140 
00141         LOG(("  write returned [rv=%x count=%u]\n", rv, n));
00142 
00143         if (NS_FAILED(rv) || (n == 0)) {
00144             if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
00145                 LOG(("  done writing; starting to read\n"));
00146                 mInput->AsyncWait(this, 0, 0, nsnull);
00147                 return NS_OK;
00148             }
00149         }
00150 
00151         mWriteOffset += n;
00152 
00153         return out->AsyncWait(this, 0, 0, nsnull);
00154     }
00155 
00156     // called on any thread
00157     NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *in)
00158     {
00159         LOG(("OnInputStreamReady\n"));
00160 
00161         nsresult rv;
00162         PRUint32 n;
00163         char buf[500];
00164 
00165         rv = in->Read(buf, sizeof(buf), &n);
00166 
00167         LOG(("  read returned [rv=%x count=%u]\n", rv, n));
00168 
00169         if (NS_FAILED(rv) || (n == 0)) {
00170             if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
00171                 PostDoneEvent();
00172                 return NS_OK;
00173             }
00174         }
00175 
00176         return in->AsyncWait(this, 0, 0, nsnull);
00177     }
00178 
00179 private:
00180     nsCOMPtr<nsIAsyncInputStream>  mInput;
00181     nsCOMPtr<nsIAsyncOutputStream> mOutput;
00182     nsCString mBuf;
00183     PRUint32  mWriteOffset;
00184 };
00185 
00186 NS_IMPL_THREADSAFE_ISUPPORTS2(MyHandler,
00187                               nsIOutputStreamCallback,
00188                               nsIInputStreamCallback)
00189 
00190 
00191 
00195 static nsresult
00196 RunCloseTest(nsISocketTransportService *sts,
00197              const char *host, int port,
00198              PRUint32 inFlags, PRUint32 outFlags)
00199 {
00200     nsresult rv;
00201 
00202     LOG(("RunCloseTest\n"));
00203 
00204     nsCOMPtr<nsISocketTransport> transport;
00205     rv = sts->CreateTransport(nsnull, 0,
00206                               nsDependentCString(host), port, nsnull,
00207                               getter_AddRefs(transport));
00208     if (NS_FAILED(rv)) return rv;
00209 
00210     nsCOMPtr<nsIInputStream> in;
00211     rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
00212     nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
00213     if (NS_FAILED(rv)) return rv;
00214 
00215     nsCOMPtr<nsIOutputStream> out;
00216     rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
00217     nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
00218     if (NS_FAILED(rv)) return rv;
00219 
00220     LOG(("waiting 1 second before closing transport and streams...\n"));
00221     PR_Sleep(PR_SecondsToInterval(1));
00222     
00223     // let nsCOMPtr destructors close everything...
00224     return NS_OK;
00225 }
00226 
00227 
00231 static nsresult
00232 RunTest(nsISocketTransportService *sts,
00233         const char *host, int port, const char *path,
00234         PRUint32 inFlags, PRUint32 outFlags)
00235 {
00236     nsresult rv;
00237 
00238     LOG(("RunTest\n"));
00239 
00240     nsCOMPtr<nsISocketTransport> transport;
00241     rv = sts->CreateTransport(nsnull, 0,
00242                               nsDependentCString(host), port, nsnull,
00243                               getter_AddRefs(transport));
00244     if (NS_FAILED(rv)) return rv;
00245 
00246     nsCOMPtr<nsIInputStream> in;
00247     rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
00248     nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
00249     if (NS_FAILED(rv)) return rv;
00250 
00251     nsCOMPtr<nsIOutputStream> out;
00252     rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
00253     nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
00254     if (NS_FAILED(rv)) return rv;
00255 
00256     MyHandler *handler = new MyHandler(path, asyncIn, asyncOut);
00257     if (handler == nsnull)
00258         return NS_ERROR_OUT_OF_MEMORY;
00259     NS_ADDREF(handler);
00260 
00261     rv = asyncOut->AsyncWait(handler, 0, 0, nsnull);
00262 
00263     if (NS_SUCCEEDED(rv)) {
00264         PLEvent* event;
00265         gDone = PR_FALSE;
00266         while (!gDone) {
00267             rv = gEventQ->WaitForEvent(&event);
00268             if (NS_FAILED(rv)) return rv;
00269             rv = gEventQ->HandleEvent(event);
00270             if (NS_FAILED(rv)) return rv;
00271         }
00272     }
00273 
00274     NS_RELEASE(handler);
00275 
00276     return NS_OK;
00277 }
00278 
00280 
00281 int
00282 main(int argc, char* argv[])
00283 {
00284     if (test_common_init(&argc, &argv) != 0)
00285         return -1;
00286 
00287     nsresult rv;
00288 
00289     if (argc < 4) {
00290         printf("usage: TestSocketTransport <host> <port> <path>\n");
00291         return -1;
00292     }
00293 
00294     {
00295         nsCOMPtr<nsIServiceManager> servMan;
00296         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00297         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00298         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00299         if (registrar)
00300             registrar->AutoRegister(nsnull);
00301 
00302 #if defined(PR_LOGGING)
00303         gTestLog = PR_NewLogModule("Test");
00304 #endif
00305 
00306         nsCOMPtr<nsIEventQueueService> eventQService =
00307                  do_GetService(kEventQueueServiceCID, &rv);
00308         if (NS_FAILED(rv)) return rv;
00309 
00310         rv = eventQService->CreateMonitoredThreadEventQueue();
00311         if (NS_FAILED(rv)) return rv;
00312 
00313         rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00314         if (NS_FAILED(rv)) return rv;
00315 
00316         nsCOMPtr<nsPISocketTransportService> sts =
00317             do_GetService(kSocketTransportServiceCID, &rv);
00318         if (NS_FAILED(rv)) return rv;
00319 
00320         LOG(("phase 1 tests...\n"));
00321 
00322         LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
00323         rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
00324                           nsITransport::OPEN_UNBUFFERED,
00325                           nsITransport::OPEN_UNBUFFERED);
00326         NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
00327 
00328         LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
00329         rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
00330                           0 /* nsITransport::OPEN_BUFFERED */,
00331                           nsITransport::OPEN_UNBUFFERED);
00332         NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
00333 
00334         LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
00335         rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
00336                           nsITransport::OPEN_UNBUFFERED,
00337                           0 /*nsITransport::OPEN_BUFFERED */);
00338         NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
00339 
00340         LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
00341         rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
00342                           0 /*nsITransport::OPEN_BUFFERED */,
00343                           0 /*nsITransport::OPEN_BUFFERED */);
00344         NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
00345 
00346         LOG(("calling Shutdown on socket transport service:\n"));
00347         sts->Shutdown();
00348 
00349         LOG(("calling Init on socket transport service:\n"));
00350         sts->Init();
00351 
00352         LOG(("phase 2 tests...\n"));
00353 
00354         LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
00355         rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
00356                      nsITransport::OPEN_UNBUFFERED,
00357                      nsITransport::OPEN_UNBUFFERED);
00358         NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
00359 
00360         LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
00361         rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
00362                      0 /* nsITransport::OPEN_BUFFERED */,
00363                      nsITransport::OPEN_UNBUFFERED);
00364         NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
00365 
00366         LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
00367         rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
00368                      nsITransport::OPEN_UNBUFFERED,
00369                      0 /*nsITransport::OPEN_BUFFERED */);
00370         NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
00371 
00372         LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
00373         rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
00374                      0 /*nsITransport::OPEN_BUFFERED */,
00375                      0 /*nsITransport::OPEN_BUFFERED */);
00376         NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
00377 
00378         LOG(("waiting 1 second before calling Shutdown...\n"));
00379         PR_Sleep(PR_SecondsToInterval(1));
00380 
00381         LOG(("calling Shutdown on socket transport service:\n"));
00382         sts->Shutdown();
00383 
00384         NS_RELEASE(gEventQ);
00385 
00386         // give background threads a chance to finish whatever work they may
00387         // be doing.
00388         LOG(("waiting 1 second before exiting...\n"));
00389         PR_Sleep(PR_SecondsToInterval(1));
00390     } // this scopes the nsCOMPtrs
00391     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00392     rv = NS_ShutdownXPCOM(nsnull);
00393     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00394     return NS_OK;
00395 }