2003-02-19 Sebastien Pouliot <spouliot@videotron.ca>
[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 (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Security.Cryptography;
13 using System.Security.Cryptography.Xml;
14 using System.Text;
15 using System.Xml;
16 using System.Xml.Xsl;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Security.Cryptography.Xml {
21
22         [TestFixture]
23         public class XmlDsigXsltTransformTest {
24
25                 protected XmlDsigXsltTransform transform;
26
27                 [SetUp]
28                 protected void SetUp () 
29                 {
30                         transform = new XmlDsigXsltTransform ();
31                 }
32
33                 [Test]
34                 public void Properties () 
35                 {
36                         Assertion.AssertEquals ("Algorithm", "http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm);
37
38                         Type[] input = transform.InputTypes;
39                         Assertion.Assert ("Input #", (input.Length == 3));
40                         // check presence of every supported input types
41                         bool istream = false;
42                         bool ixmldoc = false;
43                         bool ixmlnl = false;
44                         foreach (Type t in input) {
45                                 if (t.ToString () == "System.IO.Stream")
46                                         istream = true;
47                                 if (t.ToString () == "System.Xml.XmlDocument")
48                                         ixmldoc = true;
49                                 if (t.ToString () == "System.Xml.XmlNodeList")
50                                         ixmlnl = true;
51                         }
52                         Assertion.Assert ("Input Stream", istream);
53                         Assertion.Assert ("Input XmlDocument", ixmldoc);
54                         Assertion.Assert ("Input XmlNodeList", ixmlnl);
55
56                         Type[] output = transform.OutputTypes;
57                         Assertion.Assert ("Output #", (output.Length == 1));
58                         // check presence of every supported output types
59                         bool ostream = false;
60                         foreach (Type t in input) {
61                                 if (t.ToString () == "System.IO.Stream")
62                                         ostream = true;
63                         }
64                         Assertion.Assert ("Output Stream", ostream);
65                 }
66
67                 private string Stream2Array (Stream s) 
68                 {
69                         StringBuilder sb = new StringBuilder ();
70                         int b = s.ReadByte ();
71                         while (b != -1) {
72                                 sb.Append (b.ToString("X2"));
73                                 b = s.ReadByte ();
74                         }
75                         return sb.ToString ();
76                 }
77
78                 [Test]
79                 // can't use ExpectedException as it doesn't have a constructor with 0 parameters
80                 // [ExpectedException (typeof (XsltCompileException))]
81                 public void InvalidXslt () 
82                 {
83                         string test = "<Test>XmlDsigXsltTransform</Test>";
84                         XmlDocument doc = new XmlDocument ();
85                         doc.LoadXml (test);
86
87                         transform.LoadInnerXml (doc.ChildNodes);
88                         try {
89                                 Stream s = (Stream) transform.GetOutput ();
90                                 Assertion.Fail ("Expected XsltCompileException but got none");
91                         }
92                         catch (XsltCompileException) {
93                                 // expected
94                         }
95                         catch (Exception e) {
96                                 Assertion.Fail ("Expected XsltCompileException but got :" + e.ToString ());
97                         }
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (NullReferenceException))]
102                 public void OnlyInner () 
103                 {
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 ();
111                         doc.LoadXml (test);
112
113                         transform.LoadInnerXml (doc.ChildNodes);
114                         Stream s = (Stream) transform.GetOutput ();
115                         string output = Stream2Array (s);
116                 }
117
118                 private XmlDocument GetDoc () 
119                 {
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 ();
128                         doc.LoadXml (test);
129                         return doc;
130                 }
131
132                 [Test]
133                 [Ignore ("not working")]
134                 public void LoadInputAsXmlDocument () 
135                 {
136                         XmlDocument doc = GetDoc ();
137                         transform.LoadInput (doc);
138                         Stream s = (Stream) transform.GetOutput ();
139                         string output = Stream2Array (s);
140                 }
141
142                 [Test]
143                 [Ignore ("not working")]
144                 public void LoadInputAsXmlNodeList () 
145                 {
146                         XmlDocument doc = GetDoc ();
147                         transform.LoadInput (doc.ChildNodes);
148                         Stream s = (Stream) transform.GetOutput ();
149                         string output = Stream2Array (s);
150                 }
151
152                 [Test]
153                 [Ignore ("not working")]
154                 public void LoadInputAsStream () 
155                 {
156                         XmlDocument doc = GetDoc ();
157                         MemoryStream ms = new MemoryStream ();
158                         doc.Save (ms);
159                         ms.Position = 0;
160                         transform.LoadInput (ms);
161                         Stream s = (Stream) transform.GetOutput ();
162                         string output = Stream2Array (s);
163                 }
164
165                 protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual) 
166                 {
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);
170                         }
171                 }
172
173                 [Test]
174                 public void LoadInnerXml () 
175                 {
176                         string value = "<Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\" />";
177                         XmlDocument doc = new XmlDocument ();
178                         doc.LoadXml (value);
179
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)
183                 }
184
185                 [Test]
186                 [Ignore ("not working")]
187                 public void Load2 () 
188                 {
189                         XmlDocument doc = GetDoc ();
190                         transform.LoadInnerXml (doc.ChildNodes);
191                         transform.LoadInput (doc);
192                         Stream s = (Stream) transform.GetOutput ();
193                         string output = Stream2Array (s);
194                 }
195
196                 [Test]
197                 public void UnsupportedInput () 
198                 {
199                         byte[] bad = { 0xBA, 0xD };
200                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
201                         transform.LoadInput (bad);
202                 }
203
204                 [Test]
205                 [ExpectedException (typeof (ArgumentException))]
206                 public void UnsupportedOutput () 
207                 {
208                         XmlDocument doc = new XmlDocument();
209                         object o = transform.GetOutput (doc.GetType ());
210                 }
211         }
212 }