Merge pull request #337 from robwilkens/IdleThreadsFixes
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigXsltTransformTest.cs
1 //
2 // XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //      Atsushi Enomoto <atsushi@ximian.com>
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using System;
13 using System.IO;
14 using System.Security.Cryptography;
15 using System.Security.Cryptography.Xml;
16 using System.Text;
17 using System.Xml;
18 using System.Xml.Xsl;
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.Security.Cryptography.Xml {
23
24         // Note: GetInnerXml is protected in XmlDsigXsltTransform making it
25         // difficult to test properly. This class "open it up" :-)
26         public class UnprotectedXmlDsigXsltTransform : XmlDsigXsltTransform {
27                 public UnprotectedXmlDsigXsltTransform ()
28                 {
29                 }
30
31                 public UnprotectedXmlDsigXsltTransform (bool includeComments)
32                         : base (includeComments)
33                 {
34                 }
35
36                 public XmlNodeList UnprotectedGetInnerXml () {
37                         return base.GetInnerXml ();
38                 }
39         }
40
41         [TestFixture]
42         public class XmlDsigXsltTransformTest {
43
44                 protected UnprotectedXmlDsigXsltTransform transform;
45
46                 [SetUp]
47                 protected void SetUp () 
48                 {
49                         transform = new UnprotectedXmlDsigXsltTransform ();
50                 }
51
52                 [Test] // ctor ()
53                 public void Constructor1 ()
54                 {
55                         CheckProperties (transform);
56                 }
57
58                 [Test] // ctor (Boolean)
59                 public void Constructor2 ()
60                 {
61                         transform = new UnprotectedXmlDsigXsltTransform (true);
62                         CheckProperties (transform);
63                         transform = new UnprotectedXmlDsigXsltTransform (false);
64                         CheckProperties (transform);
65                 }
66
67                 void CheckProperties (XmlDsigXsltTransform transform)
68                 {
69                         Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm, "Algorithm");
70
71                         Type[] input = transform.InputTypes;
72                         Assert.IsTrue ((input.Length == 3), "Input #");
73                         // check presence of every supported input types
74                         bool istream = false;
75                         bool ixmldoc = false;
76                         bool ixmlnl = false;
77                         foreach (Type t in input) {
78                                 if (t.ToString () == "System.IO.Stream")
79                                         istream = true;
80                                 if (t.ToString () == "System.Xml.XmlDocument")
81                                         ixmldoc = true;
82                                 if (t.ToString () == "System.Xml.XmlNodeList")
83                                         ixmlnl = true;
84                         }
85                         Assert.IsTrue (istream, "Input Stream");
86                         Assert.IsTrue (ixmldoc, "Input XmlDocument");
87                         Assert.IsTrue (ixmlnl, "Input XmlNodeList");
88
89                         Type[] output = transform.OutputTypes;
90                         Assert.IsTrue ((output.Length == 1), "Output #");
91                         // check presence of every supported output types
92                         bool ostream = false;
93                         foreach (Type t in output) {
94                                 if (t.ToString () == "System.IO.Stream")
95                                         ostream = true;
96                         }
97                         Assert.IsTrue (ostream, "Output Stream");
98                 }
99
100                 [Test]
101                 public void GetInnerXml () 
102                 {
103                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
104                         Assert.IsNull (xnl, "Default InnerXml");
105                 }
106
107                 private string Stream2Array (Stream s) 
108                 {
109                         StringBuilder sb = new StringBuilder ();
110                         int b = s.ReadByte ();
111                         while (b != -1) {
112                                 sb.Append (b.ToString("X2"));
113                                 b = s.ReadByte ();
114                         }
115                         return sb.ToString ();
116                 }
117
118
119                 [Test]
120 #if NET_2_0
121                 [ExpectedException (typeof (ArgumentNullException))]
122 #else
123                 [ExpectedException (typeof (NullReferenceException))]
124 #endif
125                 public void EmptyXslt () 
126                 {
127                         string test = "<Test>XmlDsigXsltTransform</Test>";
128                         XmlDocument doc = new XmlDocument ();
129                         doc.LoadXml (test);
130
131                         transform.LoadInput (doc.ChildNodes);
132                         Stream s = (Stream) transform.GetOutput ();
133                 }
134
135                 [Test]
136                 // Note that this is _valid_ as an "embedded stylesheet".
137                 // (see XSLT spec 2.7)
138                 public void EmbeddedStylesheet () 
139                 {
140                         string test = "<Test xsl:version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</Test>";
141                         XmlDocument doc = new XmlDocument ();
142                         doc.LoadXml (test);
143
144                         transform.LoadInnerXml (doc.ChildNodes);
145                         transform.LoadInput (doc);
146                         Stream s = (Stream) transform.GetOutput ();
147                 }
148
149                 [Test]
150                 public void InvalidXslt () 
151                 {
152                         bool result = false;
153                         try {
154                                 string test = "<xsl:element name='foo' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</xsl:element>";
155                                 XmlDocument doc = new XmlDocument ();
156                                 doc.LoadXml (test);
157
158                                 transform.LoadInnerXml (doc.ChildNodes);
159                                 Stream s = (Stream) transform.GetOutput ();
160                         }
161 #if NET_2_0
162                         catch (Exception e) {
163                                 // we must deal with an internal exception
164                                 result = (e.GetType ().ToString ().EndsWith ("XsltLoadException"));
165                                 result = true;
166 #else
167                         catch (XsltCompileException) {
168                                 result = true;
169 #endif
170                         }
171                         finally {
172                                 Assert.IsTrue (result, "Exception not thrown");
173                         }
174                 }
175
176                 [Test]
177 #if NET_2_0
178                 [ExpectedException (typeof (ArgumentNullException))]
179 #else
180                 [ExpectedException (typeof (NullReferenceException))]
181 #endif
182                 public void OnlyInner () 
183                 {
184                         string test = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
185                         test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
186                         test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
187                         test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
188                         test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
189                         test += "</table></body></html></xsl:template></xsl:stylesheet>";
190                         XmlDocument doc = new XmlDocument ();
191                         doc.LoadXml (test);
192
193                         transform.LoadInnerXml (doc.ChildNodes);
194                         Stream s = (Stream) transform.GetOutput ();
195                         string output = Stream2Array (s);
196                 }
197
198                 private XmlDocument GetXslDoc () 
199                 {
200                         string test = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" xmlns='http://www.w3.org/2000/09/xmldsig#'>";
201                         test += "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
202                         test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
203                         test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
204                         test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
205                         test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
206                         test += "</table></body></html></xsl:template></xsl:stylesheet></Transform>";
207                         XmlDocument doc = new XmlDocument ();
208                         doc.LoadXml (test);
209                         return doc;
210                 }
211
212                 [Test]
213                 public void LoadInputAsXmlDocument () 
214                 {
215                         XmlDocument doc = GetXslDoc ();
216                         transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
217                         transform.LoadInput (doc);
218                         Stream s = (Stream) transform.GetOutput ();
219                         string output = Stream2Array (s);
220                 }
221
222                 [Test]
223                 public void LoadInputAsXmlNodeList () 
224                 {
225                         XmlDocument doc = GetXslDoc ();
226                         transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
227                         transform.LoadInput (doc.ChildNodes);
228                         Stream s = (Stream) transform.GetOutput ();
229                         string output = Stream2Array (s);
230                 }
231
232                 [Test]
233                 public void LoadInputAsStream () 
234                 {
235                         XmlDocument doc = GetXslDoc ();
236                         transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
237                         MemoryStream ms = new MemoryStream ();
238                         doc.Save (ms);
239                         ms.Position = 0;
240                         transform.LoadInput (ms);
241                         Stream s = (Stream) transform.GetOutput ();
242                         string output = Stream2Array (s);
243                 }
244
245                 protected void AreEqual (string msg, XmlNodeList expected, XmlNodeList actual) 
246                 {
247                         for (int i=0; i < expected.Count; i++) {
248                                 if (expected[i].OuterXml != actual[i].OuterXml)
249                                         Assert.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
250                         }
251                 }
252
253                 [Test]
254                 public void LoadInnerXml () 
255                 {
256                         XmlDocument doc = GetXslDoc ();
257                         transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
258                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
259                         Assert.AreEqual (doc.DocumentElement.ChildNodes, xnl, "LoadInnerXml");
260                 }
261
262                 [Test]
263                 public void Load2 () 
264                 {
265                         XmlDocument doc = GetXslDoc ();
266                         transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
267                         transform.LoadInput (doc);
268                         Stream s = (Stream) transform.GetOutput ();
269                         string output = Stream2Array (s);
270                 }
271
272                 [Test]
273                 public void UnsupportedInput () 
274                 {
275                         byte[] bad = { 0xBA, 0xD };
276                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
277                         transform.LoadInput (bad);
278                 }
279
280                 [Test]
281                 [ExpectedException (typeof (ArgumentException))]
282                 public void UnsupportedOutput () 
283                 {
284                         XmlDocument doc = new XmlDocument();
285                         object o = transform.GetOutput (doc.GetType ());
286                 }
287         }
288 }