netwerk/test/TestUpload.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "TestCommon.h"
     7 #include <algorithm>
     8 #ifdef WIN32 
     9 #include <windows.h>
    10 #endif
    12 #include "nsIComponentRegistrar.h"
    13 #include "nsIIOService.h"
    14 #include "nsIServiceManager.h"
    15 #include "nsNetUtil.h"
    17 #include "nsIUploadChannel.h"
    19 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
    21 #include "prlog.h"
    22 #if defined(PR_LOGGING)
    23 //
    24 // set NSPR_LOG_MODULES=Test:5
    25 //
    26 static PRLogModuleInfo *gTestLog = nullptr;
    27 #endif
    28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
    30 //-----------------------------------------------------------------------------
    31 // InputTestConsumer
    32 //-----------------------------------------------------------------------------
    34 class InputTestConsumer : public nsIStreamListener
    35 {
    36 public:
    38   InputTestConsumer();
    39   virtual ~InputTestConsumer();
    41   NS_DECL_ISUPPORTS
    42   NS_DECL_NSIREQUESTOBSERVER
    43   NS_DECL_NSISTREAMLISTENER
    44 };
    46 InputTestConsumer::InputTestConsumer()
    47 {
    48 }
    50 InputTestConsumer::~InputTestConsumer()
    51 {
    52 }
    54 NS_IMPL_ISUPPORTS(InputTestConsumer,
    55                   nsIStreamListener,
    56                   nsIRequestObserver)
    58 NS_IMETHODIMP
    59 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
    60 {
    61   LOG(("InputTestConsumer::OnStartRequest\n"));
    62   return NS_OK;
    63 }
    65 NS_IMETHODIMP
    66 InputTestConsumer::OnDataAvailable(nsIRequest *request, 
    67                                    nsISupports* context,
    68                                    nsIInputStream *aIStream, 
    69                                    uint64_t aSourceOffset,
    70                                    uint32_t aLength)
    71 {
    72   char buf[1025];
    73   uint32_t amt, size;
    74   nsresult rv;
    76   while (aLength) {
    77     size = std::min<uint32_t>(aLength, sizeof(buf));
    78     rv = aIStream->Read(buf, size, &amt);
    79     if (NS_FAILED(rv)) {
    80       NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv), 
    81                    "The stream should never block.");
    82       return rv;
    83     }
    84     aLength -= amt;
    85   }
    86   return NS_OK;
    87 }
    90 NS_IMETHODIMP
    91 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
    92                                  nsresult aStatus)
    93 {
    94     LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
    95     QuitPumpingEvents();
    96     return NS_OK;
    97 }
   100 int
   101 main(int argc, char* argv[])
   102 {
   103     if (test_common_init(&argc, &argv) != 0)
   104         return -1;
   106     nsresult rv;
   108     if (argc < 2) {
   109         printf("usage: %s <url> <file-to-upload>\n", argv[0]);
   110         return -1;
   111     }
   112     char* uriSpec  = argv[1];
   113     char* fileName = argv[2];
   115 #if defined(PR_LOGGING) 
   116     gTestLog = PR_NewLogModule("Test");
   117 #endif
   119     {
   120         nsCOMPtr<nsIServiceManager> servMan;
   121         NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
   123         // first thing to do is create ourselves a stream that
   124         // is to be uploaded.
   125         nsCOMPtr<nsIInputStream> uploadStream;
   126         rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
   127                                   true,
   128                                   nsDependentCString(fileName)); // XXX UTF-8
   129         if (NS_FAILED(rv)) return -1;
   131         nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
   133         // create our url.
   134         nsCOMPtr<nsIURI> uri;
   135         rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
   136         if (NS_FAILED(rv)) return -1;
   138         nsCOMPtr<nsIChannel> channel;
   139         rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
   140         if (NS_FAILED(rv)) return -1;
   142         // QI and set the upload stream
   143         nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
   144         uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
   146         // create a dummy listener
   147         InputTestConsumer* listener;
   149         listener = new InputTestConsumer;
   150         if (!listener) {
   151             NS_ERROR("Failed to create a new stream listener!");
   152             return -1;
   153         }
   154         NS_ADDREF(listener);
   156         channel->AsyncOpen(listener, nullptr);
   158         PumpEvents();
   159     } // this scopes the nsCOMPtrs
   160     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
   161     rv = NS_ShutdownXPCOM(nullptr);
   162     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
   164     return 0;
   165 }

mercurial