**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigEnvelopedSignatureTransformTest.cs
blob967bd47c2838baa156f795d2b285fef8ce96e123
1 //
2 // XmlDsigEnvelopedSignatureTransformTest.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2004 Novell Inc.
8 //
10 using System;
11 using System.IO;
12 using System.Security.Cryptography;
13 using System.Security.Cryptography.Xml;
14 using System.Text;
15 using System.Xml;
16 using System.Xml.Xsl;
18 using NUnit.Framework;
20 namespace MonoTests.System.Security.Cryptography.Xml {
22 // Note: GetInnerXml is protected in XmlDsigEnvelopedSignatureTransform making it
23 // difficult to test properly. This class "open it up" :-)
24 public class UnprotectedXmlDsigEnvelopedSignatureTransform : XmlDsigEnvelopedSignatureTransform {
26 public XmlNodeList UnprotectedGetInnerXml ()
28 return base.GetInnerXml ();
32 [TestFixture]
33 public class XmlDsigEnvelopedSignatureTransformTest : Assertion {
35 protected UnprotectedXmlDsigEnvelopedSignatureTransform transform;
37 [SetUp]
38 protected void SetUp ()
40 transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
43 [Test]
44 public void Properties ()
46 AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature", transform.Algorithm);
48 Type[] input = transform.InputTypes;
49 AssertEquals ("Input Length", 3, input.Length);
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.Xml.XmlDocument")
56 ixmldoc = true;
57 if (t.ToString () == "System.Xml.XmlNodeList")
58 ixmlnl = true;
60 Assert ("No Input Stream", !istream);
61 Assert ("Input XmlDocument", ixmldoc);
62 Assert ("Input XmlNodeList", ixmlnl);
64 Type[] output = transform.OutputTypes;
65 AssertEquals ("Output Length", 2, output.Length);
66 // check presence of every supported output types
67 bool oxmlnl = false;
68 bool oxmldoc = false;
69 foreach (Type t in output) {
70 if (t == null)
71 throw new InvalidOperationException ();
72 if (t.ToString () == "System.Xml.XmlNodeList")
73 oxmlnl = true;
74 if (t.ToString () == "System.Xml.XmlDocument")
75 oxmldoc = true;
77 Assert ("Output XmlNodeList", oxmlnl);
78 Assert ("Output XmlDocument", oxmldoc);
81 protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual)
83 for (int i=0; i < expected.Count; i++) {
84 if (expected [i].OuterXml != actual [i].OuterXml)
85 Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
89 [Test]
90 public void GetInnerXml ()
92 // Always returns null
93 AssertNull (transform.UnprotectedGetInnerXml ());
96 private XmlDocument GetDoc ()
98 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>";
99 string test = "<Envelope> " + dsig + " </Envelope>";
100 XmlDocument doc = new XmlDocument ();
101 doc.LoadXml (test);
102 return doc;
105 [Test]
106 public void LoadInputAsXmlDocument ()
108 XmlDocument doc = GetDoc ();
109 transform.LoadInput (doc);
110 object o = transform.GetOutput ();
111 AssertEquals ("EnvelopedSignature result", doc, o);
114 [Test]
115 public void LoadInputAsXmlNodeList ()
117 XmlDocument doc = GetDoc ();
118 transform.LoadInput (doc.ChildNodes);
119 XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
120 AssertEquals ("EnvelopedSignature result", doc.ChildNodes, xnl);