2 // XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
18 using NUnit.Framework;
20 namespace MonoTests.System.Security.Cryptography.Xml {
22 // Note: GetInnerXml is protected in XmlDsigBase64Transform making it
23 // difficult to test properly. This class "open it up" :-)
24 public class UnprotectedXmlDsigBase64Transform : XmlDsigBase64Transform {
26 public XmlNodeList UnprotectedGetInnerXml () {
27 return base.GetInnerXml ();
32 public class XmlDsigBase64TransformTest : Assertion {
34 protected UnprotectedXmlDsigBase64Transform transform;
37 protected void SetUp ()
39 transform = new UnprotectedXmlDsigBase64Transform ();
40 Type t = typeof (XmlDsigBase64Transform);
44 public void Properties ()
46 AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
48 Type[] input = transform.InputTypes;
49 Assert ("Input #", (input.Length == 3));
50 // check presence of every supported input types
54 foreach (Type t in input) {
55 if (t.ToString () == "System.IO.Stream")
57 if (t.ToString () == "System.Xml.XmlDocument")
59 if (t.ToString () == "System.Xml.XmlNodeList")
62 Assert ("Input Stream", istream);
63 Assert ("Input XmlDocument", ixmldoc);
64 Assert ("Input XmlNodeList", ixmlnl);
66 Type[] output = transform.OutputTypes;
67 Assert ("Output #", (output.Length == 1));
68 // check presence of every supported output types
70 foreach (Type t in input) {
71 if (t.ToString () == "System.IO.Stream")
74 Assert ("Output Stream", ostream);
80 Type [] input = transform.InputTypes;
84 // property does not return a clone
85 foreach (Type t in transform.InputTypes) {
88 // it's not a static array
89 XmlDsigBase64Transform t2 = new XmlDsigBase64Transform ();
90 foreach (Type t in t2.InputTypes) {
96 public void GetInnerXml ()
98 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
99 AssertNull ("Default InnerXml", xnl);
102 private string Stream2String (Stream s)
104 StreamReader sr = new StreamReader (s);
105 return sr.ReadToEnd ();
108 static private string base64 = "XmlDsigBase64Transform";
109 static private byte[] base64array = { 0x58, 0x6D, 0x6C, 0x44, 0x73, 0x69, 0x67, 0x42, 0x61, 0x73, 0x65, 0x36, 0x34, 0x54, 0x72, 0x61, 0x6E, 0x73, 0x66, 0x6F, 0x72, 0x6D };
111 private XmlDocument GetDoc ()
113 string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
114 XmlDocument doc = new XmlDocument ();
120 public void LoadInputAsXmlDocument ()
122 XmlDocument doc = GetDoc ();
123 transform.LoadInput (doc);
124 Stream s = (Stream) transform.GetOutput ();
125 string output = Stream2String (s);
126 AssertEquals("XmlDocument", base64, output);
130 public void LoadInputAsXmlNodeListFromXPath ()
132 XmlDocument doc = GetDoc ();
133 XmlNodeList xpath = doc.SelectNodes ("//.");
134 AssertEquals("XPathNodeList.Count", 3, xpath.Count);
135 transform.LoadInput (xpath);
136 Stream s = (Stream) transform.GetOutput ();
137 string output = Stream2String (s);
138 AssertEquals ("XPathNodeList", base64, output);
142 public void LoadInputAsXmlNodeList ()
144 XmlDocument doc = GetDoc ();
145 transform.LoadInput (doc.ChildNodes);
146 Stream s = (Stream) transform.GetOutput ();
147 string output = Stream2String (s);
148 // Note that ChildNodes does not contain the text node.
149 AssertEquals ("XmlChildNodes", String.Empty, output);
153 public void LoadInputAsStream ()
155 MemoryStream ms = new MemoryStream ();
156 byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
157 ms.Write (x, 0, x.Length);
159 transform.LoadInput (ms);
160 Stream s = (Stream) transform.GetOutput ();
161 string output = Stream2String (s);
162 AssertEquals ("MemoryStream", base64, output);
166 public void LoadInputWithUnsupportedType ()
168 byte[] bad = { 0xBA, 0xD };
169 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
170 transform.LoadInput (bad);
174 [ExpectedException (typeof (ArgumentException))]
175 public void UnsupportedOutput ()
177 XmlDocument doc = new XmlDocument();
178 object o = transform.GetOutput (doc.GetType ());