[bcl] Remove more NET_2_0 checks from class libs
[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                 public UnprotectedXmlDsigEnvelopedSignatureTransform ()
26                 {
27                 }
28
29                 public UnprotectedXmlDsigEnvelopedSignatureTransform (bool includeComments)
30                         : base (includeComments)
31                 {
32                 }
33
34                 public XmlNodeList UnprotectedGetInnerXml () 
35                 {
36                         return base.GetInnerXml ();
37                 }
38         }
39
40         [TestFixture]
41         public class XmlDsigEnvelopedSignatureTransformTest {
42                 private UnprotectedXmlDsigEnvelopedSignatureTransform transform;
43
44                 [SetUp]
45                 public void SetUp ()
46                 {
47                         transform = new UnprotectedXmlDsigEnvelopedSignatureTransform ();
48                 }
49
50                 [Test] // ctor ()
51                 public void Constructor1 ()
52                 {
53                         CheckProperties (transform);
54                 }
55
56                 [Test] // ctor (Boolean)
57                 public void Constructor2 ()
58                 {
59                         transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (true);
60                         CheckProperties (transform);
61                         transform = new UnprotectedXmlDsigEnvelopedSignatureTransform (false);
62                         CheckProperties (transform);
63                 }
64
65                 void CheckProperties (XmlDsigEnvelopedSignatureTransform transform)
66                 {
67                         Assert.AreEqual ("http://www.w3.org/2000/09/xmldsig#enveloped-signature",
68                                 transform.Algorithm, "Algorithm");
69
70                         Type [] input = transform.InputTypes;
71                         Assert.AreEqual (3, input.Length, "Input Length");
72                         // check presence of every supported input types
73                         bool istream = false;
74                         bool ixmldoc = false;
75                         bool ixmlnl = false;
76                         foreach (Type t in input) {
77                                 if (t == typeof (XmlDocument))
78                                         ixmldoc = true;
79                                 if (t == typeof (XmlNodeList))
80                                         ixmlnl = true;
81                                 if (t == typeof (Stream))
82                                         istream = true;
83                         }
84                         Assert.IsTrue (istream, "Input Stream");
85                         Assert.IsTrue (ixmldoc, "Input XmlDocument");
86                         Assert.IsTrue (ixmlnl, "Input XmlNodeList");
87
88                         Type [] output = transform.OutputTypes;
89                         Assert.AreEqual (2, output.Length, "Output Length");
90                         // check presence of every supported output types
91                         bool oxmlnl = false;
92                         bool oxmldoc = false;
93                         foreach (Type t in output) {
94                                 if (t == typeof (XmlNodeList))
95                                         oxmlnl = true;
96                                 if (t == typeof (XmlDocument))
97                                         oxmldoc = true;
98                         }
99                         Assert.IsTrue (oxmlnl, "Output XmlNodeList");
100                         Assert.IsTrue (oxmldoc, "Output XmlDocument");
101                 }
102
103                 void AssertEquals (XmlNodeList expected, XmlNodeList actual, string msg)
104                 {
105                         for (int i = 0; i < expected.Count; i++) {
106                                 if (expected [i].OuterXml != actual [i].OuterXml)
107                                         Assert.Fail (msg + " [" + i + "] expected " + expected [i].OuterXml + " bug got " + actual [i].OuterXml);
108                         }
109                 }
110
111                 [Test]
112                 public void GetInnerXml () 
113                 {
114                         // Always returns null
115                         Assert.IsNull (transform.UnprotectedGetInnerXml ());
116                 }
117
118                 private XmlDocument GetDoc () 
119                 {
120                         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>";
121                         string test = "<Envelope> " + dsig + " </Envelope>";
122                         XmlDocument doc = new XmlDocument ();
123                         doc.LoadXml (test);
124                         return doc;
125                 }
126
127                 [Test]
128                 public void LoadInputAsXmlDocument () 
129                 {
130                         XmlDocument doc = GetDoc ();
131                         transform.LoadInput (doc);
132                         object o = transform.GetOutput ();
133                         Assert.AreEqual (doc, o, "EnvelopedSignature result");
134                 }
135
136                 [Test]
137                 public void LoadInputAsXmlNodeList () 
138                 {
139                         XmlDocument doc = GetDoc ();
140                         transform.LoadInput (doc.ChildNodes);
141                         XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
142                         AssertEquals (doc.ChildNodes, xnl, "EnvelopedSignature result");
143                 }
144         }
145 }