ruby-pgp 0.0.1

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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.rbenv-version +1 -0
  4. data/.rspec +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +14 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +82 -0
  10. data/Rakefile +12 -0
  11. data/bin/jrpgp +12 -0
  12. data/bin/rpgp +12 -0
  13. data/lib/pgp/cli/runner.rb +78 -0
  14. data/lib/pgp/cli.rb +160 -0
  15. data/lib/pgp/decryptor.rb +16 -0
  16. data/lib/pgp/encryptor.rb +33 -0
  17. data/lib/pgp/gpg/engine.rb +141 -0
  18. data/lib/pgp/gpg/runner.rb +189 -0
  19. data/lib/pgp/gpg/temp_path_helper.rb +25 -0
  20. data/lib/pgp/keys_importer.rb +11 -0
  21. data/lib/pgp/log.rb +28 -0
  22. data/lib/pgp/ruby_decryptor.rb +9 -0
  23. data/lib/pgp/signer.rb +16 -0
  24. data/lib/pgp/verifier.rb +14 -0
  25. data/lib/pgp/version.rb +3 -0
  26. data/lib/pgp.rb +20 -0
  27. data/lib/ruby-pgp.rb +1 -0
  28. data/ruby-pgp.gemspec +24 -0
  29. data/run_tests.sh +7 -0
  30. data/spec/helpers/keys_helper.rb +9 -0
  31. data/spec/helpers/process_helper.rb +13 -0
  32. data/spec/helpers/temp_helper.rb +28 -0
  33. data/spec/lib/pgp/cli_spec.rb +41 -0
  34. data/spec/lib/pgp/decryptor_spec.rb +58 -0
  35. data/spec/lib/pgp/encryptor_spec.rb +86 -0
  36. data/spec/lib/pgp/gpg/engine_spec.rb +276 -0
  37. data/spec/lib/pgp/gpg/runner_integration_spec.rb +38 -0
  38. data/spec/lib/pgp/gpg/runner_spec.rb +432 -0
  39. data/spec/lib/pgp/gpg/temp_path_helper_spec.rb +53 -0
  40. data/spec/lib/pgp/signer_spec.rb +48 -0
  41. data/spec/lib/pgp/verifier_spec.rb +40 -0
  42. data/spec/lib/quirks_spec.rb +102 -0
  43. data/spec/spec_helper.rb +27 -0
  44. data/spec/support/fixtures/encrypted_with_passphrase_key.txt +1 -0
  45. data/spec/support/fixtures/encrypted_with_passphrase_key.txt.asc +13 -0
  46. data/spec/support/fixtures/private_key.asc +63 -0
  47. data/spec/support/fixtures/private_key_with_passphrase.asc +59 -0
  48. data/spec/support/fixtures/public_key.asc +36 -0
  49. data/spec/support/fixtures/public_key_with_passphrase.asc +30 -0
  50. data/spec/support/fixtures/signed_file.txt +1 -0
  51. data/spec/support/fixtures/signed_file.txt.asc +14 -0
  52. data/spec/support/fixtures/unencrypted_file.txt +1 -0
  53. data/spec/support/fixtures/unencrypted_file.txt.asc +19 -0
  54. data/spec/support/fixtures/wrong_private_key_for_signature.asc +63 -0
  55. data/spec/support/fixtures/wrong_public_key_for_signature.asc +36 -0
  56. data/spec/verify.sh +124 -0
  57. metadata +155 -0
