<commit_before>require 'test_helper'

begin
  require 'openssl'
  OpenSSL::Digest::SHA1
rescue LoadError, NameError
  $stderr.puts "Skipping Encryptor test: broken OpenSSL install"
else

class EncryptorTest < ActiveSupport::TestCase
  setup do
    @data = { some: 'data', now: Time.local(2010) }
    @encryptor = Tokie::Encryptor.new(Tokie::Claims.new(@data), secret: SECRET)
  end

  test "encrypting twice yields differing cipher text" do
    first_message = @encryptor.encrypt.split('.').first
    second_message = @encryptor.encrypt.split('.').first
    refute_equal first_message, second_message
  end

  test "messing with either encrypted values causes failure" do
    text, iv = @verifier.verify(@encryptor.encrypt).split('.')
    refute_decrypted([iv, text] * '.')
    refute_decrypted([text, munge(iv)] * '.')
    refute_decrypted([munge(text), iv] * '.')
    refute_decrypted([munge(text), munge(iv)] * '.')
  end

  test "messing with verified values causes failures" do
    text, iv = @encryptor.encrypt.split('.')
    refute_verified([iv, text] * '.')
    refute_verified([text, munge(iv)] * '.')
    refute_verified([munge(text), iv] * '.')
    refute_verified([munge(text), munge(iv)] * '.')
  end

  test "signed round tripping" do
    token = @encryptor.encrypt
    assert_equal @data, decrypt(token)
  end

  test "alternative serialization method" do
    claims = Tokie::Claims.new({ foo: 123, 'bar' => Time.utc(2010) })
    token = Tokie::Encryptor.new(claims, serializer: JSON, secret: SECRET).encrypt

    exp = { "foo" => 123, "bar" => "2010-01-01 00:00:00 UTC" }
    assert_equal exp, decrypt(token).payload
  end

  test "message obeys strict encoding" do
    bad_encoding_characters = "\n!@#"
    claims = Tokie::Claims.new("This is a very \n\nhumble string"+bad_encoding_characters)
    message, iv = Tokie::Encryptor.new(claims, secret: SECRET).encrypt

    refute_decrypted("#{::Base64.encode64 message.to_s}.#{::Base64.encode64 iv.to_s}")
    refute_verified("#{::Base64.encode64 message.to_s}.#{::Base64.encode64 iv.to_s}")

    refute_decrypted([iv,  message] * bad_encoding_characters)
    refute_verified([iv,  message] * bad_encoding_characters)
  end

  private
    def refute_decrypted(value)
      assert_raise(Tokie::InvalidMessage) do
        claims = Tokie::Claims.new(value)
        decrypt Tokie::Signer.new(claims, secret: SECRET).sign
      end
    end

    def refute_verified(token)
      assert_raise(Tokie::InvalidSignature) { decrypt token }
    end

    def munge(base64_string)
      bits = ::Base64.strict_decode64(base64_string)
      bits.reverse!
      ::Base64.strict_encode64(bits)
    end

    def decrypt(token, options = {})
      Tokie::Encryptor.decrypt token, options.merge(secret: SECRET)
    end
end

end<commit_msg>Fix tests. Remove tests for verification.<commit_after>require 'test_helper'

begin
  require 'openssl'
  OpenSSL::Digest::SHA1
rescue LoadError, NameError
  $stderr.puts "Skipping Encryptor test: broken OpenSSL install"
else

class EncryptorTest < ActiveSupport::TestCase
  setup do
    @data = { some: 'data', now: Time.local(2010) }
    @encryptor = Tokie::Encryptor.new(Tokie::Claims.new(@data), secret: SECRET)
  end

  test "encrypting twice yields differing cipher text" do
    first_message  = @encryptor.encrypt.split('.')[2]
    second_message = @encryptor.encrypt.split('.')[2]
    refute_equal first_message, second_message
  end

  test "messing with either encrypted values causes failure" do
    header, key, claims, iv, auth_tag = @encryptor.encrypt.split('.')
    refute_decrypted [header, key, claims, auth_tag, iv] * '.'

    refute_decrypted [munge(header), key, claims, iv, auth_tag] * '.'
    refute_decrypted [header, munge(key), claims, iv, auth_tag] * '.'
    refute_decrypted [header, key, munge(claims), iv, auth_tag] * '.'
    refute_decrypted [header, key, claims, munge(iv), auth_tag] * '.'
    refute_decrypted [header, key, claims, iv, munge(auth_tag)] * '.'

    refute_decrypted [munge(header), munge(key), munge(claims), munge(iv), munge(auth_tag)] * '.'
  end

  test "signed round tripping" do
    token = @encryptor.encrypt
    assert_equal @data, decrypt(token).payload
  end

  test "alternative serialization method" do
    claims = Tokie::Claims.new({ foo: 123, 'bar' => Time.utc(2010) })
    token = Tokie::Encryptor.new(claims, serializer: JSON, secret: SECRET).encrypt

    exp = { "foo" => 123, "bar" => "2010-01-01 00:00:00 UTC" }
    assert_equal exp, decrypt(token).payload
  end

  test "message obeys strict encoding" do
    bad_encoding_characters = "\n!@#"
    claims = Tokie::Claims.new("This is a very \n\nhumble string"+bad_encoding_characters)
    parts = Tokie::Encryptor.new(claims, secret: SECRET).encrypt.split('.')

    refute_decrypted encode64(parts).join('.')
    refute_decrypted parts * bad_encoding_characters
  end

  private
    def refute_decrypted(token)
      assert_raise(Tokie::InvalidMessage) { decrypt token }
    end

    def munge(base64_string)
      bits = ::Base64.strict_decode64(base64_string)
      bits.reverse!
      ::Base64.strict_encode64(bits)
    end

    def encode64(args)
      args.map { |a| ::Base64.encode64 a }
    end

    def decrypt(token, options = {})
      Tokie::Encryptor.decrypt token, options.merge(secret: SECRET)
    end
end

end
{'fraction_non_alphanumeric': 0.13451061431523578, 'fraction_numerical': 0.02028931053916964, 'mean_word_length': 3.9896907216494846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7354765', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1843, 'n_words': 402}