Back to index

lightning-sunbird  0.9+nobinonly
TestBlockingSocket.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 "nsISocketTransportService.h"
00041 #include "nsISocketTransport.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsIComponentManager.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsString.h"
00046 #include "nsILocalFile.h"
00047 #include "nsNetUtil.h"
00048 #include "prlog.h"
00049 #include "prenv.h"
00050 #include "prthread.h"
00051 #include <stdlib.h>
00052 
00054 
00055 #if defined(PR_LOGGING)
00056 //
00057 // set NSPR_LOG_MODULES=Test:5
00058 //
00059 static PRLogModuleInfo *gTestLog = nsnull;
00060 #endif
00061 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00062 
00064 
00065 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
00066 
00068 
00069 static nsresult
00070 RunBlockingTest(const nsACString &host, PRInt32 port, nsIFile *file)
00071 {
00072     nsresult rv;
00073 
00074     LOG(("RunBlockingTest\n"));
00075 
00076     nsCOMPtr<nsISocketTransportService> sts =
00077         do_GetService(kSocketTransportServiceCID, &rv);
00078     if (NS_FAILED(rv)) return rv;
00079 
00080     nsCOMPtr<nsIInputStream> input;
00081     rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file);
00082     if (NS_FAILED(rv)) return rv;
00083 
00084     nsCOMPtr<nsISocketTransport> trans;
00085     rv = sts->CreateTransport(nsnull, 0, host, port, nsnull, getter_AddRefs(trans));
00086     if (NS_FAILED(rv)) return rv;
00087 
00088     nsCOMPtr<nsIOutputStream> output;
00089     rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output));
00090     if (NS_FAILED(rv)) return rv;
00091 
00092     char buf[120];
00093     PRUint32 nr, nw;
00094     for (;;) {
00095         rv = input->Read(buf, sizeof(buf), &nr);
00096         if (NS_FAILED(rv) || (nr == 0)) return rv;
00097 
00098 /*
00099         const char *p = buf;
00100         while (nr) {
00101             rv = output->Write(p, nr, &nw);
00102             if (NS_FAILED(rv)) return rv;
00103 
00104             nr -= nw;
00105             p  += nw;
00106         }
00107 */
00108         
00109         rv = output->Write(buf, nr, &nw);
00110         if (NS_FAILED(rv)) return rv;
00111 
00112         NS_ASSERTION(nr == nw, "not all written");
00113     }
00114 
00115     LOG(("  done copying data.\n"));
00116     return NS_OK;
00117 }
00118 
00120 
00121 int
00122 main(int argc, char* argv[])
00123 {
00124     if (test_common_init(&argc, &argv) != 0)
00125         return -1;
00126 
00127     nsresult rv;
00128 
00129     if (argc < 4) {
00130         printf("usage: %s <host> <port> <file-to-read>\n", argv[0]);
00131         return -1;
00132     }
00133     char* hostName = argv[1];
00134     PRInt32 port = atoi(argv[2]);
00135     char* fileName = argv[3];
00136     {
00137         nsCOMPtr<nsIServiceManager> servMan;
00138         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00139         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00140         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00141         if (registrar)
00142             registrar->AutoRegister(nsnull);
00143 
00144 #if defined(PR_LOGGING)
00145         gTestLog = PR_NewLogModule("Test");
00146 #endif
00147 
00148         nsCOMPtr<nsILocalFile> file;
00149         rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
00150         if (NS_FAILED(rv)) return rv;
00151 
00152         rv = RunBlockingTest(nsDependentCString(hostName), port, file);
00153         if (NS_FAILED(rv))
00154             LOG(("RunBlockingTest failed [rv=%x]\n", rv));
00155 
00156         // give background threads a chance to finish whatever work they may
00157         // be doing.
00158         LOG(("sleeping for 5 seconds...\n"));
00159         PR_Sleep(PR_SecondsToInterval(5));
00160     } // this scopes the nsCOMPtrs
00161     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00162     rv = NS_ShutdownXPCOM(nsnull);
00163     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00164     return NS_OK;
00165 }