netwerk/test/unit/test_bug767025.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: Javasript; indent-tab-mode: nil; js-indent-level: 2 -*- */
michael@0 2
michael@0 3 Cu.import("resource://testing-common/httpd.js");
michael@0 4
michael@0 5 /**
michael@0 6 * This is testcase do following steps to make sure bug767025 removing
michael@0 7 * files as expection.
michael@0 8 *
michael@0 9 * STEPS:
michael@0 10 * - Schedule a offline cache update for app.manifest.
michael@0 11 * - pages/foo1, pages/foo2, pages/foo3, and pages/foo4 are cached.
michael@0 12 * - Activate pages/foo1
michael@0 13 * - Doom pages/foo1, and pages/foo2.
michael@0 14 * - pages/foo1 should keep alive while pages/foo2 was gone.
michael@0 15 * - Activate pages/foo3
michael@0 16 * - Evict all documents.
michael@0 17 * - all documents except pages/foo1 are gone since pages/foo1 & pages/foo3
michael@0 18 * are activated.
michael@0 19 */
michael@0 20
michael@0 21 Cu.import("resource://gre/modules/Services.jsm");
michael@0 22
michael@0 23 const kNS_OFFLINECACHEUPDATESERVICE_CONTRACTID =
michael@0 24 "@mozilla.org/offlinecacheupdate-service;1";
michael@0 25 const kNS_CACHESERVICE_CONTRACTID =
michael@0 26 "@mozilla.org/network/cache-service;1";
michael@0 27 const kNS_APPLICATIONCACHESERVICE_CONTRACTID =
michael@0 28 "@mozilla.org/network/application-cache-service;1";
michael@0 29
michael@0 30 const kManifest = "CACHE MANIFEST\n" +
michael@0 31 "/pages/foo1\n" +
michael@0 32 "/pages/foo2\n" +
michael@0 33 "/pages/foo3\n" +
michael@0 34 "/pages/foo4\n";
michael@0 35
michael@0 36 const kDataFileSize = 1024; // file size for each content page
michael@0 37 const kHttpLocation = "http://localhost:4444/";
michael@0 38
michael@0 39 function manifest_handler(metadata, response) {
michael@0 40 do_print("manifest\n");
michael@0 41 response.setHeader("content-type", "text/cache-manifest");
michael@0 42
michael@0 43 response.write(kManifest);
michael@0 44 }
michael@0 45
michael@0 46 function datafile_handler(metadata, response) {
michael@0 47 do_print("datafile_handler\n");
michael@0 48 let data = "";
michael@0 49
michael@0 50 while(data.length < kDataFileSize) {
michael@0 51 data = data + Math.random().toString(36).substring(2, 15);
michael@0 52 }
michael@0 53
michael@0 54 response.setHeader("content-type", "text/plain");
michael@0 55 response.write(data.substring(0, kDataFileSize));
michael@0 56 }
michael@0 57
michael@0 58 function app_handler(metadata, response) {
michael@0 59 do_print("app_handler\n");
michael@0 60 response.setHeader("content-type", "text/html");
michael@0 61
michael@0 62 response.write("<html></html>");
michael@0 63 }
michael@0 64
michael@0 65 var httpServer;
michael@0 66
michael@0 67 function init_profile() {
michael@0 68 var ps = Cc["@mozilla.org/preferences-service;1"]
michael@0 69 .getService(Ci.nsIPrefBranch);
michael@0 70 dump(ps.getBoolPref("browser.cache.offline.enable"));
michael@0 71 ps.setBoolPref("browser.cache.offline.enable", true);
michael@0 72 ps.setComplexValue("browser.cache.offline.parent_directory",
michael@0 73 Ci.nsILocalFile, do_get_profile());
michael@0 74 do_print("profile " + do_get_profile());
michael@0 75 }
michael@0 76
michael@0 77 function init_http_server() {
michael@0 78 httpServer = new HttpServer();
michael@0 79 httpServer.registerPathHandler("/app.appcache", manifest_handler);
michael@0 80 httpServer.registerPathHandler("/app", app_handler);
michael@0 81 for (i = 1; i <= 4; i++) {
michael@0 82 httpServer.registerPathHandler("/pages/foo" + i, datafile_handler);
michael@0 83 }
michael@0 84 httpServer.start(4444);
michael@0 85 }
michael@0 86
michael@0 87 function clean_app_cache() {
michael@0 88 let cache_service = Cc[kNS_CACHESERVICE_CONTRACTID].
michael@0 89 getService(Ci.nsICacheService);
michael@0 90 cache_service.evictEntries(Ci.nsICache.STORE_OFFLINE);
michael@0 91 }
michael@0 92
michael@0 93 function do_app_cache(manifestURL, pageURL) {
michael@0 94 let update_service = Cc[kNS_OFFLINECACHEUPDATESERVICE_CONTRACTID].
michael@0 95 getService(Ci.nsIOfflineCacheUpdateService);
michael@0 96
michael@0 97 Services.perms.add(manifestURL,
michael@0 98 "offline-app",
michael@0 99 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 100
michael@0 101 let update =
michael@0 102 update_service.scheduleUpdate(manifestURL,
michael@0 103 pageURL,
michael@0 104 null); /* no window */
michael@0 105
michael@0 106 return update;
michael@0 107 }
michael@0 108
michael@0 109 function watch_update(update, stateChangeHandler, cacheAvailHandler) {
michael@0 110 let observer = {
michael@0 111 QueryInterface: function QueryInterface(iftype) {
michael@0 112 return this;
michael@0 113 },
michael@0 114
michael@0 115 updateStateChanged: stateChangeHandler,
michael@0 116 applicationCacheAvailable: cacheAvailHandler
michael@0 117 };~
michael@0 118 update.addObserver(observer, false);
michael@0 119
michael@0 120 return update;
michael@0 121 }
michael@0 122
michael@0 123 function start_and_watch_app_cache(manifestURL,
michael@0 124 pageURL,
michael@0 125 stateChangeHandler,
michael@0 126 cacheAvailHandler) {
michael@0 127 let ioService = Cc["@mozilla.org/network/io-service;1"].
michael@0 128 getService(Ci.nsIIOService);
michael@0 129 let update = do_app_cache(ioService.newURI(manifestURL, null, null),
michael@0 130 ioService.newURI(pageURL, null, null));
michael@0 131 watch_update(update, stateChangeHandler, cacheAvailHandler);
michael@0 132 return update;
michael@0 133 }
michael@0 134
michael@0 135 const {STATE_FINISHED: STATE_FINISHED,
michael@0 136 STATE_CHECKING: STATE_CHECKING,
michael@0 137 STATE_ERROR: STATE_ERROR } = Ci.nsIOfflineCacheUpdateObserver;
michael@0 138
michael@0 139 /*
michael@0 140 * Start caching app1 as a non-pinned app.
michael@0 141 */
michael@0 142 function start_cache_nonpinned_app() {
michael@0 143 do_print("Start non-pinned App1");
michael@0 144 start_and_watch_app_cache(kHttpLocation + "app.appcache",
michael@0 145 kHttpLocation + "app",
michael@0 146 function (update, state) {
michael@0 147 switch(state) {
michael@0 148 case STATE_FINISHED:
michael@0 149 check_bug();
michael@0 150 break;
michael@0 151
michael@0 152 case STATE_ERROR:
michael@0 153 do_throw("App cache state = " + state);
michael@0 154 break;
michael@0 155 }
michael@0 156 },
michael@0 157 function (appcahe) {
michael@0 158 do_print("app avail " + appcache + "\n");
michael@0 159 });
michael@0 160 }
michael@0 161
michael@0 162 var hold_entry_foo1 = null;
michael@0 163
michael@0 164 function check_bug() {
michael@0 165 // activate foo1
michael@0 166 asyncOpenCacheEntry(
michael@0 167 kHttpLocation + "pages/foo1",
michael@0 168 "appcache", Ci.nsICacheStorage.OPEN_READONLY, null,
michael@0 169 function(status, entry, appcache) {
michael@0 170 let storage = get_cache_service().appCacheStorage(LoadContextInfo.default, appcache);
michael@0 171
michael@0 172 // Doom foo1 & foo2
michael@0 173 storage.asyncDoomURI(createURI(kHttpLocation + "pages/foo1"), "", { onCacheEntryDoomed: function() {
michael@0 174 storage.asyncDoomURI(createURI(kHttpLocation + "pages/foo2"), "", { onCacheEntryDoomed: function() {
michael@0 175 check_evict_cache(appcache);
michael@0 176 }});
michael@0 177 }});
michael@0 178
michael@0 179 hold_entry_foo1 = entry;
michael@0 180 });
michael@0 181 }
michael@0 182
michael@0 183 function check_evict_cache(appcache) {
michael@0 184 // Only foo2 should be removed.
michael@0 185 let file = do_get_profile().clone();
michael@0 186 file.append("OfflineCache");
michael@0 187 file.append("5");
michael@0 188 file.append("9");
michael@0 189 file.append("8379C6596B8CA4-0");
michael@0 190 do_check_eq(file.exists(), true);
michael@0 191
michael@0 192 file = do_get_profile().clone();
michael@0 193 file.append("OfflineCache");
michael@0 194 file.append("C");
michael@0 195 file.append("2");
michael@0 196 file.append("5F356A168B5E3B-0");
michael@0 197 do_check_eq(file.exists(), false);
michael@0 198
michael@0 199 // activate foo3
michael@0 200 asyncOpenCacheEntry(
michael@0 201 kHttpLocation + "pages/foo3",
michael@0 202 "appcache", Ci.nsICacheStorage.OPEN_READONLY, null,
michael@0 203 function(status, entry, appcache) {
michael@0 204 hold_entry_foo3 = entry;
michael@0 205
michael@0 206 // evict all documents.
michael@0 207 let storage = get_cache_service().appCacheStorage(LoadContextInfo.default, appcache);
michael@0 208 storage.asyncEvictStorage(null);
michael@0 209
michael@0 210 // All documents are removed except foo1 & foo3.
michael@0 211 syncWithCacheIOThread(function () {
michael@0 212 // foo1
michael@0 213 let file = do_get_profile().clone();
michael@0 214 file.append("OfflineCache");
michael@0 215 file.append("5");
michael@0 216 file.append("9");
michael@0 217 file.append("8379C6596B8CA4-0");
michael@0 218 do_check_eq(file.exists(), true);
michael@0 219
michael@0 220 file = do_get_profile().clone();
michael@0 221 file.append("OfflineCache");
michael@0 222 file.append("0");
michael@0 223 file.append("0");
michael@0 224 file.append("61FEE819921D39-0");
michael@0 225 do_check_eq(file.exists(), false);
michael@0 226
michael@0 227 file = do_get_profile().clone();
michael@0 228 file.append("OfflineCache");
michael@0 229 file.append("3");
michael@0 230 file.append("9");
michael@0 231 file.append("0D8759F1DE5452-0");
michael@0 232 do_check_eq(file.exists(), false);
michael@0 233
michael@0 234 file = do_get_profile().clone();
michael@0 235 file.append("OfflineCache");
michael@0 236 file.append("C");
michael@0 237 file.append("2");
michael@0 238 file.append("5F356A168B5E3B-0");
michael@0 239 do_check_eq(file.exists(), false);
michael@0 240
michael@0 241 // foo3
michael@0 242 file = do_get_profile().clone();
michael@0 243 file.append("OfflineCache");
michael@0 244 file.append("D");
michael@0 245 file.append("C");
michael@0 246 file.append("1ADCCC843B5C00-0");
michael@0 247 do_check_eq(file.exists(), true);
michael@0 248
michael@0 249 file = do_get_profile().clone();
michael@0 250 file.append("OfflineCache");
michael@0 251 file.append("F");
michael@0 252 file.append("0");
michael@0 253 file.append("FC3E6D6C1164E9-0");
michael@0 254 do_check_eq(file.exists(), false);
michael@0 255
michael@0 256 httpServer.stop(do_test_finished);
michael@0 257 });
michael@0 258 },
michael@0 259 appcache
michael@0 260 );
michael@0 261 }
michael@0 262
michael@0 263 function run_test() {
michael@0 264 if (newCacheBackEndUsed()) {
michael@0 265 // times out on storage.asyncDoomURI @ check_bug because that method is not implemented for appcache
michael@0 266 // either revert the test changes or implement the method (former seems more reasonable)
michael@0 267 do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed");
michael@0 268 return;
michael@0 269 }
michael@0 270
michael@0 271 if (typeof _XPCSHELL_PROCESS == "undefined" ||
michael@0 272 _XPCSHELL_PROCESS != "child") {
michael@0 273 init_profile();
michael@0 274 clean_app_cache();
michael@0 275 }
michael@0 276
michael@0 277 init_http_server();
michael@0 278 start_cache_nonpinned_app();
michael@0 279 do_test_pending();
michael@0 280 }

mercurial