1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestUpload.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 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 <algorithm> 1.11 +#ifdef WIN32 1.12 +#include <windows.h> 1.13 +#endif 1.14 + 1.15 +#include "nsIComponentRegistrar.h" 1.16 +#include "nsIIOService.h" 1.17 +#include "nsIServiceManager.h" 1.18 +#include "nsNetUtil.h" 1.19 + 1.20 +#include "nsIUploadChannel.h" 1.21 + 1.22 +static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); 1.23 + 1.24 +#include "prlog.h" 1.25 +#if defined(PR_LOGGING) 1.26 +// 1.27 +// set NSPR_LOG_MODULES=Test:5 1.28 +// 1.29 +static PRLogModuleInfo *gTestLog = nullptr; 1.30 +#endif 1.31 +#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) 1.32 + 1.33 +//----------------------------------------------------------------------------- 1.34 +// InputTestConsumer 1.35 +//----------------------------------------------------------------------------- 1.36 + 1.37 +class InputTestConsumer : public nsIStreamListener 1.38 +{ 1.39 +public: 1.40 + 1.41 + InputTestConsumer(); 1.42 + virtual ~InputTestConsumer(); 1.43 + 1.44 + NS_DECL_ISUPPORTS 1.45 + NS_DECL_NSIREQUESTOBSERVER 1.46 + NS_DECL_NSISTREAMLISTENER 1.47 +}; 1.48 + 1.49 +InputTestConsumer::InputTestConsumer() 1.50 +{ 1.51 +} 1.52 + 1.53 +InputTestConsumer::~InputTestConsumer() 1.54 +{ 1.55 +} 1.56 + 1.57 +NS_IMPL_ISUPPORTS(InputTestConsumer, 1.58 + nsIStreamListener, 1.59 + nsIRequestObserver) 1.60 + 1.61 +NS_IMETHODIMP 1.62 +InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context) 1.63 +{ 1.64 + LOG(("InputTestConsumer::OnStartRequest\n")); 1.65 + return NS_OK; 1.66 +} 1.67 + 1.68 +NS_IMETHODIMP 1.69 +InputTestConsumer::OnDataAvailable(nsIRequest *request, 1.70 + nsISupports* context, 1.71 + nsIInputStream *aIStream, 1.72 + uint64_t aSourceOffset, 1.73 + uint32_t aLength) 1.74 +{ 1.75 + char buf[1025]; 1.76 + uint32_t amt, size; 1.77 + nsresult rv; 1.78 + 1.79 + while (aLength) { 1.80 + size = std::min<uint32_t>(aLength, sizeof(buf)); 1.81 + rv = aIStream->Read(buf, size, &amt); 1.82 + if (NS_FAILED(rv)) { 1.83 + NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv), 1.84 + "The stream should never block."); 1.85 + return rv; 1.86 + } 1.87 + aLength -= amt; 1.88 + } 1.89 + return NS_OK; 1.90 +} 1.91 + 1.92 + 1.93 +NS_IMETHODIMP 1.94 +InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, 1.95 + nsresult aStatus) 1.96 +{ 1.97 + LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus)); 1.98 + QuitPumpingEvents(); 1.99 + return NS_OK; 1.100 +} 1.101 + 1.102 + 1.103 +int 1.104 +main(int argc, char* argv[]) 1.105 +{ 1.106 + if (test_common_init(&argc, &argv) != 0) 1.107 + return -1; 1.108 + 1.109 + nsresult rv; 1.110 + 1.111 + if (argc < 2) { 1.112 + printf("usage: %s <url> <file-to-upload>\n", argv[0]); 1.113 + return -1; 1.114 + } 1.115 + char* uriSpec = argv[1]; 1.116 + char* fileName = argv[2]; 1.117 + 1.118 +#if defined(PR_LOGGING) 1.119 + gTestLog = PR_NewLogModule("Test"); 1.120 +#endif 1.121 + 1.122 + { 1.123 + nsCOMPtr<nsIServiceManager> servMan; 1.124 + NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.125 + 1.126 + // first thing to do is create ourselves a stream that 1.127 + // is to be uploaded. 1.128 + nsCOMPtr<nsIInputStream> uploadStream; 1.129 + rv = NS_NewPostDataStream(getter_AddRefs(uploadStream), 1.130 + true, 1.131 + nsDependentCString(fileName)); // XXX UTF-8 1.132 + if (NS_FAILED(rv)) return -1; 1.133 + 1.134 + nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv)); 1.135 + 1.136 + // create our url. 1.137 + nsCOMPtr<nsIURI> uri; 1.138 + rv = NS_NewURI(getter_AddRefs(uri), uriSpec); 1.139 + if (NS_FAILED(rv)) return -1; 1.140 + 1.141 + nsCOMPtr<nsIChannel> channel; 1.142 + rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel)); 1.143 + if (NS_FAILED(rv)) return -1; 1.144 + 1.145 + // QI and set the upload stream 1.146 + nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel)); 1.147 + uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1); 1.148 + 1.149 + // create a dummy listener 1.150 + InputTestConsumer* listener; 1.151 + 1.152 + listener = new InputTestConsumer; 1.153 + if (!listener) { 1.154 + NS_ERROR("Failed to create a new stream listener!"); 1.155 + return -1; 1.156 + } 1.157 + NS_ADDREF(listener); 1.158 + 1.159 + channel->AsyncOpen(listener, nullptr); 1.160 + 1.161 + PumpEvents(); 1.162 + } // this scopes the nsCOMPtrs 1.163 + // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM 1.164 + rv = NS_ShutdownXPCOM(nullptr); 1.165 + NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); 1.166 + 1.167 + return 0; 1.168 +} 1.169 +