\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.2/test/xml/

Viewing File: test_c14n.rb

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