1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
|
# frozen_string_literal: true
require_relative "helper"
require "rubygems/commands/cert_command"
unless Gem::HAVE_OPENSSL
warn "Skipping `gem cert` tests. openssl not found."
end
if Gem.java_platform?
warn "Skipping `gem cert` tests on jruby."
end
class TestGemCommandsCertCommand < Gem::TestCase
ALTERNATE_CERT = load_cert "alternate"
EXPIRED_PUBLIC_CERT = load_cert "expired"
ALTERNATE_KEY_FILE = key_path "alternate"
PRIVATE_KEY_FILE = key_path "private"
PRIVATE_EC_KEY_FILE = key_path "private_ec"
PUBLIC_KEY_FILE = key_path "public"
ALTERNATE_CERT_FILE = cert_path "alternate"
CHILD_CERT_FILE = cert_path "child"
PUBLIC_CERT_FILE = cert_path "public"
EXPIRED_PUBLIC_CERT_FILE = cert_path "expired"
def setup
super
@cmd = Gem::Commands::CertCommand.new
@trust_dir = Gem::Security.trust_dir
@cleanup = []
end
def teardown
FileUtils.rm_f(@cleanup)
super
end
def test_certificates_matching
@trust_dir.trust_cert PUBLIC_CERT
@trust_dir.trust_cert ALTERNATE_CERT
matches = @cmd.certificates_matching ""
# HACK: OpenSSL::X509::Certificate#== is Object#==, so do this the hard way
match = matches.next
assert_equal ALTERNATE_CERT.to_pem, match.first.to_pem
assert_equal @trust_dir.cert_path(ALTERNATE_CERT), match.last
match = matches.next
assert_equal PUBLIC_CERT.to_pem, match.first.to_pem
assert_equal @trust_dir.cert_path(PUBLIC_CERT), match.last
assert_raise StopIteration do
matches.next
end
end
def test_certificates_matching_filter
@trust_dir.trust_cert PUBLIC_CERT
@trust_dir.trust_cert ALTERNATE_CERT
matches = @cmd.certificates_matching "alternate"
match = matches.next
assert_equal ALTERNATE_CERT.to_pem, match.first.to_pem
assert_equal @trust_dir.cert_path(ALTERNATE_CERT), match.last
assert_raise StopIteration do
matches.next
end
end
def test_execute_add
@cmd.handle_options %W[--add #{PUBLIC_CERT_FILE}]
use_ui @ui do
@cmd.execute
end
cert_path = @trust_dir.cert_path PUBLIC_CERT
assert_path_exist cert_path
assert_equal "Added '/CN=nobody/DC=example'\n", @ui.output
assert_empty @ui.error
end
def test_execute_add_twice
self.class.cert_path "alternate"
@cmd.handle_options %W[
--add #{PUBLIC_CERT_FILE}
--add #{ALTERNATE_CERT_FILE}
]
use_ui @ui do
@cmd.execute
end
expected = <<-EXPECTED
Added '/CN=nobody/DC=example'
Added '/CN=alternate/DC=example'
EXPECTED
assert_equal expected, @ui.output
assert_empty @ui.error
end
def test_execute_build
passphrase = "Foo bar"
@cmd.handle_options %W[--build nobody@example.com]
@build_ui = Gem::MockGemUi.new "#{passphrase}\n#{passphrase}"
use_ui @build_ui do
@cmd.execute
end
output = @build_ui.output.squeeze("\n").split "\n"
assert_equal "Passphrase for your Private Key: ",
output.shift
assert_equal "Please repeat the passphrase for your Private Key: ",
output.shift
assert_equal "Certificate: #{File.join @tempdir, "gem-public_cert.pem"}",
output.shift
assert_equal "Private Key: #{File.join @tempdir, "gem-private_key.pem"}",
output.shift
assert_equal "Don't forget to move the key file to somewhere private!",
output.shift
assert_empty output
assert_empty @build_ui.error
assert_path_exist File.join(@tempdir, "gem-private_key.pem")
assert_path_exist File.join(@tempdir, "gem-public_cert.pem")
end
def test_execute_build_key_algorithm_ec_key
passphrase = "Foo bar"
@cmd.handle_options %W[--build nobody@example.com --key-algorithm ec]
@build_ui = Gem::MockGemUi.new "#{passphrase}\n#{passphrase}"
use_ui @build_ui do
@cmd.execute
end
output = @build_ui.output.squeeze("\n").split "\n"
assert_equal "Passphrase for your Private Key: ",
output.shift
assert_equal "Please repeat the passphrase for your Private Key: ",
output.shift
assert_equal "Certificate: #{File.join @tempdir, "gem-public_cert.pem"}",
output.shift
assert_equal "Private Key: #{File.join @tempdir, "gem-private_key.pem"}",
output.shift
assert_equal "Don't forget to move the key file to somewhere private!",
output.shift
assert_empty output
assert_empty @build_ui.error
assert_path_exist File.join(@tempdir, "gem-private_key.pem")
cert_path = File.join(@tempdir, "gem-public_cert.pem")
assert_path_exist cert_path
cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
assert cert.public_key.is_a? OpenSSL::PKey::EC
end
def test_execute_build_bad_email_address
passphrase = "Foo bar"
email = "nobody@"
@cmd.handle_options %W[--build #{email}]
@build_ui = Gem::MockGemUi.new "#{passphrase}\n#{passphrase}"
use_ui @build_ui do
e = assert_raise Gem::CommandLineError do
@cmd.execute
end
assert_equal "Invalid email address #{email}",
e.message
assert_path_not_exist File.join(@tempdir, "gem-private_key.pem")
assert_path_not_exist File.join(@tempdir, "gem-public_cert.pem")
end
end
def test_execute_build_expiration_days
passphrase = "Foo bar"
@cmd.handle_options %W[
--build nobody@example.com
--days 26
]
@build_ui = Gem::MockGemUi.new "#{passphrase}\n#{passphrase}"
use_ui @build_ui do
@cmd.execute
end
output = @build_ui.output.squeeze("\n").split "\n"
assert_equal "Passphrase for your Private Key: ",
output.shift
assert_equal "Please repeat the passphrase for your Private Key: ",
output.shift
assert_equal "Certificate: #{File.join @tempdir, "gem-public_cert.pem"}",
output.shift
assert_equal "Private Key: #{File.join @tempdir, "gem-private_key.pem"}",
output.shift
assert_equal "Don't forget to move the key file to somewhere private!",
output.shift
assert_empty output
assert_empty @build_ui.error
assert_path_exist File.join(@tempdir, "gem-private_key.pem")
assert_path_exist File.join(@tempdir, "gem-public_cert.pem")
pem = File.read("#{@tempdir}/gem-public_cert.pem")
cert = OpenSSL::X509::Certificate.new(pem)
test = (cert.not_after - cert.not_before).to_i / (24 * 60 * 60)
assert_equal(test, 26)
end
def test_execute_build_bad_passphrase_confirmation
passphrase = "Foo bar"
passphrase_confirmation = "Fu bar"
@cmd.handle_options %W[--build nobody@example.com]
@build_ui = Gem::MockGemUi.new "#{passphrase}\n#{passphrase_confirmation}"
use_ui @build_ui do
e = assert_raise Gem::CommandLineError do
@cmd.execute
end
output = @build_ui.output.squeeze("\n").split "\n"
assert_equal "Passphrase for your Private Key: ",
output.shift
assert_equal "Please repeat the passphrase for your Private Key: ",
output.shift
assert_empty output
assert_equal "Passphrase and passphrase confirmation don't match",
e.message
end
assert_path_not_exist File.join(@tempdir, "gem-private_key.pem")
assert_path_not_exist File.join(@tempdir, "gem-public_cert.pem")
end
def test_execute_build_key
@cmd.handle_options %W[
--build nobody@example.com
--private-key #{PRIVATE_KEY_FILE}
]
use_ui @ui do
@cmd.execute
end
output = @ui.output.split "\n"
assert_equal "Certificate: #{File.join @tempdir, "gem-public_cert.pem"}",
output.shift
assert_empty output
assert_empty @ui.error
assert_path_exist File.join(@tempdir, "gem-public_cert.pem")
assert_path_not_exist File.join(@tempdir, "gem-private_key.pem")
end
def test_execute_build_encrypted_key
@cmd.handle_options %W[
--build nobody@example.com
--private-key #{ENCRYPTED_PRIVATE_KEY_PATH}
]
use_ui @ui do
@cmd.execute
end
output = @ui.output.split "\n"
assert_equal "Certificate: #{File.join @tempdir, "gem-public_cert.pem"}",
output.shift
assert_empty output
assert_empty @ui.error
assert_path_exist File.join(@tempdir, "gem-public_cert.pem")
end
def test_execute_build_ec_key
@cmd.handle_options %W[
--build nobody@example.com
--private-key #{PRIVATE_EC_KEY_FILE}
]
use_ui @ui do
@cmd.execute
end
output = @ui.output.split "\n"
assert_equal "Certificate: #{File.join @tempdir, "gem-public_cert.pem"}",
output.shift
assert_empty output
assert_empty @ui.error
assert_path_exist File.join(@tempdir, "gem-public_cert.pem")
assert_path_not_exist File.join(@tempdir, "gem-private_key.pem")
end
def test_execute_certificate
use_ui @ui do
@cmd.handle_options %W[--certificate #{PUBLIC_CERT_FILE}]
end
assert_equal "", @ui.output
assert_equal "", @ui.error
assert_equal PUBLIC_CERT.to_pem, @cmd.options[:issuer_cert].to_pem
end
def test_execute_list
@trust_dir.trust_cert PUBLIC_CERT
@trust_dir.trust_cert ALTERNATE_CERT
@cmd.handle_options %W[--list]
use_ui @ui do
@cmd.execute
end
assert_equal "/CN=alternate/DC=example\n/CN=nobody/DC=example\n",
@ui.output
assert_empty @ui.error
end
def test_execute_list_filter
@trust_dir.trust_cert PUBLIC_CERT
@trust_dir.trust_cert ALTERNATE_CERT
@cmd.handle_options %W[--list nobody]
use_ui @ui do
@cmd.execute
end
assert_equal "/CN=nobody/DC=example\n", @ui.output
assert_empty @ui.error
end
def test_execute_private_key
use_ui @ui do
@cmd.send :handle_options, %W[--private-key #{PRIVATE_KEY_FILE}]
end
assert_equal "", @ui.output
assert_equal "", @ui.error
assert_equal PRIVATE_KEY.to_pem, @cmd.options[:key].to_pem
end
def test_execute_encrypted_private_key
use_ui @ui do
@cmd.send :handle_options, %W[--private-key #{ENCRYPTED_PRIVATE_KEY_PATH}]
end
assert_equal "", @ui.output
assert_equal "", @ui.error
assert_equal ENCRYPTED_PRIVATE_KEY.to_pem, @cmd.options[:key].to_pem
end
def test_execute_remove
@trust_dir.trust_cert PUBLIC_CERT
cert_path = @trust_dir.cert_path PUBLIC_CERT
assert_path_exist cert_path
@cmd.handle_options %W[--remove nobody]
use_ui @ui do
@cmd.execute
end
assert_equal "Removed '/CN=nobody/DC=example'\n", @ui.output
assert_equal "", @ui.error
assert_path_not_exist cert_path
end
def test_execute_remove_multiple
@trust_dir.trust_cert PUBLIC_CERT
@trust_dir.trust_cert ALTERNATE_CERT
public_path = @trust_dir.cert_path PUBLIC_CERT
alternate_path = @trust_dir.cert_path ALTERNATE_CERT
assert_path_exist public_path
assert_path_exist alternate_path
@cmd.handle_options %W[--remove example]
use_ui @ui do
@cmd.execute
end
expected = <<-EXPECTED
Removed '/CN=alternate/DC=example'
Removed '/CN=nobody/DC=example'
EXPECTED
assert_equal expected, @ui.output
assert_equal "", @ui.error
assert_path_not_exist public_path
assert_path_not_exist alternate_path
end
def test_execute_remove_twice
@trust_dir.trust_cert PUBLIC_CERT
@trust_dir.trust_cert ALTERNATE_CERT
public_path = @trust_dir.cert_path PUBLIC_CERT
alternate_path = @trust_dir.cert_path ALTERNATE_CERT
assert_path_exist public_path
assert_path_exist alternate_path
@cmd.handle_options %W[--remove nobody --remove alternate]
use_ui @ui do
@cmd.execute
end
expected = <<-EXPECTED
Removed '/CN=nobody/DC=example'
Removed '/CN=alternate/DC=example'
EXPECTED
assert_equal expected, @ui.output
assert_equal "", @ui.error
assert_path_not_exist public_path
assert_path_not_exist alternate_path
end
def test_execute_sign
path = File.join @tempdir, "cert.pem"
Gem::Security.write ALTERNATE_CERT, path, 0o600
assert_equal "/CN=alternate/DC=example", ALTERNATE_CERT.issuer.to_s
@cmd.handle_options %W[
--private-key #{PRIVATE_KEY_FILE}
--certificate #{PUBLIC_CERT_FILE}
--sign #{path}
]
use_ui @ui do
@cmd.execute
end
assert_equal "", @ui.output
assert_equal "", @ui.error
cert = OpenSSL::X509::Certificate.new File.read path
assert_equal "/CN=nobody/DC=example", cert.issuer.to_s
mask = 0o100600 & (~File.umask)
assert_equal mask, File.stat(path).mode unless Gem.win_platform?
end
def test_execute_sign_encrypted_key
path = File.join @tempdir, "cert.pem"
Gem::Security.write ALTERNATE_CERT, path, 0o600
assert_equal "/CN=alternate/DC=example", ALTERNATE_CERT.issuer.to_s
@cmd.handle_options %W[
--private-key #{ENCRYPTED_PRIVATE_KEY_PATH}
--certificate #{PUBLIC_CERT_FILE}
--sign #{path}
]
use_ui @ui do
@cmd.execute
end
assert_equal "", @ui.output
assert_equal "", @ui.error
cert = OpenSSL::X509::Certificate.new File.read path
assert_equal "/CN=nobody/DC=example", cert.issuer.to_s
mask = 0o100600 & (~File.umask)
assert_equal mask, File.stat(path).mode unless Gem.win_platform?
end
def test_execute_sign_default
FileUtils.mkdir_p File.join Gem.user_home, ".gem"
private_key_path = File.join Gem.user_home, ".gem", "gem-private_key.pem"
Gem::Security.write PRIVATE_KEY, private_key_path
public_cert_path = File.join Gem.user_home, ".gem", "gem-public_cert.pem"
Gem::Security.write PUBLIC_CERT, public_cert_path
path = File.join @tempdir, "cert.pem"
Gem::Security.write ALTERNATE_CERT, path, 0o600
assert_equal "/CN=alternate/DC=example", ALTERNATE_CERT.issuer.to_s
@cmd.handle_options %W[--sign #{path}]
use_ui @ui do
@cmd.execute
end
assert_equal "", @ui.output
assert_equal "", @ui.error
cert = OpenSSL::X509::Certificate.new File.read path
assert_equal "/CN=nobody/DC=example", cert.issuer.to_s
mask = 0o100600 & (~File.umask)
assert_equal mask, File.stat(path).mode unless Gem.win_platform?
end
def test_execute_sign_default_encrypted_key
FileUtils.mkdir_p File.join(Gem.user_home, ".gem")
private_key_path = File.join Gem.user_home, ".gem", "gem-private_key.pem"
Gem::Security.write ENCRYPTED_PRIVATE_KEY, private_key_path, 0o600, PRIVATE_KEY_PASSPHRASE
public_cert_path = File.join Gem.user_home, ".gem", "gem-public_cert.pem"
Gem::Security.write PUBLIC_CERT, public_cert_path
path = File.join @tempdir, "cert.pem"
Gem::Security.write ALTERNATE_CERT, path, 0o600
assert_equal "/CN=alternate/DC=example", ALTERNATE_CERT.issuer.to_s
@cmd.handle_options %W[--sign #{path}]
use_ui @ui do
@cmd.execute
end
assert_equal "", @ui.output
assert_equal "", @ui.error
cert = OpenSSL::X509::Certificate.new File.read path
assert_equal "/CN=nobody/DC=example", cert.issuer.to_s
mask = 0o100600 & (~File.umask)
assert_equal mask, File.stat(path).mode unless Gem.win_platform?
end
def test_execute_sign_no_cert
FileUtils.mkdir_p File.join Gem.user_home, ".gem"
private_key_path = File.join Gem.user_home, ".gem", "gem-private_key.pem"
Gem::Security.write PRIVATE_KEY, private_key_path
path = File.join @tempdir, "cert.pem"
Gem::Security.write ALTERNATE_CERT, path, 0o600
assert_equal "/CN=alternate/DC=example", ALTERNATE_CERT.issuer.to_s
@cmd.handle_options %W[--sign #{path}]
use_ui @ui do
assert_raise Gem::MockGemUi::TermError do
@cmd.execute
end
end
assert_equal "", @ui.output
expected = <<-EXPECTED
ERROR: --certificate not specified and ~/.gem/gem-public_cert.pem does not exist
EXPECTED
assert_equal expected, @ui.error
end
def test_execute_sign_no_key
FileUtils.mkdir_p File.join Gem.user_home, ".gem"
public_cert_path = File.join Gem.user_home, ".gem", "gem-public_cert.pem"
Gem::Security.write PUBLIC_CERT, public_cert_path
path = File.join @tempdir, "cert.pem"
Gem::Security.write ALTERNATE_CERT, path, 0o600
assert_equal "/CN=alternate/DC=example", ALTERNATE_CERT.issuer.to_s
@cmd.handle_options %W[--sign #{path}]
use_ui @ui do
assert_raise Gem::MockGemUi::TermError do
@cmd.execute
end
end
assert_equal "", @ui.output
expected = <<-EXPECTED
ERROR: --private-key not specified and ~/.gem/gem-private_key.pem does not exist
EXPECTED
assert_equal expected, @ui.error
end
def test_execute_re_sign
gem_path = File.join Gem.user_home, ".gem"
Dir.mkdir gem_path
path = File.join @tempdir, "cert.pem"
Gem::Security.write EXPIRED_PUBLIC_CERT, path, 0o600
assert_equal "/CN=nobody/DC=example", EXPIRED_PUBLIC_CERT.issuer.to_s
tmp_expired_cert_file = File.join(Dir.tmpdir, File.basename(EXPIRED_PUBLIC_CERT_FILE))
@cleanup << tmp_expired_cert_file
File.write(tmp_expired_cert_file, File.read(EXPIRED_PUBLIC_CERT_FILE))
@cmd.handle_options %W[
--private-key #{PRIVATE_KEY_FILE}
--certificate #{tmp_expired_cert_file}
--re-sign
]
use_ui @ui do
@cmd.execute
end
expected_path = File.join(gem_path, "#{File.basename(tmp_expired_cert_file)}.expired")
assert_include(@ui.output, "INFO: Your certificate #{tmp_expired_cert_file} has been re-signed\n")
assert_match(
/INFO: Your expired certificate will be located at: #{Regexp.quote(expected_path)}\.[0-9]+/,
@ui.output
)
assert_equal "", @ui.error
end
def test_execute_re_sign_with_cert_expiration_length_days
gem_path = File.join Gem.user_home, ".gem"
Dir.mkdir gem_path
path = File.join @tempdir, "cert.pem"
Gem::Security.write EXPIRED_PUBLIC_CERT, path, 0o600
assert_equal "/CN=nobody/DC=example", EXPIRED_PUBLIC_CERT.issuer.to_s
tmp_expired_cert_file = File.join(Dir.tmpdir, File.basename(EXPIRED_PUBLIC_CERT_FILE))
@cleanup << tmp_expired_cert_file
File.write(tmp_expired_cert_file, File.read(EXPIRED_PUBLIC_CERT_FILE))
@cmd.handle_options %W[
--private-key #{PRIVATE_KEY_FILE}
--certificate #{tmp_expired_cert_file}
--re-sign
]
Gem.configuration.cert_expiration_length_days = 28
use_ui @ui do
@cmd.execute
end
re_signed_cert = OpenSSL::X509::Certificate.new(File.read(tmp_expired_cert_file))
cert_days_to_expire = (re_signed_cert.not_after - re_signed_cert.not_before).to_i / (24 * 60 * 60)
assert_equal(28, cert_days_to_expire)
assert_equal "", @ui.error
end
def test_handle_options
@cmd.handle_options %W[
--add #{PUBLIC_CERT_FILE}
--add #{ALTERNATE_CERT_FILE}
--remove nobody
--remove example
--list
--list example
--build nobody@example
--build other@example
]
assert_equal [PUBLIC_CERT.to_pem, ALTERNATE_CERT.to_pem],
@cmd.options[:add].map(&:to_pem)
assert_equal %w[nobody example], @cmd.options[:remove]
assert_equal %w[nobody@example other@example],
@cmd.options[:build].map(&:to_s)
assert_equal ["", "example"], @cmd.options[:list]
end
def test_handle_options_add_bad
nonexistent = File.join @tempdir, "nonexistent"
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--add #{nonexistent}]
end
assert_equal "invalid argument: --add #{nonexistent}: does not exist",
e.message
bad = File.join @tempdir, "bad"
FileUtils.touch bad
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--add #{bad}]
end
assert_equal "invalid argument: --add #{bad}: invalid X509 certificate",
e.message
end
def test_handle_options_certificate
nonexistent = File.join @tempdir, "nonexistent"
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--certificate #{nonexistent}]
end
assert_equal "invalid argument: --certificate #{nonexistent}: does not exist",
e.message
bad = File.join @tempdir, "bad"
FileUtils.touch bad
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--certificate #{bad}]
end
assert_equal "invalid argument: " \
"--certificate #{bad}: invalid X509 certificate",
e.message
end
def test_handle_options_key_bad
nonexistent = File.join @tempdir, "nonexistent"
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--private-key #{nonexistent}]
end
assert_equal "invalid argument: " \
"--private-key #{nonexistent}: does not exist",
e.message
bad = File.join @tempdir, "bad"
FileUtils.touch bad
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--private-key #{bad}]
end
assert_equal "invalid argument: --private-key #{bad}: invalid RSA, DSA, or EC key",
e.message
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[--private-key #{PUBLIC_KEY_FILE}]
end
assert_equal "invalid argument: " \
"--private-key #{PUBLIC_KEY_FILE}: private key not found",
e.message
end
def test_handle_options_sign
@cmd.handle_options %W[
--private-key #{ALTERNATE_KEY_FILE}
--private-key #{PRIVATE_KEY_FILE}
--certificate #{ALTERNATE_CERT_FILE}
--certificate #{PUBLIC_CERT_FILE}
--sign #{ALTERNATE_CERT_FILE}
--sign #{CHILD_CERT_FILE}
]
assert_equal PRIVATE_KEY.to_pem, @cmd.options[:key].to_pem
assert_equal PUBLIC_CERT.to_pem, @cmd.options[:issuer_cert].to_pem
assert_equal [ALTERNATE_CERT_FILE, CHILD_CERT_FILE], @cmd.options[:sign]
end
def test_handle_options_sign_encrypted_key
@cmd.handle_options %W[
--private-key #{ALTERNATE_KEY_FILE}
--private-key #{ENCRYPTED_PRIVATE_KEY_PATH}
--certificate #{ALTERNATE_CERT_FILE}
--certificate #{PUBLIC_CERT_FILE}
--sign #{ALTERNATE_CERT_FILE}
--sign #{CHILD_CERT_FILE}
]
assert_equal ENCRYPTED_PRIVATE_KEY.to_pem, @cmd.options[:key].to_pem
assert_equal PUBLIC_CERT.to_pem, @cmd.options[:issuer_cert].to_pem
assert_equal [ALTERNATE_CERT_FILE, CHILD_CERT_FILE], @cmd.options[:sign]
end
def test_handle_options_sign_nonexistent
nonexistent = File.join @tempdir, "nonexistent"
e = assert_raise Gem::OptionParser::InvalidArgument do
@cmd.handle_options %W[
--private-key #{ALTERNATE_KEY_FILE}
--certificate #{ALTERNATE_CERT_FILE}
--sign #{nonexistent}
]
end
assert_equal "invalid argument: --sign #{nonexistent}: does not exist",
e.message
end
end if Gem::HAVE_OPENSSL && !Gem.java_platform?
|