Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "TestCommon.h" |
michael@0 | 7 | #include <stdio.h> |
michael@0 | 8 | #ifdef WIN32 |
michael@0 | 9 | #include <windows.h> |
michael@0 | 10 | #endif |
michael@0 | 11 | #include "nspr.h" |
michael@0 | 12 | #include "nscore.h" |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | #include "nsIIOService.h" |
michael@0 | 15 | #include "nsIServiceManager.h" |
michael@0 | 16 | #include "nsIStreamListener.h" |
michael@0 | 17 | #include "nsIInputStream.h" |
michael@0 | 18 | #include "nsIChannel.h" |
michael@0 | 19 | #include "nsIURL.h" |
michael@0 | 20 | #include "nsIInterfaceRequestor.h" |
michael@0 | 21 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 22 | #include "nsIDNSService.h" |
michael@0 | 23 | #include "mozilla/Attributes.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "nsISimpleEnumerator.h" |
michael@0 | 26 | #include "nsNetUtil.h" |
michael@0 | 27 | #include "nsStringAPI.h" |
michael@0 | 28 | |
michael@0 | 29 | static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); |
michael@0 | 30 | |
michael@0 | 31 | static bool gError = false; |
michael@0 | 32 | static int32_t gKeepRunning = 0; |
michael@0 | 33 | |
michael@0 | 34 | #define NS_IEQUALS_IID \ |
michael@0 | 35 | { 0x11c5c8ee, 0x1dd2, 0x11b2, \ |
michael@0 | 36 | { 0xa8, 0x93, 0xbb, 0x23, 0xa1, 0xb6, 0x27, 0x76 }} |
michael@0 | 37 | |
michael@0 | 38 | class nsIEquals : public nsISupports { |
michael@0 | 39 | public: |
michael@0 | 40 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_IEQUALS_IID) |
michael@0 | 41 | NS_IMETHOD Equals(void *aPtr, bool *_retval) = 0; |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | NS_DEFINE_STATIC_IID_ACCESSOR(nsIEquals, NS_IEQUALS_IID) |
michael@0 | 45 | |
michael@0 | 46 | class ConsumerContext MOZ_FINAL : public nsIEquals { |
michael@0 | 47 | public: |
michael@0 | 48 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 49 | |
michael@0 | 50 | ConsumerContext() { } |
michael@0 | 51 | |
michael@0 | 52 | NS_IMETHOD Equals(void *aPtr, bool *_retval) { |
michael@0 | 53 | *_retval = true; |
michael@0 | 54 | if (aPtr != this) *_retval = false; |
michael@0 | 55 | return NS_OK; |
michael@0 | 56 | } |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | NS_IMPL_ISUPPORTS(ConsumerContext, nsIEquals) |
michael@0 | 60 | |
michael@0 | 61 | class Consumer : public nsIStreamListener { |
michael@0 | 62 | public: |
michael@0 | 63 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 64 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 65 | NS_DECL_NSISTREAMLISTENER |
michael@0 | 66 | |
michael@0 | 67 | Consumer(); |
michael@0 | 68 | virtual ~Consumer(); |
michael@0 | 69 | nsresult Init(nsIURI *aURI, nsIChannel *aChannel, nsISupports *aContext); |
michael@0 | 70 | nsresult Validate(nsIRequest *request, nsISupports *aContext); |
michael@0 | 71 | |
michael@0 | 72 | // member data |
michael@0 | 73 | bool mOnStart; // have we received an OnStart? |
michael@0 | 74 | bool mOnStop; // have we received an onStop? |
michael@0 | 75 | int32_t mOnDataCount; // number of times OnData was called. |
michael@0 | 76 | nsCOMPtr<nsIURI> mURI; |
michael@0 | 77 | nsCOMPtr<nsIChannel> mChannel; |
michael@0 | 78 | nsCOMPtr<nsIEquals> mContext; |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | // nsISupports implementation |
michael@0 | 82 | NS_IMPL_ISUPPORTS(Consumer, nsIStreamListener, nsIRequestObserver) |
michael@0 | 83 | |
michael@0 | 84 | |
michael@0 | 85 | // nsIRequestObserver implementation |
michael@0 | 86 | NS_IMETHODIMP |
michael@0 | 87 | Consumer::OnStartRequest(nsIRequest *request, nsISupports* aContext) { |
michael@0 | 88 | fprintf(stderr, "Consumer::OnStart() -> in\n\n"); |
michael@0 | 89 | |
michael@0 | 90 | if (mOnStart) { |
michael@0 | 91 | fprintf(stderr, "INFO: multiple OnStarts received\n"); |
michael@0 | 92 | } |
michael@0 | 93 | mOnStart = true; |
michael@0 | 94 | |
michael@0 | 95 | nsresult rv = Validate(request, aContext); |
michael@0 | 96 | if (NS_FAILED(rv)) return rv; |
michael@0 | 97 | |
michael@0 | 98 | fprintf(stderr, "Consumer::OnStart() -> out\n\n"); |
michael@0 | 99 | return rv; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | NS_IMETHODIMP |
michael@0 | 103 | Consumer::OnStopRequest(nsIRequest *request, nsISupports *aContext, |
michael@0 | 104 | nsresult aStatus) { |
michael@0 | 105 | fprintf(stderr, "Consumer::OnStop() -> in\n\n"); |
michael@0 | 106 | |
michael@0 | 107 | if (!mOnStart) { |
michael@0 | 108 | gError = true; |
michael@0 | 109 | fprintf(stderr, "ERROR: No OnStart received\n"); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | if (mOnStop) { |
michael@0 | 113 | fprintf(stderr, "INFO: multiple OnStops received\n"); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | fprintf(stderr, "INFO: received %d OnData()s\n", mOnDataCount); |
michael@0 | 117 | |
michael@0 | 118 | mOnStop = true; |
michael@0 | 119 | |
michael@0 | 120 | nsresult rv = Validate(request, aContext); |
michael@0 | 121 | if (NS_FAILED(rv)) return rv; |
michael@0 | 122 | |
michael@0 | 123 | fprintf(stderr, "Consumer::OnStop() -> out\n\n"); |
michael@0 | 124 | return rv; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | |
michael@0 | 128 | // nsIStreamListener implementation |
michael@0 | 129 | NS_IMETHODIMP |
michael@0 | 130 | Consumer::OnDataAvailable(nsIRequest *request, nsISupports *aContext, |
michael@0 | 131 | nsIInputStream *aIStream, |
michael@0 | 132 | uint64_t aOffset, uint32_t aLength) { |
michael@0 | 133 | fprintf(stderr, "Consumer::OnData() -> in\n\n"); |
michael@0 | 134 | |
michael@0 | 135 | if (!mOnStart) { |
michael@0 | 136 | gError = true; |
michael@0 | 137 | fprintf(stderr, "ERROR: No OnStart received\n"); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | mOnDataCount += 1; |
michael@0 | 141 | |
michael@0 | 142 | nsresult rv = Validate(request, aContext); |
michael@0 | 143 | if (NS_FAILED(rv)) return rv; |
michael@0 | 144 | |
michael@0 | 145 | fprintf(stderr, "Consumer::OnData() -> out\n\n"); |
michael@0 | 146 | return rv; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | // Consumer implementation |
michael@0 | 150 | Consumer::Consumer() { |
michael@0 | 151 | mOnStart = mOnStop = false; |
michael@0 | 152 | mOnDataCount = 0; |
michael@0 | 153 | gKeepRunning++; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | Consumer::~Consumer() { |
michael@0 | 157 | fprintf(stderr, "Consumer::~Consumer -> in\n\n"); |
michael@0 | 158 | |
michael@0 | 159 | if (!mOnStart) { |
michael@0 | 160 | gError = true; |
michael@0 | 161 | fprintf(stderr, "ERROR: Never got an OnStart\n"); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | if (!mOnStop) { |
michael@0 | 165 | gError = true; |
michael@0 | 166 | fprintf(stderr, "ERROR: Never got an OnStop \n"); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | fprintf(stderr, "Consumer::~Consumer -> out\n\n"); |
michael@0 | 170 | if (--gKeepRunning == 0) |
michael@0 | 171 | QuitPumpingEvents(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | nsresult |
michael@0 | 175 | Consumer::Init(nsIURI *aURI, nsIChannel* aChannel, nsISupports *aContext) { |
michael@0 | 176 | mURI = aURI; |
michael@0 | 177 | mChannel = aChannel; |
michael@0 | 178 | mContext = do_QueryInterface(aContext); |
michael@0 | 179 | return NS_OK; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | nsresult |
michael@0 | 183 | Consumer::Validate(nsIRequest* request, nsISupports *aContext) { |
michael@0 | 184 | nsresult rv = NS_OK; |
michael@0 | 185 | nsCOMPtr<nsIURI> uri; |
michael@0 | 186 | nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request); |
michael@0 | 187 | |
michael@0 | 188 | rv = aChannel->GetURI(getter_AddRefs(uri)); |
michael@0 | 189 | if (NS_FAILED(rv)) return rv; |
michael@0 | 190 | |
michael@0 | 191 | bool same = false; |
michael@0 | 192 | |
michael@0 | 193 | rv = mURI->Equals(uri, &same); |
michael@0 | 194 | if (NS_FAILED(rv)) return rv; |
michael@0 | 195 | |
michael@0 | 196 | if (!same) |
michael@0 | 197 | fprintf(stderr, "INFO: URIs do not match\n"); |
michael@0 | 198 | |
michael@0 | 199 | rv = mContext->Equals((void*)aContext, &same); |
michael@0 | 200 | if (NS_FAILED(rv)) return rv; |
michael@0 | 201 | |
michael@0 | 202 | if (!same) { |
michael@0 | 203 | gError = true; |
michael@0 | 204 | fprintf(stderr, "ERROR: Contexts do not match\n"); |
michael@0 | 205 | } |
michael@0 | 206 | return rv; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | nsresult StartLoad(const char *); |
michael@0 | 210 | |
michael@0 | 211 | int main(int argc, char *argv[]) { |
michael@0 | 212 | if (test_common_init(&argc, &argv) != 0) |
michael@0 | 213 | return -1; |
michael@0 | 214 | |
michael@0 | 215 | nsresult rv = NS_OK; |
michael@0 | 216 | bool cmdLineURL = false; |
michael@0 | 217 | |
michael@0 | 218 | if (argc > 1) { |
michael@0 | 219 | // run in signle url mode |
michael@0 | 220 | cmdLineURL = true; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
michael@0 | 224 | if (NS_FAILED(rv)) return -1; |
michael@0 | 225 | |
michael@0 | 226 | if (cmdLineURL) { |
michael@0 | 227 | rv = StartLoad(argv[1]); |
michael@0 | 228 | } else { |
michael@0 | 229 | rv = StartLoad("http://badhostnamexyz/test.txt"); |
michael@0 | 230 | } |
michael@0 | 231 | if (NS_FAILED(rv)) return -1; |
michael@0 | 232 | |
michael@0 | 233 | // Enter the message pump to allow the URL load to proceed. |
michael@0 | 234 | PumpEvents(); |
michael@0 | 235 | |
michael@0 | 236 | NS_ShutdownXPCOM(nullptr); |
michael@0 | 237 | if (gError) { |
michael@0 | 238 | fprintf(stderr, "\n\n-------ERROR-------\n\n"); |
michael@0 | 239 | } |
michael@0 | 240 | return 0; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | nsresult StartLoad(const char *aURISpec) { |
michael@0 | 244 | nsresult rv = NS_OK; |
michael@0 | 245 | |
michael@0 | 246 | // create a context |
michael@0 | 247 | ConsumerContext *context = new ConsumerContext; |
michael@0 | 248 | nsCOMPtr<nsISupports> contextSup = do_QueryInterface(context, &rv); |
michael@0 | 249 | if (NS_FAILED(rv)) return rv; |
michael@0 | 250 | |
michael@0 | 251 | |
michael@0 | 252 | nsCOMPtr<nsIIOService> serv = do_GetService(kIOServiceCID, &rv); |
michael@0 | 253 | if (NS_FAILED(rv)) return rv; |
michael@0 | 254 | |
michael@0 | 255 | // create a uri |
michael@0 | 256 | nsCOMPtr<nsIURI> uri; |
michael@0 | 257 | rv = NS_NewURI(getter_AddRefs(uri), aURISpec); |
michael@0 | 258 | if (NS_FAILED(rv)) return rv; |
michael@0 | 259 | |
michael@0 | 260 | // create a channel |
michael@0 | 261 | nsCOMPtr<nsIChannel> channel; |
michael@0 | 262 | rv = serv->NewChannelFromURI(uri, getter_AddRefs(channel)); |
michael@0 | 263 | if (NS_FAILED(rv)) return rv; |
michael@0 | 264 | |
michael@0 | 265 | Consumer *consumer = new Consumer; |
michael@0 | 266 | rv = consumer->Init(uri, channel, contextSup); |
michael@0 | 267 | if (NS_FAILED(rv)) return rv; |
michael@0 | 268 | |
michael@0 | 269 | // kick off the load |
michael@0 | 270 | nsCOMPtr<nsIRequest> request; |
michael@0 | 271 | return channel->AsyncOpen(static_cast<nsIStreamListener*>(consumer), contextSup); |
michael@0 | 272 | } |