1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

Add to/fromJSON methods to State and Accounts

This is needed since all storage in manifest v3 is key-value-pair-based
and session storage of most data is actually serialized into an
encrypted string.
This commit is contained in:
Matt Gibson
2022-08-31 17:05:07 -04:00
parent 719a6d095e
commit 77bbd3a863
12 changed files with 366 additions and 31 deletions

View File

@@ -0,0 +1,44 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { EncryptionPair } from "./account";
describe("EncryptionPair", () => {
describe("toJSON", () => {
it("should populate decryptedSerialized for buffer arrays", () => {
const pair = new EncryptionPair<string, ArrayBuffer>();
pair.decrypted = Utils.fromByteStringToArray("hello").buffer;
const json = pair.toJSON();
expect(json.decryptedSerialized).toEqual("hello");
});
it("should serialize encrypted and decrypted", () => {
const pair = new EncryptionPair<string, string>();
pair.encrypted = "hello";
pair.decrypted = "world";
const json = pair.toJSON();
expect(json.encrypted).toEqual("hello");
expect(json.decrypted).toEqual("world");
});
});
describe("fromJSON", () => {
it("should deserialize encrypted and decrypted", () => {
const pair = EncryptionPair.fromJSON({
encrypted: "hello",
decrypted: "world",
decryptedSerialized: null,
});
expect(pair.encrypted).toEqual("hello");
expect(pair.decrypted).toEqual("world");
});
it("should deserialize decryptedSerialized for buffer arrays", () => {
const pair = EncryptionPair.fromJSON<string, ArrayBuffer>({
encrypted: "encrypted",
decrypted: null,
decryptedSerialized: "hello",
});
expect(pair.decrypted).toEqual(Utils.fromByteStringToArray("hello").buffer);
});
});
});