Skip to content

Commit

Permalink
dev: introduce some helpers for introspecting on libxml2 patches
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Jan 3, 2022
1 parent b643ef6 commit a6cb299
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 19 deletions.
18 changes: 15 additions & 3 deletions lib/nokogiri/version/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,32 @@ def to_markdown
end
end

def self.uses_libxml?(requirement = nil) # :nodoc:
# :nodoc:
def self.uses_libxml?(requirement = nil)
return false unless VersionInfo.instance.libxml2?
return true unless requirement
Gem::Requirement.new(requirement).satisfied_by?(VersionInfo.instance.loaded_libxml_version)
end

def self.uses_gumbo? # :nodoc:
# :nodoc:
def self.uses_gumbo?
uses_libxml? # TODO: replace with Gumbo functionality
end

def self.jruby? # :nodoc:
# :nodoc:
def self.jruby?
VersionInfo.instance.jruby?
end

# :nodoc:
def self.libxml2_patches
if VersionInfo.instance.libxml2_using_packaged?
Nokogiri::VERSION_INFO["libxml"]["patches"]
else
[]
end
end

require_relative "../jruby/dependencies" if Nokogiri.jruby?
require_relative "../extension"

Expand Down
10 changes: 10 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def skip_unless_libxml2(msg = "this test should only run with libxml2")
skip(msg) unless Nokogiri.uses_libxml?
end

def skip_unless_libxml2_patch(patch_name)
patch_dir = File.join(__dir__, "..", "patches", "libxml2")
if File.directory?(patch_dir) && !File.exist?(File.join(patch_dir, patch_name))
raise("checking for nonexistent patch file #{patch_name.inspect}")
end
unless Nokogiri.libxml2_patches.include?(patch_name)
skip("this test needs libxml2 patched with #{patch_name}")
end
end

def skip_unless_jruby(msg = "this test should only run with jruby")
skip(msg) unless Nokogiri.jruby?
end
Expand Down
12 changes: 3 additions & 9 deletions test/html4/test_attributes_properly_escaped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ module Nokogiri
module HTML
class TestAttributesProperlyEscaped < Nokogiri::TestCase
def test_attribute_macros_are_escaped
if Nokogiri.uses_libxml? && !Nokogiri::VERSION_INFO["libxml"]["patches"]&.include?("0001-Remove-script-macro-support.patch")
skip("libxml2 has not been patched to be safe against attribute macros")
end
skip_unless_libxml2_patch("0001-Remove-script-macro-support.patch") if Nokogiri.uses_libxml?

html = "<p><i for=\"&{<test>}\"></i></p>"
document = Nokogiri::HTML::Document.new
Expand All @@ -18,9 +16,7 @@ def test_attribute_macros_are_escaped
end

def test_libxml_escapes_server_side_includes
if Nokogiri.uses_libxml? && !Nokogiri::VERSION_INFO["libxml"]["patches"]&.include?("0002-Update-entities-to-remove-handling-of-ssi.patch")
skip("libxml2 has not been patched to be safe against SSI")
end
skip_unless_libxml2_patch("0002-Update-entities-to-remove-handling-of-ssi.patch") if Nokogiri.uses_libxml?

original_html = %(<p><a href='<!--"><test>-->'></a></p>)
document = Nokogiri::HTML::Document.new
Expand All @@ -30,9 +26,7 @@ def test_libxml_escapes_server_side_includes
end

def test_libxml_escapes_server_side_includes_without_nested_quotes
if Nokogiri.uses_libxml? && !Nokogiri::VERSION_INFO["libxml"]["patches"]&.include?("0002-Update-entities-to-remove-handling-of-ssi.patch")
skip("libxml2 has not been patched to be safe against SSI")
end
skip_unless_libxml2_patch("0002-Update-entities-to-remove-handling-of-ssi.patch") if Nokogiri.uses_libxml?

original_html = %(<p><i for="<!--<test>-->"></i></p>)
document = Nokogiri::HTML::Document.new
Expand Down
4 changes: 2 additions & 2 deletions test/html4/test_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestComment < Nokogiri::TestCase
let(:html) { "<html><body><div id=under-test><!--></div><div id=also-here></div></body></html>" }

