Bug 1398231 - P2: Add a test to verify cache work when there is a temporary padding file. r=bkelly
authorTom Tung <shes050117@gmail.com>
Sat, 09 Sep 2017 15:34:59 +0800
changeset 429666 89abc58564c87354f3d135c00afccab5aaa0d510
parent 429665 e999670eb2951a8b84b221eb7ce20544a5f54b88
child 429667 9584975d84e0f443d83828be340b92391bf68377
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1398231
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1398231 - P2: Add a test to verify cache work when there is a temporary padding file. r=bkelly MozReview-Commit-ID: JhPuL7AmteM
dom/cache/test/xpcshell/head.js
dom/cache/test/xpcshell/test_padding_error_handle.js
dom/cache/test/xpcshell/xpcshell.ini
--- a/dom/cache/test/xpcshell/head.js
+++ b/dom/cache/test/xpcshell/head.js
@@ -14,17 +14,17 @@ var Cu = Components.utils;
 var ss = Cc['@mozilla.org/storage/service;1']
          .createInstance(Ci.mozIStorageService);
 var sts = Cc['@mozilla.org/network/stream-transport-service;1']
           .getService(Ci.nsIStreamTransportService);
 var hash = Cc['@mozilla.org/security/hash;1']
            .createInstance(Ci.nsICryptoHash);
 
 // Expose Cache and Fetch symbols on the global
-Cu.importGlobalProperties(['caches', 'fetch']);
+Cu.importGlobalProperties(['caches', 'File', 'fetch']);
 
 // Extract a zip file into the profile
 function create_test_profile(zipFileName) {
   do_get_profile();
 
   var directoryService = Cc['@mozilla.org/file/directory_service;1']
                          .getService(Ci.nsIProperties);
   var profileDir = directoryService.get('ProfD', Ci.nsIFile);
@@ -70,8 +70,24 @@ function create_test_profile(zipFileName
 
       istream.close();
       bostream.close();
     }
   }
 
   zipReader.close();
 }
+
+function getCacheDir()
+{
+  let dirService = Cc["@mozilla.org/file/directory_service;1"]
+                   .getService(Ci.nsIProperties);
+
+  let profileDir = dirService.get("ProfD", Ci.nsIFile);
+  let cacheDir = profileDir.clone();
+  cacheDir.append("storage");
+  cacheDir.append("default");
+  cacheDir.append("chrome");
+  cacheDir.append("cache");
+
+  return cacheDir;
+}
+
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/xpcshell/test_padding_error_handle.js
@@ -0,0 +1,65 @@
+/**
+ *  This test is mainly to verify cache actions work as usual even there exists
+ *  an unexpected padding file.
+ */
+
+function getTempPaddingFilePath() {
+  let cacheDir = getCacheDir();
+  let temporaryPaddingFile = cacheDir.clone();
+  temporaryPaddingFile.append(".padding-tmp");
+  return temporaryPaddingFile;
+}
+
+function createTempPaddingFile () {
+  let temporaryPaddingFile = getTempPaddingFilePath();
+  temporaryPaddingFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0644", 8));
+
+  ok(temporaryPaddingFile.exists(),
+     "Temporary padding file does be created by test");
+}
+
+async function run_test() {
+  do_test_pending();
+  create_test_profile('schema_25_profile.zip');
+  let cache = await caches.open("test");
+
+  // Step 1: Verify cache.match won't fail when there is a temporary padding
+  // file
+  createTempPaddingFile();
+
+  let response = await cache.match("https://www.mozilla.org");
+  ok(!!response, "Upgrade from 25 to 26 do succeed");
+
+  // Note: Only cache write actions(e.g. cache.put/add/addAll/delete) will
+  // remove unexpected temporary padding file when writting an opaque response
+  // into the file-system. Cache read actions(e.g. cache.keys/match) won't.
+  let temporaryPaddingFile = getTempPaddingFilePath();
+  ok(temporaryPaddingFile.exists(),
+     "Temporary padding file doesn't be removed by cache.match");
+
+  // Step 2: Verify cache.put won't fail when there is a temporary padding
+  // file
+  await cache.put("https://foo.com", response);
+  ok(!temporaryPaddingFile.exists(),
+     "Temporary padding file does be removed by cache.put");
+
+  // Step 3: Verify cache.keys won't fail when there is a temporary padding
+  // file
+  createTempPaddingFile();
+
+  let cacheEntries = await cache.keys("https://foo.com");
+  ok(cacheEntries.length === 1, "Cache.put does succeed");
+
+  ok(temporaryPaddingFile.exists(),
+     "Temporary padding file doesn't be removed by cache.keys");
+
+  // Step 4: Verify cache.delete won't fail when there is a temporary padding
+  // file
+  await cache.delete("https://foo.com");
+  ok(!temporaryPaddingFile.exists(),
+     "Temporary padding file does be removed by cache.delete");
+
+  await caches.delete("test");
+
+  do_test_finished();
+}
--- a/dom/cache/test/xpcshell/xpcshell.ini
+++ b/dom/cache/test/xpcshell/xpcshell.ini
@@ -8,9 +8,10 @@ support-files =
   schema_15_profile.zip
   schema_25_profile.zip
 
 # dummy test entry to generate profile zip files
 [make_profile.js]
   skip-if = true
 
 [test_migration.js]
+[test_padding_error_handle.js]
 [test_schema_26_upgrade.js]