PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/activesupport/test/xml_mini/libxml_engine_test.rb

http://github.com/IronLanguages/main
Ruby | 203 lines | 176 code | 26 blank | 1 comment | 0 complexity | 7f56d93ba77284e0d627747ebee08f0c MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require 'active_support/xml_mini'
  3. require 'active_support/core_ext/hash/conversions'
  4. begin
  5. require 'libxml'
  6. rescue LoadError
  7. # Skip libxml tests
  8. else
  9. class LibxmlEngineTest < Test::Unit::TestCase
  10. include ActiveSupport
  11. def setup
  12. @default_backend = XmlMini.backend
  13. XmlMini.backend = 'LibXML'
  14. LibXML::XML::Error.set_handler(&lambda { |error| }) #silence libxml, exceptions will do
  15. end
  16. def teardown
  17. XmlMini.backend = @default_backend
  18. end
  19. def test_exception_thrown_on_expansion_attack
  20. assert_raise LibXML::XML::Error do
  21. attack_xml = %{<?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE member [
  23. <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
  24. <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
  25. <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
  26. <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
  27. <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
  28. <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
  29. <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
  30. ]>
  31. <member>
  32. &a;
  33. </member>
  34. }
  35. Hash.from_xml(attack_xml)
  36. end
  37. end
  38. def test_setting_libxml_as_backend
  39. XmlMini.backend = 'LibXML'
  40. assert_equal XmlMini_LibXML, XmlMini.backend
  41. end
  42. def test_blank_returns_empty_hash
  43. assert_equal({}, XmlMini.parse(nil))
  44. assert_equal({}, XmlMini.parse(''))
  45. end
  46. def test_array_type_makes_an_array
  47. assert_equal_rexml(<<-eoxml)
  48. <blog>
  49. <posts type="array">
  50. <post>a post</post>
  51. <post>another post</post>
  52. </posts>
  53. </blog>
  54. eoxml
  55. end
  56. def test_one_node_document_as_hash
  57. assert_equal_rexml(<<-eoxml)
  58. <products/>
  59. eoxml
  60. end
  61. def test_one_node_with_attributes_document_as_hash
  62. assert_equal_rexml(<<-eoxml)
  63. <products foo="bar"/>
  64. eoxml
  65. end
  66. def test_products_node_with_book_node_as_hash
  67. assert_equal_rexml(<<-eoxml)
  68. <products>
  69. <book name="awesome" id="12345" />
  70. </products>
  71. eoxml
  72. end
  73. def test_products_node_with_two_book_nodes_as_hash
  74. assert_equal_rexml(<<-eoxml)
  75. <products>
  76. <book name="awesome" id="12345" />
  77. <book name="america" id="67890" />
  78. </products>
  79. eoxml
  80. end
  81. def test_single_node_with_content_as_hash
  82. assert_equal_rexml(<<-eoxml)
  83. <products>
  84. hello world
  85. </products>
  86. eoxml
  87. end
  88. def test_children_with_children
  89. assert_equal_rexml(<<-eoxml)
  90. <root>
  91. <products>
  92. <book name="america" id="67890" />
  93. </products>
  94. </root>
  95. eoxml
  96. end
  97. def test_children_with_text
  98. assert_equal_rexml(<<-eoxml)
  99. <root>
  100. <products>
  101. hello everyone
  102. </products>
  103. </root>
  104. eoxml
  105. end
  106. def test_children_with_non_adjacent_text
  107. assert_equal_rexml(<<-eoxml)
  108. <root>
  109. good
  110. <products>
  111. hello everyone
  112. </products>
  113. morning
  114. </root>
  115. eoxml
  116. end
  117. def test_parse_from_io
  118. io = StringIO.new(<<-eoxml)
  119. <root>
  120. good
  121. <products>
  122. hello everyone
  123. </products>
  124. morning
  125. </root>
  126. eoxml
  127. XmlMini.parse(io)
  128. end
  129. def test_children_with_simple_cdata
  130. assert_equal_rexml(<<-eoxml)
  131. <root>
  132. <products>
  133. <![CDATA[cdatablock]]>
  134. </products>
  135. </root>
  136. eoxml
  137. end
  138. def test_children_with_multiple_cdata
  139. assert_equal_rexml(<<-eoxml)
  140. <root>
  141. <products>
  142. <![CDATA[cdatablock1]]><![CDATA[cdatablock2]]>
  143. </products>
  144. </root>
  145. eoxml
  146. end
  147. def test_children_with_text_and_cdata
  148. assert_equal_rexml(<<-eoxml)
  149. <root>
  150. <products>
  151. hello <![CDATA[cdatablock]]>
  152. morning
  153. </products>
  154. </root>
  155. eoxml
  156. end
  157. def test_children_with_blank_text
  158. assert_equal_rexml(<<-eoxml)
  159. <root>
  160. <products> </products>
  161. </root>
  162. eoxml
  163. end
  164. def test_children_with_blank_text_and_attribute
  165. assert_equal_rexml(<<-eoxml)
  166. <root>
  167. <products type="file"> </products>
  168. </root>
  169. eoxml
  170. end
  171. private
  172. def assert_equal_rexml(xml)
  173. hash = XmlMini.with_backend('REXML') { XmlMini.parse(xml) }
  174. assert_equal(hash, XmlMini.parse(xml))
  175. end
  176. end
  177. end