gpgme 1.0.8 → 2.0.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.
Files changed (49) hide show
  1. data/examples/genkey.rb +1 -1
  2. data/examples/keylist.rb +2 -1
  3. data/examples/roundtrip.rb +7 -4
  4. data/examples/sign.rb +5 -3
  5. data/examples/verify.rb +4 -2
  6. data/ext/gpgme/extconf.rb +58 -0
  7. data/ext/gpgme/gpgme-1.3.1.tar.bz2 +0 -0
  8. data/{gpgme_n.c → ext/gpgme/gpgme_n.c} +8 -8
  9. data/ext/gpgme/libassuan-2.0.2.tar.bz2 +0 -0
  10. data/ext/gpgme/libgpg-error-1.10.tar.bz2 +0 -0
  11. data/lib/gpgme.rb +88 -1541
  12. data/lib/gpgme/compat.rb +2 -0
  13. data/lib/gpgme/constants.rb +23 -0
  14. data/lib/gpgme/crypto.rb +357 -0
  15. data/lib/gpgme/ctx.rb +462 -0
  16. data/lib/gpgme/data.rb +177 -0
  17. data/lib/gpgme/engine.rb +76 -0
  18. data/lib/gpgme/error.rb +66 -0
  19. data/lib/gpgme/io_callbacks.rb +21 -0
  20. data/lib/gpgme/key.rb +242 -0
  21. data/lib/gpgme/key_common.rb +43 -0
  22. data/lib/gpgme/key_sig.rb +35 -0
  23. data/lib/gpgme/misc.rb +66 -0
  24. data/lib/gpgme/signature.rb +85 -0
  25. data/lib/gpgme/sub_key.rb +58 -0
  26. data/lib/gpgme/user_id.rb +20 -0
  27. data/lib/gpgme/version.rb +3 -0
  28. data/test/crypto_test.rb +242 -0
  29. data/test/ctx_test.rb +426 -0
  30. data/test/data_test.rb +116 -0
  31. data/test/files/testkey_pub.gpg +52 -0
  32. data/test/files/testkey_sec.gpg +54 -0
  33. data/test/gpgme_test.rb +12 -0
  34. data/test/key_test.rb +201 -0
  35. data/test/signature_test.rb +48 -0
  36. data/test/sub_key_test.rb +45 -0
  37. data/test/support/resources.rb +516 -0
  38. data/test/test_helper.rb +83 -0
  39. metadata +144 -65
  40. data.tar.gz.sig +0 -3
  41. data/COPYING +0 -340
  42. data/COPYING.LESSER +0 -510
  43. data/Makefile +0 -172
  44. data/Manifest.txt +0 -18
  45. data/README +0 -86
  46. data/Rakefile +0 -17
  47. data/THANKS +0 -15
  48. data/extconf.rb +0 -26
  49. metadata.gz.sig +0 -0
