Bug 1648399 - Add test cases to ensure that a cache/ls directory in permanent origin directory shouldn't cause storage init to fail; r=dom-workers-and-storage-reviewers,sg,janv
authorTom Tung <ttung@mozilla.com>
Fri, 16 Apr 2021 01:09:10 +0000
changeset 576317 c1f839b8a6c4d380328a2315e33681da2d97a069
parent 576316 1f2481edad7a97140ea9b1295d5635de9d39698e
child 576318 d05d2e1fc2a1e6f9decdce457d906e53b89d399e
push id38379
push usersmolnar@mozilla.com
push dateFri, 16 Apr 2021 09:36:42 +0000
treeherdermozilla-central@a00cdb29df1f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, sg, janv
bugs1648399
milestone89.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 1648399 - Add test cases to ensure that a cache/ls directory in permanent origin directory shouldn't cause storage init to fail; r=dom-workers-and-storage-reviewers,sg,janv Depends on D80824 Differential Revision: https://phabricator.services.mozilla.com/D81680
dom/cache/test/xpcshell/head.js
dom/cache/test/xpcshell/test_originInit.js
dom/localstorage/test/unit/head.js
dom/localstorage/test/unit/test_originInit.js
--- a/dom/cache/test/xpcshell/head.js
+++ b/dom/cache/test/xpcshell/head.js
@@ -10,17 +10,20 @@
 /* global testSteps */
 
 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 const NS_APP_USER_PROFILE_50_DIR = "ProfD";
 const osWindowsName = "WINNT";
 const pathDelimiter = "/";
 
+const persistentPersistence = "persistent";
+
 const storageDirName = "storage";
