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
|
require "helper"
module Nokogiri
module XML
class TestC14N < Nokogiri::TestCase
# http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
def test_3_1
doc = Nokogiri.XML <<-eoxml
<?xml version="1.0"?>
<?xml-stylesheet href="doc.xsl"
type="text/xsl" ?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>Hello, world!<!-- Comment 1 --></doc>
<?pi-without-data ?>
<!-- Comment 2 -->
<!-- Comment 3 -->
eoxml
c14n = doc.canonicalize
assert_no_match(/version=/, c14n)
assert_match(/Hello, world/, c14n)
assert_no_match(/Comment/, c14n)
c14n = doc.canonicalize(nil, nil, true)
assert_match(/Comment/, c14n)
c14n = doc.canonicalize(nil, nil, false)
assert_no_match(/Comment/, c14n)
end
def test_exclude_block_params
xml = '<a><b></b></a>'
doc = Nokogiri.XML xml
list = []
doc.canonicalize do |node, parent|
list << [node, parent]
true
end
if Nokogiri.jruby?
assert_equal(
['a', 'document', 'document', nil, 'b', 'a'],
list.flatten.map { |x| x ? x.name : x }
)
else
assert_equal(
['a', 'document', 'document', nil, 'b', 'a', 'a', 'document'],
list.flatten.map { |x| x ? x.name : x }
)
end
end
def test_exclude_block_true
xml = '<a><b></b></a>'
doc = Nokogiri.XML xml
c14n = doc.canonicalize do |node, parent|
true
end
assert_equal xml, c14n
end
def test_exclude_block_false
xml = '<a><b></b></a>'
doc = Nokogiri.XML xml
c14n = doc.canonicalize do |node, parent|
false
end
assert_equal '', c14n
end
def test_exclude_block_nil
xml = '<a><b></b></a>'
doc = Nokogiri.XML xml
c14n = doc.canonicalize do |node, parent|
nil
end
assert_equal '', c14n
end
def test_exclude_block_object
xml = '<a><b></b></a>'
doc = Nokogiri.XML xml
c14n = doc.canonicalize do |node, parent|
Object.new
end
assert_equal xml, c14n
end
def test_c14n_node
xml = '<a><b><c></c></b></a>'
doc = Nokogiri.XML xml
c14n = doc.at_xpath('//b').canonicalize
assert_equal '<b><c></c></b>', c14n
end
def test_c14n_modes
# http://www.w3.org/TR/xml-exc-c14n/#sec-Enveloping
doc1 = Nokogiri.XML <<-EOXML
<n0:local xmlns:n0="http://foobar.org" xmlns:n3="ftp://example.org">
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
<n3:stuff xmlns:n3="ftp://example.org"/>
</n1:elem2>
</n0:local>
EOXML
doc2 = Nokogiri.XML <<-EOXML
<n2:pdu xmlns:n1="http://example.com"
xmlns:n2="http://foo.example"
xmlns:n4="http://foo.example"
xml:lang="fr"
xml:space="retain">
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
<n3:stuff xmlns:n3="ftp://example.org"/>
<n4:stuff />
</n1:elem2>
</n2:pdu>
EOXML
c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize
assert_equal '<n1:elem2 xmlns:n0="http://foobar.org" xmlns:n1="http://example.net" xmlns:n3="ftp://example.org" xml:lang="en">
<n3:stuff></n3:stuff>
</n1:elem2>', c14n
expected = '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain">
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
<n4:stuff></n4:stuff>
</n1:elem2>'
c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize
assert_equal expected, c14n
expected = '<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
</n1:elem2>'
c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0)
assert_equal expected, c14n
expected = '<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
<n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
</n1:elem2>'
c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0)
assert_equal expected, c14n
expected = '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en">
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
<n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
</n1:elem2>'
c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2'])
assert_equal expected, c14n
expected = '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en">
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
<n4:stuff></n4:stuff>
</n1:elem2>'
c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2', 'n4'])
assert_equal expected, c14n
end
def test_wrong_params
xml = '<a><b></b></a>'
doc = Nokogiri.XML xml
assert_raise(TypeError){ doc.canonicalize :wrong_type }
assert_raise(TypeError){ doc.canonicalize nil, :wrong_type }
doc.canonicalize nil, nil, :wrong_type
end
end
end
end
|