2 // XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 using NUnit.Framework;
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
18 namespace MonoTests.System.Security.Cryptography.Xml {
20 public class XmlDsigBase64TransformTest : TestCase {
22 public XmlDsigBase64TransformTest () : base ("System.Security.Cryptography.Xml.XmlDsigBase64Transform testsuite") {}
23 public XmlDsigBase64TransformTest (string name) : base (name) {}
25 protected XmlDsigBase64Transform transform;
27 protected override void SetUp ()
29 transform = new XmlDsigBase64Transform ();
32 protected override void TearDown () {}
34 public static ITest Suite {
36 return new TestSuite (typeof (XmlDsigBase64TransformTest));
40 public void TestProperties ()
42 AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
44 Type[] input = transform.InputTypes;
45 Assert ("Input #", (input.Length == 3));
46 // check presence of every supported input types
50 foreach (Type t in input) {
51 if (t.ToString () == "System.IO.Stream")
53 if (t.ToString () == "System.Xml.XmlDocument")
55 if (t.ToString () == "System.Xml.XmlNodeList")
58 Assert ("Input Stream", istream);
59 Assert ("Input XmlDocument", ixmldoc);
60 Assert ("Input XmlNodeList", ixmlnl);
62 Type[] output = transform.OutputTypes;
63 Assert ("Output #", (output.Length == 1));
64 // check presence of every supported output types
66 foreach (Type t in input) {
67 if (t.ToString () == "System.IO.Stream")
70 Assert ("Output Stream", ostream);
73 private string Stream2String (Stream s)
75 StringBuilder sb = new StringBuilder ();
76 int b = s.ReadByte ();
78 sb.Append (b.ToString("X2"));
81 return sb.ToString ();
84 private byte[] Stream2Array (Stream s)
86 string st = Stream2String (s);
87 byte[] array = new byte [st.Length / 2];
88 for (int i=0; i < array.Length; i++) {
89 string hex = st.Substring (i*2, 2);
90 array [i] = Convert.ToByte(hex, 16);
95 public void TestLoadInput ()
97 string base64 = "XmlDsigBase64Transform";
98 UTF8Encoding utf8 = new UTF8Encoding ();
99 byte[] base64array = utf8.GetBytes (base64);
100 string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
101 XmlDocument doc = new XmlDocument ();
104 // load as XmlDocument
105 transform.LoadInput (doc);
106 Stream s = (Stream) transform.GetOutput ();
107 byte[] output = Stream2Array (s);
108 AssertEquals("XmlDocument", base64, utf8.GetString (output));
110 // load as XmlNodeList
111 XmlNodeList xpath = doc.SelectNodes ("//.");
112 transform.LoadInput (xpath);
113 s = (Stream) transform.GetOutput ();
114 output = Stream2Array (s);
115 // works with MS ??? why does xpath return 3 nodes ???
116 // AssertEquals("XPathNodeList", base64, utf8.GetString (output));
118 // load as XmlNodeList
119 transform.LoadInput (doc.ChildNodes);
120 s = (Stream) transform.GetOutput ();
121 output = Stream2Array (s);
122 // FIXME: works with Mono ??? why doesn't this works with MS ???
123 AssertEquals("XmlChildNodes", base64, utf8.GetString (output));
126 MemoryStream ms = new MemoryStream ();
127 byte[] x = utf8.GetBytes (Convert.ToBase64String (base64array));
128 ms.Write (x, 0, x.Length);
130 transform.LoadInput (ms);
131 s = (Stream) transform.GetOutput ();
132 output = Stream2Array (s);
133 AssertEquals("MemoryStream", base64, utf8.GetString (output));
136 public void TestUnsupportedInput ()
138 byte[] bad = { 0xBA, 0xD };
139 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
140 transform.LoadInput (bad);
143 public void TestUnsupportedOutput ()
146 XmlDocument doc = new XmlDocument();
147 object o = transform.GetOutput (doc.GetType ());
148 Fail ("Expected ArgumentException but got none");
150 catch (ArgumentException) {
151 // this is what we expected
153 catch (Exception e) {
154 Fail ("Expected ArgumentException but got: " + e.ToString ());