2008-05-25 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigBase64TransformTest.cs
index 89941a5b9e3934e61aa9a6a4b40ee196e7bd5a40..f553a44d92036c83bf1cc497ab8d2777bed7a49f 100644 (file)
@@ -2,12 +2,12 @@
 // XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot <sebastien@ximian.com>
 //
 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+// (C) 2004 Novell (http://www.novell.com)
 //
 
-using NUnit.Framework;
 using System;
 using System.IO;
 using System.Security.Cryptography;
@@ -15,145 +15,167 @@ using System.Security.Cryptography.Xml;
 using System.Text;
 using System.Xml;
 
+using NUnit.Framework;
+
 namespace MonoTests.System.Security.Cryptography.Xml {
 
-public class XmlDsigBase64TransformTest : TestCase {
+       // Note: GetInnerXml is protected in XmlDsigBase64Transform making it
+       // difficult to test properly. This class "open it up" :-)
+       public class UnprotectedXmlDsigBase64Transform : XmlDsigBase64Transform {
 
-       public XmlDsigBase64TransformTest () : base ("System.Security.Cryptography.Xml.XmlDsigBase64Transform testsuite") {}
-       public XmlDsigBase64TransformTest (string name) : base (name) {}
+               public XmlNodeList UnprotectedGetInnerXml () {
+                       return base.GetInnerXml ();
+               }
+       }
 
-       protected XmlDsigBase64Transform transform;
+       [TestFixture]
+       public class XmlDsigBase64TransformTest : Assertion {
 
-       protected override void SetUp () 
-       {
-               transform = new XmlDsigBase64Transform ();
-       }
+               protected UnprotectedXmlDsigBase64Transform transform;
 
-       protected override void TearDown () {}
+               [SetUp]
+               protected void SetUp () 
+               {
+                       transform = new UnprotectedXmlDsigBase64Transform ();
+                       Type t = typeof (XmlDsigBase64Transform);
+               }
 
-       public static ITest Suite {
-               get { 
-                       return new TestSuite (typeof (XmlDsigBase64TransformTest)); 
+               [Test]
+               public void Properties () 
+               {
+                       AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
+
+                       Type[] input = transform.InputTypes;
+                       Assert ("Input #", (input.Length == 3));
+                       // check presence of every supported input types
+                       bool istream = false;
+                       bool ixmldoc = false;
+                       bool ixmlnl = false;
+                       foreach (Type t in input) {
+                               if (t.ToString () == "System.IO.Stream")
+                                       istream = true;
+                               if (t.ToString () == "System.Xml.XmlDocument")
+                                       ixmldoc = true;
+                               if (t.ToString () == "System.Xml.XmlNodeList")
+                                       ixmlnl = true;
+                       }
+                       Assert ("Input Stream", istream);
+                       Assert ("Input XmlDocument", ixmldoc);
+                       Assert ("Input XmlNodeList", ixmlnl);
+
+                       Type[] output = transform.OutputTypes;
+                       Assert ("Output #", (output.Length == 1));
+                       // check presence of every supported output types
+                       bool ostream = false;
+                       foreach (Type t in input) {
+                               if (t.ToString () == "System.IO.Stream")
+                                       ostream = true;
+                       }
+                       Assert ("Output Stream", ostream);
                }
-       }
 
-       public void TestProperties () 
-       {
-               AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
-
-               Type[] input = transform.InputTypes;
-               Assert ("Input #", (input.Length == 3));
-               // check presence of every supported input types
-               bool istream = false;
-               bool ixmldoc = false;
-               bool ixmlnl = false;
-               foreach (Type t in input) {
-                       if (t.ToString () == "System.IO.Stream")
-                               istream = true;
-                       if (t.ToString () == "System.Xml.XmlDocument")
-                               ixmldoc = true;
-                       if (t.ToString () == "System.Xml.XmlNodeList")
-                               ixmlnl = true;
+               [Test]
+               public void Types ()
+               {
+                       Type [] input = transform.InputTypes;
+                       input [0] = null;
+                       input [1] = null;
+                       input [2] = null;
+                       // property does not return a clone
+                       foreach (Type t in transform.InputTypes) {
+                               AssertNull (t);
+                       }
+                       // it's not a static array
+                       XmlDsigBase64Transform t2 = new XmlDsigBase64Transform ();
+                       foreach (Type t in t2.InputTypes) {
+                               AssertNotNull (t);
+                       }
                }
-               Assert ("Input Stream", istream);
-               Assert ("Input XmlDocument", ixmldoc);
-               Assert ("Input XmlNodeList", ixmlnl);
-
-               Type[] output = transform.OutputTypes;
-               Assert ("Output #", (output.Length == 1));
-               // check presence of every supported output types
-               bool ostream = false;
-               foreach (Type t in input) {
-                       if (t.ToString () == "System.IO.Stream")
-                               ostream = true;
+
+               [Test]
+               public void GetInnerXml () 
+               {
+                       XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
+                       AssertNull ("Default InnerXml", xnl);
                }
-               Assert ("Output Stream", ostream);
-       }
 
-       private string Stream2String (Stream s) 
-       {
-               StringBuilder sb = new StringBuilder ();
-               int b = s.ReadByte ();
-               while (b != -1) {
-                       sb.Append (b.ToString("X2"));
-                       b = s.ReadByte ();
+               private string Stream2String (Stream s) 
+               {
+                       StreamReader sr = new StreamReader (s);
+                       return sr.ReadToEnd ();
                }
-               return sb.ToString ();
-       }
 
-       private byte[] Stream2Array (Stream s) 
-       {
-               string st = Stream2String (s);
-               byte[] array = new byte [st.Length / 2];
-               for (int i=0; i < array.Length; i++) {
-                       string hex = st.Substring (i*2, 2);
-                       array [i] = Convert.ToByte(hex, 16);
+               static private string base64 = "XmlDsigBase64Transform";
+               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 };
+
+               private XmlDocument GetDoc () 
+               {
+                       string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
+                       XmlDocument doc = new XmlDocument ();
+                       doc.LoadXml (xml);
+                       return doc;
                }
-               return array;
-       }
 
-       public void TestLoadInput () 
-       {
-               string base64 = "XmlDsigBase64Transform";
-               UTF8Encoding utf8 = new UTF8Encoding ();
-               byte[] base64array = utf8.GetBytes (base64);
-               string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
-               XmlDocument doc = new XmlDocument ();
-               doc.LoadXml (xml);
-
-               // load as XmlDocument
-               transform.LoadInput (doc);
-               Stream s = (Stream) transform.GetOutput ();
-               byte[] output = Stream2Array (s);
-               AssertEquals("XmlDocument", base64, utf8.GetString (output));
-
-               // load as XmlNodeList
-               XmlNodeList xpath = doc.SelectNodes ("//.");
-               transform.LoadInput (xpath);
-               s = (Stream) transform.GetOutput ();
-               output = Stream2Array (s);
-               // works with MS ??? why does xpath return 3 nodes ???
-               // AssertEquals("XPathNodeList", base64, utf8.GetString (output));
-
-               // load as XmlNodeList 
-               transform.LoadInput (doc.ChildNodes);
-               s = (Stream) transform.GetOutput ();
-               output = Stream2Array (s);
-               // FIXME: works with Mono ??? why doesn't this works with MS ???
-               AssertEquals("XmlChildNodes", base64, utf8.GetString (output));
-
-               // load as Stream
-               MemoryStream ms = new MemoryStream ();
-               byte[] x = utf8.GetBytes (Convert.ToBase64String (base64array));
-               ms.Write (x, 0, x.Length);
-               ms.Position = 0;
-               transform.LoadInput (ms);
-               s = (Stream) transform.GetOutput ();
-               output = Stream2Array (s);
-               AssertEquals("MemoryStream", base64, utf8.GetString (output));
-       }
+               [Test]
+               public void LoadInputAsXmlDocument () 
+               {
+                       XmlDocument doc = GetDoc ();
+                       transform.LoadInput (doc);
+                       Stream s = (Stream) transform.GetOutput ();
+                       string output = Stream2String (s);
+                       AssertEquals("XmlDocument", base64, output);
+               }
 
-       public void TestUnsupportedInput () 
-       {
-               byte[] bad = { 0xBA, 0xD };
-               // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
-               transform.LoadInput (bad);
-       }
+               [Test]
+               public void LoadInputAsXmlNodeListFromXPath () 
+               {
+                       XmlDocument doc = GetDoc ();
+                       XmlNodeList xpath = doc.SelectNodes ("//.");
+                       AssertEquals("XPathNodeList.Count", 3, xpath.Count);
+                       transform.LoadInput (xpath);
+                       Stream s = (Stream) transform.GetOutput ();
+                       string output = Stream2String (s);
+                       AssertEquals ("XPathNodeList", base64, output);
+               }
 
-       public void TestUnsupportedOutput () 
-       {
-               try {
-                       XmlDocument doc = new XmlDocument();
-                       object o = transform.GetOutput (doc.GetType ());
-                       Fail ("Expected ArgumentException but got none");
+               [Test]
+               public void LoadInputAsXmlNodeList () 
+               {
+                       XmlDocument doc = GetDoc ();
+                       transform.LoadInput (doc.ChildNodes);
+                       Stream s = (Stream) transform.GetOutput ();
+                       string output = Stream2String (s);
+                       // Note that ChildNodes does not contain the text node.
+                       AssertEquals ("XmlChildNodes", String.Empty, output);
                }
-               catch (ArgumentException) {
-                       // this is what we expected
+
+               [Test]
+               public void LoadInputAsStream () 
+               {
+                       MemoryStream ms = new MemoryStream ();
+                       byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
+                       ms.Write (x, 0, x.Length);
+                       ms.Position = 0;
+                       transform.LoadInput (ms);
+                       Stream s = (Stream) transform.GetOutput ();
+                       string output = Stream2String (s);
+                       AssertEquals ("MemoryStream", base64, output);
                }
-               catch (Exception e) {
-                       Fail ("Expected ArgumentException but got: " + e.ToString ());
+
+               [Test]
+               public void LoadInputWithUnsupportedType () 
+               {
+                       byte[] bad = { 0xBA, 0xD };
+                       // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
+                       transform.LoadInput (bad);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void UnsupportedOutput () 
+               {
+                       XmlDocument doc = new XmlDocument();
+                       object o = transform.GetOutput (doc.GetType ());
                }
        }
 }
-
-}
\ No newline at end of file