netwerk/test/TestMCTransport.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 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <stdio.h>
michael@0 7
michael@0 8 #include "nsIServiceManager.h"
michael@0 9 #include "nsIEventQueueService.h"
michael@0 10 #include "nsIOutputStream.h"
michael@0 11 #include "nsIStreamListener.h"
michael@0 12 #include "nsITransport.h"
michael@0 13 #include "nsIInputStream.h"
michael@0 14 #include "nsIOutputStream.h"
michael@0 15 #include "nsCOMPtr.h"
michael@0 16 #include "plstr.h"
michael@0 17 #include "prprf.h"
michael@0 18 #include <algorithm>
michael@0 19
michael@0 20 #ifndef USE_CREATE_INSTANCE
michael@0 21 #include "nsICacheService.h"
michael@0 22 #include "nsICacheSession.h"
michael@0 23 #include "nsICacheEntryDescriptor.h"
michael@0 24 #include "nsNetCID.h"
michael@0 25 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
michael@0 26 static nsICacheSession *session = nullptr;
michael@0 27 static nsICacheEntryDescriptor *desc = nullptr;
michael@0 28 #endif
michael@0 29
michael@0 30 /**
michael@0 31 * This test program exercises the memory cache's nsITransport implementation.
michael@0 32 *
michael@0 33 * This test program loads a file into the memory cache (using OpenOutputStream),
michael@0 34 * and then reads the file back out (using AsyncRead). The data read from the
michael@0 35 * memory cache is written to a new file (with .out added as a suffix to the file
michael@0 36 * name).
michael@0 37 */
michael@0 38
michael@0 39 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
michael@0 40 static nsIEventQueue *gEventQ = nullptr;
michael@0 41
michael@0 42 class TestListener : public nsIStreamListener
michael@0 43 {
michael@0 44 public:
michael@0 45 NS_DECL_ISUPPORTS
michael@0 46 NS_DECL_NSISTREAMLISTENER
michael@0 47 NS_DECL_NSIREQUESTOBSERVER
michael@0 48
michael@0 49 TestListener(char *);
michael@0 50 virtual ~TestListener();
michael@0 51
michael@0 52 private:
michael@0 53 char *mFilename;
michael@0 54 FILE *mFile;
michael@0 55 };
michael@0 56
michael@0 57 NS_IMPL_ISUPPORTS(TestListener,
michael@0 58 nsIStreamListener,
michael@0 59 nsIRequestObserver)
michael@0 60
michael@0 61 TestListener::TestListener(char *filename)
michael@0 62 : mFilename(filename)
michael@0 63 , mFile(nullptr)
michael@0 64 {
michael@0 65 }
michael@0 66
michael@0 67 TestListener::~TestListener()
michael@0 68 {
michael@0 69 }
michael@0 70
michael@0 71 NS_IMETHODIMP
michael@0 72 TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
michael@0 73 {
michael@0 74 printf("OnStartRequest\n");
michael@0 75
michael@0 76 mFile = fopen(mFilename, "w");
michael@0 77 if (!mFile)
michael@0 78 return NS_ERROR_FAILURE;
michael@0 79
michael@0 80 return NS_OK;
michael@0 81 }
michael@0 82
michael@0 83 NS_IMETHODIMP
michael@0 84 TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
michael@0 85 {
michael@0 86 printf("OnStopRequest: status=%x\n", status);
michael@0 87
michael@0 88 if (mFile)
michael@0 89 fclose(mFile);
michael@0 90
michael@0 91 return NS_OK;
michael@0 92 }
michael@0 93
michael@0 94 NS_IMETHODIMP
michael@0 95 TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
michael@0 96 nsIInputStream *is,
michael@0 97 uint64_t offset, uint32_t count)
michael@0 98 {
michael@0 99 printf("OnDataAvailable: offset=%llu count=%u\n", offset, count);
michael@0 100
michael@0 101 if (!mFile) return NS_ERROR_FAILURE;
michael@0 102
michael@0 103 char buf[128];
michael@0 104 nsresult rv;
michael@0 105 uint32_t nread = 0;
michael@0 106
michael@0 107 while (count) {
michael@0 108 uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
michael@0 109
michael@0 110 rv = is->Read(buf, amount, &nread);
michael@0 111 if (NS_FAILED(rv)) return rv;
michael@0 112
michael@0 113 fwrite(buf, nread, 1, mFile);
michael@0 114 count -= nread;
michael@0 115 }
michael@0 116 return NS_OK;
michael@0 117 }
michael@0 118
michael@0 119 nsresult TestMCTransport(const char *filename)
michael@0 120 {
michael@0 121 nsresult rv = NS_OK;
michael@0 122 nsCOMPtr<nsITransport> transport;
michael@0 123
michael@0 124 #ifdef USE_CREATE_INSTANCE
michael@0 125 transport = do_CreateInstance(
michael@0 126 "@mozilla.org/network/memory-cache-transport;1", &rv);
michael@0 127 if (NS_FAILED(rv))
michael@0 128 return rv;
michael@0 129 #else
michael@0 130 nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
michael@0 131 if (NS_FAILED(rv)) return rv;
michael@0 132
michael@0 133 rv = serv->CreateSession("TestMCTransport",
michael@0 134 nsICache::STORE_IN_MEMORY, true,
michael@0 135 &session);
michael@0 136 if (NS_FAILED(rv)) return rv;
michael@0 137
michael@0 138 rv = session->OpenCacheEntry(nsDependentCString(filename),
michael@0 139 nsICache::ACCESS_READ_WRITE,
michael@0 140 nsICache::BLOCKING,
michael@0 141 &desc);
michael@0 142 if (NS_FAILED(rv)) return rv;
michael@0 143
michael@0 144 rv = desc->MarkValid();
michael@0 145 if (NS_FAILED(rv)) return rv;
michael@0 146
michael@0 147 rv = desc->GetTransport(getter_AddRefs(transport));
michael@0 148 if (NS_FAILED(rv)) return rv;
michael@0 149 #endif
michael@0 150
michael@0 151 nsCOMPtr<nsIOutputStream> os;
michael@0 152 rv = transport->OpenOutputStream(0, (uint32_t) -1, 0, getter_AddRefs(os));
michael@0 153 if (NS_FAILED(rv)) return rv;
michael@0 154
michael@0 155 char *out = PR_smprintf("%s.out", filename);
michael@0 156 nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
michael@0 157 if (!listener)
michael@0 158 return NS_ERROR_OUT_OF_MEMORY;
michael@0 159
michael@0 160 nsCOMPtr<nsIRequest> req;
michael@0 161 rv = transport->AsyncRead(listener, nullptr, 0, (uint32_t) -1, 0, getter_AddRefs(req));
michael@0 162 if (NS_FAILED(rv)) return rv;
michael@0 163
michael@0 164 FILE *file = fopen(filename, "r");
michael@0 165 if (!file)
michael@0 166 return NS_ERROR_FILE_NOT_FOUND;
michael@0 167
michael@0 168 char buf[256];
michael@0 169 uint32_t count, total=0;
michael@0 170
michael@0 171 while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
michael@0 172 printf("writing %u bytes\n", count);
michael@0 173 total += count;
michael@0 174 rv = os->Write(buf, count, &count);
michael@0 175 if (NS_FAILED(rv)) return rv;
michael@0 176
michael@0 177 // process an event
michael@0 178 PLEvent *event = nullptr;
michael@0 179 gEventQ->GetEvent(&event);
michael@0 180 if (event) gEventQ->HandleEvent(event);
michael@0 181 }
michael@0 182
michael@0 183 printf("wrote %u bytes\n", total);
michael@0 184
michael@0 185 return rv;
michael@0 186 }
michael@0 187
michael@0 188 int main(int argc, char **argv)
michael@0 189 {
michael@0 190 nsresult rv;
michael@0 191
michael@0 192 if (argc < 2) {
michael@0 193 printf("usage: %s filename\n", argv[0]);
michael@0 194 return -1;
michael@0 195 }
michael@0 196
michael@0 197 nsCOMPtr<nsIEventQueueService> eqs =
michael@0 198 do_GetService(kEventQueueServiceCID, &rv);
michael@0 199 if (NS_FAILED(rv)) {
michael@0 200 printf("failed to create event queue service: rv=%x\n", rv);
michael@0 201 return -1;
michael@0 202 }
michael@0 203
michael@0 204 rv = eqs->CreateMonitoredThreadEventQueue();
michael@0 205 if (NS_FAILED(rv)) {
michael@0 206 printf("failed to create monitored event queue: rv=%x\n", rv);
michael@0 207 return -1;
michael@0 208 }
michael@0 209
michael@0 210 rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
michael@0 211 if (NS_FAILED(rv)) {
michael@0 212 printf("failed to get thread event queue: %x\n", rv);
michael@0 213 return -1;
michael@0 214 }
michael@0 215
michael@0 216 rv = TestMCTransport(argv[1]);
michael@0 217 printf("TestMCTransport returned %x\n", rv);
michael@0 218
michael@0 219 gEventQ->ProcessPendingEvents();
michael@0 220
michael@0 221 #ifndef USE_CREATE_INSTANCE
michael@0 222 NS_IF_RELEASE(desc);
michael@0 223 NS_IF_RELEASE(session);
michael@0 224 #endif
michael@0 225 return 0;
michael@0 226 }

mercurial