gpgme-loongson 2.0.18

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.
@@ -0,0 +1,471 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ describe GPGME::Ctx do
5
+ before do
6
+ skip unless ensure_keys GPGME::PROTOCOL_OpenPGP
7
+ end
8
+
9
+ it "can instantiate" do
10
+ assert_instance_of GPGME::Ctx, GPGME::Ctx.new
11
+ end
12
+
13
+ it "doesn't close itself" do
14
+ GPGME.expects(:gpgme_release).never
15
+ GPGME::Ctx.new
16
+ end
17
+
18
+ it "closes itself if called with a block" do
19
+ GPGME.expects(:gpgme_release).with(anything)
20
+ GPGME::Ctx.new { |ctx| }
21
+ end
22
+
23
+ it "can be closed with the release method" do
24
+ GPGME.expects(:gpgme_release).with(anything)
25
+ ctx = GPGME::Ctx.new
26
+ ctx.release
27
+ end
28
+
29
+ describe :new do
30
+ # We consider :armor, :protocol, :textmode and :keylist_mode as tested
31
+ # with the other tests of this file. Here we test the rest
32
+
33
+ it ":password sets the password for the key" do
34
+ with_key PASSWORD_KEY do
35
+ input = GPGME::Data.new(TEXT[:passwored])
36
+ output = GPGME::Data.new
37
+
38
+ GPGME::Ctx.new(:password => 'gpgme') do |ctx|
39
+ ctx.decrypt_verify input, output
40
+
41
+ output.seek 0
42
+ assert_equal "Hi there", output.read.chomp
43
+
44
+ recipients = ctx.decrypt_result.recipients
45
+ assert_equal 1, recipients.size
46
+
47
+ recipient_key = ctx.get_key(recipients.first.keyid)
48
+ key = ctx.get_key(PASSWORD_KEY[:sha])
49
+
50
+ assert_equal recipient_key, key
51
+ end
52
+ end
53
+ end
54
+
55
+ it ":passphrase_callback sets the callback for the password" do
56
+ def test_pass_func(obj,par2,par3,prev_was_bad,fd)
57
+ # prev_was_bad is 0 the first time, 1 the rest
58
+ if @var == 0
59
+ assert_equal 0, prev_was_bad
60
+ else
61
+ assert_equal 1, prev_was_bad
62
+ end
63
+
64
+ @var += 1
65
+
66
+ io = IO.for_fd(fd, 'w')
67
+ io.puts "wrong pasword"
68
+ io.flush
69
+ end
70
+
71
+ def with_correct_pass_func(obj,par2,par3,prev_was_bad,fd)
72
+ io = IO.for_fd(fd, 'w')
73
+ io.puts "gpgme"
74
+ io.flush
75
+ end
76
+
77
+ with_key PASSWORD_KEY do
78
+ input = GPGME::Data.new(TEXT[:passwored])
79
+ output = GPGME::Data.new
80
+ @var = 0
81
+
82
+ assert_raises GPGME::Error::BadPassphrase do
83
+ GPGME::Ctx.new(:passphrase_callback => method(:test_pass_func)) do |ctx|
84
+ ctx.decrypt_verify input, output
85
+ end
86
+ end
87
+
88
+ # Since we request the key 3 times, we should've gone through the
89
+ # callback 3 times.
90
+ assert_equal 3, @var
91
+
92
+ input.seek 0
93
+ output.seek 0
94
+
95
+ # Shouldn't crash
96
+ GPGME::Ctx.new(:passphrase_callback => method(:with_correct_pass_func)) do |ctx|
97
+ ctx.decrypt_verify input, output
98
+ end
99
+ end
100
+ end
101
+
102
+ it ":passphrase_callback_value passes a value to the callback function" do
103
+ def checking_value(value,par2,par3,par4,fd)
104
+ assert_equal "superman", value
105
+ io = IO.for_fd(fd, 'w')
106
+ io.puts "gpgme"
107
+ io.flush
108
+ end
109
+
110
+ with_key PASSWORD_KEY do
111
+ input = GPGME::Data.new(TEXT[:passwored])
112
+ output = GPGME::Data.new
113
+
114
+ options = {
115
+ :passphrase_callback => method(:checking_value),
116
+ :passphrase_callback_value => "superman"
117
+ }
118
+
119
+ GPGME::Ctx.new(options) do |ctx|
120
+ ctx.decrypt_verify input, output
121
+ end
122
+ end
123
+ end
124
+
125
+ # TODO Don't know how to use them yet
126
+ # it ":progress_callback"
127
+ # it ":progress_callback_value"
128
+ end
129
+
130
+ describe :decrypt_result do
131
+ it "returns the list of encyption recipients" do
132
+ cipher = GPGME::Data.new(KEY_1_ENCRYPTED)
133
+ output = GPGME::Data.new
134
+
135
+ GPGME::Ctx.new do |ctx|
136
+ ctx.decrypt_verify(cipher, output)
137
+ assert_equal 1, ctx.decrypt_result.recipients.size
138
+ end
139
+ end
140
+
141
+ it "should not segfault" do
142
+ cipher = GPGME::Data.new(KEY_1_ENCRYPTED)
143
+ ouput = GPGME::Data.new
144
+
145
+ GPGME::Ctx.new do |ctx|
146
+ assert_raises ArgumentError do
147
+ ctx.decrypt_result
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ describe :armor do
154
+ it "sets false by default" do
155
+ ctx = GPGME::Ctx.new
156
+ refute ctx.armor
157
+ end
158
+
159
+ it "can set" do
160
+ ctx = GPGME::Ctx.new
161
+ ctx.armor = true
162
+ assert ctx.armor
163
+ end
164
+
165
+ it "can set and get armor" do
166
+ ctx = GPGME::Ctx.new(:armor => false)
167
+ refute ctx.armor
168
+ ctx = GPGME::Ctx.new(:armor => true)
169
+ assert ctx.armor
170
+ end
171
+ end
172
+
173
+ describe :protocol do
174
+ it "sets 0 by default" do
175
+ ctx = GPGME::Ctx.new
176
+ assert_equal 0, ctx.protocol
177
+ end
178
+
179
+ it "can set" do
180
+ ctx = GPGME::Ctx.new
181
+ ctx.protocol = 1
182
+ assert_equal 1, ctx.protocol
183
+ end
184
+
185
+ it "can set and get protocol" do
186
+ ctx = GPGME::Ctx.new(:protocol => GPGME::PROTOCOL_OpenPGP)
187
+ assert_equal GPGME::PROTOCOL_OpenPGP, ctx.protocol
188
+ end
189
+
190
+ it "doesn't allow just any value" do
191
+ assert_raises GPGME::Error::InvalidValue do
192
+ ctx = GPGME::Ctx.new(:protocol => -200)
193
+ end
194
+ end
195
+ end
196
+
197
+ describe :textmode do
198
+ it "sets false by default" do
199
+ ctx = GPGME::Ctx.new
200
+ refute ctx.textmode
201
+ end
202
+
203
+ it "can set" do
204
+ ctx = GPGME::Ctx.new
205
+ ctx.textmode = true
206
+ assert ctx.textmode
207
+ end
208
+
209
+ it "can set and get textmode" do
210
+ ctx = GPGME::Ctx.new(:textmode => false)
211
+ refute ctx.textmode
212
+ ctx = GPGME::Ctx.new(:textmode => true)
213
+ assert ctx.textmode
214
+ end
215
+ end
216
+
217
+ describe :keylist_mode do
218
+ it "sets local by default" do
219
+ ctx = GPGME::Ctx.new
220
+ assert_equal GPGME::KEYLIST_MODE_LOCAL, ctx.keylist_mode
221
+ end
222
+
223
+ it "can set and get" do
224
+ ctx = GPGME::Ctx.new(:keylist_mode => GPGME::KEYLIST_MODE_SIGS)
225
+ assert_equal GPGME::KEYLIST_MODE_SIGS, ctx.keylist_mode
226
+ end
227
+
228
+ it "can set" do
229
+ ctx = GPGME::Ctx.new
230
+ ctx.keylist_mode = GPGME::KEYLIST_MODE_SIGS
231
+ assert_equal GPGME::KEYLIST_MODE_SIGS, ctx.keylist_mode
232
+ end
233
+
234
+ it "allows the four possible values" do
235
+ [GPGME::KEYLIST_MODE_LOCAL, GPGME::KEYLIST_MODE_EXTERN,
236
+ GPGME::KEYLIST_MODE_SIGS, GPGME::KEYLIST_MODE_VALIDATE].each do |mode|
237
+ GPGME::Ctx.new(:keylist_mode => mode)
238
+ end
239
+ end
240
+
241
+ # It's not crashing?
242
+ # it "crashes with other values" do
243
+ # GPGME::Ctx.new(:keylist_mode => -200)
244
+ # end
245
+ end
246
+
247
+ # describe :set_passphrase_callback do
248
+ # def test_pass_func(par1,par2,par3,par4,par5)
249
+ # par1
250
+ # end
251
+
252
+ # test "it sets the passphrase"
253
+
254
+ # end
255
+
256
+ describe "keylist operations" do
257
+ it "can return all of the keys" do
258
+ ctx = GPGME::Ctx.new
259
+ keys = ctx.keys
260
+ ctx.release
261
+
262
+ assert keys.size >= 4
263
+ KEYS.each do |key|
264
+ assert keys.map(&:email).include?(key[:sha])
265
+ end
266
+ end
267
+
268
+ it "can return keys filtering by a pattern" do
269
+ ctx = GPGME::Ctx.new
270
+ keys = ctx.keys(KEYS.first[:sha])
271
+ ctx.release
272
+
273
+ assert_equal 1, keys.size
274
+ assert_equal KEYS.first[:sha], keys.first.email
275
+ end
276
+
277
+ it "can return only secret keys" do
278
+ ctx = GPGME::Ctx.new
279
+ keys = ctx.keys(KEYS.first[:sha], true)
280
+ ctx.release
281
+
282
+ assert keys.all?(&:secret?)
283
+ end
284
+
285
+ it "can return only public keys" do
286
+ ctx = GPGME::Ctx.new
287
+ keys = ctx.keys(KEYS.first[:sha], false)
288
+ ctx.release
289
+
290
+ refute keys.any?(&:secret?)
291
+ end
292
+
293
+ it "returns only public keys by default" do
294
+ ctx = GPGME::Ctx.new
295
+ keys = ctx.keys(KEYS.first[:sha])
296
+ ctx.release
297
+
298
+ refute keys.any?(&:secret?)
299
+ end
300
+
301
+ it "can iterate through them returning only public keys" do
302
+ GPGME::Ctx.new do |ctx|
303
+ ctx.each_key do |key|
304
+ assert_instance_of GPGME::Key, key
305
+ refute key.secret?
306
+ end
307
+ end
308
+ end
309
+
310
+ it "can iterate through them getting only secret ones" do
311
+ GPGME::Ctx.new do |ctx|
312
+ ctx.each_key("", true) do |key|
313
+ assert_instance_of GPGME::Key, key
314
+ assert key.secret?
315
+ end
316
+ end
317
+ end
318
+
319
+ it "can iterate through them filtering by pattern" do
320
+ num = 0
321
+ GPGME::Ctx.new do |ctx|
322
+ ctx.each_key(KEYS.first[:sha]) do |key|
323
+ assert_instance_of GPGME::Key, key
324
+ assert_equal KEYS.first[:sha], key.email
325
+ num += 1
326
+ end
327
+ end
328
+ assert_equal 1, num
329
+ end
330
+
331
+ it "can get only a specific key" do
332
+ GPGME::Ctx.new do |ctx|
333
+ key = ctx.get_key(KEYS.first[:sha])
334
+ assert_instance_of GPGME::Key, key
335
+ assert_equal KEYS.first[:sha], key.email
336
+ end
337
+ end
338
+ end
339
+
340
+ describe "key generation" do
341
+ it "generates a key according to specifications" do
342
+ key = <<-RUBY
343
+ <GnupgKeyParms format="internal">
344
+ Key-Type: DSA
345
+ Key-Length: 1024
346
+ Subkey-Type: ELG-E
347
+ Subkey-Length: 1024
348
+ Name-Real: Key Testér
349
+ Name-Comment: with some comments
350
+ Name-Email: test_generation@example.com
351
+ Expire-Date: 0
352
+ Passphrase: wadus
353
+ </GnupgKeyParms>
354
+ RUBY
355
+
356
+ if RUBY_VERSION > "1.9"
357
+ assert_equal key.encoding, Encoding::UTF_8
358
+ end
359
+
360
+ keys_amount = GPGME::Key.find(:public).size
361
+ GPGME::Ctx.new do |ctx|
362
+ ctx.generate_key(key.chomp)
363
+ end
364
+
365
+ assert_equal keys_amount + 1, GPGME::Key.find(:public).size
366
+
367
+ GPGME::Key.find(:public, "test_generation@example.com").each do |k|
368
+
369
+ if RUBY_VERSION > "1.9"
370
+ # Make sure UTF-8 in and UTF-8 out.
371
+ assert_equal "Key Testér", k.name
372
+ assert_equal k.name.encoding, Encoding::UTF_8
373
+ end
374
+ k.delete!(true)
375
+ end
376
+ end
377
+ end
378
+
379
+ describe "key export/import" do
380
+ it "exports and imports all keys when passing an empty string" do
381
+ original_keys = GPGME::Key.find(:public)
382
+ export = ""
383
+ GPGME::Ctx.new do |ctx|
384
+ export = ctx.export_keys("")
385
+ end
386
+ export.seek(0)
387
+
388
+ GPGME::Key.find(:public).each{|k| k.delete!(true)}
389
+ assert_equal 0, GPGME::Key.find(:public).size
390
+
391
+ result = GPGME::Key.import(export)
392
+ current_keys = GPGME::Key.find(:public)
393
+ assert_equal original_keys.size, current_keys.size
394
+ assert_equal result.imports.size, current_keys.size
395
+ assert result.imports.all?{|import| import.status == 1}
396
+
397
+ assert_equal original_keys.map(&:sha), original_keys.map(&:sha)
398
+
399
+ import_keys # If the test fails for some reason, it won't break others.
400
+ end
401
+
402
+ it "exports only one key" do
403
+ original_keys = GPGME::Key.find(:public)
404
+ key = original_keys.first
405
+ export = ""
406
+ GPGME::Ctx.new do |ctx|
407
+ export = ctx.export_keys(key.sha)
408
+ end
409
+ export.seek(0)
410
+
411
+ key.delete!(true)
412
+
413
+ result = GPGME::Key.import(export)
414
+ assert_equal 1, result.imports.size
415
+
416
+ import = result.imports.first
417
+
418
+ imported_key = GPGME::Key.find(:public, import.fpr).first
419
+ assert_equal key.sha, imported_key.sha
420
+ assert_equal key.email, imported_key.email
421
+ import_keys # If the test fails for some reason, it won't break others.
422
+ end
423
+
424
+ it "imports keys and can get a result object" do
425
+ without_key KEYS.last do
426
+ public_amount = GPGME::Key.find(:public).size
427
+ secret_amount = GPGME::Key.find(:secret).size
428
+
429
+ result = nil
430
+ GPGME::Ctx.new do |ctx|
431
+ ctx.import_keys(GPGME::Data.new(KEYS.last[:public]))
432
+ ctx.import_keys(GPGME::Data.new(KEYS.last[:secret]))
433
+
434
+ result = ctx.import_result
435
+ end
436
+
437
+ assert_equal secret_amount + 1, GPGME::Key.find(:secret).size
438
+ assert_equal public_amount + 1, GPGME::Key.find(:public).size
439
+ assert_instance_of GPGME::ImportResult, result
440
+ assert_instance_of GPGME::ImportStatus, result.imports.first
441
+ end
442
+ end
443
+ end
444
+
445
+ describe "deleting/editing of keys" do
446
+ it "can delete keys" do
447
+ original_keys = GPGME::Key.find(:public)
448
+ key = original_keys.first
449
+
450
+ GPGME::Ctx.new do |ctx|
451
+ ctx.delete_key key, true
452
+ end
453
+
454
+ assert_empty GPGME::Key.find(:public, key.sha)
455
+ import_keys
456
+ end
457
+
458
+ it "raises error if there's a secret key attached but secret key deletion isn't marked" do
459
+ original_keys = GPGME::Key.find(:public)
460
+ key = original_keys.first
461
+
462
+ assert_raises GPGME::Error::Conflict do
463
+ GPGME::Ctx.new do |ctx|
464
+ ctx.delete_key key
465
+ end
466
+ end
467
+ end
468
+ end
469
+
470
+ # Don't know how to test or use edit_key and edit_card
471
+ end