2 // DataObjectTest.cs - NUnit Test Cases for DataObject
5 // Sebastien Pouliot (spouliot@motus.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell Inc.
13 using System
.Security
.Cryptography
;
14 using System
.Security
.Cryptography
.Xml
;
17 using NUnit
.Framework
;
19 namespace MonoTests
.System
.Security
.Cryptography
.Xml
{
22 public class DataObjectTest
: Assertion
{
25 public void NewDataObject ()
27 string test
= "<Test>DataObject</Test>";
28 XmlDocument doc
= new XmlDocument ();
31 DataObject obj1
= new DataObject ();
32 Assert ("Data.Count==0", (obj1
.Data
.Count
== 0));
33 AssertEquals ("Just constructed", "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1
.GetXml ().OuterXml
));
36 obj1
.MimeType
= "mime";
37 obj1
.Encoding
= "encoding";
38 AssertEquals ("Only attributes", "<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1
.GetXml ().OuterXml
));
40 obj1
.Data
= doc
.ChildNodes
;
41 Assert ("Data.Count==1", (obj1
.Data
.Count
== 1));
43 XmlElement xel
= obj1
.GetXml ();
45 DataObject obj2
= new DataObject ();
47 AssertEquals ("obj1==obj2", (obj1
.GetXml ().OuterXml
), (obj2
.GetXml ().OuterXml
));
49 DataObject obj3
= new DataObject (obj1
.Id
, obj1
.MimeType
, obj1
.Encoding
, doc
.DocumentElement
);
50 AssertEquals ("obj2==obj3", (obj2
.GetXml ().OuterXml
), (obj3
.GetXml ().OuterXml
));
54 public void ImportDataObject ()
56 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>";
57 XmlDocument doc
= new XmlDocument ();
60 DataObject obj1
= new DataObject ();
61 obj1
.LoadXml (doc
.DocumentElement
);
62 Assert ("Data.Count==2", (obj1
.Data
.Count
== 2));
64 string s
= (obj1
.GetXml ().OuterXml
);
65 AssertEquals ("DataObject 1", value1
, s
);
67 string value2
= "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
68 doc
= new XmlDocument ();
71 DataObject obj2
= new DataObject ();
72 obj2
.LoadXml (doc
.DocumentElement
);
74 s
= (obj2
.GetXml ().OuterXml
);
75 AssertEquals ("DataObject 2", value2
, s
);
77 string value3
= "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
78 doc
= new XmlDocument ();
81 DataObject obj3
= new DataObject ();
82 obj3
.LoadXml (doc
.DocumentElement
);
84 s
= (obj3
.GetXml ().OuterXml
);
85 AssertEquals ("DataObject 3", value3
, s
);
87 string value4
= "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
88 doc
= new XmlDocument ();
91 DataObject obj4
= new DataObject ();
92 obj4
.LoadXml (doc
.DocumentElement
);
94 s
= (obj4
.GetXml ().OuterXml
);
95 AssertEquals ("DataObject 4", value4
, s
);
99 [ExpectedException (typeof (ArgumentNullException
))]
100 public void InvalidDataObject1 ()
102 DataObject obj1
= new DataObject ();
107 [ExpectedException (typeof (ArgumentNullException
))]
108 public void InvalidDataObject2 ()
110 DataObject obj1
= new DataObject ();
115 public void InvalidDataObject3 ()
117 DataObject obj1
= new DataObject ();
118 // seems this isn't invalid !?!
119 // but no exception is thrown
120 string value = "<Test>Bad</Test>";
121 XmlDocument doc
= new XmlDocument ();
123 obj1
.LoadXml (doc
.DocumentElement
);
124 string s
= (obj1
.GetXml ().OuterXml
);
125 AssertEquals ("DataObject Bad", value, s
);
129 public void GetXmlKeepDocument ()
131 XmlDocument doc
= new XmlDocument ();
132 doc
.LoadXml ("<Object xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object>");
133 DataObject obj
= new DataObject ();
134 XmlElement el1
= obj
.GetXml ();
135 obj
.LoadXml (doc
.DocumentElement
);
136 // obj.Id = "hogehoge";
137 XmlElement el2
= obj
.GetXml ();
138 AssertEquals ("Document is kept unless setting properties", doc
, el2
.OwnerDocument
);
142 public void PropertySetMakesDocumentDifferent ()
144 XmlDocument doc
= new XmlDocument ();
145 doc
.LoadXml ("<Object xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object>");
146 DataObject obj
= new DataObject ();
147 XmlElement el1
= obj
.GetXml ();
148 obj
.LoadXml (doc
.DocumentElement
);
150 XmlElement el2
= obj
.GetXml ();
151 Assert ("Document is not kept when properties are set", doc
!= el2
.OwnerDocument
);
155 public void EnvelopedObject ()
157 XmlDocument doc
= new XmlDocument ();
158 doc
.LoadXml ("<envelope><Object xmlns:dsig='http://www.w3.org/2000/09/xmldsig#' xmlns='http://www.w3.org/2000/09/xmldsig#'>test</Object></envelope>");
159 DataObject obj
= new DataObject ();
160 obj
.LoadXml (doc
.DocumentElement
.FirstChild
as XmlElement
);
162 obj
.MimeType
= "application/octet-stream";
163 obj
.Encoding
= "euc-kr";
164 XmlElement el1
= obj
.GetXml ();
165 AssertEquals ("<Object Id=\"hoge\" MimeType=\"application/octet-stream\" Encoding=\"euc-kr\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\">test</Object>", el1
.OuterXml
);
166 /* looks curious? but the element does not look to
167 be appended to the document.
168 Just commented out since it is not fixed.
169 AssertEquals (String.Empty, el1.OwnerDocument.OuterXml);