File: test_canonicalize.rb

package info (click to toggle)
ruby-libxml 5.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,804 kB
  • sloc: xml: 18,263; ansic: 7,669; ruby: 5,809; makefile: 6
file content (120 lines) | stat: -rw-r--r-- 5,884 bytes parent folder | download | duplicates (4)
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
# 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#test_canonicalize_with_w3c_c14n_3_1


  # (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
  # (2012-02-20) Failing likely due to a logic error
  #   - libxml2(c14n.c:1788) XML_ENTITY_REF_NODE is invalid node for parsing.
  def test_canonicalize_with_w3c_c14n_3_5
    #given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-5.xml'))

    # With Comments
    #expected_with_comments = IO.read(self.path('c14n/result/with-comments/example-5'))

    # TODO - CANNOT COMPLETE TEST unless libxml2 supports additional node types.
    #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'))
    # TODO - CANNOT COMPLETE TEST unless libxml2 supports additional node types.
    #assert_equal(expected_without_comments, given_doc.canonicalize(:comments => false))
    #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

    # TODO - CANNOT COMPLETE TEST unless libxml2 supports additional node types.
    #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
    # Non Canonicalized Document
    # given_doc = LibXML::XML::Document.file(self.path('c14n/given/example-7.xml'))
    #expected = IO.read(self.path('c14n/result/without-comments/example-7'))

    # e1_node = given_doc.find_first('ietf:e1', 'ietf:http://www.ietf.org')

    # Select current node, all child nodes, all attributes and namespace nodes
    #subdoc_nodes = e1_node.find("(.//.|.//@id|namespace::*)")

    # TODO - This fails because the namespace nodes aren't taken into account
    # assert_equal(expected, given_doc.canonicalize(:nodes => subdoc_nodes))
  end
end