2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography.Xml / RSAKeyValueTest.cs
1 //
2 // RSAKeyValueTest.cs - NUnit Test Cases for RSAKeyValue
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Security.Cryptography;
12 using System.Security.Cryptography.Xml;
13 using System.Xml;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Security.Cryptography.Xml {
18
19         [TestFixture]
20         public class RSAKeyValueTest {
21
22                 [Test]
23                 public void GeneratedKey () 
24                 {
25                         RSAKeyValue rsa1 = new RSAKeyValue ();
26                         Assert.IsNotNull (rsa1.Key, "Key");
27                         XmlElement xmlkey = rsa1.GetXml ();
28
29                         RSAKeyValue rsa2 = new RSAKeyValue ();
30                         rsa2.LoadXml (xmlkey);
31
32                         Assert.IsTrue ((rsa1.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml), "rsa1==rsa2");
33
34                         RSA key = rsa1.Key;
35                         RSAKeyValue rsa3 = new RSAKeyValue (key);
36                         Assert.IsTrue ((rsa3.GetXml ().OuterXml) == (rsa1.GetXml ().OuterXml), "rsa3==rsa1");
37                         Assert.IsTrue ((rsa3.GetXml ().OuterXml) == (rsa2.GetXml ().OuterXml), "rsa3==rsa2");
38                 }
39
40                 [Test]
41                 public void ImportKey () 
42                 {
43                         string rsaKey = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>ogZ1/O7iks9ncETqNxLDKoPvgrT4nFx1a3lOmpywEmgbc5+8vI5dSzReH4v0YrflY75rIJx13CYWMsaHfQ78GtXvaeshHlQ3lLTuSdYEJceKll/URlBoKQtOj5qYIVSFOIVGHv4Y/0lnLftOzIydem29KKH6lJQlJawBBssR12s=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue>";
44                         XmlDocument doc = new XmlDocument ();
45                         doc.LoadXml (rsaKey);
46
47                         RSAKeyValue rsa1 = new RSAKeyValue ();
48                         rsa1.LoadXml (doc.DocumentElement);
49
50                         string s = (rsa1.GetXml ().OuterXml);
51                         Assert.AreEqual (rsaKey, s, "RSA Key");
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof (ArgumentNullException))]
56                 public void InvalidValue1 () 
57                 {
58                         RSAKeyValue rsa = new RSAKeyValue ();
59                         rsa.LoadXml (null);
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (CryptographicException))]
64                 public void InvalidValue2 () 
65                 {
66                         string badKey = "<Test></Test>";
67                         XmlDocument doc = new XmlDocument ();
68                         doc.LoadXml (badKey);
69
70                         RSAKeyValue rsa = new RSAKeyValue ();
71                         rsa.LoadXml (doc.DocumentElement);
72                 }
73         }
74 }