<commit_before>require 'test_helper'

if TEST_TYPE == :refinement
  # JRuby 9.0.5.0 ignores the conditional and imports the refinements
  # regardless. Use send to prevent this.
  send(:using, PuTTY::Key)
end

class OpenSSLTest < Minitest::Test
  def test_from_ppk_nil
    assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(nil) }
  end

  def test_from_ppk_unsupported_algorithm
    ppk = PuTTY::Key::PPK.new(fixture_path('test.ppk'))
    assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
  end

  def test_from_ppk_rsa
    ppk = PuTTY::Key::PPK.new(fixture_path('rsa-2048.ppk'))
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::RSA, pkey)
    assert_equal(load_fixture('rsa-2048.pem'), pkey.to_pem)
  end

  def test_from_ppk_rsa_encrypted
    ppk = PuTTY::Key::PPK.new(fixture_path('rsa-2048-encrypted.ppk'), 'Test Passphrase')
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::RSA, pkey)
    assert_equal(load_fixture('rsa-2048.pem'), pkey.to_pem)
  end

  def test_from_ppk_dss
    ppk = PuTTY::Key::PPK.new(fixture_path('dss-1024.ppk'))
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::DSA, pkey)
    assert_equal(load_fixture('dss-1024.pem'), pkey.to_pem)
  end

  def test_from_ppk_dss_encrypted
    ppk = PuTTY::Key::PPK.new(fixture_path('dss-1024-encrypted.ppk'), 'Test Passphrase')
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::DSA, pkey)
    assert_equal(load_fixture('dss-1024.pem'), pkey.to_pem)
  end

  # jruby-openssl doesn't include an EC class (version 0.9.16)
  if defined?(OpenSSL::PKey::EC)
    def test_from_ppk_ecdsa_sha2_nistp256
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256.ppk'))
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp256.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp256_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256-encrypted.ppk'), 'Test Passphrase')
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp256.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp384
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384.ppk'))
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp384.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp384_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384-encrypted.ppk'), 'Test Passphrase')
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp384.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp521
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521.ppk'))
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp521.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp521_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521-encrypted.ppk'), 'Test Passphrase')
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp521.pem'), pkey.to_pem)
    end
  else
    def test_from_ppk_ecdsa_sha2_nistp256
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256.ppk'))
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp256_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256-encrypted.ppk'), 'Test Passphrase')
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp384
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384.ppk'))
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp384_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384-encrypted.ppk'), 'Test Passphrase')
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp521
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521.ppk'))
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp521_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521-encrypted.ppk'), 'Test Passphrase')
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end
  end

  def pem_to_ppk(fixture, type = nil)
    pem = load_fixture(fixture)

    # Accessing OpenSSL::PKey::EC#public_key raises a warning when the key was
    # loaded with OpenSSL::PKey.read(pem), but doesn't when instatiated with
    # OpenSSL::PKey::EC.new(pem) (Ruby 2.3.0).
    pkey = type ? type.new(pem) : OpenSSL::PKey.read(pem)

    pkey.to_ppk.tap do |ppk|
      assert_nil(ppk.comment)
    end
  end

  def test_to_ppk_rsa
    ppk = pem_to_ppk('rsa-2048.pem')
    ppk.comment = '2048 bit RSA key'
    temp_file_name do |file|
      ppk.save(file)
      assert_identical_to_fixture('rsa-2048.ppk', file)
    end
  end

  def test_to_ppk_rsa_encrypted
    ppk = pem_to_ppk('rsa-2048.pem')
    ppk.comment = '2048 bit RSA key'
    temp_file_name do |file|
      ppk.save(file, 'Test Passphrase')<commit_msg>Fix typo.<commit_after>require 'test_helper'

if TEST_TYPE == :refinement
  # JRuby 9.0.5.0 ignores the conditional and imports the refinements
  # regardless. Use send to prevent this.
  send(:using, PuTTY::Key)
end

