diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /dom/localstorage/test/unit/test_usageAfterMigration.js | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/localstorage/test/unit/test_usageAfterMigration.js')
-rw-r--r-- | dom/localstorage/test/unit/test_usageAfterMigration.js | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/dom/localstorage/test/unit/test_usageAfterMigration.js b/dom/localstorage/test/unit/test_usageAfterMigration.js new file mode 100644 index 0000000000..a0bd5efd5b --- /dev/null +++ b/dom/localstorage/test/unit/test_usageAfterMigration.js @@ -0,0 +1,164 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +add_task(async function testSteps() { + const principal = getPrincipal("http://example.com"); + + const dataFile = getRelativeFile( + "storage/default/http+++example.com/ls/data.sqlite" + ); + + const usageJournalFile = getRelativeFile( + "storage/default/http+++example.com/ls/usage-journal" + ); + + const usageFile = getRelativeFile( + "storage/default/http+++example.com/ls/usage" + ); + + const data = {}; + data.key = "foo"; + data.value = "bar"; + data.usage = data.key.length + data.value.length; + + async function createStorageForMigration(createUsageDir) { + info("Clearing"); + + let request = clear(); + await requestFinished(request); + + info("Installing package"); + + // The profile contains storage.sqlite and webappsstore.sqlite. The file + // create_db.js in the package was run locally, specifically it was + // temporarily added to xpcshell.ini and then executed: + // mach xpcshell-test --interactive dom/localstorage/test/unit/create_db.js + installPackage("usageAfterMigration_profile"); + + if (createUsageDir) { + // Origin must be initialized before the usage dir is created. + + info("Initializing storage"); + + request = initStorage(); + await requestFinished(request); + + info("Initializing temporary storage"); + + request = initTemporaryStorage(); + await requestFinished(request); + + info("Initializing origin"); + + request = initTemporaryOrigin("default", principal); + await requestFinished(request); + + info("Creating usage as a directory"); + + // This will cause a failure during migration. + usageFile.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8)); + } + } + + function verifyData() { + ok(dataFile.exists(), "Data file does exist"); + } + + async function verifyUsage(success) { + info("Verifying usage in memory"); + + let request = getOriginUsage(principal, /* fromMemory */ true); + await requestFinished(request); + + if (success) { + is(request.result.usage, data.usage, "Correct usage"); + } else { + is(request.result.usage, 0, "Zero usage"); + } + + info("Verifying usage on disk"); + + if (success) { + ok(!usageJournalFile.exists(), "Usage journal file doesn't exist"); + ok(usageFile.exists(), "Usage file does exist"); + let usage = await readUsageFromUsageFile(usageFile); + is(usage, data.usage, "Correct usage"); + } else { + ok(usageJournalFile.exists(), "Usage journal file does exist"); + ok(usageFile.exists(), "Usage file does exist"); + } + } + + info("Setting prefs"); + + Services.prefs.setBoolPref( + "dom.storage.enable_unsupported_legacy_implementation", + false + ); + + info("Stage 1 - Testing usage after successful data migration"); + + await createStorageForMigration(/* createUsageDir */ false); + + info("Getting storage"); + + let storage = getLocalStorage(principal); + + info("Opening"); + + storage.open(); + + verifyData(); + + await verifyUsage(/* success */ true); + + info("Stage 2 - Testing usage after unsuccessful data migration"); + + await createStorageForMigration(/* createUsageDir */ true); + + info("Getting storage"); + + storage = getLocalStorage(principal); + + info("Opening"); + + try { + storage.open(); + ok(false, "Should have thrown"); + } catch (ex) { + ok(true, "Did throw"); + } + + verifyData(); + + await verifyUsage(/* success */ false); + + info("Stage 3 - Testing usage after unsuccessful/successful data migration"); + + await createStorageForMigration(/* createUsageDir */ true); + + info("Getting storage"); + + storage = getLocalStorage(principal); + + info("Opening"); + + try { + storage.open(); + ok(false, "Should have thrown"); + } catch (ex) { + ok(true, "Did throw"); + } + + usageFile.remove(true); + + info("Opening"); + + storage.open(); + + verifyData(); + + await verifyUsage(/* success */ true); +}); |