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
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.
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"
46 #include "nsIIOService.h"
47 #include "nsIServiceManager.h"
48 #include "nsIStreamListener.h"
49 #include "nsIInputStream.h"
50 #include "nsIChannel.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
{
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
{
83 NS_IMETHOD
Equals(void *aPtr
, PRBool
*_retval
) {
85 if (aPtr
!= this) *_retval
= PR_FALSE
;
90 NS_IMPL_THREADSAFE_ISUPPORTS1(ConsumerContext
, nsIEquals
)
92 class Consumer
: public nsIStreamListener
{
95 NS_DECL_NSIREQUESTOBSERVER
96 NS_DECL_NSISTREAMLISTENER
100 nsresult
Init(nsIURI
*aURI
, nsIChannel
*aChannel
, nsISupports
*aContext
);
101 nsresult
Validate(nsIRequest
*request
, nsISupports
*aContext
);
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
118 Consumer::OnStartRequest(nsIRequest
*request
, nsISupports
* aContext
) {
119 fprintf(stderr
, "Consumer::OnStart() -> in\n\n");
122 fprintf(stderr
, "INFO: multiple OnStarts received\n");
126 nsresult rv
= Validate(request
, aContext
);
127 if (NS_FAILED(rv
)) return rv
;
129 fprintf(stderr
, "Consumer::OnStart() -> out\n\n");
134 Consumer::OnStopRequest(nsIRequest
*request
, nsISupports
*aContext
,
136 fprintf(stderr
, "Consumer::OnStop() -> in\n\n");
140 fprintf(stderr
, "ERROR: No OnStart received\n");
144 fprintf(stderr
, "INFO: multiple OnStops received\n");
147 fprintf(stderr
, "INFO: received %d OnData()s\n", mOnDataCount
);
151 nsresult rv
= Validate(request
, aContext
);
152 if (NS_FAILED(rv
)) return rv
;
154 fprintf(stderr
, "Consumer::OnStop() -> out\n\n");
159 // nsIStreamListener implementation
161 Consumer::OnDataAvailable(nsIRequest
*request
, nsISupports
*aContext
,
162 nsIInputStream
*aIStream
,
163 PRUint32 aOffset
, PRUint32 aLength
) {
164 fprintf(stderr
, "Consumer::OnData() -> in\n\n");
168 fprintf(stderr
, "ERROR: No OnStart received\n");
173 nsresult rv
= Validate(request
, aContext
);
174 if (NS_FAILED(rv
)) return rv
;
176 fprintf(stderr
, "Consumer::OnData() -> out\n\n");
180 // Consumer implementation
181 Consumer::Consumer() {
182 mOnStart
= mOnStop
= PR_FALSE
;
187 Consumer::~Consumer() {
188 fprintf(stderr
, "Consumer::~Consumer -> in\n\n");
192 fprintf(stderr
, "ERROR: Never got an OnStart\n");
197 fprintf(stderr
, "ERROR: Never got an OnStop \n");
200 fprintf(stderr
, "Consumer::~Consumer -> out\n\n");
201 if (--gKeepRunning
== 0)
206 Consumer::Init(nsIURI
*aURI
, nsIChannel
* aChannel
, nsISupports
*aContext
) {
209 mContext
= do_QueryInterface(aContext
);
214 Consumer::Validate(nsIRequest
* request
, nsISupports
*aContext
) {
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
;
228 fprintf(stderr
, "INFO: URIs do not match\n");
230 rv
= mContext
->Equals((void*)aContext
, &same
);
231 if (NS_FAILED(rv
)) return rv
;
235 fprintf(stderr
, "ERROR: Contexts do not match\n");
240 nsresult
StartLoad(const char *);
242 int main(int argc
, char *argv
[]) {
243 if (test_common_init(&argc
, &argv
) != 0)
247 PRBool cmdLineURL
= PR_FALSE
;
250 // run in signle url mode
251 cmdLineURL
= PR_TRUE
;
254 rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
255 if (NS_FAILED(rv
)) return rv
;
258 rv
= StartLoad(argv
[1]);
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.
267 NS_ShutdownXPCOM(nsnull
);
269 fprintf(stderr
, "\n\n-------ERROR-------\n\n");
274 nsresult
StartLoad(const char *aURISpec
) {
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
;
287 nsCOMPtr
<nsIURI
> uri
;
288 rv
= NS_NewURI(getter_AddRefs(uri
), aURISpec
);
289 if (NS_FAILED(rv
)) return rv
;
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
;
301 nsCOMPtr
<nsIRequest
> request
;
302 return channel
->AsyncOpen(static_cast<nsIStreamListener
*>(consumer
), contextSup
);