import { mock, MockProxy } from "jest-mock-extended";

import { KeyService } from "../../../../../key-management/src/abstractions/key.service";
import { makeEncString, makeStaticByteArray } from "../../../../spec";
import { EncryptService } from "../../../platform/abstractions/encrypt.service";
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
import { UserKey, OrgKey } from "../../../types/key";
import { EncryptionType } from "../../enums";
import { Utils } from "../../misc/utils";
import { ContainerService } from "../../services/container.service";

import { EncString } from "./enc-string";

describe("EncString", () => {
  afterEach(() => {
    (window as any).bitwardenContainerService = undefined;
  });

  describe("Rsa2048_OaepSha256_B64", () => {
    it("constructor", () => {
      const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data");

      expect(encString).toEqual({
        data: "data",
        encryptedString: "3.data",
        encryptionType: 3,
      });
    });

    describe("isSerializedEncString", () => {
      it("is true if valid", () => {
        expect(EncString.isSerializedEncString("3.data")).toBe(true);
      });

      it("is false if invalid", () => {
        expect(EncString.isSerializedEncString("3.data|test")).toBe(false);
      });
    });

    describe("parse existing", () => {
      it("valid", () => {
        const encString = new EncString("3.data");

        expect(encString).toEqual({
          data: "data",
          encryptedString: "3.data",
          encryptionType: 3,
        });
      });

      it("invalid", () => {
        const encString = new EncString("3.data|test");

        expect(encString).toEqual({
          encryptedString: "3.data|test",
          encryptionType: 3,
        });
      });

      const cases = [
        "aXY=|Y3Q=", // AesCbc256_B64 w/out header
        "aXY=|Y3Q=|cnNhQ3Q=", // AesCbc128_HmacSha256_B64 w/out header
        "0.QmFzZTY0UGFydA==|QmFzZTY0UGFydA==", // AesCbc256_B64 with header
        "1.QmFzZTY0UGFydA==|QmFzZTY0UGFydA==|QmFzZTY0UGFydA==", // AesCbc128_HmacSha256_B64
        "2.QmFzZTY0UGFydA==|QmFzZTY0UGFydA==|QmFzZTY0UGFydA==", // AesCbc256_HmacSha256_B64
        "3.QmFzZTY0UGFydA==", // Rsa2048_OaepSha256_B64
        "4.QmFzZTY0UGFydA==", // Rsa2048_OaepSha1_B64
        "5.QmFzZTY0UGFydA==|QmFzZTY0UGFydA==", // Rsa2048_OaepSha256_HmacSha256_B64
        "6.QmFzZTY0UGFydA==|QmFzZTY0UGFydA==", // Rsa2048_OaepSha1_HmacSha256_B64
      ];

      it.each(cases)("can retrieve data bytes for %s", (encryptedString) => {
        const encString = new EncString(encryptedString);

        const dataBytes = encString.dataBytes;
        expect(dataBytes).not.toBeNull();
        expect(dataBytes.length).toBeGreaterThan(0);
      });
    });

    describe("decrypt", () => {
      const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data");

      const keyService = mock<KeyService>();
      keyService.hasUserKey.mockResolvedValue(true);
      keyService.getUserKeyWithLegacySupport.mockResolvedValue(
        new SymmetricCryptoKey(makeStaticByteArray(32)) as UserKey,
      );

      const encryptService = mock<EncryptService>();
      encryptService.decryptToUtf8
        .calledWith(encString, expect.anything())
        .mockResolvedValue("decrypted");

      beforeEach(() => {
        (window as any).bitwardenContainerService = new ContainerService(
          keyService,
          encryptService,
        );
      });

      it("decrypts correctly", async () => {
        const decrypted = await encString.decrypt(null);

        expect(decrypted).toBe("decrypted");
      });

      it("result should be cached", async () => {
        const decrypted = await encString.decrypt(null);
        expect(encryptService.decryptToUtf8).toBeCalledTimes(1);

        expect(decrypted).toBe("decrypted");
      });
    });
  });

  describe("decryptWithKey", () => {
    const encString = new EncString(EncryptionType.Rsa2048_OaepSha256_B64, "data");

    const keyService = mock<KeyService>();
    const encryptService = mock<EncryptService>();
    encryptService.decryptToUtf8
      .calledWith(encString, expect.anything())
      .mockResolvedValue("decrypted");

    function setupEncryption() {
      encryptService.encrypt.mockImplementation(async (data, key) => {
        if (typeof data === "string") {
          return makeEncString(data);
        } else {
          return makeEncString(Utils.fromBufferToUtf8(data));
        }
      });
      encryptService.decryptToUtf8.mockImplementation(async (encString, key) => {
        return encString.data;
      });
      encryptService.decryptToBytes.mockImplementation(async (encString, key) => {
        return encString.dataBytes;
      });
    }

    beforeEach(() => {
      (window as any).bitwardenContainerService = new ContainerService(keyService, encryptService);
    });

    it("decrypts using the provided key and encryptService", async () => {
      setupEncryption();

      const key = new SymmetricCryptoKey(makeStaticByteArray(32));
      await encString.decryptWithKey(key, encryptService);

      expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(encString, key, "domain-withkey");
    });

    it("fails to decrypt when key is null", async () => {
      const decrypted = await encString.decryptWithKey(null, encryptService);

      expect(decrypted).toBe("[error: cannot decrypt]");
      expect(encString.decryptedValue).toBe("[error: cannot decrypt]");
    });

    it("fails to decrypt when encryptService is null", async () => {
      const decrypted = await encString.decryptWithKey(
        new SymmetricCryptoKey(makeStaticByteArray(32)),
        null,
      );

      expect(decrypted).toBe("[error: cannot decrypt]");
      expect(encString.decryptedValue).toBe("[error: cannot decrypt]");
    });

    it("fails to decrypt when encryptService throws", async () => {
      encryptService.decryptToUtf8.mockRejectedValue("error");

      const decrypted = await encString.decryptWithKey(
        new SymmetricCryptoKey(makeStaticByteArray(32)),
        encryptService,
      );

      expect(decrypted).toBe("[error: cannot decrypt]");
      expect(encString.decryptedValue).toBe("[error: cannot decrypt]");
    });
  });

  describe("AesCbc256_B64", () => {
    it("constructor", () => {
      const encString = new EncString(EncryptionType.AesCbc256_B64, "data", "iv");

      expect(encString).toEqual({
        data: "data",
        encryptedString: "0.iv|data",
        encryptionType: 0,
        iv: "iv",
      });
    });

    describe("isSerializedEncString", () => {
      it("is true if valid", () => {
        expect(EncString.isSerializedEncString("0.iv|data")).toBe(true);
      });

      it("is false if invalid", () => {
        expect(EncString.isSerializedEncString("0.iv|data|mac")).toBe(false);
      });
    });

    describe("parse existing", () => {
      it("valid", () => {
        const encString = new EncString("0.iv|data");

        expect(encString).toEqual({
          data: "data",
          encryptedString: "0.iv|data",
          encryptionType: 0,
          iv: "iv",
        });
      });

      it("invalid", () => {
        const encString = new EncString("0.iv|data|mac");

        expect(encString).toEqual({
          encryptedString: "0.iv|data|mac",
          encryptionType: 0,
        });
      });
    });
  });

  describe("AesCbc256_HmacSha256_B64", () => {
    it("constructor", () => {
      const encString = new EncString(EncryptionType.AesCbc256_HmacSha256_B64, "data", "iv", "mac");

      expect(encString).toEqual({
        data: "data",
        encryptedString: "2.iv|data|mac",
        encryptionType: 2,
        iv: "iv",
        mac: "mac",
      });
    });

    describe("isSerializedEncString", () => {
      it("is true if valid", () => {
        expect(EncString.isSerializedEncString("2.iv|data|mac")).toBe(true);
      });

      it("is false if invalid", () => {
        expect(EncString.isSerializedEncString("2.iv|data")).toBe(false);
      });

      it("should return false if a null string is passed in", () => {
        // Act
        const result = EncString.isSerializedEncString(null);
        // Assert
        expect(result).toBe(false);
      });

      it("should return false if an error is thrown while parsing the string", () => {
        // Arrange
        const value = "invalid.value";
        // Act
        const result = EncString.isSerializedEncString(value);
        // Assert
        expect(result).toBe(false);
      });

      describe("Access Token Parsing Tests", () => {
        const encryptedAccessToken =
          "2.rFNYSTJoljn8h6GOSNVYdQ==|4dIp7ONJzC+Kx1ClA+1aIAb7EqCQ4OjnADCYdCPg7BKkdheG+yM62ZiONFk+S6at84M+RnGWWO04aIjinTdJhlhyUmszePNATxIfX60Y+bFKQhlMuCtZpYdEmQDzXVgT43YRbf/6NnN9WzhefLqeMiocwoIJTEpLptb+Zcm7T3MJpkX4dR9w5LUOxUTNFEGd5PlWaI8FBavOkNsrzY5skRK70pvFABET5IDeRlKhi8NwbzvTzkO3SisLRzih+djiz5nEZf0+ujeGAp6P+o7l0mB0sXVsNJzcuE4S9QtHLnx31N6z3mQm5pOgP4EmEOdRIcQGc1p7dL1vXcXtaTJLtfKXoJjJbYT3wplnY9Pf8+2FVxdbM3bRB2yVsnEzgLcf9UchKThQSdOy8+5TO/prDbUt5mDpO4GmRltom5ncda8yJaD3Hw1DO7fa0Xh+kfeByxb1AwBC+GTPfqmo5uqr0J4dZsf9cGlPMTElwR3GYmD60OcQ6iDX36CZZjqqJqBwKSpepDXV39p9G347e6YAAvJenLDKtdjgfWXCMXbkwETbMgYooFDRd60KYsGIXV16UwzJSvczgTY2d+hYb2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w08nkk11vdPFVG87kWhR6ARYBWDp4AASy66YewqGhX7BNaekSTqK7DKxzQ9Adiv4XvmNEz3JO8tQrEFfE_mz9-WZiS6PlUipCxW-UtFp093_gHZh9_xgbuTdaO1u5_8Y42v0V_69v9WgzCGQGEWZ3PPaJsARGDO7FVKdPxP2S2lWIu22gydNHhfDZrOpBGHD1FpByfd5DbhKk0JdhHEPObs8RwNEweK-jlKmQpc_8bnhXFRUeMFrDL2Q2pNrYcDOpF1crIePPcWBk2_YdcWTqYVnGewT0toJ8sGlaAuAe6uOUZkBG3sxkOttkLYQtkxJYqt54gjazJ0N0GxAc0UBUDt0JnuLqk-cuxXiQO2_vHomTf7dilPq8fvqffrtrISxDVZenceg";

        it("should return false if a non-encrypted string is passed in", () => {
          // Act
          const result = EncString.isSerializedEncString(standardAccessToken);
          // Assert
          expect(result).toBe(false);
        });

        it("should return true if an encrypted string is passed in", () => {
          // Act
          const result = EncString.isSerializedEncString(encryptedAccessToken);
          // Assert
          expect(result).toBe(true);
        });
      });
    });

    it("valid", () => {
      const encString = new EncString("2.iv|data|mac");

      expect(encString).toEqual({
        data: "data",
        encryptedString: "2.iv|data|mac",
        encryptionType: 2,
        iv: "iv",
        mac: "mac",
      });
    });

    it("invalid", () => {
      const encString = new EncString("2.iv|data");

      expect(encString).toEqual({
        encryptedString: "2.iv|data",
        encryptionType: 2,
      });
    });
  });

  it("Exit early if null", () => {
    const encString = new EncString(null);

    expect(encString).toEqual({
      encryptedString: null,
    });
  });

  describe("decrypt", () => {
    let keyService: MockProxy<KeyService>;
    let encryptService: MockProxy<EncryptService>;
    let encString: EncString;

    beforeEach(() => {
      keyService = mock<KeyService>();
      encryptService = mock<EncryptService>();
      encString = new EncString(null);

      (window as any).bitwardenContainerService = new ContainerService(keyService, encryptService);
    });

    it("handles value it can't decrypt", async () => {
      encryptService.decryptToUtf8.mockRejectedValue("error");

      (window as any).bitwardenContainerService = new ContainerService(keyService, encryptService);

      const decrypted = await encString.decrypt(null);

      expect(decrypted).toBe("[error: cannot decrypt]");

      expect(encString).toEqual({
        decryptedValue: "[error: cannot decrypt]",
        encryptedString: null,
      });
    });

    it("uses provided key without depending on KeyService", async () => {
      const key = mock<SymmetricCryptoKey>();

      await encString.decrypt(null, key);

      expect(keyService.getUserKeyWithLegacySupport).not.toHaveBeenCalled();
      expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(encString, key, "provided-key");
    });

    it("gets an organization key if required", async () => {
      const orgKey = mock<OrgKey>();

      keyService.getOrgKey.calledWith("orgId").mockResolvedValue(orgKey);

      await encString.decrypt("orgId", null);

      expect(keyService.getOrgKey).toHaveBeenCalledWith("orgId");
      expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(
        encString,
        orgKey,
        "domain-orgkey-orgId",
      );
    });

    it("gets the user's decryption key if required", async () => {
      const userKey = mock<UserKey>();

      keyService.getUserKeyWithLegacySupport.mockResolvedValue(userKey);

      await encString.decrypt(null, null);

      expect(keyService.getUserKeyWithLegacySupport).toHaveBeenCalledWith();
      expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(
        encString,
        userKey,
        "domain-withlegacysupport-masterkey",
      );
    });
  });

  describe("toJSON", () => {
    it("Should be represented by the encrypted string", () => {
      const encString = new EncString(EncryptionType.AesCbc256_B64, "data", "iv");

      expect(encString.toJSON()).toBe(encString.encryptedString);
    });

    it("returns null if object is null", () => {
      expect(EncString.fromJSON(null)).toBeNull();
    });
  });
});
