2022-10-10 17:19:01 +02:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
2022-09-27 03:17:43 +02:00
|
|
|
import { Substitute, Arg, SubstituteOf } from "@fluffy-spoon/substitute";
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { KdfType } from "@bitwarden/common/enums/kdfType";
|
|
|
|
import { BitwardenPasswordProtectedImporter } from "@bitwarden/common/importers/bitwardenPasswordProtectedImporter";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
import { ImportResult } from "@bitwarden/common/models/domain/importResult";
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2022-02-23 04:02:07 +01:00
|
|
|
import { data as emptyDecryptedData } from "./testData/bitwardenJson/empty.json";
|
|
|
|
|
2022-02-07 16:33:10 +01:00
|
|
|
describe("BitwardenPasswordProtectedImporter", () => {
|
|
|
|
let importer: BitwardenPasswordProtectedImporter;
|
|
|
|
let cryptoService: SubstituteOf<CryptoService>;
|
|
|
|
let i18nService: SubstituteOf<I18nService>;
|
|
|
|
const password = Utils.newGuid();
|
|
|
|
const result = new ImportResult();
|
|
|
|
let jDoc: {
|
|
|
|
encrypted?: boolean;
|
|
|
|
passwordProtected?: boolean;
|
|
|
|
salt?: string;
|
|
|
|
kdfIterations?: any;
|
|
|
|
kdfType?: any;
|
|
|
|
encKeyValidation_DO_NOT_EDIT?: string;
|
|
|
|
data?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
cryptoService = Substitute.for<CryptoService>();
|
|
|
|
i18nService = Substitute.for<I18nService>();
|
|
|
|
|
|
|
|
jDoc = {
|
|
|
|
encrypted: true,
|
|
|
|
passwordProtected: true,
|
|
|
|
salt: "c2FsdA==",
|
|
|
|
kdfIterations: 100000,
|
|
|
|
kdfType: KdfType.PBKDF2_SHA256,
|
|
|
|
encKeyValidation_DO_NOT_EDIT: Utils.newGuid(),
|
|
|
|
data: Utils.newGuid(),
|
|
|
|
};
|
|
|
|
|
|
|
|
result.success = true;
|
2022-02-23 04:02:07 +01:00
|
|
|
importer = new BitwardenPasswordProtectedImporter(cryptoService, i18nService, password);
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Required Json Data", () => {
|
|
|
|
it("succeeds with default jdoc", async () => {
|
2022-02-23 04:02:07 +01:00
|
|
|
cryptoService.decryptToUtf8(Arg.any(), Arg.any()).resolves(emptyDecryptedData);
|
2022-02-07 16:33:10 +01:00
|
|
|
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if encrypted === false", async () => {
|
|
|
|
jDoc.encrypted = false;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if encrypted === null", async () => {
|
|
|
|
jDoc.encrypted = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if passwordProtected === false", async () => {
|
|
|
|
jDoc.passwordProtected = false;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if passwordProtected === null", async () => {
|
|
|
|
jDoc.passwordProtected = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if salt === null", async () => {
|
|
|
|
jDoc.salt = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if kdfIterations === null", async () => {
|
|
|
|
jDoc.kdfIterations = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if kdfIterations is not a number", async () => {
|
|
|
|
jDoc.kdfIterations = "not a number";
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if kdfType === null", async () => {
|
|
|
|
jDoc.kdfType = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if kdfType is not a string", async () => {
|
|
|
|
jDoc.kdfType = "not a valid kdf type";
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if kdfType is not a known kdfType", async () => {
|
|
|
|
jDoc.kdfType = -1;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if encKeyValidation_DO_NOT_EDIT === null", async () => {
|
|
|
|
jDoc.encKeyValidation_DO_NOT_EDIT = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails if data === null", async () => {
|
|
|
|
jDoc.data = null;
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|