class OpenSSLTest < Minitest::Test
  def test_from_ppk_nil
    assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(nil) }
  end

  def test_from_ppk_unsupported_algorithm
    ppk = PuTTY::Key::PPK.new(fixture_path('test.ppk'))
    assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
  end

  def test_from_ppk_rsa
    ppk = PuTTY::Key::PPK.new(fixture_path('rsa-2048.ppk'))
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::RSA, pkey)
    assert_equal(load_fixture('rsa-2048.pem'), pkey.to_pem)
  end

  def test_from_ppk_rsa_encrypted
    ppk = PuTTY::Key::PPK.new(fixture_path('rsa-2048-encrypted.ppk'), 'Test Passphrase')
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::RSA, pkey)
    assert_equal(load_fixture('rsa-2048.pem'), pkey.to_pem)
  end

  def test_from_ppk_dss
    ppk = PuTTY::Key::PPK.new(fixture_path('dss-1024.ppk'))
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::DSA, pkey)
    assert_equal(load_fixture('dss-1024.pem'), pkey.to_pem)
  end

  def test_from_ppk_dss_encrypted
    ppk = PuTTY::Key::PPK.new(fixture_path('dss-1024-encrypted.ppk'), 'Test Passphrase')
    pkey = OpenSSL::PKey.from_ppk(ppk)
    assert_kind_of(OpenSSL::PKey::DSA, pkey)
    assert_equal(load_fixture('dss-1024.pem'), pkey.to_pem)
  end

  # jruby-openssl doesn't include an EC class (version 0.9.16)
  if defined?(OpenSSL::PKey::EC)
    def test_from_ppk_ecdsa_sha2_nistp256
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256.ppk'))
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp256.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp256_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256-encrypted.ppk'), 'Test Passphrase')
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp256.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp384
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384.ppk'))
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp384.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp384_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384-encrypted.ppk'), 'Test Passphrase')
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp384.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp521
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521.ppk'))
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp521.pem'), pkey.to_pem)
    end

    def test_from_ppk_ecdsa_sha2_nistp521_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521-encrypted.ppk'), 'Test Passphrase')
      pkey = OpenSSL::PKey.from_ppk(ppk)
      assert_kind_of(OpenSSL::PKey::EC, pkey)
      assert_equal(load_fixture('ecdsa-sha2-nistp521.pem'), pkey.to_pem)
    end
  else
    def test_from_ppk_ecdsa_sha2_nistp256
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256.ppk'))
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp256_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp256-encrypted.ppk'), 'Test Passphrase')
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp384
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384.ppk'))
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp384_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp384-encrypted.ppk'), 'Test Passphrase')
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp521
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521.ppk'))
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end

    def test_from_ppk_ecdsa_sha2_nistp521_encrypted
      ppk = PuTTY::Key::PPK.new(fixture_path('ecdsa-sha2-nistp521-encrypted.ppk'), 'Test Passphrase')
      assert_raises(ArgumentError) { OpenSSL::PKey.from_ppk(ppk) }
    end
  end

  def pem_to_ppk(fixture, type = nil)
    pem = load_fixture(fixture)

    # Accessing OpenSSL::PKey::EC#public_key raises a warning when the key was
    # loaded with OpenSSL::PKey.read(pem), but doesn't when instantiated with
    # OpenSSL::PKey::EC.new(pem) (Ruby 2.3.0).
    pkey = type ? type.new(pem) : OpenSSL::PKey.read(pem)

    pkey.to_ppk.tap do |ppk|
      assert_nil(ppk.comment)
    end
  end

  def test_to_ppk_rsa
    ppk = pem_to_ppk('rsa-2048.pem')
    ppk.comment = '2048 bit RSA key'
    temp_file_name do |file|
      ppk.save(file)
      assert_identical_to_fixture('rsa-2048.ppk', file)
    end
  end

  def test_to_ppk_rsa_encrypted
    ppk = pem_to_ppk('rsa-2048.pem')
    ppk.comment = '2048 bit RSA key'
    temp_file_name do |file|
      ppk.save(file, 'Test Passphrase')
{'fraction_non_alphanumeric': 0.13048196039424903, 'fraction_numerical': 0.0330952165656931, 'mean_word_length': 4.330120481927711, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6160652', 'n_tokens_mistral': 5067, 'n_tokens_neox': 4805, 'n_words': 572}