GPGME.import/export moved from HighLevel to GPGME::Key

This commit is contained in:
Albert Llop 2011-04-24 14:17:00 +02:00
Родитель 59869c326b
Коммит 0440cc295a
5 изменённых файлов: 183 добавлений и 106 удалений

Просмотреть файл

@ -34,6 +34,7 @@ module GPGME
# signers. Must be an array of sign identifiers.
# * +:output+ if specified, it will write the output into it. It will be
# converted to a {GPGME::Data} object, so it could be a file for example.
# * Any other option accepted by {GPGME::Ctx.new}
#
# @return [GPGME::Data] a {GPGME::Data} object that can be read.
#
@ -113,6 +114,7 @@ module GPGME
# * +:output+ if specified, it will write the output into it. It will
# me converted to a {GPGME::Data} object, so it can also be a file,
# for example.
# * Any other option accepted by {GPGME::Ctx.new}
#
# @param &block
# In the block all the signatures are yielded, so one could verify them.
@ -192,6 +194,7 @@ module GPGME
# not specified.
# - +GPGME::SIG_MODE_DETACH+ for a detached signature
# - +GPGME::SIG_MODE_CLEAR+ for a cleartext signature
# * Any other option accepted by {GPGME::Ctx.new}
#
# @return [GPGME::Data] a {GPGME::Data} that can be read.
#
@ -249,6 +252,8 @@ module GPGME
# for which the signature was created.
# * +:output+ where to store the result of the signature. Will be
# converted to a {GPGME::Data} object.
# * Any other option accepted by {GPGME::Ctx.new}
#
# @param &block
# In the block all the signatures are yielded, so one could verify them.
# See examples.
@ -316,59 +321,6 @@ module GPGME
GPGME.sign text, options.merge(:mode => GPGME::SIG_MODE_DETACH)
end
# Exports a key
#
# GPGME.export pattern, options
#
# @param pattern
# Identifier of the key to export.
#
# @param [Hash] options
# * +:output+ specify where to write the key to. It will be converted to
# a {GPGME::Data}, so it could be a file
#
# @return [GPGME::Data] the exported key.
#
# @example
# key = GPGME.export "mrsimo@example.com"
#
# @example writing to a file
# out = File.open("my.key", "w+")
# GPGME.export "mrsimo@example.com", :output => out
#
def export(pattern, options = {})
check_version(options)
output = Data.new(options[:output])
GPGME::Ctx.new(options) do |ctx|
ctx.export_keys(pattern, output)
end
output.seek(0)
output
end
# Imports a key
#
# GPGME.import keydata, options
#
# @param keydata
# The key to import. It will be converted to a {GPGME::Data} object,
# so could be a file for example.
#
# @example
# GPGME.import(File.open("my.key"))
#
def import(keydata, options = {})
check_version(options)
GPGME::Ctx.new(options) do |ctx|
ctx.import_keys(Data.new(keydata))
ctx.import_result
end
end
##
# Verify that the engine implementing the protocol +proto+ is installed in
# the system. Can be one of +PROTOCOL_OpenPGP+ or +PROTOCOL_CMS+.

Просмотреть файл

