2 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
4 require 'rubygems/commands/cert_command'
6 unless defined? OpenSSL then
7 warn "`gem cert` tests are being skipped, module OpenSSL not found"
10 class TestGemCommandsCertCommand < RubyGemTestCase
15 @orig_security_trust_dir = Gem::Security::OPT[:trust_dir]
16 Gem::Security::OPT[:trust_dir] = @tempdir
18 @cmd = Gem::Commands::CertCommand.new
20 root = File.expand_path(File.dirname(__FILE__))
22 FileUtils.cp File.join(root, 'data', 'gem-private_key.pem'), @tempdir
23 FileUtils.cp File.join(root, 'data', 'gem-public_cert.pem'), @tempdir
25 @cert_file_name = File.join @tempdir, 'gem-public_cert.pem'
26 @pkey_file_name = File.join @tempdir, 'gem-private_key.pem'
30 Gem::Security::OPT[:trust_dir] = @orig_security_trust_dir
37 @cmd.send :handle_options, %W[--add #{@cert_file_name}]
40 assert_equal "Added '/CN=rubygems/DC=example/DC=com'\n", @ui.output
41 assert_equal '', @ui.error
44 def test_execute_build
45 FileUtils.rm @cert_file_name
46 FileUtils.rm @pkey_file_name
50 @cmd.send :handle_options, %W[--build nobody@example.com]
54 output = @ui.output.split "\n"
56 assert_equal 'Public Cert: gem-public_cert.pem', output.shift
57 assert_equal 'Private Key: gem-private_key.pem', output.shift
58 assert_equal 'Don\'t forget to move the key file to somewhere private...',
60 assert_equal [], output
62 assert_equal '', @ui.error
64 assert File.exist?(File.join(@tempdir, 'gem-private_key.pem'))
65 assert File.exist?(File.join(@tempdir, 'gem-public_cert.pem'))
68 def test_execute_certificate
70 @cmd.send :handle_options, %W[--certificate #{@cert_file_name}]
73 assert_equal '', @ui.output
74 assert_equal '', @ui.error
76 assert_equal File.read(@cert_file_name),
77 Gem::Security::OPT[:issuer_cert].to_s
82 @cmd.send :handle_options, %W[--list]
85 assert_equal "/CN=rubygems/DC=example/DC=com\n", @ui.output
86 assert_equal '', @ui.error
89 def test_execute_private_key
91 @cmd.send :handle_options, %W[--private-key #{@pkey_file_name}]
94 assert_equal '', @ui.output
95 assert_equal '', @ui.error
97 assert_equal File.read(@pkey_file_name),
98 Gem::Security::OPT[:issuer_key].to_s
101 def test_execute_remove
103 @cmd.send :handle_options, %W[--remove rubygems]
106 assert_equal "Removed '/CN=rubygems/DC=example/DC=com'\n", @ui.output
107 assert_equal '', @ui.error
109 assert !File.exist?(@cert_file_name)
112 def test_execute_sign
114 @cmd.send :handle_options, %W[
115 -K #{@pkey_file_name} -C #{@cert_file_name} --sign #{@cert_file_name}
119 assert_equal '', @ui.output
120 assert_equal '', @ui.error
122 # HACK this test sucks
125 end if defined? OpenSSL