Sha256: f6bbebf9b881ccc2df9c8f18f4a8821be2dde4f2bad262820cf82b42af152f65

Contents?: true

Size: 1.65 KB

Versions: 13

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true
require 'rubygems/test_case'
require 'net/https'
require 'rubygems/request'

# = Testing Bundled CA
#
# The tested hosts are explained in detail here: https://github.com/rubygems/rubygems/commit/5e16a5428f973667cabfa07e94ff939e7a83ebd9
#

if ENV["CI"] || ENV["TEST_SSL"]
  class TestBundledCA < Gem::TestCase

    THIS_FILE = File.expand_path __FILE__

    def bundled_certificate_store
      store = OpenSSL::X509::Store.new

      ssl_cert_glob =
        File.expand_path '../../../lib/rubygems/ssl_certs/*/*.pem', THIS_FILE

      Dir[ssl_cert_glob].each do |ssl_cert|
        store.add_file ssl_cert
      end

      store
    end

    def assert_https(host)
      if self.respond_to? :_assertions # minitest <= 4
        self._assertions += 1
      else # minitest >= 5
        self.assertions += 1
      end
      http = Net::HTTP.new(host, 443)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.cert_store = bundled_certificate_store
      http.get('/')
    rescue Errno::ENOENT, Errno::ETIMEDOUT, SocketError
      skip "#{host} seems offline, I can't tell whether ssl would work."
    rescue OpenSSL::SSL::SSLError => e
      # Only fail for certificate verification errors
      if e.message =~ /certificate verify failed/
        flunk "#{host} is not verifiable using the included certificates. Error was: #{e.message}"
      end
      raise
    end

    def test_accessing_rubygems
      assert_https('rubygems.org')
    end

    def test_accessing_fastly
      assert_https('rubygems.global.ssl.fastly.net')
    end

    def test_accessing_new_index
      assert_https('fastly.rubygems.org')
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubygems-update-3.1.4 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.3 test/rubygems/test_bundled_ca.rb
rubygems-update-3.0.8 test/rubygems/test_bundled_ca.rb
rubygems-update-3.0.7 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.2 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.1 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.0 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.0.pre3 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.0.pre2 test/rubygems/test_bundled_ca.rb
rubygems-update-3.1.0.pre1 test/rubygems/test_bundled_ca.rb
rubygems-update-3.0.6 test/rubygems/test_bundled_ca.rb
rubygems-update-3.0.5 test/rubygems/test_bundled_ca.rb
rubygems-update-3.0.4 test/rubygems/test_bundled_ca.rb