summaryrefslogtreecommitdiffstats
path: root/toolkit/components/kvstore/test
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/components/kvstore/test')
-rw-r--r--toolkit/components/kvstore/test/xpcshell/test_kvstore.js74
1 files changed, 73 insertions, 1 deletions
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");