2023-04-06 22:41:09 +02:00
|
|
|
import { mock, MockProxy } from "jest-mock-extended";
|
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";
|
[AC-1266] Enums filename conventions (#5140)
* refactor: update clientType enum
* refactor: update deviceType filename
* refactor: update encryptedExportType filename
* refactor: update encryptionType filename
* refactor: update eventType filename
* refactor: update fieldType filename
* refactor: update fileUploadType filename
* refactor: update hashPurpose filename
* refactor: update htmlStorageLocation filename
* refactor: update kdfType filename
* refactor: update keySuffixOptions filename
* refactor: update linkedIdType filename
* refactor: update logLevelType filename
* refactor: update nativeMessagingVersion filename
* refactor: update notificationType filename
* refactor: update productType filename
* refactor: update secureNoteType filename
* refactor: update stateVersion filename
* refactor: update storageLocation filename
* refactor: update themeType filename
* refactor: update uriMatchType filename
* fix: update kdfType classes missed in initial pass, refs AC-1266
* fix: missing import update for device-type
* refactor: add barrel file for enums and update pathed import statements, refs AC-1266
* fix: incorrect import statements for web, refs AC-1266
* fix: missed import statement updates (browser), refs AC-1266
* fix: missed import statement changes (cli), refs AC-1266
* fix: missed import statement changes (desktop), refs AC-1266
* fix: prettier, refs AC-1266
* refactor: (libs) update relative paths to use barrel file, refs AC-1266
* fix: missed find/replace import statements for SecureNoteType, refs AC-1266
* refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266
* fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-05 05:42:21 +02:00
|
|
|
import { KdfType } from "@bitwarden/common/enums";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
2023-03-23 11:43:27 +01:00
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
import {
|
|
|
|
BitwardenPasswordProtectedImporter,
|
|
|
|
BitwardenJsonImporter,
|
|
|
|
} from "../src/importers/bitwarden";
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
import { emptyAccountEncrypted } from "./test-data/bitwarden-json/account-encrypted.json";
|
|
|
|
import { emptyUnencryptedExport } from "./test-data/bitwarden-json/unencrypted.json";
|
2022-02-23 04:02:07 +01:00
|
|
|
|
2022-02-07 16:33:10 +01:00
|
|
|
describe("BitwardenPasswordProtectedImporter", () => {
|
|
|
|
let importer: BitwardenPasswordProtectedImporter;
|
2023-04-06 22:41:09 +02:00
|
|
|
let cryptoService: MockProxy<CryptoService>;
|
|
|
|
let i18nService: MockProxy<I18nService>;
|
2022-02-07 16:33:10 +01:00
|
|
|
const password = Utils.newGuid();
|
2023-04-06 22:41:09 +02:00
|
|
|
const promptForPassword_callback = async () => {
|
|
|
|
return password;
|
2022-02-07 16:33:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-04-06 22:41:09 +02:00
|
|
|
cryptoService = mock<CryptoService>();
|
|
|
|
i18nService = mock<I18nService>();
|
|
|
|
|
|
|
|
importer = new BitwardenPasswordProtectedImporter(
|
|
|
|
cryptoService,
|
|
|
|
i18nService,
|
|
|
|
promptForPassword_callback
|
|
|
|
);
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
describe("Unencrypted", () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(BitwardenJsonImporter.prototype, "parse");
|
|
|
|
});
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
it("Should call BitwardenJsonImporter", async () => {
|
|
|
|
expect((await importer.parse(emptyUnencryptedExport)).success).toEqual(true);
|
|
|
|
expect(BitwardenJsonImporter.prototype.parse).toHaveBeenCalledWith(emptyUnencryptedExport);
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
2023-04-06 22:41:09 +02:00
|
|
|
});
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
describe("Account encrypted", () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(BitwardenJsonImporter.prototype, "parse");
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
it("Should call BitwardenJsonImporter", async () => {
|
|
|
|
expect((await importer.parse(emptyAccountEncrypted)).success).toEqual(true);
|
|
|
|
expect(BitwardenJsonImporter.prototype.parse).toHaveBeenCalledWith(emptyAccountEncrypted);
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
2023-04-06 22:41:09 +02:00
|
|
|
});
|
2022-02-07 16:33:10 +01:00
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
describe("Password protected", () => {
|
|
|
|
let jDoc: {
|
|
|
|
encrypted?: boolean;
|
|
|
|
passwordProtected?: boolean;
|
|
|
|
salt?: string;
|
|
|
|
kdfIterations?: any;
|
|
|
|
kdfType?: any;
|
|
|
|
encKeyValidation_DO_NOT_EDIT?: string;
|
|
|
|
data?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jDoc = {
|
|
|
|
encrypted: true,
|
|
|
|
passwordProtected: true,
|
|
|
|
salt: "c2FsdA==",
|
|
|
|
kdfIterations: 100000,
|
|
|
|
kdfType: KdfType.PBKDF2_SHA256,
|
|
|
|
encKeyValidation_DO_NOT_EDIT: Utils.newGuid(),
|
|
|
|
data: Utils.newGuid(),
|
|
|
|
};
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
|
|
|
|
2023-04-06 22:41:09 +02:00
|
|
|
it("succeeds with default jdoc", async () => {
|
|
|
|
cryptoService.decryptToUtf8.mockReturnValue(Promise.resolve(emptyUnencryptedExport));
|
|
|
|
|
|
|
|
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(true);
|
2022-02-07 16:33:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|