Code.require_file("../test_helper.exs", __DIR__)

defmodule Mix.LocalTest do
  use MixTest.Case

  # openssl rsa -in elixirest.pem -pubout > elixirest.pub
  @public_key """
  -----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA37moKP1dGGLhsP3d8Fwv
  W25SoYZUY2K+Iq7A0OBV36Rnb8yW3BWjfh5YtmPvUCfYUbNCW2HTMMgBntkQ4YmN
  B9tHVZazl2uX9lGCfZZPFc/9umvKRojCPkMN81MfTxqnY0oaLHr6DB86RsWHB+ld
  782Xf+nd9q3LFdUl8SGlKX7uzfVWd4EWYNcL7aLeLSupZWeNg8uVmY3zua0EgIlQ
  XryalIOZb/R+pwprWZoftCl+20FGYi/mJpo/idFtXsR0sJKF4X0W3NORT9RIRbs9
  WdjiFi+eIP7Nm8KSF4pbaXCqSmVf9cgvUuGTxc9/P5GcIPAlkcsSrE5peLyUCk5f
  2QIDAQAB
  -----END PUBLIC KEY-----
  """

  # openssl genrsa -aes256 -out elixirtest.pem -passout stdin 2048
  @private_key """
  -----BEGIN RSA PRIVATE KEY-----
  Proc-Type: 4,ENCRYPTED
  DEK-Info: AES-256-CBC,48BA5153DA2F120ECE063B33C1204A49

  5gp3daNWujH7o9S/dJQEt9TYTRP0pPZtU55PlZrzWt52optr7XHW/ENOm84g5J70
  QCPELp12jfQsNiPwbVWXKy2zD3QlNiAelf65hqLWJTWli7XIXfdP46VXOu67OKf9
  Ziw4HQ+AdBEwFt20wJst77iy17sNlyxp5DhNDonnSizzIowgUAJkoNI5aBUU6D8X
  KTSIftZW35Z4SudkazdoHepEfItZTI8mB4rvfn71Q4oOBA1rAuUUmdPWoPBfUHDa
  hvIp2T2Q8zZYqm0+SjDxZUYOOreE7fuf5NSLhHHt7+jyWQmtaVxnOWms72G+9xT0
  NGmOEB0WEg1kBsUbYOXXwyCAZhNA6MaKCtgjQczRTK+geS1xNaFc9FDEk3ZjN4Z8
  PxrKQoqo+2aQGVcat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Zzv8fo1binMKpptFzokbWEOjcPCb3tPuomG
  ZRkW3qO2pdyYX2N7VXYG9tGi2HrN/oFrWnHPoYF23v85V8WxNkODOCpTz85e6R5v
  PVu+FCNFj5weEOTRhtEQyJo7mU5qIRwYeZvVxiC6W+XeFs95wdBE/Lvpg8yZ8D9d
  -----END RSA PRIVATE KEY-----
  """

  @csv """
  1.2.5,ABC,0.9.0
  1.2.3,DEF,1.0.0
  1.2.4,GHI,1.0.0
  """

  # openssl dgst -sha512 -sign elixirtest.pem hex-1.x.csv | openssl base64 > elixirtest.csv.signed
  @csv_signed """
  VRydmXOdEXQcKJu/SK/nKnE00T+s/T4mpXrYROMSXhD/s8ClvdimnGg61ie3YBS6
  LXOjlEhbtMHRM2rTOUvv4z7FcyzwvSxSjunlVi2g3c1pVOZ78MonnYhGb44tZw/q
  SOVmV+jJhc9EZFMIAAM3plMoyssyw2pMh7ZB/DxCQTIem3Qf0Ujzc2bYkLVlw7R+
  1Rn6dcYEgCzyldVkAUMaYBwieyweWALA+YVDCMudJJK2J7p1OnuoPSVV+N3OkB/Z
  T6Jj5ljD+54XnuxAMcgCoF9lpOwXscnw/Ma+8JqIoWo0jNFE3ji+8dGCUzQUdSe8
  llLXgJJE2tGpDhEXBA3idg==
  """

  # We don't actually use it but it exists for documentation purposes.
  _ = @private_key

  setup_all do
    File.mkdir_p!(Mix.Local.public_keys_path())

    Mix.Local.public_keys_path()
    |> Path.join("test_key.pub")
    |> File.write!(@public_key)
  end

  @tag :tmp_dir
  test "select correct versions from csv", %{tmp_dir: tmp_dir} do
    File.cd!(tmp_dir, fn ->
      File.write!("csv", @csv)
      File.write!("csv.signed", @csv_signed)

      assert {"1.0.0", "1.2.4", "GHI"} =
               Mix.Local.find_matching_versions_from_signed_csv!("name", nil, "csv")
    end)
  end

  @tag :tmp_dir
  test "select specific version from csv", %{tmp_dir: tmp_dir} do
    File.cd!(tmp_dir, fn ->
      File.write!("csv", @csv)
      File.write!("csv.signed", @csv_signed)

      assert {"0.9.0", "1.2.5", "ABC"} =
               Mix.Local.find_matching_versions_from_signed_csv!("name", "1.2.5", "csv")

      assert {"1.0.0", "1.2.3", "DEF"} =
               Mix.Local.find_matching_versions_from_signed_csv!("name", "1.2.3", "csv")

      assert_raise Mix.Error, "Could not find a version of name matching: 1.3.0", fn ->
        Mix.Local.find_matching_versions_from_signed_csv!("name", "1.3.0", "csv")
      end
    end)
  end

  @tag :tmp_dir
  test "raise on bad signature", %{tmp_dir: tmp_dir} do
    File.cd!(tmp_dir, fn ->
      csv_signed = String.replace(@csv_signed, "VRy", "BAD")
      File.write!("csv", @csv)
      File.write!("csv.signed", csv_signed)

      assert_raise Mix.Error, fn ->
        Mix.Local.find_matching_versions_from_signed_csv!("name", nil, "csv")
      end
    end)
  end
end
