netwerk/test/TestCacheService.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7
michael@0 8 #include "nspr.h"
michael@0 9 #include "nscore.h"
michael@0 10 #include "nsError.h"
michael@0 11 #include "nsIServiceManager.h"
michael@0 12 #include "nsNetCID.h"
michael@0 13 #include "nsICache.h"
michael@0 14 #include "nsICacheService.h"
michael@0 15 #include "nsICacheSession.h"
michael@0 16 #include "nsICacheEntryDescriptor.h"
michael@0 17 #include "nsICacheListener.h"
michael@0 18 #include "nsIDNSService.h"
michael@0 19 #include "nsXPCOM.h"
michael@0 20 #include "nsISupportsPrimitives.h"
michael@0 21 #include "nsSupportsPrimitives.h"
michael@0 22 #include "nsIEventQueueService.h"
michael@0 23
michael@0 24
michael@0 25 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
michael@0 26 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
michael@0 27
michael@0 28 nsCOMPtr<nsIEventQueue> gEventQ;
michael@0 29 nsCOMPtr<nsICacheService> gCacheService;
michael@0 30
michael@0 31 class AsyncCacheRequest
michael@0 32 {
michael@0 33 public:
michael@0 34 AsyncCacheRequest(const char * key);
michael@0 35 ~AsyncCacheRequest();
michael@0 36
michael@0 37 const char * mKey;
michael@0 38 };
michael@0 39
michael@0 40
michael@0 41 nsresult
michael@0 42 MakeCacheSession(const char * clientID, nsICacheSession **session)
michael@0 43 {
michael@0 44 nsresult rv;
michael@0 45
michael@0 46 if (!gCacheService) {
michael@0 47 // nsCOMPtr<nsICacheService> cacheService =
michael@0 48 // do_GetService(kCacheServiceCID, &rv);
michael@0 49 gCacheService = do_GetService(kCacheServiceCID, &rv);
michael@0 50 if (NS_FAILED(rv) || !gCacheService) {
michael@0 51 printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
michael@0 52 goto error_exit;
michael@0 53 }
michael@0 54 }
michael@0 55
michael@0 56 rv = gCacheService->CreateSession(clientID,
michael@0 57 nsICache::STORE_IN_MEMORY,
michael@0 58 nsICache::NOT_STREAM_BASED,
michael@0 59 session);
michael@0 60 if (NS_FAILED(rv))
michael@0 61 printf("nsCacheService::CreateSession() failed : %x\n", rv);
michael@0 62
michael@0 63 error_exit:
michael@0 64 return rv;
michael@0 65 }
michael@0 66
michael@0 67
michael@0 68 void
michael@0 69 TestMemoryObjectCache()
michael@0 70 {
michael@0 71 printf("\nTesting Memory Object Cache:\n");
michael@0 72 nsCOMPtr<nsICacheSession> session;
michael@0 73 nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
michael@0 74 if (NS_FAILED(rv)) return;
michael@0 75
michael@0 76 nsCOMPtr<nsICacheEntryDescriptor> descriptor;
michael@0 77
michael@0 78 // Test ACCESS_READ for nonexistent entry
michael@0 79 printf("\nTest ACCESS_READ:\n");
michael@0 80 rv = session->OpenCacheEntry(NS_LITERAL_CSTRING("nonexistent entry"),
michael@0 81 nsICache::ACCESS_READ,
michael@0 82 nsICache::BLOCKING,
michael@0 83 getter_AddRefs(descriptor));
michael@0 84 if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
michael@0 85 printf("OpenCacheEntry(ACCESS_READ) returned: %x for nonexistent entry\n", rv);
michael@0 86
michael@0 87 NS_NAMED_LITERAL_CSTRING(cacheKey, "http://www.mozilla.org/somekey");
michael@0 88
michael@0 89 // Test creating new entry
michael@0 90 printf("\nTest ACCESS_READ_WRITE:\n");
michael@0 91 rv = session->OpenCacheEntry(cacheKey,
michael@0 92 nsICache::ACCESS_READ_WRITE,
michael@0 93 nsICache::BLOCKING,
michael@0 94 getter_AddRefs(descriptor));
michael@0 95 if (NS_FAILED(rv)) {
michael@0 96 printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
michael@0 97 goto error_exit;
michael@0 98 }
michael@0 99
michael@0 100 nsCOMPtr<nsISupportsCString> foo =
michael@0 101 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
michael@0 102
michael@0 103 foo->SetData(NS_LITERAL_CSTRING("hello world"));
michael@0 104
michael@0 105 rv = descriptor->SetCacheElement(foo);
michael@0 106 rv = descriptor->SetDataSize(11);
michael@0 107 rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
michael@0 108 descriptor = nullptr;
michael@0 109
michael@0 110 // Test refetching entry
michael@0 111
michael@0 112 rv = session->OpenCacheEntry(cacheKey,
michael@0 113 nsICache::ACCESS_READ_WRITE,
michael@0 114 nsICache::BLOCKING,
michael@0 115 getter_AddRefs(descriptor));
michael@0 116 if (NS_FAILED(rv)) {
michael@0 117 printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
michael@0 118 goto error_exit;
michael@0 119 }
michael@0 120
michael@0 121 nsCOMPtr<nsISupportsCString> bar;
michael@0 122 descriptor->GetCacheElement(getter_AddRefs(bar));
michael@0 123 if (foo.get() != bar.get()) {
michael@0 124 printf("cache elements not the same\n");
michael@0 125 } else {
michael@0 126 printf("data matches...\n");
michael@0 127 }
michael@0 128
michael@0 129 char * metaData;
michael@0 130 rv = descriptor->GetMetaDataElement("itemOne", &metaData);
michael@0 131 if (NS_SUCCEEDED(rv)) printf("metaData = %s\n", metaData);
michael@0 132 else printf("GetMetaDataElement failed : rv = %x\n", rv);
michael@0 133 descriptor = nullptr;
michael@0 134
michael@0 135 // Test ACCESS_WRITE entry
michael@0 136 printf("\nTest ACCESS_WRITE:\n");
michael@0 137 rv = session->OpenCacheEntry(cacheKey,
michael@0 138 nsICache::ACCESS_WRITE,
michael@0 139 nsICache::BLOCKING,
michael@0 140 getter_AddRefs(descriptor));
michael@0 141 if (NS_FAILED(rv)) {
michael@0 142 printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
michael@0 143 goto error_exit;
michael@0 144 }
michael@0 145 rv = descriptor->GetCacheElement(getter_AddRefs(bar));
michael@0 146 if (bar)
michael@0 147 printf("FAILED: we didn't get new entry.\n");
michael@0 148 if (NS_FAILED(rv))
michael@0 149 printf("GetCacheElement failed : %x\n", rv);
michael@0 150
michael@0 151 rv = descriptor->GetMetaDataElement("itemOne", &metaData);
michael@0 152 if (NS_SUCCEEDED(rv))
michael@0 153 if (metaData) printf("metaData = %s\n", metaData);
michael@0 154 else printf("metaData = nullptr\n");
michael@0 155 else printf("GetMetaDataElement failed : rv = %x\n", rv);
michael@0 156
michael@0 157 printf("\n");
michael@0 158 error_exit:
michael@0 159
michael@0 160 return;
michael@0 161 }
michael@0 162
michael@0 163
michael@0 164 int
michael@0 165 main(int argc, char* argv[])
michael@0 166 {
michael@0 167 nsresult rv = NS_OK;
michael@0 168
michael@0 169 // Start up XPCOM
michael@0 170 rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
michael@0 171 if (NS_FAILED(rv)) return rv;
michael@0 172
michael@0 173 /**
michael@0 174 * Create event queue for this thread
michael@0 175 */
michael@0 176 nsCOMPtr<nsIEventQueueService> eventQService =
michael@0 177 do_GetService(kEventQueueServiceCID, &rv);
michael@0 178 if (NS_FAILED(rv)) goto error_exit;
michael@0 179
michael@0 180 eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
michael@0 181
michael@0 182 /**
michael@0 183 * Test Cache
michael@0 184 */
michael@0 185 TestMemoryObjectCache();
michael@0 186
michael@0 187
michael@0 188 error_exit:
michael@0 189 gEventQ = nullptr;
michael@0 190 eventQService = nullptr;
michael@0 191
michael@0 192 NS_ShutdownXPCOM(nullptr);
michael@0 193
michael@0 194 printf("XPCOM shut down.\n\n");
michael@0 195 return rv;
michael@0 196 }
michael@0 197
michael@0 198
michael@0 199

mercurial