Module: RSpec::PGPMatchers::GPGRunner

Defined in:
lib/rspec/pgp_matchers/gpg_runner.rb

Overview

A helper module for executing GnuPG commands.

Class Method Summary collapse

Class Method Details

.run_command(gpg_cmd) ⇒ Array

Executes arbitrary GnuPG command.

Examples:

# Will list all GnuPG keys
run_command("--list-keys")
# Will list keys and their keygrips
run_command("--list-keys --with-keygrip")

Parameters:

  • gpg_cmd (String)

    command to run

Returns:

  • (Array)

    tuple [stdout, stderr, status] like in stdlib’s Open3.capture3



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec/pgp_matchers/gpg_runner.rb', line 23

def run_command(gpg_cmd)
  env = { "LC_ALL" => "C" } # Gettext English locale

  gpg_executable = Shellwords.escape(RSpec::PGPMatchers.gpg_executable)
  homedir_path = Shellwords.escape(RSpec::PGPMatchers.homedir)

  Open3.capture3(env, <<~SH)
    #{gpg_executable} \
    --homedir #{homedir_path} \
    --no-permission-warning \
    #{gpg_cmd}
  SH
end

.run_decrypt(encrypted_string) ⇒ Array

Decrypts a message.

Parameters:

  • encrypted_string (String)

    encrypted message

Returns:

  • (Array)

    tuple [stdout, stderr, status] like in stdlib’s Open3.capture3



42
43
44
45
46
47
48
# File 'lib/rspec/pgp_matchers/gpg_runner.rb', line 42

def run_decrypt(encrypted_string)
  enc_file = make_tempfile_containing(encrypted_string)
  cmd = gpg_decrypt_command(enc_file)
  run_command(cmd)
ensure
  File.unlink(enc_file.path)
end

.run_verify(cleartext, signature_string) ⇒ Array

Verifies a signature.

Parameters:

  • cleartext (String)

    message in clear text

  • signature_string (String)

    signature

Returns:

  • (Array)

    tuple [stdout, stderr, status] like in stdlib’s Open3.capture3



56
57
58
59
60
61
62
63
# File 'lib/rspec/pgp_matchers/gpg_runner.rb', line 56

def run_verify(cleartext, signature_string)
  sig_file = make_tempfile_containing(signature_string)
  data_file = make_tempfile_containing(cleartext)
  cmd = gpg_verify_command(sig_file, data_file)
  run_command(cmd)
ensure
  File.unlink(sig_file.path, data_file.path)
end