CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / netwerk / test / TestCallbacks.cpp
blob2374575dd23f7aa96b1dff9c884974920696f279
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
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 <stdio.h>
40 #ifdef WIN32
41 #include <windows.h>
42 #endif
43 #include "nspr.h"
44 #include "nscore.h"
45 #include "nsCOMPtr.h"
46 #include "nsIIOService.h"
47 #include "nsIServiceManager.h"
48 #include "nsIStreamListener.h"
49 #include "nsIInputStream.h"
50 #include "nsIChannel.h"
51 #include "nsIURL.h"
52 #include "nsIInterfaceRequestor.h"
53 #include "nsIInterfaceRequestorUtils.h"
54 #include "nsIDNSService.h"
56 #include "nsISimpleEnumerator.h"
57 #include "nsNetUtil.h"
58 #include "nsStringAPI.h"
60 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
62 static PRBool gError = PR_FALSE;
63 static PRInt32 gKeepRunning = 0;
65 #define NS_IEQUALS_IID \
66 { 0x11c5c8ee, 0x1dd2, 0x11b2, \
67 { 0xa8, 0x93, 0xbb, 0x23, 0xa1, 0xb6, 0x27, 0x76 }}
69 class nsIEquals : public nsISupports {
70 public:
71 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IEQUALS_IID)
72 NS_IMETHOD Equals(void *aPtr, PRBool *_retval) = 0;
75 NS_DEFINE_STATIC_IID_ACCESSOR(nsIEquals, NS_IEQUALS_IID)
77 class ConsumerContext : public nsIEquals {
78 public:
79 NS_DECL_ISUPPORTS
81 ConsumerContext() { }
83 NS_IMETHOD Equals(void *aPtr, PRBool *_retval) {
84 *_retval = PR_TRUE;
85 if (aPtr != this) *_retval = PR_FALSE;
86 return NS_OK;
90 NS_IMPL_THREADSAFE_ISUPPORTS1(ConsumerContext, nsIEquals)
92 class Consumer : public nsIStreamListener {
93 public:
94 NS_DECL_ISUPPORTS
95 NS_DECL_NSIREQUESTOBSERVER
96 NS_DECL_NSISTREAMLISTENER
98 Consumer();
99 virtual ~Consumer();
100 nsresult Init(nsIURI *aURI, nsIChannel *aChannel, nsISupports *aContext);
101 nsresult Validate(nsIRequest *request, nsISupports *aContext);
103 // member data
104 PRBool mOnStart; // have we received an OnStart?
105 PRBool mOnStop; // have we received an onStop?
106 PRInt32 mOnDataCount; // number of times OnData was called.
107 nsCOMPtr<nsIURI> mURI;
108 nsCOMPtr<nsIChannel> mChannel;
109 nsCOMPtr<nsIEquals> mContext;
112 // nsISupports implementation
113 NS_IMPL_THREADSAFE_ISUPPORTS2(Consumer, nsIStreamListener, nsIRequestObserver)
116 // nsIRequestObserver implementation
117 NS_IMETHODIMP
118 Consumer::OnStartRequest(nsIRequest *request, nsISupports* aContext) {
119 fprintf(stderr, "Consumer::OnStart() -> in\n\n");
121 if (mOnStart) {
122 fprintf(stderr, "INFO: multiple OnStarts received\n");
124 mOnStart = PR_TRUE;
126 nsresult rv = Validate(request, aContext);
127 if (NS_FAILED(rv)) return rv;
129 fprintf(stderr, "Consumer::OnStart() -> out\n\n");
130 return rv;
133 NS_IMETHODIMP
134 Consumer::OnStopRequest(nsIRequest *request, nsISupports *aContext,
135 nsresult aStatus) {
136 fprintf(stderr, "Consumer::OnStop() -> in\n\n");
138 if (!mOnStart) {
139 gError = PR_TRUE;
140 fprintf(stderr, "ERROR: No OnStart received\n");
143 if (mOnStop) {
144 fprintf(stderr, "INFO: multiple OnStops received\n");
147 fprintf(stderr, "INFO: received %d OnData()s\n", mOnDataCount);
149 mOnStop = PR_TRUE;
151 nsresult rv = Validate(request, aContext);
152 if (NS_FAILED(rv)) return rv;
154 fprintf(stderr, "Consumer::OnStop() -> out\n\n");
155 return rv;
159 // nsIStreamListener implementation
160 NS_IMETHODIMP
161 Consumer::OnDataAvailable(nsIRequest *request, nsISupports *aContext,
162 nsIInputStream *aIStream,
163 PRUint32 aOffset, PRUint32 aLength) {
164 fprintf(stderr, "Consumer::OnData() -> in\n\n");
166 if (!mOnStart) {
167 gError = PR_TRUE;
168 fprintf(stderr, "ERROR: No OnStart received\n");
171 mOnDataCount += 1;
173 nsresult rv = Validate(request, aContext);
174 if (NS_FAILED(rv)) return rv;
176 fprintf(stderr, "Consumer::OnData() -> out\n\n");
177 return rv;
180 // Consumer implementation
181 Consumer::Consumer() {
182 mOnStart = mOnStop = PR_FALSE;
183 mOnDataCount = 0;
184 gKeepRunning++;
187 Consumer::~Consumer() {
188 fprintf(stderr, "Consumer::~Consumer -> in\n\n");
190 if (!mOnStart) {
191 gError = PR_TRUE;
192 fprintf(stderr, "ERROR: Never got an OnStart\n");
195 if (!mOnStop) {
196 gError = PR_TRUE;
197 fprintf(stderr, "ERROR: Never got an OnStop \n");
200 fprintf(stderr, "Consumer::~Consumer -> out\n\n");
201 if (--gKeepRunning == 0)
202 QuitPumpingEvents();
205 nsresult
206 Consumer::Init(nsIURI *aURI, nsIChannel* aChannel, nsISupports *aContext) {
207 mURI = aURI;
208 mChannel = aChannel;
209 mContext = do_QueryInterface(aContext);
210 return NS_OK;
213 nsresult
214 Consumer::Validate(nsIRequest* request, nsISupports *aContext) {
215 nsresult rv = NS_OK;
216 nsCOMPtr<nsIURI> uri;
217 nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
219 rv = aChannel->GetURI(getter_AddRefs(uri));
220 if (NS_FAILED(rv)) return rv;
222 PRBool same = PR_FALSE;
224 rv = mURI->Equals(uri, &same);
225 if (NS_FAILED(rv)) return rv;
227 if (!same)
228 fprintf(stderr, "INFO: URIs do not match\n");
230 rv = mContext->Equals((void*)aContext, &same);
231 if (NS_FAILED(rv)) return rv;
233 if (!same) {
234 gError = PR_TRUE;
235 fprintf(stderr, "ERROR: Contexts do not match\n");
237 return rv;
240 nsresult StartLoad(const char *);
242 int main(int argc, char *argv[]) {
243 if (test_common_init(&argc, &argv) != 0)
244 return -1;
246 nsresult rv = NS_OK;
247 PRBool cmdLineURL = PR_FALSE;
249 if (argc > 1) {
250 // run in signle url mode
251 cmdLineURL = PR_TRUE;
254 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
255 if (NS_FAILED(rv)) return rv;
257 if (cmdLineURL) {
258 rv = StartLoad(argv[1]);
259 } else {
260 rv = StartLoad("http://badhostnamexyz/test.txt");
262 if (NS_FAILED(rv)) return rv;
264 // Enter the message pump to allow the URL load to proceed.
265 PumpEvents();
267 NS_ShutdownXPCOM(nsnull);
268 if (gError) {
269 fprintf(stderr, "\n\n-------ERROR-------\n\n");
271 return rv;
274 nsresult StartLoad(const char *aURISpec) {
275 nsresult rv = NS_OK;
277 // create a context
278 ConsumerContext *context = new ConsumerContext;
279 nsCOMPtr<nsISupports> contextSup = do_QueryInterface(context, &rv);
280 if (NS_FAILED(rv)) return rv;
283 nsCOMPtr<nsIIOService> serv = do_GetService(kIOServiceCID, &rv);
284 if (NS_FAILED(rv)) return rv;
286 // create a uri
287 nsCOMPtr<nsIURI> uri;
288 rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
289 if (NS_FAILED(rv)) return rv;
291 // create a channel
292 nsCOMPtr<nsIChannel> channel;
293 rv = serv->NewChannelFromURI(uri, getter_AddRefs(channel));
294 if (NS_FAILED(rv)) return rv;
296 Consumer *consumer = new Consumer;
297 rv = consumer->Init(uri, channel, contextSup);
298 if (NS_FAILED(rv)) return rv;
300 // kick off the load
301 nsCOMPtr<nsIRequest> request;
302 return channel->AsyncOpen(static_cast<nsIStreamListener*>(consumer), contextSup);