2007-12-18 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / XmlDsigEnvelopedSignatureTransformTest.cs
1 //
2 // XmlDsigEnvelopedSignatureTransformTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2004 Novell Inc.
8 //
9
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;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Security.Cryptography.Xml {
21
22         // Note: GetInnerXml is protected in XmlDsigEnvelopedSignatureTransform making it
23         // difficult to test properly. This class "open it up" :-)
24         public class UnprotectedXmlDsigEnvelopedSignatureTransform : XmlDsigEnvelopedSignatureTransform {
25
26                 public XmlNodeList UnprotectedGetInnerXml () 
27                 {
28                         return base.GetInnerXml ();
29                 }
30         }
31
32         [TestFixture]
33         public class XmlDsigEnvelopedSignatureTransformTest : Assertion {
34
35                 protected UnprotectedXmlDsigEnvelopedSignatureTransform transform;
36
37                 [SetUp]
38                 protected void SetUp () 
39                 {
40                         transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
41                 }
42
43                 [Test]
44                 public void Properties () 
45                 {
46                         AssertEquals ("Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature", transform.Algorithm);
47
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;
59                         }
60                         Assert ("No Input Stream", !istream);
61                         Assert ("Input XmlDocument", ixmldoc);
62                         Assert ("Input XmlNodeList", ixmlnl);
63
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;
76                         }
77                         Assert ("Output XmlNodeList", oxmlnl);
78                         Assert ("Output XmlDocument", oxmldoc);
79                 }
80
81                 protected void AssertEquals (string msg, XmlNodeList expected, XmlNodeList actual) 
82                 {
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);
86                         }
87                 }
88
89                 [Test]
90                 public void GetInnerXml () 
91                 {
92                         // Always returns null
93                         AssertNull (transform.UnprotectedGetInnerXml ());
94                 }
95
96                 private XmlDocument GetDoc () 
97                 {
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;
103                 }
104
105                 [Test]
106                 public void LoadInputAsXmlDocument () 
107                 {
108                         XmlDocument doc = GetDoc ();
109                         transform.LoadInput (doc);
110                         object o = transform.GetOutput ();
111                         AssertEquals ("EnvelopedSignature result", doc, o);
112                 }
113
114                 [Test]
115                 public void LoadInputAsXmlNodeList () 
116                 {
117                         XmlDocument doc = GetDoc ();
118                         transform.LoadInput (doc.ChildNodes);
119                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
120                         AssertEquals ("EnvelopedSignature result", doc.ChildNodes, xnl);
121                 }
122         }
123 }