From c30332cd262864e220938bc5023cd186f5cf54a2 Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Thu, 27 Sep 2018 23:25:43 +0200 Subject: [PATCH] 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 | 16 +++ .../test/unit/test_persist_groupLimit.js | 105 ++++++++++++++++++ dom/quota/test/unit/xpcshell.ini | 1 + 3 files changed, 122 insertions(+) create mode 100644 dom/quota/test/unit/test_persist_groupLimit.js diff --git a/dom/quota/test/unit/head.js b/dom/quota/test/unit/head.js index ba5694084238..300f4115a78d 100644 --- a/dom/quota/test/unit/head.js +++ b/dom/quota/test/unit/head.js @@ -7,6 +7,7 @@ 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) { @@ -96,6 +97,17 @@ function resetTesting() 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(); @@ -335,6 +347,10 @@ var SpecialPowers = { this._getPrefs().setBoolPref(prefName, value); }, + setIntPref: function(prefName, value) { + this._getPrefs().setIntPref(prefName, value); + }, + clearUserPref: function(prefName) { this._getPrefs().clearUserPref(prefName); }, diff --git a/dom/quota/test/unit/test_persist_groupLimit.js b/dom/quota/test/unit/test_persist_groupLimit.js new file mode 100644 index 000000000000..895e8bf9cb6c --- /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(); +} diff --git a/dom/quota/test/unit/xpcshell.ini b/dom/quota/test/unit/xpcshell.ini index 24e370aa2e42..ae1f1a525df3 100644 --- a/dom/quota/test/unit/xpcshell.ini +++ b/dom/quota/test/unit/xpcshell.ini @@ -29,6 +29,7 @@ support-files = [test_obsoleteOriginAttributesUpgrade.js] [test_originAttributesUpgrade.js] [test_persist.js] +[test_persist_groupLimit.js] [test_removeAppsUpgrade.js] [test_removeLocalStorage.js] [test_simpledb.js]