Bug 459878, fix up errors for 3.0.5 -> 3.1b2 MU test
[mozilla-1.9.git] / netwerk / test / TestStreamPump.cpp
blob4d73376ca2623d2acc37b370500aac9ad4207b75
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 "nsIStreamTransportService.h"
41 #include "nsIAsyncInputStream.h"
42 #include "nsIProgressEventSink.h"
43 #include "nsIInterfaceRequestor.h"
44 #include "nsIInterfaceRequestorUtils.h"
45 #include "nsIProxyObjectManager.h"
46 #include "nsIRequest.h"
47 #include "nsIServiceManager.h"
48 #include "nsIComponentManager.h"
49 #include "nsISeekableStream.h"
50 #include "nsCOMPtr.h"
51 #include "nsMemory.h"
52 #include "nsStringAPI.h"
53 #include "nsIFileStreams.h"
54 #include "nsIStreamListener.h"
55 #include "nsILocalFile.h"
56 #include "nsNetUtil.h"
57 #include "nsAutoLock.h"
58 #include "prlog.h"
59 #include "prprf.h"
61 ////////////////////////////////////////////////////////////////////////////////
63 #if defined(PR_LOGGING)
65 // set NSPR_LOG_MODULES=Test:5
67 static PRLogModuleInfo *gTestLog = nsnull;
68 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
69 #else
70 #define LOG(args)
71 #endif
73 ////////////////////////////////////////////////////////////////////////////////
75 class MyListener : public nsIStreamListener
77 public:
78 NS_DECL_ISUPPORTS
80 MyListener() {}
81 virtual ~MyListener() {}
83 NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx)
85 LOG(("MyListener::OnStartRequest\n"));
86 return NS_OK;
89 NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx,
90 nsIInputStream *stream,
91 PRUint32 offset, PRUint32 count)
93 LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset, count));
95 char buf[500];
96 nsresult rv;
98 while (count) {
99 PRUint32 n, amt = PR_MIN(count, sizeof(buf));
101 rv = stream->Read(buf, amt, &n);
102 if (NS_FAILED(rv)) {
103 LOG((" read returned 0x%08x\n", rv));
104 return rv;
107 fwrite(buf, n, 1, stdout);
108 printf("\n");
110 LOG((" read %u bytes\n", n));
111 count -= n;
114 return NS_OK;
117 NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
119 LOG(("MyListener::OnStopRequest [status=%x]\n", status));
120 QuitPumpingEvents();
121 return NS_OK;
125 NS_IMPL_ISUPPORTS2(MyListener,
126 nsIRequestObserver,
127 nsIStreamListener)
129 ////////////////////////////////////////////////////////////////////////////////
132 * asynchronously copy file.
134 static nsresult
135 RunTest(nsIFile *file, PRInt64 offset, PRInt64 length)
137 nsresult rv;
139 LOG(("RunTest\n"));
141 nsCOMPtr<nsIInputStream> stream;
142 rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
143 if (NS_FAILED(rv)) return rv;
145 nsCOMPtr<nsIInputStreamPump> pump;
146 rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream, offset, length);
147 if (NS_FAILED(rv)) return rv;
149 rv = pump->AsyncRead(new MyListener(), nsnull);
150 if (NS_FAILED(rv)) return rv;
152 PumpEvents();
153 return NS_OK;
156 ////////////////////////////////////////////////////////////////////////////////
159 main(int argc, char* argv[])
161 if (test_common_init(&argc, &argv) != 0)
162 return -1;
164 nsresult rv;
166 if (argc < 4) {
167 printf("usage: %s <file-to-read> <start-offset> <read-length>\n", argv[0]);
168 return -1;
170 char* fileName = argv[1];
171 PRInt64 offset, length;
172 int err = PR_sscanf(argv[2], "%lld", &offset);
173 if (err == -1) {
174 printf("Start offset must be an integer!\n");
175 return 1;
177 err = PR_sscanf(argv[3], "%lld", &length);
178 if (err == -1) {
179 printf("Length must be an integer!\n");
180 return 1;
184 nsCOMPtr<nsIServiceManager> servMan;
185 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
186 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
187 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
188 if (registrar)
189 registrar->AutoRegister(nsnull);
191 #if defined(PR_LOGGING)
192 gTestLog = PR_NewLogModule("Test");
193 #endif
195 nsCOMPtr<nsILocalFile> file;
196 rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
197 if (NS_FAILED(rv)) return rv;
199 rv = RunTest(file, offset, length);
200 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
202 // give background threads a chance to finish whatever work they may
203 // be doing.
204 PR_Sleep(PR_SecondsToInterval(1));
205 } // this scopes the nsCOMPtrs
206 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
207 rv = NS_ShutdownXPCOM(nsnull);
208 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
209 return NS_OK;