1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestCallbacks.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,272 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "TestCommon.h" 1.10 +#include <stdio.h> 1.11 +#ifdef WIN32 1.12 +#include <windows.h> 1.13 +#endif 1.14 +#include "nspr.h" 1.15 +#include "nscore.h" 1.16 +#include "nsCOMPtr.h" 1.17 +#include "nsIIOService.h" 1.18 +#include "nsIServiceManager.h" 1.19 +#include "nsIStreamListener.h" 1.20 +#include "nsIInputStream.h" 1.21 +#include "nsIChannel.h" 1.22 +#include "nsIURL.h" 1.23 +#include "nsIInterfaceRequestor.h" 1.24 +#include "nsIInterfaceRequestorUtils.h" 1.25 +#include "nsIDNSService.h" 1.26 +#include "mozilla/Attributes.h" 1.27 + 1.28 +#include "nsISimpleEnumerator.h" 1.29 +#include "nsNetUtil.h" 1.30 +#include "nsStringAPI.h" 1.31 + 1.32 +static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); 1.33 + 1.34 +static bool gError = false; 1.35 +static int32_t gKeepRunning = 0; 1.36 + 1.37 +#define NS_IEQUALS_IID \ 1.38 + { 0x11c5c8ee, 0x1dd2, 0x11b2, \ 1.39 + { 0xa8, 0x93, 0xbb, 0x23, 0xa1, 0xb6, 0x27, 0x76 }} 1.40 + 1.41 +class nsIEquals : public nsISupports { 1.42 +public: 1.43 + NS_DECLARE_STATIC_IID_ACCESSOR(NS_IEQUALS_IID) 1.44 + NS_IMETHOD Equals(void *aPtr, bool *_retval) = 0; 1.45 +}; 1.46 + 1.47 +NS_DEFINE_STATIC_IID_ACCESSOR(nsIEquals, NS_IEQUALS_IID) 1.48 + 1.49 +class ConsumerContext MOZ_FINAL : public nsIEquals { 1.50 +public: 1.51 + NS_DECL_THREADSAFE_ISUPPORTS 1.52 + 1.53 + ConsumerContext() { } 1.54 + 1.55 + NS_IMETHOD Equals(void *aPtr, bool *_retval) { 1.56 + *_retval = true; 1.57 + if (aPtr != this) *_retval = false; 1.58 + return NS_OK; 1.59 + } 1.60 +}; 1.61 + 1.62 +NS_IMPL_ISUPPORTS(ConsumerContext, nsIEquals) 1.63 + 1.64 +class Consumer : public nsIStreamListener { 1.65 +public: 1.66 + NS_DECL_THREADSAFE_ISUPPORTS 1.67 + NS_DECL_NSIREQUESTOBSERVER 1.68 + NS_DECL_NSISTREAMLISTENER 1.69 + 1.70 + Consumer(); 1.71 + virtual ~Consumer(); 1.72 + nsresult Init(nsIURI *aURI, nsIChannel *aChannel, nsISupports *aContext); 1.73 + nsresult Validate(nsIRequest *request, nsISupports *aContext); 1.74 + 1.75 + // member data 1.76 + bool mOnStart; // have we received an OnStart? 1.77 + bool mOnStop; // have we received an onStop? 1.78 + int32_t mOnDataCount; // number of times OnData was called. 1.79 + nsCOMPtr<nsIURI> mURI; 1.80 + nsCOMPtr<nsIChannel> mChannel; 1.81 + nsCOMPtr<nsIEquals> mContext; 1.82 +}; 1.83 + 1.84 +// nsISupports implementation 1.85 +NS_IMPL_ISUPPORTS(Consumer, nsIStreamListener, nsIRequestObserver) 1.86 + 1.87 + 1.88 +// nsIRequestObserver implementation 1.89 +NS_IMETHODIMP 1.90 +Consumer::OnStartRequest(nsIRequest *request, nsISupports* aContext) { 1.91 + fprintf(stderr, "Consumer::OnStart() -> in\n\n"); 1.92 + 1.93 + if (mOnStart) { 1.94 + fprintf(stderr, "INFO: multiple OnStarts received\n"); 1.95 + } 1.96 + mOnStart = true; 1.97 + 1.98 + nsresult rv = Validate(request, aContext); 1.99 + if (NS_FAILED(rv)) return rv; 1.100 + 1.101 + fprintf(stderr, "Consumer::OnStart() -> out\n\n"); 1.102 + return rv; 1.103 +} 1.104 + 1.105 +NS_IMETHODIMP 1.106 +Consumer::OnStopRequest(nsIRequest *request, nsISupports *aContext, 1.107 + nsresult aStatus) { 1.108 + fprintf(stderr, "Consumer::OnStop() -> in\n\n"); 1.109 + 1.110 + if (!mOnStart) { 1.111 + gError = true; 1.112 + fprintf(stderr, "ERROR: No OnStart received\n"); 1.113 + } 1.114 + 1.115 + if (mOnStop) { 1.116 + fprintf(stderr, "INFO: multiple OnStops received\n"); 1.117 + } 1.118 + 1.119 + fprintf(stderr, "INFO: received %d OnData()s\n", mOnDataCount); 1.120 + 1.121 + mOnStop = true; 1.122 + 1.123 + nsresult rv = Validate(request, aContext); 1.124 + if (NS_FAILED(rv)) return rv; 1.125 + 1.126 + fprintf(stderr, "Consumer::OnStop() -> out\n\n"); 1.127 + return rv; 1.128 +} 1.129 + 1.130 + 1.131 +// nsIStreamListener implementation 1.132 +NS_IMETHODIMP 1.133 +Consumer::OnDataAvailable(nsIRequest *request, nsISupports *aContext, 1.134 + nsIInputStream *aIStream, 1.135 + uint64_t aOffset, uint32_t aLength) { 1.136 + fprintf(stderr, "Consumer::OnData() -> in\n\n"); 1.137 + 1.138 + if (!mOnStart) { 1.139 + gError = true; 1.140 + fprintf(stderr, "ERROR: No OnStart received\n"); 1.141 + } 1.142 + 1.143 + mOnDataCount += 1; 1.144 + 1.145 + nsresult rv = Validate(request, aContext); 1.146 + if (NS_FAILED(rv)) return rv; 1.147 + 1.148 + fprintf(stderr, "Consumer::OnData() -> out\n\n"); 1.149 + return rv; 1.150 +} 1.151 + 1.152 +// Consumer implementation 1.153 +Consumer::Consumer() { 1.154 + mOnStart = mOnStop = false; 1.155 + mOnDataCount = 0; 1.156 + gKeepRunning++; 1.157 +} 1.158 + 1.159 +Consumer::~Consumer() { 1.160 + fprintf(stderr, "Consumer::~Consumer -> in\n\n"); 1.161 + 1.162 + if (!mOnStart) { 1.163 + gError = true; 1.164 + fprintf(stderr, "ERROR: Never got an OnStart\n"); 1.165 + } 1.166 + 1.167 + if (!mOnStop) { 1.168 + gError = true; 1.169 + fprintf(stderr, "ERROR: Never got an OnStop \n"); 1.170 + } 1.171 + 1.172 + fprintf(stderr, "Consumer::~Consumer -> out\n\n"); 1.173 + if (--gKeepRunning == 0) 1.174 + QuitPumpingEvents(); 1.175 +} 1.176 + 1.177 +nsresult 1.178 +Consumer::Init(nsIURI *aURI, nsIChannel* aChannel, nsISupports *aContext) { 1.179 + mURI = aURI; 1.180 + mChannel = aChannel; 1.181 + mContext = do_QueryInterface(aContext); 1.182 + return NS_OK; 1.183 +} 1.184 + 1.185 +nsresult 1.186 +Consumer::Validate(nsIRequest* request, nsISupports *aContext) { 1.187 + nsresult rv = NS_OK; 1.188 + nsCOMPtr<nsIURI> uri; 1.189 + nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request); 1.190 + 1.191 + rv = aChannel->GetURI(getter_AddRefs(uri)); 1.192 + if (NS_FAILED(rv)) return rv; 1.193 + 1.194 + bool same = false; 1.195 + 1.196 + rv = mURI->Equals(uri, &same); 1.197 + if (NS_FAILED(rv)) return rv; 1.198 + 1.199 + if (!same) 1.200 + fprintf(stderr, "INFO: URIs do not match\n"); 1.201 + 1.202 + rv = mContext->Equals((void*)aContext, &same); 1.203 + if (NS_FAILED(rv)) return rv; 1.204 + 1.205 + if (!same) { 1.206 + gError = true; 1.207 + fprintf(stderr, "ERROR: Contexts do not match\n"); 1.208 + } 1.209 + return rv; 1.210 +} 1.211 + 1.212 +nsresult StartLoad(const char *); 1.213 + 1.214 +int main(int argc, char *argv[]) { 1.215 + if (test_common_init(&argc, &argv) != 0) 1.216 + return -1; 1.217 + 1.218 + nsresult rv = NS_OK; 1.219 + bool cmdLineURL = false; 1.220 + 1.221 + if (argc > 1) { 1.222 + // run in signle url mode 1.223 + cmdLineURL = true; 1.224 + } 1.225 + 1.226 + rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.227 + if (NS_FAILED(rv)) return -1; 1.228 + 1.229 + if (cmdLineURL) { 1.230 + rv = StartLoad(argv[1]); 1.231 + } else { 1.232 + rv = StartLoad("http://badhostnamexyz/test.txt"); 1.233 + } 1.234 + if (NS_FAILED(rv)) return -1; 1.235 + 1.236 + // Enter the message pump to allow the URL load to proceed. 1.237 + PumpEvents(); 1.238 + 1.239 + NS_ShutdownXPCOM(nullptr); 1.240 + if (gError) { 1.241 + fprintf(stderr, "\n\n-------ERROR-------\n\n"); 1.242 + } 1.243 + return 0; 1.244 +} 1.245 + 1.246 +nsresult StartLoad(const char *aURISpec) { 1.247 + nsresult rv = NS_OK; 1.248 + 1.249 + // create a context 1.250 + ConsumerContext *context = new ConsumerContext; 1.251 + nsCOMPtr<nsISupports> contextSup = do_QueryInterface(context, &rv); 1.252 + if (NS_FAILED(rv)) return rv; 1.253 + 1.254 + 1.255 + nsCOMPtr<nsIIOService> serv = do_GetService(kIOServiceCID, &rv); 1.256 + if (NS_FAILED(rv)) return rv; 1.257 + 1.258 + // create a uri 1.259 + nsCOMPtr<nsIURI> uri; 1.260 + rv = NS_NewURI(getter_AddRefs(uri), aURISpec); 1.261 + if (NS_FAILED(rv)) return rv; 1.262 + 1.263 + // create a channel 1.264 + nsCOMPtr<nsIChannel> channel; 1.265 + rv = serv->NewChannelFromURI(uri, getter_AddRefs(channel)); 1.266 + if (NS_FAILED(rv)) return rv; 1.267 + 1.268 + Consumer *consumer = new Consumer; 1.269 + rv = consumer->Init(uri, channel, contextSup); 1.270 + if (NS_FAILED(rv)) return rv; 1.271 + 1.272 + // kick off the load 1.273 + nsCOMPtr<nsIRequest> request; 1.274 + return channel->AsyncOpen(static_cast<nsIStreamListener*>(consumer), contextSup); 1.275 +}