2016-08-10 04:50:59 +01:00
|
|
|
require "utils"
|
|
|
|
|
|
|
|
class Gpg
|
2017-12-23 16:52:50 +00:00
|
|
|
module_function
|
2016-08-10 04:50:59 +01:00
|
|
|
|
2017-12-23 16:52:50 +00:00
|
|
|
def executable
|
|
|
|
which "gpg"
|
2016-08-10 16:46:58 +01:00
|
|
|
end
|
|
|
|
|
2017-12-23 16:52:50 +00:00
|
|
|
def available?
|
2017-10-22 02:48:38 +01:00
|
|
|
File.executable?(executable.to_s)
|
2016-08-10 04:50:59 +01:00
|
|
|
end
|
|
|
|
|
2017-12-23 16:52:50 +00:00
|
|
|
def create_test_key(path)
|
2016-08-10 04:50:59 +01:00
|
|
|
odie "No GPG present to test against!" unless available?
|
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
(path/"batch.gpg").write <<~EOS
|
2016-08-10 04:50:59 +01:00
|
|
|
Key-Type: RSA
|
|
|
|
Key-Length: 2048
|
|
|
|
Subkey-Type: RSA
|
|
|
|
Subkey-Length: 2048
|
|
|
|
Name-Real: Testing
|
|
|
|
Name-Email: testing@foo.bar
|
|
|
|
Expire-Date: 1d
|
2017-08-28 16:51:58 +01:00
|
|
|
%no-protection
|
2016-08-10 04:50:59 +01:00
|
|
|
%commit
|
|
|
|
EOS
|
2017-10-22 02:48:38 +01:00
|
|
|
system executable, "--batch", "--gen-key", "batch.gpg"
|
2016-08-10 04:50:59 +01:00
|
|
|
end
|
2017-08-28 16:51:58 +01:00
|
|
|
|
2017-12-23 16:52:50 +00:00
|
|
|
def cleanup_test_processes!
|
2017-08-28 16:51:58 +01:00
|
|
|
odie "No GPG present to test against!" unless available?
|
2017-12-23 16:52:50 +00:00
|
|
|
|
2017-10-22 02:48:38 +01:00
|
|
|
gpgconf = Pathname.new(executable).parent/"gpgconf"
|
2017-08-28 16:51:58 +01:00
|
|
|
|
|
|
|
system gpgconf, "--kill", "gpg-agent"
|
|
|
|
system gpgconf, "--homedir", "keyrings/live", "--kill",
|
|
|
|
"gpg-agent"
|
|
|
|
end
|
2017-09-01 01:50:26 +01:00
|
|
|
|
2017-12-23 16:52:50 +00:00
|
|
|
def test(path)
|
2017-09-01 01:50:26 +01:00
|
|
|
create_test_key(path)
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
cleanup_test_processes!
|
|
|
|
end
|
|
|
|
end
|
2016-08-10 04:50:59 +01:00
|
|
|
end
|