On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / netwerk / test / TestRes.cpp
blobdfaa3d8e9b9250b3fba5351bf47b52cd4557984f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "nsIResProtocolHandler.h"
39 #include "nsIServiceManager.h"
40 #include "nsIIOService.h"
41 #include "nsIInputStream.h"
42 #include "nsIComponentManager.h"
43 #include "nsIComponentRegistrar.h"
44 #include "nsIStreamListener.h"
45 #include "nsIEventQueueService.h"
46 #include "nsIURI.h"
47 #include "nsCRT.h"
48 #include "nsNetCID.h"
50 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
51 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
53 ////////////////////////////////////////////////////////////////////////////////
55 nsresult
56 SetupMapping()
58 nsresult rv;
60 nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
61 if (NS_FAILED(rv)) return rv;
63 nsCOMPtr<nsIProtocolHandler> ph;
64 rv = serv->GetProtocolHandler("res", getter_AddRefs(ph));
65 if (NS_FAILED(rv)) return rv;
67 nsCOMPtr<nsIResProtocolHandler> resPH = do_QueryInterface(ph, &rv);
68 if (NS_FAILED(rv)) return rv;
70 rv = resPH->AppendSubstitution("foo", "file://y|/");
71 if (NS_FAILED(rv)) return rv;
73 rv = resPH->AppendSubstitution("foo", "file://y|/mozilla/dist/win32_D.OBJ/bin/");
74 if (NS_FAILED(rv)) return rv;
76 return rv;
79 ////////////////////////////////////////////////////////////////////////////////
81 nsresult
82 TestOpenInputStream(const char* url)
84 nsresult rv;
86 nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
87 if (NS_FAILED(rv)) return rv;
89 nsCOMPtr<nsIChannel> channel;
90 rv = serv->NewChannel(url,
91 nsnull, // base uri
92 getter_AddRefs(channel));
93 if (NS_FAILED(rv)) return rv;
95 nsCOMPtr<nsIInputStream> in;
96 rv = channel->Open(getter_AddRefs(in));
97 if (NS_FAILED(rv)) {
98 fprintf(stdout, "failed to OpenInputStream for %s\n", url);
99 return NS_OK;
102 char buf[1024];
103 while (1) {
104 PRUint32 amt;
105 rv = in->Read(buf, sizeof(buf), &amt);
106 if (NS_FAILED(rv)) return rv;
107 if (amt == 0) break; // eof
109 char* str = buf;
110 while (amt-- > 0) {
111 fputc(*str++, stdout);
114 nsCOMPtr<nsIURI> uri;
115 char* str;
117 rv = channel->GetOriginalURI(getter_AddRefs(uri));
118 if (NS_FAILED(rv)) return rv;
119 rv = uri->GetSpec(&str);
120 if (NS_FAILED(rv)) return rv;
121 fprintf(stdout, "%s resolved to ", str);
122 nsCRT::free(str);
124 rv = channel->GetURI(getter_AddRefs(uri));
125 if (NS_FAILED(rv)) return rv;
126 rv = uri->GetSpec(&str);
127 if (NS_FAILED(rv)) return rv;
128 fprintf(stdout, "%s\n", str);
129 nsCRT::free(str);
131 return NS_OK;
134 ////////////////////////////////////////////////////////////////////////////////
136 PRBool gDone = PR_FALSE;
137 nsIEventQueue* gEventQ = nsnull;
139 class Listener : public nsIStreamListener
141 public:
142 NS_DECL_ISUPPORTS
144 Listener() {}
145 virtual ~Listener() {}
147 NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
148 nsresult rv;
149 nsCOMPtr<nsIURI> uri;
150 nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
152 rv = channel->GetURI(getter_AddRefs(uri));
153 if (NS_SUCCEEDED(rv)) {
154 char* str;
155 rv = uri->GetSpec(&str);
156 if (NS_SUCCEEDED(rv)) {
157 fprintf(stdout, "Starting to load %s\n", str);
158 nsCRT::free(str);
161 return NS_OK;
164 NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt,
165 nsresult aStatus) {
166 nsresult rv;
167 nsCOMPtr<nsIURI> uri;
168 nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
170 rv = channel->GetURI(getter_AddRefs(uri));
171 if (NS_SUCCEEDED(rv)) {
172 char* str;
173 rv = uri->GetSpec(&str);
174 if (NS_SUCCEEDED(rv)) {
175 fprintf(stdout, "Ending load %s, status=%x\n", str, aStatus);
176 nsCRT::free(str);
179 gDone = PR_TRUE;
180 return NS_OK;
183 NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports *ctxt,
184 nsIInputStream *inStr,
185 PRUint32 sourceOffset, PRUint32 count) {
186 nsresult rv;
187 char buf[1024];
188 while (count > 0) {
189 PRUint32 amt;
190 rv = inStr->Read(buf, sizeof(buf), &amt);
191 count -= amt;
192 char* c = buf;
193 while (amt-- > 0) {
194 fputc(*c++, stdout);
197 return NS_OK;
201 NS_IMPL_ISUPPORTS2(Listener, nsIStreamListener, nsIRequestObserver)
203 nsresult
204 TestAsyncRead(const char* url)
206 nsresult rv;
208 nsCOMPtr<nsIEventQueueService> eventQService =
209 do_GetService(kEventQueueServiceCID, &rv);
210 if (NS_FAILED(rv)) return rv;
212 rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
213 if (NS_FAILED(rv)) return rv;
215 nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
216 if (NS_FAILED(rv)) return rv;
218 nsCOMPtr<nsIChannel> channel;
219 rv = serv->NewChannel(url,
220 nsnull, // base uri
221 getter_AddRefs(channel));
222 if (NS_FAILED(rv)) return rv;
224 nsCOMPtr<nsIStreamListener> listener = new Listener();
225 if (listener == nsnull)
226 return NS_ERROR_OUT_OF_MEMORY;
227 rv = channel->AsyncOpen(nsnull, listener);
228 if (NS_FAILED(rv)) return rv;
230 while (!gDone) {
231 PLEvent* event;
232 rv = gEventQ->GetEvent(&event);
233 if (NS_FAILED(rv)) return rv;
234 rv = gEventQ->HandleEvent(event);
235 if (NS_FAILED(rv)) return rv;
238 return rv;
242 main(int argc, char* argv[])
244 nsresult rv;
246 nsCOMPtr<nsIServiceManager> servMan;
247 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
248 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
249 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
250 if (registrar)
251 registrar->AutoRegister(nsnull);
253 NS_ASSERTION(NS_SUCCEEDED(rv), "AutoregisterComponents failed");
255 if (argc < 2) {
256 printf("usage: %s resource://foo/<path-to-resolve>\n", argv[0]);
257 return -1;
260 rv = SetupMapping();
261 NS_ASSERTION(NS_SUCCEEDED(rv), "SetupMapping failed");
262 if (NS_FAILED(rv)) return rv;
264 rv = TestOpenInputStream(argv[1]);
265 NS_ASSERTION(NS_SUCCEEDED(rv), "TestOpenInputStream failed");
267 rv = TestAsyncRead(argv[1]);
268 NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
269 } // this scopes the nsCOMPtrs
270 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
271 rv = NS_ShutdownXPCOM(nsnull);
272 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
273 return rv;
276 ////////////////////////////////////////////////////////////////////////////////