2 // XmlDsigBase64TransformTest.cs - NUnit Test Cases for XmlDsigBase64Transform
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
13 using System
.Security
.Cryptography
;
14 using System
.Security
.Cryptography
.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 ();
32 public class XmlDsigBase64TransformTest
: Assertion
{
34 protected UnprotectedXmlDsigBase64Transform transform
;
37 protected void SetUp ()
39 transform
= new UnprotectedXmlDsigBase64Transform ();
40 Type t
= typeof (XmlDsigBase64Transform
);
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
54 foreach (Type t
in input
) {
55 if (t
.ToString () == "System.IO.Stream")
57 if (t
.ToString () == "System.Xml.XmlDocument")
59 if (t
.ToString () == "System.Xml.XmlNodeList")
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
70 foreach (Type t
in input
) {
71 if (t
.ToString () == "System.IO.Stream")
74 Assert ("Output Stream", ostream
);
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 ();
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
);
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
);
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
);
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
);
141 transform
.LoadInput (ms
);
142 Stream s
= (Stream
) transform
.GetOutput ();
143 string output
= Stream2String (s
);
144 AssertEquals ("MemoryStream", base64
, output
);
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
);
156 [ExpectedException (typeof (ArgumentException
))]
157 public void UnsupportedOutput ()
159 XmlDocument doc
= new XmlDocument();
160 object o
= transform
.GetOutput (doc
.GetType ());