DataObjectTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // DataObjectTest.cs - NUnit Test Cases for DataObject
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Xml;
  14. namespace MonoTests.System.Security.Cryptography.Xml {
  15. public class DataObjectTest : TestCase {
  16. public DataObjectTest () : base ("System.Security.Cryptography.Xml.DataObject testsuite") {}
  17. public DataObjectTest (string name) : base (name) {}
  18. protected override void SetUp () {}
  19. protected override void TearDown () {}
  20. public static ITest Suite {
  21. get {
  22. return new TestSuite (typeof (DataObjectTest));
  23. }
  24. }
  25. public void TestNewDataObject ()
  26. {
  27. string test = "<Test>DataObject</Test>";
  28. XmlDocument doc = new XmlDocument ();
  29. doc.LoadXml (test);
  30. DataObject obj1 = new DataObject ();
  31. Assert ("Data.Count==0", (obj1.Data.Count == 0));
  32. obj1.Id = "id";
  33. obj1.MimeType = "mime";
  34. obj1.Encoding = "encoding";
  35. AssertEquals ("Only attributes", "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml));
  36. obj1.Data = doc.ChildNodes;
  37. Assert ("Data.Count==1", (obj1.Data.Count == 1));
  38. XmlElement xel = obj1.GetXml ();
  39. DataObject obj2 = new DataObject ();
  40. obj2.LoadXml (xel);
  41. AssertEquals ("obj1==obj2", (obj1.GetXml ().OuterXml), (obj2.GetXml ().OuterXml));
  42. DataObject obj3 = new DataObject (obj1.Id, obj1.MimeType, obj1.Encoding, doc.DocumentElement);
  43. AssertEquals ("obj2==obj3", (obj2.GetXml ().OuterXml), (obj3.GetXml ().OuterXml));
  44. }
  45. public void TestImportDataObject ()
  46. {
  47. 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>";
  48. XmlDocument doc = new XmlDocument ();
  49. doc.LoadXml (value1);
  50. DataObject obj1 = new DataObject ();
  51. obj1.LoadXml (doc.DocumentElement);
  52. Assert ("Data.Count==2", (obj1.Data.Count == 2));
  53. string s = (obj1.GetXml ().OuterXml);
  54. AssertEquals ("DataObject 1", value1, s);
  55. string value2 = "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  56. doc = new XmlDocument ();
  57. doc.LoadXml (value2);
  58. DataObject obj2 = new DataObject ();
  59. obj2.LoadXml (doc.DocumentElement);
  60. s = (obj2.GetXml ().OuterXml);
  61. AssertEquals ("DataObject 2", value2, s);
  62. string value3 = "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  63. doc = new XmlDocument ();
  64. doc.LoadXml (value3);
  65. DataObject obj3 = new DataObject ();
  66. obj3.LoadXml (doc.DocumentElement);
  67. s = (obj3.GetXml ().OuterXml);
  68. AssertEquals ("DataObject 3", value3, s);
  69. string value4 = "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
  70. doc = new XmlDocument ();
  71. doc.LoadXml (value4);
  72. DataObject obj4 = new DataObject ();
  73. obj4.LoadXml (doc.DocumentElement);
  74. s = (obj4.GetXml ().OuterXml);
  75. AssertEquals ("DataObject 4", value4, s);
  76. }
  77. public void TestInvalidDataObject ()
  78. {
  79. DataObject obj1 = new DataObject ();
  80. try {
  81. obj1.Data = null;
  82. Fail ("Expected ArgumentNullException but none");
  83. }
  84. catch (ArgumentNullException) {
  85. // this is expected
  86. }
  87. catch (Exception e) {
  88. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  89. }
  90. try {
  91. obj1.LoadXml (null);
  92. Fail ("Expected ArgumentNullException but none");
  93. }
  94. catch (ArgumentNullException) {
  95. // this is expected
  96. }
  97. catch (Exception e) {
  98. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  99. }
  100. // seems this isn't invalid !?!
  101. string value = "<Test>Bad</Test>";
  102. XmlDocument doc = new XmlDocument ();
  103. doc.LoadXml (value);
  104. obj1.LoadXml (doc.DocumentElement);
  105. string s = (obj1.GetXml ().OuterXml);
  106. AssertEquals ("DataObject Bad", value, s);
  107. }
  108. }
  109. }