DataObjectTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // DataObjectTest.cs - NUnit Test Cases for DataObject
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  9. // (C) 2004 Novell Inc.
  10. //
  11. using System;
  12. using System.Security.Cryptography;
  13. using System.Security.Cryptography.Xml;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Security.Cryptography.Xml {
  17. [TestFixture]
  18. public class DataObjectTest : Assertion {
  19. [Test]
  20. public void NewDataObject ()
  21. {
  22. string test = "<Test>DataObject</Test>";
  23. XmlDocument doc = new XmlDocument ();
  24. doc.LoadXml (test);
  25. DataObject obj1 = new DataObject ();
  26. Assert ("Data.Count==0", (obj1.Data.Count == 0));
  27. AssertEquals ("Just constructed", "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml));
  28. obj1.Id = "id";
  29. obj1.MimeType = "mime";
  30. obj1.Encoding = "encoding";
  31. AssertEquals ("Only attributes", "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml));
  32. obj1.Data = doc.ChildNodes;
  33. Assert ("Data.Count==1", (obj1.Data.Count == 1));
  34. XmlElement xel = obj1.GetXml ();
  35. DataObject obj2 = new DataObject ();
  36. obj2.LoadXml (xel);
  37. AssertEquals ("obj1==obj2", (obj1.GetXml ().OuterXml), (obj2.GetXml ().OuterXml));
  38. DataObject obj3 = new DataObject (obj1.Id, obj1.MimeType, obj1.Encoding, doc.DocumentElement);
  39. AssertEquals ("obj2==obj3", (obj2.GetXml ().OuterXml), (obj3.GetXml ().OuterXml));
  40. }
  41. [Test]
  42. public void ImportDataObject ()
  43. {
  44. string value1 = "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\">DataObject1</Test><Test xmlns=\"\">DataObject2</Test></Object>";
  45. XmlDocument doc = new XmlDocument ();
  46. doc.LoadXml (value1);
  47. DataObject obj1 = new DataObject ();
  48. obj1.LoadXml (doc.DocumentElement);
  49. Assert ("Data.Count==2", (obj1.Data.Count == 2));
  50. string s = (obj1.GetXml ().OuterXml);
  51. AssertEquals ("DataObject 1", value1, s);
  52. string value2 = "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  53. doc = new XmlDocument ();
  54. doc.LoadXml (value2);
  55. DataObject obj2 = new DataObject ();
  56. obj2.LoadXml (doc.DocumentElement);
  57. s = (obj2.GetXml ().OuterXml);
  58. AssertEquals ("DataObject 2", value2, s);
  59. string value3 = "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  60. doc = new XmlDocument ();
  61. doc.LoadXml (value3);
  62. DataObject obj3 = new DataObject ();
  63. obj3.LoadXml (doc.DocumentElement);
  64. s = (obj3.GetXml ().OuterXml);
  65. AssertEquals ("DataObject 3", value3, s);
  66. string value4 = "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  67. doc = new XmlDocument ();
  68. doc.LoadXml (value4);
  69. DataObject obj4 = new DataObject ();
  70. obj4.LoadXml (doc.DocumentElement);
  71. s = (obj4.GetXml ().OuterXml);
  72. AssertEquals ("DataObject 4", value4, s);
  73. }
  74. [Test]
  75. [ExpectedException (typeof (ArgumentNullException))]
  76. public void InvalidDataObject1 ()
  77. {
  78. DataObject obj1 = new DataObject ();
  79. obj1.Data = null;
  80. }
  81. [Test]
  82. [ExpectedException (typeof (ArgumentNullException))]
  83. public void InvalidDataObject2 ()
  84. {
  85. DataObject obj1 = new DataObject ();
  86. obj1.LoadXml (null);
  87. }
  88. [Test]
  89. public void InvalidDataObject3 ()
  90. {
  91. DataObject obj1 = new DataObject ();
  92. // seems this isn't invalid !?!
  93. // but no exception is thrown
  94. string value = "<Test>Bad</Test>";
  95. XmlDocument doc = new XmlDocument ();
  96. doc.LoadXml (value);
  97. obj1.LoadXml (doc.DocumentElement);
  98. string s = (obj1.GetXml ().OuterXml);
  99. AssertEquals ("DataObject Bad", value, s);
  100. }
  101. [Test]
  102. public void GetXmlKeepDocument ()
  103. {
  104. XmlDocument doc = new XmlDocument ();
  105. doc.LoadXml ("<Object xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object>");
  106. DataObject obj = new DataObject ();
  107. XmlElement el1 = obj.GetXml ();
  108. obj.LoadXml (doc.DocumentElement);
  109. // obj.Id = "hogehoge";
  110. XmlElement el2 = obj.GetXml ();
  111. AssertEquals ("Document is kept unless setting properties", doc, el2.OwnerDocument);
  112. }
  113. [Test]
  114. public void PropertySetMakesDocumentDifferent ()
  115. {
  116. XmlDocument doc = new XmlDocument ();
  117. doc.LoadXml ("<Object xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object>");
  118. DataObject obj = new DataObject ();
  119. XmlElement el1 = obj.GetXml ();
  120. obj.LoadXml (doc.DocumentElement);
  121. obj.Id = "hogehoge";
  122. XmlElement el2 = obj.GetXml ();
  123. Assert ("Document is not kept when properties are set", doc != el2.OwnerDocument);
  124. }
  125. [Test]
  126. public void EnvelopedObject ()
  127. {
  128. XmlDocument doc = new XmlDocument ();
  129. doc.LoadXml ("<envelope><Object xmlns:dsig='http://www.w3.org/2000/09/xmldsig#' xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object></envelope>");
  130. DataObject obj = new DataObject ();
  131. obj.LoadXml (doc.DocumentElement.FirstChild as XmlElement);
  132. obj.Id = "hoge";
  133. obj.MimeType = "application/octet-stream";
  134. obj.Encoding = "euc-kr";
  135. XmlElement el1 = obj.GetXml ();
  136. AssertEquals ("<Object Id=\"hoge\" MimeType=\"application/octet-stream\" Encoding=\"euc-kr\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\">test</Object>", el1.OuterXml);
  137. /* looks curious? but the element does not look to
  138. be appended to the document.
  139. Just commented out since it is not fixed.
  140. AssertEquals (String.Empty, el1.OwnerDocument.OuterXml);
  141. */
  142. }
  143. }
  144. }