1 //
2 // XmlDsigEnvelopedSignatureTransformTest.cs
3 //
4 // Author:
5 //	Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2004 Novell Inc.
8 //
9 #if !MOBILE
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 using System.Xml.Xsl;
18 
19 using NUnit.Framework;
20 
21 namespace MonoTests.System.Security.Cryptography.Xml {
22 
23 	// Note: GetInnerXml is protected in XmlDsigEnvelopedSignatureTransform making it
24 	// difficult to test properly. This class "open it up" :-)
25 	public class UnprotectedXmlDsigEnvelopedSignatureTransform : XmlDsigEnvelopedSignatureTransform {
UnprotectedXmlDsigEnvelopedSignatureTransform()26 		public UnprotectedXmlDsigEnvelopedSignatureTransform ()
27 		{
28 		}
29 
UnprotectedXmlDsigEnvelopedSignatureTransform(bool includeComments)30 		public UnprotectedXmlDsigEnvelopedSignatureTransform (bool includeComments)
31 			: base (includeComments)
32 		{
33 		}
34 
UnprotectedGetInnerXml()35 		public XmlNodeList UnprotectedGetInnerXml ()
36 		{
37 			return base.GetInnerXml ();
38 		}
39 	}
40 
41 	[TestFixture]
42 	public class XmlDsigEnvelopedSignatureTransformTest {
43 		private UnprotectedXmlDsigEnvelopedSignatureTransform transform;
44 
45 		[SetUp]
SetUp()46 		public void SetUp ()
47 		{
48 			transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
49 		}
50 
51 		[Test] // ctor ()
Constructor1()52 		public void Constructor1 ()
53 		{
54 			CheckProperties (transform);
55 		}
56 
57 		[Test] // ctor (Boolean)
Constructor2()58 		public void Constructor2 ()
59 		{
60 			transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (true);
61 			CheckProperties (transform);
62 			transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (false);
63 			CheckProperties (transform);
64 		}
65 
CheckProperties(XmlDsigEnvelopedSignatureTransform transform)66 		void CheckProperties (XmlDsigEnvelopedSignatureTransform transform)
67 		{
68 			Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#enveloped-signature",
69 				transform.Algorithm, "Algorithm");
70 
71 			Type [] input = transform.InputTypes;
72 			Assert.AreEqual (3, input.Length, "Input Length");
73 			// check presence of every supported input types
74 			bool istream = false;
75 			bool ixmldoc = false;
76 			bool ixmlnl = false;
77 			foreach (Type t in input) {
78 				if (t == typeof (XmlDocument))
79 					ixmldoc = true;
80 				if (t == typeof (XmlNodeList))
81 					ixmlnl = true;
82 				if (t == typeof (Stream))
83 					istream = true;
84 			}
85 			Assert.IsTrue (istream, "Input Stream");
86 			Assert.IsTrue (ixmldoc, "Input XmlDocument");
87 			Assert.IsTrue (ixmlnl, "Input XmlNodeList");
88 
89 			Type [] output = transform.OutputTypes;
90 			Assert.AreEqual (2, output.Length, "Output Length");
91 			// check presence of every supported output types
92 			bool oxmlnl = false;
93 			bool oxmldoc = false;
94 			foreach (Type t in output) {
95 				if (t == typeof (XmlNodeList))
96 					oxmlnl = true;
97 				if (t == typeof (XmlDocument))
98 					oxmldoc = true;
99 			}
100 			Assert.IsTrue (oxmlnl, "Output XmlNodeList");
101 			Assert.IsTrue (oxmldoc, "Output XmlDocument");
102 		}
103 
AssertEquals(XmlNodeList expected, XmlNodeList actual, string msg)104 		void AssertEquals (XmlNodeList expected, XmlNodeList actual, string msg)
105 		{
106 			for (int i = 0; i < expected.Count; i++) {
107 				if (expected [i].OuterXml != actual [i].OuterXml)
108 					Assert.Fail (msg + " [" + i + "] expected " + expected [i].OuterXml + " bug got " + actual [i].OuterXml);
109 			}
110 		}
111 
112 		[Test]
GetInnerXml()113 		public void GetInnerXml ()
114 		{
115 			// Always returns null
116 			Assert.IsNull (transform.UnprotectedGetInnerXml ());
117 		}
118 
GetDoc()119 		private XmlDocument GetDoc ()
120 		{
121 			string dsig = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" /><Reference URI=\"\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" /></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>fdy6S2NLpnT4fMdokUHSHsmpcvo=</DigestValue></Reference></Signature>";
122 			string test = "<Envelope> " + dsig + " </Envelope>";
123 			XmlDocument doc = new XmlDocument ();
124 			doc.LoadXml (test);
125 			return doc;
126 		}
127 
128 		[Test]
LoadInputAsXmlDocument()129 		public void LoadInputAsXmlDocument ()
130 		{
131 			XmlDocument doc = GetDoc ();
132 			transform.LoadInput (doc);
133 			object o = transform.GetOutput ();
134 			Assert.AreEqual (doc, o, "EnvelopedSignature result");
135 		}
136 
137 		[Test]
LoadInputAsXmlNodeList()138 		public void LoadInputAsXmlNodeList ()
139 		{
140 			XmlDocument doc = GetDoc ();
141 			transform.LoadInput (doc.ChildNodes);
142 			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
143 			AssertEquals (doc.ChildNodes, xnl, "EnvelopedSignature result");
144 		}
145 	}
146 }
147 #endif
148