<commit_before># -*- encoding: utf-8 -*-
require 'test_helper'

describe GPGME::Ctx do
  before do
    skip unless GPGME::Engine.check_version GPGME::PROTOCOL_OpenPGP
  end

  it "can instantiate" do
    assert_instance_of GPGME::Ctx, GPGME::Ctx.new
  end

  it "doesn't close itself" do
    GPGME.expects(:gpgme_release).never
    GPGME::Ctx.new
  end

  it "closes itself if called with a block" do
    GPGME.expects(:gpgme_release).with(anything)
    GPGME::Ctx.new { |ctx| }
  end

  it "can be closed with the release method" do
    GPGME.expects(:gpgme_release).with(anything)
    ctx = GPGME::Ctx.new
    ctx.release
  end

  describe :new do
    # We consider :armor, :protocol, :textmode and :keylist_mode as tested
    # with the other tests of this file. Here we test the rest

    it ":password sets the password for the key" do
      with_key PASSWORD_KEY do
        input  = GPGME::Data.new(TEXT[:passwored])
        output = GPGME::Data.new

        GPGME::Ctx.new(:password => '<PASSWORD>') do |ctx|
          ctx.decrypt_verify input, output

          output.seek 0
          assert_equal "Hi there", output.read.chomp

          recipients = ctx.decrypt_result.recipients
          assert_equal 1, recipients.size

          recipient_key = ctx.get_key(recipients.first.keyid)
          key = ctx.get_key(PASSWORD_KEY[:sha])

          assert_equal recipient_key, key
        end
      end
    end

    it ":passphrase_callback sets the callback for the password" do
      def test_pass_func(obj,par2,par3,prev_was_bad,fd)
        # prev_was_bad is 0 the first time, 1 the rest
        if @var == 0
          assert_equal 0, prev_was_bad
        else
          assert_equal 1, prev_was_bad
        end

        @var += 1

        io = IO.for_fd(fd, 'w')
        io.puts "wrong pasword"
        io.flush
      end

      def with_correct_pass_func(obj,par2,par3,prev_was_bad,fd)
        io = IO.for_fd(fd, 'w')
        io.puts "gpgme"
        io.flush
      end

      with_key PASSWORD_KEY do
        input  = GPGME::Data.new(TEXT[:passwored])
        output = GPGME::Data.new
        @var = 0

        assert_raises GPGME::Error::BadPassphrase do
          GPGME::Ctx.new(:passphrase_callback => method(:test_pass_func)) do |ctx|
            ctx.decrypt_verify input, output
          end
        end

        # Since we request the key 3 times, we should've gone through the
        # callback 3 times.
        assert_equal 3, @var

        input.seek 0
        output.seek 0

        # Shouldn't crash
        GPGME::Ctx.new(:passphrase_callback => method(:with_correct_pass_func)) do |ctx|
          ctx.decrypt_verify input, output
        end
      end
    end

    it ":passphrase_callback_value passes a value to the callback function" do
      def checking_value(value,par2,par3,par4,fd)
        assert_equal "superman", value
        io = IO.for_fd(fd, 'w')
        io.puts "gpgme"
        io.flush
      end

      with_key PASSWORD_KEY do
        input  = GPGME::Data.new(TEXT[:passwored])
        output = GPGME::Data.new

        options = {
          :passphrase_callback => method(:checking_value),
          :passphrase_callback_value => "superman"
        }

        GPGME::Ctx.new(options) do |ctx|
          ctx.decrypt_verify input, output
        end
      end
    end

    # TODO Don't know how to use them yet
    # it ":progress_callback"
    # it ":progress_callback_value"
  end

  describe :decrypt_result do
    it "returns the list of encyption recipients" do
      cipher = GPGME::Data.new(KEY_1_ENCRYPTED)
      output = GPGME::Data.new

      GPGME::Ctx.new do |ctx|
        ctx.decrypt_verify(cipher, output)
        assert_equal 1, ctx.decrypt_result.recipients.size
      end
    end

#    it "should not segfault" do
#      cipher = GPGME::Data.new(KEY_1_ENCRYPTED)
#      ouput = GPGME::Data.new
#      
#      GPGME::Ctx.new do |ctx|
#        # This should fail more gracefully
#        ctx.decrypt_result
#      end
#    end
  end

  describe :armor do
    it "sets false by default" do
      ctx = GPGME::Ctx.new
      refute ctx.armor
    end

    it "can set" do
      ctx = GPGME::Ctx.new
      ctx.armor = true
      assert ctx.armor
    end<commit_msg>Failing test for already fixed https://github.com/ueno/ruby-gpgme/issues/57<commit_after># -*- encoding: utf-8 -*-
require 'test_helper'

