From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- .../kvstore/test/xpcshell/test_kvstore.js | 74 +++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) (limited to 'toolkit/components/kvstore/test') diff --git a/toolkit/components/kvstore/test/xpcshell/test_kvstore.js b/toolkit/components/kvstore/test/xpcshell/test_kvstore.js index 363feaa43a..4c591622e1 100644 --- a/toolkit/components/kvstore/test/xpcshell/test_kvstore.js +++ b/toolkit/components/kvstore/test/xpcshell/test_kvstore.js @@ -12,9 +12,16 @@ function run_test() { run_next_test(); } -async function makeDatabaseDir(name) { +async function makeDatabaseDir(name, { mockCorrupted = false } = {}) { const databaseDir = PathUtils.join(PathUtils.profileDir, name); await IOUtils.makeDirectory(databaseDir); + if (mockCorrupted) { + // Mock a corrupted db. + await IOUtils.write( + PathUtils.join(databaseDir, "data.safe.bin"), + new Uint8Array([0x00, 0x00, 0x00, 0x00]) + ); + } return databaseDir; } @@ -26,6 +33,71 @@ add_task(async function getService() { Assert.ok(gKeyValueService); }); +add_task(async function getOrCreate_defaultRecoveryStrategyError() { + const databaseDir = await makeDatabaseDir("getOrCreate_Error", { + mockCorrupted: true, + }); + + await Assert.rejects( + KeyValueService.getOrCreate(databaseDir, "db"), + /FileInvalid/ + ); +}); + +add_task(async function getOrCreateWithOptions_RecoveryStrategyError() { + const databaseDir = await makeDatabaseDir("getOrCreateWithOptions_Error", { + mockCorrupted: true, + }); + + await Assert.rejects( + KeyValueService.getOrCreateWithOptions(databaseDir, "db", { + strategy: KeyValueService.RecoveryStrategy.ERROR, + }), + /FileInvalid/ + ); +}); + +add_task(async function getOrCreateWithOptions_RecoveryStrategyRename() { + const databaseDir = await makeDatabaseDir("getOrCreateWithOptions_Rename", { + mockCorrupted: true, + }); + + const database = await KeyValueService.getOrCreateWithOptions( + databaseDir, + "db", + { + strategy: KeyValueService.RecoveryStrategy.RENAME, + } + ); + Assert.ok(database); + + Assert.ok( + await IOUtils.exists(PathUtils.join(databaseDir, "data.safe.bin.corrupt")), + "Expect corrupt file to be found" + ); +}); + +add_task(async function getOrCreateWithOptions_RecoveryStrategyDiscard() { + const databaseDir = await makeDatabaseDir("getOrCreateWithOptions_Discard", { + mockCorrupted: true, + }); + + const database = await KeyValueService.getOrCreateWithOptions( + databaseDir, + "db", + { + strategy: KeyValueService.RecoveryStrategy.DISCARD, + } + ); + Assert.ok(database); + + Assert.equal( + await IOUtils.exists(PathUtils.join(databaseDir, "data.safe.bin.corrupt")), + false, + "Expect corrupt file to not exist" + ); +}); + add_task(async function getOrCreate() { const databaseDir = await makeDatabaseDir("getOrCreate"); const database = await KeyValueService.getOrCreate(databaseDir, "db"); -- cgit v1.2.3