merge backout. CLOSED TREE
[mozilla-central.git] / netwerk / test / TestBlockingSocket.cpp
blob72cca56242783e6eb03bdc45d2eab761ee57895c
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 "nsISocketTransportService.h"
41 #include "nsISocketTransport.h"
42 #include "nsIServiceManager.h"
43 #include "nsIComponentManager.h"
44 #include "nsCOMPtr.h"
45 #include "nsStringAPI.h"
46 #include "nsILocalFile.h"
47 #include "nsNetUtil.h"
48 #include "prlog.h"
49 #include "prenv.h"
50 #include "prthread.h"
51 #include <stdlib.h>
53 ////////////////////////////////////////////////////////////////////////////////
55 #if defined(PR_LOGGING)
57 // set NSPR_LOG_MODULES=Test:5
59 static PRLogModuleInfo *gTestLog = nsnull;
60 #endif
61 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
63 ////////////////////////////////////////////////////////////////////////////////
65 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
67 ////////////////////////////////////////////////////////////////////////////////
69 static nsresult
70 RunBlockingTest(const nsACString &host, PRInt32 port, nsIFile *file)
72 nsresult rv;
74 LOG(("RunBlockingTest\n"));
76 nsCOMPtr<nsISocketTransportService> sts =
77 do_GetService(kSocketTransportServiceCID, &rv);
78 if (NS_FAILED(rv)) return rv;
80 nsCOMPtr<nsIInputStream> input;
81 rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file);
82 if (NS_FAILED(rv)) return rv;
84 nsCOMPtr<nsISocketTransport> trans;
85 rv = sts->CreateTransport(nsnull, 0, host, port, nsnull, getter_AddRefs(trans));
86 if (NS_FAILED(rv)) return rv;
88 nsCOMPtr<nsIOutputStream> output;
89 rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output));
90 if (NS_FAILED(rv)) return rv;
92 char buf[120];
93 PRUint32 nr, nw;
94 for (;;) {
95 rv = input->Read(buf, sizeof(buf), &nr);
96 if (NS_FAILED(rv) || (nr == 0)) return rv;
99 const char *p = buf;
100 while (nr) {
101 rv = output->Write(p, nr, &nw);
102 if (NS_FAILED(rv)) return rv;
104 nr -= nw;
105 p += nw;
109 rv = output->Write(buf, nr, &nw);
110 if (NS_FAILED(rv)) return rv;
112 NS_ASSERTION(nr == nw, "not all written");
115 LOG((" done copying data.\n"));
116 return NS_OK;
119 ////////////////////////////////////////////////////////////////////////////////
122 main(int argc, char* argv[])
124 if (test_common_init(&argc, &argv) != 0)
125 return -1;
127 nsresult rv;
129 if (argc < 4) {
130 printf("usage: %s <host> <port> <file-to-read>\n", argv[0]);
131 return -1;
133 char* hostName = argv[1];
134 PRInt32 port = atoi(argv[2]);
135 char* fileName = argv[3];
137 nsCOMPtr<nsIServiceManager> servMan;
138 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
139 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
140 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
141 if (registrar)
142 registrar->AutoRegister(nsnull);
144 #if defined(PR_LOGGING)
145 gTestLog = PR_NewLogModule("Test");
146 #endif
148 nsCOMPtr<nsILocalFile> file;
149 rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
150 if (NS_FAILED(rv)) return rv;
152 rv = RunBlockingTest(nsDependentCString(hostName), port, file);
153 #if defined(PR_LOGGING)
154 if (NS_FAILED(rv))
155 LOG(("RunBlockingTest failed [rv=%x]\n", rv));
156 #endif
158 // give background threads a chance to finish whatever work they may
159 // be doing.
160 LOG(("sleeping for 5 seconds...\n"));
161 PR_Sleep(PR_SecondsToInterval(5));
162 } // this scopes the nsCOMPtrs
163 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
164 rv = NS_ShutdownXPCOM(nsnull);
165 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
166 return NS_OK;