gecko-dev/netwerk/test/TestUpload.cpp
Ehsan Akhgari e368dc9c85 Bug 579517 - Part 1: Automated conversion of NSPR numeric types to stdint types in Gecko; r=bsmedberg
This patch was generated by a script.  Here's the source of the script for
future reference:

function convert() {
echo "Converting $1 to $2..."
find . ! -wholename "*nsprpub*" \
       ! -wholename "*security/nss*" \
       ! -wholename "*/.hg*" \
       ! -wholename "obj-ff-dbg*" \
       ! -name nsXPCOMCID.h \
       ! -name prtypes.h \
         -type f \
      \( -iname "*.cpp" \
         -o -iname "*.h" \
         -o -iname "*.c" \
         -o -iname "*.cc" \
         -o -iname "*.idl" \
         -o -iname "*.ipdl" \
         -o -iname "*.ipdlh" \
         -o -iname "*.mm" \) | \
    xargs -n 1 sed -i -e "s/\b$1\b/$2/g"
}

convert PRInt8 int8_t
convert PRUint8 uint8_t
convert PRInt16 int16_t
convert PRUint16 uint16_t
convert PRInt32 int32_t
convert PRUint32 uint32_t
convert PRInt64 int64_t
convert PRUint64 uint64_t

convert PRIntn int
convert PRUintn unsigned

convert PRSize size_t

convert PROffset32 int32_t
convert PROffset64 int64_t

convert PRPtrdiff ptrdiff_t

convert PRFloat64 double
2012-08-22 11:56:38 -04:00

166 lines
4.3 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "TestCommon.h"
#ifdef WIN32
#include <windows.h>
#endif
#include "nsIComponentRegistrar.h"
#include "nsIIOService.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
#include "nsIUploadChannel.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
#include "prlog.h"
#if defined(PR_LOGGING)
//
// set NSPR_LOG_MODULES=Test:5
//
static PRLogModuleInfo *gTestLog = nullptr;
#endif
#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
//-----------------------------------------------------------------------------
// InputTestConsumer
//-----------------------------------------------------------------------------
class InputTestConsumer : public nsIStreamListener
{
public:
InputTestConsumer();
virtual ~InputTestConsumer();
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
};
InputTestConsumer::InputTestConsumer()
{
}
InputTestConsumer::~InputTestConsumer()
{
}
NS_IMPL_ISUPPORTS2(InputTestConsumer,
nsIStreamListener,
nsIRequestObserver)
NS_IMETHODIMP
InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
{
LOG(("InputTestConsumer::OnStartRequest\n"));
return NS_OK;
}
NS_IMETHODIMP
InputTestConsumer::OnDataAvailable(nsIRequest *request,
nsISupports* context,
nsIInputStream *aIStream,
uint32_t aSourceOffset,
uint32_t aLength)
{
char buf[1025];
uint32_t amt, size;
nsresult rv;
while (aLength) {
size = NS_MIN<uint32_t>(aLength, sizeof(buf));
rv = aIStream->Read(buf, size, &amt);
if (NS_FAILED(rv)) {
NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv),
"The stream should never block.");
return rv;
}
aLength -= amt;
}
return NS_OK;
}
NS_IMETHODIMP
InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
nsresult aStatus)
{
LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
QuitPumpingEvents();
return NS_OK;
}
int
main(int argc, char* argv[])
{
if (test_common_init(&argc, &argv) != 0)
return -1;
nsresult rv;
if (argc < 2) {
printf("usage: %s <url> <file-to-upload>\n", argv[0]);
return -1;
}
char* uriSpec = argv[1];
char* fileName = argv[2];
#if defined(PR_LOGGING)
gTestLog = PR_NewLogModule("Test");
#endif
{
nsCOMPtr<nsIServiceManager> servMan;
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
// first thing to do is create ourselves a stream that
// is to be uploaded.
nsCOMPtr<nsIInputStream> uploadStream;
rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
true,
nsDependentCString(fileName), // XXX UTF-8
0, ioService);
if (NS_FAILED(rv)) return rv;
// create our url.
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIChannel> channel;
rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
if (NS_FAILED(rv)) return rv;
// QI and set the upload stream
nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
// create a dummy listener
InputTestConsumer* listener;
listener = new InputTestConsumer;
if (!listener) {
NS_ERROR("Failed to create a new stream listener!");
return -1;
}
NS_ADDREF(listener);
channel->AsyncOpen(listener, nullptr);
PumpEvents();
} // this scopes the nsCOMPtrs
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
rv = NS_ShutdownXPCOM(nullptr);
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
return 0;
}