Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider merging exact key feature #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/gpgme/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ def find(secret, keys_or_names = nil, purposes = [])
keys
end

# Works similar as {.find}, however it restricts the way the keys are looked up.
# GPG has the issue that finding a key for bar@example.com, also returns a key
# for foobar@example.com.
# This can be restricted by adding <> around the address: <bar@example.com>.
# Hence {.find_exact} simply wraps <> around each email you passed to the method and delegates
# the rest to {.find}
#
# @example
# GPGME::Key.find_exact(:public, "bar@example.com")
# # => return the public key of bar@example.com, but not for
# # foobar@example.com
def find_exact(secret, keys_or_names = nil, purposes = [])
keys_or_names = [""] if keys_or_names.nil? || (keys_or_names.is_a?(Array) && keys_or_names.empty?)
find(
secret,
[keys_or_names].flatten.collect{|k| if k =~ /.*@.*/ && !(k =~ /<.*@.*>/) then "<#{k}>" else k end },
purposes
)
end

def get(fingerprint)
Ctx.new do |ctx|
ctx.get_key(fingerprint)
Expand Down
22 changes: 22 additions & 0 deletions test/key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@
assert keys.empty?
end
end

describe :find_exact do
it "wraps an email address with angle brackets" do
GPGME::Key.expects(:find).with(:public,['<bar@example.com>'],[])
GPGME::Key.find_exact(:public,'bar@example.com')
end

it "wraps multiple email addresses with angle brackets" do
GPGME::Key.expects(:find).with(:public,['<bar@example.com>','<foo@example.com>'],[])
GPGME::Key.find_exact(:public,['bar@example.com','foo@example.com'])
end

it "does not touch other strings than email addresses" do
GPGME::Key.expects(:find).with(:public,['Bar Example'],[])
GPGME::Key.find_exact(:public,'Bar Example')
end

it "does the same in mixed mode" do
GPGME::Key.expects(:find).with(:public,['<bar@example.com>','Foo Example'],[])
GPGME::Key.find_exact(:public,['bar@example.com','Foo Example'])
end
end

describe :export do
# Testing the lazy way with expectations. I think tests in
Expand Down