2 // XmlDsigXsltTransformTest.cs - NUnit Test Cases for XmlDsigXsltTransform
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
12 using System.Security.Cryptography;
13 using System.Security.Cryptography.Xml;
18 using NUnit.Framework;
20 namespace MonoTests.System.Security.Cryptography.Xml {
23 public class XmlDsigXsltTransformTest {
25 protected XmlDsigXsltTransform transform;
28 protected void SetUp ()
30 transform = new XmlDsigXsltTransform ();
34 public void Properties ()
36 Assertion.AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm);
38 Type[] input = transform.InputTypes;
39 Assertion.Assert ("Input #", (input.Length == 3));
40 // check presence of every supported input types
44 foreach (Type t in input) {
45 if (t.ToString () == "System.IO.Stream")
47 if (t.ToString () == "System.Xml.XmlDocument")
49 if (t.ToString () == "System.Xml.XmlNodeList")
52 Assertion.Assert ("Input Stream", istream);
53 Assertion.Assert ("Input XmlDocument", ixmldoc);
54 Assertion.Assert ("Input XmlNodeList", ixmlnl);
56 Type[] output = transform.OutputTypes;
57 Assertion.Assert ("Output #", (output.Length == 1));
58 // check presence of every supported output types
60 foreach (Type t in input) {
61 if (t.ToString () == "System.IO.Stream")
64 Assertion.Assert ("Output Stream", ostream);
67 private string Stream2Array (Stream s)
69 StringBuilder sb = new StringBuilder ();
70 int b = s.ReadByte ();
72 sb.Append (b.ToString("X2"));
75 return sb.ToString ();
79 // can't use ExpectedException as it doesn't have a constructor with 0 parameters
80 // [ExpectedException (typeof (XsltCompileException))]
81 public void InvalidXslt ()
83 string test = "<Test>XmlDsigXsltTransform</Test>";
84 XmlDocument doc = new XmlDocument ();
87 transform.LoadInnerXml (doc.ChildNodes);
89 Stream s = (Stream) transform.GetOutput ();
90 Assertion.Fail ("Expected XsltCompileException but got none");
92 catch (XsltCompileException) {
96 Assertion.Fail ("Expected XsltCompileException but got :" + e.ToString ());
101 [ExpectedException (typeof (NullReferenceException))]
102 public void OnlyInner ()
104 string test = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
105 test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
106 test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
107 test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
108 test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
109 test += "</table></body></html></xsl:template></xsl:stylesheet>";
110 XmlDocument doc = new XmlDocument ();
113 transform.LoadInnerXml (doc.ChildNodes);
114 Stream s = (Stream) transform.GetOutput ();
115 string output = Stream2Array (s);
118 private XmlDocument GetDoc ()
120 string test = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">";
121 test += "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/TR/xhtml1/strict\" version=\"1.0\">";
122 test += "<xsl:output encoding=\"UTF-8\" indent=\"no\" method=\"xml\" />";
123 test += "<xsl:template match=\"/\"><html><head><title>Notaries</title>";
124 test += "</head><body><table><xsl:for-each select=\"Notaries/Notary\">";
125 test += "<tr><th><xsl:value-of select=\"@name\" /></th></tr></xsl:for-each>";
126 test += "</table></body></html></xsl:template></xsl:stylesheet></Transform>";
127 XmlDocument doc = new XmlDocument ();
133 [Ignore ("not working")]
134 public void LoadInputAsXmlDocument ()
136 XmlDocument doc = GetDoc ();
137 transform.LoadInput (doc);
138 Stream s = (Stream) transform.GetOutput ();
139 string output = Stream2Array (s);
143 [Ignore ("not working")]
144 public void LoadInputAsXmlNodeList ()
146 XmlDocument doc = GetDoc ();
147 transform.LoadInput (doc.ChildNodes);
148 Stream s = (Stream) transform.GetOutput ();
149 string output = Stream2Array (s);
153 [Ignore ("not working")]
154 public void LoadInputAsStream ()
156 XmlDocument doc = GetDoc ();
157 MemoryStream ms = new MemoryStream ();
160 transform.LoadInput (ms);
161 Stream s = (Stream) transform.GetOutput ();
162 string output = Stream2Array (s);
165 protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
167 for (int i=0; i < expected.Count; i++) {
168 if (expected[i].OuterXml != actual[i].OuterXml)
169 Assertion.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
174 public void LoadInnerXml ()
176 string value = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" />";
177 XmlDocument doc = new XmlDocument ();
180 transform.LoadInnerXml (doc.ChildNodes);
181 // note: GetInnerXml is protected so we can't AssertEquals :-(
182 // unless we use reflection (making it a lot more complicated)
186 [Ignore ("not working")]
189 XmlDocument doc = GetDoc ();
190 transform.LoadInnerXml (doc.ChildNodes);
191 transform.LoadInput (doc);
192 Stream s = (Stream) transform.GetOutput ();
193 string output = Stream2Array (s);
197 public void UnsupportedInput ()
199 byte[] bad = { 0xBA, 0xD };
200 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
201 transform.LoadInput (bad);
205 [ExpectedException (typeof (ArgumentException))]
206 public void UnsupportedOutput ()
208 XmlDocument doc = new XmlDocument();
209 object o = transform.GetOutput (doc.GetType ());