if Nokogiri.uses_libxml?
if Nokogiri::VersionInfo.instance.libxml2_using_packaged? && Nokogiri::VERSION_INFO["libxml"]["patches"]&.include?("0008-htmlParseComment-handle-abruptly-closed-comments.patch")
if Nokogiri.libxml2_patches.include?("0008-htmlParseComment-handle-abruptly-closed-comments.patch")
it "behaves as if the comment is closed correctly" do # COMPLIANT
assert_equal 1, subject.children.length
assert subject.children.first.comment?
Expand Down Expand Up @@ -54,7 +54,7 @@ class TestComment < Nokogiri::TestCase
let(:html) { "<html><body><div id=under-test><!---></div><div id=also-here></div></body></html>" }

if Nokogiri.uses_libxml?
if Nokogiri::VersionInfo.instance.libxml2_using_packaged? && Nokogiri::VERSION_INFO["libxml"]["patches"]&.include?("0008-htmlParseComment-handle-abruptly-closed-comments.patch")
if Nokogiri.libxml2_patches.include?("0008-htmlParseComment-handle-abruptly-closed-comments.patch")
it "behaves as if the comment is closed correctly" do # COMPLIANT
assert_equal 1, subject.children.length
assert subject.children.first.comment?
Expand Down
3 changes: 3 additions & 0 deletions test/test_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_version_info_for_xerces_and_nekohtml
skip_unless_jruby("xerces/nekohtml is only used for JRuby")
assert_equal(Nokogiri::XERCES_VERSION, version_info["other_libraries"]["xerces"])
assert_equal(Nokogiri::NEKO_VERSION, version_info["other_libraries"]["nekohtml"])
assert_empty(Nokogiri.libxml2_patches)
end

def test_version_info_for_libxml
Expand All @@ -41,12 +42,14 @@ def test_version_info_for_libxml
if Nokogiri::VersionInfo.instance.libxml2_using_packaged?
assert_equal("packaged", version_info["libxml"]["source"])
assert(version_info["libxml"]["patches"])
assert_equal(Nokogiri.libxml2_patches, version_info["libxml"]["patches"])
assert_equal(Nokogiri::VersionInfo.instance.libxml2_precompiled?, version_info["libxml"]["precompiled"])
end
if Nokogiri::VersionInfo.instance.libxml2_using_system?
assert_equal("system", version_info["libxml"]["source"])
refute(version_info["libxml"].key?("precompiled"))
refute(version_info["libxml"].key?("patches"))
assert_empty(Nokogiri.libxml2_patches)
end

assert_equal(Nokogiri::LIBXML_COMPILED_VERSION, version_info["libxml"]["compiled"])
Expand Down
3 changes: 1 addition & 2 deletions test/xml/sax/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ def call_parse_io_with_encoding(encoding)
end

it :test_large_cdata_is_handled do
# see #2132 and https://gitlab.gnome.org/GNOME/libxml2/-/issues/200
skip("Upstream libxml2 <= 2.9.10 needs to be patched") if Nokogiri::VersionInfo.instance.libxml2_using_system?
skip("see #2132 and https://gitlab.gnome.org/GNOME/libxml2/-/issues/200") if Nokogiri.uses_libxml?("<=2.9.10")

template = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
4 changes: 1 addition & 3 deletions test/xml/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ def test_xpath_syntax_error_should_not_display_line_and_column_if_both_are_zero
end

def test_huge_xpath_query
if Nokogiri.uses_libxml?("~>2.9.11") && !Nokogiri::VERSION_INFO["libxml"]["patches"]&.include?("0007-Fix-XPath-recursion-limit.patch")
skip("libxml2 under test is broken with respect to xpath query recusion depth")
end
skip_unless_libxml2_patch("0007-Fix-XPath-recursion-limit.patch") if Nokogiri.uses_libxml?("~>2.9.11")

# real world example
# from https://github.com/sparklemotion/nokogiri/issues/2257
Expand Down

0 comments on commit a6cb299

Please sign in to comment.