Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SignatureTest.cs « System.Security.Cryptography.Xml « Test « System.Security « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ff34814c9c6c399b27cd7abc9f2f2e04e74b8e33 (plain)
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
//
// SignatureTest.cs - NUnit Test Cases for SignedXml
//
// Author:
//	Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;

using NUnit.Framework;

namespace MonoTests.System.Security.Cryptography.Xml {

	[TestFixture]
	public class SignatureTest : Assertion {

		protected Signature signature;

		[SetUp]
		protected void SetUp () 
		{
			signature = new Signature ();
		}

		[Test]
		[ExpectedException (typeof (CryptographicException))]
		public void Signature1 () 
		{
			// empty - missing SignedInfo
			XmlElement xel = signature.GetXml ();
		}

		[Test]
		[ExpectedException (typeof (CryptographicException))]
		public void Signature2 () 
		{
			SignedInfo info = new SignedInfo ();
			signature.SignedInfo = info;
			info.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
			signature.SignatureValue = new byte [128];
			// no reference element are present
			XmlElement xel = signature.GetXml ();
		}

		[Test]
		public void Load () 
		{
			string expected = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (expected);
			signature.LoadXml (doc.DocumentElement);
			string result = signature.GetXml ().OuterXml;
			AssertCrypto.AssertXmlEquals ("Load", expected, result);
		}

		[Test]
		[ExpectedException (typeof (CryptographicException))]
		public void LoadXmlMalformed1 ()
		{
			SignedXml s = new SignedXml ();
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml ("<root/>");
			s.LoadXml (doc.DocumentElement);
		}

		[Test]
		[ExpectedException (typeof (CryptographicException))]
		public void LoadXmlMalformed2 ()
		{
			SignedXml s = new SignedXml ();
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml ("<ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#'><foo/><bar/></ds:Signature>");
			s.LoadXml (doc.DocumentElement);
		}
	}
}