2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / DataObjectTest.cs
1 //
2 // DataObjectTest.cs - NUnit 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
12 using System;
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
15 using System.Xml;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Security.Cryptography.Xml {
20
21         [TestFixture]
22         public class DataObjectTest {
23
24                 [Test]
25                 public void NewDataObject () 
26                 {
27                         string test = "<Test>DataObject</Test>";
28                         XmlDocument doc = new XmlDocument ();
29                         doc.LoadXml (test);
30
31                         DataObject obj1 = new DataObject ();
32                         Assert.IsTrue ((obj1.Data.Count == 0), "Data.Count==0");
33                         Assert.AreEqual ("<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml), "Just constructed");
34
35                         obj1.Id = "id";
36                         obj1.MimeType = "mime";
37                         obj1.Encoding = "encoding";
38                         Assert.AreEqual ("<Object Id=\"id\" MimeType=\"mime\" Encoding=\"encoding\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", (obj1.GetXml ().OuterXml), "Only attributes");
39
40                         obj1.Data = doc.ChildNodes;
41                         Assert.IsTrue ((obj1.Data.Count == 1), "Data.Count==1");
42
43                         XmlElement xel = obj1.GetXml ();
44
45                         DataObject obj2 = new DataObject ();
46                         obj2.LoadXml (xel);
47                         Assert.AreEqual ((obj1.GetXml ().OuterXml), (obj2.GetXml ().OuterXml), "obj1==obj2");
48
49                         DataObject obj3 = new DataObject (obj1.Id, obj1.MimeType, obj1.Encoding, doc.DocumentElement);
50                         Assert.AreEqual ((obj2.GetXml ().OuterXml), (obj3.GetXml ().OuterXml), "obj2==obj3");
51                 }
52
53                 [Test]
54                 public void ImportDataObject () 
55                 {
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 ();
58                         doc.LoadXml (value1);
59
60                         DataObject obj1 = new DataObject ();
61                         obj1.LoadXml (doc.DocumentElement);
62                         Assert.IsTrue ((obj1.Data.Count == 2), "Data.Count==2");
63
64                         string s = (obj1.GetXml ().OuterXml);
65                         Assert.AreEqual (value1, s, "DataObject 1");
66
67                         string value2 = "<Object xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
68                         doc = new XmlDocument ();
69                         doc.LoadXml (value2);
70
71                         DataObject obj2 = new DataObject ();
72                         obj2.LoadXml (doc.DocumentElement);
73
74                         s = (obj2.GetXml ().OuterXml);
75                         Assert.AreEqual (value2, s, "DataObject 2");
76
77                         string value3 = "<Object Id=\"id\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
78                         doc = new XmlDocument ();
79                         doc.LoadXml (value3);
80
81                         DataObject obj3 = new DataObject ();
82                         obj3.LoadXml (doc.DocumentElement);
83
84                         s = (obj3.GetXml ().OuterXml);
85                         Assert.AreEqual (value3, s, "DataObject 3");
86
87                         string value4 = "<Object MimeType=\"mime\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><Test xmlns=\"\" /></Object>";
88                         doc = new XmlDocument ();
89                         doc.LoadXml (value4);
90
91                         DataObject obj4 = new DataObject ();
92                         obj4.LoadXml (doc.DocumentElement);
93
94                         s = (obj4.GetXml ().OuterXml);
95                         Assert.AreEqual (value4, s, "DataObject 4");
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void InvalidDataObject1 () 
101                 {
102                         DataObject obj1 = new DataObject ();
103                         obj1.Data = null;
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentNullException))]
108                 public void InvalidDataObject2 () 
109                 {
110                         DataObject obj1 = new DataObject ();
111                         obj1.LoadXml (null);
112                 }
113
114                 [Test]
115                 public void InvalidDataObject3 () 
116                 {
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 ();
122                         doc.LoadXml (value);
123                         obj1.LoadXml (doc.DocumentElement);
124                         string s = (obj1.GetXml ().OuterXml);
125                         Assert.AreEqual (value, s, "DataObject Bad");
126                 }
127
128                 [Test]
129                 public void GetXmlKeepDocument ()
130                 {
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                         Assert.AreEqual (doc, el2.OwnerDocument, "Document is kept unless setting properties");
139                 }
140
141                 [Test]
142                 public void PropertySetMakesDocumentDifferent ()
143                 {
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);
149                         obj.Id = "hogehoge";
150                         XmlElement el2 = obj.GetXml ();
151                         Assert.IsTrue (doc != el2.OwnerDocument, "Document is not kept when properties are set");
152                 }
153
154                 [Test]
155                 public void EnvelopedObject ()
156                 {
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);
161                         obj.Id = "hoge";
162                         obj.MimeType = "application/octet-stream";
163                         obj.Encoding = "euc-kr";
164                         XmlElement el1 = obj.GetXml ();
165                         Assert.AreEqual ("<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                         Assert.AreEqual (String.Empty, el1.OwnerDocument.OuterXml);
170                         */
171                 }
172
173                 [Test]
174                 public void SetDataAfterId ()
175                 {
176                         DataObject d = new DataObject ();
177                         XmlElement el = new XmlDocument ().CreateElement ("foo");
178                         d.Id = "id:1";
179                         d.Data = el.SelectNodes (".");
180                         Assert.AreEqual ("id:1", d.Id);
181                 }
182
183                 [Test]
184                 public void SetMimeTypeAfterId ()
185                 {
186                         XmlElement el = new XmlDocument ().CreateElement ("foo");
187                         DataObject d = new DataObject ("id:1", null, null, el);
188                         d.MimeType = "text/html";
189                         Assert.AreEqual ("id:1", d.Id);
190                 }
191         }
192 }