Skip to content
Newer
Older
100644 91 lines (64 sloc) 2.69 KB
36d7a4a @drbrain Move RubyGemTestCase to Gem::TestCase that lives in lib/ along with o…
drbrain authored Jan 19, 2011
1 require 'rubygems/test_case'
69dfba4 @drbrain Add test for Gem::Security::build_cert, fix test poisoning from the c…
drbrain authored Dec 3, 2010
2 require 'rubygems/security'
cc9c00d @zenspider Quell openssl warnings during tests
zenspider authored Feb 16, 2011
3 require 'rubygems/fix_openssl_warnings'
69dfba4 @drbrain Add test for Gem::Security::build_cert, fix test poisoning from the c…
drbrain authored Dec 4, 2010
4
36d7a4a @drbrain Move RubyGemTestCase to Gem::TestCase that lives in lib/ along with o…
drbrain authored Jan 19, 2011
5 class TestGemSecurity < Gem::TestCase
69dfba4 @drbrain Add test for Gem::Security::build_cert, fix test poisoning from the c…
drbrain authored Dec 4, 2010
6
7 def test_class_build_cert
8 name = OpenSSL::X509::Name.parse "CN=nobody/DC=example"
9 key = OpenSSL::PKey::RSA.new 512
10 opt = { :cert_age => 60 }
11
12 cert = Gem::Security.build_cert name, key, opt
13
14 assert_kind_of OpenSSL::X509::Certificate, cert
15
16 assert_equal 2, cert.version
17 assert_equal 0, cert.serial
18 assert_equal key.public_key.to_pem, cert.public_key.to_pem
19 assert_in_delta Time.now, cert.not_before, 10
20 assert_in_delta Time.now + 60, cert.not_after, 10
21 assert_equal name.to_s, cert.subject.to_s
22
23 assert_equal 3, cert.extensions.length
24
25 constraints = cert.extensions.find { |ext| ext.oid == 'basicConstraints' }
26 assert_equal 'CA:FALSE', constraints.value
27
28 key_usage = cert.extensions.find { |ext| ext.oid == 'keyUsage' }
29 assert_equal 'Digital Signature, Key Encipherment, Data Encipherment',
30 key_usage.value
31
32 key_ident = cert.extensions.find { |ext| ext.oid == 'subjectKeyIdentifier' }
33 assert_equal 59, key_ident.value.length
34
35 assert_equal name.to_s, cert.issuer.to_s
36 assert_equal name.to_s, cert.subject.to_s
37 end
38
d028ac2 @drbrain Test Gem::Security::build_self_signed_cert, ::sign_cert
drbrain authored Dec 3, 2010
39 def test_class_build_self_signed_cert
40 email = 'nobody@example'
41 opt = {
42 :cert_age => 60,
43 :key_size => 512,
44 :save_cert => false,
45 :save_key => false,
46 }
47
48 result = Gem::Security.build_self_signed_cert email, opt
49
50 key = result[:key]
51
52 assert_kind_of OpenSSL::PKey::RSA, key
53 # assert_equal 512, key.something_here
54
55 cert = result[:cert]
56
57 assert_equal '/CN=nobody/DC=example', cert.issuer.to_s
58 end
59
60 def test_class_sign_cert
61 name = OpenSSL::X509::Name.parse "CN=nobody/DC=example"
62 key = OpenSSL::PKey::RSA.new 512
63 cert = OpenSSL::X509::Certificate.new
64
65 cert.subject = name
66 cert.public_key = key.public_key
67
68 signed = Gem::Security.sign_cert cert, key, cert
69
70 assert cert.verify key
71 assert_equal name.to_s, signed.subject.to_s
72 end
73
fa046a0 @drbrain Pull ::email_to_name out of Gem::Security::build_self_signed_cert for…
drbrain authored Dec 3, 2010
74 def test_class_email_to_name
75 munger = Gem::Security::OPT[:munge_re]
76
77 assert_equal '/CN=nobody/DC=example',
78 Gem::Security.email_to_name('nobody@example', munger).to_s
79
80 assert_equal '/CN=nobody/DC=example/DC=com',
81 Gem::Security.email_to_name('nobody@example.com', munger).to_s
82
83 assert_equal '/CN=no.body/DC=example',
84 Gem::Security.email_to_name('no.body@example', munger).to_s
85
86 assert_equal '/CN=no_body/DC=example',
87 Gem::Security.email_to_name('no+body@example', munger).to_s
88 end
89
5996a8f @luislavena Backport r30734 from Ruby trunk
luislavena authored Jan 31, 2011
90 end if defined?(OpenSSL)
Something went wrong with that request. Please try again.