Sha256: f34f25f857e4a1614d3d779dc8e209f917c6ba8aa6d0a5261c28906a6b60b524

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# (c) Copyright 2018 Ribose Inc.
#

require "open3"
require "tempfile"

module RSpec
  module PGPMatchers
    module GPGMatcherHelper
      def detect_signers(stderr_str)
        rx = /(?<ok>Good|BAD) signature from .*\<(?<email>[^>]+)\>/

        stderr_str.to_enum(:scan, rx).map do
          {
            email: $~["email"],
            ok: ($~["ok"] == "Good"),
          }
        end
      end

      def detect_recipients(stderr_str)
        rx = /encrypted with .*\n.*\<(?<email>[^>]+)\>/

        stderr_str.to_enum(:scan, rx).map do
          $~["email"]
        end
      end

      def match_cleartext(cleartext)
        if cleartext != expected_cleartext
          msg_mismatch(cleartext)
        end
      end

      def match_recipients(recipients)
        if expected_recipients.sort != recipients.sort
          msg_wrong_recipients(recipients)
        end
      end

      # Checks if signature is valid.  If `expected_signer` is not `nil`, then
      # it additionally checks if the signature was issued by expected signer.
      def match_signature(signature)
        if !signature[:ok]
          msg_mismatch(text)
        elsif expected_signer && signature[:email] != expected_signer
          msg_wrong_signer(signature[:email])
        end
      end

      def make_tempfile_containing(file_content)
        tempfile = Tempfile.new
        tempfile.write(file_content)
        tempfile.flush
      end

      def run_command(cmd)
        env = { "LC_ALL" => "C" } # Gettext English locale
        Open3.capture3(env, cmd)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-pgp_matchers-0.1.0 lib/rspec/pgp_matchers/gpg_matcher_helper.rb