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