Bug 1493908 - Part 2: Add a test to verify that persisted origins are not constrained by the group limit; r=asuth
authorJan Varga <jan.varga@gmail.com>
Thu, 27 Sep 2018 23:25:43 +0200
changeset 486867 3b37ec34b104624fbd8c79b7756c30e6044c04b7
parent 486866 415b25aeccca73c30c0689bda05d46ad414ebf40
child 486924 3c8df5d50089a3e4d03a31d57e6a7fdbcfbd871f
push id246
push userfmarier@mozilla.com
push dateSat, 13 Oct 2018 00:15:40 +0000
reviewersasuth
bugs1493908
milestone64.0a1
Bug 1493908 - Part 2: Add a test to verify that persisted origins are not constrained by the group limit; r=asuth
dom/quota/test/unit/head.js
dom/quota/test/unit/test_persist_groupLimit.js
dom/quota/test/unit/xpcshell.ini
--- a/dom/quota/test/unit/head.js
+++ b/dom/quota/test/unit/head.js
@@ -2,16 +2,17 @@
  * Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 const NS_OK = Cr.NS_OK;
 const NS_ERROR_FAILURE = Cr.NS_ERROR_FAILURE;
 const NS_ERROR_UNEXPECTED = Cr.NS_ERROR_UNEXPECTED;
 const NS_ERROR_STORAGE_BUSY = Cr.NS_ERROR_STORAGE_BUSY;
+const NS_ERROR_FILE_NO_DEVICE_SPACE = Cr.NS_ERROR_FILE_NO_DEVICE_SPACE;
 
 function is(a, b, msg)
 {
   Assert.equal(a, b, msg);
 }
 
 function ok(cond, msg)
 {
@@ -91,16 +92,27 @@ function enableTesting()
 }
 
 function resetTesting()
 {
   SpecialPowers.clearUserPref("dom.quotaManager.testing");
   SpecialPowers.clearUserPref("dom.simpleDB.enabled");
 }
 
+function setGlobalLimit(globalLimit)
+{
+  SpecialPowers.setIntPref("dom.quotaManager.temporaryStorage.fixedLimit",
+                           globalLimit);
+}
+
+function resetGlobalLimit()
+{
+  SpecialPowers.clearUserPref("dom.quotaManager.temporaryStorage.fixedLimit");
+}
+
 function init(callback)
 {
   let request = SpecialPowers._getQuotaManager().init();
   request.callback = callback;
 
   return request;
 }
 
@@ -330,16 +342,20 @@ var SpecialPowers = {
   getBoolPref: function(prefName) {
     return this._getPrefs().getBoolPref(prefName);
   },
 
   setBoolPref: function(prefName, value) {
     this._getPrefs().setBoolPref(prefName, value);
   },
 
+  setIntPref: function(prefName, value) {
+    this._getPrefs().setIntPref(prefName, value);
+  },
+
   clearUserPref: function(prefName) {
     this._getPrefs().clearUserPref(prefName);
   },
 
   _getPrefs: function() {
     let prefService =
       Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
     return prefService.getBranch(null);
new file mode 100644
--- /dev/null
+++ b/dom/quota/test/unit/test_persist_groupLimit.js
@@ -0,0 +1,105 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+/**
+ * This test is mainly to verify that persisted origins are not constrained by
+ * the group limit.  It consits of these steps:
+ * - Set the limits as small as our limits allow.  This does result in needing
+ *   to perform 10 megs of writes which is a lot for a test but not horrible.
+ * - Create databases for 2 origins under the same group.
+ * - Have the foo2 origin use up the shared group quota.
+ * - Verify neither origin can write additional data (via a single byte write).
+ * - Do navigator.storage.persist() for that foo2 origin.
+ * - Verify that both origins can now write an additional byte.  This
+ *   demonstrates that:
+ *   - foo2 no longer counts against the group limit at all since foo1 can
+ *     write a byte.
+ *   - foo2 is no longer constrained by the group limit itself.
+ */
+async function testSteps()
+{
+  // The group limit is calculated as 20% of the global limit and the minimum
+  // value of the group limit is 10 MB.
+
+  const groupLimitKB = 10 * 1024;
+  const globalLimitKB = groupLimitKB * 5;
+
+  const urls = [
+    "http://foo1.example.com",
+    "http://foo2.example.com",
+  ];
+
+  const foo2Index = 1;
+
+  let index;
+
+  info("Setting limits");
+
+  setGlobalLimit(globalLimitKB);
+
+  let request = clear();
+  await requestFinished(request);
+
+  info("Opening databases");
+
+  let databases = [];
+  for (index = 0; index < urls.length; index++) {
+    let database = getSimpleDatabase(getPrincipal(urls[index]));
+
+    request = database.open("data");
+    await requestFinished(request);
+
+    databases.push(database);
+  }
+
+  info("Filling up the whole group");
+
+  try {
+    request = databases[foo2Index].write(new ArrayBuffer(groupLimitKB * 1024));
+    await requestFinished(request);
+    ok(true, "Should not have thrown");
+  } catch(ex) {
+    ok(false, "Should not have thrown");
+  }
+
+  info("Verifying no more data can be written");
+
+  for (index = 0; index < urls.length; index++) {
+    try {
+      request = databases[index].write(new ArrayBuffer(1));
+      await requestFinished(request);
+      ok(false, "Should have thrown");
+    } catch(ex) {
+      ok(true, "Should have thrown");
+      ok(ex == NS_ERROR_FILE_NO_DEVICE_SPACE, "Threw right code");
+    }
+  }
+
+  info("Persisting origin");
+
+  request = persist(getPrincipal(urls[foo2Index]));
+  await requestFinished(request);
+
+  info("Verifying more data data can be written");
+
+  for (index = 0; index < urls.length; index++) {
+    try {
+      request = databases[index].write(new ArrayBuffer(1));
+      await requestFinished(request);
+      ok(true, "Should not have thrown");
+    } catch(ex) {
+      ok(false, "Should not have thrown");
+    }
+  }
+
+  info("Closing databases");
+
+  for (index = 0; index < urls.length; index++) {
+    request = databases[index].close();
+    await requestFinished(request);
+  }
+
+  finishTest();
+}
--- a/dom/quota/test/unit/xpcshell.ini
+++ b/dom/quota/test/unit/xpcshell.ini
@@ -24,15 +24,16 @@ support-files =
 [test_bad_origin_directory.js]
 [test_defaultStorageUpgrade.js]
 [test_getUsage.js]
 [test_idbSubdirUpgrade.js]
 [test_morgueCleanup.js]
 [test_obsoleteOriginAttributesUpgrade.js]
 [test_originAttributesUpgrade.js]
 [test_persist.js]
+[test_persist_groupLimit.js]
 [test_removeAppsUpgrade.js]
 [test_removeLocalStorage.js]
 [test_simpledb.js]
 [test_storagePersistentUpgrade.js]
 [test_tempMetadataCleanup.js]
 [test_unknownFiles.js]
 [test_version2_1upgrade.js]