DataObjectTest.cs 6.0 KB

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