1 // 2 // DataObjectTest.cs - Test Cases for DataObject 3 // 4 // Author: 5 // Sebastien Pouliot (spouliot@motus.com) 6 // Atsushi Enomoto (atsushi@ximian.com) 7 // 8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) 9 // (C) 2004 Novell Inc. 10 // 11 // Licensed to the .NET Foundation under one or more agreements. 12 // See the LICENSE file in the project root for more information. 13 14 using System.Xml; 15 using Xunit; 16 17 namespace System.Security.Cryptography.Xml.Tests 18 { 19 20 public class DataObjectTest 21 { 22 23 [Fact] NewDataObject()24 public void NewDataObject() 25 { 26 string test = "<Test>DataObject</Test>"; 27 XmlDocument doc = new XmlDocument(); 28 doc.LoadXml(test); 29 30 DataObject obj1 = new DataObject(); 31 Assert.True((obj1.Data.Count == 0), "Data.Count==0"); 32 Assert.Equal("<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml().OuterXml)); 33 34 obj1.Id = "id"; 35 obj1.MimeType = "mime"; 36 obj1.Encoding = "encoding"; 37 Assert.Equal("<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml().OuterXml)); 38 39 obj1.Data = doc.ChildNodes; 40 Assert.True((obj1.Data.Count == 1), "Data.Count==1"); 41 42 XmlElement xel = obj1.GetXml(); 43 44 DataObject obj2 = new DataObject(); 45 obj2.LoadXml(xel); 46 Assert.Equal((obj1.GetXml().OuterXml), (obj2.GetXml().OuterXml)); 47 48 DataObject obj3 = new DataObject(obj1.Id, obj1.MimeType, obj1.Encoding, doc.DocumentElement); 49 Assert.Equal((obj2.GetXml().OuterXml), (obj3.GetXml().OuterXml)); 50 } 51 52 [Fact] ImportDataObject()53 public void ImportDataObject() 54 { 55 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>"; 56 XmlDocument doc = new XmlDocument(); 57 doc.LoadXml(value1); 58 59 DataObject obj1 = new DataObject(); 60 obj1.LoadXml(doc.DocumentElement); 61 Assert.True((obj1.Data.Count == 2), "Data.Count==2"); 62 63 string s = (obj1.GetXml().OuterXml); 64 Assert.Equal(value1, s); 65 66 string value2 = "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>"; 67 doc = new XmlDocument(); 68 doc.LoadXml(value2); 69 70 DataObject obj2 = new DataObject(); 71 obj2.LoadXml(doc.DocumentElement); 72 73 s = (obj2.GetXml().OuterXml); 74 Assert.Equal(value2, s); 75 76 string value3 = "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>"; 77 doc = new XmlDocument(); 78 doc.LoadXml(value3); 79 80 DataObject obj3 = new DataObject(); 81 obj3.LoadXml(doc.DocumentElement); 82 83 s = (obj3.GetXml().OuterXml); 84 Assert.Equal(value3, s); 85 86 string value4 = "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>"; 87 doc = new XmlDocument(); 88 doc.LoadXml(value4); 89 90 DataObject obj4 = new DataObject(); 91 obj4.LoadXml(doc.DocumentElement); 92 93 s = (obj4.GetXml().OuterXml); 94 Assert.Equal(value4, s); 95 } 96 97 [Fact] InvalidDataObject1()98 public void InvalidDataObject1() 99 { 100 DataObject obj1 = new DataObject(); 101 Assert.Throws<ArgumentNullException>(() => obj1.Data = null); 102 } 103 104 [Fact] InvalidDataObject2()105 public void InvalidDataObject2() 106 { 107 DataObject obj1 = new DataObject(); 108 Assert.Throws<ArgumentNullException>(() => obj1.LoadXml(null)); 109 } 110 111 [Fact] InvalidDataObject3()112 public void InvalidDataObject3() 113 { 114 DataObject obj1 = new DataObject(); 115 // seems this isn't invalid !?! 116 // but no exception is thrown 117 string value = "<Test>Bad</Test>"; 118 XmlDocument doc = new XmlDocument(); 119 doc.LoadXml(value); 120 obj1.LoadXml(doc.DocumentElement); 121 string s = (obj1.GetXml().OuterXml); 122 Assert.Equal(value, s); 123 } 124 125 [Fact] GetXmlKeepDocument()126 public void GetXmlKeepDocument() 127 { 128 XmlDocument doc = new XmlDocument(); 129 doc.LoadXml("<Object xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object>"); 130 DataObject obj = new DataObject(); 131 XmlElement el1 = obj.GetXml(); 132 obj.LoadXml(doc.DocumentElement); 133 // obj.Id = "hogehoge"; 134 XmlElement el2 = obj.GetXml(); 135 Assert.Equal(doc, el2.OwnerDocument); 136 } 137 138 [Fact] PropertySetMakesDocumentDifferent()139 public void PropertySetMakesDocumentDifferent() 140 { 141 XmlDocument doc = new XmlDocument(); 142 doc.LoadXml("<Object xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object>"); 143 DataObject obj = new DataObject(); 144 XmlElement el1 = obj.GetXml(); 145 obj.LoadXml(doc.DocumentElement); 146 obj.Id = "hogehoge"; 147 XmlElement el2 = obj.GetXml(); 148 Assert.True(doc != el2.OwnerDocument, "Document is not kept when properties are set"); 149 } 150 151 [Fact] EnvelopedObject()152 public void EnvelopedObject() 153 { 154 XmlDocument doc = new XmlDocument(); 155 doc.LoadXml("<envelope><Object xmlns:dsig='http://www.w3.org/2000/09/xmldsig#' xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object></envelope>"); 156 DataObject obj = new DataObject(); 157 obj.LoadXml(doc.DocumentElement.FirstChild as XmlElement); 158 obj.Id = "hoge"; 159 obj.MimeType = "application/octet-stream"; 160 obj.Encoding = "euc-kr"; 161 XmlElement el1 = obj.GetXml(); 162 Assert.Equal("<Object Id=\"hoge\" MimeType=\"application/octet-stream\" Encoding=\"euc-kr\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\">test</Object>", el1.OuterXml); 163 /* looks curious? but the element does not look to 164 be appended to the document. 165 Just commented out since it is not fixed. 166 Assert.AreEqual (String.Empty, el1.OwnerDocument.OuterXml); 167 */ 168 } 169 170 [Fact] SetDataAfterId()171 public void SetDataAfterId() 172 { 173 DataObject d = new DataObject(); 174 XmlElement el = new XmlDocument().CreateElement("foo"); 175 d.Id = "id:1"; 176 d.Data = el.SelectNodes("."); 177 Assert.Equal("id:1", d.Id); 178 } 179 180 [Fact] SetMimeTypeAfterId()181 public void SetMimeTypeAfterId() 182 { 183 XmlElement el = new XmlDocument().CreateElement("foo"); 184 DataObject d = new DataObject("id:1", null, null, el); 185 d.MimeType = "text/html"; 186 Assert.Equal("id:1", d.Id); 187 } 188 } 189 } 190