+const persistentPersistenceDirName = "permanent";
 const defaultPersistenceDirName = "default";
 
 function cacheClientDirName() {
   return "cache";
 }
 
 // services required be initialized in order to run CacheStorage
 var ss = Cc["@mozilla.org/storage/service;1"].createInstance(
@@ -81,16 +84,20 @@ function resetTesting() {
 function initStorage() {
   return Services.qms.init();
 }
 
 function initTemporaryStorage() {
   return Services.qms.initTemporaryStorage();
 }
 
+function initPersistentOrigin(principal) {
+  return Services.qms.initializePersistentOrigin(principal);
+}
+
 function initTemporaryOrigin(principal) {
   return Services.qms.initializeTemporaryOrigin("default", principal);
 }
 
 function clearOrigin(principal) {
   let request = Services.qms.clearStoragesForPrincipal(principal, "default");
 
   return request;
--- a/dom/cache/test/xpcshell/test_originInit.js
+++ b/dom/cache/test/xpcshell/test_originInit.js
@@ -26,16 +26,21 @@ async function testSteps() {
     `${basePath}/${originDirName}/${cacheClientDirName()}/.padding-tmp`
   );
   // ToDo: Replace morgue with a getter function once we expose the
   // filename of it to a IDL file.
   const morgueDir = getRelativeFile(
     `${basePath}/${originDirName}/${cacheClientDirName()}/morgue`
   );
 
+  const persistentCacheDir = getRelativeFile(
+    `${storageDirName}/${persistentPersistenceDirName}/${originDirName}/` +
+      `${cacheClientDirName()}`
+  );
+
   async function createNormalCacheOrigin() {
     async function sandboxScript() {
       const cache = await caches.open("myCache");
       const request = new Request("https://example.com/index.html");
       const response = new Response("hello world");
       await cache.put(request, response);
     }
 
@@ -65,16 +70,20 @@ async function testSteps() {
   function removeDir(dir) {
     dir.remove(true);
   }
 
   function createEmptyFile(file) {
     file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
   }
 
+  function createEmptyDirectory(dir) {
+    dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o0755);
+  }
+
   function checkFiles(
     expectCachesDatabase,
     expectPaddingFile,
     expectTempPaddingFile,
     expectMorgueDir
   ) {
     let exists = cachesDatabase.exists();
     if (expectCachesDatabase) {
@@ -158,24 +167,63 @@ async function testSteps() {
       false,
       createCachesDatabase && createMorgueDir
     );
 
     request = clearOrigin(principal);
     await requestFinished(request);
   }
 
+  async function initPersistentTestOrigin() {
+    let request = initStorage();
+    await requestFinished(request);
+
+    request = initTemporaryStorage();
+    await requestFinished(request);
+
+    request = initPersistentOrigin(principal);
+    await requestFinished(request);
+  }
+
+  async function clearPersistentTestOrigin() {
+    let request = Services.qms.clearStoragesForPrincipal(
+      principal,
+      persistentPersistence
+    );
+    await requestFinished(request);
+  }
+
   // Test all possible combinations.
   for (let createCachesDatabase of [false, true]) {
     for (let createPaddingFile of [false, true]) {
       for (let createTempPaddingFile of [false, true]) {
         for (let createMorgueDir of [false, true]) {
           await testOriginInit(
             createCachesDatabase,
             createPaddingFile,
             createTempPaddingFile,
             createMorgueDir
           );
         }
       }
     }
   }
+
+  async function testPermanentCacheDir() {
+    info(
+      `Testing InitializeOrigin doesn't fail when there is a cache ` +
+        `directory in the ${persistentPersistenceDirName} repository`
+    );
+
+    createEmptyDirectory(persistentCacheDir);
+
+    await initPersistentTestOrigin();
+
+    ok(
+      true,
+      "A persistent cache directory didn't cause InitializeOrigin to fail"
+    );
+
+    await clearPersistentTestOrigin();
+  }
+
+  await testPermanentCacheDir();
 }
--- a/dom/localstorage/test/unit/head.js
+++ b/dom/localstorage/test/unit/head.js
@@ -110,16 +110,20 @@ function setTimeout(callback, timeout) {
 function initStorage() {
   return Services.qms.init();
 }
 
 function initTemporaryStorage() {
   return Services.qms.initTemporaryStorage();
 }
 
+function initPersistentOrigin(principal) {
+  return Services.qms.initializePersistentOrigin(principal);
+}
+
 function initTemporaryOrigin(persistence, principal) {
   return Services.qms.initializeTemporaryOrigin(persistence, principal);
 }
 
 function getOriginUsage(principal, fromMemory = false) {
   let request = Services.qms.getUsageForPrincipal(
     principal,
     function() {},
--- a/dom/localstorage/test/unit/test_originInit.js
+++ b/dom/localstorage/test/unit/test_originInit.js
@@ -1,26 +1,42 @@
 /**
  * Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 async function testSteps() {
+  const storageDirName = "storage";
+  const persistenceTypeDefaultDirName = "default";
+  const persistenceTypePersistentDirName = "permanent";
+
   const principal = getPrincipal("http://example.com");
 
+  const originDirName = "http+++example.com";
+
+  const clientLSDirName = "ls";
+
   const dataFile = getRelativeFile(
-    "storage/default/http+++example.com/ls/data.sqlite"
+    `${storageDirName}/${persistenceTypeDefaultDirName}/${originDirName}/` +
+      `${clientLSDirName}/data.sqlite`
   );
 
   const usageJournalFile = getRelativeFile(
-    "storage/default/http+++example.com/ls/usage-journal"
+    `${storageDirName}/${persistenceTypeDefaultDirName}/${originDirName}/` +
+      `${clientLSDirName}/usage-journal`
   );
 
   const usageFile = getRelativeFile(
-    "storage/default/http+++example.com/ls/usage"
+    `${storageDirName}/${persistenceTypeDefaultDirName}/${originDirName}/` +
+      `${clientLSDirName}/usage`
+  );
+
+  const persistentLSDir = getRelativeFile(
+    `${storageDirName}/${persistenceTypePersistentDirName}/${originDirName}/` +
+      `${clientLSDirName}`
   );
 
   const data = {};
   data.key = "key1";
   data.value = "value1";
   data.usage = data.key.length + data.value.length;
 
   const usageFileCookie = 0x420a420a;
@@ -34,17 +50,21 @@ async function testSteps() {
     await requestFinished(request);
   }
 
   function removeFile(file) {
     file.remove(false);
   }
 
   function createEmptyFile(file) {
-    file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0644", 8));
+    file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o0644);
+  }
+
+  function createEmptyDirectory(dir) {
+    dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o0755);
   }
 
   function getBinaryOutputStream(file) {
     var ostream = Cc[
       "@mozilla.org/network/file-output-stream;1"
     ].createInstance(Ci.nsIFileOutputStream);
     ostream.init(file, -1, parseInt("0644", 8), 0);
 
@@ -90,16 +110,35 @@ async function testSteps() {
     ok(usage == data.usage, "Correct usage");
   }
 
   async function clearTestOrigin() {
     let request = clearOrigin(principal, "default");
     await requestFinished(request);
   }
 
+  async function initPersistentTestOrigin() {
+    let request = initStorage();
+    await requestFinished(request);
+
+    request = initTemporaryStorage();
+    await requestFinished(request);
+
+    request = initPersistentOrigin(principal);
+    await requestFinished(request);
+  }
+
+  async function clearPersistentTestOrigin() {
+    let request = Services.qms.clearStoragesForPrincipal(
+      principal,
+      "persistent"
+    );
+    await requestFinished(request);
+  }
+
   info("Setting prefs");
 
   Services.prefs.setBoolPref("dom.storage.next_gen", true);
 
   info(
     "Stage 1 - " +
       "data.sqlite file doesn't exist, " +
       "usage-journal file doesn't exist, " +
@@ -294,9 +333,19 @@ async function testSteps() {
 
   createEmptyFile(usageJournalFile);
 
   await initTestOrigin();
 
   await checkFiles(/* wantData */ true, /* wantUsage */ true);
 
   await clearTestOrigin();
+
+  info("Stage 12 - any ls directory in permanent repository exists");
+
+  await createEmptyDirectory(persistentLSDir);
+
+  await initPersistentTestOrigin();
+
+  await clearPersistentTestOrigin();
+
+  ok(true, "A persistent ls directory didn't cause InitializeOrigin to fail");
 }