2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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 <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
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 using NUnit.Framework;
19
20 namespace MonoTests.System.Security.Cryptography.Xml {
21
22         // Note: GetInnerXml is protected in XmlDsigBase64Transform making it
23         // difficult to test properly. This class "open it up" :-)
24         public class UnprotectedXmlDsigBase64Transform : XmlDsigBase64Transform {
25
26                 public XmlNodeList UnprotectedGetInnerXml () {
27                         return base.GetInnerXml ();
28                 }
29         }
30
31         [TestFixture]
32         public class XmlDsigBase64TransformTest : Assertion {
33
34                 protected UnprotectedXmlDsigBase64Transform transform;
35
36                 [SetUp]
37                 protected void SetUp () 
38                 {
39                         transform = new UnprotectedXmlDsigBase64Transform ();
40                         Type t = typeof (XmlDsigBase64Transform);
41                 }
42
43                 [Test]
44                 public void Properties () 
45                 {
46                         AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
47
48                         Type[] input = transform.InputTypes;
49                         Assert ("Input #", (input.Length == 3));
50                         // check presence of every supported input types
51                         bool istream = false;
52                         bool ixmldoc = false;
53                         bool ixmlnl = false;
54                         foreach (Type t in input) {
55                                 if (t.ToString () == "System.IO.Stream")
56                                         istream = true;
57                                 if (t.ToString () == "System.Xml.XmlDocument")
58                                         ixmldoc = true;
59                                 if (t.ToString () == "System.Xml.XmlNodeList")
60                                         ixmlnl = true;
61                         }
62                         Assert ("Input Stream", istream);
63                         Assert ("Input XmlDocument", ixmldoc);
64                         Assert ("Input XmlNodeList", ixmlnl);
65
66                         Type[] output = transform.OutputTypes;
67                         Assert ("Output #", (output.Length == 1));
68                         // check presence of every supported output types
69                         bool ostream = false;
70                         foreach (Type t in input) {
71                                 if (t.ToString () == "System.IO.Stream")
72                                         ostream = true;
73                         }
74                         Assert ("Output Stream", ostream);
75                 }
76
77                 [Test]
78                 public void Types ()
79                 {
80                         Type [] input = transform.InputTypes;
81                         input [0] = null;
82                         input [1] = null;
83                         input [2] = null;
84                         // property does not return a clone
85                         foreach (Type t in transform.InputTypes) {
86                                 AssertNull (t);
87                         }
88                         // it's not a static array
89                         XmlDsigBase64Transform t2 = new XmlDsigBase64Transform ();
90                         foreach (Type t in t2.InputTypes) {
91                                 AssertNotNull (t);
92                         }
93                 }
94
95                 [Test]
96                 public void GetInnerXml () 
97                 {
98                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
99                         AssertNull ("Default InnerXml", xnl);
100                 }
101
102                 private string Stream2String (Stream s) 
103                 {
104                         StreamReader sr = new StreamReader (s);
105                         return sr.ReadToEnd ();
106                 }
107
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 };
110
111                 private XmlDocument GetDoc () 
112                 {
113                         string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
114                         XmlDocument doc = new XmlDocument ();
115                         doc.LoadXml (xml);
116                         return doc;
117                 }
118
119                 [Test]
120                 public void LoadInputAsXmlDocument () 
121                 {
122                         XmlDocument doc = GetDoc ();
123                         transform.LoadInput (doc);
124                         Stream s = (Stream) transform.GetOutput ();
125                         string output = Stream2String (s);
126                         AssertEquals("XmlDocument", base64, output);
127                 }
128
129                 [Test]
130                 public void LoadInputAsXmlNodeListFromXPath () 
131                 {
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);
139                 }
140
141                 [Test]
142                 public void LoadInputAsXmlNodeList () 
143                 {
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);
150                 }
151
152                 [Test]
153                 public void LoadInputAsStream () 
154                 {
155                         MemoryStream ms = new MemoryStream ();
156                         byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
157                         ms.Write (x, 0, x.Length);
158                         ms.Position = 0;
159                         transform.LoadInput (ms);
160                         Stream s = (Stream) transform.GetOutput ();
161                         string output = Stream2String (s);
162                         AssertEquals ("MemoryStream", base64, output);
163                 }
164
165                 [Test]
166                 public void LoadInputWithUnsupportedType () 
167                 {
168                         byte[] bad = { 0xBA, 0xD };
169                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
170                         transform.LoadInput (bad);
171                 }
172
173                 [Test]
174                 [ExpectedException (typeof (ArgumentException))]
175                 public void UnsupportedOutput () 
176                 {
177                         XmlDocument doc = new XmlDocument();
178                         object o = transform.GetOutput (doc.GetType ());
179                 }
180         }
181 }