1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestStreamPump.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,177 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "TestCommon.h" 1.9 +#include "nsIComponentRegistrar.h" 1.10 +#include "nsIStreamTransportService.h" 1.11 +#include "nsIAsyncInputStream.h" 1.12 +#include "nsIProgressEventSink.h" 1.13 +#include "nsIInterfaceRequestor.h" 1.14 +#include "nsIInterfaceRequestorUtils.h" 1.15 +#include "nsIRequest.h" 1.16 +#include "nsIServiceManager.h" 1.17 +#include "nsIComponentManager.h" 1.18 +#include "nsISeekableStream.h" 1.19 +#include "nsCOMPtr.h" 1.20 +#include "nsMemory.h" 1.21 +#include "nsStringAPI.h" 1.22 +#include "nsIFileStreams.h" 1.23 +#include "nsIStreamListener.h" 1.24 +#include "nsIFile.h" 1.25 +#include "nsNetUtil.h" 1.26 +#include "nsAutoLock.h" 1.27 +#include "prlog.h" 1.28 +#include "prprf.h" 1.29 +#include <algorithm> 1.30 + 1.31 +//////////////////////////////////////////////////////////////////////////////// 1.32 + 1.33 +#if defined(PR_LOGGING) 1.34 +// 1.35 +// set NSPR_LOG_MODULES=Test:5 1.36 +// 1.37 +static PRLogModuleInfo *gTestLog = nullptr; 1.38 +#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) 1.39 +#else 1.40 +#define LOG(args) 1.41 +#endif 1.42 + 1.43 +//////////////////////////////////////////////////////////////////////////////// 1.44 + 1.45 +class MyListener : public nsIStreamListener 1.46 +{ 1.47 +public: 1.48 + NS_DECL_ISUPPORTS 1.49 + 1.50 + MyListener() {} 1.51 + virtual ~MyListener() {} 1.52 + 1.53 + NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx) 1.54 + { 1.55 + LOG(("MyListener::OnStartRequest\n")); 1.56 + return NS_OK; 1.57 + } 1.58 + 1.59 + NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx, 1.60 + nsIInputStream *stream, 1.61 + uint64_t offset, uint32_t count) 1.62 + { 1.63 + LOG(("MyListener::OnDataAvailable [offset=%llu count=%u]\n", offset, count)); 1.64 + 1.65 + char buf[500]; 1.66 + nsresult rv; 1.67 + 1.68 + while (count) { 1.69 + uint32_t n, amt = std::min<uint32_t>(count, sizeof(buf)); 1.70 + 1.71 + rv = stream->Read(buf, amt, &n); 1.72 + if (NS_FAILED(rv)) { 1.73 + LOG((" read returned 0x%08x\n", rv)); 1.74 + return rv; 1.75 + } 1.76 + 1.77 + fwrite(buf, n, 1, stdout); 1.78 + printf("\n"); 1.79 + 1.80 + LOG((" read %u bytes\n", n)); 1.81 + count -= n; 1.82 + } 1.83 + 1.84 + return NS_OK; 1.85 + } 1.86 + 1.87 + NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) 1.88 + { 1.89 + LOG(("MyListener::OnStopRequest [status=%x]\n", status)); 1.90 + QuitPumpingEvents(); 1.91 + return NS_OK; 1.92 + } 1.93 +}; 1.94 + 1.95 +NS_IMPL_ISUPPORTS(MyListener, 1.96 + nsIRequestObserver, 1.97 + nsIStreamListener) 1.98 + 1.99 +//////////////////////////////////////////////////////////////////////////////// 1.100 + 1.101 +/** 1.102 + * asynchronously copy file. 1.103 + */ 1.104 +static nsresult 1.105 +RunTest(nsIFile *file, int64_t offset, int64_t length) 1.106 +{ 1.107 + nsresult rv; 1.108 + 1.109 + LOG(("RunTest\n")); 1.110 + 1.111 + nsCOMPtr<nsIInputStream> stream; 1.112 + rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file); 1.113 + if (NS_FAILED(rv)) return rv; 1.114 + 1.115 + nsCOMPtr<nsIInputStreamPump> pump; 1.116 + rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream, offset, length); 1.117 + if (NS_FAILED(rv)) return rv; 1.118 + 1.119 + rv = pump->AsyncRead(new MyListener(), nullptr); 1.120 + if (NS_FAILED(rv)) return rv; 1.121 + 1.122 + PumpEvents(); 1.123 + return NS_OK; 1.124 +} 1.125 + 1.126 +//////////////////////////////////////////////////////////////////////////////// 1.127 + 1.128 +int 1.129 +main(int argc, char* argv[]) 1.130 +{ 1.131 + if (test_common_init(&argc, &argv) != 0) 1.132 + return -1; 1.133 + 1.134 + nsresult rv; 1.135 + 1.136 + if (argc < 4) { 1.137 + printf("usage: %s <file-to-read> <start-offset> <read-length>\n", argv[0]); 1.138 + return -1; 1.139 + } 1.140 + char* fileName = argv[1]; 1.141 + int64_t offset, length; 1.142 + int err = PR_sscanf(argv[2], "%lld", &offset); 1.143 + if (err == -1) { 1.144 + printf("Start offset must be an integer!\n"); 1.145 + return 1; 1.146 + } 1.147 + err = PR_sscanf(argv[3], "%lld", &length); 1.148 + if (err == -1) { 1.149 + printf("Length must be an integer!\n"); 1.150 + return 1; 1.151 + } 1.152 + 1.153 + { 1.154 + nsCOMPtr<nsIServiceManager> servMan; 1.155 + NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.156 + nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); 1.157 + NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); 1.158 + if (registrar) 1.159 + registrar->AutoRegister(nullptr); 1.160 + 1.161 +#if defined(PR_LOGGING) 1.162 + gTestLog = PR_NewLogModule("Test"); 1.163 +#endif 1.164 + 1.165 + nsCOMPtr<nsIFile> file; 1.166 + rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file)); 1.167 + if (NS_FAILED(rv)) return rv; 1.168 + 1.169 + rv = RunTest(file, offset, length); 1.170 + NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed"); 1.171 + 1.172 + // give background threads a chance to finish whatever work they may 1.173 + // be doing. 1.174 + PR_Sleep(PR_SecondsToInterval(1)); 1.175 + } // this scopes the nsCOMPtrs 1.176 + // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM 1.177 + rv = NS_ShutdownXPCOM(nullptr); 1.178 + NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); 1.179 + return NS_OK; 1.180 +}