@@ -0,0 +1,276 @@
1
+ require 'spec_helper'
2
+
3
+ describe GPG::Engine do
4
+ include TempHelper
5
+
6
+ let(:engine) { GPG::Engine.new }
7
+ let(:runner) { engine.runner }
8
+
9
+ def setup_valid_gpg_version
10
+ allow(runner).to receive(:version_default).and_return('2.0.4')
11
+ end
12
+
13
+ def setup_invalid_gpg_version
14
+ allow(runner).to receive(:version_default).and_return('1.9.14')
15
+ end
16
+
17
+ describe :delete_all_private_keys do
18
+ it 'deletes all the private keys' do
19
+ setup_valid_gpg_version
20
+ allow(runner).to receive(:read_private_key_fingerprints).and_return(['fp1', 'fp2'])
21
+ allow(runner).to receive(:delete_private_key)
22
+
23
+ engine.delete_all_private_keys
24
+
25
+ expect(runner).to have_received(:delete_private_key).with('fp1')
26
+ expect(runner).to have_received(:delete_private_key).with('fp2')
27
+ end
28
+
29
+ it 'fails when gpg is not correctly installed' do
30
+ setup_invalid_gpg_version
31
+
32
+ expect{
33
+ engine.delete_all_private_keys
34
+ }.to raise_exception('GPG Version is incorrect')
35
+ end
36
+ end
37
+
38
+ describe :delete_all_public_keys do
39
+ it 'deletes all the public keys' do
40
+ setup_valid_gpg_version
41
+ allow(runner).to receive(:read_public_key_fingerprints).and_return(['fp1', 'fp2'])
42
+ allow(runner).to receive(:delete_public_key)
43
+
44
+ engine.delete_all_public_keys
45
+
46
+ expect(runner).to have_received(:delete_public_key).with('fp1')
47
+ expect(runner).to have_received(:delete_public_key).with('fp2')
48
+ end
49
+
50
+ it 'fails when gpg is not correctly installed' do
51
+ setup_invalid_gpg_version
52
+
53
+ expect{
54
+ engine.delete_all_public_keys
55
+ }.to raise_exception('GPG Version is incorrect')
56
+ end
57
+ end
58
+
59
+ describe :delete_all_keys do
60
+ it 'deletes all private and public keys' do
61
+ deleted_fingerprints = []
62
+
63
+ allow(runner).to receive(:read_private_key_fingerprints).and_return(['privfp1', 'privfp2'])
64
+ allow(runner).to receive(:delete_private_key) { |k| deleted_fingerprints << k }
65
+
66
+ allow(runner).to receive(:read_public_key_fingerprints).and_return(['pubfp1', 'pubfp2'])
67
+ allow(runner).to receive(:delete_public_key) { |k| deleted_fingerprints << k }
68
+
69
+ engine.delete_all_keys
70
+
71
+ expect(deleted_fingerprints).to eq(['privfp1', 'privfp2', 'pubfp1', 'pubfp2'])
72
+ end
73
+ end
74
+
75
+ describe :import_key do
76
+ it 'creates a temporary file and imports the key' do
77
+ setup_valid_gpg_version
78
+ temp_file_stub = setup_temp_file('key contents aaaaa')
79
+ allow(runner).to receive(:import_key_from_file).with(temp_file_stub.path).and_return([
80
+ 'email1@gmail.com',
81
+ 'email2@gmail.com'
82
+ ])
83
+
84
+ expect(engine.import_key('key contents aaaaa')).to eq(['email1@gmail.com', 'email2@gmail.com'])
85
+
86
+ expect(temp_file_stub).to have_received(:write)
87
+ expect(temp_file_stub).to have_received(:rewind)
88
+ expect(runner).to have_received(:import_key_from_file)
89
+ end
90
+
91
+ it 'fails when gpg is not correctly installed' do
92
+ setup_invalid_gpg_version
93
+
94
+ expect{
95
+ engine.import_key('aaaaaaaaaa')
96
+ }.to raise_exception('GPG Version is incorrect')
97
+ end
98
+ end
99
+
100
+ describe :verify_signature do
101
+ it 'verifies the signature using the pgp runner' do
102
+ setup_valid_gpg_version
103
+ setup_temp_paths(['path2', 'path1'])
104
+ allow(File).to receive(:write).with('path1', 'signature contents')
105
+ allow(File).to receive(:read).with('path2').and_return('secret plain')
106
+ allow(runner).to receive(:verify_signature_file)
107
+ .with('path1', 'path2')
108
+ .and_return(true)
109
+
110
+ expect(engine.verify_signature('signature contents')).to eq([true, 'secret plain'])
111
+
112
+ expect(runner).to have_received(:verify_signature_file)
113
+ expect(File).to have_received(:write)
114
+ expect(File).to have_received(:read)
115
+ end
116
+
117
+ it 'returns no data when verification failed' do
118
+ setup_valid_gpg_version
119
+ setup_temp_paths(['path2', 'path1'])
120
+ allow(File).to receive(:write)
121
+ allow(runner).to receive(:verify_signature_file).and_return(false)
122
+
123
+ expect(engine.verify_signature('signature contents')).to eq([false, ''])
124
+ end
125
+
126
+ it 'fails when gpg is not correctly installed' do
127
+ setup_invalid_gpg_version
128
+
129
+ expect{
130
+ engine.verify_signature('aaaaaaaaaa')
131
+ }.to raise_exception('GPG Version is incorrect')
132
+ end
133
+ end
134
+
135
+ describe :decrypt do
136
+ it 'decrypts the data with a passphrase' do
137
+ setup_valid_gpg_version
138
+ setup_temp_paths(['path2', 'path1'])
139
+ allow(File).to receive(:write).with('path1', 'encrypted message')
140
+ allow(File).to receive(:read).with('path2').and_return('the answer is 42')
141
+ allow(runner).to receive(:decrypt_file)
142
+ .with('path1', 'path2', 'supersecret')
143
+ .and_return(true)
144
+
145
+ expect(engine.decrypt('encrypted message', 'supersecret')).to eq([true, 'the answer is 42'])
146
+
147
+ expect(runner).to have_received(:decrypt_file)
148
+ expect(File).to have_received(:write)
149
+ expect(File).to have_received(:read)
150
+ end
151
+
152
+ it 'returns no data when decryption failed' do
153
+ setup_valid_gpg_version
154
+ setup_temp_paths(['path2', 'path1'])
155
+ allow(File).to receive(:write)
156
+ allow(runner).to receive(:decrypt_file).and_return(false)
157
+
158
+ expect(engine.decrypt('encrypted text')).to eq([false, ''])
159
+ end
160
+
161
+ it 'fails when gpg is not correctly installed' do
162
+ setup_invalid_gpg_version
163
+
164
+ expect{
165
+ engine.decrypt('aaaaaaaaaa')
166
+ }.to raise_exception('GPG Version is incorrect')
167
+ end
168
+ end
169
+
170
+ describe :encrypt do
171
+ it 'encrypts the data' do
172
+ setup_valid_gpg_version
173
+ setup_temp_paths(['path2', 'path1'])
174
+ allow(File).to receive(:write).with('path1', 'plain text message')
175
+ allow(File).to receive(:read).with('path2').and_return('encrypted text')
176
+ allow(runner).to receive(:encrypt_file)
177
+ .with('path1', 'path2', ['email1@gmail.com', 'email2@gmail.com'])
178
+ .and_return(true)
179
+
180
+ expect(engine.encrypt('plain text message', ['email1@gmail.com', 'email2@gmail.com'])).to eq([true, 'encrypted text'])
181
+
182
+ expect(runner).to have_received(:encrypt_file)
183
+ expect(File).to have_received(:write)
184
+ expect(File).to have_received(:read)
185
+ end
186
+
187
+ it 'fails when gpg is not correctly installed' do
188
+ setup_invalid_gpg_version
189
+
190
+ expect{
191
+ engine.encrypt('some text', ['aaa@gmail.com'])
192
+ }.to raise_exception('GPG Version is incorrect')
193
+ end
194
+
195
+ it 'raises an error when the recipient parameters are empty' do
196
+ expect {
197
+ engine.encrypt('some text', [])
198
+ }.to raise_exception 'Recipients cannot be empty'
199
+ end
200
+
201
+ it 'returns no data when encryption failed' do
202
+ setup_valid_gpg_version
203
+ setup_temp_paths(['path2', 'path1'])
204
+ allow(File).to receive(:write)
205
+ allow(runner).to receive(:encrypt_file).and_return(false)
206
+
207
+ expect(engine.encrypt('some text', ['aaa@gmail.com'])).to eq([false, ''])
208
+ end
209
+ end
210
+
211
+ describe :sign do
212
+ it 'signs the data with a passphrase' do
213
+ setup_valid_gpg_version
214
+ setup_temp_paths(['path2', 'path1'])
215
+ allow(File).to receive(:write).with('path1', 'plain text message')
216
+ allow(File).to receive(:read).with('path2').and_return('encrypted signature')
217
+ allow(runner).to receive(:sign_file)
218
+ .with('path1', 'path2', 'supersecret')
219
+ .and_return(true)
220
+
221
+ expect(engine.sign('plain text message', 'supersecret')).to eq([true, 'encrypted signature'])
222
+
223
+ expect(runner).to have_received(:sign_file)
224
+ expect(File).to have_received(:write)
225
+ expect(File).to have_received(:read)
226
+ end
227
+
228
+ it 'returns no data when signing failed' do
229
+ setup_valid_gpg_version
230
+ setup_temp_paths(['path2', 'path1'])
231
+ allow(File).to receive(:write)
232
+ allow(runner).to receive(:sign_file).and_return(false)
233
+
234
+ expect(engine.sign('something')).to eq([false, ''])
235
+ end
236
+
237
+ it 'fails when gpg is not correctly installed' do
238
+ setup_invalid_gpg_version
239
+
240
+ expect{
241
+ engine.sign('aaaaaaaaaa')
242
+ }.to raise_exception('GPG Version is incorrect')
243
+ end
244
+ end
245
+
246
+ describe :read_recipients do
247
+ it 'merges the private and public recipients' do
248
+ setup_valid_gpg_version
249
+ allow(runner).to receive(:read_public_key_recipients).and_return([
250
+ 'email1@gmail.com',
251
+ 'email1@gmail.com',
252
+ 'email2@gmail.com',
253
+ 'email3@gmail.com'
254
+ ])
255
+ allow(runner).to receive(:read_private_key_recipients).and_return([
256
+ 'email3@gmail.com',
257
+ 'email4@gmail.com'
258
+ ])
259
+
260
+ expect(engine.read_recipients).to eq([
261
+ 'email1@gmail.com',
262
+ 'email2@gmail.com',
263
+ 'email3@gmail.com',
264
+ 'email4@gmail.com'
265
+ ])
266
+ end
267
+
268
+ it 'fails when gpg is not correctly installed' do
269
+ setup_invalid_gpg_version
270
+
271
+ expect{
272
+ engine.read_recipients
273
+ }.to raise_exception('GPG Version is incorrect')
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe GPG::Runner do
4
+ include KeysHelper
5
+
6
+ let(:runner) { GPG::Runner.new }
7
+
8
+ before {
9
+ PGP::Log.verbose = true
10
+ remove_all_keys
11
+ }
12
+
13
+ after {
14
+ remove_all_keys
15
+ PGP::Log.verbose = false
16
+ }
17
+
18
+ it 'has no keys by default' do
19
+ expect(runner.read_private_key_fingerprints).to eq([])
20
+ expect(runner.read_public_key_fingerprints).to eq([])
21
+ end
22
+
23
+ it 'uses GPG version 2' do
24
+ expect(runner.version_default).to include('2.')
25
+ end
26
+
27
+ it 'imports private keys' do
28
+ runner.import_key_from_file(Fixtures_Path.join('private_key.asc'))
29
+
30
+ expect(runner.read_private_key_fingerprints).to eq(['A99BFCC3B6B952D66AFC1F3C48508D311DD34131'])
31
+ end
32
+
33
+ it 'imports public keys' do
34
+ runner.import_key_from_file(Fixtures_Path.join('public_key.asc'))
35
+
36
+ expect(runner.read_public_key_fingerprints).to eq(['A99BFCC3B6B952D66AFC1F3C48508D311DD34131'])
37
+ end
38
+ end