benburkert-gpgme 0.1.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/test/key_test.rb ADDED
@@ -0,0 +1,201 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ describe GPGME::Key do
5
+
6
+ it "has certain attributes" do
7
+ key = GPGME::Key.find(:secret).first
8
+ [:keylist_mode, :protocol, :owner_trust, :issuer_serial,
9
+ :issuer_name, :chain_id, :subkeys, :uids].each do |attrib|
10
+ assert key.respond_to?(attrib), "Key doesn't respond to #{attrib}"
11
+ end
12
+ end
13
+
14
+ it "won't allow the creation of GPGME::Key's without the C API" do
15
+ assert_raises NoMethodError do
16
+ GPGME::Key.new
17
+ end
18
+ end
19
+
20
+ it "can compare one with another even though if they're not the same object" do
21
+ key1 = GPGME::Key.find(:secret).first
22
+ key2 = GPGME::Key.find(:secret).first
23
+ refute_equal key1.object_id, key2.object_id
24
+ assert_equal key1, key2
25
+ end
26
+
27
+ describe :find do
28
+ it "should return all by default" do
29
+ keys = GPGME::Key.find :secret
30
+ assert_instance_of GPGME::Key, keys.first
31
+ assert 0 < keys.size
32
+ end
33
+
34
+ it "returns an array even if you pass only one descriptor" do
35
+ keys_one = GPGME::Key.find(:secret, KEYS.first[:sha]).map{|key| key.subkeys.map(&:keyid)}
36
+ keys_array = GPGME::Key.find(:secret, [KEYS.first[:sha]]).map{|key| key.subkeys.map(&:keyid)}
37
+ assert_equal keys_one, keys_array
38
+ end
39
+
40
+ it "returns only secret keys if told to do so" do
41
+ keys = GPGME::Key.find :secret
42
+ assert keys.all?(&:secret?)
43
+ end
44
+
45
+ it "returns only public keys if told to do so" do
46
+ keys = GPGME::Key.find :public
47
+ assert keys.none?(&:secret?)
48
+ end
49
+
50
+ it "filters by capabilities" do
51
+ GPGME::Key.any_instance.stubs(:usable_for?).returns(false)
52
+ keys = GPGME::Key.find :public, "", :wadusing
53
+ assert keys.empty?
54
+ end
55
+ end
56
+
57
+ describe :export do
58
+ # Testing the lazy way with expectations. I think tests in
59
+ # the Ctx class are enough.
60
+ it "exports any key that matches the pattern" do
61
+ GPGME::Ctx.any_instance.expects(:export_keys).with("", anything)
62
+ GPGME::Key.export("")
63
+ end
64
+
65
+ it "exports any key that matches the pattern, can specify output" do
66
+ data = GPGME::Data.new
67
+ GPGME::Ctx.any_instance.expects(:export_keys).with("wadus", data)
68
+ ret = GPGME::Key.export("wadus", :output => data)
69
+ assert_equal data, ret
70
+ end
71
+
72
+ it "can specify options for Ctx" do
73
+ GPGME::Ctx.expects(:new).with(:armor => true).yields(mock(:export_keys => true))
74
+ GPGME::Key.export("wadus", :armor => true)
75
+ end
76
+ end
77
+
78
+ describe "#export" do
79
+ it "can export from the key instance" do
80
+ key = GPGME::Key.find(:public).first
81
+ GPGME::Key.expects(:export).with(key.sha, {})
82
+
83
+ key.export
84
+ end
85
+
86
+ it "can export from the key instance passing variables" do
87
+ key = GPGME::Key.find(:public).first
88
+ GPGME::Key.expects(:export).with(key.sha, {:armor => true})
89
+
90
+ key.export :armor => true
91
+ end
92
+ end
93
+
94
+ describe :import do
95
+ it "can import keys" do
96
+ data = GPGME::Data.new
97
+ GPGME::Ctx.any_instance.expects(:import_keys).with(data)
98
+ GPGME::Ctx.any_instance.expects(:import_result).returns("wadus")
99
+
100
+ assert_equal "wadus", GPGME::Key.import(data)
101
+ end
102
+
103
+ it "can specify options for Ctx" do
104
+ GPGME::Ctx.expects(:new).with(:armor => true).yields(mock(:import_keys => true, :import_result => true))
105
+ GPGME::Key.import("wadus", :armor => true)
106
+ end
107
+ end
108
+
109
+ # describe :trust do
110
+ # it "returns :revoked if it is so"
111
+ # it "returns :expired if it is expired"
112
+ # it "returns :disabled if it is so"
113
+ # it "returns :invalid if it is so"
114
+ # it "returns nil otherwise"
115
+ # end
116
+
117
+ # describe :capability do
118
+ # it "returns an array of possible capabilities"
119
+ # end
120
+
121
+ # describe :secret? do
122
+ # "returns true/false depending on the instance variable"
123
+ # end
124
+
125
+ describe :usable_for? do
126
+ it "checks for the capabilities of the key and returns true if it matches all" do
127
+ key = GPGME::Key.find(:secret).first
128
+
129
+ key.stubs(:capability).returns([:encrypt, :sign])
130
+ assert key.usable_for?([])
131
+
132
+ key.stubs(:capability).returns([:encrypt, :sign])
133
+ assert key.usable_for?([:encrypt])
134
+
135
+ key.stubs(:capability).returns([:encrypt, :sign])
136
+ refute key.usable_for?([:certify])
137
+ end
138
+
139
+ it "returns false if the key is expired or revoked or disabled or disabled" do
140
+ key = GPGME::Key.find(:secret).first
141
+ key.stubs(:trust).returns(:revoked)
142
+ key.stubs(:capability).returns([:encrypt, :sign])
143
+ refute key.usable_for?([:encrypt])
144
+ end
145
+ end
146
+
147
+ describe :delete! do
148
+ it "deletes the key itself and its secret one if called with true" do
149
+ begin
150
+ key = KEYS.first
151
+ GPGME::Key.find(:public, key[:sha]).first.delete!(true)
152
+
153
+ assert_empty GPGME::Key.find(:public, key[:sha])
154
+ assert_empty GPGME::Key.find(:secret, key[:sha])
155
+ ensure
156
+ import_key key
157
+ end
158
+ end
159
+
160
+ it "raises GPGME::Error::Conflict if we're deleting a key that is secret" do
161
+ key = KEYS.first
162
+ assert_raises GPGME::Error::Conflict do
163
+ GPGME::Key.find(:secret, key[:sha]).first.delete!
164
+ end
165
+ refute_empty GPGME::Key.find(:secret, key[:sha])
166
+ end
167
+ end
168
+
169
+ it "knows if the key is expired" do
170
+ key = GPGME::Key.find(:secret).first
171
+ refute key.expired
172
+
173
+ with_key EXPIRED_KEY do
174
+ key = GPGME::Key.find(:secret, EXPIRED_KEY[:sha]).first
175
+ assert key.expired
176
+ end
177
+ end
178
+
179
+ it "returns the expiry date of the first subkey" do
180
+ key = GPGME::Key.find(:secret).first
181
+ subkey = key.primary_subkey
182
+ subkey.expects(:expired).returns(true)
183
+
184
+ assert key.expired
185
+ end
186
+
187
+ describe :inspect do
188
+ it "can be inspected" do
189
+ key = GPGME::Key.find(:secret).first
190
+ key.inspect
191
+ end
192
+ end
193
+
194
+ describe :to_s do
195
+ it "can be coerced into a String" do
196
+ key = GPGME::Key.find(:secret).first
197
+ key.to_s
198
+ end
199
+ end
200
+ end
201
+
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ describe GPGME::Signature do
5
+ it "#valid? is true when the signature is valid" do
6
+ crypto = GPGME::Crypto.new
7
+ signatures = 0
8
+ sign = crypto.sign "Hi there"
9
+
10
+ crypto.verify(sign) do |signature|
11
+ assert_instance_of GPGME::Signature, signature
12
+ assert signature.valid?
13
+ refute signature.expired_signature?
14
+ refute signature.expired_key?
15
+ refute signature.revoked_key?
16
+ refute signature.bad?
17
+ refute signature.no_key?
18
+ signatures += 1
19
+ end
20
+
21
+ assert_equal 1, signatures
22
+ end
23
+
24
+ it "#expired_key? is true when the key has expired" do
25
+ with_key EXPIRED_KEY do
26
+ crypto = GPGME::Crypto.new
27
+ signatures = 0
28
+ crypto.verify(TEXT[:expired_key_sign]) do |signature|
29
+ assert_instance_of GPGME::Signature, signature
30
+ refute signature.valid?
31
+ refute signature.expired_signature?
32
+ assert signature.expired_key?
33
+ refute signature.revoked_key?
34
+ refute signature.bad?
35
+ refute signature.no_key?
36
+ signatures += 1
37
+ end
38
+
39
+ assert_equal 1, signatures
40
+ end
41
+ end
42
+
43
+ # TODO Find how to test these
44
+ # it "#expired_signature? is true when the signature has expired"
45
+ # it "#revoked_key? is true when the key has been revoked"
46
+ # it "#bad? is true when the signature is bad"
47
+ # it "#no_key? is true when we don't have the key to verify the signature"
48
+ end
@@ -0,0 +1,45 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ describe GPGME::SubKey do
5
+
6
+ # We trust Key for common methods that come from KeyCommon
7
+
8
+ it "has certain attributes" do
9
+ subkey = GPGME::Key.find(:secret).first.primary_subkey
10
+ [:pubkey_algo, :length, :keyid, :fpr, :fingerprint].each do |attrib|
11
+ assert subkey.respond_to?(attrib), "Key doesn't respond to #{attrib}"
12
+ end
13
+ end
14
+
15
+ it "won't allow the creation of GPGME::SubKey's without the C API" do
16
+ assert_raises NoMethodError do
17
+ GPGME::SubKey.new
18
+ end
19
+ end
20
+
21
+ it "knows if the key is expired" do
22
+ subkey = GPGME::Key.find(:secret).first.primary_subkey
23
+ refute subkey.expired
24
+
25
+ with_key EXPIRED_KEY do
26
+ subkey = GPGME::Key.find(:secret, EXPIRED_KEY[:sha]).first.primary_subkey
27
+ assert subkey.expired
28
+ end
29
+ end
30
+
31
+ describe :inspect do
32
+ it "can be inspected" do
33
+ subkey = GPGME::Key.find(:secret).first.primary_subkey
34
+ subkey.inspect
35
+ end
36
+ end
37
+
38
+ describe :to_s do
39
+ it "can be coerced into a String" do
40
+ subkey = GPGME::Key.find(:secret).first.primary_subkey
41
+ subkey.to_s
42
+ end
43
+ end
44
+
45
+ end