Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / netwerk / test / TestUpload.cpp
blob7e21c12fc96c690283ac09ec1d49b723fac24593
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 "TestCommon.h"
39 #ifdef WIN32
40 #include <windows.h>
41 #endif
43 #include "nsIComponentRegistrar.h"
44 #include "nsIIOService.h"
45 #include "nsIServiceManager.h"
46 #include "nsNetUtil.h"
48 #include "nsIUploadChannel.h"
50 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
52 #include "prlog.h"
53 #if defined(PR_LOGGING)
55 // set NSPR_LOG_MODULES=Test:5
57 static PRLogModuleInfo *gTestLog = nsnull;
58 #endif
59 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
61 //-----------------------------------------------------------------------------
62 // InputTestConsumer
63 //-----------------------------------------------------------------------------
65 class InputTestConsumer : public nsIStreamListener
67 public:
69 InputTestConsumer();
70 virtual ~InputTestConsumer();
72 NS_DECL_ISUPPORTS
73 NS_DECL_NSIREQUESTOBSERVER
74 NS_DECL_NSISTREAMLISTENER
77 InputTestConsumer::InputTestConsumer()
81 InputTestConsumer::~InputTestConsumer()
85 NS_IMPL_ISUPPORTS2(InputTestConsumer,
86 nsIStreamListener,
87 nsIRequestObserver)
89 NS_IMETHODIMP
90 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
92 LOG(("InputTestConsumer::OnStartRequest\n"));
93 return NS_OK;
96 NS_IMETHODIMP
97 InputTestConsumer::OnDataAvailable(nsIRequest *request,
98 nsISupports* context,
99 nsIInputStream *aIStream,
100 PRUint32 aSourceOffset,
101 PRUint32 aLength)
103 char buf[1025];
104 PRUint32 amt, size;
105 nsresult rv;
107 while (aLength) {
108 size = PR_MIN(aLength, sizeof(buf));
109 rv = aIStream->Read(buf, size, &amt);
110 if (NS_FAILED(rv)) {
111 NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv),
112 "The stream should never block.");
113 return rv;
115 aLength -= amt;
117 return NS_OK;
121 NS_IMETHODIMP
122 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
123 nsresult aStatus)
125 LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
126 QuitPumpingEvents();
127 return NS_OK;
132 main(int argc, char* argv[])
134 if (test_common_init(&argc, &argv) != 0)
135 return -1;
137 nsresult rv;
139 if (argc < 2) {
140 printf("usage: %s <url> <file-to-upload>\n", argv[0]);
141 return -1;
143 char* uriSpec = argv[1];
144 char* fileName = argv[2];
146 #if defined(PR_LOGGING)
147 gTestLog = PR_NewLogModule("Test");
148 #endif
151 nsCOMPtr<nsIServiceManager> servMan;
152 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
153 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
154 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
155 if (registrar)
156 registrar->AutoRegister(nsnull);
158 nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
159 // first thing to do is create ourselves a stream that
160 // is to be uploaded.
161 nsCOMPtr<nsIInputStream> uploadStream;
162 rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
163 PR_TRUE,
164 nsDependentCString(fileName), // XXX UTF-8
165 0, ioService);
166 if (NS_FAILED(rv)) return rv;
168 // create our url.
169 nsCOMPtr<nsIURI> uri;
170 rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
171 if (NS_FAILED(rv)) return rv;
173 nsCOMPtr<nsIChannel> channel;
174 rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
175 if (NS_FAILED(rv)) return rv;
177 // QI and set the upload stream
178 nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
179 uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
181 // create a dummy listener
182 InputTestConsumer* listener;
184 listener = new InputTestConsumer;
185 if (!listener) {
186 NS_ERROR("Failed to create a new stream listener!");
187 return -1;
189 NS_ADDREF(listener);
191 channel->AsyncOpen(listener, nsnull);
193 PumpEvents();
194 } // this scopes the nsCOMPtrs
195 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
196 rv = NS_ShutdownXPCOM(nsnull);
197 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
199 return 0;