@ -11,6 +11,8 @@ module GPGME
include KeyCommon
class << self
##
# Returns an array of {GPGME::Key} objects that match the parameters.
# * +secret+ set to +:secret+ to get only secret keys, or to +:public+ to
@ -32,7 +34,7 @@ module GPGME
# GPGME::Key.find(:public, "mrsimo@example.com", :sign)
# # => return the public keys that match mrsimo@exampl.com and are
# # capable of signing
def self.find(secret, keys_or_names = nil, purposes = [])
def find(secret, keys_or_names = nil, purposes = [])
secret = (secret == :secret)
keys_or_names = [""] if keys_or_names.nil? || keys_or_names.empty?
keys_or_names = [keys_or_names].flatten
@ -53,6 +55,77 @@ module GPGME
keys
end
# Exports a key
#
# GPGME::Key.export pattern, options
#
# @param pattern
# Identifier of the key to export.
#
# @param [Hash] options
# * +:output+ specify where to write the key to. It will be converted to
# a {GPGME::Data}, so it could be a file, for example.
# * Any other option accepted by {GPGME::Ctx.new}
#
# @return [GPGME::Data] the exported key.
#
# @example
# key = GPGME::Key.export "mrsimo@example.com"
#
# @example writing to a file
# out = File.open("my.key", "w+")
# GPGME::Key.export "mrsimo@example.com", :output => out
#
def export(pattern, options = {})
output = Data.new(options[:output])
GPGME::Ctx.new(options) do |ctx|
ctx.export_keys(pattern, output)
end
output.seek(0)
output
end
# Imports a key
#
# GPGME::Key.import keydata, options
#
# @param keydata
# The key to import. It will be converted to a {GPGME::Data} object,
# so could be a file, for example.
# @param options
# Any other option accepted by {GPGME::Ctx.new}
#
# @example
# GPGME::Key.import(File.open("my.key"))
#
def import(keydata, options = {})
GPGME::Ctx.new(options) do |ctx|
ctx.import_keys(Data.new(keydata))
ctx.import_result
end
end
end
##
# Exports this key. Accepts the same options as {GPGME::Ctx.new}, and
# +options[:output]+, where you can specify something that can become a
# {GPGME::Data}, where the output will go.
#
# @example
# key.export(:armor => true)
# # => GPGME::Data you can read with ASCII armored format
#
# @example
# file = File.open("key.asc", "w+")
# key.export(:output => file)
# # => the key will be written to the file.
#
def export(options = {})
Key.export self, options
end
##
# Delete this key. If it's public, and has a secret one it will fail unless
# +allow_secret+ is specified as true.

Просмотреть файл

@ -239,7 +239,7 @@ RUBY
GPGME::Key.find(:public).each{|k| k.delete!(true)}
assert_equal 0, GPGME::Key.find(:public).size
result = GPGME.import(export)
result = GPGME::Key.import(export)
current_keys = GPGME::Key.find(:public)
assert_equal original_keys.size, current_keys.size
assert_equal result.imports.size, current_keys.size
@ -261,7 +261,7 @@ RUBY
key.delete!(true)
result = GPGME.import(export)
result = GPGME::Key.import(export)
assert_equal 1, result.imports.size
import = result.imports.first

Просмотреть файл

@ -47,6 +47,58 @@ describe GPGME::Key do
end
end
describe :export do
# Testing the lazy way with expectations. I think tests in
# the Ctx class are enough.
it "exports any key that matches the pattern" do
GPGME::Ctx.any_instance.expects(:export_keys).with("", anything)
GPGME::Key.export("")
end
it "exports any key that matches the pattern, can specify output" do
data = GPGME::Data.new
GPGME::Ctx.any_instance.expects(:export_keys).with("wadus", data)
ret = GPGME::Key.export("wadus", :output => data)
assert_equal data, ret
end
it "can specify options for Ctx" do
GPGME::Ctx.expects(:new).with(:armor => true).yields(mock(:export_keys => true))
GPGME::Key.export("wadus", :armor => true)
end
end
describe "#export" do
it "can export from the key instance" do
key = GPGME::Key.find(:public).first
GPGME::Key.expects(:export).with(key, {})
key.export
end
it "can export from the key instance passing variables" do
key = GPGME::Key.find(:public).first
GPGME::Key.expects(:export).with(key, {:armor => true})
key.export :armor => true
end
end
describe :import do
it "can import keys" do
data = GPGME::Data.new
GPGME::Ctx.any_instance.expects(:import_keys).with(data)
GPGME::Ctx.any_instance.expects(:import_result).returns("wadus")
assert_equal "wadus", GPGME::Key.import(data)
end
it "can specify options for Ctx" do
GPGME::Ctx.expects(:new).with(:armor => true).yields(mock(:import_keys => true, :import_result => true))
GPGME::Key.import("wadus", :armor => true)
end
end
# describe :trust do
# it "returns :revoked if it is so"
# it "returns :expired if it is expired"

Просмотреть файл

@ -16,8 +16,8 @@ def import_keys
end
def import_key(key)
GPGME.import key[:public]
GPGME.import key[:private]
GPGME::Key.import key[:public]
GPGME::Key.import key[:private]
end
def remove_keys