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