**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigBase64TransformTest.cs
blob2238f0b598533ca7ee36e70b2ca3c0cff9245b70
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 //
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;
18 using NUnit.Framework;
20 namespace MonoTests.System.Security.Cryptography.Xml {
22 // Note: GetInnerXml is protected in XmlDsigBase64Transform making it
23 // difficult to test properly. This class "open it up" :-)
24 public class UnprotectedXmlDsigBase64Transform : XmlDsigBase64Transform {
26 public XmlNodeList UnprotectedGetInnerXml () {
27 return base.GetInnerXml ();
31 [TestFixture]
32 public class XmlDsigBase64TransformTest : Assertion {
34 protected UnprotectedXmlDsigBase64Transform transform;
36 [SetUp]
37 protected void SetUp ()
39 transform = new UnprotectedXmlDsigBase64Transform ();
40 Type t = typeof (XmlDsigBase64Transform);
43 [Test]
44 public void Properties ()
46 AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#base64", transform.Algorithm);
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;
62 Assert ("Input Stream", istream);
63 Assert ("Input XmlDocument", ixmldoc);
64 Assert ("Input XmlNodeList", ixmlnl);
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;
74 Assert ("Output Stream", ostream);
77 [Test]
78 public void GetInnerXml ()
80 XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
81 AssertNull ("Default InnerXml", xnl);
84 private string Stream2String (Stream s)
86 StreamReader sr = new StreamReader (s);
87 return sr.ReadToEnd ();
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 };
93 private XmlDocument GetDoc ()
95 string xml = "<Test>" + Convert.ToBase64String (base64array) + "</Test>";
96 XmlDocument doc = new XmlDocument ();
97 doc.LoadXml (xml);
98 return doc;
101 [Test]
102 public void LoadInputAsXmlDocument ()
104 XmlDocument doc = GetDoc ();
105 transform.LoadInput (doc);
106 Stream s = (Stream) transform.GetOutput ();
107 string output = Stream2String (s);
108 AssertEquals("XmlDocument", base64, output);
111 [Test]
112 public void LoadInputAsXmlNodeListFromXPath ()
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);
123 [Test]
124 public void LoadInputAsXmlNodeList ()
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);
134 [Test]
135 public void LoadInputAsStream ()
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);
147 [Test]
148 public void LoadInputWithUnsupportedType ()
150 byte[] bad = { 0xBA, 0xD };
151 // LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
152 transform.LoadInput (bad);
155 [Test]
156 [ExpectedException (typeof (ArgumentException))]
157 public void UnsupportedOutput ()
159 XmlDocument doc = new XmlDocument();
160 object o = transform.GetOutput (doc.GetType ());