ruby_gpg 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,7 @@
1
+ # v0.2.0 (2010-03-17)
2
+
3
+ * Add `RubyGpg.decrypt_string` method.
4
+
1
5
  # v0.1.1 (2010-03-16)
2
6
 
3
7
  * Improve error handling.
data/Rakefile CHANGED
@@ -27,6 +27,14 @@ Spec::Rake::SpecTask.new(:spec) do |spec|
27
27
  spec.spec_files = FileList['spec/**/*_spec.rb']
28
28
  end
29
29
 
30
+ namespace :spec do
31
+ Spec::Rake::SpecTask.new(:doc) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.spec_files = FileList['spec/**/*_spec.rb']
34
+ spec.spec_opts << '--format specdoc'
35
+ end
36
+ end
37
+
30
38
  Spec::Rake::SpecTask.new(:rcov) do |spec|
31
39
  spec.libs << 'lib' << 'spec'
32
40
  spec.pattern = 'spec/**/*_spec.rb'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -0,0 +1,13 @@
1
+ Feature: Decrypt String
2
+ In order to read an encrypted string
3
+ As a user
4
+ I want to decrypt it
5
+
6
+ Scenario: Decrypt with proper recipient/recipient
7
+ Given a key pair for Slow Joe Crow with passphrase test
8
+ And a file named "secrets" containing "some content"
9
+ And I encrypt the file "secrets" for "Slow Joe Crow"
10
+ And I read the file "secrets.gpg" to a string
11
+ And the string should not be "secrets"
12
+ When I decrypt the string with passphrase "test"
13
+ Then the string should be "secrets"
@@ -15,6 +15,14 @@ Given /^the file "([^\"]*)" does not exist$/ do |filename|
15
15
  end
16
16
  end
17
17
 
18
+ Given /^I read the file "([^\"]*)" to a string$/ do |filename|
19
+ @string = File.read("#{TMP_PATH}/#{filename}")
20
+ end
21
+
22
+ Given /^the string should not be "([^\"]*)"$/ do |string|
23
+ @string.strip.should_not == string.strip
24
+ end
25
+
18
26
  When /^I encrypt the file "([^\"]*)" for "([^\"]*)"$/ do |filename, recipient|
19
27
  RubyGpg.encrypt("#{TMP_PATH}/#{filename}", recipient)
20
28
  end
@@ -29,6 +37,10 @@ When /^I decrypt the file "([^\"]*)" with passphrase "([^\"]*)"$/ do |filename,
29
37
  RubyGpg.decrypt("#{TMP_PATH}/#{filename}", passphrase)
30
38
  end
31
39
 
40
+ When /^I decrypt the string with passphrase "([^\"]*)"$/ do |passphrase|
41
+ @string = RubyGpg.decrypt_string(@string, passphrase)
42
+ end
43
+
32
44
  Then /^the command should raise an error matching "([^\"]*)"$/ do |error|
33
45
  @command.should raise_error(/#{Regexp.escape(error)}/)
34
46
  end
@@ -47,4 +59,8 @@ end
47
59
 
48
60
  Then /^the file "([^\"]*)" should contain "([^\"]*)"$/ do |filename, content|
49
61
  File.read("#{TMP_PATH}/#{filename}").should include(content)
50
- end
62
+ end
63
+
64
+ Then /^the string should be "([^\"]*)"$/ do |string|
65
+ string.strip.should == string.strip
66
+ end
data/lib/ruby_gpg.rb CHANGED
@@ -27,13 +27,25 @@ module RubyGpg
27
27
  run_command(command)
28
28
  end
29
29
 
30
+ def decrypt_string(string, passphrase = nil)
31
+ command = gpg_command.dup
32
+ command << " --passphrase #{passphrase}" if passphrase
33
+ command << " --decrypt"
34
+ run_command(command, string)
35
+ end
36
+
30
37
  private
31
- def run_command(command)
38
+ def run_command(command, input = nil)
39
+ output = ""
32
40
  Open3.popen3(command) do |stdin, stdout, stderr|
41
+ stdin.write(input) if input
42
+ stdin.close_write
43
+ output << stdout.read
33
44
  error = stderr.read
34
45
  if error && !error.empty?
35
46
  raise "GPG command (#{command}) failed with: #{error}"
36
47
  end
37
48
  end
49
+ output
38
50
  end
39
51
  end
data/ruby_gpg.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby_gpg}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Justin Blake"]
12
- s.date = %q{2010-03-16}
12
+ s.date = %q{2010-03-17}
13
13
  s.description = %q{Ruby wrapper for the gpg binary}
14
14
  s.email = %q{justin@megablaix.com}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "cucumber.yml",
28
+ "features/decrypt_string.feature",
28
29
  "features/decryption.feature",
29
30
  "features/encryption.feature",
30
31
  "features/step_definitions/ruby_gpg_steps.rb",
@@ -121,4 +121,45 @@ describe "RubyGpg" do
121
121
  lambda { run_decrypt }.should_not raise_error
122
122
  end
123
123
  end
124
+
125
+ describe '.decrypt_string(string)' do
126
+ def run_decrypt_string(passphrase = nil)
127
+ RubyGpg.decrypt_string('encrypted string', passphrase)
128
+ end
129
+
130
+ it "uses the configured gpg command" do
131
+ expect_command_to_match(/^#{Regexp.escape(RubyGpg.gpg_command)}/)
132
+ run_decrypt_string
133
+ end
134
+
135
+ it "issues a decrypt command to gpg" do
136
+ expect_command_to_match("--decrypt")
137
+ run_decrypt_string
138
+ end
139
+
140
+ it "accepts an optional passphrase" do
141
+ expect_command_to_match("--passphrase secret")
142
+ run_decrypt_string("secret")
143
+ end
144
+
145
+ it "sends the passed string as stdin" do
146
+ @stdin.expects(:write).with("encrypted string")
147
+ run_decrypt_string
148
+ end
149
+
150
+ it "returns the decrypted string" do
151
+ @stdout.write("decrypted string")
152
+ @stdout.rewind
153
+ run_decrypt_string.should == "decrypted string"
154
+ end
155
+
156
+ it "raises any errors from gpg" do
157
+ stub_error("error message")
158
+ lambda { run_decrypt_string }.should raise_error(/GPG command \(.*gpg.*--decrypt.*\) failed with: error message/)
159
+ end
160
+
161
+ it "does not raise if there is no output from gpg" do
162
+ lambda { run_decrypt_string }.should_not raise_error
163
+ end
164
+ end
124
165
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Justin Blake
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-16 00:00:00 -04:00
17
+ date: 2010-03-17 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -87,6 +87,7 @@ files:
87
87
  - Rakefile
88
88
  - VERSION
89
89
  - cucumber.yml
90
+ - features/decrypt_string.feature
90
91
  - features/decryption.feature
91
92
  - features/encryption.feature
92
93
  - features/step_definitions/ruby_gpg_steps.rb