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:
44
libs/common/src/models/domain/encryption-pair.spec.ts
Normal file
44
libs/common/src/models/domain/encryption-pair.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user