* roottypes.cs: Rename from tree.cs.
[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 GetInnerXml () 
79                 {
80                         XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
81                         AssertNull ("Default InnerXml", xnl);
82                 }
83
84                 private string Stream2String (Stream s) 
85                 {
86                         StreamReader sr = new StreamReader (s);
87                         return sr.ReadToEnd ();
88                 }
89
90                 static private string base64 = "XmlDsigBase64Transform";
91                 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 };
92
93                 private XmlDocument GetDoc () 
94                 {
95                         string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
96                         XmlDocument doc = new XmlDocument ();
97                         doc.LoadXml (xml);
98                         return doc;
99                 }
100
101                 [Test]
102                 public void LoadInputAsXmlDocument () 
103                 {
104                         XmlDocument doc = GetDoc ();
105                         transform.LoadInput (doc);
106                         Stream s = (Stream) transform.GetOutput ();
107                         string output = Stream2String (s);
108                         AssertEquals("XmlDocument", base64, output);
109                 }
110
111                 [Test]
112                 public void LoadInputAsXmlNodeListFromXPath () 
113                 {
114                         XmlDocument doc = GetDoc ();
115                         XmlNodeList xpath = doc.SelectNodes ("//.");
116                         AssertEquals("XPathNodeList.Count", 3, xpath.Count);
117                         transform.LoadInput (xpath);
118                         Stream s = (Stream) transform.GetOutput ();
119                         string output = Stream2String (s);
120                         AssertEquals ("XPathNodeList", base64, output);
121                 }
122
123                 [Test]
124                 public void LoadInputAsXmlNodeList () 
125                 {
126                         XmlDocument doc = GetDoc ();
127                         transform.LoadInput (doc.ChildNodes);
128                         Stream s = (Stream) transform.GetOutput ();
129                         string output = Stream2String (s);
130                         // Note that ChildNodes does not contain the text node.
131                         AssertEquals ("XmlChildNodes", String.Empty, output);
132                 }
133
134                 [Test]
135                 public void LoadInputAsStream () 
136                 {
137                         MemoryStream ms = new MemoryStream ();
138                         byte[] x = Encoding.UTF8.GetBytes (Convert.ToBase64String (base64array));
139                         ms.Write (x, 0, x.Length);
140                         ms.Position = 0;
141                         transform.LoadInput (ms);
142                         Stream s = (Stream) transform.GetOutput ();
143                         string output = Stream2String (s);
144                         AssertEquals ("MemoryStream", base64, output);
145                 }
146
147                 [Test]
148                 public void LoadInputWithUnsupportedType () 
149                 {
150                         byte[] bad = { 0xBA, 0xD };
151                         // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
152                         transform.LoadInput (bad);
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ArgumentException))]
157                 public void UnsupportedOutput () 
158                 {
159                         XmlDocument doc = new XmlDocument();
160                         object o = transform.GetOutput (doc.GetType ());
161                 }
162         }
163 }