1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestStreamChannel.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 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 "nsCOMPtr.h" 1.19 +#include "nsMemory.h" 1.20 +#include "nsStringAPI.h" 1.21 +#include "nsIFileStreams.h" 1.22 +#include "nsIStreamListener.h" 1.23 +#include "nsIFile.h" 1.24 +#include "nsNetUtil.h" 1.25 +#include "nsAutoLock.h" 1.26 +#include "prlog.h" 1.27 +#include <algorithm> 1.28 + 1.29 +//////////////////////////////////////////////////////////////////////////////// 1.30 + 1.31 +#if defined(PR_LOGGING) 1.32 +// 1.33 +// set NSPR_LOG_MODULES=Test:5 1.34 +// 1.35 +static PRLogModuleInfo *gTestLog = nullptr; 1.36 +#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) 1.37 +#else 1.38 +#define LOG(args) 1.39 +#endif 1.40 + 1.41 +//////////////////////////////////////////////////////////////////////////////// 1.42 + 1.43 +static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID); 1.44 + 1.45 +//////////////////////////////////////////////////////////////////////////////// 1.46 + 1.47 +class MyListener : public nsIStreamListener 1.48 +{ 1.49 +public: 1.50 + NS_DECL_ISUPPORTS 1.51 + 1.52 + MyListener() {} 1.53 + virtual ~MyListener() {} 1.54 + 1.55 + NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx) 1.56 + { 1.57 + LOG(("MyListener::OnStartRequest\n")); 1.58 + return NS_OK; 1.59 + } 1.60 + 1.61 + NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx, 1.62 + nsIInputStream *stream, 1.63 + uint32_t offset, uint32_t count) 1.64 + { 1.65 + LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset, count)); 1.66 + 1.67 + char buf[500]; 1.68 + nsresult rv; 1.69 + 1.70 + while (count) { 1.71 + uint32_t n, amt = std::min<uint32_t>(count, sizeof(buf)); 1.72 + 1.73 + rv = stream->Read(buf, amt, &n); 1.74 + if (NS_FAILED(rv)) { 1.75 + LOG((" read returned 0x%08x\n", rv)); 1.76 + return rv; 1.77 + } 1.78 + 1.79 + LOG((" read %u bytes\n", n)); 1.80 + count -= n; 1.81 + } 1.82 + 1.83 + return NS_OK; 1.84 + } 1.85 + 1.86 + NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) 1.87 + { 1.88 + LOG(("MyListener::OnStopRequest [status=%x]\n", status)); 1.89 + QuitPumpingEvents(); 1.90 + return NS_OK; 1.91 + } 1.92 +}; 1.93 + 1.94 +NS_IMPL_ISUPPORTS(MyListener, 1.95 + nsIRequestObserver, 1.96 + nsIStreamListener) 1.97 + 1.98 +//////////////////////////////////////////////////////////////////////////////// 1.99 + 1.100 +class MyCallbacks : public nsIInterfaceRequestor 1.101 + , public nsIProgressEventSink 1.102 +{ 1.103 +public: 1.104 + NS_DECL_ISUPPORTS 1.105 + 1.106 + MyCallbacks() {} 1.107 + virtual ~MyCallbacks() {} 1.108 + 1.109 + NS_IMETHOD GetInterface(const nsID &iid, void **result) 1.110 + { 1.111 + return QueryInterface(iid, result); 1.112 + } 1.113 + 1.114 + NS_IMETHOD OnStatus(nsIRequest *req, nsISupports *ctx, nsresult status, 1.115 + const char16_t *statusArg) 1.116 + { 1.117 + LOG(("MyCallbacks::OnStatus [status=%x]\n", status)); 1.118 + return NS_OK; 1.119 + } 1.120 + 1.121 + NS_IMETHOD OnProgress(nsIRequest *req, nsISupports *ctx, 1.122 + uint64_t progress, uint64_t progressMax) 1.123 + { 1.124 + LOG(("MyCallbacks::OnProgress [progress=%llu/%llu]\n", progress, progressMax)); 1.125 + return NS_OK; 1.126 + } 1.127 +}; 1.128 + 1.129 +NS_IMPL_ISUPPORTS(MyCallbacks, 1.130 + nsIInterfaceRequestor, 1.131 + nsIProgressEventSink) 1.132 + 1.133 +//////////////////////////////////////////////////////////////////////////////// 1.134 + 1.135 +/** 1.136 + * asynchronously copy file. 1.137 + */ 1.138 +static nsresult 1.139 +RunTest(nsIFile *file) 1.140 +{ 1.141 + nsresult rv; 1.142 + 1.143 + LOG(("RunTest\n")); 1.144 + 1.145 + nsCOMPtr<nsIInputStream> stream; 1.146 + rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file); 1.147 + if (NS_FAILED(rv)) return rv; 1.148 + 1.149 + nsCOMPtr<nsIURI> uri = do_CreateInstance(kSimpleURICID); 1.150 + if (uri) 1.151 + uri->SetSpec(NS_LITERAL_CSTRING("foo://bar")); 1.152 + 1.153 + nsCOMPtr<nsIChannel> chan; 1.154 + rv = NS_NewInputStreamChannel(getter_AddRefs(chan), uri, stream); 1.155 + if (NS_FAILED(rv)) return rv; 1.156 + 1.157 + rv = chan->SetNotificationCallbacks(new MyCallbacks()); 1.158 + if (NS_FAILED(rv)) return rv; 1.159 + 1.160 + rv = chan->AsyncOpen(new MyListener(), nullptr); 1.161 + if (NS_FAILED(rv)) return rv; 1.162 + 1.163 + PumpEvents(); 1.164 + return NS_OK; 1.165 +} 1.166 + 1.167 +//////////////////////////////////////////////////////////////////////////////// 1.168 + 1.169 +int 1.170 +main(int argc, char* argv[]) 1.171 +{ 1.172 + if (test_common_init(&argc, &argv) != 0) 1.173 + return -1; 1.174 + 1.175 + nsresult rv; 1.176 + 1.177 + if (argc < 2) { 1.178 + printf("usage: %s <file-to-read>\n", argv[0]); 1.179 + return -1; 1.180 + } 1.181 + char* fileName = argv[1]; 1.182 + { 1.183 + nsCOMPtr<nsIServiceManager> servMan; 1.184 + NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.185 + nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); 1.186 + NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); 1.187 + if (registrar) 1.188 + registrar->AutoRegister(nullptr); 1.189 + 1.190 +#if defined(PR_LOGGING) 1.191 + gTestLog = PR_NewLogModule("Test"); 1.192 +#endif 1.193 + 1.194 + nsCOMPtr<nsIFile> file; 1.195 + rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file)); 1.196 + if (NS_FAILED(rv)) return rv; 1.197 + 1.198 + rv = RunTest(file); 1.199 + NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed"); 1.200 + 1.201 + // give background threads a chance to finish whatever work they may 1.202 + // be doing. 1.203 + PR_Sleep(PR_SecondsToInterval(1)); 1.204 + } // this scopes the nsCOMPtrs 1.205 + // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM 1.206 + rv = NS_ShutdownXPCOM(nullptr); 1.207 + NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); 1.208 + return NS_OK; 1.209 +}