New test.
[mono.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Security / SymmetricDecryptionKeyTest.cs
1 //
2 // SymmetricDecryptionKeyTest.cs - NUnit Test Cases for SymmetricDecryptionKey
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using Microsoft.Web.Services.Security;
12 using System;
13 using System.Security.Cryptography;
14 using System.Web.Services.Protocols;
15 using System.Xml;
16
17 namespace MonoTests.MS.Web.Services.Security {
18
19         [TestFixture]
20         public class SymmetricDecryptionKeyTest : Assertion {
21
22                 private void UnsupportedAlgorithm (string algo) 
23                 {
24                         SymmetricAlgorithm sa = SymmetricAlgorithm.Create (algo);
25                         try {
26                                 SymmetricDecryptionKey sdk = new SymmetricDecryptionKey (sa);
27                                 Fail (algo + " - Expected SecurityFault but got none");
28                         }
29                         catch (SoapHeaderException she) {
30                                 // this is expected (but not documented)
31                                 // SecurityFault isn't public so we catch it's ancestor
32                                 // worse you can create SymmetricEncryptionKey with those algorithms
33                                 if (she.ToString ().StartsWith ("Microsoft.Web.Services.Security.SecurityFault")) {
34                                         // this is expected
35                                 }
36                                 else
37                                         Fail ("Expected SecurityFault but got " + she.ToString ());
38                         }
39                         catch (Exception e) {
40                                 Fail (algo + " - Expected SecurityFault but got " + e.ToString ());
41                         }
42                 }
43
44                 [Test]
45                 public void UnsupportedAlgorithms () 
46                 {
47                         UnsupportedAlgorithm ("DES");
48                         UnsupportedAlgorithm ("RC2");
49                 }
50
51                 private void SupportedAlgorithm (string algo) 
52                 {
53                         SymmetricAlgorithm sa = SymmetricAlgorithm.Create (algo);
54                         SymmetricDecryptionKey sdk = new SymmetricDecryptionKey (sa);
55                 }
56
57                 [Test]
58                 public void SupportedAlgorithms () 
59                 {
60                         SupportedAlgorithm ("Rijndael");
61                         SupportedAlgorithm ("TripleDES");
62                 }
63
64                 [Test]
65                 public void NullAlgoConstructor () 
66                 {
67                         try {
68                                 SymmetricDecryptionKey sdk = new SymmetricDecryptionKey (null);
69                                 Fail ("Expected SecurityFault but got none");
70                         }
71                         catch (SoapHeaderException she) {
72                                 // this is expected (from WSE)
73                                 // should be ArgumentNullException
74                                 // SecurityFault isn't public so we catch it's ancestor
75                                 // worse you can create SymmetricEncryptionKey with those algorithms
76                                 if (she.ToString ().StartsWith ("Microsoft.Web.Services.Security.SecurityFault")) {
77                                         // this is expected
78                                 }
79                                 else
80                                         Fail ("Expected SecurityFault but got " + she.ToString ());
81                         }
82                         catch (Exception e) {
83                                 Fail ("Expected SecurityFault but got " + e.ToString ());
84                         }
85                 }
86
87                 [Test]
88                 public void AlgoWithKeyConstructor () 
89                 {
90                         SymmetricAlgorithm sa = SymmetricAlgorithm.Create ("TripleDES");
91                         byte[] key = new byte [32]; 
92                         SymmetricDecryptionKey sdk = new SymmetricDecryptionKey (sa, key);
93                         AssertNull ("Name", sdk.Name);
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentNullException))]
98                 public void Constructor_NullValue () 
99                 {
100                         SymmetricAlgorithm sa = SymmetricAlgorithm.Create ("TripleDES");
101                         SymmetricDecryptionKey sdk = null;
102                         sdk = new SymmetricDecryptionKey (sa, null);
103                 }
104
105                 [Test]
106                 public void Constructor_NullAlgorithm () 
107                 {
108                         SymmetricDecryptionKey sdk = null;
109                         byte[] key = new byte [32]; 
110                         try {
111                                 sdk = new SymmetricDecryptionKey (null, key);
112                                 Fail ("Expected SecurityFault but got none");
113                         }
114                         catch (SoapHeaderException she) {
115                                 // this is expected (from WSE)
116                                 // should be ArgumentNullException
117                                 // SecurityFault isn't public so we catch it's ancestor
118                                 // worse you can create SymmetricEncryptionKey with those algorithms
119                                 if (she.ToString ().StartsWith ("Microsoft.Web.Services.Security.SecurityFault")) {
120                                         // this is expected
121                                 }
122                                 else
123                                         Fail ("Expected SecurityFault but got " + she.ToString ());
124                         }
125                         catch (Exception e) {
126                                 Fail ("Expected SecurityFault but got " + e.ToString ());
127                         }
128                 }
129         }
130 }