@@ -0,0 +1,426 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ describe GPGME::Ctx do
5
+ it "can instantiate" do
6
+ assert_instance_of GPGME::Ctx, GPGME::Ctx.new
7
+ end
8
+
9
+ it "doesn't close itself" do
10
+ GPGME.expects(:gpgme_release).never
11
+ GPGME::Ctx.new
12
+ end
13
+
14
+ it "closes itself if called with a block" do
15
+ GPGME.expects(:gpgme_release).with(anything)
16
+ GPGME::Ctx.new { |ctx| }
17
+ end
18
+
19
+ it "can be closed with the release method" do
20
+ GPGME.expects(:gpgme_release).with(anything)
21
+ ctx = GPGME::Ctx.new
22
+ ctx.release
23
+ end
24
+
25
+ describe :new do
26
+ # We consider :armor, :protocol, :textmode and :keylist_mode as tested
27
+ # with the other tests of this file. Here we test the rest
28
+
29
+ it ":password sets the password for the key" do
30
+ with_key PASSWORD_KEY do
31
+ input = GPGME::Data.new(TEXT[:passwored])
32
+ output = GPGME::Data.new
33
+
34
+ GPGME::Ctx.new(:password => 'gpgme') do |ctx|
35
+ ctx.decrypt_verify input, output
36
+ end
37
+
38
+ output.seek 0
39
+ assert_equal "Hi there", output.read.chomp
40
+ end
41
+ end
42
+
43
+ it ":passphrase_callback sets the callback for the password" do
44
+ def test_pass_func(obj,par2,par3,prev_was_bad,fd)
45
+ # prev_was_bad is 0 the first time, 1 the rest
46
+ if @var == 0
47
+ assert_equal 0, prev_was_bad
48
+ else
49
+ assert_equal 1, prev_was_bad
50
+ end
51
+
52
+ @var += 1
53
+
54
+ io = IO.for_fd(fd, 'w')
55
+ io.puts "wrong pasword"
56
+ io.flush
57
+ end
58
+
59
+ def with_correct_pass_func(obj,par2,par3,prev_was_bad,fd)
60
+ io = IO.for_fd(fd, 'w')
61
+ io.puts "gpgme"
62
+ io.flush
63
+ end
64
+
65
+ with_key PASSWORD_KEY do
66
+ input = GPGME::Data.new(TEXT[:passwored])
67
+ output = GPGME::Data.new
68
+ @var = 0
69
+
70
+ assert_raises GPGME::Error::BadPassphrase do
71
+ GPGME::Ctx.new(:passphrase_callback => method(:test_pass_func)) do |ctx|
72
+ ctx.decrypt_verify input, output
73
+ end
74
+ end
75
+
76
+ # Since we request the key 3 times, we should've gone through the
77
+ # callback 3 times.
78
+ assert_equal 3, @var
79
+
80
+ input.seek 0
81
+ output.seek 0
82
+
83
+ # Shouldn't crash
84
+ GPGME::Ctx.new(:passphrase_callback => method(:with_correct_pass_func)) do |ctx|
85
+ ctx.decrypt_verify input, output
86
+ end
87
+ end
88
+ end
89
+
90
+ it ":passphrase_callback_value passes a value to the callback function" do
91
+ def checking_value(value,par2,par3,par4,fd)
92
+ assert_equal "superman", value
93
+ io = IO.for_fd(fd, 'w')
94
+ io.puts "gpgme"
95
+ io.flush
96
+ end
97
+
98
+ with_key PASSWORD_KEY do
99
+ input = GPGME::Data.new(TEXT[:passwored])
100
+ output = GPGME::Data.new
101
+
102
+ options = {
103
+ :passphrase_callback => method(:checking_value),
104
+ :passphrase_callback_value => "superman"
105
+ }
106
+
107
+ GPGME::Ctx.new(options) do |ctx|
108
+ ctx.decrypt_verify input, output
109
+ end
110
+ end
111
+ end
112
+
113
+ # TODO Don't know how to use them yet
114
+ # it ":progress_callback"
115
+ # it ":progress_callback_value"
116
+ end
117
+
118
+ describe :armor do
119
+ it "sets false by default" do
120
+ ctx = GPGME::Ctx.new
121
+ refute ctx.armor
122
+ end
123
+
124
+ it "can set" do
125
+ ctx = GPGME::Ctx.new
126
+ ctx.armor = true
127
+ assert ctx.armor
128
+ end
129
+
130
+ it "can set and get armor" do
131
+ ctx = GPGME::Ctx.new(:armor => false)
132
+ refute ctx.armor
133
+ ctx = GPGME::Ctx.new(:armor => true)
134
+ assert ctx.armor
135
+ end
136
+ end
137
+
138
+ describe :protocol do
139
+ it "sets 0 by default" do
140
+ ctx = GPGME::Ctx.new
141
+ assert_equal 0, ctx.protocol
142
+ end
143
+
144
+ it "can set" do
145
+ ctx = GPGME::Ctx.new
146
+ ctx.protocol = 1
147
+ assert_equal 1, ctx.protocol
148
+ end
149
+
150
+ it "can set and get protocol" do
151
+ ctx = GPGME::Ctx.new(:protocol => GPGME::PROTOCOL_OpenPGP)
152
+ assert_equal GPGME::PROTOCOL_OpenPGP, ctx.protocol
153
+ end
154
+
155
+ it "doesn't allow just any value" do
156
+ assert_raises GPGME::Error::InvalidValue do
157
+ ctx = GPGME::Ctx.new(:protocol => -200)
158
+ end
159
+ end
160
+ end
161
+
162
+ describe :textmode do
163
+ it "sets false by default" do
164
+ ctx = GPGME::Ctx.new
165
+ refute ctx.textmode
166
+ end
167
+
168
+ it "can set" do
169
+ ctx = GPGME::Ctx.new
170
+ ctx.textmode = true
171
+ assert ctx.textmode
172
+ end
173
+
174
+ it "can set and get textmode" do
175
+ ctx = GPGME::Ctx.new(:textmode => false)
176
+ refute ctx.textmode
177
+ ctx = GPGME::Ctx.new(:textmode => true)
178
+ assert ctx.textmode
179
+ end
180
+ end
181
+
182
+ describe :keylist_mode do
183
+ it "sets local by default" do
184
+ ctx = GPGME::Ctx.new
185
+ assert_equal GPGME::KEYLIST_MODE_LOCAL, ctx.keylist_mode
186
+ end
187
+
188
+ it "can set and get" do
189
+ ctx = GPGME::Ctx.new(:keylist_mode => GPGME::KEYLIST_MODE_SIGS)
190
+ assert_equal GPGME::KEYLIST_MODE_SIGS, ctx.keylist_mode
191
+ end
192
+
193
+ it "can set" do
194
+ ctx = GPGME::Ctx.new
195
+ ctx.keylist_mode = GPGME::KEYLIST_MODE_SIGS
196
+ assert_equal GPGME::KEYLIST_MODE_SIGS, ctx.keylist_mode
197
+ end
198
+
199
+ it "allows the four possible values" do
200
+ [GPGME::KEYLIST_MODE_LOCAL, GPGME::KEYLIST_MODE_EXTERN,
201
+ GPGME::KEYLIST_MODE_SIGS, GPGME::KEYLIST_MODE_VALIDATE].each do |mode|
202
+ GPGME::Ctx.new(:keylist_mode => mode)
203
+ end
204
+ end
205
+
206
+ # It's not crashing?
207
+ # it "crashes with other values" do
208
+ # GPGME::Ctx.new(:keylist_mode => -200)
209
+ # end
210
+ end
211
+
212
+ # describe :set_passphrase_callback do
213
+ # def test_pass_func(par1,par2,par3,par4,par5)
214
+ # par1
215
+ # end
216
+
217
+ # test "it sets the passphrase"
218
+
219
+ # end
220
+
221
+ describe "keylist operations" do
222
+ it "can return all of the keys" do
223
+ ctx = GPGME::Ctx.new
224
+ keys = ctx.keys
225
+ ctx.release
226
+
227
+ assert keys.size >= 4
228
+ KEYS.each do |key|
229
+ assert keys.map(&:email).include?(key[:sha])
230
+ end
231
+ end
232
+
233
+ it "can return keys filtering by a pattern" do
234
+ ctx = GPGME::Ctx.new
235
+ keys = ctx.keys(KEYS.first[:sha])
236
+ ctx.release
237
+
238
+ assert_equal 1, keys.size
239
+ assert_equal KEYS.first[:sha], keys.first.email
240
+ end
241
+
242
+ it "can return only secret keys" do
243
+ ctx = GPGME::Ctx.new
244
+ keys = ctx.keys(KEYS.first[:sha], true)
245
+ ctx.release
246
+
247
+ assert keys.all?(&:secret?)
248
+ end
249
+
250
+ it "can return only public keys" do
251
+ ctx = GPGME::Ctx.new
252
+ keys = ctx.keys(KEYS.first[:sha], false)
253
+ ctx.release
254
+
255
+ refute keys.any?(&:secret?)
256
+ end
257
+
258
+ it "returns only public keys by default" do
259
+ ctx = GPGME::Ctx.new
260
+ keys = ctx.keys(KEYS.first[:sha])
261
+ ctx.release
262
+
263
+ refute keys.any?(&:secret?)
264
+ end
265
+
266
+ it "can iterate through them returning only public keys" do
267
+ GPGME::Ctx.new do |ctx|
268
+ ctx.each_key do |key|
269
+ assert_instance_of GPGME::Key, key
270
+ refute key.secret?
271
+ end
272
+ end
273
+ end
274
+
275
+ it "can iterate through them getting only secret ones" do
276
+ GPGME::Ctx.new do |ctx|
277
+ ctx.each_key("", true) do |key|
278
+ assert_instance_of GPGME::Key, key
279
+ assert key.secret?
280
+ end
281
+ end
282
+ end
283
+
284
+ it "can iterate through them filtering by pattern" do
285
+ num = 0
286
+ GPGME::Ctx.new do |ctx|
287
+ ctx.each_key(KEYS.first[:sha]) do |key|
288
+ assert_instance_of GPGME::Key, key
289
+ assert_equal KEYS.first[:sha], key.email
290
+ num += 1
291
+ end
292
+ end
293
+ assert_equal 1, num
294
+ end
295
+
296
+ it "can get only a specific key" do
297
+ GPGME::Ctx.new do |ctx|
298
+ key = ctx.get_key(KEYS.first[:sha])
299
+ assert_instance_of GPGME::Key, key
300
+ assert_equal KEYS.first[:sha], key.email
301
+ end
302
+ end
303
+ end
304
+
305
+ describe "key generation" do
306
+ it "generates a key according to specifications" do
307
+ key = <<-RUBY
308
+ <GnupgKeyParms format="internal">
309
+ Key-Type: DSA
310
+ Key-Length: 1024
311
+ Subkey-Type: ELG-E
312
+ Subkey-Length: 1024
313
+ Name-Real: Key Tester
314
+ Name-Comment: with some comments
315
+ Name-Email: test_generation@example.com
316
+ Expire-Date: 0
317
+ Passphrase: wadus
318
+ </GnupgKeyParms>
319
+ RUBY
320
+
321
+ keys_amount = GPGME::Key.find(:public).size
322
+ GPGME::Ctx.new do |ctx|
323
+ ctx.generate_key(key.chomp)
324
+ end
325
+
326
+ assert_equal keys_amount + 1, GPGME::Key.find(:public).size
327
+
328
+ GPGME::Key.find(:public, "test_generation@example.com").each do |k|
329
+ k.delete!(true)
330
+ end
331
+ end
332
+ end
333
+
334
+ describe "key export/import" do
335
+ it "exports and imports all keys when passing an empty string" do
336
+ original_keys = GPGME::Key.find(:public)
337
+ export = ""
338
+ GPGME::Ctx.new do |ctx|
339
+ export = ctx.export_keys("")
340
+ end
341
+ export.seek(0)
342
+
343
+ GPGME::Key.find(:public).each{|k| k.delete!(true)}
344
+ assert_equal 0, GPGME::Key.find(:public).size
345
+
346
+ result = GPGME::Key.import(export)
347
+ current_keys = GPGME::Key.find(:public)
348
+ assert_equal original_keys.size, current_keys.size
349
+ assert_equal result.imports.size, current_keys.size
350
+ assert result.imports.all?{|import| import.status == 1}
351
+
352
+ assert_equal original_keys.map(&:sha), original_keys.map(&:sha)
353
+
354
+ import_keys # If the test fails for some reason, it won't break others.
355
+ end
356
+
357
+ it "exports only one key" do
358
+ original_keys = GPGME::Key.find(:public)
359
+ key = original_keys.first
360
+ export = ""
361
+ GPGME::Ctx.new do |ctx|
362
+ export = ctx.export_keys(key.sha)
363
+ end
364
+ export.seek(0)
365
+
366
+ key.delete!(true)
367
+
368
+ result = GPGME::Key.import(export)
369
+ assert_equal 1, result.imports.size
370
+
371
+ import = result.imports.first
372
+
373
+ imported_key = GPGME::Key.find(:public, import.fpr).first
374
+ assert_equal key.sha, imported_key.sha
375
+ assert_equal key.email, imported_key.email
376
+ import_keys # If the test fails for some reason, it won't break others.
377
+ end
378
+
379
+ it "imports keys and can get a result object" do
380
+ without_key KEYS.last do
381
+ public_amount = GPGME::Key.find(:public).size
382
+ secret_amount = GPGME::Key.find(:secret).size
383
+
384
+ result = nil
385
+ GPGME::Ctx.new do |ctx|
386
+ ctx.import_keys(GPGME::Data.new(KEYS.last[:public]))
387
+ ctx.import_keys(GPGME::Data.new(KEYS.last[:secret]))
388
+
389
+ result = ctx.import_result
390
+ end
391
+
392
+ assert_equal secret_amount + 1, GPGME::Key.find(:secret).size
393
+ assert_equal public_amount + 1, GPGME::Key.find(:public).size
394
+ assert_instance_of GPGME::ImportResult, result
395
+ assert_instance_of GPGME::ImportStatus, result.imports.first
396
+ end
397
+ end
398
+ end
399
+
400
+ describe "deleting/editing of keys" do
401
+ it "can delete keys" do
402
+ original_keys = GPGME::Key.find(:public)
403
+ key = original_keys.first
404
+
405
+ GPGME::Ctx.new do |ctx|
406
+ ctx.delete_key key, true
407
+ end
408
+
409
+ assert_empty GPGME::Key.find(:public, key.sha)
410
+ import_keys
411
+ end
412
+
413
+ it "raises error if there's a secret key attached but secret key deletion isn't marked" do
414
+ original_keys = GPGME::Key.find(:public)
415
+ key = original_keys.first
416
+
417
+ assert_raises GPGME::Error::Conflict do
418
+ GPGME::Ctx.new do |ctx|
419
+ ctx.delete_key key
420
+ end
421
+ end
422
+ end
423
+ end
424
+
425
+ # Don't know how to test or use edit_key and edit_card
426
+ end
@@ -0,0 +1,116 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ describe GPGME::Data do
5
+ describe :new do
6
+ it "smartly creates an empty buffer if nothing passed" do
7
+ data = GPGME::Data.new
8
+ assert_instance_of GPGME::Data, data
9
+ assert_respond_to data, :read
10
+ assert_respond_to data, :write
11
+ end
12
+
13
+ it "doesn't create a new object if the object passed is a Data already" do
14
+ data = GPGME::Data.new
15
+ new_data = GPGME::Data.new(data)
16
+
17
+ assert_equal data, new_data
18
+ end
19
+
20
+ it "creates a data from strings" do
21
+ data = GPGME::Data.new("wadus")
22
+ assert_equal "wadus", data.read
23
+ end
24
+
25
+ it "creates a data from a file" do
26
+ # magic fromfile
27
+ data = GPGME::Data.new(File.open(__FILE__))
28
+ assert_match /magic fromfile/, data.read
29
+ end
30
+
31
+ it "creates a data from file descriptor" do
32
+ # magic filedescriptor
33
+ File.open(__FILE__) do |f|
34
+ data = GPGME::Data.new(f.fileno)
35
+ assert_match /magic filedescriptor/, data.read
36
+ end
37
+ end
38
+ end
39
+
40
+ describe :read do
41
+ it "allows to read only a length of the object" do
42
+ data = GPGME::Data.new("wadus")
43
+ assert_equal "wad", data.read(3)
44
+ end
45
+
46
+ it "returns nil if reading 0 length" do
47
+ data = GPGME::Data.new("wadus")
48
+ assert_nil data.read(0)
49
+ end
50
+
51
+ it "returns the full thing if reading without parameter" do
52
+ data = GPGME::Data.new("wadus")
53
+ assert_equal "wadus", data.read
54
+ end
55
+ end
56
+
57
+ ##
58
+ # We consider seek tested by these ones, since we have to seek(0) before
59
+ # reading.
60
+ describe :write do
61
+ it "writes data to it" do
62
+ data = GPGME::Data.new
63
+ data.write("wadus")
64
+ data.seek(0)
65
+ assert_equal "wadus", data.read
66
+ end
67
+
68
+ it "writes data to it, specifying the length of the things to write" do
69
+ data = GPGME::Data.new
70
+ data.write("wadus", 5)
71
+ data.seek(0)
72
+ assert_equal "wadus", data.read
73
+ end
74
+
75
+ it "writes only a limited part if specified a small number" do
76
+ data = GPGME::Data.new
77
+ data.write("wadus", 3)
78
+ data.seek(0)
79
+ assert_equal "wad", data.read
80
+ end
81
+
82
+ # TODO: test doesn't pass, I believe there might be a security issue here,
83
+ # random crap is written to the buffer if a longer size is passed.
84
+ #
85
+ # it "writes only the full data passed even if the length is bigger" do
86
+ # data = GPGME::Data.new
87
+ # data.write("wadus", 100)
88
+ # data.seek(0)
89
+ # assert_equal "wadus", data.read
90
+ # end
91
+ end
92
+
93
+ describe :encoding do
94
+ it "has encoding 0 by default (DATA_ENCODING_NONE)" do
95
+ data = GPGME::Data.new("wadus")
96
+ assert_equal GPGME::DATA_ENCODING_NONE, data.encoding
97
+ end
98
+
99
+ it "can set encodings" do
100
+ data = GPGME::Data.new("wadus")
101
+ [ GPGME::DATA_ENCODING_ARMOR, GPGME::DATA_ENCODING_BASE64,
102
+ GPGME::DATA_ENCODING_BINARY,GPGME::DATA_ENCODING_NONE ].each do |encoding|
103
+ data.encoding = encoding
104
+ assert_equal encoding, data.encoding
105
+ end
106
+ end
107
+
108
+ it "breaks if not set a proper encoding value" do
109
+ data = GPGME::Data.new("wadus")
110
+ assert_raises GPGME::Error::InvalidValue do
111
+ data.encoding = 64
112
+ end
113
+ end
114
+ end
115
+ end
116
+