2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2021-06-01 23:32:47 -04:00
|
|
|
require_relative "helper"
|
2012-11-29 01:52:18 -05:00
|
|
|
require "rubygems/source"
|
2014-09-13 23:30:02 -04:00
|
|
|
require "rubygems/indexer"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
class TestGemSource < Gem::TestCase
|
|
|
|
def tuple(*args)
|
|
|
|
Gem::NameTuple.new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
@specs = spec_fetcher do |fetcher|
|
|
|
|
fetcher.spec "a", "1.a"
|
|
|
|
fetcher.gem "a", 1
|
|
|
|
fetcher.spec "a", 2
|
|
|
|
fetcher.spec "b", 2
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
@source = Gem::Source.new(@gem_repo)
|
|
|
|
end
|
|
|
|
|
2014-12-06 19:53:01 -05:00
|
|
|
def test_initialize_invalid_uri
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise URI::InvalidURIError do
|
2014-12-06 19:53:01 -05:00
|
|
|
Gem::Source.new "git@example:a.git"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_git
|
|
|
|
repository = "git@example:a.git"
|
|
|
|
|
|
|
|
source = Gem::Source::Git.new "a", repository, "master", false
|
|
|
|
|
|
|
|
assert_equal repository, source.uri
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_cache_dir_escapes_windows_paths
|
|
|
|
uri = URI.parse("file:///C:/WINDOWS/Temp/gem_repo")
|
2013-09-14 04:59:02 -04:00
|
|
|
root = Gem.spec_cache_dir
|
2012-11-29 01:52:18 -05:00
|
|
|
cache_dir = @source.cache_dir(uri).gsub(root, "")
|
|
|
|
assert cache_dir !~ /:/, "#{cache_dir} should not contain a :"
|
|
|
|
end
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
def test_dependency_resolver_set_bundler_api
|
2014-09-13 23:30:02 -04:00
|
|
|
response = Net::HTTPResponse.new "1.1", 200, "OK"
|
2020-12-22 18:45:19 -05:00
|
|
|
response.uri = URI("http://example")
|
2014-09-13 23:30:02 -04:00
|
|
|
|
2020-12-22 18:45:19 -05:00
|
|
|
@fetcher.data[@gem_repo] = response
|
2013-11-10 12:51:40 -05:00
|
|
|
|
|
|
|
set = @source.dependency_resolver_set
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
assert_kind_of Gem::Resolver::APISet, set
|
2013-11-10 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def test_dependency_resolver_set_file_uri
|
|
|
|
Gem::Indexer.new(@tempdir).generate_index
|
|
|
|
|
|
|
|
source = Gem::Source.new "file://#{@tempdir}/"
|
|
|
|
|
|
|
|
set = source.dependency_resolver_set
|
|
|
|
|
|
|
|
assert_kind_of Gem::Resolver::IndexSet, set
|
|
|
|
end
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
def test_dependency_resolver_set_marshal_api
|
|
|
|
set = @source.dependency_resolver_set
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
assert_kind_of Gem::Resolver::IndexSet, set
|
2013-11-10 12:51:40 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_fetch_spec
|
2013-11-11 19:16:41 -05:00
|
|
|
a1 = @specs["a-1"]
|
|
|
|
|
|
|
|
spec_uri = "#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{a1.spec_name}"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
spec = @source.fetch_spec tuple("a", Gem::Version.new(1), "ruby")
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal a1.full_name, spec.full_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
cache_dir = @source.cache_dir URI.parse(spec_uri)
|
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
cache_file = File.join cache_dir, a1.spec_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
assert File.exist?(cache_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_spec_cached
|
2013-11-11 19:16:41 -05:00
|
|
|
a1 = @specs["a-1"]
|
|
|
|
|
|
|
|
spec_uri = "#{@gem_repo}/#{Gem::MARSHAL_SPEC_DIR}#{a1.spec_name}"
|
2012-11-29 01:52:18 -05:00
|
|
|
@fetcher.data["#{spec_uri}.rz"] = nil
|
|
|
|
|
|
|
|
cache_dir = @source.cache_dir URI.parse(spec_uri)
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
cache_file = File.join cache_dir, a1.spec_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open cache_file, "wb" do |io|
|
2013-11-11 19:16:41 -05:00
|
|
|
Marshal.dump a1, io
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
spec = @source.fetch_spec tuple("a", Gem::Version.new(1), "ruby")
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal a1.full_name, spec.full_name
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_spec_platform
|
2013-11-11 19:16:41 -05:00
|
|
|
specs = spec_fetcher do |fetcher|
|
|
|
|
fetcher.legacy_platform
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
spec = @source.fetch_spec tuple("pl", Gem::Version.new(1), "i386-linux")
|
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal specs["pl-1-x86-linux"].full_name, spec.full_name
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch_spec_platform_ruby
|
|
|
|
spec = @source.fetch_spec tuple("a", Gem::Version.new(1), nil)
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal @specs["a-1"].full_name, spec.full_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
spec = @source.fetch_spec tuple("a", Gem::Version.new(1), "")
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal @specs["a-1"].full_name, spec.full_name
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_specs
|
2020-06-10 13:46:05 -04:00
|
|
|
released = @source.load_specs(:released).map {|spec| spec.full_name }
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal %W[a-2 a-1 b-2], released
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
cache_dir = File.join Gem.spec_cache_dir, "gems.example.com%80"
|
2012-11-29 01:52:18 -05:00
|
|
|
assert File.exist?(cache_dir), "#{cache_dir} does not exist"
|
|
|
|
|
|
|
|
cache_file = File.join cache_dir, "specs.#{Gem.marshal_version}"
|
|
|
|
assert File.exist?(cache_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_specs_cached
|
2013-11-11 19:16:41 -05:00
|
|
|
latest_specs = @source.load_specs :latest
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# Make sure the cached version is actually different:
|
2013-11-11 19:16:41 -05:00
|
|
|
latest_specs << Gem::NameTuple.new("cached", Gem::Version.new("1.0.0"), "ruby")
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
@fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] = nil
|
|
|
|
@fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}"] =
|
2013-11-11 19:16:41 -05:00
|
|
|
" " * Marshal.dump(latest_specs).length
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
cache_dir = File.join Gem.spec_cache_dir, "gems.example.com%80"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
|
|
|
|
cache_file = File.join cache_dir, "latest_specs.#{Gem.marshal_version}"
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open cache_file, "wb" do |io|
|
2013-11-11 19:16:41 -05:00
|
|
|
Marshal.dump latest_specs, io
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
cached_specs = @source.load_specs :latest
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal latest_specs, cached_specs
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_specs_cached_empty
|
2013-11-11 19:16:41 -05:00
|
|
|
latest_specs = @source.load_specs :latest
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# Make sure the cached version is actually different:
|
2013-11-11 19:16:41 -05:00
|
|
|
latest_specs << Gem::NameTuple.new("fixed", Gem::Version.new("1.0.0"), "ruby")
|
2012-11-29 01:52:18 -05:00
|
|
|
# Setup valid data on the 'remote'
|
|
|
|
@fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] =
|
2013-11-11 19:16:41 -05:00
|
|
|
util_gzip(Marshal.dump(latest_specs))
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
cache_dir = File.join Gem.spec_cache_dir, "gems.example.com%80"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
|
|
|
|
cache_file = File.join cache_dir, "latest_specs.#{Gem.marshal_version}"
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open cache_file, "wb" do |io|
|
2012-11-29 01:52:18 -05:00
|
|
|
# Setup invalid data in the cache:
|
2013-11-11 19:16:41 -05:00
|
|
|
io.write Marshal.dump(latest_specs)[0, 10]
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
fixed_specs = @source.load_specs :latest
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-11 19:16:41 -05:00
|
|
|
assert_equal latest_specs, fixed_specs
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_specs_from_unavailable_uri
|
|
|
|
src = Gem::Source.new("http://not-there.nothing")
|
|
|
|
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::RemoteFetcher::FetchError do
|
2012-11-29 01:52:18 -05:00
|
|
|
src.load_specs :latest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_spaceship
|
|
|
|
remote = @source
|
2013-11-11 19:16:41 -05:00
|
|
|
specific = Gem::Source::SpecificFile.new @specs["a-1"].cache_file
|
2013-09-14 04:59:02 -04:00
|
|
|
installed = Gem::Source::Installed.new
|
|
|
|
local = Gem::Source::Local.new
|
|
|
|
|
2019-05-03 13:56:58 -04:00
|
|
|
assert_equal(0, remote.<=>(remote), "remote <=> remote")
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2019-05-03 13:56:58 -04:00
|
|
|
assert_equal(-1, remote.<=>(specific), "remote <=> specific")
|
|
|
|
assert_equal(1, specific.<=>(remote), "specific <=> remote")
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2019-05-03 13:56:58 -04:00
|
|
|
assert_equal(-1, remote.<=>(local), "remote <=> local")
|
|
|
|
assert_equal(1, local.<=>(remote), "local <=> remote")
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2020-03-27 08:49:55 -04:00
|
|
|
assert_equal(-1, remote.<=>(installed), "remote <=> installed")
|
2019-05-03 13:56:58 -04:00
|
|
|
assert_equal(1, installed.<=>(remote), "installed <=> remote")
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
no_uri = @source.dup
|
|
|
|
no_uri.instance_variable_set :@uri, nil
|
|
|
|
|
2019-05-03 13:56:58 -04:00
|
|
|
assert_equal(-1, remote.<=>(no_uri), "remote <=> no_uri")
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def test_spaceship_order_is_preserved_when_uri_differs
|
|
|
|
sourceA = Gem::Source.new "http://example.com/a"
|
|
|
|
sourceB = Gem::Source.new "http://example.com/b"
|
|
|
|
|
2020-03-27 08:49:17 -04:00
|
|
|
assert_equal(0, sourceA.<=>(sourceA), "sourceA <=> sourceA")
|
|
|
|
assert_equal(1, sourceA.<=>(sourceB), "sourceA <=> sourceB")
|
|
|
|
assert_equal(1, sourceB.<=>(sourceA), "sourceB <=> sourceA")
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
|
2013-10-18 17:56:18 -04:00
|
|
|
def test_update_cache_eh
|
|
|
|
assert @source.update_cache?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update_cache_eh_home_nonexistent
|
2020-06-11 06:12:13 -04:00
|
|
|
FileUtils.rmdir Gem.user_home
|
2013-10-18 17:56:18 -04:00
|
|
|
|
|
|
|
refute @source.update_cache?
|
|
|
|
end
|
|
|
|
|
2019-12-13 06:19:08 -05:00
|
|
|
def test_typo_squatting
|
|
|
|
rubygems_source = Gem::Source.new("https://rubgems.org")
|
|
|
|
assert rubygems_source.typo_squatting?("rubygems.org")
|
|
|
|
assert rubygems_source.typo_squatting?("rubyagems.org")
|
|
|
|
assert rubygems_source.typo_squatting?("rubyasgems.org")
|
|
|
|
refute rubygems_source.typo_squatting?("rubysertgems.org")
|
|
|
|
end
|
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
def test_typo_squatting_false_positive
|
|
|
|
rubygems_source = Gem::Source.new("https://rubygems.org")
|
|
|
|
refute rubygems_source.typo_squatting?("rubygems.org")
|
|
|
|
end
|
|
|
|
|
2019-12-13 06:19:08 -05:00
|
|
|
def test_typo_squatting_custom_distance_threshold
|
|
|
|
rubygems_source = Gem::Source.new("https://rubgems.org")
|
|
|
|
distance_threshold = 5
|
|
|
|
assert rubygems_source.typo_squatting?("rubysertgems.org", distance_threshold)
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|