2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2022-07-17 04:08:51 -04:00
|
|
|
require_relative "helper"
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
require "webrick"
|
|
|
|
require "webrick/https" if Gem::HAVE_OPENSSL
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
unless Gem::HAVE_OPENSSL
|
2022-07-17 04:08:51 -04:00
|
|
|
warn "Skipping Gem::RemoteFetcher tests. openssl not found."
|
2018-07-04 08:45:46 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
require "rubygems/remote_fetcher"
|
|
|
|
require "rubygems/package"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
# = Testing Proxy Settings
|
|
|
|
#
|
|
|
|
# These tests check the proper proxy server settings by running two
|
|
|
|
# web servers. The web server at http://localhost:#{SERVER_PORT}
|
|
|
|
# represents the normal gem server and returns a gemspec with a rake
|
|
|
|
# version of 0.4.11. The web server at http://localhost:#{PROXY_PORT}
|
|
|
|
# represents the proxy server and returns a different dataset where
|
|
|
|
# rake has version 0.4.2. This allows us to detect which server is
|
|
|
|
# returning the data.
|
|
|
|
#
|
|
|
|
# Note that the proxy server is not a *real* proxy server. But our
|
|
|
|
# software doesn't really care, as long as we hit the proxy URL when a
|
|
|
|
# proxy is configured.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
class TestGemRemoteFetcher < Gem::TestCase
|
2007-11-10 02:48:56 -05:00
|
|
|
include Gem::DefaultUserInteraction
|
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
SERVER_DATA = <<-EOY.freeze
|
2007-11-10 02:48:56 -05:00
|
|
|
--- !ruby/object:Gem::Cache
|
|
|
|
gems:
|
|
|
|
rake-0.4.11: !ruby/object:Gem::Specification
|
|
|
|
rubygems_version: "0.7"
|
|
|
|
specification_version: 1
|
|
|
|
name: rake
|
|
|
|
version: !ruby/object:Gem::Version
|
|
|
|
version: 0.4.11
|
|
|
|
date: 2004-11-12
|
|
|
|
summary: Ruby based make-like utility.
|
|
|
|
require_paths:
|
|
|
|
- lib
|
|
|
|
author: Jim Weirich
|
|
|
|
email: jim@weirichhouse.org
|
|
|
|
homepage: http://rake.rubyforge.org
|
|
|
|
description: Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax.
|
|
|
|
autorequire:
|
|
|
|
bindir: bin
|
|
|
|
has_rdoc: true
|
|
|
|
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
|
|
requirements:
|
|
|
|
-
|
|
|
|
- ">"
|
|
|
|
- !ruby/object:Gem::Version
|
|
|
|
version: 0.0.0
|
|
|
|
version:
|
|
|
|
platform: ruby
|
|
|
|
files:
|
|
|
|
- README
|
|
|
|
test_files: []
|
|
|
|
library_stubs:
|
|
|
|
rdoc_options:
|
|
|
|
extra_rdoc_files:
|
|
|
|
executables:
|
|
|
|
- rake
|
|
|
|
extensions: []
|
|
|
|
requirements: []
|
|
|
|
dependencies: []
|
|
|
|
EOY
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
PROXY_DATA = SERVER_DATA.gsub(/0.4.11/, "0.4.2")
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2015-07-28 16:02:38 -04:00
|
|
|
# Generated via:
|
|
|
|
# x = OpenSSL::PKey::DH.new(2048) # wait a while...
|
|
|
|
# x.to_s => pem
|
2020-04-20 12:32:51 -04:00
|
|
|
TEST_KEY_DH2048 = OpenSSL::PKey::DH.new <<-_END_OF_PEM_
|
2015-07-28 16:02:38 -04:00
|
|
|
-----BEGIN DH PARAMETERS-----
|
|
|
|
MIIBCAKCAQEA3Ze2EHSfYkZLUn557torAmjBgPsqzbodaRaGZtgK1gEU+9nNJaFV
|
|
|
|
G1JKhmGUiEDyIW7idsBpe4sX/Wqjnp48Lr8IeI/SlEzLdoGpf05iRYXC8Cm9o8aM
|
|
|
|
cfmVgoSEAo9YLBpzoji2jHkO7Q5IPt4zxbTdlmmGFLc/GO9q7LGHhC+rcMcNTGsM
|
|
|
|
49AnILNn49pq4Y72jSwdmvq4psHZwwFBbPwLdw6bLUDDCN90jfqvYt18muwUxDiN
|
|
|
|
NP0fuvVAIB158VnQ0liHSwcl6+9vE1mL0Jo/qEXQxl0+UdKDjaGfTsn6HIrwTnmJ
|
|
|
|
PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg==
|
|
|
|
-----END DH PARAMETERS-----
|
2020-04-20 12:32:51 -04:00
|
|
|
_END_OF_PEM_
|
2015-07-28 16:02:38 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def setup
|
2015-10-26 04:47:18 -04:00
|
|
|
@proxies = %w[https_proxy http_proxy HTTP_PROXY http_proxy_user HTTP_PROXY_USER http_proxy_pass HTTP_PROXY_PASS no_proxy NO_PROXY]
|
2012-12-15 02:29:43 -05:00
|
|
|
@old_proxies = @proxies.map {|k| ENV[k] }
|
|
|
|
@proxies.each {|k| ENV[k] = nil }
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
super
|
2020-04-10 15:59:13 -04:00
|
|
|
start_servers
|
|
|
|
self.enable_yaml = true
|
|
|
|
self.enable_zip = false
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
base_server_uri = "http://localhost:#{normal_server_port}"
|
|
|
|
@proxy_uri = "http://localhost:#{proxy_server_port}"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
@server_uri = base_server_uri + "/yaml"
|
|
|
|
@server_z_uri = base_server_uri + "/yaml.Z"
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
@cache_dir = File.join @gemhome, "cache"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-03-07 03:44:45 -05:00
|
|
|
# TODO: why does the remote fetcher need it written to disk?
|
2022-07-17 04:08:51 -04:00
|
|
|
@a1, @a1_gem = util_gem "a", "1" do |s|
|
|
|
|
s.executables << "a_bin"
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
@a1.loaded_from = File.join(@gemhome, "specifications", @a1.full_name)
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
Gem::RemoteFetcher.fetcher = nil
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_ui = Gem::MockGemUi.new
|
2008-06-25 22:06:00 -04:00
|
|
|
@fetcher = Gem::RemoteFetcher.fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
def teardown
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher.close_all
|
2020-04-10 15:59:13 -04:00
|
|
|
stop_servers
|
2008-10-25 18:58:43 -04:00
|
|
|
super
|
|
|
|
Gem.configuration[:http_proxy] = nil
|
2012-12-15 02:29:43 -05:00
|
|
|
@proxies.each_with_index {|k, i| ENV[k] = @old_proxies[i] }
|
2008-10-25 18:58:43 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_self_fetcher
|
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
2008-10-25 18:58:43 -04:00
|
|
|
refute_nil fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_kind_of Gem::RemoteFetcher, fetcher
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_self_fetcher_with_proxy
|
2022-07-17 04:08:51 -04:00
|
|
|
proxy_uri = "http://proxy.example.com"
|
2007-11-10 02:48:56 -05:00
|
|
|
Gem.configuration[:http_proxy] = proxy_uri
|
2008-06-25 22:06:00 -04:00
|
|
|
Gem::RemoteFetcher.fetcher = nil
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
refute_nil fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_kind_of Gem::RemoteFetcher, fetcher
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_equal proxy_uri, fetcher.instance_variable_get(:@proxy).to_s
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
def test_fetch_path_bad_uri
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise ArgumentError do
|
2020-01-31 21:14:04 -05:00
|
|
|
@fetcher.fetch_path("gems.example.com/yaml", nil, true)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "uri scheme is invalid: nil", e.message
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_proxy
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2008-06-25 22:06:00 -04:00
|
|
|
assert_data_from_server @fetcher.fetch_path(@server_uri)
|
2020-01-31 21:14:04 -05:00
|
|
|
response = @fetcher.fetch_path(@server_uri, nil, true)
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal SERVER_DATA.size, response["content-length"].to_i
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-06 00:13:08 -05:00
|
|
|
def test_cache_update_path
|
2022-07-17 04:08:51 -04:00
|
|
|
uri = URI "http://example/file"
|
|
|
|
path = File.join @tempdir, "file"
|
2012-12-06 00:13:08 -05:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
fetcher = util_fuck_with_fetcher "hello"
|
2012-12-06 00:13:08 -05:00
|
|
|
|
|
|
|
data = fetcher.cache_update_path uri, path
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "hello", data
|
2012-12-06 00:13:08 -05:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "hello", File.read(path)
|
2012-12-06 00:13:08 -05:00
|
|
|
end
|
|
|
|
|
2021-12-13 10:53:50 -05:00
|
|
|
def test_cache_update_path_with_utf8_internal_encoding
|
2022-07-17 04:08:51 -04:00
|
|
|
with_internal_encoding("UTF-8") do
|
|
|
|
uri = URI "http://example/file"
|
|
|
|
path = File.join @tempdir, "file"
|
2021-12-13 10:53:50 -05:00
|
|
|
data = String.new("\xC8").force_encoding(Encoding::BINARY)
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher data
|
|
|
|
|
|
|
|
written_data = fetcher.cache_update_path uri, path
|
|
|
|
|
|
|
|
assert_equal data, written_data
|
|
|
|
assert_equal data, File.binread(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-06 00:13:08 -05:00
|
|
|
def test_cache_update_path_no_update
|
2022-07-17 04:08:51 -04:00
|
|
|
uri = URI "http://example/file"
|
|
|
|
path = File.join @tempdir, "file"
|
2012-12-06 00:13:08 -05:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
fetcher = util_fuck_with_fetcher "hello"
|
2012-12-06 00:13:08 -05:00
|
|
|
|
|
|
|
data = fetcher.cache_update_path uri, path, false
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "hello", data
|
2012-12-06 00:13:08 -05:00
|
|
|
|
2020-05-25 08:05:45 -04:00
|
|
|
assert_path_not_exist path
|
2012-12-06 00:13:08 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def util_fuck_with_fetcher(data, blow = false)
|
2008-03-31 18:40:06 -04:00
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
|
|
|
fetcher.instance_variable_set :@test_data, data
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless blow
|
|
|
|
def fetcher.fetch_path(arg, *rest)
|
2008-03-31 18:40:06 -04:00
|
|
|
@test_arg = arg
|
|
|
|
@test_data
|
|
|
|
end
|
|
|
|
else
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetcher.fetch_path(arg, *rest)
|
2008-03-31 18:40:06 -04:00
|
|
|
# OMG I'm such an ass
|
|
|
|
class << self; remove_method :fetch_path; end
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.fetch_path(arg, *rest)
|
2008-03-31 18:40:06 -04:00
|
|
|
@test_arg = arg
|
|
|
|
@test_data
|
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
raise Gem::RemoteFetcher::FetchError.new("haha!", "")
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_download
|
|
|
|
a1_data = nil
|
2022-07-17 04:08:51 -04:00
|
|
|
File.open @a1_gem, "rb" do |fp|
|
2008-03-31 18:40:06 -04:00
|
|
|
a1_data = fp.read
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher a1_data
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
a1_cache_gem = @a1.cache_file
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal a1_cache_gem, fetcher.download(@a1, "http://gems.example.com")
|
2008-03-31 18:40:06 -04:00
|
|
|
assert_equal("http://gems.example.com/gems/a-1.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
assert File.exist?(a1_cache_gem)
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def test_download_with_auth
|
|
|
|
a1_data = nil
|
2022-07-17 04:08:51 -04:00
|
|
|
File.open @a1_gem, "rb" do |fp|
|
2014-09-13 23:30:02 -04:00
|
|
|
a1_data = fp.read
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher a1_data
|
|
|
|
|
|
|
|
a1_cache_gem = @a1.cache_file
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal a1_cache_gem, fetcher.download(@a1, "http://user:password@gems.example.com")
|
2014-09-13 23:30:02 -04:00
|
|
|
assert_equal("http://user:password@gems.example.com/gems/a-1.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
assert File.exist?(a1_cache_gem)
|
|
|
|
end
|
|
|
|
|
2021-08-21 11:40:21 -04:00
|
|
|
def test_download_with_token
|
|
|
|
a1_data = nil
|
2022-07-17 04:08:51 -04:00
|
|
|
File.open @a1_gem, "rb" do |fp|
|
2021-08-21 11:40:21 -04:00
|
|
|
a1_data = fp.read
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher a1_data
|
|
|
|
|
|
|
|
a1_cache_gem = @a1.cache_file
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal a1_cache_gem, fetcher.download(@a1, "http://token@gems.example.com")
|
2021-08-21 11:40:21 -04:00
|
|
|
assert_equal("http://token@gems.example.com/gems/a-1.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
assert File.exist?(a1_cache_gem)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_download_with_x_oauth_basic
|
|
|
|
a1_data = nil
|
2022-07-17 04:08:51 -04:00
|
|
|
File.open @a1_gem, "rb" do |fp|
|
2021-08-21 11:40:21 -04:00
|
|
|
a1_data = fp.read
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher a1_data
|
|
|
|
|
|
|
|
a1_cache_gem = @a1.cache_file
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal a1_cache_gem, fetcher.download(@a1, "http://token:x-oauth-basic@gems.example.com")
|
2021-08-21 11:40:21 -04:00
|
|
|
assert_equal("http://token:x-oauth-basic@gems.example.com/gems/a-1.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
assert File.exist?(a1_cache_gem)
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def test_download_with_encoded_auth
|
|
|
|
a1_data = nil
|
2022-07-17 04:08:51 -04:00
|
|
|
File.open @a1_gem, "rb" do |fp|
|
2014-09-13 23:30:02 -04:00
|
|
|
a1_data = fp.read
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher a1_data
|
|
|
|
|
|
|
|
a1_cache_gem = @a1.cache_file
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal a1_cache_gem, fetcher.download(@a1, "http://user:%25pas%25sword@gems.example.com")
|
2014-09-13 23:30:02 -04:00
|
|
|
assert_equal("http://user:%25pas%25sword@gems.example.com/gems/a-1.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
assert File.exist?(a1_cache_gem)
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def test_download_cached
|
|
|
|
FileUtils.mv @a1_gem, @cache_dir
|
|
|
|
|
|
|
|
inst = Gem::RemoteFetcher.fetcher
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal @a1.cache_file, inst.download(@a1, "http://gems.example.com")
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_download_local
|
|
|
|
FileUtils.mv @a1_gem, @tempdir
|
2010-02-21 21:52:35 -05:00
|
|
|
local_path = File.join @tempdir, @a1.file_name
|
2008-03-31 18:40:06 -04:00
|
|
|
inst = nil
|
|
|
|
|
|
|
|
Dir.chdir @tempdir do
|
|
|
|
inst = Gem::RemoteFetcher.fetcher
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
assert_equal @a1.cache_file, inst.download(@a1, local_path)
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def test_download_local_space
|
2022-07-17 04:08:51 -04:00
|
|
|
space_path = File.join @tempdir, "space path"
|
2009-06-09 17:38:59 -04:00
|
|
|
FileUtils.mkdir space_path
|
|
|
|
FileUtils.mv @a1_gem, space_path
|
2010-02-21 21:52:35 -05:00
|
|
|
local_path = File.join space_path, @a1.file_name
|
2009-06-09 17:38:59 -04:00
|
|
|
inst = nil
|
|
|
|
|
|
|
|
Dir.chdir @tempdir do
|
|
|
|
inst = Gem::RemoteFetcher.fetcher
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
assert_equal @a1.cache_file, inst.download(@a1, local_path)
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def test_download_install_dir
|
2022-07-17 04:08:51 -04:00
|
|
|
a1_data = File.open @a1_gem, "rb" do |fp|
|
2011-05-31 23:45:05 -04:00
|
|
|
fp.read
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher a1_data
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
install_dir = File.join @tempdir, "more_gems"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
a1_cache_gem = File.join install_dir, "cache", @a1.file_name
|
2008-06-25 22:06:00 -04:00
|
|
|
FileUtils.mkdir_p(File.dirname(a1_cache_gem))
|
2022-07-17 04:08:51 -04:00
|
|
|
actual = fetcher.download(@a1, "http://gems.example.com", install_dir)
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
assert_equal a1_cache_gem, actual
|
|
|
|
assert_equal("http://gems.example.com/gems/a-1.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
|
|
|
|
assert File.exist?(a1_cache_gem)
|
|
|
|
end
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
unless win_platform? || Process.uid.zero? # File.chmod doesn't work
|
2008-03-31 18:40:06 -04:00
|
|
|
def test_download_local_read_only
|
|
|
|
FileUtils.mv @a1_gem, @tempdir
|
2010-02-21 21:52:35 -05:00
|
|
|
local_path = File.join @tempdir, @a1.file_name
|
2008-03-31 18:40:06 -04:00
|
|
|
inst = nil
|
2011-05-31 23:45:05 -04:00
|
|
|
FileUtils.chmod 0555, @a1.cache_dir
|
2020-08-04 01:25:59 -04:00
|
|
|
FileUtils.mkdir_p File.join(Gem.user_dir, "cache") rescue nil
|
|
|
|
FileUtils.chmod 0555, File.join(Gem.user_dir, "cache")
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
Dir.chdir @tempdir do
|
|
|
|
inst = Gem::RemoteFetcher.fetcher
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal(File.join(@tempdir, @a1.file_name),
|
|
|
|
inst.download(@a1, local_path))
|
2008-03-31 18:40:06 -04:00
|
|
|
ensure
|
2020-08-04 01:25:59 -04:00
|
|
|
FileUtils.chmod 0755, File.join(Gem.user_dir, "cache")
|
2011-05-31 23:45:05 -04:00
|
|
|
FileUtils.chmod 0755, @a1.cache_dir
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def test_download_read_only
|
2011-05-31 23:45:05 -04:00
|
|
|
FileUtils.chmod 0555, @a1.cache_dir
|
|
|
|
FileUtils.chmod 0555, @gemhome
|
2008-06-25 22:06:00 -04:00
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher File.read(@a1_gem)
|
2022-07-17 04:08:51 -04:00
|
|
|
fetcher.download(@a1, "http://gems.example.com")
|
2011-05-31 23:45:05 -04:00
|
|
|
a1_cache_gem = File.join Gem.user_dir, "cache", @a1.file_name
|
|
|
|
assert File.exist? a1_cache_gem
|
2008-06-25 22:06:00 -04:00
|
|
|
ensure
|
2011-05-31 23:45:05 -04:00
|
|
|
FileUtils.chmod 0755, @gemhome
|
|
|
|
FileUtils.chmod 0755, @a1.cache_dir
|
2008-06-25 22:06:00 -04:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_download_platform_legacy
|
2022-07-17 04:08:51 -04:00
|
|
|
original_platform = "old-platform"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
e1, e1_gem = util_gem "e", "1" do |s|
|
2008-03-31 18:40:06 -04:00
|
|
|
s.platform = Gem::Platform::CURRENT
|
|
|
|
s.instance_variable_set :@original_platform, original_platform
|
|
|
|
end
|
2022-07-17 04:08:51 -04:00
|
|
|
e1.loaded_from = File.join(@gemhome, "specifications", e1.full_name)
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
e1_data = nil
|
2022-07-17 04:08:51 -04:00
|
|
|
File.open e1_gem, "rb" do |fp|
|
2008-03-31 18:40:06 -04:00
|
|
|
e1_data = fp.read
|
|
|
|
end
|
|
|
|
|
|
|
|
fetcher = util_fuck_with_fetcher e1_data, :blow_chunks
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
e1_cache_gem = e1.cache_file
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal e1_cache_gem, fetcher.download(e1, "http://gems.example.com")
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
assert_equal("http://gems.example.com/gems/#{e1.original_name}.gem",
|
|
|
|
fetcher.instance_variable_get(:@test_arg).to_s)
|
|
|
|
assert File.exist?(e1_cache_gem)
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_download_same_file
|
|
|
|
FileUtils.mv @a1_gem, @tempdir
|
|
|
|
local_path = File.join @tempdir, @a1.file_name
|
|
|
|
inst = nil
|
|
|
|
|
|
|
|
Dir.chdir @tempdir do
|
|
|
|
inst = Gem::RemoteFetcher.fetcher
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
cache_path = @a1.cache_file
|
2010-02-21 21:52:35 -05:00
|
|
|
FileUtils.mv local_path, cache_path
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
gem = Gem::Package.new cache_path
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
assert_equal cache_path, inst.download(gem.spec, cache_path)
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def test_download_unsupported
|
|
|
|
inst = Gem::RemoteFetcher.fetcher
|
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise ArgumentError do
|
2022-07-17 04:08:51 -04:00
|
|
|
inst.download @a1, "ftp://gems.rubyforge.org"
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "unsupported URI scheme ftp", e.message
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2011-08-04 21:00:01 -04:00
|
|
|
def test_download_to_cache
|
2022-07-17 04:08:51 -04:00
|
|
|
@a2, @a2_gem = util_gem "a", "2"
|
2011-08-04 21:00:01 -04:00
|
|
|
|
|
|
|
util_setup_spec_fetcher @a1, @a2
|
|
|
|
@fetcher.instance_variable_set :@a1, @a1
|
|
|
|
@fetcher.instance_variable_set :@a2, @a2
|
2018-11-21 05:20:47 -05:00
|
|
|
def @fetcher.fetch_path(uri, mtime = nil, head = false)
|
2011-08-04 21:00:01 -04:00
|
|
|
case uri.request_uri
|
|
|
|
when /#{@a1.spec_name}/ then
|
|
|
|
Gem.deflate Marshal.dump @a1
|
|
|
|
when /#{@a2.spec_name}/ then
|
|
|
|
Gem.deflate Marshal.dump @a2
|
|
|
|
else
|
|
|
|
uri.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
gem = Gem::RemoteFetcher.fetcher.download_to_cache dep "a"
|
2011-08-04 21:00:01 -04:00
|
|
|
|
|
|
|
assert_equal @a2.file_name, File.basename(gem)
|
|
|
|
end
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def test_fetch_path_gzip
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def fetcher.fetch_http(uri, mtime, head = nil)
|
2022-07-17 04:08:51 -04:00
|
|
|
Gem::Util.gzip "foo"
|
2008-06-25 22:06:00 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "foo", fetcher.fetch_path(@uri + "foo.gz")
|
2008-06-25 22:06:00 -04:00
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
def test_fetch_path_gzip_unmodified
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def fetcher.fetch_http(uri, mtime, head = nil)
|
2008-09-25 06:13:50 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_nil fetcher.fetch_path(@uri + "foo.gz", Time.at(0))
|
2008-09-25 06:13:50 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_fetch_path_io_error
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def fetcher.fetch_http(*)
|
2008-06-25 22:06:00 -04:00
|
|
|
raise EOFError
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://example.com/uri"
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2011-05-31 23:45:05 -04:00
|
|
|
fetcher.fetch_path url
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
assert_equal "EOFError: EOFError (#{url})", e.message
|
|
|
|
assert_equal url, e.uri
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_path_socket_error
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def fetcher.fetch_http(uri, mtime, head = nil)
|
2008-06-25 22:06:00 -04:00
|
|
|
raise SocketError
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://example.com/uri"
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2011-05-31 23:45:05 -04:00
|
|
|
fetcher.fetch_path url
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
assert_equal "SocketError: SocketError (#{url})", e.message
|
|
|
|
assert_equal url, e.uri
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_path_system_call_error
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def fetcher.fetch_http(uri, mtime = nil, head = nil)
|
2022-07-17 04:08:51 -04:00
|
|
|
raise Errno::ECONNREFUSED, "connect(2)"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://example.com/uri"
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2011-05-31 23:45:05 -04:00
|
|
|
fetcher.fetch_path url
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2020-03-24 14:51:43 -04:00
|
|
|
assert_match %r{ECONNREFUSED:.*connect\(2\) \(#{Regexp.escape url}\)\z},
|
2007-11-10 02:48:56 -05:00
|
|
|
e.message
|
2011-05-31 23:45:05 -04:00
|
|
|
assert_equal url, e.uri
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2021-04-14 23:47:18 -04:00
|
|
|
def test_fetch_path_timeout_error
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
|
|
|
@fetcher = fetcher
|
|
|
|
|
|
|
|
def fetcher.fetch_http(uri, mtime = nil, head = nil)
|
2022-07-17 04:08:51 -04:00
|
|
|
raise Timeout::Error, "timed out"
|
2021-04-14 23:47:18 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://example.com/uri"
|
2021-04-14 23:47:18 -04:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2021-04-14 23:47:18 -04:00
|
|
|
fetcher.fetch_path url
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match %r{Timeout::Error: timed out \(#{Regexp.escape url}\)\z},
|
|
|
|
e.message
|
|
|
|
assert_equal url, e.uri
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_path_getaddrinfo_error
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
|
|
|
@fetcher = fetcher
|
|
|
|
|
|
|
|
def fetcher.fetch_http(uri, mtime = nil, head = nil)
|
2022-07-17 04:08:51 -04:00
|
|
|
raise SocketError, "getaddrinfo: nodename nor servname provided"
|
2021-04-14 23:47:18 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://example.com/uri"
|
2021-04-14 23:47:18 -04:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2021-04-14 23:47:18 -04:00
|
|
|
fetcher.fetch_path url
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match %r{SocketError: getaddrinfo: nodename nor servname provided \(#{Regexp.escape url}\)\z},
|
|
|
|
e.message
|
|
|
|
assert_equal url, e.uri
|
|
|
|
end
|
|
|
|
|
2018-12-14 03:01:40 -05:00
|
|
|
def test_fetch_path_openssl_ssl_sslerror
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
|
|
|
@fetcher = fetcher
|
|
|
|
|
|
|
|
def fetcher.fetch_http(uri, mtime = nil, head = nil)
|
|
|
|
raise OpenSSL::SSL::SSLError
|
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://example.com/uri"
|
2018-12-14 03:01:40 -05:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2018-12-14 03:01:40 -05:00
|
|
|
fetcher.fetch_path url
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "OpenSSL::SSL::SSLError: OpenSSL::SSL::SSLError (#{url})", e.message
|
|
|
|
assert_equal url, e.uri
|
|
|
|
end
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def test_fetch_path_unmodified
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def fetcher.fetch_http(uri, mtime, head = nil)
|
2008-09-25 06:13:50 -04:00
|
|
|
nil
|
2008-06-25 22:06:00 -04:00
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
assert_nil fetcher.fetch_path(URI.parse(@gem_repo), Time.at(0))
|
2008-06-25 22:06:00 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_implicit_no_proxy
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV["http_proxy"] = "http://fakeurl:12345"
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new :no_proxy
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_data_from_server fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_implicit_proxy
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV["http_proxy"] = @proxy_uri
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_data_from_proxy fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_implicit_upper_case_proxy
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV["HTTP_PROXY"] = @proxy_uri
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_data_from_proxy fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_implicit_proxy_no_env
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_data_from_server fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def test_fetch_http
|
2008-03-31 18:40:06 -04:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://gems.example.com/redirect"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def fetcher.request(uri, request_class, last_modified = nil)
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://gems.example.com/redirect"
|
2018-11-21 05:20:47 -05:00
|
|
|
unless defined? @requested
|
2008-03-31 18:40:06 -04:00
|
|
|
@requested = true
|
2008-09-25 06:13:50 -04:00
|
|
|
res = Net::HTTPMovedPermanently.new nil, 301, nil
|
2022-07-17 04:08:51 -04:00
|
|
|
res.add_field "Location", url
|
2008-03-31 18:40:06 -04:00
|
|
|
res
|
|
|
|
else
|
|
|
|
res = Net::HTTPOK.new nil, 200, nil
|
2022-07-17 04:08:51 -04:00
|
|
|
def res.body() "real_path" end
|
2008-03-31 18:40:06 -04:00
|
|
|
res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
data = fetcher.fetch_http URI.parse(url)
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "real_path", data
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def test_fetch_http_redirects
|
2008-03-31 18:40:06 -04:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://gems.example.com/redirect"
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def fetcher.request(uri, request_class, last_modified = nil)
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://gems.example.com/redirect"
|
2008-09-25 06:13:50 -04:00
|
|
|
res = Net::HTTPMovedPermanently.new nil, 301, nil
|
2022-07-17 04:08:51 -04:00
|
|
|
res.add_field "Location", url
|
2008-03-31 18:40:06 -04:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2011-05-31 23:45:05 -04:00
|
|
|
fetcher.fetch_http URI.parse(url)
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
assert_equal "too many redirects (#{url})", e.message
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2016-09-27 20:57:53 -04:00
|
|
|
def test_fetch_http_redirects_without_location
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
|
|
|
@fetcher = fetcher
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "http://gems.example.com/redirect"
|
2016-09-27 20:57:53 -04:00
|
|
|
|
|
|
|
def fetcher.request(uri, request_class, last_modified = nil)
|
|
|
|
res = Net::HTTPMovedPermanently.new nil, 301, nil
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2016-09-27 20:57:53 -04:00
|
|
|
fetcher.fetch_http URI.parse(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "redirecting but no redirect location was given (#{url})", e.message
|
|
|
|
end
|
|
|
|
|
2015-10-29 20:54:12 -04:00
|
|
|
def test_fetch_http_with_additional_headers
|
|
|
|
ENV["http_proxy"] = @proxy_uri
|
|
|
|
ENV["no_proxy"] = URI::parse(@server_uri).host
|
2022-05-20 04:15:15 -04:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil, nil, { "X-Captain" => "murphy" }
|
2015-10-29 20:54:12 -04:00
|
|
|
@fetcher = fetcher
|
|
|
|
assert_equal "murphy", fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
def assert_fetch_s3(url, signature, token=nil, region="us-east-1", instance_profile_json=nil)
|
2014-09-13 23:30:02 -04:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2014-09-13 23:30:02 -04:00
|
|
|
$fetched_uri = nil
|
2019-06-24 20:36:31 -04:00
|
|
|
$instance_profile = instance_profile_json
|
2014-09-13 23:30:02 -04:00
|
|
|
|
|
|
|
def fetcher.request(uri, request_class, last_modified = nil)
|
|
|
|
$fetched_uri = uri
|
|
|
|
res = Net::HTTPOK.new nil, 200, nil
|
2022-07-17 04:08:51 -04:00
|
|
|
def res.body() "success" end
|
2014-09-13 23:30:02 -04:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2019-07-03 19:35:59 -04:00
|
|
|
def fetcher.s3_uri_signer(uri)
|
2022-07-17 04:08:51 -04:00
|
|
|
require "json"
|
2019-07-03 19:35:59 -04:00
|
|
|
s3_uri_signer = Gem::S3URISigner.new(uri)
|
2019-07-09 21:52:54 -04:00
|
|
|
def s3_uri_signer.ec2_metadata_credentials_json
|
|
|
|
JSON.parse($instance_profile)
|
2019-07-03 19:35:59 -04:00
|
|
|
end
|
2019-08-14 15:00:27 -04:00
|
|
|
# Running sign operation to make sure uri.query is not mutated
|
|
|
|
s3_uri_signer.sign
|
|
|
|
raise "URI query is not empty: #{uri.query}" unless uri.query.nil?
|
2019-07-03 19:35:59 -04:00
|
|
|
s3_uri_signer
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
data = fetcher.fetch_s3 URI.parse(url)
|
|
|
|
|
2019-06-28 13:21:12 -04:00
|
|
|
assert_equal "https://my-bucket.s3.#{region}.amazonaws.com/gems/specs.4.8.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=testuser%2F20190624%2F#{region}%2Fs3%2Faws4_request&X-Amz-Date=20190624T050641Z&X-Amz-Expires=86400#{token ? "&X-Amz-Security-Token=" + token : ""}&X-Amz-SignedHeaders=host&X-Amz-Signature=#{signature}", $fetched_uri.to_s
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_equal "success", data
|
2014-09-13 23:30:02 -04:00
|
|
|
ensure
|
|
|
|
$fetched_uri = nil
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
def test_fetch_s3_config_creds
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :id => "testuser", :secret => "testpass" },
|
2018-08-27 06:05:04 -04:00
|
|
|
}
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "20f974027db2f3cd6193565327a7c73457a138efb1a63ea248d185ce6827d41b"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_config_creds_with_region
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :id => "testuser", :secret => "testpass", :region => "us-west-2" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "4afc3010757f1fd143e769f1d1dabd406476a4fc7c120e9884fd02acbb8f26c9", nil, "us-west-2"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_config_creds_with_token
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :id => "testuser", :secret => "testpass", :security_token => "testtoken" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "935160a427ef97e7630f799232b8f208c4a4e49aad07d0540572a2ad5fe9f93c", "testtoken"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_env_creds
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV["AWS_ACCESS_KEY_ID"] = "testuser"
|
|
|
|
ENV["AWS_SECRET_ACCESS_KEY"] = "testpass"
|
|
|
|
ENV["AWS_SESSION_TOKEN"] = nil
|
2019-06-24 20:36:31 -04:00
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :provider => "env" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "20f974027db2f3cd6193565327a7c73457a138efb1a63ea248d185ce6827d41b"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
|
|
|
ensure
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV.each_key {|key| ENV.delete(key) if key.start_with?("AWS") }
|
2019-06-24 20:36:31 -04:00
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_env_creds_with_region
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV["AWS_ACCESS_KEY_ID"] = "testuser"
|
|
|
|
ENV["AWS_SECRET_ACCESS_KEY"] = "testpass"
|
|
|
|
ENV["AWS_SESSION_TOKEN"] = nil
|
2019-06-24 20:36:31 -04:00
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :provider => "env", :region => "us-west-2" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "4afc3010757f1fd143e769f1d1dabd406476a4fc7c120e9884fd02acbb8f26c9", nil, "us-west-2"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
2018-08-27 06:05:04 -04:00
|
|
|
ensure
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV.each_key {|key| ENV.delete(key) if key.start_with?("AWS") }
|
2019-06-24 20:36:31 -04:00
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_env_creds_with_token
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV["AWS_ACCESS_KEY_ID"] = "testuser"
|
|
|
|
ENV["AWS_SECRET_ACCESS_KEY"] = "testpass"
|
|
|
|
ENV["AWS_SESSION_TOKEN"] = "testtoken"
|
2019-06-24 20:36:31 -04:00
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :provider => "env" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "935160a427ef97e7630f799232b8f208c4a4e49aad07d0540572a2ad5fe9f93c", "testtoken"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
|
|
|
ensure
|
2022-07-17 04:08:51 -04:00
|
|
|
ENV.each_key {|key| ENV.delete(key) if key.start_with?("AWS") }
|
2018-08-27 06:05:04 -04:00
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_url_creds
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://testuser:testpass@my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "20f974027db2f3cd6193565327a7c73457a138efb1a63ea248d185ce6827d41b"
|
2019-06-24 20:36:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_instance_profile_creds
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :provider => "instance_profile" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "20f974027db2f3cd6193565327a7c73457a138efb1a63ea248d185ce6827d41b", nil, "us-east-1",
|
2019-06-24 20:36:31 -04:00
|
|
|
'{"AccessKeyId": "testuser", "SecretAccessKey": "testpass"}'
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_instance_profile_creds_with_region
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :provider => "instance_profile", :region => "us-west-2" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "4afc3010757f1fd143e769f1d1dabd406476a4fc7c120e9884fd02acbb8f26c9", nil, "us-west-2",
|
2019-06-24 20:36:31 -04:00
|
|
|
'{"AccessKeyId": "testuser", "SecretAccessKey": "testpass"}'
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_instance_profile_creds_with_token
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :provider => "instance_profile" },
|
2019-06-24 20:36:31 -04:00
|
|
|
}
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
2019-06-24 20:36:31 -04:00
|
|
|
Time.stub :now, Time.at(1561353581) do
|
2022-07-17 04:08:51 -04:00
|
|
|
assert_fetch_s3 url, "935160a427ef97e7630f799232b8f208c4a4e49aad07d0540572a2ad5fe9f93c", "testtoken", "us-east-1",
|
2019-06-24 20:36:31 -04:00
|
|
|
'{"AccessKeyId": "testuser", "SecretAccessKey": "testpass", "Token": "testtoken"}'
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
2018-08-27 06:05:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def refute_fetch_s3(url, expected_message)
|
2014-09-13 23:30:02 -04:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2018-08-27 06:05:04 -04:00
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::RemoteFetcher::FetchError do
|
2014-09-13 23:30:02 -04:00
|
|
|
fetcher.fetch_s3 URI.parse(url)
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
assert_match expected_message, e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_no_source_key
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
|
|
|
refute_fetch_s3 url, "no s3_source key exists in .gemrc"
|
2018-08-27 06:05:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_no_host
|
|
|
|
Gem.configuration[:s3_source] = {
|
2022-07-17 04:08:51 -04:00
|
|
|
"my-bucket" => { :id => "testuser", :secret => "testpass" },
|
2018-08-27 06:05:04 -04:00
|
|
|
}
|
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://other-bucket/gems/specs.4.8.gz"
|
|
|
|
refute_fetch_s3 url, "no key for host other-bucket in s3_source in .gemrc"
|
2018-08-27 06:05:04 -04:00
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_no_id
|
2022-07-17 04:08:51 -04:00
|
|
|
Gem.configuration[:s3_source] = { "my-bucket" => { :secret => "testpass" } }
|
2018-08-27 06:05:04 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
|
|
|
refute_fetch_s3 url, "s3_source for my-bucket missing id or secret"
|
2018-08-27 06:05:04 -04:00
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_s3_no_secret
|
2022-07-17 04:08:51 -04:00
|
|
|
Gem.configuration[:s3_source] = { "my-bucket" => { :id => "testuser" } }
|
2018-08-27 06:05:04 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
url = "s3://my-bucket/gems/specs.4.8.gz"
|
|
|
|
refute_fetch_s3 url, "s3_source for my-bucket missing id or secret"
|
2018-08-27 06:05:04 -04:00
|
|
|
ensure
|
|
|
|
Gem.configuration[:s3_source] = nil
|
2014-09-13 23:30:02 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_observe_no_proxy_env_single_host
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2012-11-29 01:52:18 -05:00
|
|
|
ENV["http_proxy"] = @proxy_uri
|
|
|
|
ENV["no_proxy"] = URI::parse(@server_uri).host
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_data_from_server fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_observe_no_proxy_env_list
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2012-11-29 01:52:18 -05:00
|
|
|
ENV["http_proxy"] = @proxy_uri
|
|
|
|
ENV["no_proxy"] = "fakeurl.com, #{URI::parse(@server_uri).host}"
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_data_from_server fetcher.fetch_path(@server_uri)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_request_block
|
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_throws :block_called do
|
2022-07-17 04:08:51 -04:00
|
|
|
fetcher.request URI("http://example"), Net::HTTP::Get do |req|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_kind_of Net::HTTPGenericRequest, req
|
|
|
|
throw :block_called
|
|
|
|
end
|
|
|
|
end
|
2013-07-22 18:46:50 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_yaml_error_on_size
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2020-04-10 15:59:13 -04:00
|
|
|
self.enable_yaml = false
|
2007-11-10 02:48:56 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new nil
|
2014-11-04 02:23:59 -05:00
|
|
|
@fetcher = fetcher
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_error { fetcher.size }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
def test_ssl_connection
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server
|
2022-07-17 04:08:51 -04:00
|
|
|
temp_ca_cert = File.join(__dir__, "ca_cert.pem")
|
2012-04-19 19:07:48 -04:00
|
|
|
with_configured_fetcher(":ssl_ca_cert: #{temp_ca_cert}") do |fetcher|
|
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml")
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_ssl_client_cert_auth_connection
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server({
|
2013-09-14 04:59:02 -04:00
|
|
|
:SSLVerifyClient =>
|
2022-05-20 04:15:15 -04:00
|
|
|
OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT })
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
temp_ca_cert = File.join(__dir__, "ca_cert.pem")
|
|
|
|
temp_client_cert = File.join(__dir__, "client.pem")
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
with_configured_fetcher(
|
|
|
|
":ssl_ca_cert: #{temp_ca_cert}\n" +
|
|
|
|
":ssl_client_cert: #{temp_client_cert}\n") do |fetcher|
|
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_not_allow_invalid_client_cert_auth_connection
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server({
|
2013-09-14 04:59:02 -04:00
|
|
|
:SSLVerifyClient =>
|
2022-05-20 04:15:15 -04:00
|
|
|
OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT })
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2022-07-17 04:08:51 -04:00
|
|
|
temp_ca_cert = File.join(__dir__, "ca_cert.pem")
|
|
|
|
temp_client_cert = File.join(__dir__, "invalid_client.pem")
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
with_configured_fetcher(
|
|
|
|
":ssl_ca_cert: #{temp_ca_cert}\n" +
|
|
|
|
":ssl_client_cert: #{temp_client_cert}\n") do |fetcher|
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::RemoteFetcher::FetchError do
|
2018-08-27 06:05:04 -04:00
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml")
|
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
def test_do_not_allow_insecure_ssl_connection_by_default
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server
|
2012-04-19 19:07:48 -04:00
|
|
|
with_configured_fetcher do |fetcher|
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::RemoteFetcher::FetchError do
|
2012-04-19 19:07:48 -04:00
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml")
|
|
|
|
end
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
|
|
|
|
def test_ssl_connection_allow_verify_none
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server
|
2012-04-19 19:07:48 -04:00
|
|
|
with_configured_fetcher(":ssl_verify_mode: 0") do |fetcher|
|
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml")
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
|
|
|
|
def test_do_not_follow_insecure_redirect
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server
|
2022-07-17 04:08:51 -04:00
|
|
|
temp_ca_cert = File.join(__dir__, "ca_cert.pem")
|
2018-12-19 02:20:17 -05:00
|
|
|
expected_error_message =
|
|
|
|
"redirecting to non-https resource: #{@server_uri} (https://localhost:#{ssl_server.config[:Port]}/insecure_redirect?to=#{@server_uri})"
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
with_configured_fetcher(":ssl_ca_cert: #{temp_ca_cert}") do |fetcher|
|
2021-05-10 23:25:46 -04:00
|
|
|
err = assert_raise Gem::RemoteFetcher::FetchError do
|
2012-04-19 19:07:48 -04:00
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/insecure_redirect?to=#{@server_uri}")
|
|
|
|
end
|
2018-12-19 02:20:17 -05:00
|
|
|
|
|
|
|
assert_equal(err.message, expected_error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nil_ca_cert
|
2020-04-10 15:59:13 -04:00
|
|
|
ssl_server = start_ssl_server
|
2018-12-19 02:20:17 -05:00
|
|
|
temp_ca_cert = nil
|
|
|
|
|
|
|
|
with_configured_fetcher(":ssl_ca_cert: #{temp_ca_cert}") do |fetcher|
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::RemoteFetcher::FetchError do
|
2018-12-19 02:20:17 -05:00
|
|
|
fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}")
|
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
|
|
|
|
def with_configured_fetcher(config_str = nil, &block)
|
|
|
|
if config_str
|
2022-07-17 04:08:51 -04:00
|
|
|
temp_conf = File.join @tempdir, ".gemrc"
|
|
|
|
File.open temp_conf, "w" do |fp|
|
2012-04-19 19:07:48 -04:00
|
|
|
fp.puts config_str
|
|
|
|
end
|
|
|
|
Gem.configuration = Gem::ConfigFile.new %W[--config-file #{temp_conf}]
|
|
|
|
end
|
2014-11-04 02:23:59 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.new
|
|
|
|
yield fetcher
|
2012-04-19 19:07:48 -04:00
|
|
|
ensure
|
2014-11-04 02:23:59 -05:00
|
|
|
fetcher.close_all
|
2012-04-19 19:07:48 -04:00
|
|
|
Gem.configuration = nil
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def assert_error(exception_class=Exception)
|
|
|
|
got_exception = false
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
begin
|
|
|
|
yield
|
2011-01-18 19:08:49 -05:00
|
|
|
rescue exception_class
|
2007-11-10 02:48:56 -05:00
|
|
|
got_exception = true
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
assert got_exception, "Expected exception conforming to #{exception_class}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_data_from_server(data)
|
2012-05-06 21:23:07 -04:00
|
|
|
assert_match(/0\.4\.11/, data, "Data is not from server")
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_data_from_proxy(data)
|
2012-05-06 21:23:07 -04:00
|
|
|
assert_match(/0\.4\.2/, data, "Data is not from proxy")
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class NilLog < WEBrick::Log
|
|
|
|
def log(level, data) #Do nothing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
private
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
attr_reader :normal_server, :proxy_server
|
|
|
|
attr_accessor :enable_zip, :enable_yaml
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
def start_servers
|
|
|
|
@normal_server ||= start_server(SERVER_DATA)
|
|
|
|
@proxy_server ||= start_server(PROXY_DATA)
|
|
|
|
@enable_yaml = true
|
|
|
|
@enable_zip = false
|
|
|
|
@ssl_server = nil
|
|
|
|
@ssl_server_thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_servers
|
|
|
|
if @normal_server
|
|
|
|
@normal_server.kill.join
|
|
|
|
@normal_server = nil
|
|
|
|
end
|
|
|
|
if @proxy_server
|
|
|
|
@proxy_server.kill.join
|
|
|
|
@proxy_server = nil
|
|
|
|
end
|
|
|
|
if @ssl_server
|
|
|
|
@ssl_server.stop
|
2014-09-13 23:30:02 -04:00
|
|
|
@ssl_server = nil
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
if @ssl_server_thread
|
|
|
|
@ssl_server_thread.kill.join
|
|
|
|
@ssl_server_thread = nil
|
2014-07-01 04:20:39 -04:00
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
utils = WEBrick::Utils # TimeoutHandler is since 1.9
|
|
|
|
utils::TimeoutHandler.terminate if defined?(utils::TimeoutHandler.terminate)
|
|
|
|
end
|
2014-07-01 04:20:39 -04:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
def normal_server_port
|
|
|
|
@normal_server[:server].config[:Port]
|
|
|
|
end
|
2013-11-10 12:51:40 -05:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
def proxy_server_port
|
|
|
|
@proxy_server[:server].config[:Port]
|
|
|
|
end
|
2013-11-10 12:51:40 -05:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
def start_ssl_server(config = {})
|
2021-05-10 23:27:07 -04:00
|
|
|
pend "starting this test server fails randomly on jruby" if Gem.java_platform?
|
2020-04-10 15:38:30 -04:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
null_logger = NilLog.new
|
|
|
|
server = WEBrick::HTTPServer.new({
|
|
|
|
:Port => 0,
|
|
|
|
:Logger => null_logger,
|
|
|
|
:AccessLog => [],
|
|
|
|
:SSLEnable => true,
|
2022-07-17 04:08:51 -04:00
|
|
|
:SSLCACertificateFile => File.join(__dir__, "ca_cert.pem"),
|
|
|
|
:SSLCertificate => cert("ssl_cert.pem"),
|
|
|
|
:SSLPrivateKey => key("ssl_key.pem"),
|
2020-04-10 15:59:13 -04:00
|
|
|
:SSLVerifyClient => nil,
|
2020-12-08 02:33:39 -05:00
|
|
|
:SSLCertName => nil,
|
2020-04-10 15:59:13 -04:00
|
|
|
}.merge(config))
|
|
|
|
server.mount_proc("/yaml") do |req, res|
|
|
|
|
res.body = "--- true\n"
|
|
|
|
end
|
|
|
|
server.mount_proc("/insecure_redirect") do |req, res|
|
2022-07-17 04:08:51 -04:00
|
|
|
res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, req.query["to"])
|
2020-04-10 15:59:13 -04:00
|
|
|
end
|
|
|
|
server.ssl_context.tmp_dh_callback = proc { TEST_KEY_DH2048 }
|
|
|
|
t = Thread.new do
|
|
|
|
begin
|
|
|
|
server.start
|
|
|
|
rescue Exception => ex
|
|
|
|
puts "ERROR during server thread: #{ex.message}"
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
server.shutdown
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
end
|
|
|
|
while server.status != :Running
|
|
|
|
sleep 0.1
|
|
|
|
unless t.alive?
|
|
|
|
t.join
|
|
|
|
raise
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
end
|
|
|
|
@ssl_server = server
|
|
|
|
@ssl_server_thread = t
|
|
|
|
server
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_server(data)
|
|
|
|
null_logger = NilLog.new
|
|
|
|
s = WEBrick::HTTPServer.new(
|
|
|
|
:Port => 0,
|
|
|
|
:DocumentRoot => nil,
|
|
|
|
:Logger => null_logger,
|
|
|
|
:AccessLog => null_logger
|
|
|
|
)
|
2020-06-10 13:46:05 -04:00
|
|
|
s.mount_proc("/kill") {|req, res| s.shutdown }
|
2020-04-10 15:59:13 -04:00
|
|
|
s.mount_proc("/yaml") do |req, res|
|
|
|
|
if req["X-Captain"]
|
|
|
|
res.body = req["X-Captain"]
|
|
|
|
elsif @enable_yaml
|
|
|
|
res.body = data
|
2022-07-17 04:08:51 -04:00
|
|
|
res["Content-Type"] = "text/plain"
|
|
|
|
res["content-length"] = data.size
|
2020-04-10 15:59:13 -04:00
|
|
|
else
|
|
|
|
res.status = "404"
|
|
|
|
res.body = "<h1>NOT FOUND</h1>"
|
2022-07-17 04:08:51 -04:00
|
|
|
res["Content-Type"] = "text/html"
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
end
|
|
|
|
s.mount_proc("/yaml.Z") do |req, res|
|
|
|
|
if @enable_zip
|
|
|
|
res.body = Zlib::Deflate.deflate(data)
|
2022-07-17 04:08:51 -04:00
|
|
|
res["Content-Type"] = "text/plain"
|
2020-04-10 15:59:13 -04:00
|
|
|
else
|
|
|
|
res.status = "404"
|
|
|
|
res.body = "<h1>NOT FOUND</h1>"
|
2022-07-17 04:08:51 -04:00
|
|
|
res["Content-Type"] = "text/html"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
th = Thread.new do
|
|
|
|
begin
|
|
|
|
s.start
|
|
|
|
rescue Exception => ex
|
|
|
|
abort "ERROR during server thread: #{ex.message}"
|
|
|
|
ensure
|
|
|
|
s.shutdown
|
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
end
|
2020-04-10 15:59:13 -04:00
|
|
|
th[:server] = s
|
|
|
|
th
|
|
|
|
end
|
2012-04-19 19:07:48 -04:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
def cert(filename)
|
2020-04-10 16:09:41 -04:00
|
|
|
OpenSSL::X509::Certificate.new(File.read(File.join(__dir__, filename)))
|
2020-04-10 15:59:13 -04:00
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2020-04-10 15:59:13 -04:00
|
|
|
def key(filename)
|
2020-04-10 16:09:41 -04:00
|
|
|
OpenSSL::PKey::RSA.new(File.read(File.join(__dir__, filename)))
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2020-12-08 02:33:39 -05:00
|
|
|
end if Gem::HAVE_OPENSSL
|