2 // XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell (http://www.novell.com)
14 using System.Security.Cryptography;
15 using System.Security.Cryptography.Xml;
20 using NUnit.Framework;
22 namespace MonoTests.System.Security.Cryptography.Xml {
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 ()
31 public UnprotectedXmlDsigXsltTransform (bool includeComments)
32 : base (includeComments)
36 public XmlNodeList UnprotectedGetInnerXml () {
37 return base.GetInnerXml ();
42 public class XmlDsigXsltTransformTest {
44 protected UnprotectedXmlDsigXsltTransform transform;
47 protected void SetUp ()
49 transform = new UnprotectedXmlDsigXsltTransform ();
53 public void Constructor1 ()
55 CheckProperties (transform);
58 [Test] // ctor (Boolean)
59 public void Constructor2 ()
61 transform = new UnprotectedXmlDsigXsltTransform (true);
62 CheckProperties (transform);
63 transform = new UnprotectedXmlDsigXsltTransform (false);
64 CheckProperties (transform);
67 void CheckProperties (XmlDsigXsltTransform transform)
69 Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm, "Algorithm");
71 Type[] input = transform.InputTypes;
72 Assert.IsTrue ((input.Length == 3), "Input #");
73 // check presence of every supported input types
77 foreach (Type t in input) {
78 if (t.ToString () == "System.IO.Stream")
80 if (t.ToString () == "System.Xml.XmlDocument")
82 if (t.ToString () == "System.Xml.XmlNodeList")
85 Assert.IsTrue (istream, "Input Stream");
86 Assert.IsTrue (ixmldoc, "Input XmlDocument");
87 Assert.IsTrue (ixmlnl, "Input XmlNodeList");
89 Type[] output = transform.OutputTypes;
90 Assert.IsTrue ((output.Length == 1), "Output #");
91 // check presence of every supported output types
93 foreach (Type t in output) {
94 if (t.ToString () == "System.IO.Stream")
97 Assert.IsTrue (ostream, "Output Stream");
101 public void GetInnerXml ()
103 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
104 Assert.IsNull (xnl, "Default InnerXml");
107 private string Stream2Array (Stream s)
109 StringBuilder sb = new StringBuilder ();
110 int b = s.ReadByte ();
112 sb.Append (b.ToString("X2"));
115 return sb.ToString ();
121 [ExpectedException (typeof (ArgumentNullException))]
123 [ExpectedException (typeof (NullReferenceException))]
125 public void EmptyXslt ()
127 string test = "<Test>XmlDsigXsltTransform</Test>";
128 XmlDocument doc = new XmlDocument ();
131 transform.LoadInput (doc.ChildNodes);
132 Stream s = (Stream) transform.GetOutput ();
136 // Note that this is _valid_ as an "embedded stylesheet".
137 // (see XSLT spec 2.7)
138 public void EmbeddedStylesheet ()
140 string test = "<Test xsl:version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</Test>";
141 XmlDocument doc = new XmlDocument ();
144 transform.LoadInnerXml (doc.ChildNodes);
145 transform.LoadInput (doc);
146 Stream s = (Stream) transform.GetOutput ();
150 public void InvalidXslt ()
154 string test = "<xsl:element name='foo' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>XmlDsigXsltTransform</xsl:element>";
155 XmlDocument doc = new XmlDocument ();
158 transform.LoadInnerXml (doc.ChildNodes);
159 Stream s = (Stream) transform.GetOutput ();
162 catch (Exception e) {
163 // we must deal with an internal exception
164 result = (e.GetType ().ToString ().EndsWith ("XsltLoadException"));
167 catch (XsltCompileException) {
172 Assert.IsTrue (result, "Exception not thrown");
178 [ExpectedException (typeof (ArgumentNullException))]
180 [ExpectedException (typeof (NullReferenceException))]
182 public void OnlyInner ()
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 ();
193 transform.LoadInnerXml (doc.ChildNodes);
194 Stream s = (Stream) transform.GetOutput ();
195 string output = Stream2Array (s);
198 private XmlDocument GetXslDoc ()
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 ();
213 public void LoadInputAsXmlDocument ()
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);
223 public void LoadInputAsXmlNodeList ()
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);
233 public void LoadInputAsStream ()
235 XmlDocument doc = GetXslDoc ();
236 transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
237 MemoryStream ms = new MemoryStream ();
240 transform.LoadInput (ms);
241 Stream s = (Stream) transform.GetOutput ();
242 string output = Stream2Array (s);
245 protected void AreEqual (string msg, XmlNodeList expected, XmlNodeList actual)
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);
254 public void LoadInnerXml ()
256 XmlDocument doc = GetXslDoc ();
257 transform.LoadInnerXml (doc.DocumentElement.ChildNodes);
258 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
259 Assert.AreEqual (doc.DocumentElement.ChildNodes, xnl, "LoadInnerXml");
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);
273 public void UnsupportedInput ()
275 byte[] bad = { 0xBA, 0xD };
276 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
277 transform.LoadInput (bad);
281 [ExpectedException (typeof (ArgumentException))]
282 public void UnsupportedOutput ()
284 XmlDocument doc = new XmlDocument();
285 object o = transform.GetOutput (doc.GetType ());