Improved general test coverage.

This commit is contained in:
Albert Llop 2011-05-02 00:49:33 +02:00
Родитель eb1e6f4112
Коммит d36f9f3296
6 изменённых файлов: 125 добавлений и 19 удалений

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

@ -36,7 +36,7 @@ module GPGME
# # capable of signing
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 = [""] if keys_or_names.nil? || (keys_or_names.is_a?(Array) && keys_or_names.empty?)
keys_or_names = [keys_or_names].flatten
purposes = [purposes].flatten.compact.uniq

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

@ -13,17 +13,38 @@ module GPGME
status_code == GPGME::GPG_ERR_NO_ERROR
end
def expired_signature?
status_code == GPGME::GPG_ERR_SIG_EXPIRED
end
def expired_key?
status_code == GPGME::GPG_ERR_KEY_EXPIRED
end
def revoked_key?
status_code == GPGME::GPG_ERR_CERT_REVOKED
end
def bad?
status_code == GPGME::GPG_ERR_BAD_SIGNATURE
end
def no_key?
status_code == GPGME::GPG_ERR_NO_PUBKEY
end
def status_code
GPGME::gpgme_err_code(status)
end
def from
@from ||= begin
ctx = Ctx.new
if from_key = ctx.get_key(fingerprint)
"#{from_key.subkeys[0].keyid} #{from_key.uids[0].uid}"
else
fingerprint
Ctx.new do
if from_key = ctx.get_key(fingerprint)
"#{from_key.subkeys[0].keyid} #{from_key.uids[0].uid}"
else
fingerprint
end
end
end
end
@ -48,7 +69,7 @@ module GPGME
"Signature made from revoked key #{from}"
when GPGME::GPG_ERR_BAD_SIGNATURE
"Bad signature from #{from}"
when GPGME::GPG_ERR_NO_ERROR
when GPGME::GPG_ERR_NO_PUBKEY
"No public key for #{from}"
end
end

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

@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-
require 'test_helper'
require 'tempfile'
describe GPGME::Crypto do
describe "default options functionality" do
@ -43,12 +44,83 @@ describe GPGME::Crypto do
end
end
# it "can specify which key(s) to use for encrypting with a string"
# it "can specify which key to use for encrypting with a Key object"
# it "can also sign at the same time"
# it "can be signed by more than one person"
# it "outputs to a file if specified"
# it "outputs to something else that responds to write"
it "doesn't raise an error and returns something when encrypting nothing" do
data = GPGME::Crypto.new.encrypt nil, :always_trust => true
refute_empty data.read
data = GPGME::Crypto.new.encrypt "", :always_trust => true
refute_empty data.read
end
it "can specify which key(s) to use for encrypting with a string" do
crypto = GPGME::Crypto.new :always_trust => true
key = KEYS.last
encrypted = crypto.encrypt TEXT[:plain], :recipients => key[:sha]
assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
remove_key key
encrypted.seek 0
assert_raises GPGME::Error::DecryptFailed do
crypto.decrypt(encrypted)
end
import_key key
end
it "can specify which key to use for encrypting with a Key object" do
crypto = GPGME::Crypto.new :always_trust => true
key = KEYS.last
real_key = GPGME::Key.find(:public, key[:sha]).first
encrypted = crypto.encrypt TEXT[:plain], :recipients => real_key
assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
remove_key key
encrypted.seek 0
assert_raises GPGME::Error::DecryptFailed do
crypto.decrypt(encrypted)
end
import_key key
end
it "can also sign at the same time" do
crypto = GPGME::Crypto.new :always_trust => true
encrypted = crypto.encrypt TEXT[:plain], :sign => true
signatures = 0
crypto.verify(encrypted) do |signature|
assert_instance_of GPGME::Signature, signature
signatures += 1
end
assert_equal 1, signatures
end
it "can be signed by more than one person" do
crypto = GPGME::Crypto.new :always_trust => true
encrypted = crypto.encrypt TEXT[:plain], :sign => true, :signers => KEYS.map{|k| k[:sha]}
signatures = 0
crypto.verify(encrypted) do |signature|
assert_instance_of GPGME::Signature, signature
signatures += 1
end
assert_equal 4, signatures
end
it "outputs to a file if specified" do
crypto = GPGME::Crypto.new :always_trust => true
file = Tempfile.new "test"
crypto.encrypt TEXT[:plain], :output => file
file_contents = file.read
file.seek 0
refute_empty file_contents
assert_equal TEXT[:plain], crypto.decrypt(file).read
end
# TODO find how to test
# it "raises GPGME::Error::UnusablePublicKey"
# it "raises GPGME::Error::UnusableSecretKey"
end
describe "symmetric encryption/decryption" do
@ -133,6 +205,7 @@ describe GPGME::Crypto do
crypto.verify(sign) do |signature|
assert_instance_of GPGME::Signature, signature
assert signature.valid?
signatures += 1
end

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

@ -27,7 +27,7 @@ describe GPGME::Ctx do
# with the other tests of this file. Here we test the rest
it ":password sets the password for the key" do
with_password_key do
with_key PASSWORD_KEY do
input = GPGME::Data.new(TEXT[:passwored])
output = GPGME::Data.new
@ -62,7 +62,7 @@ describe GPGME::Ctx do
io.flush
end
with_password_key do
with_key PASSWORD_KEY do
input = GPGME::Data.new(TEXT[:passwored])
output = GPGME::Data.new
@var = 0
@ -95,7 +95,7 @@ describe GPGME::Ctx do
io.flush
end
with_password_key do
with_key PASSWORD_KEY do
input = GPGME::Data.new(TEXT[:passwored])
output = GPGME::Data.new

12
test/signature_test.rb Normal file
Просмотреть файл

@ -0,0 +1,12 @@
# -*- encoding: utf-8 -*-
require 'test_helper'
describe GPGME::Signature do
# TODO Find how to test these
# it "#valid? is true when the signature is valid"
# it "#expired_signature? is true when the signature has expired"
# it "#expired_key? is true when the key has expired"
# it "#revoked_key? is true when the key has been revoked"
# it "#bad? is true when the signature is bad"
# it "#no_key? is true when we don't have the key to verify the signature"
end

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

@ -55,13 +55,13 @@ def with_keys(size, &block)
end
end
def with_password_key(&block)
import_key PASSWORD_KEY
def with_key(key, &block)
import_key key
begin
yield
ensure
remove_key PASSWORD_KEY
remove_key key
end
end