* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / SymmetricAlgorithm2Test.cs
1 //
2 // SymmetricAlgorithm2Test.cs -
3 //      Non generated NUnit Test Cases for SymmetricAlgorithm
4 //
5 // Author:
6 //      Sebastien Pouliot  <spouliot@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.IO;
33 using System.Security.Cryptography;
34 using System.Text;
35
36 namespace MonoTests.System.Security.Cryptography {
37
38         // SymmetricAlgorithm is a abstract class - so most of it's functionality wont
39         // be tested here (but will be in its descendants).
40
41         [TestFixture]
42         public class SymmetricAlgorithm2Test : Assertion {
43
44                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
45                 {
46                         AllTests.AssertEquals (msg, array1, array2);
47                 }
48
49                 [Test]
50                 public void KeySize_SameSize () 
51                 {
52                         using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create ()) {
53                                 // get a copy of the key
54                                 byte[] key = algo.Key;
55                                 int ks = algo.KeySize;
56                                 // set the key size
57                                 algo.KeySize = ks;
58                                 // did it change the key ? Yes!
59                                 Assert ("Key", BitConverter.ToString (key) != BitConverter.ToString (algo.Key));
60                         }
61                 }
62
63                 [Test]
64                 public void BlockSize_SameSize () 
65                 {
66                         using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create ()) {
67                                 // get a copy of the IV
68                                 byte[] iv = algo.IV;
69                                 int bs = algo.BlockSize;
70                                 // set the iv size
71                                 algo.BlockSize = bs;
72                                 // did it change the IV ? No!
73                                 Assert ("IV", BitConverter.ToString (iv) == BitConverter.ToString (algo.IV));
74                         }
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (CryptographicException))]
79                 public void InvalidBlockSize () 
80                 {
81                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
82                         algo.BlockSize = 255;
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (CryptographicException))]
87                 public void InvalidFeedbackSize () 
88                 {
89                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
90                         algo.FeedbackSize = algo.BlockSize + 1;
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (ArgumentNullException))]
95                 public void IV_Null () 
96                 {
97                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
98                         algo.IV = null;
99                 }
100
101                 [Test]
102 #if NET_2_0
103                 [ExpectedException (typeof (CryptographicException))]
104 #endif
105                 public void IV_None () 
106                 {
107                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
108                         algo.IV = new byte [0]; // e.g. stream ciphers
109                 }
110
111                 [Test]
112                 [ExpectedException (typeof (CryptographicException))]
113                 public void IV_TooBig () 
114                 {
115                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
116                         algo.IV = new byte [algo.BlockSize + 1];
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (ArgumentNullException))]
121                 public void Key_Null () 
122                 {
123                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
124                         algo.Key = null;
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (CryptographicException))]
129                 public void Key_WrongSize () 
130                 {
131                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
132                         algo.Key = new byte [255];
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (CryptographicException))]
137                 public void KeySize_WrongSize () 
138                 {
139                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
140                         int n = 0;
141                         while (algo.ValidKeySize (++n));
142                         algo.KeySize = n;
143                 }
144
145                 [Test]
146                 [ExpectedException (typeof (CryptographicException))]
147                 public void InvalidCipherMode () 
148                 {
149                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
150                         algo.Mode = (CipherMode) 255;
151                 }
152
153                 [Test]
154                 [ExpectedException (typeof (CryptographicException))]
155                 public void InvalidPaddingMode () 
156                 {
157                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
158                         algo.Padding = (PaddingMode) 255;
159                 }
160
161                 [Test]
162 #if NET_2_0
163                 [ExpectedException (typeof (CryptographicException))]
164 #endif
165                 public void FeedbackZero ()
166                 {
167                         // thanks to Yakk for the sample
168                         DES des = new DESCryptoServiceProvider();
169                         des.FeedbackSize = 0;
170                         des.Padding = PaddingMode.None;
171                         des.Mode = CipherMode.ECB;
172                         des.Key = new byte [8] { 8, 7, 6, 5, 4, 3, 2, 1 };
173
174                         ICryptoTransform enc = des.CreateEncryptor (); 
175                         byte[] response = new byte [16];
176                         byte[] challenge = new byte [16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
177
178                         enc.TransformBlock (challenge, 0, challenge.Length, response, 0);
179                         AssertEquals ("7A-64-CD-1B-4F-EE-B5-92-54-90-53-E9-83-71-A6-0C", BitConverter.ToString (response));
180                 }
181         }
182 }