# encoding: UTF-8
require_relative './test_helper'

class TestCanonicalize < Minitest::Test
  def path(file)
    File.join(File.dirname(__FILE__), file)
  end
  
  # (www.w3.org) 3.1 PIs, Comments, and Outside of Document Element
  # http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
  def test_canonicalize_with_w3c_c14n_3_1
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-1.xml'))

    # With Comments
    expected_with_comments = IO.read(self.path('c14n/result/with-comments/example-1'))
    assert_equal(expected_with_comments, given_doc.canonicalize(:comments => true))

    # Without Comments
    expected_without_comments = IO.read(self.path('c14n/result/without-comments/example-1'))
    assert_equal(expected_without_comments, given_doc.canonicalize)
    assert_equal(expected_without_comments, given_doc.canonicalize(:comments => false))

    # Without Comments (XML_C14N_1_1)
    expected_1_1_without_comments = IO.read(self.path('c14n/result/1-1-without-comments/example-1'))
    mode = LibXML::XML::Document::XML_C14N_1_1
    assert_equal(expected_1_1_without_comments, given_doc.canonicalize(:mode => mode))
  end

  # (www.w3.org) 3.2 Whitespace in Document Content
  # http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent
  def test_canonicalize_with_w3c_c14n_3_2
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-2.xml'))
    expected = IO.read(self.path('c14n/result/without-comments/example-2'))
    assert_equal(expected, given_doc.canonicalize)

    expected_1_1_without_comments = IO.read(self.path('c14n/result/1-1-without-comments/example-2'))
    mode = LibXML::XML::Document::XML_C14N_1_1
    assert_equal(expected_1_1_without_comments, given_doc.canonicalize(:mode => mode))
  end

  # (www.w3.org) 3.3 Start and End Tags
  # http://www.w3.org/TR/xml-c14n#Example-SETags
  # (2012-02-20) Test failing due to missing 'attr' in 'e9' node.
  #   - Embedded DTD not parsed out of XML, therefore default attributes are
  #     not applied to canonicalization.
  def test_canonicalize_with_w3c_c14n_3_3
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-3.xml'))
    expected = IO.read(self.path('c14n/result/without-comments/example-3'))
    assert_equal(expected, given_doc.canonicalize)

    expected_1_1_without_comments = IO.read(self.path('c14n/result/1-1-without-comments/example-3'))
    mode = LibXML::XML::Document::XML_C14N_1_1
    assert_equal(expected_1_1_without_comments, given_doc.canonicalize(:mode => mode))
  end

  # (www.w3.org) 3.4 Character Modifications and Character References
  # http://www.w3.org/TR/xml-c14n#Example-Chars
  def test_canonicalize_with_w3c_c14n_3_4
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-4.xml'))
    expected = IO.read(self.path('c14n/result/without-comments/example-4'))
    assert_equal(expected, given_doc.canonicalize)

    expected_1_1_without_comments = IO.read(self.path('c14n/result/1-1-without-comments/example-4'))
    mode = LibXML::XML::Document::XML_C14N_1_1
    assert_equal(expected_1_1_without_comments, given_doc.canonicalize(:mode => mode))
  end

  # (www.w3.org) 3.5 Entity References
  # http://www.w3.org/TR/xml-c14n#Example-Entities
  def test_canonicalize_with_w3c_c14n_3_5
    # NOENT resolves entity references, DTDLOAD loads the external entity
    options = LibXML::XML::Parser::Options::NOENT | LibXML::XML::Parser::Options::DTDLOAD
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-5.xml'), options: options)

    # With Comments
    expected_with_comments = IO.read(self.path('c14n/result/with-comments/example-5'))
    assert_equal(expected_with_comments, given_doc.canonicalize(:comments => true))

    # Without Comments
    expected_without_comments = IO.read(self.path('c14n/result/without-comments/example-5'))
    assert_equal(expected_without_comments, given_doc.canonicalize(:comments => false))

    # Without Comments (XML_C14N_1_1)
    expected_1_1_without_comments = IO.read(self.path('c14n/result/1-1-without-comments/example-5'))
    mode = LibXML::XML::Document::XML_C14N_1_1
    assert_equal(expected_1_1_without_comments, given_doc.canonicalize(:mode => mode))
  end

  # (www.w3.org) 3.6 UTF-8 Encoding
  # http://www.w3.org/TR/xml-c14n#Example-UTF8
  def test_canonicalize_with_w3c_c14n_3_6
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-6.xml'))
    expected = IO.read(self.path('c14n/result/without-comments/example-6'), :encoding => Encoding::UTF_8)
    assert_equal(expected, given_doc.canonicalize)

    expected_1_1_without_comments = IO.read(self.path('c14n/result/1-1-without-comments/example-6'), :encoding => Encoding::UTF_8)
    mode = LibXML::XML::Document::XML_C14N_1_1
    assert_equal(expected_1_1_without_comments, given_doc.canonicalize(:mode => mode))
  end

  # (www.w3.org) 3.7 Document Subsets
  # http://www.w3.org/TR/xml-c14n#Example-DocSubsets
  def test_canonicalize_with_w3c_c14n_3_7
    # Parse with DTDATTR and DTDLOAD so default attributes and ID types
    # from the internal DTD subset are materialized
    options = LibXML::XML::Parser::Options::DTDATTR | LibXML::XML::Parser::Options::DTDLOAD
    given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-7.xml'),
                                           options: options)
    expected = IO.read(self.path('c14n/result/without-comments/example-7'))

    ctx = given_doc.context
    ctx.register_namespace('ietf', 'http://www.ietf.org')

    # W3C C14N spec example 3.7 XPath expression for document subsets
    subdoc_nodes = ctx.find(
      "(//. | //@* | //namespace::*)" \
      "[self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2))" \
      " or count(id('E3')|ancestor-or-self::node()) = count(ancestor-or-self::node())]")

    assert_equal(expected, given_doc.canonicalize(:nodes => subdoc_nodes))
  end
end
