mirror of
https://github.com/bitwarden/browser.git
synced 2025-03-02 03:41:09 +01:00
Validate parameters in encrypt service tests (#13630)
This commit is contained in:
parent
c9e20687ef
commit
7ba8dd98e6
@ -37,16 +37,45 @@ describe("EncryptService", () => {
|
|||||||
const actual = await encryptService.encrypt(null, key);
|
const actual = await encryptService.encrypt(null, key);
|
||||||
expect(actual).toBeNull();
|
expect(actual).toBeNull();
|
||||||
});
|
});
|
||||||
|
it("creates an EncString for Aes256Cbc", async () => {
|
||||||
|
const key = new SymmetricCryptoKey(makeStaticByteArray(32));
|
||||||
|
const plainValue = "data";
|
||||||
|
cryptoFunctionService.aesEncrypt.mockResolvedValue(makeStaticByteArray(4, 100));
|
||||||
|
cryptoFunctionService.randomBytes.mockResolvedValue(makeStaticByteArray(16) as CsprngArray);
|
||||||
|
const result = await encryptService.encrypt(plainValue, key);
|
||||||
|
expect(cryptoFunctionService.aesEncrypt).toHaveBeenCalledWith(
|
||||||
|
Utils.fromByteStringToArray(plainValue),
|
||||||
|
makeStaticByteArray(16),
|
||||||
|
makeStaticByteArray(32),
|
||||||
|
);
|
||||||
|
expect(cryptoFunctionService.hmac).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
expect(Utils.fromB64ToArray(result.data).length).toEqual(4);
|
||||||
|
expect(Utils.fromB64ToArray(result.iv).length).toEqual(16);
|
||||||
|
});
|
||||||
it("creates an EncString for Aes256Cbc_HmacSha256_B64", async () => {
|
it("creates an EncString for Aes256Cbc_HmacSha256_B64", async () => {
|
||||||
const key = new SymmetricCryptoKey(makeStaticByteArray(64));
|
const key = new SymmetricCryptoKey(makeStaticByteArray(64));
|
||||||
const plainValue = "data";
|
const plainValue = "data";
|
||||||
cryptoFunctionService.hmac.mockResolvedValue(makeStaticByteArray(32));
|
cryptoFunctionService.hmac.mockResolvedValue(makeStaticByteArray(32));
|
||||||
cryptoFunctionService.aesEncrypt.mockResolvedValue(makeStaticByteArray(32));
|
cryptoFunctionService.aesEncrypt.mockResolvedValue(makeStaticByteArray(4, 100));
|
||||||
cryptoFunctionService.randomBytes.mockResolvedValue(makeStaticByteArray(16) as CsprngArray);
|
cryptoFunctionService.randomBytes.mockResolvedValue(makeStaticByteArray(16) as CsprngArray);
|
||||||
const result = await encryptService.encrypt(plainValue, key);
|
const result = await encryptService.encrypt(plainValue, key);
|
||||||
expect(cryptoFunctionService.aesEncrypt).toHaveBeenCalled();
|
expect(cryptoFunctionService.aesEncrypt).toHaveBeenCalledWith(
|
||||||
expect(cryptoFunctionService.hmac).toHaveBeenCalled();
|
Utils.fromByteStringToArray(plainValue),
|
||||||
expect(Utils.fromB64ToArray(result.data).length).toEqual(32);
|
makeStaticByteArray(16),
|
||||||
|
makeStaticByteArray(32),
|
||||||
|
);
|
||||||
|
|
||||||
|
const macData = new Uint8Array(16 + 4);
|
||||||
|
macData.set(makeStaticByteArray(16));
|
||||||
|
macData.set(makeStaticByteArray(4, 100), 16);
|
||||||
|
expect(cryptoFunctionService.hmac).toHaveBeenCalledWith(
|
||||||
|
macData,
|
||||||
|
makeStaticByteArray(32, 32),
|
||||||
|
"sha256",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(Utils.fromB64ToArray(result.data).length).toEqual(4);
|
||||||
expect(Utils.fromB64ToArray(result.iv).length).toEqual(16);
|
expect(Utils.fromB64ToArray(result.iv).length).toEqual(16);
|
||||||
expect(Utils.fromB64ToArray(result.mac).length).toEqual(32);
|
expect(Utils.fromB64ToArray(result.mac).length).toEqual(32);
|
||||||
});
|
});
|
||||||
@ -242,7 +271,10 @@ describe("EncryptService", () => {
|
|||||||
|
|
||||||
const actual = await encryptService.decryptToUtf8(encString, key);
|
const actual = await encryptService.decryptToUtf8(encString, key);
|
||||||
expect(actual).toEqual("data");
|
expect(actual).toEqual("data");
|
||||||
expect(cryptoFunctionService.compareFast).toHaveBeenCalled();
|
expect(cryptoFunctionService.compareFast).toHaveBeenCalledWith(
|
||||||
|
makeStaticByteArray(32, 0),
|
||||||
|
makeStaticByteArray(32, 0),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("decrypts data with provided key for Aes256Cbc", async () => {
|
it("decrypts data with provided key for Aes256Cbc", async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user