2002-11-20 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigBase64TransformTest.cs
1 //
2 // XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.IO;
13 using System.Security.Cryptography;
14 using System.Security.Cryptography.Xml;
15 using System.Text;
16 using System.Xml;
17
18 namespace MonoTests.System.Security.Cryptography.Xml {
19
20 public class XmlDsigBase64TransformTest : TestCase {
21
22         public XmlDsigBase64TransformTest () : base ("System.Security.Cryptography.Xml.XmlDsigBase64Transform testsuite") {}
23         public XmlDsigBase64TransformTest (string name) : base (name) {}
24
25         protected XmlDsigBase64Transform transform;
26
27         protected override void SetUp () 
28         {
29                 transform = new XmlDsigBase64Transform ();
30         }
31
32         protected override void TearDown () {}
33
34         public static ITest Suite {
35                 get { 
36                         return new TestSuite (typeof (XmlDsigBase64TransformTest)); 
37                 }
38         }
39
40         public void TestProperties () 
41         {
42                 AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
43
44                 Type[] input = transform.InputTypes;
45                 Assert ("Input #", (input.Length == 3));
46                 // check presence of every supported input types
47                 bool istream = false;
48                 bool ixmldoc = false;
49                 bool ixmlnl = false;
50                 foreach (Type t in input) {
51                         if (t.ToString () == "System.IO.Stream")
52                                 istream = true;
53                         if (t.ToString () == "System.Xml.XmlDocument")
54                                 ixmldoc = true;
55                         if (t.ToString () == "System.Xml.XmlNodeList")
56                                 ixmlnl = true;
57                 }
58                 Assert ("Input Stream", istream);
59                 Assert ("Input XmlDocument", ixmldoc);
60                 Assert ("Input XmlNodeList", ixmlnl);
61
62                 Type[] output = transform.OutputTypes;
63                 Assert ("Output #", (output.Length == 1));
64                 // check presence of every supported output types
65                 bool ostream = false;
66                 foreach (Type t in input) {
67                         if (t.ToString () == "System.IO.Stream")
68                                 ostream = true;
69                 }
70                 Assert ("Output Stream", ostream);
71         }
72
73         private string Stream2String (Stream s) 
74         {
75                 StringBuilder sb = new StringBuilder ();
76                 int b = s.ReadByte ();
77                 while (b != -1) {
78                         sb.Append (b.ToString("X2"));
79                         b = s.ReadByte ();
80                 }
81                 return sb.ToString ();
82         }
83
84         private byte[] Stream2Array (Stream s) 
85         {
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);
91                 }
92                 return array;
93         }
94
95         public void TestLoadInput () 
96         {
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 ();
102                 doc.LoadXml (xml);
103
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));
109
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));
117
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));
124
125                 // load as Stream
126                 MemoryStream ms = new MemoryStream ();
127                 byte[] x = utf8.GetBytes (Convert.ToBase64String (base64array));
128                 ms.Write (x, 0, x.Length);
129                 ms.Position = 0;
130                 transform.LoadInput (ms);
131                 s = (Stream) transform.GetOutput ();
132                 output = Stream2Array (s);
133                 AssertEquals("MemoryStream", base64, utf8.GetString (output));
134         }
135
136         public void TestUnsupportedInput () 
137         {
138                 byte[] bad = { 0xBA, 0xD };
139                 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
140                 transform.LoadInput (bad);
141         }
142
143         public void TestUnsupportedOutput () 
144         {
145                 try {
146                         XmlDocument doc = new XmlDocument();
147                         object o = transform.GetOutput (doc.GetType ());
148                         Fail ("Expected ArgumentException but got none");
149                 }
150                 catch (ArgumentException) {
151                         // this is what we expected
152                 }
153                 catch (Exception e) {
154                         Fail ("Expected ArgumentException but got: " + e.ToString ());
155                 }
156         }
157 }
158
159 }