describe GPGME::Ctx do
  before do
    skip unless GPGME::Engine.check_version GPGME::PROTOCOL_OpenPGP
  end

  it "can instantiate" do
    assert_instance_of GPGME::Ctx, GPGME::Ctx.new
  end

  it "doesn't close itself" do
    GPGME.expects(:gpgme_release).never
    GPGME::Ctx.new
  end

  it "closes itself if called with a block" do
    GPGME.expects(:gpgme_release).with(anything)
    GPGME::Ctx.new { |ctx| }
  end

  it "can be closed with the release method" do
    GPGME.expects(:gpgme_release).with(anything)
    ctx = GPGME::Ctx.new
    ctx.release
  end

  describe :new do
    # We consider :armor, :protocol, :textmode and :keylist_mode as tested
    # with the other tests of this file. Here we test the rest

    it ":password sets the password for the key" do
      with_key PASSWORD_KEY do
        input  = GPGME::Data.new(TEXT[:passwored])
        output = GPGME::Data.new

        GPGME::Ctx.new(:password => '<PASSWORD>') do |ctx|
          ctx.decrypt_verify input, output

          output.seek 0
          assert_equal "Hi there", output.read.chomp

          recipients = ctx.decrypt_result.recipients
          assert_equal 1, recipients.size

          recipient_key = ctx.get_key(recipients.first.keyid)
          key = ctx.get_key(PASSWORD_KEY[:sha])

          assert_equal recipient_key, key
        end
      end
    end

    it ":passphrase_callback sets the callback for the password" do
      def test_pass_func(obj,par2,par3,prev_was_bad,fd)
        # prev_was_bad is 0 the first time, 1 the rest
        if @var == 0
          assert_equal 0, prev_was_bad
        else
          assert_equal 1, prev_was_bad
        end

        @var += 1

        io = IO.for_fd(fd, 'w')
        io.puts "wrong pasword"
        io.flush
      end

      def with_correct_pass_func(obj,par2,par3,prev_was_bad,fd)
        io = IO.for_fd(fd, 'w')
        io.puts "gpgme"
        io.flush
      end

      with_key PASSWORD_KEY do
        input  = GPGME::Data.new(TEXT[:passwored])
        output = GPGME::Data.new
        @var = 0

        assert_raises GPGME::Error::BadPassphrase do
          GPGME::Ctx.new(:passphrase_callback => method(:test_pass_func)) do |ctx|
            ctx.decrypt_verify input, output
          end
        end

        # Since we request the key 3 times, we should've gone through the
        # callback 3 times.
        assert_equal 3, @var

        input.seek 0
        output.seek 0

        # Shouldn't crash
        GPGME::Ctx.new(:passphrase_callback => method(:with_correct_pass_func)) do |ctx|
          ctx.decrypt_verify input, output
        end
      end
    end

    it ":passphrase_callback_value passes a value to the callback function" do
      def checking_value(value,par2,par3,par4,fd)
        assert_equal "superman", value
        io = IO.for_fd(fd, 'w')
        io.puts "gpgme"
        io.flush
      end

      with_key PASSWORD_KEY do
        input  = GPGME::Data.new(TEXT[:passwored])
        output = GPGME::Data.new

        options = {
          :passphrase_callback => method(:checking_value),
          :passphrase_callback_value => "<PASSWORD>"
        }

        GPGME::Ctx.new(options) do |ctx|
          ctx.decrypt_verify input, output
        end
      end
    end

    # TODO Don't know how to use them yet
    # it ":progress_callback"
    # it ":progress_callback_value"
  end

  describe :decrypt_result do
    it "returns the list of encyption recipients" do
      cipher = GPGME::Data.new(KEY_1_ENCRYPTED)
      output = GPGME::Data.new

      GPGME::Ctx.new do |ctx|
        ctx.decrypt_verify(cipher, output)
        assert_equal 1, ctx.decrypt_result.recipients.size
      end
    end

    it "should not segfault" do
      cipher = GPGME::Data.new(KEY_1_ENCRYPTED)
      ouput = GPGME::Data.new
      
      GPGME::Ctx.new do |ctx|
        assert_raises ArgumentError do
          ctx.decrypt_result
        end
      end
    end
  end

  describe :armor do
    it "sets false by default" do
      ctx = GPGME::Ctx.new
      refute ctx.armor
    end

    it "can set" do
      ctx = GPGME::Ctx.new
      ctx.armor = true
      assert ctx.armor
    end
{'fraction_non_alphanumeric': 0.09071655488881045, 'fraction_numerical': 0.006118366866690199, 'mean_word_length': 2.2099697885196377, 'pattern_counts': {'":': 10, '<': 6, '<?xml version=': 0, '>': 16, 'https://': 1, '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': '573448', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2796, 'n_words': 719}