netwerk/test/TestStreamPump.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

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "TestCommon.h"
michael@0 6 #include "nsIComponentRegistrar.h"
michael@0 7 #include "nsIStreamTransportService.h"
michael@0 8 #include "nsIAsyncInputStream.h"
michael@0 9 #include "nsIProgressEventSink.h"
michael@0 10 #include "nsIInterfaceRequestor.h"
michael@0 11 #include "nsIInterfaceRequestorUtils.h"
michael@0 12 #include "nsIRequest.h"
michael@0 13 #include "nsIServiceManager.h"
michael@0 14 #include "nsIComponentManager.h"
michael@0 15 #include "nsISeekableStream.h"
michael@0 16 #include "nsCOMPtr.h"
michael@0 17 #include "nsMemory.h"
michael@0 18 #include "nsStringAPI.h"
michael@0 19 #include "nsIFileStreams.h"
michael@0 20 #include "nsIStreamListener.h"
michael@0 21 #include "nsIFile.h"
michael@0 22 #include "nsNetUtil.h"
michael@0 23 #include "nsAutoLock.h"
michael@0 24 #include "prlog.h"
michael@0 25 #include "prprf.h"
michael@0 26 #include <algorithm>
michael@0 27
michael@0 28 ////////////////////////////////////////////////////////////////////////////////
michael@0 29
michael@0 30 #if defined(PR_LOGGING)
michael@0 31 //
michael@0 32 // set NSPR_LOG_MODULES=Test:5
michael@0 33 //
michael@0 34 static PRLogModuleInfo *gTestLog = nullptr;
michael@0 35 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
michael@0 36 #else
michael@0 37 #define LOG(args)
michael@0 38 #endif
michael@0 39
michael@0 40 ////////////////////////////////////////////////////////////////////////////////
michael@0 41
michael@0 42 class MyListener : public nsIStreamListener
michael@0 43 {
michael@0 44 public:
michael@0 45 NS_DECL_ISUPPORTS
michael@0 46
michael@0 47 MyListener() {}
michael@0 48 virtual ~MyListener() {}
michael@0 49
michael@0 50 NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx)
michael@0 51 {
michael@0 52 LOG(("MyListener::OnStartRequest\n"));
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx,
michael@0 57 nsIInputStream *stream,
michael@0 58 uint64_t offset, uint32_t count)
michael@0 59 {
michael@0 60 LOG(("MyListener::OnDataAvailable [offset=%llu count=%u]\n", offset, count));
michael@0 61
michael@0 62 char buf[500];
michael@0 63 nsresult rv;
michael@0 64
michael@0 65 while (count) {
michael@0 66 uint32_t n, amt = std::min<uint32_t>(count, sizeof(buf));
michael@0 67
michael@0 68 rv = stream->Read(buf, amt, &n);
michael@0 69 if (NS_FAILED(rv)) {
michael@0 70 LOG((" read returned 0x%08x\n", rv));
michael@0 71 return rv;
michael@0 72 }
michael@0 73
michael@0 74 fwrite(buf, n, 1, stdout);
michael@0 75 printf("\n");
michael@0 76
michael@0 77 LOG((" read %u bytes\n", n));
michael@0 78 count -= n;
michael@0 79 }
michael@0 80
michael@0 81 return NS_OK;
michael@0 82 }
michael@0 83
michael@0 84 NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
michael@0 85 {
michael@0 86 LOG(("MyListener::OnStopRequest [status=%x]\n", status));
michael@0 87 QuitPumpingEvents();
michael@0 88 return NS_OK;
michael@0 89 }
michael@0 90 };
michael@0 91
michael@0 92 NS_IMPL_ISUPPORTS(MyListener,
michael@0 93 nsIRequestObserver,
michael@0 94 nsIStreamListener)
michael@0 95
michael@0 96 ////////////////////////////////////////////////////////////////////////////////
michael@0 97
michael@0 98 /**
michael@0 99 * asynchronously copy file.
michael@0 100 */
michael@0 101 static nsresult
michael@0 102 RunTest(nsIFile *file, int64_t offset, int64_t length)
michael@0 103 {
michael@0 104 nsresult rv;
michael@0 105
michael@0 106 LOG(("RunTest\n"));
michael@0 107
michael@0 108 nsCOMPtr<nsIInputStream> stream;
michael@0 109 rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
michael@0 110 if (NS_FAILED(rv)) return rv;
michael@0 111
michael@0 112 nsCOMPtr<nsIInputStreamPump> pump;
michael@0 113 rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream, offset, length);
michael@0 114 if (NS_FAILED(rv)) return rv;
michael@0 115
michael@0 116 rv = pump->AsyncRead(new MyListener(), nullptr);
michael@0 117 if (NS_FAILED(rv)) return rv;
michael@0 118
michael@0 119 PumpEvents();
michael@0 120 return NS_OK;
michael@0 121 }
michael@0 122
michael@0 123 ////////////////////////////////////////////////////////////////////////////////
michael@0 124
michael@0 125 int
michael@0 126 main(int argc, char* argv[])
michael@0 127 {
michael@0 128 if (test_common_init(&argc, &argv) != 0)
michael@0 129 return -1;
michael@0 130
michael@0 131 nsresult rv;
michael@0 132
michael@0 133 if (argc < 4) {
michael@0 134 printf("usage: %s <file-to-read> <start-offset> <read-length>\n", argv[0]);
michael@0 135 return -1;
michael@0 136 }
michael@0 137 char* fileName = argv[1];
michael@0 138 int64_t offset, length;
michael@0 139 int err = PR_sscanf(argv[2], "%lld", &offset);
michael@0 140 if (err == -1) {
michael@0 141 printf("Start offset must be an integer!\n");
michael@0 142 return 1;
michael@0 143 }
michael@0 144 err = PR_sscanf(argv[3], "%lld", &length);
michael@0 145 if (err == -1) {
michael@0 146 printf("Length must be an integer!\n");
michael@0 147 return 1;
michael@0 148 }
michael@0 149
michael@0 150 {
michael@0 151 nsCOMPtr<nsIServiceManager> servMan;
michael@0 152 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
michael@0 153 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
michael@0 154 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
michael@0 155 if (registrar)
michael@0 156 registrar->AutoRegister(nullptr);
michael@0 157
michael@0 158 #if defined(PR_LOGGING)
michael@0 159 gTestLog = PR_NewLogModule("Test");
michael@0 160 #endif
michael@0 161
michael@0 162 nsCOMPtr<nsIFile> file;
michael@0 163 rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
michael@0 164 if (NS_FAILED(rv)) return rv;
michael@0 165
michael@0 166 rv = RunTest(file, offset, length);
michael@0 167 NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
michael@0 168
michael@0 169 // give background threads a chance to finish whatever work they may
michael@0 170 // be doing.
michael@0 171 PR_Sleep(PR_SecondsToInterval(1));
michael@0 172 } // this scopes the nsCOMPtrs
michael@0 173 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
michael@0 174 rv = NS_ShutdownXPCOM(nullptr);
michael@0 175 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
michael@0 176 return NS_OK;
michael@0 177 }

mercurial